1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROwing Monsters And Cloning Shrimps
50 #include "nonbonded.h"
54 #include "gmx_fatal.h"
57 #include "mtop_util.h"
64 * E X C L U S I O N H A N D L I N G
68 static void SETEXCL_(t_excl e[], atom_id i, atom_id j)
72 static void RMEXCL_(t_excl e[], atom_id i, atom_id j)
74 e[j] = e[j] & ~(1<<i);
76 static gmx_bool ISEXCL_(t_excl e[], atom_id i, atom_id j)
78 return (gmx_bool)(e[j] & (1<<i));
80 static gmx_bool NOTEXCL_(t_excl e[], atom_id i, atom_id j)
82 return !(ISEXCL(e, i, j));
85 #define SETEXCL(e, i, j) (e)[((atom_id) (j))] |= (1<<((atom_id) (i)))
86 #define RMEXCL(e, i, j) (e)[((atom_id) (j))] &= (~(1<<((atom_id) (i))))
87 #define ISEXCL(e, i, j) (gmx_bool) ((e)[((atom_id) (j))] & (1<<((atom_id) (i))))
88 #define NOTEXCL(e, i, j) !(ISEXCL(e, i, j))
92 round_up_to_simd_width(int length, int simd_width)
94 int offset, newlength;
96 offset = (simd_width > 0) ? length % simd_width : 0;
98 return (offset == 0) ? length : length-offset+simd_width;
100 /************************************************
102 * U T I L I T I E S F O R N S
104 ************************************************/
106 static void reallocate_nblist(t_nblist *nl)
110 fprintf(debug, "reallocating neigborlist (ielec=%d, ivdw=%d, igeometry=%d, type=%d), maxnri=%d\n",
111 nl->ielec, nl->ivdw, nl->igeometry, nl->type, nl->maxnri);
113 srenew(nl->iinr, nl->maxnri);
114 if (nl->igeometry == GMX_NBLIST_GEOMETRY_CG_CG)
116 srenew(nl->iinr_end, nl->maxnri);
118 srenew(nl->gid, nl->maxnri);
119 srenew(nl->shift, nl->maxnri);
120 srenew(nl->jindex, nl->maxnri+1);
124 static void init_nblist(FILE *log, t_nblist *nl_sr, t_nblist *nl_lr,
125 int maxsr, int maxlr,
126 int ivdw, int ivdwmod,
127 int ielec, int ielecmod,
128 int igeometry, int type)
134 for (i = 0; (i < 2); i++)
136 nl = (i == 0) ? nl_sr : nl_lr;
137 homenr = (i == 0) ? maxsr : maxlr;
145 /* Set coul/vdw in neighborlist, and for the normal loops we determine
146 * an index of which one to call.
149 nl->ivdwmod = ivdwmod;
151 nl->ielecmod = ielecmod;
153 nl->igeometry = igeometry;
155 if (nl->type == GMX_NBLIST_INTERACTION_FREE_ENERGY)
157 nl->igeometry = GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE;
160 /* This will also set the simd_padding_width field */
161 gmx_nonbonded_set_kernel_pointers( (i == 0) ? log : NULL, nl);
163 /* maxnri is influenced by the number of shifts (maximum is 8)
164 * and the number of energy groups.
165 * If it is not enough, nl memory will be reallocated during the run.
166 * 4 seems to be a reasonable factor, which only causes reallocation
167 * during runs with tiny and many energygroups.
169 nl->maxnri = homenr*4;
178 reallocate_nblist(nl);
183 fprintf(debug, "Initiating neighbourlist (ielec=%d, ivdw=%d, type=%d) for %s interactions,\nwith %d SR, %d LR atoms.\n",
184 nl->ielec, nl->ivdw, nl->type, gmx_nblist_geometry_names[nl->igeometry], maxsr, maxlr);
189 void init_neighbor_list(FILE *log, t_forcerec *fr, int homenr)
191 /* Make maxlr tunable! (does not seem to be a big difference though)
192 * This parameter determines the number of i particles in a long range
193 * neighbourlist. Too few means many function calls, too many means
196 int maxsr, maxsr_wat, maxlr, maxlr_wat;
197 int ielec, ielecf, ivdw, ielecmod, ielecmodf, ivdwmod, type;
199 int igeometry_def, igeometry_w, igeometry_ww;
203 /* maxsr = homenr-fr->nWatMol*3; */
208 gmx_fatal(FARGS, "%s, %d: Negative number of short range atoms.\n"
209 "Call your Gromacs dealer for assistance.", __FILE__, __LINE__);
211 /* This is just for initial allocation, so we do not reallocate
212 * all the nlist arrays many times in a row.
213 * The numbers seem very accurate, but they are uncritical.
215 maxsr_wat = min(fr->nWatMol, (homenr+2)/3);
219 maxlr_wat = min(maxsr_wat, maxlr);
223 maxlr = maxlr_wat = 0;
226 /* Determine the values for ielec/ivdw. */
227 ielec = fr->nbkernel_elec_interaction;
228 ivdw = fr->nbkernel_vdw_interaction;
229 ielecmod = fr->nbkernel_elec_modifier;
230 ivdwmod = fr->nbkernel_vdw_modifier;
231 type = GMX_NBLIST_INTERACTION_STANDARD;
233 fr->ns.bCGlist = (getenv("GMX_NBLISTCG") != 0);
236 igeometry_def = GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE;
240 igeometry_def = GMX_NBLIST_GEOMETRY_CG_CG;
243 fprintf(log, "\nUsing charge-group - charge-group neighbor lists and kernels\n\n");
247 if (fr->solvent_opt == esolTIP4P)
249 igeometry_w = GMX_NBLIST_GEOMETRY_WATER4_PARTICLE;
250 igeometry_ww = GMX_NBLIST_GEOMETRY_WATER4_WATER4;
254 igeometry_w = GMX_NBLIST_GEOMETRY_WATER3_PARTICLE;
255 igeometry_ww = GMX_NBLIST_GEOMETRY_WATER3_WATER3;
258 for (i = 0; i < fr->nnblists; i++)
260 nbl = &(fr->nblists[i]);
262 if ((fr->adress_type != eAdressOff) && (i >= fr->nnblists/2))
264 type = GMX_NBLIST_INTERACTION_ADRESS;
266 init_nblist(log, &nbl->nlist_sr[eNL_VDWQQ], &nbl->nlist_lr[eNL_VDWQQ],
267 maxsr, maxlr, ivdw, ivdwmod, ielec, ielecmod, igeometry_def, type);
268 init_nblist(log, &nbl->nlist_sr[eNL_VDW], &nbl->nlist_lr[eNL_VDW],
269 maxsr, maxlr, ivdw, ivdwmod, GMX_NBKERNEL_ELEC_NONE, eintmodNONE, igeometry_def, type);
270 init_nblist(log, &nbl->nlist_sr[eNL_QQ], &nbl->nlist_lr[eNL_QQ],
271 maxsr, maxlr, GMX_NBKERNEL_VDW_NONE, eintmodNONE, ielec, ielecmod, igeometry_def, type);
272 init_nblist(log, &nbl->nlist_sr[eNL_VDWQQ_WATER], &nbl->nlist_lr[eNL_VDWQQ_WATER],
273 maxsr_wat, maxlr_wat, ivdw, ivdwmod, ielec, ielecmod, igeometry_w, type);
274 init_nblist(log, &nbl->nlist_sr[eNL_QQ_WATER], &nbl->nlist_lr[eNL_QQ_WATER],
275 maxsr_wat, maxlr_wat, GMX_NBKERNEL_VDW_NONE, eintmodNONE, ielec, ielecmod, igeometry_w, type);
276 init_nblist(log, &nbl->nlist_sr[eNL_VDWQQ_WATERWATER], &nbl->nlist_lr[eNL_VDWQQ_WATERWATER],
277 maxsr_wat, maxlr_wat, ivdw, ivdwmod, ielec, ielecmod, igeometry_ww, type);
278 init_nblist(log, &nbl->nlist_sr[eNL_QQ_WATERWATER], &nbl->nlist_lr[eNL_QQ_WATERWATER],
279 maxsr_wat, maxlr_wat, GMX_NBKERNEL_VDW_NONE, eintmodNONE, ielec, ielecmod, igeometry_ww, type);
281 /* Did we get the solvent loops so we can use optimized water kernels? */
282 if (nbl->nlist_sr[eNL_VDWQQ_WATER].kernelptr_vf == NULL
283 || nbl->nlist_sr[eNL_QQ_WATER].kernelptr_vf == NULL
284 #ifndef DISABLE_WATERWATER_NLIST
285 || nbl->nlist_sr[eNL_VDWQQ_WATERWATER].kernelptr_vf == NULL
286 || nbl->nlist_sr[eNL_QQ_WATERWATER].kernelptr_vf == NULL
290 fr->solvent_opt = esolNO;
291 fprintf(log, "Note: The available nonbonded kernels do not support water optimization - disabling.\n");
294 if (fr->efep != efepNO)
296 if ((fr->bEwald) && (fr->sc_alphacoul > 0)) /* need to handle long range differently if using softcore */
298 ielecf = GMX_NBKERNEL_ELEC_EWALD;
299 ielecmodf = eintmodNONE;
304 ielecmodf = ielecmod;
307 init_nblist(log, &nbl->nlist_sr[eNL_VDWQQ_FREE], &nbl->nlist_lr[eNL_VDWQQ_FREE],
308 maxsr, maxlr, ivdw, ivdwmod, ielecf, ielecmod, GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE, GMX_NBLIST_INTERACTION_FREE_ENERGY);
309 init_nblist(log, &nbl->nlist_sr[eNL_VDW_FREE], &nbl->nlist_lr[eNL_VDW_FREE],
310 maxsr, maxlr, ivdw, ivdwmod, GMX_NBKERNEL_ELEC_NONE, eintmodNONE, GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE, GMX_NBLIST_INTERACTION_FREE_ENERGY);
311 init_nblist(log, &nbl->nlist_sr[eNL_QQ_FREE], &nbl->nlist_lr[eNL_QQ_FREE],
312 maxsr, maxlr, GMX_NBKERNEL_VDW_NONE, eintmodNONE, ielecf, ielecmod, GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE, GMX_NBLIST_INTERACTION_FREE_ENERGY);
316 if (fr->bQMMM && fr->qr->QMMMscheme != eQMMMschemeoniom)
318 init_nblist(log, &fr->QMMMlist, NULL,
319 maxsr, maxlr, 0, 0, ielec, ielecmod, GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE, GMX_NBLIST_INTERACTION_STANDARD);
327 fr->ns.nblist_initialized = TRUE;
330 static void reset_nblist(t_nblist *nl)
341 static void reset_neighbor_lists(t_forcerec *fr, gmx_bool bResetSR, gmx_bool bResetLR)
347 /* only reset the short-range nblist */
348 reset_nblist(&(fr->QMMMlist));
351 for (n = 0; n < fr->nnblists; n++)
353 for (i = 0; i < eNL_NR; i++)
357 reset_nblist( &(fr->nblists[n].nlist_sr[i]) );
361 reset_nblist( &(fr->nblists[n].nlist_lr[i]) );
370 static inline void new_i_nblist(t_nblist *nlist, atom_id i_atom, int shift, int gid)
372 int i, k, nri, nshift;
376 /* Check whether we have to increase the i counter */
378 (nlist->iinr[nri] != i_atom) ||
379 (nlist->shift[nri] != shift) ||
380 (nlist->gid[nri] != gid))
382 /* This is something else. Now see if any entries have
383 * been added in the list of the previous atom.
386 ((nlist->jindex[nri+1] > nlist->jindex[nri]) &&
387 (nlist->gid[nri] != -1)))
389 /* If so increase the counter */
392 if (nlist->nri >= nlist->maxnri)
394 nlist->maxnri += over_alloc_large(nlist->nri);
395 reallocate_nblist(nlist);
398 /* Set the number of neighbours and the atom number */
399 nlist->jindex[nri+1] = nlist->jindex[nri];
400 nlist->iinr[nri] = i_atom;
401 nlist->gid[nri] = gid;
402 nlist->shift[nri] = shift;
406 static inline void close_i_nblist(t_nblist *nlist)
408 int nri = nlist->nri;
413 /* Add elements up to padding. Since we allocate memory in units
414 * of the simd_padding width, we do not have to check for possible
415 * list reallocation here.
417 while ((nlist->nrj % nlist->simd_padding_width) != 0)
419 /* Use -4 here, so we can write forces for 4 atoms before real data */
420 nlist->jjnr[nlist->nrj++] = -4;
422 nlist->jindex[nri+1] = nlist->nrj;
424 len = nlist->nrj - nlist->jindex[nri];
426 /* nlist length for water i molecules is treated statically
429 if (len > nlist->maxlen)
436 static inline void close_nblist(t_nblist *nlist)
438 /* Only close this nblist when it has been initialized.
439 * Avoid the creation of i-lists with no j-particles.
443 /* Some assembly kernels do not support empty lists,
444 * make sure here that we don't generate any empty lists.
445 * With the current ns code this branch is taken in two cases:
446 * No i-particles at all: nri=-1 here
447 * There are i-particles, but no j-particles; nri=0 here
453 /* Close list number nri by incrementing the count */
458 static inline void close_neighbor_lists(t_forcerec *fr, gmx_bool bMakeQMMMnblist)
464 close_nblist(&(fr->QMMMlist));
467 for (n = 0; n < fr->nnblists; n++)
469 for (i = 0; (i < eNL_NR); i++)
471 close_nblist(&(fr->nblists[n].nlist_sr[i]));
472 close_nblist(&(fr->nblists[n].nlist_lr[i]));
478 static inline void add_j_to_nblist(t_nblist *nlist, atom_id j_atom, gmx_bool bLR)
480 int nrj = nlist->nrj;
482 if (nlist->nrj >= nlist->maxnrj)
484 nlist->maxnrj = round_up_to_simd_width(over_alloc_small(nlist->nrj + 1), nlist->simd_padding_width);
488 fprintf(debug, "Increasing %s nblist (ielec=%d,ivdw=%d,type=%d,igeometry=%d) j size to %d\n",
489 bLR ? "LR" : "SR", nlist->ielec, nlist->ivdw, nlist->type, nlist->igeometry, nlist->maxnrj);
492 srenew(nlist->jjnr, nlist->maxnrj);
495 nlist->jjnr[nrj] = j_atom;
499 static inline void add_j_to_nblist_cg(t_nblist *nlist,
500 atom_id j_start, int j_end,
501 t_excl *bexcl, gmx_bool i_is_j,
504 int nrj = nlist->nrj;
507 if (nlist->nrj >= nlist->maxnrj)
509 nlist->maxnrj = over_alloc_small(nlist->nrj + 1);
512 fprintf(debug, "Increasing %s nblist (ielec=%d,ivdw=%d,type=%d,igeometry=%d) j size to %d\n",
513 bLR ? "LR" : "SR", nlist->ielec, nlist->ivdw, nlist->type, nlist->igeometry, nlist->maxnrj);
516 srenew(nlist->jjnr, nlist->maxnrj);
517 srenew(nlist->jjnr_end, nlist->maxnrj);
518 srenew(nlist->excl, nlist->maxnrj*MAX_CGCGSIZE);
521 nlist->jjnr[nrj] = j_start;
522 nlist->jjnr_end[nrj] = j_end;
524 if (j_end - j_start > MAX_CGCGSIZE)
526 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);
529 /* Set the exclusions */
530 for (j = j_start; j < j_end; j++)
532 nlist->excl[nrj*MAX_CGCGSIZE + j - j_start] = bexcl[j];
536 /* Avoid double counting of intra-cg interactions */
537 for (j = 1; j < j_end-j_start; j++)
539 nlist->excl[nrj*MAX_CGCGSIZE + j] |= (1<<j) - 1;
547 put_in_list_t (gmx_bool bHaveVdW[],
564 put_in_list_at(gmx_bool bHaveVdW[],
580 /* The a[] index has been removed,
581 * to put it back in i_atom should be a[i0] and jj should be a[jj].
586 t_nblist * vdwc_free = NULL;
587 t_nblist * vdw_free = NULL;
588 t_nblist * coul_free = NULL;
589 t_nblist * vdwc_ww = NULL;
590 t_nblist * coul_ww = NULL;
592 int i, j, jcg, igid, gid, nbl_ind, ind_ij;
593 atom_id jj, jj0, jj1, i_atom;
598 real *charge, *chargeB;
599 real qi, qiB, qq, rlj;
600 gmx_bool bFreeEnergy, bFree, bFreeJ, bNotEx, *bPert;
601 gmx_bool bDoVdW_i, bDoCoul_i, bDoCoul_i_sol;
605 /* Copy some pointers */
607 charge = md->chargeA;
608 chargeB = md->chargeB;
611 bPert = md->bPerturbed;
615 nicg = index[icg+1]-i0;
617 /* Get the i charge group info */
618 igid = GET_CGINFO_GID(cginfo[icg]);
620 iwater = (solvent_opt != esolNO) ? GET_CGINFO_SOLOPT(cginfo[icg]) : esolNO;
625 /* Check if any of the particles involved are perturbed.
626 * If not we can do the cheaper normal put_in_list
627 * and use more solvent optimization.
629 for (i = 0; i < nicg; i++)
631 bFreeEnergy |= bPert[i0+i];
633 /* Loop over the j charge groups */
634 for (j = 0; (j < nj && !bFreeEnergy); j++)
639 /* Finally loop over the atoms in the j-charge group */
640 for (jj = jj0; jj < jj1; jj++)
642 bFreeEnergy |= bPert[jj];
647 /* Unpack pointers to neighbourlist structs */
648 if (fr->nnblists == 1)
654 nbl_ind = fr->gid2nblists[GID(igid, jgid, ngid)];
658 nlist = fr->nblists[nbl_ind].nlist_lr;
662 nlist = fr->nblists[nbl_ind].nlist_sr;
665 if (iwater != esolNO)
667 vdwc = &nlist[eNL_VDWQQ_WATER];
668 vdw = &nlist[eNL_VDW];
669 coul = &nlist[eNL_QQ_WATER];
670 #ifndef DISABLE_WATERWATER_NLIST
671 vdwc_ww = &nlist[eNL_VDWQQ_WATERWATER];
672 coul_ww = &nlist[eNL_QQ_WATERWATER];
677 vdwc = &nlist[eNL_VDWQQ];
678 vdw = &nlist[eNL_VDW];
679 coul = &nlist[eNL_QQ];
684 if (iwater != esolNO)
686 /* Loop over the atoms in the i charge group */
688 gid = GID(igid, jgid, ngid);
689 /* Create new i_atom for each energy group */
690 if (bDoCoul && bDoVdW)
692 new_i_nblist(vdwc, i_atom, shift, gid);
693 #ifndef DISABLE_WATERWATER_NLIST
694 new_i_nblist(vdwc_ww, i_atom, shift, gid);
699 new_i_nblist(vdw, i_atom, shift, gid);
703 new_i_nblist(coul, i_atom, shift, gid);
704 #ifndef DISABLE_WATERWATER_NLIST
705 new_i_nblist(coul_ww, i_atom, shift, gid);
708 /* Loop over the j charge groups */
709 for (j = 0; (j < nj); j++)
719 jwater = GET_CGINFO_SOLOPT(cginfo[jcg]);
721 if (iwater == esolSPC && jwater == esolSPC)
723 /* Interaction between two SPC molecules */
726 /* VdW only - only first atoms in each water interact */
727 add_j_to_nblist(vdw, jj0, bLR);
731 #ifdef DISABLE_WATERWATER_NLIST
732 /* Add entries for the three atoms - only do VdW if we need to */
735 add_j_to_nblist(coul, jj0, bLR);
739 add_j_to_nblist(vdwc, jj0, bLR);
741 add_j_to_nblist(coul, jj0+1, bLR);
742 add_j_to_nblist(coul, jj0+2, bLR);
744 /* One entry for the entire water-water interaction */
747 add_j_to_nblist(coul_ww, jj0, bLR);
751 add_j_to_nblist(vdwc_ww, jj0, bLR);
756 else if (iwater == esolTIP4P && jwater == esolTIP4P)
758 /* Interaction between two TIP4p molecules */
761 /* VdW only - only first atoms in each water interact */
762 add_j_to_nblist(vdw, jj0, bLR);
766 #ifdef DISABLE_WATERWATER_NLIST
767 /* Add entries for the four atoms - only do VdW if we need to */
770 add_j_to_nblist(vdw, jj0, bLR);
772 add_j_to_nblist(coul, jj0+1, bLR);
773 add_j_to_nblist(coul, jj0+2, bLR);
774 add_j_to_nblist(coul, jj0+3, bLR);
776 /* One entry for the entire water-water interaction */
779 add_j_to_nblist(coul_ww, jj0, bLR);
783 add_j_to_nblist(vdwc_ww, jj0, bLR);
790 /* j charge group is not water, but i is.
791 * Add entries to the water-other_atom lists; the geometry of the water
792 * molecule doesn't matter - that is taken care of in the nonbonded kernel,
793 * so we don't care if it is SPC or TIP4P...
800 for (jj = jj0; (jj < jj1); jj++)
804 add_j_to_nblist(coul, jj, bLR);
810 for (jj = jj0; (jj < jj1); jj++)
812 if (bHaveVdW[type[jj]])
814 add_j_to_nblist(vdw, jj, bLR);
820 /* _charge_ _groups_ interact with both coulomb and LJ */
821 /* Check which atoms we should add to the lists! */
822 for (jj = jj0; (jj < jj1); jj++)
824 if (bHaveVdW[type[jj]])
828 add_j_to_nblist(vdwc, jj, bLR);
832 add_j_to_nblist(vdw, jj, bLR);
835 else if (charge[jj] != 0)
837 add_j_to_nblist(coul, jj, bLR);
844 close_i_nblist(coul);
845 close_i_nblist(vdwc);
846 #ifndef DISABLE_WATERWATER_NLIST
847 close_i_nblist(coul_ww);
848 close_i_nblist(vdwc_ww);
853 /* no solvent as i charge group */
854 /* Loop over the atoms in the i charge group */
855 for (i = 0; i < nicg; i++)
858 gid = GID(igid, jgid, ngid);
861 /* Create new i_atom for each energy group */
862 if (bDoVdW && bDoCoul)
864 new_i_nblist(vdwc, i_atom, shift, gid);
868 new_i_nblist(vdw, i_atom, shift, gid);
872 new_i_nblist(coul, i_atom, shift, gid);
874 bDoVdW_i = (bDoVdW && bHaveVdW[type[i_atom]]);
875 bDoCoul_i = (bDoCoul && qi != 0);
877 if (bDoVdW_i || bDoCoul_i)
879 /* Loop over the j charge groups */
880 for (j = 0; (j < nj); j++)
884 /* Check for large charge groups */
895 /* Finally loop over the atoms in the j-charge group */
896 for (jj = jj0; jj < jj1; jj++)
898 bNotEx = NOTEXCL(bExcl, i, jj);
906 add_j_to_nblist(coul, jj, bLR);
911 if (bHaveVdW[type[jj]])
913 add_j_to_nblist(vdw, jj, bLR);
918 if (bHaveVdW[type[jj]])
922 add_j_to_nblist(vdwc, jj, bLR);
926 add_j_to_nblist(vdw, jj, bLR);
929 else if (charge[jj] != 0)
931 add_j_to_nblist(coul, jj, bLR);
939 close_i_nblist(coul);
940 close_i_nblist(vdwc);
946 /* we are doing free energy */
947 vdwc_free = &nlist[eNL_VDWQQ_FREE];
948 vdw_free = &nlist[eNL_VDW_FREE];
949 coul_free = &nlist[eNL_QQ_FREE];
950 /* Loop over the atoms in the i charge group */
951 for (i = 0; i < nicg; i++)
954 gid = GID(igid, jgid, ngid);
956 qiB = chargeB[i_atom];
958 /* Create new i_atom for each energy group */
959 if (bDoVdW && bDoCoul)
961 new_i_nblist(vdwc, i_atom, shift, gid);
965 new_i_nblist(vdw, i_atom, shift, gid);
969 new_i_nblist(coul, i_atom, shift, gid);
972 new_i_nblist(vdw_free, i_atom, shift, gid);
973 new_i_nblist(coul_free, i_atom, shift, gid);
974 new_i_nblist(vdwc_free, i_atom, shift, gid);
976 bDoVdW_i = (bDoVdW &&
977 (bHaveVdW[type[i_atom]] || bHaveVdW[typeB[i_atom]]));
978 bDoCoul_i = (bDoCoul && (qi != 0 || qiB != 0));
979 /* For TIP4P the first atom does not have a charge,
980 * but the last three do. So we should still put an atom
981 * without LJ but with charge in the water-atom neighborlist
982 * for a TIP4p i charge group.
983 * For SPC type water the first atom has LJ and charge,
984 * so there is no such problem.
986 if (iwater == esolNO)
988 bDoCoul_i_sol = bDoCoul_i;
992 bDoCoul_i_sol = bDoCoul;
995 if (bDoVdW_i || bDoCoul_i_sol)
997 /* Loop over the j charge groups */
998 for (j = 0; (j < nj); j++)
1002 /* Check for large charge groups */
1013 /* Finally loop over the atoms in the j-charge group */
1014 bFree = bPert[i_atom];
1015 for (jj = jj0; (jj < jj1); jj++)
1017 bFreeJ = bFree || bPert[jj];
1018 /* Complicated if, because the water H's should also
1019 * see perturbed j-particles
1021 if (iwater == esolNO || i == 0 || bFreeJ)
1023 bNotEx = NOTEXCL(bExcl, i, jj);
1031 if (charge[jj] != 0 || chargeB[jj] != 0)
1033 add_j_to_nblist(coul_free, jj, bLR);
1036 else if (!bDoCoul_i)
1038 if (bHaveVdW[type[jj]] || bHaveVdW[typeB[jj]])
1040 add_j_to_nblist(vdw_free, jj, bLR);
1045 if (bHaveVdW[type[jj]] || bHaveVdW[typeB[jj]])
1047 if (charge[jj] != 0 || chargeB[jj] != 0)
1049 add_j_to_nblist(vdwc_free, jj, bLR);
1053 add_j_to_nblist(vdw_free, jj, bLR);
1056 else if (charge[jj] != 0 || chargeB[jj] != 0)
1058 add_j_to_nblist(coul_free, jj, bLR);
1064 /* This is done whether or not bWater is set */
1065 if (charge[jj] != 0)
1067 add_j_to_nblist(coul, jj, bLR);
1070 else if (!bDoCoul_i_sol)
1072 if (bHaveVdW[type[jj]])
1074 add_j_to_nblist(vdw, jj, bLR);
1079 if (bHaveVdW[type[jj]])
1081 if (charge[jj] != 0)
1083 add_j_to_nblist(vdwc, jj, bLR);
1087 add_j_to_nblist(vdw, jj, bLR);
1090 else if (charge[jj] != 0)
1092 add_j_to_nblist(coul, jj, bLR);
1100 close_i_nblist(vdw);
1101 close_i_nblist(coul);
1102 close_i_nblist(vdwc);
1103 close_i_nblist(vdw_free);
1104 close_i_nblist(coul_free);
1105 close_i_nblist(vdwc_free);
1111 put_in_list_adress(gmx_bool bHaveVdW[],
1127 /* The a[] index has been removed,
1128 * to put it back in i_atom should be a[i0] and jj should be a[jj].
1133 t_nblist * vdwc_adress = NULL;
1134 t_nblist * vdw_adress = NULL;
1135 t_nblist * coul_adress = NULL;
1136 t_nblist * vdwc_ww = NULL;
1137 t_nblist * coul_ww = NULL;
1139 int i, j, jcg, igid, gid, nbl_ind, nbl_ind_adress;
1140 atom_id jj, jj0, jj1, i_atom;
1145 real *charge, *chargeB;
1147 real qi, qiB, qq, rlj;
1148 gmx_bool bFreeEnergy, bFree, bFreeJ, bNotEx, *bPert;
1149 gmx_bool bDoVdW_i, bDoCoul_i, bDoCoul_i_sol;
1151 gmx_bool j_all_atom;
1153 t_nblist *nlist, *nlist_adress;
1154 gmx_bool bEnergyGroupCG;
1156 /* Copy some pointers */
1157 cginfo = fr->cginfo;
1158 charge = md->chargeA;
1159 chargeB = md->chargeB;
1162 bPert = md->bPerturbed;
1165 /* Get atom range */
1167 nicg = index[icg+1]-i0;
1169 /* Get the i charge group info */
1170 igid = GET_CGINFO_GID(cginfo[icg]);
1172 iwater = (solvent_opt != esolNO) ? GET_CGINFO_SOLOPT(cginfo[icg]) : esolNO;
1176 gmx_fatal(FARGS, "AdResS does not support free energy pertubation\n");
1179 /* Unpack pointers to neighbourlist structs */
1180 if (fr->nnblists == 2)
1187 nbl_ind = fr->gid2nblists[GID(igid, jgid, ngid)];
1188 nbl_ind_adress = nbl_ind+fr->nnblists/2;
1192 nlist = fr->nblists[nbl_ind].nlist_lr;
1193 nlist_adress = fr->nblists[nbl_ind_adress].nlist_lr;
1197 nlist = fr->nblists[nbl_ind].nlist_sr;
1198 nlist_adress = fr->nblists[nbl_ind_adress].nlist_sr;
1202 vdwc = &nlist[eNL_VDWQQ];
1203 vdw = &nlist[eNL_VDW];
1204 coul = &nlist[eNL_QQ];
1206 vdwc_adress = &nlist_adress[eNL_VDWQQ];
1207 vdw_adress = &nlist_adress[eNL_VDW];
1208 coul_adress = &nlist_adress[eNL_QQ];
1210 /* We do not support solvent optimization with AdResS for now.
1211 For this we would need hybrid solvent-other kernels */
1213 /* no solvent as i charge group */
1214 /* Loop over the atoms in the i charge group */
1215 for (i = 0; i < nicg; i++)
1218 gid = GID(igid, jgid, ngid);
1219 qi = charge[i_atom];
1221 /* Create new i_atom for each energy group */
1222 if (bDoVdW && bDoCoul)
1224 new_i_nblist(vdwc, i_atom, shift, gid);
1225 new_i_nblist(vdwc_adress, i_atom, shift, gid);
1230 new_i_nblist(vdw, i_atom, shift, gid);
1231 new_i_nblist(vdw_adress, i_atom, shift, gid);
1236 new_i_nblist(coul, i_atom, shift, gid);
1237 new_i_nblist(coul_adress, i_atom, shift, gid);
1239 bDoVdW_i = (bDoVdW && bHaveVdW[type[i_atom]]);
1240 bDoCoul_i = (bDoCoul && qi != 0);
1242 /* Here we find out whether the energy groups interaction belong to a
1243 * coarse-grained (vsite) or atomistic interaction. Note that, beacuse
1244 * interactions between coarse-grained and other (atomistic) energygroups
1245 * are excluded automatically by grompp, it is sufficient to check for
1246 * the group id of atom i (igid) */
1247 bEnergyGroupCG = !egp_explicit(fr, igid);
1249 if (bDoVdW_i || bDoCoul_i)
1251 /* Loop over the j charge groups */
1252 for (j = 0; (j < nj); j++)
1256 /* Check for large charge groups */
1267 /* Finally loop over the atoms in the j-charge group */
1268 for (jj = jj0; jj < jj1; jj++)
1270 bNotEx = NOTEXCL(bExcl, i, jj);
1272 /* Now we have to exclude interactions which will be zero
1273 * anyway due to the AdResS weights (in previous implementations
1274 * this was done in the force kernel). This is necessary as
1275 * pure interactions (those with b_hybrid=false, i.e. w_i*w_j==1 or 0)
1276 * are put into neighbour lists which will be passed to the
1277 * standard (optimized) kernels for speed. The interactions with
1278 * b_hybrid=true are placed into the _adress neighbour lists and
1279 * processed by the generic AdResS kernel.
1281 if ( (bEnergyGroupCG &&
1282 wf[i_atom] >= 1-GMX_REAL_EPS && wf[jj] >= 1-GMX_REAL_EPS ) ||
1283 ( !bEnergyGroupCG && wf[jj] <= GMX_REAL_EPS ) )
1288 b_hybrid = !((wf[i_atom] >= 1-GMX_REAL_EPS && wf[jj] >= 1-GMX_REAL_EPS) ||
1289 (wf[i_atom] <= GMX_REAL_EPS && wf[jj] <= GMX_REAL_EPS));
1295 if (charge[jj] != 0)
1299 add_j_to_nblist(coul, jj, bLR);
1303 add_j_to_nblist(coul_adress, jj, bLR);
1307 else if (!bDoCoul_i)
1309 if (bHaveVdW[type[jj]])
1313 add_j_to_nblist(vdw, jj, bLR);
1317 add_j_to_nblist(vdw_adress, jj, bLR);
1323 if (bHaveVdW[type[jj]])
1325 if (charge[jj] != 0)
1329 add_j_to_nblist(vdwc, jj, bLR);
1333 add_j_to_nblist(vdwc_adress, jj, bLR);
1340 add_j_to_nblist(vdw, jj, bLR);
1344 add_j_to_nblist(vdw_adress, jj, bLR);
1349 else if (charge[jj] != 0)
1353 add_j_to_nblist(coul, jj, bLR);
1357 add_j_to_nblist(coul_adress, jj, bLR);
1366 close_i_nblist(vdw);
1367 close_i_nblist(coul);
1368 close_i_nblist(vdwc);
1369 close_i_nblist(vdw_adress);
1370 close_i_nblist(coul_adress);
1371 close_i_nblist(vdwc_adress);
1377 put_in_list_qmmm(gmx_bool gmx_unused bHaveVdW[],
1379 t_mdatoms gmx_unused * md,
1389 gmx_bool gmx_unused bDoVdW,
1390 gmx_bool gmx_unused bDoCoul,
1391 int gmx_unused solvent_opt)
1394 int i, j, jcg, igid, gid;
1395 atom_id jj, jj0, jj1, i_atom;
1399 /* Get atom range */
1401 nicg = index[icg+1]-i0;
1403 /* Get the i charge group info */
1404 igid = GET_CGINFO_GID(fr->cginfo[icg]);
1406 coul = &fr->QMMMlist;
1408 /* Loop over atoms in the ith charge group */
1409 for (i = 0; i < nicg; i++)
1412 gid = GID(igid, jgid, ngid);
1413 /* Create new i_atom for each energy group */
1414 new_i_nblist(coul, i_atom, shift, gid);
1416 /* Loop over the j charge groups */
1417 for (j = 0; j < nj; j++)
1421 /* Charge groups cannot have QM and MM atoms simultaneously */
1426 /* Finally loop over the atoms in the j-charge group */
1427 for (jj = jj0; jj < jj1; jj++)
1429 bNotEx = NOTEXCL(bExcl, i, jj);
1432 add_j_to_nblist(coul, jj, bLR);
1437 close_i_nblist(coul);
1442 put_in_list_cg(gmx_bool gmx_unused bHaveVdW[],
1444 t_mdatoms gmx_unused * md,
1454 gmx_bool gmx_unused bDoVdW,
1455 gmx_bool gmx_unused bDoCoul,
1456 int gmx_unused solvent_opt)
1459 int igid, gid, nbl_ind;
1463 cginfo = fr->cginfo[icg];
1465 igid = GET_CGINFO_GID(cginfo);
1466 gid = GID(igid, jgid, ngid);
1468 /* Unpack pointers to neighbourlist structs */
1469 if (fr->nnblists == 1)
1475 nbl_ind = fr->gid2nblists[gid];
1479 vdwc = &fr->nblists[nbl_ind].nlist_lr[eNL_VDWQQ];
1483 vdwc = &fr->nblists[nbl_ind].nlist_sr[eNL_VDWQQ];
1486 /* Make a new neighbor list for charge group icg.
1487 * Currently simply one neighbor list is made with LJ and Coulomb.
1488 * If required, zero interactions could be removed here
1489 * or in the force loop.
1491 new_i_nblist(vdwc, index[icg], shift, gid);
1492 vdwc->iinr_end[vdwc->nri] = index[icg+1];
1494 for (j = 0; (j < nj); j++)
1497 /* Skip the icg-icg pairs if all self interactions are excluded */
1498 if (!(jcg == icg && GET_CGINFO_EXCL_INTRA(cginfo)))
1500 /* Here we add the j charge group jcg to the list,
1501 * exclusions are also added to the list.
1503 add_j_to_nblist_cg(vdwc, index[jcg], index[jcg+1], bExcl, icg == jcg, bLR);
1507 close_i_nblist(vdwc);
1510 static void setexcl(atom_id start, atom_id end, t_blocka *excl, gmx_bool b,
1517 for (i = start; i < end; i++)
1519 for (k = excl->index[i]; k < excl->index[i+1]; k++)
1521 SETEXCL(bexcl, i-start, excl->a[k]);
1527 for (i = start; i < end; i++)
1529 for (k = excl->index[i]; k < excl->index[i+1]; k++)
1531 RMEXCL(bexcl, i-start, excl->a[k]);
1537 int calc_naaj(int icg, int cgtot)
1541 if ((cgtot % 2) == 1)
1543 /* Odd number of charge groups, easy */
1544 naaj = 1 + (cgtot/2);
1546 else if ((cgtot % 4) == 0)
1548 /* Multiple of four is hard */
1585 fprintf(log, "naaj=%d\n", naaj);
1591 /************************************************
1593 * S I M P L E C O R E S T U F F
1595 ************************************************/
1597 static real calc_image_tric(rvec xi, rvec xj, matrix box,
1598 rvec b_inv, int *shift)
1600 /* This code assumes that the cut-off is smaller than
1601 * a half times the smallest diagonal element of the box.
1603 const real h25 = 2.5;
1608 /* Compute diff vector */
1609 dz = xj[ZZ] - xi[ZZ];
1610 dy = xj[YY] - xi[YY];
1611 dx = xj[XX] - xi[XX];
1613 /* Perform NINT operation, using trunc operation, therefore
1614 * we first add 2.5 then subtract 2 again
1616 tz = dz*b_inv[ZZ] + h25;
1618 dz -= tz*box[ZZ][ZZ];
1619 dy -= tz*box[ZZ][YY];
1620 dx -= tz*box[ZZ][XX];
1622 ty = dy*b_inv[YY] + h25;
1624 dy -= ty*box[YY][YY];
1625 dx -= ty*box[YY][XX];
1627 tx = dx*b_inv[XX]+h25;
1629 dx -= tx*box[XX][XX];
1631 /* Distance squared */
1632 r2 = (dx*dx) + (dy*dy) + (dz*dz);
1634 *shift = XYZ2IS(tx, ty, tz);
1639 static real calc_image_rect(rvec xi, rvec xj, rvec box_size,
1640 rvec b_inv, int *shift)
1642 const real h15 = 1.5;
1648 /* Compute diff vector */
1649 dx = xj[XX] - xi[XX];
1650 dy = xj[YY] - xi[YY];
1651 dz = xj[ZZ] - xi[ZZ];
1653 /* Perform NINT operation, using trunc operation, therefore
1654 * we first add 1.5 then subtract 1 again
1656 tx = dx*b_inv[XX] + h15;
1657 ty = dy*b_inv[YY] + h15;
1658 tz = dz*b_inv[ZZ] + h15;
1663 /* Correct diff vector for translation */
1664 ddx = tx*box_size[XX] - dx;
1665 ddy = ty*box_size[YY] - dy;
1666 ddz = tz*box_size[ZZ] - dz;
1668 /* Distance squared */
1669 r2 = (ddx*ddx) + (ddy*ddy) + (ddz*ddz);
1671 *shift = XYZ2IS(tx, ty, tz);
1676 static void add_simple(t_ns_buf *nsbuf, int nrj, atom_id cg_j,
1677 gmx_bool bHaveVdW[], int ngid, t_mdatoms *md,
1678 int icg, int jgid, t_block *cgs, t_excl bexcl[],
1679 int shift, t_forcerec *fr, put_in_list_t *put_in_list)
1681 if (nsbuf->nj + nrj > MAX_CG)
1683 put_in_list(bHaveVdW, ngid, md, icg, jgid, nsbuf->ncg, nsbuf->jcg,
1684 cgs->index, bexcl, shift, fr, FALSE, TRUE, TRUE, fr->solvent_opt);
1685 /* Reset buffer contents */
1686 nsbuf->ncg = nsbuf->nj = 0;
1688 nsbuf->jcg[nsbuf->ncg++] = cg_j;
1692 static void ns_inner_tric(rvec x[], int icg, int *i_egp_flags,
1693 int njcg, atom_id jcg[],
1694 matrix box, rvec b_inv, real rcut2,
1695 t_block *cgs, t_ns_buf **ns_buf,
1696 gmx_bool bHaveVdW[], int ngid, t_mdatoms *md,
1697 t_excl bexcl[], t_forcerec *fr,
1698 put_in_list_t *put_in_list)
1702 int *cginfo = fr->cginfo;
1703 atom_id cg_j, *cgindex;
1706 cgindex = cgs->index;
1708 for (j = 0; (j < njcg); j++)
1711 nrj = cgindex[cg_j+1]-cgindex[cg_j];
1712 if (calc_image_tric(x[icg], x[cg_j], box, b_inv, &shift) < rcut2)
1714 jgid = GET_CGINFO_GID(cginfo[cg_j]);
1715 if (!(i_egp_flags[jgid] & EGP_EXCL))
1717 add_simple(&ns_buf[jgid][shift], nrj, cg_j,
1718 bHaveVdW, ngid, md, icg, jgid, cgs, bexcl, shift, fr,
1725 static void ns_inner_rect(rvec x[], int icg, int *i_egp_flags,
1726 int njcg, atom_id jcg[],
1727 gmx_bool bBox, rvec box_size, rvec b_inv, real rcut2,
1728 t_block *cgs, t_ns_buf **ns_buf,
1729 gmx_bool bHaveVdW[], int ngid, t_mdatoms *md,
1730 t_excl bexcl[], t_forcerec *fr,
1731 put_in_list_t *put_in_list)
1735 int *cginfo = fr->cginfo;
1736 atom_id cg_j, *cgindex;
1739 cgindex = cgs->index;
1743 for (j = 0; (j < njcg); j++)
1746 nrj = cgindex[cg_j+1]-cgindex[cg_j];
1747 if (calc_image_rect(x[icg], x[cg_j], box_size, b_inv, &shift) < rcut2)
1749 jgid = GET_CGINFO_GID(cginfo[cg_j]);
1750 if (!(i_egp_flags[jgid] & EGP_EXCL))
1752 add_simple(&ns_buf[jgid][shift], nrj, cg_j,
1753 bHaveVdW, ngid, md, icg, jgid, cgs, bexcl, shift, fr,
1761 for (j = 0; (j < njcg); j++)
1764 nrj = cgindex[cg_j+1]-cgindex[cg_j];
1765 if ((rcut2 == 0) || (distance2(x[icg], x[cg_j]) < rcut2))
1767 jgid = GET_CGINFO_GID(cginfo[cg_j]);
1768 if (!(i_egp_flags[jgid] & EGP_EXCL))
1770 add_simple(&ns_buf[jgid][CENTRAL], nrj, cg_j,
1771 bHaveVdW, ngid, md, icg, jgid, cgs, bexcl, CENTRAL, fr,
1779 /* ns_simple_core needs to be adapted for QMMM still 2005 */
1781 static int ns_simple_core(t_forcerec *fr,
1782 gmx_localtop_t *top,
1784 matrix box, rvec box_size,
1785 t_excl bexcl[], atom_id *aaj,
1786 int ngid, t_ns_buf **ns_buf,
1787 put_in_list_t *put_in_list, gmx_bool bHaveVdW[])
1791 int nsearch, icg, jcg, igid, i0, nri, nn;
1794 /* atom_id *i_atoms; */
1795 t_block *cgs = &(top->cgs);
1796 t_blocka *excl = &(top->excls);
1799 gmx_bool bBox, bTriclinic;
1802 rlist2 = sqr(fr->rlist);
1804 bBox = (fr->ePBC != epbcNONE);
1807 for (m = 0; (m < DIM); m++)
1809 b_inv[m] = divide_err(1.0, box_size[m]);
1811 bTriclinic = TRICLINIC(box);
1818 cginfo = fr->cginfo;
1821 for (icg = fr->cg0; (icg < fr->hcg); icg++)
1824 i0 = cgs->index[icg];
1825 nri = cgs->index[icg+1]-i0;
1826 i_atoms = &(cgs->a[i0]);
1827 i_eg_excl = fr->eg_excl + ngid*md->cENER[*i_atoms];
1828 setexcl(nri,i_atoms,excl,TRUE,bexcl);
1830 igid = GET_CGINFO_GID(cginfo[icg]);
1831 i_egp_flags = fr->egp_flags + ngid*igid;
1832 setexcl(cgs->index[icg], cgs->index[icg+1], excl, TRUE, bexcl);
1834 naaj = calc_naaj(icg, cgs->nr);
1837 ns_inner_tric(fr->cg_cm, icg, i_egp_flags, naaj, &(aaj[icg]),
1838 box, b_inv, rlist2, cgs, ns_buf,
1839 bHaveVdW, ngid, md, bexcl, fr, put_in_list);
1843 ns_inner_rect(fr->cg_cm, icg, i_egp_flags, naaj, &(aaj[icg]),
1844 bBox, box_size, b_inv, rlist2, cgs, ns_buf,
1845 bHaveVdW, ngid, md, bexcl, fr, put_in_list);
1849 for (nn = 0; (nn < ngid); nn++)
1851 for (k = 0; (k < SHIFTS); k++)
1853 nsbuf = &(ns_buf[nn][k]);
1856 put_in_list(bHaveVdW, ngid, md, icg, nn, nsbuf->ncg, nsbuf->jcg,
1857 cgs->index, bexcl, k, fr, FALSE, TRUE, TRUE, fr->solvent_opt);
1858 nsbuf->ncg = nsbuf->nj = 0;
1862 /* setexcl(nri,i_atoms,excl,FALSE,bexcl); */
1863 setexcl(cgs->index[icg], cgs->index[icg+1], excl, FALSE, bexcl);
1865 close_neighbor_lists(fr, FALSE);
1870 /************************************************
1872 * N S 5 G R I D S T U F F
1874 ************************************************/
1876 static inline void get_dx(int Nx, real gridx, real rc2, int xgi, real x,
1877 int *dx0, int *dx1, real *dcx2)
1905 for (i = xgi0; i >= 0; i--)
1907 dcx = (i+1)*gridx-x;
1916 for (i = xgi1; i < Nx; i++)
1929 static inline void get_dx_dd(int Nx, real gridx, real rc2, int xgi, real x,
1930 int ncpddc, int shift_min, int shift_max,
1931 int *g0, int *g1, real *dcx2)
1934 int g_min, g_max, shift_home;
1967 g_min = (shift_min == shift_home ? 0 : ncpddc);
1968 g_max = (shift_max == shift_home ? ncpddc - 1 : Nx - 1);
1975 else if (shift_max < 0)
1990 /* Check one grid cell down */
1991 dcx = ((*g0 - 1) + 1)*gridx - x;
2003 /* Check one grid cell up */
2004 dcx = (*g1 + 1)*gridx - x;
2016 #define sqr(x) ((x)*(x))
2017 #define calc_dx2(XI, YI, ZI, y) (sqr(XI-y[XX]) + sqr(YI-y[YY]) + sqr(ZI-y[ZZ]))
2018 #define calc_cyl_dx2(XI, YI, y) (sqr(XI-y[XX]) + sqr(YI-y[YY]))
2019 /****************************************************
2021 * F A S T N E I G H B O R S E A R C H I N G
2023 * Optimized neighboursearching routine using grid
2024 * at least 1x1x1, see GROMACS manual
2026 ****************************************************/
2029 static void get_cutoff2(t_forcerec *fr, gmx_bool bDoLongRange,
2030 real *rvdw2, real *rcoul2,
2031 real *rs2, real *rm2, real *rl2)
2033 *rs2 = sqr(fr->rlist);
2035 if (bDoLongRange && fr->bTwinRange)
2037 /* The VdW and elec. LR cut-off's could be different,
2038 * so we can not simply set them to rlistlong.
2040 if (EVDW_MIGHT_BE_ZERO_AT_CUTOFF(fr->vdwtype) &&
2041 fr->rvdw > fr->rlist)
2043 *rvdw2 = sqr(fr->rlistlong);
2047 *rvdw2 = sqr(fr->rvdw);
2049 if (EEL_MIGHT_BE_ZERO_AT_CUTOFF(fr->eeltype) &&
2050 fr->rcoulomb > fr->rlist)
2052 *rcoul2 = sqr(fr->rlistlong);
2056 *rcoul2 = sqr(fr->rcoulomb);
2061 /* Workaround for a gcc -O3 or -ffast-math problem */
2065 *rm2 = min(*rvdw2, *rcoul2);
2066 *rl2 = max(*rvdw2, *rcoul2);
2069 static void init_nsgrid_lists(t_forcerec *fr, int ngid, gmx_ns_t *ns)
2071 real rvdw2, rcoul2, rs2, rm2, rl2;
2074 get_cutoff2(fr, TRUE, &rvdw2, &rcoul2, &rs2, &rm2, &rl2);
2076 /* Short range buffers */
2077 snew(ns->nl_sr, ngid);
2079 snew(ns->nsr, ngid);
2080 snew(ns->nlr_ljc, ngid);
2081 snew(ns->nlr_one, ngid);
2083 /* Always allocate both list types, since rcoulomb might now change with PME load balancing */
2084 /* Long range VdW and Coul buffers */
2085 snew(ns->nl_lr_ljc, ngid);
2086 /* Long range VdW or Coul only buffers */
2087 snew(ns->nl_lr_one, ngid);
2089 for (j = 0; (j < ngid); j++)
2091 snew(ns->nl_sr[j], MAX_CG);
2092 snew(ns->nl_lr_ljc[j], MAX_CG);
2093 snew(ns->nl_lr_one[j], MAX_CG);
2098 "ns5_core: rs2 = %g, rm2 = %g, rl2 = %g (nm^2)\n",
2103 static int nsgrid_core(t_commrec *cr, t_forcerec *fr,
2104 matrix box, int ngid,
2105 gmx_localtop_t *top,
2107 t_excl bexcl[], gmx_bool *bExcludeAlleg,
2109 put_in_list_t *put_in_list,
2110 gmx_bool bHaveVdW[],
2111 gmx_bool bDoLongRange, gmx_bool bMakeQMMMnblist)
2114 atom_id **nl_lr_ljc, **nl_lr_one, **nl_sr;
2115 int *nlr_ljc, *nlr_one, *nsr;
2116 gmx_domdec_t *dd = NULL;
2117 t_block *cgs = &(top->cgs);
2118 int *cginfo = fr->cginfo;
2119 /* atom_id *i_atoms,*cgsindex=cgs->index; */
2121 int cell_x, cell_y, cell_z;
2122 int d, tx, ty, tz, dx, dy, dz, cj;
2123 #ifdef ALLOW_OFFDIAG_LT_HALFDIAG
2124 int zsh_ty, zsh_tx, ysh_tx;
2126 int dx0, dx1, dy0, dy1, dz0, dz1;
2127 int Nx, Ny, Nz, shift = -1, j, nrj, nns, nn = -1;
2128 real gridx, gridy, gridz, grid_x, grid_y, grid_z;
2129 real *dcx2, *dcy2, *dcz2;
2131 int cg0, cg1, icg = -1, cgsnr, i0, igid, nri, naaj, max_jcg;
2132 int jcg0, jcg1, jjcg, cgj0, jgid;
2133 int *grida, *gridnra, *gridind;
2134 gmx_bool rvdw_lt_rcoul, rcoul_lt_rvdw;
2135 rvec xi, *cgcm, grid_offset;
2136 real r2, rs2, rvdw2, rcoul2, rm2, rl2, XI, YI, ZI, dcx, dcy, dcz, tmp1, tmp2;
2138 gmx_bool bDomDec, bTriclinicX, bTriclinicY;
2143 bDomDec = DOMAINDECOMP(cr);
2149 bTriclinicX = ((YY < grid->npbcdim &&
2150 (!bDomDec || dd->nc[YY] == 1) && box[YY][XX] != 0) ||
2151 (ZZ < grid->npbcdim &&
2152 (!bDomDec || dd->nc[ZZ] == 1) && box[ZZ][XX] != 0));
2153 bTriclinicY = (ZZ < grid->npbcdim &&
2154 (!bDomDec || dd->nc[ZZ] == 1) && box[ZZ][YY] != 0);
2158 get_cutoff2(fr, bDoLongRange, &rvdw2, &rcoul2, &rs2, &rm2, &rl2);
2160 rvdw_lt_rcoul = (rvdw2 >= rcoul2);
2161 rcoul_lt_rvdw = (rcoul2 >= rvdw2);
2163 if (bMakeQMMMnblist)
2171 nl_lr_ljc = ns->nl_lr_ljc;
2172 nl_lr_one = ns->nl_lr_one;
2173 nlr_ljc = ns->nlr_ljc;
2174 nlr_one = ns->nlr_one;
2182 gridind = grid->index;
2183 gridnra = grid->nra;
2186 gridx = grid->cell_size[XX];
2187 gridy = grid->cell_size[YY];
2188 gridz = grid->cell_size[ZZ];
2192 copy_rvec(grid->cell_offset, grid_offset);
2193 copy_ivec(grid->ncpddc, ncpddc);
2198 #ifdef ALLOW_OFFDIAG_LT_HALFDIAG
2199 zsh_ty = floor(-box[ZZ][YY]/box[YY][YY]+0.5);
2200 zsh_tx = floor(-box[ZZ][XX]/box[XX][XX]+0.5);
2201 ysh_tx = floor(-box[YY][XX]/box[XX][XX]+0.5);
2202 if (zsh_tx != 0 && ysh_tx != 0)
2204 /* This could happen due to rounding, when both ratios are 0.5 */
2213 /* We only want a list for the test particle */
2222 /* Set the shift range */
2223 for (d = 0; d < DIM; d++)
2227 /* Check if we need periodicity shifts.
2228 * Without PBC or with domain decomposition we don't need them.
2230 if (d >= ePBC2npbcdim(fr->ePBC) || (bDomDec && dd->nc[d] > 1))
2237 box[XX][XX] - fabs(box[YY][XX]) - fabs(box[ZZ][XX]) < sqrt(rl2))
2248 /* Loop over charge groups */
2249 for (icg = cg0; (icg < cg1); icg++)
2251 igid = GET_CGINFO_GID(cginfo[icg]);
2252 /* Skip this charge group if all energy groups are excluded! */
2253 if (bExcludeAlleg[igid])
2258 i0 = cgs->index[icg];
2260 if (bMakeQMMMnblist)
2262 /* Skip this charge group if it is not a QM atom while making a
2263 * QM/MM neighbourlist
2265 if (md->bQM[i0] == FALSE)
2267 continue; /* MM particle, go to next particle */
2270 /* Compute the number of charge groups that fall within the control
2273 naaj = calc_naaj(icg, cgsnr);
2280 /* make a normal neighbourlist */
2284 /* Get the j charge-group and dd cell shift ranges */
2285 dd_get_ns_ranges(cr->dd, icg, &jcg0, &jcg1, sh0, sh1);
2290 /* Compute the number of charge groups that fall within the control
2293 naaj = calc_naaj(icg, cgsnr);
2299 /* The i-particle is awlways the test particle,
2300 * so we want all j-particles
2302 max_jcg = cgsnr - 1;
2306 max_jcg = jcg1 - cgsnr;
2311 i_egp_flags = fr->egp_flags + igid*ngid;
2313 /* Set the exclusions for the atoms in charge group icg using a bitmask */
2314 setexcl(i0, cgs->index[icg+1], &top->excls, TRUE, bexcl);
2316 ci2xyz(grid, icg, &cell_x, &cell_y, &cell_z);
2318 /* Changed iicg to icg, DvdS 990115
2319 * (but see consistency check above, DvdS 990330)
2322 fprintf(log, "icg=%5d, naaj=%5d, cell %d %d %d\n",
2323 icg, naaj, cell_x, cell_y, cell_z);
2325 /* Loop over shift vectors in three dimensions */
2326 for (tz = -shp[ZZ]; tz <= shp[ZZ]; tz++)
2328 ZI = cgcm[icg][ZZ]+tz*box[ZZ][ZZ];
2329 /* Calculate range of cells in Z direction that have the shift tz */
2330 zgi = cell_z + tz*Nz;
2333 get_dx(Nz, gridz, rl2, zgi, ZI, &dz0, &dz1, dcz2);
2335 get_dx_dd(Nz, gridz, rl2, zgi, ZI-grid_offset[ZZ],
2336 ncpddc[ZZ], sh0[ZZ], sh1[ZZ], &dz0, &dz1, dcz2);
2342 for (ty = -shp[YY]; ty <= shp[YY]; ty++)
2344 YI = cgcm[icg][YY]+ty*box[YY][YY]+tz*box[ZZ][YY];
2345 /* Calculate range of cells in Y direction that have the shift ty */
2348 ygi = (int)(Ny + (YI - grid_offset[YY])*grid_y) - Ny;
2352 ygi = cell_y + ty*Ny;
2355 get_dx(Ny, gridy, rl2, ygi, YI, &dy0, &dy1, dcy2);
2357 get_dx_dd(Ny, gridy, rl2, ygi, YI-grid_offset[YY],
2358 ncpddc[YY], sh0[YY], sh1[YY], &dy0, &dy1, dcy2);
2364 for (tx = -shp[XX]; tx <= shp[XX]; tx++)
2366 XI = cgcm[icg][XX]+tx*box[XX][XX]+ty*box[YY][XX]+tz*box[ZZ][XX];
2367 /* Calculate range of cells in X direction that have the shift tx */
2370 xgi = (int)(Nx + (XI - grid_offset[XX])*grid_x) - Nx;
2374 xgi = cell_x + tx*Nx;
2377 get_dx(Nx, gridx, rl2, xgi*Nx, XI, &dx0, &dx1, dcx2);
2379 get_dx_dd(Nx, gridx, rl2, xgi, XI-grid_offset[XX],
2380 ncpddc[XX], sh0[XX], sh1[XX], &dx0, &dx1, dcx2);
2386 /* Adress: an explicit cg that has a weigthing function of 0 is excluded
2387 * from the neigbour list as it will not interact */
2388 if (fr->adress_type != eAdressOff)
2390 if (md->wf[cgs->index[icg]] <= GMX_REAL_EPS && egp_explicit(fr, igid))
2395 /* Get shift vector */
2396 shift = XYZ2IS(tx, ty, tz);
2398 range_check(shift, 0, SHIFTS);
2400 for (nn = 0; (nn < ngid); nn++)
2407 fprintf(log, "shift: %2d, dx0,1: %2d,%2d, dy0,1: %2d,%2d, dz0,1: %2d,%2d\n",
2408 shift, dx0, dx1, dy0, dy1, dz0, dz1);
2409 fprintf(log, "cgcm: %8.3f %8.3f %8.3f\n", cgcm[icg][XX],
2410 cgcm[icg][YY], cgcm[icg][ZZ]);
2411 fprintf(log, "xi: %8.3f %8.3f %8.3f\n", XI, YI, ZI);
2413 for (dx = dx0; (dx <= dx1); dx++)
2415 tmp1 = rl2 - dcx2[dx];
2416 for (dy = dy0; (dy <= dy1); dy++)
2418 tmp2 = tmp1 - dcy2[dy];
2421 for (dz = dz0; (dz <= dz1); dz++)
2423 if (tmp2 > dcz2[dz])
2425 /* Find grid-cell cj in which possible neighbours are */
2426 cj = xyz2ci(Ny, Nz, dx, dy, dz);
2428 /* Check out how many cgs (nrj) there in this cell */
2431 /* Find the offset in the cg list */
2434 /* Check if all j's are out of range so we
2435 * can skip the whole cell.
2436 * Should save some time, especially with DD.
2439 (grida[cgj0] >= max_jcg &&
2440 (grida[cgj0] >= jcg1 || grida[cgj0+nrj-1] < jcg0)))
2446 for (j = 0; (j < nrj); j++)
2448 jjcg = grida[cgj0+j];
2450 /* check whether this guy is in range! */
2451 if ((jjcg >= jcg0 && jjcg < jcg1) ||
2454 r2 = calc_dx2(XI, YI, ZI, cgcm[jjcg]);
2457 /* jgid = gid[cgsatoms[cgsindex[jjcg]]]; */
2458 jgid = GET_CGINFO_GID(cginfo[jjcg]);
2459 /* check energy group exclusions */
2460 if (!(i_egp_flags[jgid] & EGP_EXCL))
2464 if (nsr[jgid] >= MAX_CG)
2466 /* Add to short-range list */
2467 put_in_list(bHaveVdW, ngid, md, icg, jgid,
2468 nsr[jgid], nl_sr[jgid],
2469 cgs->index, /* cgsatoms, */ bexcl,
2470 shift, fr, FALSE, TRUE, TRUE, fr->solvent_opt);
2473 nl_sr[jgid][nsr[jgid]++] = jjcg;
2477 if (nlr_ljc[jgid] >= MAX_CG)
2479 /* Add to LJ+coulomb long-range list */
2480 put_in_list(bHaveVdW, ngid, md, icg, jgid,
2481 nlr_ljc[jgid], nl_lr_ljc[jgid], top->cgs.index,
2482 bexcl, shift, fr, TRUE, TRUE, TRUE, fr->solvent_opt);
2485 nl_lr_ljc[jgid][nlr_ljc[jgid]++] = jjcg;
2489 if (nlr_one[jgid] >= MAX_CG)
2491 /* Add to long-range list with only coul, or only LJ */
2492 put_in_list(bHaveVdW, ngid, md, icg, jgid,
2493 nlr_one[jgid], nl_lr_one[jgid], top->cgs.index,
2494 bexcl, shift, fr, TRUE, rvdw_lt_rcoul, rcoul_lt_rvdw, fr->solvent_opt);
2497 nl_lr_one[jgid][nlr_one[jgid]++] = jjcg;
2509 /* CHECK whether there is anything left in the buffers */
2510 for (nn = 0; (nn < ngid); nn++)
2514 put_in_list(bHaveVdW, ngid, md, icg, nn, nsr[nn], nl_sr[nn],
2515 cgs->index, /* cgsatoms, */ bexcl,
2516 shift, fr, FALSE, TRUE, TRUE, fr->solvent_opt);
2519 if (nlr_ljc[nn] > 0)
2521 put_in_list(bHaveVdW, ngid, md, icg, nn, nlr_ljc[nn],
2522 nl_lr_ljc[nn], top->cgs.index,
2523 bexcl, shift, fr, TRUE, TRUE, TRUE, fr->solvent_opt);
2526 if (nlr_one[nn] > 0)
2528 put_in_list(bHaveVdW, ngid, md, icg, nn, nlr_one[nn],
2529 nl_lr_one[nn], top->cgs.index,
2530 bexcl, shift, fr, TRUE, rvdw_lt_rcoul, rcoul_lt_rvdw, fr->solvent_opt);
2536 /* setexcl(nri,i_atoms,&top->atoms.excl,FALSE,bexcl); */
2537 setexcl(cgs->index[icg], cgs->index[icg+1], &top->excls, FALSE, bexcl);
2539 /* No need to perform any left-over force calculations anymore (as we used to do here)
2540 * since we now save the proper long-range lists for later evaluation.
2545 /* Close neighbourlists */
2546 close_neighbor_lists(fr, bMakeQMMMnblist);
2551 void ns_realloc_natoms(gmx_ns_t *ns, int natoms)
2555 if (natoms > ns->nra_alloc)
2557 ns->nra_alloc = over_alloc_dd(natoms);
2558 srenew(ns->bexcl, ns->nra_alloc);
2559 for (i = 0; i < ns->nra_alloc; i++)
2566 void init_ns(FILE *fplog, const t_commrec *cr,
2567 gmx_ns_t *ns, t_forcerec *fr,
2568 const gmx_mtop_t *mtop)
2570 int mt, icg, nr_in_cg, maxcg, i, j, jcg, ngid, ncg;
2574 /* Compute largest charge groups size (# atoms) */
2576 for (mt = 0; mt < mtop->nmoltype; mt++)
2578 cgs = &mtop->moltype[mt].cgs;
2579 for (icg = 0; (icg < cgs->nr); icg++)
2581 nr_in_cg = max(nr_in_cg, (int)(cgs->index[icg+1]-cgs->index[icg]));
2585 /* Verify whether largest charge group is <= max cg.
2586 * This is determined by the type of the local exclusion type
2587 * Exclusions are stored in bits. (If the type is not large
2588 * enough, enlarge it, unsigned char -> unsigned short -> unsigned long)
2590 maxcg = sizeof(t_excl)*8;
2591 if (nr_in_cg > maxcg)
2593 gmx_fatal(FARGS, "Max #atoms in a charge group: %d > %d\n",
2597 ngid = mtop->groups.grps[egcENER].nr;
2598 snew(ns->bExcludeAlleg, ngid);
2599 for (i = 0; i < ngid; i++)
2601 ns->bExcludeAlleg[i] = TRUE;
2602 for (j = 0; j < ngid; j++)
2604 if (!(fr->egp_flags[i*ngid+j] & EGP_EXCL))
2606 ns->bExcludeAlleg[i] = FALSE;
2614 ns->grid = init_grid(fplog, fr);
2615 init_nsgrid_lists(fr, ngid, ns);
2620 snew(ns->ns_buf, ngid);
2621 for (i = 0; (i < ngid); i++)
2623 snew(ns->ns_buf[i], SHIFTS);
2625 ncg = ncg_mtop(mtop);
2626 snew(ns->simple_aaj, 2*ncg);
2627 for (jcg = 0; (jcg < ncg); jcg++)
2629 ns->simple_aaj[jcg] = jcg;
2630 ns->simple_aaj[jcg+ncg] = jcg;
2634 /* Create array that determines whether or not atoms have VdW */
2635 snew(ns->bHaveVdW, fr->ntype);
2636 for (i = 0; (i < fr->ntype); i++)
2638 for (j = 0; (j < fr->ntype); j++)
2640 ns->bHaveVdW[i] = (ns->bHaveVdW[i] ||
2642 ((BHAMA(fr->nbfp, fr->ntype, i, j) != 0) ||
2643 (BHAMB(fr->nbfp, fr->ntype, i, j) != 0) ||
2644 (BHAMC(fr->nbfp, fr->ntype, i, j) != 0)) :
2645 ((C6(fr->nbfp, fr->ntype, i, j) != 0) ||
2646 (C12(fr->nbfp, fr->ntype, i, j) != 0))));
2651 pr_bvec(debug, 0, "bHaveVdW", ns->bHaveVdW, fr->ntype, TRUE);
2656 if (!DOMAINDECOMP(cr))
2658 /* This could be reduced with particle decomposition */
2659 ns_realloc_natoms(ns, mtop->natoms);
2662 ns->nblist_initialized = FALSE;
2664 /* nbr list debug dump */
2666 char *ptr = getenv("GMX_DUMP_NL");
2669 ns->dump_nl = strtol(ptr, NULL, 10);
2672 fprintf(fplog, "GMX_DUMP_NL = %d", ns->dump_nl);
2683 int search_neighbours(FILE *log, t_forcerec *fr,
2685 gmx_localtop_t *top,
2686 gmx_groups_t *groups,
2688 t_nrnb *nrnb, t_mdatoms *md,
2690 gmx_bool bDoLongRangeNS)
2692 t_block *cgs = &(top->cgs);
2693 rvec box_size, grid_x0, grid_x1;
2695 real min_size, grid_dens;
2699 gmx_bool *i_egp_flags;
2700 int cg_start, cg_end, start, end;
2703 gmx_domdec_zones_t *dd_zones;
2704 put_in_list_t *put_in_list;
2708 /* Set some local variables */
2710 ngid = groups->grps[egcENER].nr;
2712 for (m = 0; (m < DIM); m++)
2714 box_size[m] = box[m][m];
2717 if (fr->ePBC != epbcNONE)
2719 if (sqr(fr->rlistlong) >= max_cutoff2(fr->ePBC, box))
2721 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.");
2725 min_size = min(box_size[XX], min(box_size[YY], box_size[ZZ]));
2726 if (2*fr->rlistlong >= min_size)
2728 gmx_fatal(FARGS, "One of the box diagonal elements has become smaller than twice the cut-off length.");
2733 if (DOMAINDECOMP(cr))
2735 ns_realloc_natoms(ns, cgs->index[cgs->nr]);
2739 /* Reset the neighbourlists */
2740 reset_neighbor_lists(fr, TRUE, TRUE);
2742 if (bGrid && bFillGrid)
2746 if (DOMAINDECOMP(cr))
2748 dd_zones = domdec_zones(cr->dd);
2754 get_nsgrid_boundaries(grid->nboundeddim, box, NULL, NULL, NULL, NULL,
2755 cgs->nr, fr->cg_cm, grid_x0, grid_x1, &grid_dens);
2757 grid_first(log, grid, NULL, NULL, box, grid_x0, grid_x1,
2758 fr->rlistlong, grid_dens);
2762 /* Don't know why this all is... (DvdS 3/99) */
2768 end = (cgs->nr+1)/2;
2771 if (DOMAINDECOMP(cr))
2774 fill_grid(dd_zones, grid, end, -1, end, fr->cg_cm);
2776 grid->icg1 = dd_zones->izone[dd_zones->nizone-1].cg1;
2780 fill_grid(NULL, grid, cgs->nr, fr->cg0, fr->hcg, fr->cg_cm);
2781 grid->icg0 = fr->cg0;
2782 grid->icg1 = fr->hcg;
2792 calc_elemnr(grid, start, end, cgs->nr);
2794 grid_last(grid, start, end, cgs->nr);
2799 print_grid(debug, grid);
2804 /* Set the grid cell index for the test particle only.
2805 * The cell to cg index is not corrected, but that does not matter.
2807 fill_grid(NULL, ns->grid, fr->hcg, fr->hcg-1, fr->hcg, fr->cg_cm);
2811 if (fr->adress_type == eAdressOff)
2813 if (!fr->ns.bCGlist)
2815 put_in_list = put_in_list_at;
2819 put_in_list = put_in_list_cg;
2824 put_in_list = put_in_list_adress;
2831 nsearch = nsgrid_core(cr, fr, box, ngid, top,
2832 grid, ns->bexcl, ns->bExcludeAlleg,
2833 md, put_in_list, ns->bHaveVdW,
2834 bDoLongRangeNS, FALSE);
2836 /* neighbour searching withouth QMMM! QM atoms have zero charge in
2837 * the classical calculation. The charge-charge interaction
2838 * between QM and MM atoms is handled in the QMMM core calculation
2839 * (see QMMM.c). The VDW however, we'd like to compute classically
2840 * and the QM MM atom pairs have just been put in the
2841 * corresponding neighbourlists. in case of QMMM we still need to
2842 * fill a special QMMM neighbourlist that contains all neighbours
2843 * of the QM atoms. If bQMMM is true, this list will now be made:
2845 if (fr->bQMMM && fr->qr->QMMMscheme != eQMMMschemeoniom)
2847 nsearch += nsgrid_core(cr, fr, box, ngid, top,
2848 grid, ns->bexcl, ns->bExcludeAlleg,
2849 md, put_in_list_qmmm, ns->bHaveVdW,
2850 bDoLongRangeNS, TRUE);
2855 nsearch = ns_simple_core(fr, top, md, box, box_size,
2856 ns->bexcl, ns->simple_aaj,
2857 ngid, ns->ns_buf, put_in_list, ns->bHaveVdW);
2865 inc_nrnb(nrnb, eNR_NS, nsearch);
2866 /* inc_nrnb(nrnb,eNR_LR,fr->nlr); */
2871 int natoms_beyond_ns_buffer(t_inputrec *ir, t_forcerec *fr, t_block *cgs,
2872 matrix scale_tot, rvec *x)
2874 int cg0, cg1, cg, a0, a1, a, i, j;
2875 real rint, hbuf2, scale;
2877 gmx_bool bIsotropic;
2882 rint = max(ir->rcoulomb, ir->rvdw);
2883 if (ir->rlist < rint)
2885 gmx_fatal(FARGS, "The neighbor search buffer has negative size: %f nm",
2893 if (!EI_DYNAMICS(ir->eI) || !DYNAMIC_BOX(*ir))
2895 hbuf2 = sqr(0.5*(ir->rlist - rint));
2896 for (cg = cg0; cg < cg1; cg++)
2898 a0 = cgs->index[cg];
2899 a1 = cgs->index[cg+1];
2900 for (a = a0; a < a1; a++)
2902 if (distance2(cg_cm[cg], x[a]) > hbuf2)
2912 scale = scale_tot[0][0];
2913 for (i = 1; i < DIM; i++)
2915 /* With anisotropic scaling, the original spherical ns volumes become
2916 * ellipsoids. To avoid costly transformations we use the minimum
2917 * eigenvalue of the scaling matrix for determining the buffer size.
2918 * Since the lower half is 0, the eigenvalues are the diagonal elements.
2920 scale = min(scale, scale_tot[i][i]);
2921 if (scale_tot[i][i] != scale_tot[i-1][i-1])
2925 for (j = 0; j < i; j++)
2927 if (scale_tot[i][j] != 0)
2933 hbuf2 = sqr(0.5*(scale*ir->rlist - rint));
2936 for (cg = cg0; cg < cg1; cg++)
2938 svmul(scale, cg_cm[cg], cgsc);
2939 a0 = cgs->index[cg];
2940 a1 = cgs->index[cg+1];
2941 for (a = a0; a < a1; a++)
2943 if (distance2(cgsc, x[a]) > hbuf2)
2952 /* Anistropic scaling */
2953 for (cg = cg0; cg < cg1; cg++)
2955 /* Since scale_tot contains the transpose of the scaling matrix,
2956 * we need to multiply with the transpose.
2958 tmvmul_ur0(scale_tot, cg_cm[cg], cgsc);
2959 a0 = cgs->index[cg];
2960 a1 = cgs->index[cg+1];
2961 for (a = a0; a < a1; a++)
2963 if (distance2(cgsc, x[a]) > hbuf2)