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 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
52 #include "nonbonded.h"
56 #include "gmx_fatal.h"
59 #include "mtop_util.h"
66 * E X C L U S I O N H A N D L I N G
70 static void SETEXCL_(t_excl e[],atom_id i,atom_id j)
71 { e[j] = e[j] | (1<<i); }
72 static void RMEXCL_(t_excl e[],atom_id i,atom_id j)
73 { e[j]=e[j] & ~(1<<i); }
74 static gmx_bool ISEXCL_(t_excl e[],atom_id i,atom_id j)
75 { return (gmx_bool)(e[j] & (1<<i)); }
76 static gmx_bool NOTEXCL_(t_excl e[],atom_id i,atom_id j)
77 { return !(ISEXCL(e,i,j)); }
79 #define SETEXCL(e,i,j) (e)[((atom_id) (j))] |= (1<<((atom_id) (i)))
80 #define RMEXCL(e,i,j) (e)[((atom_id) (j))] &= (~(1<<((atom_id) (i))))
81 #define ISEXCL(e,i,j) (gmx_bool) ((e)[((atom_id) (j))] & (1<<((atom_id) (i))))
82 #define NOTEXCL(e,i,j) !(ISEXCL(e,i,j))
86 round_up_to_simd_width(int length, int simd_width)
90 offset = (simd_width>0) ? length % simd_width : 0;
92 return (offset==0) ? length : length-offset+simd_width;
94 /************************************************
96 * U T I L I T I E S F O R N S
98 ************************************************/
100 static void reallocate_nblist(t_nblist *nl)
104 fprintf(debug,"reallocating neigborlist (ielec=%d, ivdw=%d, igeometry=%d, free_energy=%d), maxnri=%d\n",
105 nl->ielec,nl->ivdw,nl->igeometry,nl->free_energy,nl->maxnri);
107 srenew(nl->iinr, nl->maxnri);
108 if (nl->igeometry == GMX_NBLIST_GEOMETRY_CG_CG)
110 srenew(nl->iinr_end,nl->maxnri);
112 srenew(nl->gid, nl->maxnri);
113 srenew(nl->shift, nl->maxnri);
114 srenew(nl->jindex, nl->maxnri+1);
118 static void init_nblist(FILE *log, t_nblist *nl_sr,t_nblist *nl_lr,
120 int ivdw, int ivdwmod,
121 int ielec, int ielecmod,
122 gmx_bool bfree, int igeometry)
130 nl = (i == 0) ? nl_sr : nl_lr;
131 homenr = (i == 0) ? maxsr : maxlr;
139 /* Set coul/vdw in neighborlist, and for the normal loops we determine
140 * an index of which one to call.
143 nl->ivdwmod = ivdwmod;
145 nl->ielecmod = ielecmod;
146 nl->free_energy = bfree;
147 nl->igeometry = igeometry;
151 nl->igeometry = GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE;
154 /* This will also set the simd_padding_width field */
155 gmx_nonbonded_set_kernel_pointers( (i==0) ? log : NULL,nl);
157 /* maxnri is influenced by the number of shifts (maximum is 8)
158 * and the number of energy groups.
159 * If it is not enough, nl memory will be reallocated during the run.
160 * 4 seems to be a reasonable factor, which only causes reallocation
161 * during runs with tiny and many energygroups.
163 nl->maxnri = homenr*4;
172 reallocate_nblist(nl);
177 fprintf(debug,"Initiating neighbourlist (ielec=%d, ivdw=%d, free=%d) for %s interactions,\nwith %d SR, %d LR atoms.\n",
178 nl->ielec,nl->ivdw,nl->free_energy,gmx_nblist_geometry_names[nl->igeometry],maxsr,maxlr);
183 void init_neighbor_list(FILE *log,t_forcerec *fr,int homenr)
185 /* Make maxlr tunable! (does not seem to be a big difference though)
186 * This parameter determines the number of i particles in a long range
187 * neighbourlist. Too few means many function calls, too many means
190 int maxsr,maxsr_wat,maxlr,maxlr_wat;
191 int ielec,ielecf,ivdw,ielecmod,ielecmodf,ivdwmod;
193 int igeometry_def,igeometry_w,igeometry_ww;
197 /* maxsr = homenr-fr->nWatMol*3; */
202 gmx_fatal(FARGS,"%s, %d: Negative number of short range atoms.\n"
203 "Call your Gromacs dealer for assistance.",__FILE__,__LINE__);
205 /* This is just for initial allocation, so we do not reallocate
206 * all the nlist arrays many times in a row.
207 * The numbers seem very accurate, but they are uncritical.
209 maxsr_wat = min(fr->nWatMol,(homenr+2)/3);
213 maxlr_wat = min(maxsr_wat,maxlr);
217 maxlr = maxlr_wat = 0;
220 /* Determine the values for ielec/ivdw. */
221 ielec = fr->nbkernel_elec_interaction;
222 ivdw = fr->nbkernel_vdw_interaction;
223 ielecmod = fr->nbkernel_elec_modifier;
224 ivdwmod = fr->nbkernel_vdw_modifier;
226 fr->ns.bCGlist = (getenv("GMX_NBLISTCG") != 0);
229 igeometry_def = GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE;
233 igeometry_def = GMX_NBLIST_GEOMETRY_CG_CG;
236 fprintf(log,"\nUsing charge-group - charge-group neighbor lists and kernels\n\n");
240 if (fr->solvent_opt == esolTIP4P) {
241 igeometry_w = GMX_NBLIST_GEOMETRY_WATER4_PARTICLE;
242 igeometry_ww = GMX_NBLIST_GEOMETRY_WATER4_WATER4;
244 igeometry_w = GMX_NBLIST_GEOMETRY_WATER3_PARTICLE;
245 igeometry_ww = GMX_NBLIST_GEOMETRY_WATER3_WATER3;
248 for(i=0; i<fr->nnblists; i++)
250 nbl = &(fr->nblists[i]);
251 init_nblist(log,&nbl->nlist_sr[eNL_VDWQQ],&nbl->nlist_lr[eNL_VDWQQ],
252 maxsr,maxlr,ivdw,ivdwmod,ielec,ielecmod,FALSE,igeometry_def);
253 init_nblist(log,&nbl->nlist_sr[eNL_VDW],&nbl->nlist_lr[eNL_VDW],
254 maxsr,maxlr,ivdw,ivdwmod,GMX_NBKERNEL_ELEC_NONE,eintmodNONE,FALSE,igeometry_def);
255 init_nblist(log,&nbl->nlist_sr[eNL_QQ],&nbl->nlist_lr[eNL_QQ],
256 maxsr,maxlr,GMX_NBKERNEL_VDW_NONE,eintmodNONE,ielec,ielecmod,FALSE,igeometry_def);
257 init_nblist(log,&nbl->nlist_sr[eNL_VDWQQ_WATER],&nbl->nlist_lr[eNL_VDWQQ_WATER],
258 maxsr_wat,maxlr_wat,ivdw,ivdwmod,ielec,ielecmod, FALSE,igeometry_w);
259 init_nblist(log,&nbl->nlist_sr[eNL_QQ_WATER],&nbl->nlist_lr[eNL_QQ_WATER],
260 maxsr_wat,maxlr_wat,GMX_NBKERNEL_VDW_NONE,eintmodNONE,ielec,ielecmod, FALSE,igeometry_w);
261 init_nblist(log,&nbl->nlist_sr[eNL_VDWQQ_WATERWATER],&nbl->nlist_lr[eNL_VDWQQ_WATERWATER],
262 maxsr_wat,maxlr_wat,ivdw,ivdwmod,ielec,ielecmod, FALSE,igeometry_ww);
263 init_nblist(log,&nbl->nlist_sr[eNL_QQ_WATERWATER],&nbl->nlist_lr[eNL_QQ_WATERWATER],
264 maxsr_wat,maxlr_wat,GMX_NBKERNEL_VDW_NONE,eintmodNONE,ielec,ielecmod, FALSE,igeometry_ww);
266 /* Did we get the solvent loops so we can use optimized water kernels? */
267 if(nbl->nlist_sr[eNL_VDWQQ_WATER].kernelptr_vf==NULL
268 || nbl->nlist_sr[eNL_QQ_WATER].kernelptr_vf==NULL
269 #ifndef DISABLE_WATERWATER_NLIST
270 || nbl->nlist_sr[eNL_VDWQQ_WATERWATER].kernelptr_vf==NULL
271 || nbl->nlist_sr[eNL_QQ_WATERWATER].kernelptr_vf==NULL
275 fr->solvent_opt = esolNO;
276 fprintf(log,"Note: The available nonbonded kernels do not support water optimization - disabling.\n");
279 if (fr->efep != efepNO)
281 if ((fr->bEwald) && (fr->sc_alphacoul > 0)) /* need to handle long range differently if using softcore */
283 ielecf = GMX_NBKERNEL_ELEC_EWALD;
284 ielecmodf = eintmodNONE;
289 ielecmodf = ielecmod;
292 init_nblist(log,&nbl->nlist_sr[eNL_VDWQQ_FREE],&nbl->nlist_lr[eNL_VDWQQ_FREE],
293 maxsr,maxlr,ivdw,ivdwmod,ielecf,ielecmod,TRUE,GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE);
294 init_nblist(log,&nbl->nlist_sr[eNL_VDW_FREE],&nbl->nlist_lr[eNL_VDW_FREE],
295 maxsr,maxlr,ivdw,ivdwmod,GMX_NBKERNEL_ELEC_NONE,eintmodNONE,TRUE,GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE);
296 init_nblist(log,&nbl->nlist_sr[eNL_QQ_FREE],&nbl->nlist_lr[eNL_QQ_FREE],
297 maxsr,maxlr,GMX_NBKERNEL_VDW_NONE,eintmodNONE,ielecf,ielecmod,TRUE,GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE);
301 if (fr->bQMMM && fr->qr->QMMMscheme != eQMMMschemeoniom)
303 init_nblist(log,&fr->QMMMlist,NULL,
304 maxsr,maxlr,0,0,ielec,ielecmod,FALSE,GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE);
312 fr->ns.nblist_initialized=TRUE;
315 static void reset_nblist(t_nblist *nl)
326 static void reset_neighbor_lists(t_forcerec *fr,gmx_bool bResetSR, gmx_bool bResetLR)
332 /* only reset the short-range nblist */
333 reset_nblist(&(fr->QMMMlist));
336 for(n=0; n<fr->nnblists; n++)
338 for(i=0; i<eNL_NR; i++)
342 reset_nblist( &(fr->nblists[n].nlist_sr[i]) );
346 reset_nblist( &(fr->nblists[n].nlist_lr[i]) );
355 static inline void new_i_nblist(t_nblist *nlist,
356 gmx_bool bLR,atom_id i_atom,int shift,int gid)
362 /* Check whether we have to increase the i counter */
364 (nlist->iinr[nri] != i_atom) ||
365 (nlist->shift[nri] != shift) ||
366 (nlist->gid[nri] != gid))
368 /* This is something else. Now see if any entries have
369 * been added in the list of the previous atom.
372 ((nlist->jindex[nri+1] > nlist->jindex[nri]) &&
373 (nlist->gid[nri] != -1)))
375 /* If so increase the counter */
378 if (nlist->nri >= nlist->maxnri)
380 nlist->maxnri += over_alloc_large(nlist->nri);
381 reallocate_nblist(nlist);
384 /* Set the number of neighbours and the atom number */
385 nlist->jindex[nri+1] = nlist->jindex[nri];
386 nlist->iinr[nri] = i_atom;
387 nlist->gid[nri] = gid;
388 nlist->shift[nri] = shift;
392 static inline void close_i_nblist(t_nblist *nlist)
394 int nri = nlist->nri;
399 /* Add elements up to padding. Since we allocate memory in units
400 * of the simd_padding width, we do not have to check for possible
401 * list reallocation here.
403 while((nlist->nrj % nlist->simd_padding_width)!=0)
405 /* Use -4 here, so we can write forces for 4 atoms before real data */
406 nlist->jjnr[nlist->nrj++]=-4;
408 nlist->jindex[nri+1] = nlist->nrj;
410 len=nlist->nrj - nlist->jindex[nri];
412 /* nlist length for water i molecules is treated statically
415 if (len > nlist->maxlen)
422 static inline void close_nblist(t_nblist *nlist)
424 /* Only close this nblist when it has been initialized.
425 * Avoid the creation of i-lists with no j-particles.
429 /* Some assembly kernels do not support empty lists,
430 * make sure here that we don't generate any empty lists.
431 * With the current ns code this branch is taken in two cases:
432 * No i-particles at all: nri=-1 here
433 * There are i-particles, but no j-particles; nri=0 here
439 /* Close list number nri by incrementing the count */
444 static inline void close_neighbor_lists(t_forcerec *fr,gmx_bool bMakeQMMMnblist)
450 close_nblist(&(fr->QMMMlist));
453 for(n=0; n<fr->nnblists; n++)
455 for(i=0; (i<eNL_NR); i++)
457 close_nblist(&(fr->nblists[n].nlist_sr[i]));
458 close_nblist(&(fr->nblists[n].nlist_lr[i]));
464 static inline void add_j_to_nblist(t_nblist *nlist,atom_id j_atom,gmx_bool bLR)
468 if (nlist->nrj >= nlist->maxnrj)
470 nlist->maxnrj = round_up_to_simd_width(over_alloc_small(nlist->nrj + 1),nlist->simd_padding_width);
473 fprintf(debug,"Increasing %s nblist (ielec=%d,ivdw=%d,free=%d,igeometry=%d) j size to %d\n",
474 bLR ? "LR" : "SR",nlist->ielec,nlist->ivdw,nlist->free_energy,nlist->igeometry,nlist->maxnrj);
476 srenew(nlist->jjnr,nlist->maxnrj);
479 nlist->jjnr[nrj] = j_atom;
483 static inline void add_j_to_nblist_cg(t_nblist *nlist,
484 atom_id j_start,int j_end,
485 t_excl *bexcl,gmx_bool i_is_j,
491 if (nlist->nrj >= nlist->maxnrj)
493 nlist->maxnrj = over_alloc_small(nlist->nrj + 1);
495 fprintf(debug,"Increasing %s nblist (ielec=%d,ivdw=%d,free=%d,igeometry=%d) j size to %d\n",
496 bLR ? "LR" : "SR",nlist->ielec,nlist->ivdw,nlist->free_energy,nlist->igeometry,nlist->maxnrj);
498 srenew(nlist->jjnr ,nlist->maxnrj);
499 srenew(nlist->jjnr_end,nlist->maxnrj);
500 srenew(nlist->excl ,nlist->maxnrj*MAX_CGCGSIZE);
503 nlist->jjnr[nrj] = j_start;
504 nlist->jjnr_end[nrj] = j_end;
506 if (j_end - j_start > MAX_CGCGSIZE)
508 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);
511 /* Set the exclusions */
512 for(j=j_start; j<j_end; j++)
514 nlist->excl[nrj*MAX_CGCGSIZE + j - j_start] = bexcl[j];
518 /* Avoid double counting of intra-cg interactions */
519 for(j=1; j<j_end-j_start; j++)
521 nlist->excl[nrj*MAX_CGCGSIZE + j] |= (1<<j) - 1;
529 put_in_list_t(gmx_bool bHaveVdW[],
546 put_in_list_at(gmx_bool bHaveVdW[],
562 /* The a[] index has been removed,
563 * to put it back in i_atom should be a[i0] and jj should be a[jj].
568 t_nblist * vdwc_free = NULL;
569 t_nblist * vdw_free = NULL;
570 t_nblist * coul_free = NULL;
571 t_nblist * vdwc_ww = NULL;
572 t_nblist * coul_ww = NULL;
574 int i,j,jcg,igid,gid,nbl_ind,ind_ij;
575 atom_id jj,jj0,jj1,i_atom;
580 real *charge,*chargeB;
582 gmx_bool bFreeEnergy,bFree,bFreeJ,bNotEx,*bPert;
583 gmx_bool bDoVdW_i,bDoCoul_i,bDoCoul_i_sol;
587 /* Copy some pointers */
589 charge = md->chargeA;
590 chargeB = md->chargeB;
593 bPert = md->bPerturbed;
597 nicg = index[icg+1]-i0;
599 /* Get the i charge group info */
600 igid = GET_CGINFO_GID(cginfo[icg]);
602 iwater = (solvent_opt!=esolNO) ? GET_CGINFO_SOLOPT(cginfo[icg]) : esolNO;
607 /* Check if any of the particles involved are perturbed.
608 * If not we can do the cheaper normal put_in_list
609 * and use more solvent optimization.
611 for(i=0; i<nicg; i++)
613 bFreeEnergy |= bPert[i0+i];
615 /* Loop over the j charge groups */
616 for(j=0; (j<nj && !bFreeEnergy); j++)
621 /* Finally loop over the atoms in the j-charge group */
622 for(jj=jj0; jj<jj1; jj++)
624 bFreeEnergy |= bPert[jj];
629 /* Unpack pointers to neighbourlist structs */
630 if (fr->nnblists == 1)
636 nbl_ind = fr->gid2nblists[GID(igid,jgid,ngid)];
640 nlist = fr->nblists[nbl_ind].nlist_lr;
644 nlist = fr->nblists[nbl_ind].nlist_sr;
647 if (iwater != esolNO)
649 vdwc = &nlist[eNL_VDWQQ_WATER];
650 vdw = &nlist[eNL_VDW];
651 coul = &nlist[eNL_QQ_WATER];
652 #ifndef DISABLE_WATERWATER_NLIST
653 vdwc_ww = &nlist[eNL_VDWQQ_WATERWATER];
654 coul_ww = &nlist[eNL_QQ_WATERWATER];
659 vdwc = &nlist[eNL_VDWQQ];
660 vdw = &nlist[eNL_VDW];
661 coul = &nlist[eNL_QQ];
666 if (iwater != esolNO)
668 /* Loop over the atoms in the i charge group */
670 gid = GID(igid,jgid,ngid);
671 /* Create new i_atom for each energy group */
672 if (bDoCoul && bDoVdW)
674 new_i_nblist(vdwc,bLR,i_atom,shift,gid);
675 #ifndef DISABLE_WATERWATER_NLIST
676 new_i_nblist(vdwc_ww,bLR,i_atom,shift,gid);
681 new_i_nblist(vdw,bLR,i_atom,shift,gid);
685 new_i_nblist(coul,bLR,i_atom,shift,gid);
686 #ifndef DISABLE_WATERWATER_NLIST
687 new_i_nblist(coul_ww,bLR,i_atom,shift,gid);
690 /* Loop over the j charge groups */
691 for(j=0; (j<nj); j++)
701 jwater = GET_CGINFO_SOLOPT(cginfo[jcg]);
703 if (iwater == esolSPC && jwater == esolSPC)
705 /* Interaction between two SPC molecules */
708 /* VdW only - only first atoms in each water interact */
709 add_j_to_nblist(vdw,jj0,bLR);
713 #ifdef DISABLE_WATERWATER_NLIST
714 /* Add entries for the three atoms - only do VdW if we need to */
717 add_j_to_nblist(coul,jj0,bLR);
721 add_j_to_nblist(vdwc,jj0,bLR);
723 add_j_to_nblist(coul,jj0+1,bLR);
724 add_j_to_nblist(coul,jj0+2,bLR);
726 /* One entry for the entire water-water interaction */
729 add_j_to_nblist(coul_ww,jj0,bLR);
733 add_j_to_nblist(vdwc_ww,jj0,bLR);
738 else if (iwater == esolTIP4P && jwater == esolTIP4P)
740 /* Interaction between two TIP4p molecules */
743 /* VdW only - only first atoms in each water interact */
744 add_j_to_nblist(vdw,jj0,bLR);
748 #ifdef DISABLE_WATERWATER_NLIST
749 /* Add entries for the four atoms - only do VdW if we need to */
752 add_j_to_nblist(vdw,jj0,bLR);
754 add_j_to_nblist(coul,jj0+1,bLR);
755 add_j_to_nblist(coul,jj0+2,bLR);
756 add_j_to_nblist(coul,jj0+3,bLR);
758 /* One entry for the entire water-water interaction */
761 add_j_to_nblist(coul_ww,jj0,bLR);
765 add_j_to_nblist(vdwc_ww,jj0,bLR);
772 /* j charge group is not water, but i is.
773 * Add entries to the water-other_atom lists; the geometry of the water
774 * molecule doesn't matter - that is taken care of in the nonbonded kernel,
775 * so we don't care if it is SPC or TIP4P...
782 for(jj=jj0; (jj<jj1); jj++)
786 add_j_to_nblist(coul,jj,bLR);
792 for(jj=jj0; (jj<jj1); jj++)
794 if (bHaveVdW[type[jj]])
796 add_j_to_nblist(vdw,jj,bLR);
802 /* _charge_ _groups_ interact with both coulomb and LJ */
803 /* Check which atoms we should add to the lists! */
804 for(jj=jj0; (jj<jj1); jj++)
806 if (bHaveVdW[type[jj]])
810 add_j_to_nblist(vdwc,jj,bLR);
814 add_j_to_nblist(vdw,jj,bLR);
817 else if (charge[jj] != 0)
819 add_j_to_nblist(coul,jj,bLR);
826 close_i_nblist(coul);
827 close_i_nblist(vdwc);
828 #ifndef DISABLE_WATERWATER_NLIST
829 close_i_nblist(coul_ww);
830 close_i_nblist(vdwc_ww);
835 /* no solvent as i charge group */
836 /* Loop over the atoms in the i charge group */
837 for(i=0; i<nicg; i++)
840 gid = GID(igid,jgid,ngid);
843 /* Create new i_atom for each energy group */
844 if (bDoVdW && bDoCoul)
846 new_i_nblist(vdwc,bLR,i_atom,shift,gid);
850 new_i_nblist(vdw,bLR,i_atom,shift,gid);
854 new_i_nblist(coul,bLR,i_atom,shift,gid);
856 bDoVdW_i = (bDoVdW && bHaveVdW[type[i_atom]]);
857 bDoCoul_i = (bDoCoul && qi!=0);
859 if (bDoVdW_i || bDoCoul_i)
861 /* Loop over the j charge groups */
862 for(j=0; (j<nj); j++)
866 /* Check for large charge groups */
877 /* Finally loop over the atoms in the j-charge group */
878 for(jj=jj0; jj<jj1; jj++)
880 bNotEx = NOTEXCL(bExcl,i,jj);
888 add_j_to_nblist(coul,jj,bLR);
893 if (bHaveVdW[type[jj]])
895 add_j_to_nblist(vdw,jj,bLR);
900 if (bHaveVdW[type[jj]])
904 add_j_to_nblist(vdwc,jj,bLR);
908 add_j_to_nblist(vdw,jj,bLR);
911 else if (charge[jj] != 0)
913 add_j_to_nblist(coul,jj,bLR);
921 close_i_nblist(coul);
922 close_i_nblist(vdwc);
928 /* we are doing free energy */
929 vdwc_free = &nlist[eNL_VDWQQ_FREE];
930 vdw_free = &nlist[eNL_VDW_FREE];
931 coul_free = &nlist[eNL_QQ_FREE];
932 /* Loop over the atoms in the i charge group */
933 for(i=0; i<nicg; i++)
936 gid = GID(igid,jgid,ngid);
938 qiB = chargeB[i_atom];
940 /* Create new i_atom for each energy group */
941 if (bDoVdW && bDoCoul)
942 new_i_nblist(vdwc,bLR,i_atom,shift,gid);
944 new_i_nblist(vdw,bLR,i_atom,shift,gid);
946 new_i_nblist(coul,bLR,i_atom,shift,gid);
948 new_i_nblist(vdw_free,bLR,i_atom,shift,gid);
949 new_i_nblist(coul_free,bLR,i_atom,shift,gid);
950 new_i_nblist(vdwc_free,bLR,i_atom,shift,gid);
952 bDoVdW_i = (bDoVdW &&
953 (bHaveVdW[type[i_atom]] || bHaveVdW[typeB[i_atom]]));
954 bDoCoul_i = (bDoCoul && (qi!=0 || qiB!=0));
955 /* For TIP4P the first atom does not have a charge,
956 * but the last three do. So we should still put an atom
957 * without LJ but with charge in the water-atom neighborlist
958 * for a TIP4p i charge group.
959 * For SPC type water the first atom has LJ and charge,
960 * so there is no such problem.
962 if (iwater == esolNO)
964 bDoCoul_i_sol = bDoCoul_i;
968 bDoCoul_i_sol = bDoCoul;
971 if (bDoVdW_i || bDoCoul_i_sol)
973 /* Loop over the j charge groups */
974 for(j=0; (j<nj); j++)
978 /* Check for large charge groups */
989 /* Finally loop over the atoms in the j-charge group */
990 bFree = bPert[i_atom];
991 for(jj=jj0; (jj<jj1); jj++)
993 bFreeJ = bFree || bPert[jj];
994 /* Complicated if, because the water H's should also
995 * see perturbed j-particles
997 if (iwater==esolNO || i==0 || bFreeJ)
999 bNotEx = NOTEXCL(bExcl,i,jj);
1007 if (charge[jj]!=0 || chargeB[jj]!=0)
1009 add_j_to_nblist(coul_free,jj,bLR);
1012 else if (!bDoCoul_i)
1014 if (bHaveVdW[type[jj]] || bHaveVdW[typeB[jj]])
1016 add_j_to_nblist(vdw_free,jj,bLR);
1021 if (bHaveVdW[type[jj]] || bHaveVdW[typeB[jj]])
1023 if (charge[jj]!=0 || chargeB[jj]!=0)
1025 add_j_to_nblist(vdwc_free,jj,bLR);
1029 add_j_to_nblist(vdw_free,jj,bLR);
1032 else if (charge[jj]!=0 || chargeB[jj]!=0)
1033 add_j_to_nblist(coul_free,jj,bLR);
1038 /* This is done whether or not bWater is set */
1039 if (charge[jj] != 0)
1041 add_j_to_nblist(coul,jj,bLR);
1044 else if (!bDoCoul_i_sol)
1046 if (bHaveVdW[type[jj]])
1048 add_j_to_nblist(vdw,jj,bLR);
1053 if (bHaveVdW[type[jj]])
1055 if (charge[jj] != 0)
1057 add_j_to_nblist(vdwc,jj,bLR);
1061 add_j_to_nblist(vdw,jj,bLR);
1064 else if (charge[jj] != 0)
1066 add_j_to_nblist(coul,jj,bLR);
1074 close_i_nblist(vdw);
1075 close_i_nblist(coul);
1076 close_i_nblist(vdwc);
1077 close_i_nblist(vdw_free);
1078 close_i_nblist(coul_free);
1079 close_i_nblist(vdwc_free);
1085 put_in_list_qmmm(gmx_bool bHaveVdW[],
1102 int i,j,jcg,igid,gid;
1103 atom_id jj,jj0,jj1,i_atom;
1107 /* Get atom range */
1109 nicg = index[icg+1]-i0;
1111 /* Get the i charge group info */
1112 igid = GET_CGINFO_GID(fr->cginfo[icg]);
1114 coul = &fr->QMMMlist;
1116 /* Loop over atoms in the ith charge group */
1117 for (i=0;i<nicg;i++)
1120 gid = GID(igid,jgid,ngid);
1121 /* Create new i_atom for each energy group */
1122 new_i_nblist(coul,bLR,i_atom,shift,gid);
1124 /* Loop over the j charge groups */
1129 /* Charge groups cannot have QM and MM atoms simultaneously */
1134 /* Finally loop over the atoms in the j-charge group */
1135 for(jj=jj0; jj<jj1; jj++)
1137 bNotEx = NOTEXCL(bExcl,i,jj);
1139 add_j_to_nblist(coul,jj,bLR);
1143 close_i_nblist(coul);
1148 put_in_list_cg(gmx_bool bHaveVdW[],
1165 int igid,gid,nbl_ind;
1169 cginfo = fr->cginfo[icg];
1171 igid = GET_CGINFO_GID(cginfo);
1172 gid = GID(igid,jgid,ngid);
1174 /* Unpack pointers to neighbourlist structs */
1175 if (fr->nnblists == 1)
1181 nbl_ind = fr->gid2nblists[gid];
1185 vdwc = &fr->nblists[nbl_ind].nlist_lr[eNL_VDWQQ];
1189 vdwc = &fr->nblists[nbl_ind].nlist_sr[eNL_VDWQQ];
1192 /* Make a new neighbor list for charge group icg.
1193 * Currently simply one neighbor list is made with LJ and Coulomb.
1194 * If required, zero interactions could be removed here
1195 * or in the force loop.
1197 new_i_nblist(vdwc,bLR,index[icg],shift,gid);
1198 vdwc->iinr_end[vdwc->nri] = index[icg+1];
1200 for(j=0; (j<nj); j++)
1203 /* Skip the icg-icg pairs if all self interactions are excluded */
1204 if (!(jcg == icg && GET_CGINFO_EXCL_INTRA(cginfo)))
1206 /* Here we add the j charge group jcg to the list,
1207 * exclusions are also added to the list.
1209 add_j_to_nblist_cg(vdwc,index[jcg],index[jcg+1],bExcl,icg==jcg,bLR);
1213 close_i_nblist(vdwc);
1216 static void setexcl(atom_id start,atom_id end,t_blocka *excl,gmx_bool b,
1223 for(i=start; i<end; i++)
1225 for(k=excl->index[i]; k<excl->index[i+1]; k++)
1227 SETEXCL(bexcl,i-start,excl->a[k]);
1233 for(i=start; i<end; i++)
1235 for(k=excl->index[i]; k<excl->index[i+1]; k++)
1237 RMEXCL(bexcl,i-start,excl->a[k]);
1243 int calc_naaj(int icg,int cgtot)
1247 if ((cgtot % 2) == 1)
1249 /* Odd number of charge groups, easy */
1250 naaj = 1 + (cgtot/2);
1252 else if ((cgtot % 4) == 0)
1254 /* Multiple of four is hard */
1291 fprintf(log,"naaj=%d\n",naaj);
1297 /************************************************
1299 * S I M P L E C O R E S T U F F
1301 ************************************************/
1303 static real calc_image_tric(rvec xi,rvec xj,matrix box,
1304 rvec b_inv,int *shift)
1306 /* This code assumes that the cut-off is smaller than
1307 * a half times the smallest diagonal element of the box.
1314 /* Compute diff vector */
1315 dz = xj[ZZ] - xi[ZZ];
1316 dy = xj[YY] - xi[YY];
1317 dx = xj[XX] - xi[XX];
1319 /* Perform NINT operation, using trunc operation, therefore
1320 * we first add 2.5 then subtract 2 again
1322 tz = dz*b_inv[ZZ] + h25;
1324 dz -= tz*box[ZZ][ZZ];
1325 dy -= tz*box[ZZ][YY];
1326 dx -= tz*box[ZZ][XX];
1328 ty = dy*b_inv[YY] + h25;
1330 dy -= ty*box[YY][YY];
1331 dx -= ty*box[YY][XX];
1333 tx = dx*b_inv[XX]+h25;
1335 dx -= tx*box[XX][XX];
1337 /* Distance squared */
1338 r2 = (dx*dx) + (dy*dy) + (dz*dz);
1340 *shift = XYZ2IS(tx,ty,tz);
1345 static real calc_image_rect(rvec xi,rvec xj,rvec box_size,
1346 rvec b_inv,int *shift)
1354 /* Compute diff vector */
1355 dx = xj[XX] - xi[XX];
1356 dy = xj[YY] - xi[YY];
1357 dz = xj[ZZ] - xi[ZZ];
1359 /* Perform NINT operation, using trunc operation, therefore
1360 * we first add 1.5 then subtract 1 again
1362 tx = dx*b_inv[XX] + h15;
1363 ty = dy*b_inv[YY] + h15;
1364 tz = dz*b_inv[ZZ] + h15;
1369 /* Correct diff vector for translation */
1370 ddx = tx*box_size[XX] - dx;
1371 ddy = ty*box_size[YY] - dy;
1372 ddz = tz*box_size[ZZ] - dz;
1374 /* Distance squared */
1375 r2 = (ddx*ddx) + (ddy*ddy) + (ddz*ddz);
1377 *shift = XYZ2IS(tx,ty,tz);
1382 static void add_simple(t_ns_buf *nsbuf,int nrj,atom_id cg_j,
1383 gmx_bool bHaveVdW[],int ngid,t_mdatoms *md,
1384 int icg,int jgid,t_block *cgs,t_excl bexcl[],
1385 int shift,t_forcerec *fr,put_in_list_t *put_in_list)
1387 if (nsbuf->nj + nrj > MAX_CG)
1389 put_in_list(bHaveVdW,ngid,md,icg,jgid,nsbuf->ncg,nsbuf->jcg,
1390 cgs->index,bexcl,shift,fr,FALSE,TRUE,TRUE,fr->solvent_opt);
1391 /* Reset buffer contents */
1392 nsbuf->ncg = nsbuf->nj = 0;
1394 nsbuf->jcg[nsbuf->ncg++] = cg_j;
1398 static void ns_inner_tric(rvec x[],int icg,int *i_egp_flags,
1399 int njcg,atom_id jcg[],
1400 matrix box,rvec b_inv,real rcut2,
1401 t_block *cgs,t_ns_buf **ns_buf,
1402 gmx_bool bHaveVdW[],int ngid,t_mdatoms *md,
1403 t_excl bexcl[],t_forcerec *fr,
1404 put_in_list_t *put_in_list)
1408 int *cginfo=fr->cginfo;
1409 atom_id cg_j,*cgindex;
1412 cgindex = cgs->index;
1414 for(j=0; (j<njcg); j++)
1417 nrj = cgindex[cg_j+1]-cgindex[cg_j];
1418 if (calc_image_tric(x[icg],x[cg_j],box,b_inv,&shift) < rcut2)
1420 jgid = GET_CGINFO_GID(cginfo[cg_j]);
1421 if (!(i_egp_flags[jgid] & EGP_EXCL))
1423 add_simple(&ns_buf[jgid][shift],nrj,cg_j,
1424 bHaveVdW,ngid,md,icg,jgid,cgs,bexcl,shift,fr,
1431 static void ns_inner_rect(rvec x[],int icg,int *i_egp_flags,
1432 int njcg,atom_id jcg[],
1433 gmx_bool bBox,rvec box_size,rvec b_inv,real rcut2,
1434 t_block *cgs,t_ns_buf **ns_buf,
1435 gmx_bool bHaveVdW[],int ngid,t_mdatoms *md,
1436 t_excl bexcl[],t_forcerec *fr,
1437 put_in_list_t *put_in_list)
1441 int *cginfo=fr->cginfo;
1442 atom_id cg_j,*cgindex;
1445 cgindex = cgs->index;
1449 for(j=0; (j<njcg); j++)
1452 nrj = cgindex[cg_j+1]-cgindex[cg_j];
1453 if (calc_image_rect(x[icg],x[cg_j],box_size,b_inv,&shift) < rcut2)
1455 jgid = GET_CGINFO_GID(cginfo[cg_j]);
1456 if (!(i_egp_flags[jgid] & EGP_EXCL))
1458 add_simple(&ns_buf[jgid][shift],nrj,cg_j,
1459 bHaveVdW,ngid,md,icg,jgid,cgs,bexcl,shift,fr,
1467 for(j=0; (j<njcg); j++)
1470 nrj = cgindex[cg_j+1]-cgindex[cg_j];
1471 if ((rcut2 == 0) || (distance2(x[icg],x[cg_j]) < rcut2)) {
1472 jgid = GET_CGINFO_GID(cginfo[cg_j]);
1473 if (!(i_egp_flags[jgid] & EGP_EXCL))
1475 add_simple(&ns_buf[jgid][CENTRAL],nrj,cg_j,
1476 bHaveVdW,ngid,md,icg,jgid,cgs,bexcl,CENTRAL,fr,
1484 /* ns_simple_core needs to be adapted for QMMM still 2005 */
1486 static int ns_simple_core(t_forcerec *fr,
1487 gmx_localtop_t *top,
1489 matrix box,rvec box_size,
1490 t_excl bexcl[],atom_id *aaj,
1491 int ngid,t_ns_buf **ns_buf,
1492 put_in_list_t *put_in_list,gmx_bool bHaveVdW[])
1496 int nsearch,icg,jcg,igid,i0,nri,nn;
1499 /* atom_id *i_atoms; */
1500 t_block *cgs=&(top->cgs);
1501 t_blocka *excl=&(top->excls);
1504 gmx_bool bBox,bTriclinic;
1507 rlist2 = sqr(fr->rlist);
1509 bBox = (fr->ePBC != epbcNONE);
1512 for(m=0; (m<DIM); m++)
1514 b_inv[m] = divide(1.0,box_size[m]);
1516 bTriclinic = TRICLINIC(box);
1523 cginfo = fr->cginfo;
1526 for (icg=fr->cg0; (icg<fr->hcg); icg++)
1529 i0 = cgs->index[icg];
1530 nri = cgs->index[icg+1]-i0;
1531 i_atoms = &(cgs->a[i0]);
1532 i_eg_excl = fr->eg_excl + ngid*md->cENER[*i_atoms];
1533 setexcl(nri,i_atoms,excl,TRUE,bexcl);
1535 igid = GET_CGINFO_GID(cginfo[icg]);
1536 i_egp_flags = fr->egp_flags + ngid*igid;
1537 setexcl(cgs->index[icg],cgs->index[icg+1],excl,TRUE,bexcl);
1539 naaj=calc_naaj(icg,cgs->nr);
1542 ns_inner_tric(fr->cg_cm,icg,i_egp_flags,naaj,&(aaj[icg]),
1543 box,b_inv,rlist2,cgs,ns_buf,
1544 bHaveVdW,ngid,md,bexcl,fr,put_in_list);
1548 ns_inner_rect(fr->cg_cm,icg,i_egp_flags,naaj,&(aaj[icg]),
1549 bBox,box_size,b_inv,rlist2,cgs,ns_buf,
1550 bHaveVdW,ngid,md,bexcl,fr,put_in_list);
1554 for(nn=0; (nn<ngid); nn++)
1556 for(k=0; (k<SHIFTS); k++)
1558 nsbuf = &(ns_buf[nn][k]);
1561 put_in_list(bHaveVdW,ngid,md,icg,nn,nsbuf->ncg,nsbuf->jcg,
1562 cgs->index,bexcl,k,fr,FALSE,TRUE,TRUE,fr->solvent_opt);
1563 nsbuf->ncg=nsbuf->nj=0;
1567 /* setexcl(nri,i_atoms,excl,FALSE,bexcl); */
1568 setexcl(cgs->index[icg],cgs->index[icg+1],excl,FALSE,bexcl);
1570 close_neighbor_lists(fr,FALSE);
1575 /************************************************
1577 * N S 5 G R I D S T U F F
1579 ************************************************/
1581 static inline void get_dx(int Nx,real gridx,real rc2,int xgi,real x,
1582 int *dx0,int *dx1,real *dcx2)
1610 for(i=xgi0; i>=0; i--)
1612 dcx = (i+1)*gridx-x;
1619 for(i=xgi1; i<Nx; i++)
1632 static inline void get_dx_dd(int Nx,real gridx,real rc2,int xgi,real x,
1633 int ncpddc,int shift_min,int shift_max,
1634 int *g0,int *g1,real *dcx2)
1637 int g_min,g_max,shift_home;
1670 g_min = (shift_min == shift_home ? 0 : ncpddc);
1671 g_max = (shift_max == shift_home ? ncpddc - 1 : Nx - 1);
1678 else if (shift_max < 0)
1693 /* Check one grid cell down */
1694 dcx = ((*g0 - 1) + 1)*gridx - x;
1706 /* Check one grid cell up */
1707 dcx = (*g1 + 1)*gridx - x;
1719 #define sqr(x) ((x)*(x))
1720 #define calc_dx2(XI,YI,ZI,y) (sqr(XI-y[XX]) + sqr(YI-y[YY]) + sqr(ZI-y[ZZ]))
1721 #define calc_cyl_dx2(XI,YI,y) (sqr(XI-y[XX]) + sqr(YI-y[YY]))
1722 /****************************************************
1724 * F A S T N E I G H B O R S E A R C H I N G
1726 * Optimized neighboursearching routine using grid
1727 * at least 1x1x1, see GROMACS manual
1729 ****************************************************/
1732 static void get_cutoff2(t_forcerec *fr,gmx_bool bDoLongRange,
1733 real *rvdw2,real *rcoul2,
1734 real *rs2,real *rm2,real *rl2)
1736 *rs2 = sqr(fr->rlist);
1738 if (bDoLongRange && fr->bTwinRange)
1740 /* The VdW and elec. LR cut-off's could be different,
1741 * so we can not simply set them to rlistlong.
1743 if (EVDW_MIGHT_BE_ZERO_AT_CUTOFF(fr->vdwtype) &&
1744 fr->rvdw > fr->rlist)
1746 *rvdw2 = sqr(fr->rlistlong);
1750 *rvdw2 = sqr(fr->rvdw);
1752 if (EEL_MIGHT_BE_ZERO_AT_CUTOFF(fr->eeltype) &&
1753 fr->rcoulomb > fr->rlist)
1755 *rcoul2 = sqr(fr->rlistlong);
1759 *rcoul2 = sqr(fr->rcoulomb);
1764 /* Workaround for a gcc -O3 or -ffast-math problem */
1768 *rm2 = min(*rvdw2,*rcoul2);
1769 *rl2 = max(*rvdw2,*rcoul2);
1772 static void init_nsgrid_lists(t_forcerec *fr,int ngid,gmx_ns_t *ns)
1774 real rvdw2,rcoul2,rs2,rm2,rl2;
1777 get_cutoff2(fr,TRUE,&rvdw2,&rcoul2,&rs2,&rm2,&rl2);
1779 /* Short range buffers */
1780 snew(ns->nl_sr,ngid);
1783 snew(ns->nlr_ljc,ngid);
1784 snew(ns->nlr_one,ngid);
1786 /* Always allocate both list types, since rcoulomb might now change with PME load balancing */
1787 /* Long range VdW and Coul buffers */
1788 snew(ns->nl_lr_ljc,ngid);
1789 /* Long range VdW or Coul only buffers */
1790 snew(ns->nl_lr_one,ngid);
1792 for(j=0; (j<ngid); j++) {
1793 snew(ns->nl_sr[j],MAX_CG);
1794 snew(ns->nl_lr_ljc[j],MAX_CG);
1795 snew(ns->nl_lr_one[j],MAX_CG);
1800 "ns5_core: rs2 = %g, rm2 = %g, rl2 = %g (nm^2)\n",
1805 static int nsgrid_core(FILE *log,t_commrec *cr,t_forcerec *fr,
1806 matrix box,rvec box_size,int ngid,
1807 gmx_localtop_t *top,
1808 t_grid *grid,rvec x[],
1809 t_excl bexcl[],gmx_bool *bExcludeAlleg,
1810 t_nrnb *nrnb,t_mdatoms *md,
1811 real *lambda,real *dvdlambda,
1812 gmx_grppairener_t *grppener,
1813 put_in_list_t *put_in_list,
1814 gmx_bool bHaveVdW[],
1815 gmx_bool bDoLongRange,gmx_bool bMakeQMMMnblist)
1818 atom_id **nl_lr_ljc,**nl_lr_one,**nl_sr;
1819 int *nlr_ljc,*nlr_one,*nsr;
1820 gmx_domdec_t *dd=NULL;
1821 t_block *cgs=&(top->cgs);
1822 int *cginfo=fr->cginfo;
1823 /* atom_id *i_atoms,*cgsindex=cgs->index; */
1825 int cell_x,cell_y,cell_z;
1826 int d,tx,ty,tz,dx,dy,dz,cj;
1827 #ifdef ALLOW_OFFDIAG_LT_HALFDIAG
1828 int zsh_ty,zsh_tx,ysh_tx;
1830 int dx0,dx1,dy0,dy1,dz0,dz1;
1831 int Nx,Ny,Nz,shift=-1,j,nrj,nns,nn=-1;
1832 real gridx,gridy,gridz,grid_x,grid_y,grid_z;
1833 real *dcx2,*dcy2,*dcz2;
1835 int cg0,cg1,icg=-1,cgsnr,i0,igid,nri,naaj,max_jcg;
1836 int jcg0,jcg1,jjcg,cgj0,jgid;
1837 int *grida,*gridnra,*gridind;
1838 gmx_bool rvdw_lt_rcoul,rcoul_lt_rvdw;
1839 rvec xi,*cgcm,grid_offset;
1840 real r2,rs2,rvdw2,rcoul2,rm2,rl2,XI,YI,ZI,dcx,dcy,dcz,tmp1,tmp2;
1842 gmx_bool bDomDec,bTriclinicX,bTriclinicY;
1847 bDomDec = DOMAINDECOMP(cr);
1853 bTriclinicX = ((YY < grid->npbcdim &&
1854 (!bDomDec || dd->nc[YY]==1) && box[YY][XX] != 0) ||
1855 (ZZ < grid->npbcdim &&
1856 (!bDomDec || dd->nc[ZZ]==1) && box[ZZ][XX] != 0));
1857 bTriclinicY = (ZZ < grid->npbcdim &&
1858 (!bDomDec || dd->nc[ZZ]==1) && box[ZZ][YY] != 0);
1862 get_cutoff2(fr,bDoLongRange,&rvdw2,&rcoul2,&rs2,&rm2,&rl2);
1864 rvdw_lt_rcoul = (rvdw2 >= rcoul2);
1865 rcoul_lt_rvdw = (rcoul2 >= rvdw2);
1867 if (bMakeQMMMnblist)
1875 nl_lr_ljc = ns->nl_lr_ljc;
1876 nl_lr_one = ns->nl_lr_one;
1877 nlr_ljc = ns->nlr_ljc;
1878 nlr_one = ns->nlr_one;
1886 gridind = grid->index;
1887 gridnra = grid->nra;
1890 gridx = grid->cell_size[XX];
1891 gridy = grid->cell_size[YY];
1892 gridz = grid->cell_size[ZZ];
1896 copy_rvec(grid->cell_offset,grid_offset);
1897 copy_ivec(grid->ncpddc,ncpddc);
1902 #ifdef ALLOW_OFFDIAG_LT_HALFDIAG
1903 zsh_ty = floor(-box[ZZ][YY]/box[YY][YY]+0.5);
1904 zsh_tx = floor(-box[ZZ][XX]/box[XX][XX]+0.5);
1905 ysh_tx = floor(-box[YY][XX]/box[XX][XX]+0.5);
1906 if (zsh_tx!=0 && ysh_tx!=0)
1908 /* This could happen due to rounding, when both ratios are 0.5 */
1917 /* We only want a list for the test particle */
1926 /* Set the shift range */
1927 for(d=0; d<DIM; d++)
1931 /* Check if we need periodicity shifts.
1932 * Without PBC or with domain decomposition we don't need them.
1934 if (d >= ePBC2npbcdim(fr->ePBC) || (bDomDec && dd->nc[d] > 1))
1941 box[XX][XX] - fabs(box[YY][XX]) - fabs(box[ZZ][XX]) < sqrt(rl2))
1952 /* Loop over charge groups */
1953 for(icg=cg0; (icg < cg1); icg++)
1955 igid = GET_CGINFO_GID(cginfo[icg]);
1956 /* Skip this charge group if all energy groups are excluded! */
1957 if (bExcludeAlleg[igid])
1962 i0 = cgs->index[icg];
1964 if (bMakeQMMMnblist)
1966 /* Skip this charge group if it is not a QM atom while making a
1967 * QM/MM neighbourlist
1969 if (md->bQM[i0]==FALSE)
1971 continue; /* MM particle, go to next particle */
1974 /* Compute the number of charge groups that fall within the control
1977 naaj = calc_naaj(icg,cgsnr);
1984 /* make a normal neighbourlist */
1988 /* Get the j charge-group and dd cell shift ranges */
1989 dd_get_ns_ranges(cr->dd,icg,&jcg0,&jcg1,sh0,sh1);
1994 /* Compute the number of charge groups that fall within the control
1997 naaj = calc_naaj(icg,cgsnr);
2003 /* The i-particle is awlways the test particle,
2004 * so we want all j-particles
2006 max_jcg = cgsnr - 1;
2010 max_jcg = jcg1 - cgsnr;
2015 i_egp_flags = fr->egp_flags + igid*ngid;
2017 /* Set the exclusions for the atoms in charge group icg using a bitmask */
2018 setexcl(i0,cgs->index[icg+1],&top->excls,TRUE,bexcl);
2020 ci2xyz(grid,icg,&cell_x,&cell_y,&cell_z);
2022 /* Changed iicg to icg, DvdS 990115
2023 * (but see consistency check above, DvdS 990330)
2026 fprintf(log,"icg=%5d, naaj=%5d, cell %d %d %d\n",
2027 icg,naaj,cell_x,cell_y,cell_z);
2029 /* Loop over shift vectors in three dimensions */
2030 for (tz=-shp[ZZ]; tz<=shp[ZZ]; tz++)
2032 ZI = cgcm[icg][ZZ]+tz*box[ZZ][ZZ];
2033 /* Calculate range of cells in Z direction that have the shift tz */
2034 zgi = cell_z + tz*Nz;
2037 get_dx(Nz,gridz,rl2,zgi,ZI,&dz0,&dz1,dcz2);
2039 get_dx_dd(Nz,gridz,rl2,zgi,ZI-grid_offset[ZZ],
2040 ncpddc[ZZ],sh0[ZZ],sh1[ZZ],&dz0,&dz1,dcz2);
2046 for (ty=-shp[YY]; ty<=shp[YY]; ty++)
2048 YI = cgcm[icg][YY]+ty*box[YY][YY]+tz*box[ZZ][YY];
2049 /* Calculate range of cells in Y direction that have the shift ty */
2052 ygi = (int)(Ny + (YI - grid_offset[YY])*grid_y) - Ny;
2056 ygi = cell_y + ty*Ny;
2059 get_dx(Ny,gridy,rl2,ygi,YI,&dy0,&dy1,dcy2);
2061 get_dx_dd(Ny,gridy,rl2,ygi,YI-grid_offset[YY],
2062 ncpddc[YY],sh0[YY],sh1[YY],&dy0,&dy1,dcy2);
2068 for (tx=-shp[XX]; tx<=shp[XX]; tx++)
2070 XI = cgcm[icg][XX]+tx*box[XX][XX]+ty*box[YY][XX]+tz*box[ZZ][XX];
2071 /* Calculate range of cells in X direction that have the shift tx */
2074 xgi = (int)(Nx + (XI - grid_offset[XX])*grid_x) - Nx;
2078 xgi = cell_x + tx*Nx;
2081 get_dx(Nx,gridx,rl2,xgi*Nx,XI,&dx0,&dx1,dcx2);
2083 get_dx_dd(Nx,gridx,rl2,xgi,XI-grid_offset[XX],
2084 ncpddc[XX],sh0[XX],sh1[XX],&dx0,&dx1,dcx2);
2090 /* Adress: an explicit cg that has a weigthing function of 0 is excluded
2091 * from the neigbour list as it will not interact */
2092 if (fr->adress_type != eAdressOff){
2093 if (md->wf[cgs->index[icg]]==0 && egp_explicit(fr, igid)){
2097 /* Get shift vector */
2098 shift=XYZ2IS(tx,ty,tz);
2100 range_check(shift,0,SHIFTS);
2102 for(nn=0; (nn<ngid); nn++)
2109 fprintf(log,"shift: %2d, dx0,1: %2d,%2d, dy0,1: %2d,%2d, dz0,1: %2d,%2d\n",
2110 shift,dx0,dx1,dy0,dy1,dz0,dz1);
2111 fprintf(log,"cgcm: %8.3f %8.3f %8.3f\n",cgcm[icg][XX],
2112 cgcm[icg][YY],cgcm[icg][ZZ]);
2113 fprintf(log,"xi: %8.3f %8.3f %8.3f\n",XI,YI,ZI);
2115 for (dx=dx0; (dx<=dx1); dx++)
2117 tmp1 = rl2 - dcx2[dx];
2118 for (dy=dy0; (dy<=dy1); dy++)
2120 tmp2 = tmp1 - dcy2[dy];
2123 for (dz=dz0; (dz<=dz1); dz++) {
2124 if (tmp2 > dcz2[dz]) {
2125 /* Find grid-cell cj in which possible neighbours are */
2126 cj = xyz2ci(Ny,Nz,dx,dy,dz);
2128 /* Check out how many cgs (nrj) there in this cell */
2131 /* Find the offset in the cg list */
2134 /* Check if all j's are out of range so we
2135 * can skip the whole cell.
2136 * Should save some time, especially with DD.
2139 (grida[cgj0] >= max_jcg &&
2140 (grida[cgj0] >= jcg1 || grida[cgj0+nrj-1] < jcg0)))
2146 for (j=0; (j<nrj); j++)
2148 jjcg = grida[cgj0+j];
2150 /* check whether this guy is in range! */
2151 if ((jjcg >= jcg0 && jjcg < jcg1) ||
2154 r2=calc_dx2(XI,YI,ZI,cgcm[jjcg]);
2156 /* jgid = gid[cgsatoms[cgsindex[jjcg]]]; */
2157 jgid = GET_CGINFO_GID(cginfo[jjcg]);
2158 /* check energy group exclusions */
2159 if (!(i_egp_flags[jgid] & EGP_EXCL))
2163 if (nsr[jgid] >= MAX_CG)
2165 /* Add to short-range list */
2166 put_in_list(bHaveVdW,ngid,md,icg,jgid,
2167 nsr[jgid],nl_sr[jgid],
2168 cgs->index,/* cgsatoms, */ bexcl,
2169 shift,fr,FALSE,TRUE,TRUE,fr->solvent_opt);
2172 nl_sr[jgid][nsr[jgid]++]=jjcg;
2176 if (nlr_ljc[jgid] >= MAX_CG)
2178 /* Add to LJ+coulomb long-range list */
2179 put_in_list(bHaveVdW,ngid,md,icg,jgid,
2180 nlr_ljc[jgid],nl_lr_ljc[jgid],top->cgs.index,
2181 bexcl,shift,fr,TRUE,TRUE,TRUE,fr->solvent_opt);
2184 nl_lr_ljc[jgid][nlr_ljc[jgid]++]=jjcg;
2188 if (nlr_one[jgid] >= MAX_CG)
2190 /* Add to long-range list with only coul, or only LJ */
2191 put_in_list(bHaveVdW,ngid,md,icg,jgid,
2192 nlr_one[jgid],nl_lr_one[jgid],top->cgs.index,
2193 bexcl,shift,fr,TRUE,rvdw_lt_rcoul,rcoul_lt_rvdw,fr->solvent_opt);
2196 nl_lr_one[jgid][nlr_one[jgid]++]=jjcg;
2208 /* CHECK whether there is anything left in the buffers */
2209 for(nn=0; (nn<ngid); nn++)
2213 put_in_list(bHaveVdW,ngid,md,icg,nn,nsr[nn],nl_sr[nn],
2214 cgs->index, /* cgsatoms, */ bexcl,
2215 shift,fr,FALSE,TRUE,TRUE,fr->solvent_opt);
2218 if (nlr_ljc[nn] > 0)
2220 put_in_list(bHaveVdW,ngid,md,icg,nn,nlr_ljc[nn],
2221 nl_lr_ljc[nn],top->cgs.index,
2222 bexcl,shift,fr,TRUE,TRUE,TRUE,fr->solvent_opt);
2225 if (nlr_one[nn] > 0)
2227 put_in_list(bHaveVdW,ngid,md,icg,nn,nlr_one[nn],
2228 nl_lr_one[nn],top->cgs.index,
2229 bexcl,shift,fr,TRUE,rvdw_lt_rcoul,rcoul_lt_rvdw,fr->solvent_opt);
2235 /* setexcl(nri,i_atoms,&top->atoms.excl,FALSE,bexcl); */
2236 setexcl(cgs->index[icg],cgs->index[icg+1],&top->excls,FALSE,bexcl);
2238 /* No need to perform any left-over force calculations anymore (as we used to do here)
2239 * since we now save the proper long-range lists for later evaluation.
2244 /* Close neighbourlists */
2245 close_neighbor_lists(fr,bMakeQMMMnblist);
2250 void ns_realloc_natoms(gmx_ns_t *ns,int natoms)
2254 if (natoms > ns->nra_alloc)
2256 ns->nra_alloc = over_alloc_dd(natoms);
2257 srenew(ns->bexcl,ns->nra_alloc);
2258 for(i=0; i<ns->nra_alloc; i++)
2265 void init_ns(FILE *fplog,const t_commrec *cr,
2266 gmx_ns_t *ns,t_forcerec *fr,
2267 const gmx_mtop_t *mtop,
2270 int mt,icg,nr_in_cg,maxcg,i,j,jcg,ngid,ncg;
2274 /* Compute largest charge groups size (# atoms) */
2276 for(mt=0; mt<mtop->nmoltype; mt++) {
2277 cgs = &mtop->moltype[mt].cgs;
2278 for (icg=0; (icg < cgs->nr); icg++)
2280 nr_in_cg=max(nr_in_cg,(int)(cgs->index[icg+1]-cgs->index[icg]));
2284 /* Verify whether largest charge group is <= max cg.
2285 * This is determined by the type of the local exclusion type
2286 * Exclusions are stored in bits. (If the type is not large
2287 * enough, enlarge it, unsigned char -> unsigned short -> unsigned long)
2289 maxcg = sizeof(t_excl)*8;
2290 if (nr_in_cg > maxcg)
2292 gmx_fatal(FARGS,"Max #atoms in a charge group: %d > %d\n",
2296 ngid = mtop->groups.grps[egcENER].nr;
2297 snew(ns->bExcludeAlleg,ngid);
2298 for(i=0; i<ngid; i++) {
2299 ns->bExcludeAlleg[i] = TRUE;
2300 for(j=0; j<ngid; j++)
2302 if (!(fr->egp_flags[i*ngid+j] & EGP_EXCL))
2304 ns->bExcludeAlleg[i] = FALSE;
2311 ns->grid = init_grid(fplog,fr);
2312 init_nsgrid_lists(fr,ngid,ns);
2317 snew(ns->ns_buf,ngid);
2318 for(i=0; (i<ngid); i++)
2320 snew(ns->ns_buf[i],SHIFTS);
2322 ncg = ncg_mtop(mtop);
2323 snew(ns->simple_aaj,2*ncg);
2324 for(jcg=0; (jcg<ncg); jcg++)
2326 ns->simple_aaj[jcg] = jcg;
2327 ns->simple_aaj[jcg+ncg] = jcg;
2331 /* Create array that determines whether or not atoms have VdW */
2332 snew(ns->bHaveVdW,fr->ntype);
2333 for(i=0; (i<fr->ntype); i++)
2335 for(j=0; (j<fr->ntype); j++)
2337 ns->bHaveVdW[i] = (ns->bHaveVdW[i] ||
2339 ((BHAMA(fr->nbfp,fr->ntype,i,j) != 0) ||
2340 (BHAMB(fr->nbfp,fr->ntype,i,j) != 0) ||
2341 (BHAMC(fr->nbfp,fr->ntype,i,j) != 0)) :
2342 ((C6(fr->nbfp,fr->ntype,i,j) != 0) ||
2343 (C12(fr->nbfp,fr->ntype,i,j) != 0))));
2347 pr_bvec(debug,0,"bHaveVdW",ns->bHaveVdW,fr->ntype,TRUE);
2351 if (!DOMAINDECOMP(cr))
2353 /* This could be reduced with particle decomposition */
2354 ns_realloc_natoms(ns,mtop->natoms);
2357 ns->nblist_initialized=FALSE;
2359 /* nbr list debug dump */
2361 char *ptr=getenv("GMX_DUMP_NL");
2364 ns->dump_nl=strtol(ptr,NULL,10);
2367 fprintf(fplog, "GMX_DUMP_NL = %d", ns->dump_nl);
2378 int search_neighbours(FILE *log,t_forcerec *fr,
2379 rvec x[],matrix box,
2380 gmx_localtop_t *top,
2381 gmx_groups_t *groups,
2383 t_nrnb *nrnb,t_mdatoms *md,
2384 real *lambda,real *dvdlambda,
2385 gmx_grppairener_t *grppener,
2387 gmx_bool bDoLongRangeNS,
2388 gmx_bool bPadListsForKernels)
2390 t_block *cgs=&(top->cgs);
2391 rvec box_size,grid_x0,grid_x1;
2393 real min_size,grid_dens;
2397 gmx_bool *i_egp_flags;
2398 int cg_start,cg_end,start,end;
2401 gmx_domdec_zones_t *dd_zones;
2402 put_in_list_t *put_in_list;
2406 /* Set some local variables */
2408 ngid = groups->grps[egcENER].nr;
2410 for(m=0; (m<DIM); m++)
2412 box_size[m] = box[m][m];
2415 if (fr->ePBC != epbcNONE)
2417 if (sqr(fr->rlistlong) >= max_cutoff2(fr->ePBC,box))
2419 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.");
2423 min_size = min(box_size[XX],min(box_size[YY],box_size[ZZ]));
2424 if (2*fr->rlistlong >= min_size)
2425 gmx_fatal(FARGS,"One of the box diagonal elements has become smaller than twice the cut-off length.");
2429 if (DOMAINDECOMP(cr))
2431 ns_realloc_natoms(ns,cgs->index[cgs->nr]);
2435 /* Reset the neighbourlists */
2436 reset_neighbor_lists(fr,TRUE,TRUE);
2438 if (bGrid && bFillGrid)
2442 if (DOMAINDECOMP(cr))
2444 dd_zones = domdec_zones(cr->dd);
2450 get_nsgrid_boundaries(grid->nboundeddim,box,NULL,NULL,NULL,NULL,
2451 cgs->nr,fr->cg_cm,grid_x0,grid_x1,&grid_dens);
2453 grid_first(log,grid,NULL,NULL,fr->ePBC,box,grid_x0,grid_x1,
2454 fr->rlistlong,grid_dens);
2458 /* Don't know why this all is... (DvdS 3/99) */
2464 end = (cgs->nr+1)/2;
2467 if (DOMAINDECOMP(cr))
2470 fill_grid(log,dd_zones,grid,end,-1,end,fr->cg_cm);
2472 grid->icg1 = dd_zones->izone[dd_zones->nizone-1].cg1;
2476 fill_grid(log,NULL,grid,cgs->nr,fr->cg0,fr->hcg,fr->cg_cm);
2477 grid->icg0 = fr->cg0;
2478 grid->icg1 = fr->hcg;
2486 calc_elemnr(log,grid,start,end,cgs->nr);
2488 grid_last(log,grid,start,end,cgs->nr);
2492 check_grid(debug,grid);
2493 print_grid(debug,grid);
2498 /* Set the grid cell index for the test particle only.
2499 * The cell to cg index is not corrected, but that does not matter.
2501 fill_grid(log,NULL,ns->grid,fr->hcg,fr->hcg-1,fr->hcg,fr->cg_cm);
2505 if (!fr->ns.bCGlist)
2507 put_in_list = put_in_list_at;
2511 put_in_list = put_in_list_cg;
2518 nsearch = nsgrid_core(log,cr,fr,box,box_size,ngid,top,
2519 grid,x,ns->bexcl,ns->bExcludeAlleg,
2520 nrnb,md,lambda,dvdlambda,grppener,
2521 put_in_list,ns->bHaveVdW,
2522 bDoLongRangeNS,FALSE);
2524 /* neighbour searching withouth QMMM! QM atoms have zero charge in
2525 * the classical calculation. The charge-charge interaction
2526 * between QM and MM atoms is handled in the QMMM core calculation
2527 * (see QMMM.c). The VDW however, we'd like to compute classically
2528 * and the QM MM atom pairs have just been put in the
2529 * corresponding neighbourlists. in case of QMMM we still need to
2530 * fill a special QMMM neighbourlist that contains all neighbours
2531 * of the QM atoms. If bQMMM is true, this list will now be made:
2533 if (fr->bQMMM && fr->qr->QMMMscheme!=eQMMMschemeoniom)
2535 nsearch += nsgrid_core(log,cr,fr,box,box_size,ngid,top,
2536 grid,x,ns->bexcl,ns->bExcludeAlleg,
2537 nrnb,md,lambda,dvdlambda,grppener,
2538 put_in_list_qmmm,ns->bHaveVdW,
2539 bDoLongRangeNS,TRUE);
2544 nsearch = ns_simple_core(fr,top,md,box,box_size,
2545 ns->bexcl,ns->simple_aaj,
2546 ngid,ns->ns_buf,put_in_list,ns->bHaveVdW);
2554 inc_nrnb(nrnb,eNR_NS,nsearch);
2555 /* inc_nrnb(nrnb,eNR_LR,fr->nlr); */
2560 int natoms_beyond_ns_buffer(t_inputrec *ir,t_forcerec *fr,t_block *cgs,
2561 matrix scale_tot,rvec *x)
2563 int cg0,cg1,cg,a0,a1,a,i,j;
2564 real rint,hbuf2,scale;
2566 gmx_bool bIsotropic;
2571 rint = max(ir->rcoulomb,ir->rvdw);
2572 if (ir->rlist < rint)
2574 gmx_fatal(FARGS,"The neighbor search buffer has negative size: %f nm",
2582 if (!EI_DYNAMICS(ir->eI) || !DYNAMIC_BOX(*ir))
2584 hbuf2 = sqr(0.5*(ir->rlist - rint));
2585 for(cg=cg0; cg<cg1; cg++)
2587 a0 = cgs->index[cg];
2588 a1 = cgs->index[cg+1];
2589 for(a=a0; a<a1; a++)
2591 if (distance2(cg_cm[cg],x[a]) > hbuf2)
2601 scale = scale_tot[0][0];
2602 for(i=1; i<DIM; i++)
2604 /* With anisotropic scaling, the original spherical ns volumes become
2605 * ellipsoids. To avoid costly transformations we use the minimum
2606 * eigenvalue of the scaling matrix for determining the buffer size.
2607 * Since the lower half is 0, the eigenvalues are the diagonal elements.
2609 scale = min(scale,scale_tot[i][i]);
2610 if (scale_tot[i][i] != scale_tot[i-1][i-1])
2616 if (scale_tot[i][j] != 0)
2622 hbuf2 = sqr(0.5*(scale*ir->rlist - rint));
2625 for(cg=cg0; cg<cg1; cg++)
2627 svmul(scale,cg_cm[cg],cgsc);
2628 a0 = cgs->index[cg];
2629 a1 = cgs->index[cg+1];
2630 for(a=a0; a<a1; a++)
2632 if (distance2(cgsc,x[a]) > hbuf2)
2641 /* Anistropic scaling */
2642 for(cg=cg0; cg<cg1; cg++)
2644 /* Since scale_tot contains the transpose of the scaling matrix,
2645 * we need to multiply with the transpose.
2647 tmvmul_ur0(scale_tot,cg_cm[cg],cgsc);
2648 a0 = cgs->index[cg];
2649 a1 = cgs->index[cg+1];
2650 for(a=a0; a<a1; a++)
2652 if (distance2(cgsc,x[a]) > hbuf2)