2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
46 #include "gromacs/math/utilities.h"
47 #include "gromacs/math/vec.h"
48 #include "types/commrec.h"
52 #include "nonbonded.h"
57 #include "gromacs/topology/mtop_util.h"
62 #include "gromacs/pbcutil/ishift.h"
63 #include "gromacs/pbcutil/pbc.h"
64 #include "gromacs/utility/fatalerror.h"
65 #include "gromacs/utility/smalloc.h"
68 * E X C L U S I O N H A N D L I N G
72 static void SETEXCL_(t_excl e[], atom_id i, atom_id j)
76 static void RMEXCL_(t_excl e[], atom_id i, atom_id j)
78 e[j] = e[j] & ~(1<<i);
80 static gmx_bool ISEXCL_(t_excl e[], atom_id i, atom_id j)
82 return (gmx_bool)(e[j] & (1<<i));
84 static gmx_bool NOTEXCL_(t_excl e[], atom_id i, atom_id j)
86 return !(ISEXCL(e, i, j));
89 #define SETEXCL(e, i, j) (e)[((atom_id) (j))] |= (1<<((atom_id) (i)))
90 #define RMEXCL(e, i, j) (e)[((atom_id) (j))] &= (~(1<<((atom_id) (i))))
91 #define ISEXCL(e, i, j) (gmx_bool) ((e)[((atom_id) (j))] & (1<<((atom_id) (i))))
92 #define NOTEXCL(e, i, j) !(ISEXCL(e, i, j))
96 round_up_to_simd_width(int length, int simd_width)
98 int offset, newlength;
100 offset = (simd_width > 0) ? length % simd_width : 0;
102 return (offset == 0) ? length : length-offset+simd_width;
104 /************************************************
106 * U T I L I T I E S F O R N S
108 ************************************************/
110 void reallocate_nblist(t_nblist *nl)
114 fprintf(debug, "reallocating neigborlist (ielec=%d, ivdw=%d, igeometry=%d, type=%d), maxnri=%d\n",
115 nl->ielec, nl->ivdw, nl->igeometry, nl->type, nl->maxnri);
117 srenew(nl->iinr, nl->maxnri);
118 if (nl->igeometry == GMX_NBLIST_GEOMETRY_CG_CG)
120 srenew(nl->iinr_end, nl->maxnri);
122 srenew(nl->gid, nl->maxnri);
123 srenew(nl->shift, nl->maxnri);
124 srenew(nl->jindex, nl->maxnri+1);
128 static void init_nblist(FILE *log, t_nblist *nl_sr, t_nblist *nl_lr,
129 int maxsr, int maxlr,
130 int ivdw, int ivdwmod,
131 int ielec, int ielecmod,
132 int igeometry, int type)
138 for (i = 0; (i < 2); i++)
140 nl = (i == 0) ? nl_sr : nl_lr;
141 homenr = (i == 0) ? maxsr : maxlr;
149 /* Set coul/vdw in neighborlist, and for the normal loops we determine
150 * an index of which one to call.
153 nl->ivdwmod = ivdwmod;
155 nl->ielecmod = ielecmod;
157 nl->igeometry = igeometry;
159 if (nl->type == GMX_NBLIST_INTERACTION_FREE_ENERGY)
161 nl->igeometry = GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE;
164 /* This will also set the simd_padding_width field */
165 gmx_nonbonded_set_kernel_pointers( (i == 0) ? log : NULL, nl);
167 /* maxnri is influenced by the number of shifts (maximum is 8)
168 * and the number of energy groups.
169 * If it is not enough, nl memory will be reallocated during the run.
170 * 4 seems to be a reasonable factor, which only causes reallocation
171 * during runs with tiny and many energygroups.
173 nl->maxnri = homenr*4;
183 reallocate_nblist(nl);
188 fprintf(debug, "Initiating neighbourlist (ielec=%d, ivdw=%d, type=%d) for %s interactions,\nwith %d SR, %d LR atoms.\n",
189 nl->ielec, nl->ivdw, nl->type, gmx_nblist_geometry_names[nl->igeometry], maxsr, maxlr);
194 void init_neighbor_list(FILE *log, t_forcerec *fr, int homenr)
196 /* Make maxlr tunable! (does not seem to be a big difference though)
197 * This parameter determines the number of i particles in a long range
198 * neighbourlist. Too few means many function calls, too many means
201 int maxsr, maxsr_wat, maxlr, maxlr_wat;
202 int ielec, ielecf, ivdw, ielecmod, ielecmodf, ivdwmod, type;
204 int igeometry_def, igeometry_w, igeometry_ww;
208 /* maxsr = homenr-fr->nWatMol*3; */
213 gmx_fatal(FARGS, "%s, %d: Negative number of short range atoms.\n"
214 "Call your Gromacs dealer for assistance.", __FILE__, __LINE__);
216 /* This is just for initial allocation, so we do not reallocate
217 * all the nlist arrays many times in a row.
218 * The numbers seem very accurate, but they are uncritical.
220 maxsr_wat = min(fr->nWatMol, (homenr+2)/3);
224 maxlr_wat = min(maxsr_wat, maxlr);
228 maxlr = maxlr_wat = 0;
231 /* Determine the values for ielec/ivdw. */
232 ielec = fr->nbkernel_elec_interaction;
233 ivdw = fr->nbkernel_vdw_interaction;
234 ielecmod = fr->nbkernel_elec_modifier;
235 ivdwmod = fr->nbkernel_vdw_modifier;
236 type = GMX_NBLIST_INTERACTION_STANDARD;
238 fr->ns.bCGlist = (getenv("GMX_NBLISTCG") != 0);
241 igeometry_def = GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE;
245 igeometry_def = GMX_NBLIST_GEOMETRY_CG_CG;
248 fprintf(log, "\nUsing charge-group - charge-group neighbor lists and kernels\n\n");
252 if (fr->solvent_opt == esolTIP4P)
254 igeometry_w = GMX_NBLIST_GEOMETRY_WATER4_PARTICLE;
255 igeometry_ww = GMX_NBLIST_GEOMETRY_WATER4_WATER4;
259 igeometry_w = GMX_NBLIST_GEOMETRY_WATER3_PARTICLE;
260 igeometry_ww = GMX_NBLIST_GEOMETRY_WATER3_WATER3;
263 for (i = 0; i < fr->nnblists; i++)
265 nbl = &(fr->nblists[i]);
267 if ((fr->adress_type != eAdressOff) && (i >= fr->nnblists/2))
269 type = GMX_NBLIST_INTERACTION_ADRESS;
271 init_nblist(log, &nbl->nlist_sr[eNL_VDWQQ], &nbl->nlist_lr[eNL_VDWQQ],
272 maxsr, maxlr, ivdw, ivdwmod, ielec, ielecmod, igeometry_def, type);
273 init_nblist(log, &nbl->nlist_sr[eNL_VDW], &nbl->nlist_lr[eNL_VDW],
274 maxsr, maxlr, ivdw, ivdwmod, GMX_NBKERNEL_ELEC_NONE, eintmodNONE, igeometry_def, type);
275 init_nblist(log, &nbl->nlist_sr[eNL_QQ], &nbl->nlist_lr[eNL_QQ],
276 maxsr, maxlr, GMX_NBKERNEL_VDW_NONE, eintmodNONE, ielec, ielecmod, igeometry_def, type);
277 init_nblist(log, &nbl->nlist_sr[eNL_VDWQQ_WATER], &nbl->nlist_lr[eNL_VDWQQ_WATER],
278 maxsr_wat, maxlr_wat, ivdw, ivdwmod, ielec, ielecmod, igeometry_w, type);
279 init_nblist(log, &nbl->nlist_sr[eNL_QQ_WATER], &nbl->nlist_lr[eNL_QQ_WATER],
280 maxsr_wat, maxlr_wat, GMX_NBKERNEL_VDW_NONE, eintmodNONE, ielec, ielecmod, igeometry_w, type);
281 init_nblist(log, &nbl->nlist_sr[eNL_VDWQQ_WATERWATER], &nbl->nlist_lr[eNL_VDWQQ_WATERWATER],
282 maxsr_wat, maxlr_wat, ivdw, ivdwmod, ielec, ielecmod, igeometry_ww, type);
283 init_nblist(log, &nbl->nlist_sr[eNL_QQ_WATERWATER], &nbl->nlist_lr[eNL_QQ_WATERWATER],
284 maxsr_wat, maxlr_wat, GMX_NBKERNEL_VDW_NONE, eintmodNONE, ielec, ielecmod, igeometry_ww, type);
286 /* Did we get the solvent loops so we can use optimized water kernels? */
287 if (nbl->nlist_sr[eNL_VDWQQ_WATER].kernelptr_vf == NULL
288 || nbl->nlist_sr[eNL_QQ_WATER].kernelptr_vf == NULL
289 #ifndef DISABLE_WATERWATER_NLIST
290 || nbl->nlist_sr[eNL_VDWQQ_WATERWATER].kernelptr_vf == NULL
291 || nbl->nlist_sr[eNL_QQ_WATERWATER].kernelptr_vf == NULL
295 fr->solvent_opt = esolNO;
298 fprintf(log, "Note: The available nonbonded kernels do not support water optimization - disabling.\n");
302 if (fr->efep != efepNO)
304 if ((fr->bEwald) && (fr->sc_alphacoul > 0)) /* need to handle long range differently if using softcore */
306 ielecf = GMX_NBKERNEL_ELEC_EWALD;
307 ielecmodf = eintmodNONE;
312 ielecmodf = ielecmod;
315 init_nblist(log, &nbl->nlist_sr[eNL_VDWQQ_FREE], &nbl->nlist_lr[eNL_VDWQQ_FREE],
316 maxsr, maxlr, ivdw, ivdwmod, ielecf, ielecmod, GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE, GMX_NBLIST_INTERACTION_FREE_ENERGY);
317 init_nblist(log, &nbl->nlist_sr[eNL_VDW_FREE], &nbl->nlist_lr[eNL_VDW_FREE],
318 maxsr, maxlr, ivdw, ivdwmod, GMX_NBKERNEL_ELEC_NONE, eintmodNONE, GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE, GMX_NBLIST_INTERACTION_FREE_ENERGY);
319 init_nblist(log, &nbl->nlist_sr[eNL_QQ_FREE], &nbl->nlist_lr[eNL_QQ_FREE],
320 maxsr, maxlr, GMX_NBKERNEL_VDW_NONE, eintmodNONE, ielecf, ielecmod, GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE, GMX_NBLIST_INTERACTION_FREE_ENERGY);
324 if (fr->bQMMM && fr->qr->QMMMscheme != eQMMMschemeoniom)
326 init_nblist(log, &fr->QMMMlist, NULL,
327 maxsr, maxlr, 0, 0, ielec, ielecmod, GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE, GMX_NBLIST_INTERACTION_STANDARD);
335 fr->ns.nblist_initialized = TRUE;
338 static void reset_nblist(t_nblist *nl)
348 static void reset_neighbor_lists(t_forcerec *fr, gmx_bool bResetSR, gmx_bool bResetLR)
354 /* only reset the short-range nblist */
355 reset_nblist(&(fr->QMMMlist));
358 for (n = 0; n < fr->nnblists; n++)
360 for (i = 0; i < eNL_NR; i++)
364 reset_nblist( &(fr->nblists[n].nlist_sr[i]) );
368 reset_nblist( &(fr->nblists[n].nlist_lr[i]) );
377 static gmx_inline void new_i_nblist(t_nblist *nlist, atom_id i_atom, int shift, int gid)
379 int i, k, nri, nshift;
383 /* Check whether we have to increase the i counter */
385 (nlist->iinr[nri] != i_atom) ||
386 (nlist->shift[nri] != shift) ||
387 (nlist->gid[nri] != gid))
389 /* This is something else. Now see if any entries have
390 * been added in the list of the previous atom.
393 ((nlist->jindex[nri+1] > nlist->jindex[nri]) &&
394 (nlist->gid[nri] != -1)))
396 /* If so increase the counter */
399 if (nlist->nri >= nlist->maxnri)
401 nlist->maxnri += over_alloc_large(nlist->nri);
402 reallocate_nblist(nlist);
405 /* Set the number of neighbours and the atom number */
406 nlist->jindex[nri+1] = nlist->jindex[nri];
407 nlist->iinr[nri] = i_atom;
408 nlist->gid[nri] = gid;
409 nlist->shift[nri] = shift;
413 /* Adding to previous list. First remove possible previous padding */
414 if (nlist->simd_padding_width > 1)
416 while (nlist->nrj > 0 && nlist->jjnr[nlist->nrj-1] < 0)
424 static gmx_inline void close_i_nblist(t_nblist *nlist)
426 int nri = nlist->nri;
431 /* Add elements up to padding. Since we allocate memory in units
432 * of the simd_padding width, we do not have to check for possible
433 * list reallocation here.
435 while ((nlist->nrj % nlist->simd_padding_width) != 0)
437 /* Use -4 here, so we can write forces for 4 atoms before real data */
438 nlist->jjnr[nlist->nrj++] = -4;
440 nlist->jindex[nri+1] = nlist->nrj;
442 len = nlist->nrj - nlist->jindex[nri];
446 static gmx_inline void close_nblist(t_nblist *nlist)
448 /* Only close this nblist when it has been initialized.
449 * Avoid the creation of i-lists with no j-particles.
453 /* Some assembly kernels do not support empty lists,
454 * make sure here that we don't generate any empty lists.
455 * With the current ns code this branch is taken in two cases:
456 * No i-particles at all: nri=-1 here
457 * There are i-particles, but no j-particles; nri=0 here
463 /* Close list number nri by incrementing the count */
468 static gmx_inline void close_neighbor_lists(t_forcerec *fr, gmx_bool bMakeQMMMnblist)
474 close_nblist(&(fr->QMMMlist));
477 for (n = 0; n < fr->nnblists; n++)
479 for (i = 0; (i < eNL_NR); i++)
481 close_nblist(&(fr->nblists[n].nlist_sr[i]));
482 close_nblist(&(fr->nblists[n].nlist_lr[i]));
488 static gmx_inline void add_j_to_nblist(t_nblist *nlist, atom_id j_atom, gmx_bool bLR)
490 int nrj = nlist->nrj;
492 if (nlist->nrj >= nlist->maxnrj)
494 nlist->maxnrj = round_up_to_simd_width(over_alloc_small(nlist->nrj + 1), nlist->simd_padding_width);
498 fprintf(debug, "Increasing %s nblist (ielec=%d,ivdw=%d,type=%d,igeometry=%d) j size to %d\n",
499 bLR ? "LR" : "SR", nlist->ielec, nlist->ivdw, nlist->type, nlist->igeometry, nlist->maxnrj);
502 srenew(nlist->jjnr, nlist->maxnrj);
505 nlist->jjnr[nrj] = j_atom;
509 static gmx_inline void add_j_to_nblist_cg(t_nblist *nlist,
510 atom_id j_start, int j_end,
511 t_excl *bexcl, gmx_bool i_is_j,
514 int nrj = nlist->nrj;
517 if (nlist->nrj >= nlist->maxnrj)
519 nlist->maxnrj = over_alloc_small(nlist->nrj + 1);
522 fprintf(debug, "Increasing %s nblist (ielec=%d,ivdw=%d,type=%d,igeometry=%d) j size to %d\n",
523 bLR ? "LR" : "SR", nlist->ielec, nlist->ivdw, nlist->type, nlist->igeometry, nlist->maxnrj);
526 srenew(nlist->jjnr, nlist->maxnrj);
527 srenew(nlist->jjnr_end, nlist->maxnrj);
528 srenew(nlist->excl, nlist->maxnrj*MAX_CGCGSIZE);
531 nlist->jjnr[nrj] = j_start;
532 nlist->jjnr_end[nrj] = j_end;
534 if (j_end - j_start > MAX_CGCGSIZE)
536 gmx_fatal(FARGS, "The charge-group - charge-group neighborlist do not support charge groups larger than %d, found a charge group of size %d", MAX_CGCGSIZE, j_end-j_start);
539 /* Set the exclusions */
540 for (j = j_start; j < j_end; j++)
542 nlist->excl[nrj*MAX_CGCGSIZE + j - j_start] = bexcl[j];
546 /* Avoid double counting of intra-cg interactions */
547 for (j = 1; j < j_end-j_start; j++)
549 nlist->excl[nrj*MAX_CGCGSIZE + j] |= (1<<j) - 1;
557 put_in_list_t (gmx_bool bHaveVdW[],
574 put_in_list_at(gmx_bool bHaveVdW[],
590 /* The a[] index has been removed,
591 * to put it back in i_atom should be a[i0] and jj should be a[jj].
596 t_nblist * vdwc_free = NULL;
597 t_nblist * vdw_free = NULL;
598 t_nblist * coul_free = NULL;
599 t_nblist * vdwc_ww = NULL;
600 t_nblist * coul_ww = NULL;
602 int i, j, jcg, igid, gid, nbl_ind, ind_ij;
603 atom_id jj, jj0, jj1, i_atom;
608 real *charge, *chargeB;
609 real qi, qiB, qq, rlj;
610 gmx_bool bFreeEnergy, bFree, bFreeJ, bNotEx, *bPert;
611 gmx_bool bDoVdW_i, bDoCoul_i, bDoCoul_i_sol;
615 /* Copy some pointers */
617 charge = md->chargeA;
618 chargeB = md->chargeB;
621 bPert = md->bPerturbed;
625 nicg = index[icg+1]-i0;
627 /* Get the i charge group info */
628 igid = GET_CGINFO_GID(cginfo[icg]);
630 iwater = (solvent_opt != esolNO) ? GET_CGINFO_SOLOPT(cginfo[icg]) : esolNO;
635 /* Check if any of the particles involved are perturbed.
636 * If not we can do the cheaper normal put_in_list
637 * and use more solvent optimization.
639 for (i = 0; i < nicg; i++)
641 bFreeEnergy |= bPert[i0+i];
643 /* Loop over the j charge groups */
644 for (j = 0; (j < nj && !bFreeEnergy); j++)
649 /* Finally loop over the atoms in the j-charge group */
650 for (jj = jj0; jj < jj1; jj++)
652 bFreeEnergy |= bPert[jj];
657 /* Unpack pointers to neighbourlist structs */
658 if (fr->nnblists == 1)
664 nbl_ind = fr->gid2nblists[GID(igid, jgid, ngid)];
668 nlist = fr->nblists[nbl_ind].nlist_lr;
672 nlist = fr->nblists[nbl_ind].nlist_sr;
675 if (iwater != esolNO)
677 vdwc = &nlist[eNL_VDWQQ_WATER];
678 vdw = &nlist[eNL_VDW];
679 coul = &nlist[eNL_QQ_WATER];
680 #ifndef DISABLE_WATERWATER_NLIST
681 vdwc_ww = &nlist[eNL_VDWQQ_WATERWATER];
682 coul_ww = &nlist[eNL_QQ_WATERWATER];
687 vdwc = &nlist[eNL_VDWQQ];
688 vdw = &nlist[eNL_VDW];
689 coul = &nlist[eNL_QQ];
694 if (iwater != esolNO)
696 /* Loop over the atoms in the i charge group */
698 gid = GID(igid, jgid, ngid);
699 /* Create new i_atom for each energy group */
700 if (bDoCoul && bDoVdW)
702 new_i_nblist(vdwc, i_atom, shift, gid);
703 #ifndef DISABLE_WATERWATER_NLIST
704 new_i_nblist(vdwc_ww, i_atom, shift, gid);
709 new_i_nblist(vdw, i_atom, shift, gid);
713 new_i_nblist(coul, i_atom, shift, gid);
714 #ifndef DISABLE_WATERWATER_NLIST
715 new_i_nblist(coul_ww, i_atom, shift, gid);
718 /* Loop over the j charge groups */
719 for (j = 0; (j < nj); j++)
729 jwater = GET_CGINFO_SOLOPT(cginfo[jcg]);
731 if (iwater == esolSPC && jwater == esolSPC)
733 /* Interaction between two SPC molecules */
736 /* VdW only - only first atoms in each water interact */
737 add_j_to_nblist(vdw, jj0, bLR);
741 #ifdef DISABLE_WATERWATER_NLIST
742 /* Add entries for the three atoms - only do VdW if we need to */
745 add_j_to_nblist(coul, jj0, bLR);
749 add_j_to_nblist(vdwc, jj0, bLR);
751 add_j_to_nblist(coul, jj0+1, bLR);
752 add_j_to_nblist(coul, jj0+2, bLR);
754 /* One entry for the entire water-water interaction */
757 add_j_to_nblist(coul_ww, jj0, bLR);
761 add_j_to_nblist(vdwc_ww, jj0, bLR);
766 else if (iwater == esolTIP4P && jwater == esolTIP4P)
768 /* Interaction between two TIP4p molecules */
771 /* VdW only - only first atoms in each water interact */
772 add_j_to_nblist(vdw, jj0, bLR);
776 #ifdef DISABLE_WATERWATER_NLIST
777 /* Add entries for the four atoms - only do VdW if we need to */
780 add_j_to_nblist(vdw, jj0, bLR);
782 add_j_to_nblist(coul, jj0+1, bLR);
783 add_j_to_nblist(coul, jj0+2, bLR);
784 add_j_to_nblist(coul, jj0+3, bLR);
786 /* One entry for the entire water-water interaction */
789 add_j_to_nblist(coul_ww, jj0, bLR);
793 add_j_to_nblist(vdwc_ww, jj0, bLR);
800 /* j charge group is not water, but i is.
801 * Add entries to the water-other_atom lists; the geometry of the water
802 * molecule doesn't matter - that is taken care of in the nonbonded kernel,
803 * so we don't care if it is SPC or TIP4P...
810 for (jj = jj0; (jj < jj1); jj++)
814 add_j_to_nblist(coul, jj, bLR);
820 for (jj = jj0; (jj < jj1); jj++)
822 if (bHaveVdW[type[jj]])
824 add_j_to_nblist(vdw, jj, bLR);
830 /* _charge_ _groups_ interact with both coulomb and LJ */
831 /* Check which atoms we should add to the lists! */
832 for (jj = jj0; (jj < jj1); jj++)
834 if (bHaveVdW[type[jj]])
838 add_j_to_nblist(vdwc, jj, bLR);
842 add_j_to_nblist(vdw, jj, bLR);
845 else if (charge[jj] != 0)
847 add_j_to_nblist(coul, jj, bLR);
854 close_i_nblist(coul);
855 close_i_nblist(vdwc);
856 #ifndef DISABLE_WATERWATER_NLIST
857 close_i_nblist(coul_ww);
858 close_i_nblist(vdwc_ww);
863 /* no solvent as i charge group */
864 /* Loop over the atoms in the i charge group */
865 for (i = 0; i < nicg; i++)
868 gid = GID(igid, jgid, ngid);
871 /* Create new i_atom for each energy group */
872 if (bDoVdW && bDoCoul)
874 new_i_nblist(vdwc, i_atom, shift, gid);
878 new_i_nblist(vdw, i_atom, shift, gid);
882 new_i_nblist(coul, i_atom, shift, gid);
884 bDoVdW_i = (bDoVdW && bHaveVdW[type[i_atom]]);
885 bDoCoul_i = (bDoCoul && qi != 0);
887 if (bDoVdW_i || bDoCoul_i)
889 /* Loop over the j charge groups */
890 for (j = 0; (j < nj); j++)
894 /* Check for large charge groups */
905 /* Finally loop over the atoms in the j-charge group */
906 for (jj = jj0; jj < jj1; jj++)
908 bNotEx = NOTEXCL(bExcl, i, jj);
916 add_j_to_nblist(coul, jj, bLR);
921 if (bHaveVdW[type[jj]])
923 add_j_to_nblist(vdw, jj, bLR);
928 if (bHaveVdW[type[jj]])
932 add_j_to_nblist(vdwc, jj, bLR);
936 add_j_to_nblist(vdw, jj, bLR);
939 else if (charge[jj] != 0)
941 add_j_to_nblist(coul, jj, bLR);
949 close_i_nblist(coul);
950 close_i_nblist(vdwc);
956 /* we are doing free energy */
957 vdwc_free = &nlist[eNL_VDWQQ_FREE];
958 vdw_free = &nlist[eNL_VDW_FREE];
959 coul_free = &nlist[eNL_QQ_FREE];
960 /* Loop over the atoms in the i charge group */
961 for (i = 0; i < nicg; i++)
964 gid = GID(igid, jgid, ngid);
966 qiB = chargeB[i_atom];
968 /* Create new i_atom for each energy group */
969 if (bDoVdW && bDoCoul)
971 new_i_nblist(vdwc, i_atom, shift, gid);
975 new_i_nblist(vdw, i_atom, shift, gid);
979 new_i_nblist(coul, i_atom, shift, gid);
982 new_i_nblist(vdw_free, i_atom, shift, gid);
983 new_i_nblist(coul_free, i_atom, shift, gid);
984 new_i_nblist(vdwc_free, i_atom, shift, gid);
986 bDoVdW_i = (bDoVdW &&
987 (bHaveVdW[type[i_atom]] || bHaveVdW[typeB[i_atom]]));
988 bDoCoul_i = (bDoCoul && (qi != 0 || qiB != 0));
989 /* For TIP4P the first atom does not have a charge,
990 * but the last three do. So we should still put an atom
991 * without LJ but with charge in the water-atom neighborlist
992 * for a TIP4p i charge group.
993 * For SPC type water the first atom has LJ and charge,
994 * so there is no such problem.
996 if (iwater == esolNO)
998 bDoCoul_i_sol = bDoCoul_i;
1002 bDoCoul_i_sol = bDoCoul;
1005 if (bDoVdW_i || bDoCoul_i_sol)
1007 /* Loop over the j charge groups */
1008 for (j = 0; (j < nj); j++)
1012 /* Check for large charge groups */
1023 /* Finally loop over the atoms in the j-charge group */
1024 bFree = bPert[i_atom];
1025 for (jj = jj0; (jj < jj1); jj++)
1027 bFreeJ = bFree || bPert[jj];
1028 /* Complicated if, because the water H's should also
1029 * see perturbed j-particles
1031 if (iwater == esolNO || i == 0 || bFreeJ)
1033 bNotEx = NOTEXCL(bExcl, i, jj);
1041 if (charge[jj] != 0 || chargeB[jj] != 0)
1043 add_j_to_nblist(coul_free, jj, bLR);
1046 else if (!bDoCoul_i)
1048 if (bHaveVdW[type[jj]] || bHaveVdW[typeB[jj]])
1050 add_j_to_nblist(vdw_free, jj, bLR);
1055 if (bHaveVdW[type[jj]] || bHaveVdW[typeB[jj]])
1057 if (charge[jj] != 0 || chargeB[jj] != 0)
1059 add_j_to_nblist(vdwc_free, jj, bLR);
1063 add_j_to_nblist(vdw_free, jj, bLR);
1066 else if (charge[jj] != 0 || chargeB[jj] != 0)
1068 add_j_to_nblist(coul_free, jj, bLR);
1074 /* This is done whether or not bWater is set */
1075 if (charge[jj] != 0)
1077 add_j_to_nblist(coul, jj, bLR);
1080 else if (!bDoCoul_i_sol)
1082 if (bHaveVdW[type[jj]])
1084 add_j_to_nblist(vdw, jj, bLR);
1089 if (bHaveVdW[type[jj]])
1091 if (charge[jj] != 0)
1093 add_j_to_nblist(vdwc, jj, bLR);
1097 add_j_to_nblist(vdw, jj, bLR);
1100 else if (charge[jj] != 0)
1102 add_j_to_nblist(coul, jj, bLR);
1110 close_i_nblist(vdw);
1111 close_i_nblist(coul);
1112 close_i_nblist(vdwc);
1113 close_i_nblist(vdw_free);
1114 close_i_nblist(coul_free);
1115 close_i_nblist(vdwc_free);
1121 put_in_list_adress(gmx_bool bHaveVdW[],
1137 /* The a[] index has been removed,
1138 * to put it back in i_atom should be a[i0] and jj should be a[jj].
1143 t_nblist * vdwc_adress = NULL;
1144 t_nblist * vdw_adress = NULL;
1145 t_nblist * coul_adress = NULL;
1146 t_nblist * vdwc_ww = NULL;
1147 t_nblist * coul_ww = NULL;
1149 int i, j, jcg, igid, gid, nbl_ind, nbl_ind_adress;
1150 atom_id jj, jj0, jj1, i_atom;
1155 real *charge, *chargeB;
1157 real qi, qiB, qq, rlj;
1158 gmx_bool bFreeEnergy, bFree, bFreeJ, bNotEx, *bPert;
1159 gmx_bool bDoVdW_i, bDoCoul_i, bDoCoul_i_sol;
1161 gmx_bool j_all_atom;
1163 t_nblist *nlist, *nlist_adress;
1164 gmx_bool bEnergyGroupCG;
1166 /* Copy some pointers */
1167 cginfo = fr->cginfo;
1168 charge = md->chargeA;
1169 chargeB = md->chargeB;
1172 bPert = md->bPerturbed;
1175 /* Get atom range */
1177 nicg = index[icg+1]-i0;
1179 /* Get the i charge group info */
1180 igid = GET_CGINFO_GID(cginfo[icg]);
1182 iwater = (solvent_opt != esolNO) ? GET_CGINFO_SOLOPT(cginfo[icg]) : esolNO;
1186 gmx_fatal(FARGS, "AdResS does not support free energy pertubation\n");
1189 /* Unpack pointers to neighbourlist structs */
1190 if (fr->nnblists == 2)
1197 nbl_ind = fr->gid2nblists[GID(igid, jgid, ngid)];
1198 nbl_ind_adress = nbl_ind+fr->nnblists/2;
1202 nlist = fr->nblists[nbl_ind].nlist_lr;
1203 nlist_adress = fr->nblists[nbl_ind_adress].nlist_lr;
1207 nlist = fr->nblists[nbl_ind].nlist_sr;
1208 nlist_adress = fr->nblists[nbl_ind_adress].nlist_sr;
1212 vdwc = &nlist[eNL_VDWQQ];
1213 vdw = &nlist[eNL_VDW];
1214 coul = &nlist[eNL_QQ];
1216 vdwc_adress = &nlist_adress[eNL_VDWQQ];
1217 vdw_adress = &nlist_adress[eNL_VDW];
1218 coul_adress = &nlist_adress[eNL_QQ];
1220 /* We do not support solvent optimization with AdResS for now.
1221 For this we would need hybrid solvent-other kernels */
1223 /* no solvent as i charge group */
1224 /* Loop over the atoms in the i charge group */
1225 for (i = 0; i < nicg; i++)
1228 gid = GID(igid, jgid, ngid);
1229 qi = charge[i_atom];
1231 /* Create new i_atom for each energy group */
1232 if (bDoVdW && bDoCoul)
1234 new_i_nblist(vdwc, i_atom, shift, gid);
1235 new_i_nblist(vdwc_adress, i_atom, shift, gid);
1240 new_i_nblist(vdw, i_atom, shift, gid);
1241 new_i_nblist(vdw_adress, i_atom, shift, gid);
1246 new_i_nblist(coul, i_atom, shift, gid);
1247 new_i_nblist(coul_adress, i_atom, shift, gid);
1249 bDoVdW_i = (bDoVdW && bHaveVdW[type[i_atom]]);
1250 bDoCoul_i = (bDoCoul && qi != 0);
1252 /* Here we find out whether the energy groups interaction belong to a
1253 * coarse-grained (vsite) or atomistic interaction. Note that, beacuse
1254 * interactions between coarse-grained and other (atomistic) energygroups
1255 * are excluded automatically by grompp, it is sufficient to check for
1256 * the group id of atom i (igid) */
1257 bEnergyGroupCG = !egp_explicit(fr, igid);
1259 if (bDoVdW_i || bDoCoul_i)
1261 /* Loop over the j charge groups */
1262 for (j = 0; (j < nj); j++)
1266 /* Check for large charge groups */
1277 /* Finally loop over the atoms in the j-charge group */
1278 for (jj = jj0; jj < jj1; jj++)
1280 bNotEx = NOTEXCL(bExcl, i, jj);
1282 /* Now we have to exclude interactions which will be zero
1283 * anyway due to the AdResS weights (in previous implementations
1284 * this was done in the force kernel). This is necessary as
1285 * pure interactions (those with b_hybrid=false, i.e. w_i*w_j==1 or 0)
1286 * are put into neighbour lists which will be passed to the
1287 * standard (optimized) kernels for speed. The interactions with
1288 * b_hybrid=true are placed into the _adress neighbour lists and
1289 * processed by the generic AdResS kernel.
1291 if ( (bEnergyGroupCG &&
1292 wf[i_atom] >= 1-GMX_REAL_EPS && wf[jj] >= 1-GMX_REAL_EPS ) ||
1293 ( !bEnergyGroupCG && wf[jj] <= GMX_REAL_EPS ) )
1298 b_hybrid = !((wf[i_atom] >= 1-GMX_REAL_EPS && wf[jj] >= 1-GMX_REAL_EPS) ||
1299 (wf[i_atom] <= GMX_REAL_EPS && wf[jj] <= GMX_REAL_EPS));
1305 if (charge[jj] != 0)
1309 add_j_to_nblist(coul, jj, bLR);
1313 add_j_to_nblist(coul_adress, jj, bLR);
1317 else if (!bDoCoul_i)
1319 if (bHaveVdW[type[jj]])
1323 add_j_to_nblist(vdw, jj, bLR);
1327 add_j_to_nblist(vdw_adress, jj, bLR);
1333 if (bHaveVdW[type[jj]])
1335 if (charge[jj] != 0)
1339 add_j_to_nblist(vdwc, jj, bLR);
1343 add_j_to_nblist(vdwc_adress, jj, bLR);
1350 add_j_to_nblist(vdw, jj, bLR);
1354 add_j_to_nblist(vdw_adress, jj, bLR);
1359 else if (charge[jj] != 0)
1363 add_j_to_nblist(coul, jj, bLR);
1367 add_j_to_nblist(coul_adress, jj, bLR);
1376 close_i_nblist(vdw);
1377 close_i_nblist(coul);
1378 close_i_nblist(vdwc);
1379 close_i_nblist(vdw_adress);
1380 close_i_nblist(coul_adress);
1381 close_i_nblist(vdwc_adress);
1387 put_in_list_qmmm(gmx_bool gmx_unused bHaveVdW[],
1389 t_mdatoms gmx_unused * md,
1399 gmx_bool gmx_unused bDoVdW,
1400 gmx_bool gmx_unused bDoCoul,
1401 int gmx_unused solvent_opt)
1404 int i, j, jcg, igid, gid;
1405 atom_id jj, jj0, jj1, i_atom;
1409 /* Get atom range */
1411 nicg = index[icg+1]-i0;
1413 /* Get the i charge group info */
1414 igid = GET_CGINFO_GID(fr->cginfo[icg]);
1416 coul = &fr->QMMMlist;
1418 /* Loop over atoms in the ith charge group */
1419 for (i = 0; i < nicg; i++)
1422 gid = GID(igid, jgid, ngid);
1423 /* Create new i_atom for each energy group */
1424 new_i_nblist(coul, i_atom, shift, gid);
1426 /* Loop over the j charge groups */
1427 for (j = 0; j < nj; j++)
1431 /* Charge groups cannot have QM and MM atoms simultaneously */
1436 /* Finally loop over the atoms in the j-charge group */
1437 for (jj = jj0; jj < jj1; jj++)
1439 bNotEx = NOTEXCL(bExcl, i, jj);
1442 add_j_to_nblist(coul, jj, bLR);
1447 close_i_nblist(coul);
1452 put_in_list_cg(gmx_bool gmx_unused bHaveVdW[],
1454 t_mdatoms gmx_unused * md,
1464 gmx_bool gmx_unused bDoVdW,
1465 gmx_bool gmx_unused bDoCoul,
1466 int gmx_unused solvent_opt)
1469 int igid, gid, nbl_ind;
1473 cginfo = fr->cginfo[icg];
1475 igid = GET_CGINFO_GID(cginfo);
1476 gid = GID(igid, jgid, ngid);
1478 /* Unpack pointers to neighbourlist structs */
1479 if (fr->nnblists == 1)
1485 nbl_ind = fr->gid2nblists[gid];
1489 vdwc = &fr->nblists[nbl_ind].nlist_lr[eNL_VDWQQ];
1493 vdwc = &fr->nblists[nbl_ind].nlist_sr[eNL_VDWQQ];
1496 /* Make a new neighbor list for charge group icg.
1497 * Currently simply one neighbor list is made with LJ and Coulomb.
1498 * If required, zero interactions could be removed here
1499 * or in the force loop.
1501 new_i_nblist(vdwc, index[icg], shift, gid);
1502 vdwc->iinr_end[vdwc->nri] = index[icg+1];
1504 for (j = 0; (j < nj); j++)
1507 /* Skip the icg-icg pairs if all self interactions are excluded */
1508 if (!(jcg == icg && GET_CGINFO_EXCL_INTRA(cginfo)))
1510 /* Here we add the j charge group jcg to the list,
1511 * exclusions are also added to the list.
1513 add_j_to_nblist_cg(vdwc, index[jcg], index[jcg+1], bExcl, icg == jcg, bLR);
1517 close_i_nblist(vdwc);
1520 static void setexcl(atom_id start, atom_id end, t_blocka *excl, gmx_bool b,
1527 for (i = start; i < end; i++)
1529 for (k = excl->index[i]; k < excl->index[i+1]; k++)
1531 SETEXCL(bexcl, i-start, excl->a[k]);
1537 for (i = start; i < end; i++)
1539 for (k = excl->index[i]; k < excl->index[i+1]; k++)
1541 RMEXCL(bexcl, i-start, excl->a[k]);
1547 int calc_naaj(int icg, int cgtot)
1551 if ((cgtot % 2) == 1)
1553 /* Odd number of charge groups, easy */
1554 naaj = 1 + (cgtot/2);
1556 else if ((cgtot % 4) == 0)
1558 /* Multiple of four is hard */
1595 fprintf(log, "naaj=%d\n", naaj);
1601 /************************************************
1603 * S I M P L E C O R E S T U F F
1605 ************************************************/
1607 static real calc_image_tric(rvec xi, rvec xj, matrix box,
1608 rvec b_inv, int *shift)
1610 /* This code assumes that the cut-off is smaller than
1611 * a half times the smallest diagonal element of the box.
1613 const real h25 = 2.5;
1618 /* Compute diff vector */
1619 dz = xj[ZZ] - xi[ZZ];
1620 dy = xj[YY] - xi[YY];
1621 dx = xj[XX] - xi[XX];
1623 /* Perform NINT operation, using trunc operation, therefore
1624 * we first add 2.5 then subtract 2 again
1626 tz = dz*b_inv[ZZ] + h25;
1628 dz -= tz*box[ZZ][ZZ];
1629 dy -= tz*box[ZZ][YY];
1630 dx -= tz*box[ZZ][XX];
1632 ty = dy*b_inv[YY] + h25;
1634 dy -= ty*box[YY][YY];
1635 dx -= ty*box[YY][XX];
1637 tx = dx*b_inv[XX]+h25;
1639 dx -= tx*box[XX][XX];
1641 /* Distance squared */
1642 r2 = (dx*dx) + (dy*dy) + (dz*dz);
1644 *shift = XYZ2IS(tx, ty, tz);
1649 static real calc_image_rect(rvec xi, rvec xj, rvec box_size,
1650 rvec b_inv, int *shift)
1652 const real h15 = 1.5;
1658 /* Compute diff vector */
1659 dx = xj[XX] - xi[XX];
1660 dy = xj[YY] - xi[YY];
1661 dz = xj[ZZ] - xi[ZZ];
1663 /* Perform NINT operation, using trunc operation, therefore
1664 * we first add 1.5 then subtract 1 again
1666 tx = dx*b_inv[XX] + h15;
1667 ty = dy*b_inv[YY] + h15;
1668 tz = dz*b_inv[ZZ] + h15;
1673 /* Correct diff vector for translation */
1674 ddx = tx*box_size[XX] - dx;
1675 ddy = ty*box_size[YY] - dy;
1676 ddz = tz*box_size[ZZ] - dz;
1678 /* Distance squared */
1679 r2 = (ddx*ddx) + (ddy*ddy) + (ddz*ddz);
1681 *shift = XYZ2IS(tx, ty, tz);
1686 static void add_simple(t_ns_buf *nsbuf, int nrj, atom_id cg_j,
1687 gmx_bool bHaveVdW[], int ngid, t_mdatoms *md,
1688 int icg, int jgid, t_block *cgs, t_excl bexcl[],
1689 int shift, t_forcerec *fr, put_in_list_t *put_in_list)
1691 if (nsbuf->nj + nrj > MAX_CG)
1693 put_in_list(bHaveVdW, ngid, md, icg, jgid, nsbuf->ncg, nsbuf->jcg,
1694 cgs->index, bexcl, shift, fr, FALSE, TRUE, TRUE, fr->solvent_opt);
1695 /* Reset buffer contents */
1696 nsbuf->ncg = nsbuf->nj = 0;
1698 nsbuf->jcg[nsbuf->ncg++] = cg_j;
1702 static void ns_inner_tric(rvec x[], int icg, int *i_egp_flags,
1703 int njcg, atom_id jcg[],
1704 matrix box, rvec b_inv, real rcut2,
1705 t_block *cgs, t_ns_buf **ns_buf,
1706 gmx_bool bHaveVdW[], int ngid, t_mdatoms *md,
1707 t_excl bexcl[], t_forcerec *fr,
1708 put_in_list_t *put_in_list)
1712 int *cginfo = fr->cginfo;
1713 atom_id cg_j, *cgindex;
1716 cgindex = cgs->index;
1718 for (j = 0; (j < njcg); j++)
1721 nrj = cgindex[cg_j+1]-cgindex[cg_j];
1722 if (calc_image_tric(x[icg], x[cg_j], box, b_inv, &shift) < rcut2)
1724 jgid = GET_CGINFO_GID(cginfo[cg_j]);
1725 if (!(i_egp_flags[jgid] & EGP_EXCL))
1727 add_simple(&ns_buf[jgid][shift], nrj, cg_j,
1728 bHaveVdW, ngid, md, icg, jgid, cgs, bexcl, shift, fr,
1735 static void ns_inner_rect(rvec x[], int icg, int *i_egp_flags,
1736 int njcg, atom_id jcg[],
1737 gmx_bool bBox, rvec box_size, rvec b_inv, real rcut2,
1738 t_block *cgs, t_ns_buf **ns_buf,
1739 gmx_bool bHaveVdW[], int ngid, t_mdatoms *md,
1740 t_excl bexcl[], t_forcerec *fr,
1741 put_in_list_t *put_in_list)
1745 int *cginfo = fr->cginfo;
1746 atom_id cg_j, *cgindex;
1749 cgindex = cgs->index;
1753 for (j = 0; (j < njcg); j++)
1756 nrj = cgindex[cg_j+1]-cgindex[cg_j];
1757 if (calc_image_rect(x[icg], x[cg_j], box_size, b_inv, &shift) < rcut2)
1759 jgid = GET_CGINFO_GID(cginfo[cg_j]);
1760 if (!(i_egp_flags[jgid] & EGP_EXCL))
1762 add_simple(&ns_buf[jgid][shift], nrj, cg_j,
1763 bHaveVdW, ngid, md, icg, jgid, cgs, bexcl, shift, fr,
1771 for (j = 0; (j < njcg); j++)
1774 nrj = cgindex[cg_j+1]-cgindex[cg_j];
1775 if ((rcut2 == 0) || (distance2(x[icg], x[cg_j]) < rcut2))
1777 jgid = GET_CGINFO_GID(cginfo[cg_j]);
1778 if (!(i_egp_flags[jgid] & EGP_EXCL))
1780 add_simple(&ns_buf[jgid][CENTRAL], nrj, cg_j,
1781 bHaveVdW, ngid, md, icg, jgid, cgs, bexcl, CENTRAL, fr,
1789 /* ns_simple_core needs to be adapted for QMMM still 2005 */
1791 static int ns_simple_core(t_forcerec *fr,
1792 gmx_localtop_t *top,
1794 matrix box, rvec box_size,
1795 t_excl bexcl[], atom_id *aaj,
1796 int ngid, t_ns_buf **ns_buf,
1797 put_in_list_t *put_in_list, gmx_bool bHaveVdW[])
1801 int nsearch, icg, jcg, igid, i0, nri, nn;
1804 /* atom_id *i_atoms; */
1805 t_block *cgs = &(top->cgs);
1806 t_blocka *excl = &(top->excls);
1809 gmx_bool bBox, bTriclinic;
1812 rlist2 = sqr(fr->rlist);
1814 bBox = (fr->ePBC != epbcNONE);
1817 for (m = 0; (m < DIM); m++)
1819 b_inv[m] = divide_err(1.0, box_size[m]);
1821 bTriclinic = TRICLINIC(box);
1828 cginfo = fr->cginfo;
1831 for (icg = fr->cg0; (icg < fr->hcg); icg++)
1834 i0 = cgs->index[icg];
1835 nri = cgs->index[icg+1]-i0;
1836 i_atoms = &(cgs->a[i0]);
1837 i_eg_excl = fr->eg_excl + ngid*md->cENER[*i_atoms];
1838 setexcl(nri,i_atoms,excl,TRUE,bexcl);
1840 igid = GET_CGINFO_GID(cginfo[icg]);
1841 i_egp_flags = fr->egp_flags + ngid*igid;
1842 setexcl(cgs->index[icg], cgs->index[icg+1], excl, TRUE, bexcl);
1844 naaj = calc_naaj(icg, cgs->nr);
1847 ns_inner_tric(fr->cg_cm, icg, i_egp_flags, naaj, &(aaj[icg]),
1848 box, b_inv, rlist2, cgs, ns_buf,
1849 bHaveVdW, ngid, md, bexcl, fr, put_in_list);
1853 ns_inner_rect(fr->cg_cm, icg, i_egp_flags, naaj, &(aaj[icg]),
1854 bBox, box_size, b_inv, rlist2, cgs, ns_buf,
1855 bHaveVdW, ngid, md, bexcl, fr, put_in_list);
1859 for (nn = 0; (nn < ngid); nn++)
1861 for (k = 0; (k < SHIFTS); k++)
1863 nsbuf = &(ns_buf[nn][k]);
1866 put_in_list(bHaveVdW, ngid, md, icg, nn, nsbuf->ncg, nsbuf->jcg,
1867 cgs->index, bexcl, k, fr, FALSE, TRUE, TRUE, fr->solvent_opt);
1868 nsbuf->ncg = nsbuf->nj = 0;
1872 /* setexcl(nri,i_atoms,excl,FALSE,bexcl); */
1873 setexcl(cgs->index[icg], cgs->index[icg+1], excl, FALSE, bexcl);
1875 close_neighbor_lists(fr, FALSE);
1880 /************************************************
1882 * N S 5 G R I D S T U F F
1884 ************************************************/
1886 static gmx_inline void get_dx(int Nx, real gridx, real rc2, int xgi, real x,
1887 int *dx0, int *dx1, real *dcx2)
1915 for (i = xgi0; i >= 0; i--)
1917 dcx = (i+1)*gridx-x;
1926 for (i = xgi1; i < Nx; i++)
1939 static gmx_inline void get_dx_dd(int Nx, real gridx, real rc2, int xgi, real x,
1940 int ncpddc, int shift_min, int shift_max,
1941 int *g0, int *g1, real *dcx2)
1944 int g_min, g_max, shift_home;
1977 g_min = (shift_min == shift_home ? 0 : ncpddc);
1978 g_max = (shift_max == shift_home ? ncpddc - 1 : Nx - 1);
1985 else if (shift_max < 0)
2000 /* Check one grid cell down */
2001 dcx = ((*g0 - 1) + 1)*gridx - x;
2013 /* Check one grid cell up */
2014 dcx = (*g1 + 1)*gridx - x;
2026 #define sqr(x) ((x)*(x))
2027 #define calc_dx2(XI, YI, ZI, y) (sqr(XI-y[XX]) + sqr(YI-y[YY]) + sqr(ZI-y[ZZ]))
2028 #define calc_cyl_dx2(XI, YI, y) (sqr(XI-y[XX]) + sqr(YI-y[YY]))
2029 /****************************************************
2031 * F A S T N E I G H B O R S E A R C H I N G
2033 * Optimized neighboursearching routine using grid
2034 * at least 1x1x1, see GROMACS manual
2036 ****************************************************/
2039 static void get_cutoff2(t_forcerec *fr, gmx_bool bDoLongRange,
2040 real *rvdw2, real *rcoul2,
2041 real *rs2, real *rm2, real *rl2)
2043 *rs2 = sqr(fr->rlist);
2045 if (bDoLongRange && fr->bTwinRange)
2047 /* With plain cut-off or RF we need to make the list exactly
2048 * up to the cut-off and the cut-off's can be different,
2049 * so we can not simply set them to rlistlong.
2050 * To keep this code compatible with (exotic) old cases,
2051 * we also create lists up to rvdw/rcoulomb for PME and Ewald.
2052 * The interaction check should correspond to:
2053 * !ir_vdw/coulomb_might_be_zero_at_cutoff from inputrec.c.
2055 if (((fr->vdwtype == evdwCUT || fr->vdwtype == evdwPME) &&
2056 fr->vdw_modifier == eintmodNONE) ||
2057 fr->rvdw <= fr->rlist)
2059 *rvdw2 = sqr(fr->rvdw);
2063 *rvdw2 = sqr(fr->rlistlong);
2065 if (((fr->eeltype == eelCUT ||
2066 (EEL_RF(fr->eeltype) && fr->eeltype != eelRF_ZERO) ||
2067 fr->eeltype == eelPME ||
2068 fr->eeltype == eelEWALD) &&
2069 fr->coulomb_modifier == eintmodNONE) ||
2070 fr->rcoulomb <= fr->rlist)
2072 *rcoul2 = sqr(fr->rcoulomb);
2076 *rcoul2 = sqr(fr->rlistlong);
2081 /* Workaround for a gcc -O3 or -ffast-math problem */
2085 *rm2 = min(*rvdw2, *rcoul2);
2086 *rl2 = max(*rvdw2, *rcoul2);
2089 static void init_nsgrid_lists(t_forcerec *fr, int ngid, gmx_ns_t *ns)
2091 real rvdw2, rcoul2, rs2, rm2, rl2;
2094 get_cutoff2(fr, TRUE, &rvdw2, &rcoul2, &rs2, &rm2, &rl2);
2096 /* Short range buffers */
2097 snew(ns->nl_sr, ngid);
2099 snew(ns->nsr, ngid);
2100 snew(ns->nlr_ljc, ngid);
2101 snew(ns->nlr_one, ngid);
2103 /* Always allocate both list types, since rcoulomb might now change with PME load balancing */
2104 /* Long range VdW and Coul buffers */
2105 snew(ns->nl_lr_ljc, ngid);
2106 /* Long range VdW or Coul only buffers */
2107 snew(ns->nl_lr_one, ngid);
2109 for (j = 0; (j < ngid); j++)
2111 snew(ns->nl_sr[j], MAX_CG);
2112 snew(ns->nl_lr_ljc[j], MAX_CG);
2113 snew(ns->nl_lr_one[j], MAX_CG);
2118 "ns5_core: rs2 = %g, rm2 = %g, rl2 = %g (nm^2)\n",
2123 static int nsgrid_core(t_commrec *cr, t_forcerec *fr,
2124 matrix box, int ngid,
2125 gmx_localtop_t *top,
2127 t_excl bexcl[], gmx_bool *bExcludeAlleg,
2129 put_in_list_t *put_in_list,
2130 gmx_bool bHaveVdW[],
2131 gmx_bool bDoLongRange, gmx_bool bMakeQMMMnblist)
2134 atom_id **nl_lr_ljc, **nl_lr_one, **nl_sr;
2135 int *nlr_ljc, *nlr_one, *nsr;
2137 t_block *cgs = &(top->cgs);
2138 int *cginfo = fr->cginfo;
2139 /* atom_id *i_atoms,*cgsindex=cgs->index; */
2141 int cell_x, cell_y, cell_z;
2142 int d, tx, ty, tz, dx, dy, dz, cj;
2143 #ifdef ALLOW_OFFDIAG_LT_HALFDIAG
2144 int zsh_ty, zsh_tx, ysh_tx;
2146 int dx0, dx1, dy0, dy1, dz0, dz1;
2147 int Nx, Ny, Nz, shift = -1, j, nrj, nns, nn = -1;
2148 real gridx, gridy, gridz, grid_x, grid_y, grid_z;
2149 real *dcx2, *dcy2, *dcz2;
2151 int cg0, cg1, icg = -1, cgsnr, i0, igid, nri, naaj, max_jcg;
2152 int jcg0, jcg1, jjcg, cgj0, jgid;
2153 int *grida, *gridnra, *gridind;
2154 gmx_bool rvdw_lt_rcoul, rcoul_lt_rvdw;
2155 rvec xi, *cgcm, grid_offset;
2156 real r2, rs2, rvdw2, rcoul2, rm2, rl2, XI, YI, ZI, dcx, dcy, dcz, tmp1, tmp2;
2158 gmx_bool bDomDec, bTriclinicX, bTriclinicY;
2163 bDomDec = DOMAINDECOMP(cr);
2166 bTriclinicX = ((YY < grid->npbcdim &&
2167 (!bDomDec || dd->nc[YY] == 1) && box[YY][XX] != 0) ||
2168 (ZZ < grid->npbcdim &&
2169 (!bDomDec || dd->nc[ZZ] == 1) && box[ZZ][XX] != 0));
2170 bTriclinicY = (ZZ < grid->npbcdim &&
2171 (!bDomDec || dd->nc[ZZ] == 1) && box[ZZ][YY] != 0);
2175 get_cutoff2(fr, bDoLongRange, &rvdw2, &rcoul2, &rs2, &rm2, &rl2);
2177 rvdw_lt_rcoul = (rvdw2 >= rcoul2);
2178 rcoul_lt_rvdw = (rcoul2 >= rvdw2);
2180 if (bMakeQMMMnblist)
2188 nl_lr_ljc = ns->nl_lr_ljc;
2189 nl_lr_one = ns->nl_lr_one;
2190 nlr_ljc = ns->nlr_ljc;
2191 nlr_one = ns->nlr_one;
2199 gridind = grid->index;
2200 gridnra = grid->nra;
2203 gridx = grid->cell_size[XX];
2204 gridy = grid->cell_size[YY];
2205 gridz = grid->cell_size[ZZ];
2209 copy_rvec(grid->cell_offset, grid_offset);
2210 copy_ivec(grid->ncpddc, ncpddc);
2215 #ifdef ALLOW_OFFDIAG_LT_HALFDIAG
2216 zsh_ty = floor(-box[ZZ][YY]/box[YY][YY]+0.5);
2217 zsh_tx = floor(-box[ZZ][XX]/box[XX][XX]+0.5);
2218 ysh_tx = floor(-box[YY][XX]/box[XX][XX]+0.5);
2219 if (zsh_tx != 0 && ysh_tx != 0)
2221 /* This could happen due to rounding, when both ratios are 0.5 */
2230 /* We only want a list for the test particle */
2239 /* Set the shift range */
2240 for (d = 0; d < DIM; d++)
2244 /* Check if we need periodicity shifts.
2245 * Without PBC or with domain decomposition we don't need them.
2247 if (d >= ePBC2npbcdim(fr->ePBC) || (bDomDec && dd->nc[d] > 1))
2254 box[XX][XX] - fabs(box[YY][XX]) - fabs(box[ZZ][XX]) < sqrt(rl2))
2265 /* Loop over charge groups */
2266 for (icg = cg0; (icg < cg1); icg++)
2268 igid = GET_CGINFO_GID(cginfo[icg]);
2269 /* Skip this charge group if all energy groups are excluded! */
2270 if (bExcludeAlleg[igid])
2275 i0 = cgs->index[icg];
2277 if (bMakeQMMMnblist)
2279 /* Skip this charge group if it is not a QM atom while making a
2280 * QM/MM neighbourlist
2282 if (md->bQM[i0] == FALSE)
2284 continue; /* MM particle, go to next particle */
2287 /* Compute the number of charge groups that fall within the control
2290 naaj = calc_naaj(icg, cgsnr);
2297 /* make a normal neighbourlist */
2301 /* Get the j charge-group and dd cell shift ranges */
2302 dd_get_ns_ranges(cr->dd, icg, &jcg0, &jcg1, sh0, sh1);
2307 /* Compute the number of charge groups that fall within the control
2310 naaj = calc_naaj(icg, cgsnr);
2316 /* The i-particle is awlways the test particle,
2317 * so we want all j-particles
2319 max_jcg = cgsnr - 1;
2323 max_jcg = jcg1 - cgsnr;
2328 i_egp_flags = fr->egp_flags + igid*ngid;
2330 /* Set the exclusions for the atoms in charge group icg using a bitmask */
2331 setexcl(i0, cgs->index[icg+1], &top->excls, TRUE, bexcl);
2333 ci2xyz(grid, icg, &cell_x, &cell_y, &cell_z);
2335 /* Changed iicg to icg, DvdS 990115
2336 * (but see consistency check above, DvdS 990330)
2339 fprintf(log, "icg=%5d, naaj=%5d, cell %d %d %d\n",
2340 icg, naaj, cell_x, cell_y, cell_z);
2342 /* Loop over shift vectors in three dimensions */
2343 for (tz = -shp[ZZ]; tz <= shp[ZZ]; tz++)
2345 ZI = cgcm[icg][ZZ]+tz*box[ZZ][ZZ];
2346 /* Calculate range of cells in Z direction that have the shift tz */
2347 zgi = cell_z + tz*Nz;
2350 get_dx(Nz, gridz, rl2, zgi, ZI, &dz0, &dz1, dcz2);
2352 get_dx_dd(Nz, gridz, rl2, zgi, ZI-grid_offset[ZZ],
2353 ncpddc[ZZ], sh0[ZZ], sh1[ZZ], &dz0, &dz1, dcz2);
2359 for (ty = -shp[YY]; ty <= shp[YY]; ty++)
2361 YI = cgcm[icg][YY]+ty*box[YY][YY]+tz*box[ZZ][YY];
2362 /* Calculate range of cells in Y direction that have the shift ty */
2365 ygi = (int)(Ny + (YI - grid_offset[YY])*grid_y) - Ny;
2369 ygi = cell_y + ty*Ny;
2372 get_dx(Ny, gridy, rl2, ygi, YI, &dy0, &dy1, dcy2);
2374 get_dx_dd(Ny, gridy, rl2, ygi, YI-grid_offset[YY],
2375 ncpddc[YY], sh0[YY], sh1[YY], &dy0, &dy1, dcy2);
2381 for (tx = -shp[XX]; tx <= shp[XX]; tx++)
2383 XI = cgcm[icg][XX]+tx*box[XX][XX]+ty*box[YY][XX]+tz*box[ZZ][XX];
2384 /* Calculate range of cells in X direction that have the shift tx */
2387 xgi = (int)(Nx + (XI - grid_offset[XX])*grid_x) - Nx;
2391 xgi = cell_x + tx*Nx;
2394 get_dx(Nx, gridx, rl2, xgi*Nx, XI, &dx0, &dx1, dcx2);
2396 get_dx_dd(Nx, gridx, rl2, xgi, XI-grid_offset[XX],
2397 ncpddc[XX], sh0[XX], sh1[XX], &dx0, &dx1, dcx2);
2403 /* Adress: an explicit cg that has a weigthing function of 0 is excluded
2404 * from the neigbour list as it will not interact */
2405 if (fr->adress_type != eAdressOff)
2407 if (md->wf[cgs->index[icg]] <= GMX_REAL_EPS && egp_explicit(fr, igid))
2412 /* Get shift vector */
2413 shift = XYZ2IS(tx, ty, tz);
2415 range_check(shift, 0, SHIFTS);
2417 for (nn = 0; (nn < ngid); nn++)
2424 fprintf(log, "shift: %2d, dx0,1: %2d,%2d, dy0,1: %2d,%2d, dz0,1: %2d,%2d\n",
2425 shift, dx0, dx1, dy0, dy1, dz0, dz1);
2426 fprintf(log, "cgcm: %8.3f %8.3f %8.3f\n", cgcm[icg][XX],
2427 cgcm[icg][YY], cgcm[icg][ZZ]);
2428 fprintf(log, "xi: %8.3f %8.3f %8.3f\n", XI, YI, ZI);
2430 for (dx = dx0; (dx <= dx1); dx++)
2432 tmp1 = rl2 - dcx2[dx];
2433 for (dy = dy0; (dy <= dy1); dy++)
2435 tmp2 = tmp1 - dcy2[dy];
2438 for (dz = dz0; (dz <= dz1); dz++)
2440 if (tmp2 > dcz2[dz])
2442 /* Find grid-cell cj in which possible neighbours are */
2443 cj = xyz2ci(Ny, Nz, dx, dy, dz);
2445 /* Check out how many cgs (nrj) there in this cell */
2448 /* Find the offset in the cg list */
2451 /* Check if all j's are out of range so we
2452 * can skip the whole cell.
2453 * Should save some time, especially with DD.
2456 (grida[cgj0] >= max_jcg &&
2457 (grida[cgj0] >= jcg1 || grida[cgj0+nrj-1] < jcg0)))
2463 for (j = 0; (j < nrj); j++)
2465 jjcg = grida[cgj0+j];
2467 /* check whether this guy is in range! */
2468 if ((jjcg >= jcg0 && jjcg < jcg1) ||
2471 r2 = calc_dx2(XI, YI, ZI, cgcm[jjcg]);
2474 /* jgid = gid[cgsatoms[cgsindex[jjcg]]]; */
2475 jgid = GET_CGINFO_GID(cginfo[jjcg]);
2476 /* check energy group exclusions */
2477 if (!(i_egp_flags[jgid] & EGP_EXCL))
2481 if (nsr[jgid] >= MAX_CG)
2483 /* Add to short-range list */
2484 put_in_list(bHaveVdW, ngid, md, icg, jgid,
2485 nsr[jgid], nl_sr[jgid],
2486 cgs->index, /* cgsatoms, */ bexcl,
2487 shift, fr, FALSE, TRUE, TRUE, fr->solvent_opt);
2490 nl_sr[jgid][nsr[jgid]++] = jjcg;
2494 if (nlr_ljc[jgid] >= MAX_CG)
2496 /* Add to LJ+coulomb long-range list */
2497 put_in_list(bHaveVdW, ngid, md, icg, jgid,
2498 nlr_ljc[jgid], nl_lr_ljc[jgid], top->cgs.index,
2499 bexcl, shift, fr, TRUE, TRUE, TRUE, fr->solvent_opt);
2502 nl_lr_ljc[jgid][nlr_ljc[jgid]++] = jjcg;
2506 if (nlr_one[jgid] >= MAX_CG)
2508 /* Add to long-range list with only coul, or only LJ */
2509 put_in_list(bHaveVdW, ngid, md, icg, jgid,
2510 nlr_one[jgid], nl_lr_one[jgid], top->cgs.index,
2511 bexcl, shift, fr, TRUE, rvdw_lt_rcoul, rcoul_lt_rvdw, fr->solvent_opt);
2514 nl_lr_one[jgid][nlr_one[jgid]++] = jjcg;
2526 /* CHECK whether there is anything left in the buffers */
2527 for (nn = 0; (nn < ngid); nn++)
2531 put_in_list(bHaveVdW, ngid, md, icg, nn, nsr[nn], nl_sr[nn],
2532 cgs->index, /* cgsatoms, */ bexcl,
2533 shift, fr, FALSE, TRUE, TRUE, fr->solvent_opt);
2536 if (nlr_ljc[nn] > 0)
2538 put_in_list(bHaveVdW, ngid, md, icg, nn, nlr_ljc[nn],
2539 nl_lr_ljc[nn], top->cgs.index,
2540 bexcl, shift, fr, TRUE, TRUE, TRUE, fr->solvent_opt);
2543 if (nlr_one[nn] > 0)
2545 put_in_list(bHaveVdW, ngid, md, icg, nn, nlr_one[nn],
2546 nl_lr_one[nn], top->cgs.index,
2547 bexcl, shift, fr, TRUE, rvdw_lt_rcoul, rcoul_lt_rvdw, fr->solvent_opt);
2553 /* setexcl(nri,i_atoms,&top->atoms.excl,FALSE,bexcl); */
2554 setexcl(cgs->index[icg], cgs->index[icg+1], &top->excls, FALSE, bexcl);
2556 /* No need to perform any left-over force calculations anymore (as we used to do here)
2557 * since we now save the proper long-range lists for later evaluation.
2562 /* Close neighbourlists */
2563 close_neighbor_lists(fr, bMakeQMMMnblist);
2568 void ns_realloc_natoms(gmx_ns_t *ns, int natoms)
2572 if (natoms > ns->nra_alloc)
2574 ns->nra_alloc = over_alloc_dd(natoms);
2575 srenew(ns->bexcl, ns->nra_alloc);
2576 for (i = 0; i < ns->nra_alloc; i++)
2583 void init_ns(FILE *fplog, const t_commrec *cr,
2584 gmx_ns_t *ns, t_forcerec *fr,
2585 const gmx_mtop_t *mtop)
2587 int mt, icg, nr_in_cg, maxcg, i, j, jcg, ngid, ncg;
2591 /* Compute largest charge groups size (# atoms) */
2593 for (mt = 0; mt < mtop->nmoltype; mt++)
2595 cgs = &mtop->moltype[mt].cgs;
2596 for (icg = 0; (icg < cgs->nr); icg++)
2598 nr_in_cg = max(nr_in_cg, (int)(cgs->index[icg+1]-cgs->index[icg]));
2602 /* Verify whether largest charge group is <= max cg.
2603 * This is determined by the type of the local exclusion type
2604 * Exclusions are stored in bits. (If the type is not large
2605 * enough, enlarge it, unsigned char -> unsigned short -> unsigned long)
2607 maxcg = sizeof(t_excl)*8;
2608 if (nr_in_cg > maxcg)
2610 gmx_fatal(FARGS, "Max #atoms in a charge group: %d > %d\n",
2614 ngid = mtop->groups.grps[egcENER].nr;
2615 snew(ns->bExcludeAlleg, ngid);
2616 for (i = 0; i < ngid; i++)
2618 ns->bExcludeAlleg[i] = TRUE;
2619 for (j = 0; j < ngid; j++)
2621 if (!(fr->egp_flags[i*ngid+j] & EGP_EXCL))
2623 ns->bExcludeAlleg[i] = FALSE;
2631 ns->grid = init_grid(fplog, fr);
2632 init_nsgrid_lists(fr, ngid, ns);
2637 snew(ns->ns_buf, ngid);
2638 for (i = 0; (i < ngid); i++)
2640 snew(ns->ns_buf[i], SHIFTS);
2642 ncg = ncg_mtop(mtop);
2643 snew(ns->simple_aaj, 2*ncg);
2644 for (jcg = 0; (jcg < ncg); jcg++)
2646 ns->simple_aaj[jcg] = jcg;
2647 ns->simple_aaj[jcg+ncg] = jcg;
2651 /* Create array that determines whether or not atoms have VdW */
2652 snew(ns->bHaveVdW, fr->ntype);
2653 for (i = 0; (i < fr->ntype); i++)
2655 for (j = 0; (j < fr->ntype); j++)
2657 ns->bHaveVdW[i] = (ns->bHaveVdW[i] ||
2659 ((BHAMA(fr->nbfp, fr->ntype, i, j) != 0) ||
2660 (BHAMB(fr->nbfp, fr->ntype, i, j) != 0) ||
2661 (BHAMC(fr->nbfp, fr->ntype, i, j) != 0)) :
2662 ((C6(fr->nbfp, fr->ntype, i, j) != 0) ||
2663 (C12(fr->nbfp, fr->ntype, i, j) != 0))));
2668 pr_bvec(debug, 0, "bHaveVdW", ns->bHaveVdW, fr->ntype, TRUE);
2673 if (!DOMAINDECOMP(cr))
2675 ns_realloc_natoms(ns, mtop->natoms);
2678 ns->nblist_initialized = FALSE;
2680 /* nbr list debug dump */
2682 char *ptr = getenv("GMX_DUMP_NL");
2685 ns->dump_nl = strtol(ptr, NULL, 10);
2688 fprintf(fplog, "GMX_DUMP_NL = %d", ns->dump_nl);
2699 int search_neighbours(FILE *log, t_forcerec *fr,
2701 gmx_localtop_t *top,
2702 gmx_groups_t *groups,
2704 t_nrnb *nrnb, t_mdatoms *md,
2706 gmx_bool bDoLongRangeNS)
2708 t_block *cgs = &(top->cgs);
2709 rvec box_size, grid_x0, grid_x1;
2711 real min_size, grid_dens;
2715 gmx_bool *i_egp_flags;
2716 int cg_start, cg_end, start, end;
2719 gmx_domdec_zones_t *dd_zones;
2720 put_in_list_t *put_in_list;
2724 /* Set some local variables */
2726 ngid = groups->grps[egcENER].nr;
2728 for (m = 0; (m < DIM); m++)
2730 box_size[m] = box[m][m];
2733 if (fr->ePBC != epbcNONE)
2735 if (sqr(fr->rlistlong) >= max_cutoff2(fr->ePBC, box))
2737 gmx_fatal(FARGS, "One of the box vectors has become shorter than twice the cut-off length or box_yy-|box_zy| or box_zz has become smaller than the cut-off.");
2741 min_size = min(box_size[XX], min(box_size[YY], box_size[ZZ]));
2742 if (2*fr->rlistlong >= min_size)
2744 gmx_fatal(FARGS, "One of the box diagonal elements has become smaller than twice the cut-off length.");
2749 if (DOMAINDECOMP(cr))
2751 ns_realloc_natoms(ns, cgs->index[cgs->nr]);
2755 /* Reset the neighbourlists */
2756 reset_neighbor_lists(fr, TRUE, TRUE);
2758 if (bGrid && bFillGrid)
2762 if (DOMAINDECOMP(cr))
2764 dd_zones = domdec_zones(cr->dd);
2770 get_nsgrid_boundaries(grid->nboundeddim, box, NULL, NULL, NULL, NULL,
2771 cgs->nr, fr->cg_cm, grid_x0, grid_x1, &grid_dens);
2773 grid_first(log, grid, NULL, NULL, box, grid_x0, grid_x1,
2774 fr->rlistlong, grid_dens);
2781 if (DOMAINDECOMP(cr))
2784 fill_grid(dd_zones, grid, end, -1, end, fr->cg_cm);
2786 grid->icg1 = dd_zones->izone[dd_zones->nizone-1].cg1;
2790 fill_grid(NULL, grid, cgs->nr, fr->cg0, fr->hcg, fr->cg_cm);
2791 grid->icg0 = fr->cg0;
2792 grid->icg1 = fr->hcg;
2796 calc_elemnr(grid, start, end, cgs->nr);
2798 grid_last(grid, start, end, cgs->nr);
2803 print_grid(debug, grid);
2808 /* Set the grid cell index for the test particle only.
2809 * The cell to cg index is not corrected, but that does not matter.
2811 fill_grid(NULL, ns->grid, fr->hcg, fr->hcg-1, fr->hcg, fr->cg_cm);
2815 if (fr->adress_type == eAdressOff)
2817 if (!fr->ns.bCGlist)
2819 put_in_list = put_in_list_at;
2823 put_in_list = put_in_list_cg;
2828 put_in_list = put_in_list_adress;
2835 nsearch = nsgrid_core(cr, fr, box, ngid, top,
2836 grid, ns->bexcl, ns->bExcludeAlleg,
2837 md, put_in_list, ns->bHaveVdW,
2838 bDoLongRangeNS, FALSE);
2840 /* neighbour searching withouth QMMM! QM atoms have zero charge in
2841 * the classical calculation. The charge-charge interaction
2842 * between QM and MM atoms is handled in the QMMM core calculation
2843 * (see QMMM.c). The VDW however, we'd like to compute classically
2844 * and the QM MM atom pairs have just been put in the
2845 * corresponding neighbourlists. in case of QMMM we still need to
2846 * fill a special QMMM neighbourlist that contains all neighbours
2847 * of the QM atoms. If bQMMM is true, this list will now be made:
2849 if (fr->bQMMM && fr->qr->QMMMscheme != eQMMMschemeoniom)
2851 nsearch += nsgrid_core(cr, fr, box, ngid, top,
2852 grid, ns->bexcl, ns->bExcludeAlleg,
2853 md, put_in_list_qmmm, ns->bHaveVdW,
2854 bDoLongRangeNS, TRUE);
2859 nsearch = ns_simple_core(fr, top, md, box, box_size,
2860 ns->bexcl, ns->simple_aaj,
2861 ngid, ns->ns_buf, put_in_list, ns->bHaveVdW);
2869 inc_nrnb(nrnb, eNR_NS, nsearch);
2870 /* inc_nrnb(nrnb,eNR_LR,fr->nlr); */
2875 int natoms_beyond_ns_buffer(t_inputrec *ir, t_forcerec *fr, t_block *cgs,
2876 matrix scale_tot, rvec *x)
2878 int cg0, cg1, cg, a0, a1, a, i, j;
2879 real rint, hbuf2, scale;
2881 gmx_bool bIsotropic;
2886 rint = max(ir->rcoulomb, ir->rvdw);
2887 if (ir->rlist < rint)
2889 gmx_fatal(FARGS, "The neighbor search buffer has negative size: %f nm",
2897 if (!EI_DYNAMICS(ir->eI) || !DYNAMIC_BOX(*ir))
2899 hbuf2 = sqr(0.5*(ir->rlist - rint));
2900 for (cg = cg0; cg < cg1; cg++)
2902 a0 = cgs->index[cg];
2903 a1 = cgs->index[cg+1];
2904 for (a = a0; a < a1; a++)
2906 if (distance2(cg_cm[cg], x[a]) > hbuf2)
2916 scale = scale_tot[0][0];
2917 for (i = 1; i < DIM; i++)
2919 /* With anisotropic scaling, the original spherical ns volumes become
2920 * ellipsoids. To avoid costly transformations we use the minimum
2921 * eigenvalue of the scaling matrix for determining the buffer size.
2922 * Since the lower half is 0, the eigenvalues are the diagonal elements.
2924 scale = min(scale, scale_tot[i][i]);
2925 if (scale_tot[i][i] != scale_tot[i-1][i-1])
2929 for (j = 0; j < i; j++)
2931 if (scale_tot[i][j] != 0)
2937 hbuf2 = sqr(0.5*(scale*ir->rlist - rint));
2940 for (cg = cg0; cg < cg1; cg++)
2942 svmul(scale, cg_cm[cg], cgsc);
2943 a0 = cgs->index[cg];
2944 a1 = cgs->index[cg+1];
2945 for (a = a0; a < a1; a++)
2947 if (distance2(cgsc, x[a]) > hbuf2)
2956 /* Anistropic scaling */
2957 for (cg = cg0; cg < cg1; cg++)
2959 /* Since scale_tot contains the transpose of the scaling matrix,
2960 * we need to multiply with the transpose.
2962 tmvmul_ur0(scale_tot, cg_cm[cg], cgsc);
2963 a0 = cgs->index[cg];
2964 a1 = cgs->index[cg+1];
2965 for (a = a0; a < a1; a++)
2967 if (distance2(cgsc, x[a]) > hbuf2)