2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2017,2018,2019, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
39 #include "vsite_parm.h"
47 #include "gromacs/gmxpreprocess/add_par.h"
48 #include "gromacs/gmxpreprocess/gpp_atomtype.h"
49 #include "gromacs/gmxpreprocess/grompp_impl.h"
50 #include "gromacs/gmxpreprocess/notset.h"
51 #include "gromacs/gmxpreprocess/resall.h"
52 #include "gromacs/gmxpreprocess/toputil.h"
53 #include "gromacs/math/functions.h"
54 #include "gromacs/math/units.h"
55 #include "gromacs/math/vec.h"
56 #include "gromacs/mdtypes/md_enums.h"
57 #include "gromacs/topology/ifunc.h"
58 #include "gromacs/topology/topology.h"
59 #include "gromacs/utility/arrayref.h"
60 #include "gromacs/utility/cstringutil.h"
61 #include "gromacs/utility/fatalerror.h"
62 #include "gromacs/utility/gmxassert.h"
63 #include "gromacs/utility/smalloc.h"
64 #include "gromacs/utility/strconvert.h"
69 t_iatom &ai() { return a[0]; }
70 t_iatom &aj() { return a[1]; }
71 t_iatom &ak() { return a[2]; }
72 t_iatom &al() { return a[3]; }
83 vsitebondparam_t *vsbp;
91 static int vsite_bond_nrcheck(int ftype)
95 if ((interaction_function[ftype].flags & (IF_BTYPE | IF_CONSTRAINT | IF_ATYPE)) || (ftype == F_IDIHS))
97 nrcheck = NRAL(ftype);
107 static void enter_bonded(int nratoms, int *nrbonded, t_mybonded **bondeds,
112 srenew(*bondeds, *nrbonded+1);
114 /* copy atom numbers */
115 for (j = 0; j < nratoms; j++)
117 (*bondeds)[*nrbonded].a[j] = param->a[j];
120 (*bondeds)[*nrbonded].c = param->c0();
125 static void get_bondeds(int nrat, const t_iatom atoms[],
126 at2vsitebond_t *at2vb,
127 int *nrbond, t_mybonded **bonds,
128 int *nrang, t_mybonded **angles,
129 int *nridih, t_mybonded **idihs )
131 int k, i, ftype, nrcheck;
134 for (k = 0; k < nrat; k++)
136 for (i = 0; i < at2vb[atoms[k]].nr; i++)
138 ftype = at2vb[atoms[k]].vsbp[i].ftype;
139 param = at2vb[atoms[k]].vsbp[i].param;
140 nrcheck = vsite_bond_nrcheck(ftype);
141 /* abuse nrcheck to see if we're adding bond, angle or idih */
144 case 2: enter_bonded(nrcheck, nrbond, bonds, param); break;
145 case 3: enter_bonded(nrcheck, nrang, angles, param); break;
146 case 4: enter_bonded(nrcheck, nridih, idihs, param); break;
152 static at2vsitebond_t *make_at2vsitebond(int natoms, t_params plist[])
155 int ftype, i, j, nrcheck, nr;
157 at2vsitebond_t *at2vb;
162 for (ftype = 0; (ftype < F_NRE); ftype++)
164 if ((interaction_function[ftype].flags & IF_VSITE) && ftype != F_VSITEN)
166 for (i = 0; (i < plist[ftype].nr); i++)
168 for (j = 0; j < NRAL(ftype); j++)
170 bVSI[plist[ftype].param[i].a[j]] = TRUE;
176 for (ftype = 0; (ftype < F_NRE); ftype++)
178 nrcheck = vsite_bond_nrcheck(ftype);
181 for (i = 0; (i < plist[ftype].nr); i++)
183 aa = plist[ftype].param[i].a;
184 for (j = 0; j < nrcheck; j++)
188 nr = at2vb[aa[j]].nr;
191 srenew(at2vb[aa[j]].vsbp, nr+10);
193 at2vb[aa[j]].vsbp[nr].ftype = ftype;
194 at2vb[aa[j]].vsbp[nr].param = &plist[ftype].param[i];
206 static void done_at2vsitebond(int natoms, at2vsitebond_t *at2vb)
210 for (i = 0; i < natoms; i++)
214 sfree(at2vb[i].vsbp);
220 static at2vsitecon_t *make_at2vsitecon(int natoms, t_params plist[])
223 int ftype, i, j, ai, aj, nr;
224 at2vsitecon_t *at2vc;
229 for (ftype = 0; (ftype < F_NRE); ftype++)
231 if ((interaction_function[ftype].flags & IF_VSITE) && ftype != F_VSITEN)
233 for (i = 0; (i < plist[ftype].nr); i++)
235 for (j = 0; j < NRAL(ftype); j++)
237 bVSI[plist[ftype].param[i].a[j]] = TRUE;
243 for (ftype = 0; (ftype < F_NRE); ftype++)
245 if (interaction_function[ftype].flags & IF_CONSTRAINT)
247 for (i = 0; (i < plist[ftype].nr); i++)
249 ai = plist[ftype].param[i].ai();
250 aj = plist[ftype].param[i].aj();
251 if (bVSI[ai] && bVSI[aj])
253 /* Store forward direction */
257 srenew(at2vc[ai].aj, nr+10);
259 at2vc[ai].aj[nr] = aj;
261 /* Store backward direction */
265 srenew(at2vc[aj].aj, nr+10);
267 at2vc[aj].aj[nr] = ai;
278 static void done_at2vsitecon(int natoms, at2vsitecon_t *at2vc)
282 for (i = 0; i < natoms; i++)
293 static void print_bad(FILE *fp,
294 int nrbond, t_mybonded *bonds,
295 int nrang, t_mybonded *angles,
296 int nridih, t_mybonded *idihs )
302 fprintf(fp, "bonds:");
303 for (i = 0; i < nrbond; i++)
305 fprintf(fp, " %d-%d (%g)",
306 bonds[i].ai()+1, bonds[i].aj()+1, bonds[i].c);
312 fprintf(fp, "angles:");
313 for (i = 0; i < nrang; i++)
315 fprintf(fp, " %d-%d-%d (%g)",
316 angles[i].ai()+1, angles[i].aj()+1,
317 angles[i].ak()+1, angles[i].c);
323 fprintf(fp, "idihs:");
324 for (i = 0; i < nridih; i++)
326 fprintf(fp, " %d-%d-%d-%d (%g)",
327 idihs[i].ai()+1, idihs[i].aj()+1,
328 idihs[i].ak()+1, idihs[i].al()+1, idihs[i].c);
334 static void print_param(FILE *fp, int ftype, int i, t_param *param)
337 static int prev_ftype = NOTSET;
338 static int prev_i = NOTSET;
341 if ( (ftype != prev_ftype) || (i != prev_i) )
347 fprintf(fp, "(%d) plist[%s].param[%d]",
348 pass, interaction_function[ftype].name, i);
349 for (j = 0; j < NRFP(ftype); j++)
351 fprintf(fp, ".c[%d]=%g ", j, param->c[j]);
357 static real get_bond_length(int nrbond, t_mybonded bonds[],
358 t_iatom ai, t_iatom aj)
364 for (i = 0; i < nrbond && (bondlen == NOTSET); i++)
366 /* check both ways */
367 if ( ( (ai == bonds[i].ai()) && (aj == bonds[i].aj()) ) ||
368 ( (ai == bonds[i].aj()) && (aj == bonds[i].ai()) ) )
370 bondlen = bonds[i].c; /* note: bonds[i].c might be NOTSET */
376 static real get_angle(int nrang, t_mybonded angles[],
377 t_iatom ai, t_iatom aj, t_iatom ak)
383 for (i = 0; i < nrang && (angle == NOTSET); i++)
385 /* check both ways */
386 if ( ( (ai == angles[i].ai()) && (aj == angles[i].aj()) && (ak == angles[i].ak()) ) ||
387 ( (ai == angles[i].ak()) && (aj == angles[i].aj()) && (ak == angles[i].ai()) ) )
389 angle = DEG2RAD*angles[i].c;
395 static char *get_atomtype_name_AB(t_atom *atom, gpp_atomtype *atype)
399 name = get_atomtype_name(atom->type, atype);
401 /* When using the decoupling option, atom types are changed
402 * to decoupled for the non-bonded interactions, but the virtual
403 * sites constructions should be based on the "normal" interactions.
404 * So we return the state B atom type name if the state A atom
405 * type is the decoupled one. We should actually check for the atom
406 * type number, but that's not passed here. So we check for
407 * the decoupled atom type name. This should not cause trouble
408 * as this code is only used for topologies with v-sites without
409 * parameters generated by pdb2gmx.
411 if (strcmp(name, "decoupled") == 0)
413 name = get_atomtype_name(atom->typeB, atype);
419 static bool calc_vsite3_param(gpp_atomtype *atype,
420 t_param *param, t_atoms *at,
421 int nrbond, t_mybonded *bonds,
422 int nrang, t_mybonded *angles )
424 /* i = virtual site | ,k
425 * j = 1st bonded heavy atom | i-j
426 * k,l = 2nd bonded atoms | `l
430 real bjk, bjl, a = -1, b = -1;
431 /* check if this is part of a NH3 , NH2-umbrella or CH3 group,
432 * i.e. if atom k and l are dummy masses (MNH* or MCH3*) */
434 ( (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->ak()], atype), "MNH", 3) == 0) &&
435 (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->al()], atype), "MNH", 3) == 0) ) ||
436 ( (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->ak()], atype), "MCH3", 4) == 0) &&
437 (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->al()], atype), "MCH3", 4) == 0) );
439 bjk = get_bond_length(nrbond, bonds, param->aj(), param->ak());
440 bjl = get_bond_length(nrbond, bonds, param->aj(), param->al());
441 bError = (bjk == NOTSET) || (bjl == NOTSET);
444 /* now we get some XH2/XH3 group specific construction */
445 /* note: we call the heavy atom 'C' and the X atom 'N' */
446 real bMM, bCM, bCN, bNH, aCNH, dH, rH, dM, rM;
449 /* check if bonds from heavy atom (j) to dummy masses (k,l) are equal: */
450 bError = bError || (bjk != bjl);
452 /* the X atom (C or N) in the XH2/XH3 group is the first after the masses: */
453 aN = std::max(param->ak(), param->al())+1;
455 /* get common bonds */
456 bMM = get_bond_length(nrbond, bonds, param->ak(), param->al());
458 bCN = get_bond_length(nrbond, bonds, param->aj(), aN);
459 bError = bError || (bMM == NOTSET) || (bCN == NOTSET);
461 /* calculate common things */
463 dM = std::sqrt( gmx::square(bCM) - gmx::square(rM) );
465 /* are we dealing with the X atom? */
466 if (param->ai() == aN)
468 /* this is trivial */
469 a = b = 0.5 * bCN/dM;
474 /* get other bondlengths and angles: */
475 bNH = get_bond_length(nrbond, bonds, aN, param->ai());
476 aCNH = get_angle (nrang, angles, param->aj(), aN, param->ai());
477 bError = bError || (bNH == NOTSET) || (aCNH == NOTSET);
480 dH = bCN - bNH * std::cos(aCNH);
481 rH = bNH * std::sin(aCNH);
483 a = 0.5 * ( dH/dM + rH/rM );
484 b = 0.5 * ( dH/dM - rH/rM );
489 gmx_fatal(FARGS, "calc_vsite3_param not implemented for the general case "
490 "(atom %d)", param->ai()+1);
499 static bool calc_vsite3fd_param(t_param *param,
500 int nrbond, t_mybonded *bonds,
501 int nrang, t_mybonded *angles)
503 /* i = virtual site | ,k
504 * j = 1st bonded heavy atom | i-j
505 * k,l = 2nd bonded atoms | `l
509 real bij, bjk, bjl, aijk, aijl, rk, rl;
511 bij = get_bond_length(nrbond, bonds, param->ai(), param->aj());
512 bjk = get_bond_length(nrbond, bonds, param->aj(), param->ak());
513 bjl = get_bond_length(nrbond, bonds, param->aj(), param->al());
514 aijk = get_angle (nrang, angles, param->ai(), param->aj(), param->ak());
515 aijl = get_angle (nrang, angles, param->ai(), param->aj(), param->al());
516 bError = (bij == NOTSET) || (bjk == NOTSET) || (bjl == NOTSET) ||
517 (aijk == NOTSET) || (aijl == NOTSET);
519 rk = bjk * std::sin(aijk);
520 rl = bjl * std::sin(aijl);
521 param->c0() = rk / (rk + rl);
522 param->c1() = -bij; /* 'bond'-length for fixed distance vsite */
527 static bool calc_vsite3fad_param(t_param *param,
528 int nrbond, t_mybonded *bonds,
529 int nrang, t_mybonded *angles)
531 /* i = virtual site |
532 * j = 1st bonded heavy atom | i-j
533 * k = 2nd bonded heavy atom | `k-l
534 * l = 3d bonded heavy atom |
537 bool bSwapParity, bError;
540 bSwapParity = ( param->c1() == -1 );
542 bij = get_bond_length(nrbond, bonds, param->ai(), param->aj());
543 aijk = get_angle (nrang, angles, param->ai(), param->aj(), param->ak());
544 bError = (bij == NOTSET) || (aijk == NOTSET);
546 param->c1() = bij; /* 'bond'-length for fixed distance vsite */
547 param->c0() = RAD2DEG*aijk; /* 'bond'-angle for fixed angle vsite */
551 param->c0() = 360 - param->c0();
557 static bool calc_vsite3out_param(gpp_atomtype *atype,
558 t_param *param, t_atoms *at,
559 int nrbond, t_mybonded *bonds,
560 int nrang, t_mybonded *angles)
562 /* i = virtual site | ,k
563 * j = 1st bonded heavy atom | i-j
564 * k,l = 2nd bonded atoms | `l
565 * NOTE: i is out of the j-k-l plane!
568 bool bXH3, bError, bSwapParity;
569 real bij, bjk, bjl, aijk, aijl, akjl, pijk, pijl, a, b, c;
571 /* check if this is part of a NH2-umbrella, NH3 or CH3 group,
572 * i.e. if atom k and l are dummy masses (MNH* or MCH3*) */
574 ( (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->ak()], atype), "MNH", 3) == 0) &&
575 (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->al()], atype), "MNH", 3) == 0) ) ||
576 ( (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->ak()], atype), "MCH3", 4) == 0) &&
577 (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->al()], atype), "MCH3", 4) == 0) );
579 /* check if construction parity must be swapped */
580 bSwapParity = ( param->c1() == -1 );
582 bjk = get_bond_length(nrbond, bonds, param->aj(), param->ak());
583 bjl = get_bond_length(nrbond, bonds, param->aj(), param->al());
584 bError = (bjk == NOTSET) || (bjl == NOTSET);
587 /* now we get some XH3 group specific construction */
588 /* note: we call the heavy atom 'C' and the X atom 'N' */
589 real bMM, bCM, bCN, bNH, aCNH, dH, rH, rHx, rHy, dM, rM;
592 /* check if bonds from heavy atom (j) to dummy masses (k,l) are equal: */
593 bError = bError || (bjk != bjl);
595 /* the X atom (C or N) in the XH3 group is the first after the masses: */
596 aN = std::max(param->ak(), param->al())+1;
598 /* get all bondlengths and angles: */
599 bMM = get_bond_length(nrbond, bonds, param->ak(), param->al());
601 bCN = get_bond_length(nrbond, bonds, param->aj(), aN);
602 bNH = get_bond_length(nrbond, bonds, aN, param->ai());
603 aCNH = get_angle (nrang, angles, param->aj(), aN, param->ai());
605 (bMM == NOTSET) || (bCN == NOTSET) || (bNH == NOTSET) || (aCNH == NOTSET);
608 dH = bCN - bNH * std::cos(aCNH);
609 rH = bNH * std::sin(aCNH);
610 /* we assume the H's are symmetrically distributed */
611 rHx = rH*std::cos(DEG2RAD*30);
612 rHy = rH*std::sin(DEG2RAD*30);
614 dM = std::sqrt( gmx::square(bCM) - gmx::square(rM) );
615 a = 0.5*( (dH/dM) - (rHy/rM) );
616 b = 0.5*( (dH/dM) + (rHy/rM) );
622 /* this is the general construction */
624 bij = get_bond_length(nrbond, bonds, param->ai(), param->aj());
625 aijk = get_angle (nrang, angles, param->ai(), param->aj(), param->ak());
626 aijl = get_angle (nrang, angles, param->ai(), param->aj(), param->al());
627 akjl = get_angle (nrang, angles, param->ak(), param->aj(), param->al());
629 (bij == NOTSET) || (aijk == NOTSET) || (aijl == NOTSET) || (akjl == NOTSET);
631 pijk = std::cos(aijk)*bij;
632 pijl = std::cos(aijl)*bij;
633 a = ( pijk + (pijk*std::cos(akjl)-pijl) * std::cos(akjl) / gmx::square(std::sin(akjl)) ) / bjk;
634 b = ( pijl + (pijl*std::cos(akjl)-pijk) * std::cos(akjl) / gmx::square(std::sin(akjl)) ) / bjl;
635 c = -std::sqrt( gmx::square(bij) -
636 ( gmx::square(pijk) - 2*pijk*pijl*std::cos(akjl) + gmx::square(pijl) )
637 / gmx::square(std::sin(akjl)) )
638 / ( bjk*bjl*std::sin(akjl) );
654 static bool calc_vsite4fd_param(t_param *param,
655 int nrbond, t_mybonded *bonds,
656 int nrang, t_mybonded *angles)
658 /* i = virtual site | ,k
659 * j = 1st bonded heavy atom | i-j-m
660 * k,l,m = 2nd bonded atoms | `l
664 real bij, bjk, bjl, bjm, aijk, aijl, aijm, akjm, akjl;
665 real pk, pl, pm, cosakl, cosakm, sinakl, sinakm, cl, cm;
667 bij = get_bond_length(nrbond, bonds, param->ai(), param->aj());
668 bjk = get_bond_length(nrbond, bonds, param->aj(), param->ak());
669 bjl = get_bond_length(nrbond, bonds, param->aj(), param->al());
670 bjm = get_bond_length(nrbond, bonds, param->aj(), param->am());
671 aijk = get_angle (nrang, angles, param->ai(), param->aj(), param->ak());
672 aijl = get_angle (nrang, angles, param->ai(), param->aj(), param->al());
673 aijm = get_angle (nrang, angles, param->ai(), param->aj(), param->am());
674 akjm = get_angle (nrang, angles, param->ak(), param->aj(), param->am());
675 akjl = get_angle (nrang, angles, param->ak(), param->aj(), param->al());
676 bError = (bij == NOTSET) || (bjk == NOTSET) || (bjl == NOTSET) || (bjm == NOTSET) ||
677 (aijk == NOTSET) || (aijl == NOTSET) || (aijm == NOTSET) || (akjm == NOTSET) ||
682 pk = bjk*std::sin(aijk);
683 pl = bjl*std::sin(aijl);
684 pm = bjm*std::sin(aijm);
685 cosakl = (std::cos(akjl) - std::cos(aijk)*std::cos(aijl)) / (std::sin(aijk)*std::sin(aijl));
686 cosakm = (std::cos(akjm) - std::cos(aijk)*std::cos(aijm)) / (std::sin(aijk)*std::sin(aijm));
687 if (cosakl < -1 || cosakl > 1 || cosakm < -1 || cosakm > 1)
689 fprintf(stderr, "virtual site %d: angle ijk = %f, angle ijl = %f, angle ijm = %f\n",
690 param->ai()+1, RAD2DEG*aijk, RAD2DEG*aijl, RAD2DEG*aijm);
691 gmx_fatal(FARGS, "invalid construction in calc_vsite4fd for atom %d: "
692 "cosakl=%f, cosakm=%f\n", param->ai()+1, cosakl, cosakm);
694 sinakl = std::sqrt(1-gmx::square(cosakl));
695 sinakm = std::sqrt(1-gmx::square(cosakm));
697 /* note: there is a '+' because of the way the sines are calculated */
698 cl = -pk / ( pl*cosakl - pk + pl*sinakl*(pm*cosakm-pk)/(pm*sinakm) );
699 cm = -pk / ( pm*cosakm - pk + pm*sinakm*(pl*cosakl-pk)/(pl*sinakl) );
711 calc_vsite4fdn_param(t_param *param,
712 int nrbond, t_mybonded *bonds,
713 int nrang, t_mybonded *angles)
715 /* i = virtual site | ,k
716 * j = 1st bonded heavy atom | i-j-m
717 * k,l,m = 2nd bonded atoms | `l
721 real bij, bjk, bjl, bjm, aijk, aijl, aijm;
722 real pk, pl, pm, a, b;
724 bij = get_bond_length(nrbond, bonds, param->ai(), param->aj());
725 bjk = get_bond_length(nrbond, bonds, param->aj(), param->ak());
726 bjl = get_bond_length(nrbond, bonds, param->aj(), param->al());
727 bjm = get_bond_length(nrbond, bonds, param->aj(), param->am());
728 aijk = get_angle (nrang, angles, param->ai(), param->aj(), param->ak());
729 aijl = get_angle (nrang, angles, param->ai(), param->aj(), param->al());
730 aijm = get_angle (nrang, angles, param->ai(), param->aj(), param->am());
732 bError = (bij == NOTSET) || (bjk == NOTSET) || (bjl == NOTSET) || (bjm == NOTSET) ||
733 (aijk == NOTSET) || (aijl == NOTSET) || (aijm == NOTSET);
738 /* Calculate component of bond j-k along the direction i-j */
739 pk = -bjk*std::cos(aijk);
741 /* Calculate component of bond j-l along the direction i-j */
742 pl = -bjl*std::cos(aijl);
744 /* Calculate component of bond j-m along the direction i-j */
745 pm = -bjm*std::cos(aijm);
747 if (fabs(pl) < 1000*GMX_REAL_MIN || fabs(pm) < 1000*GMX_REAL_MIN)
749 fprintf(stderr, "virtual site %d: angle ijk = %f, angle ijl = %f, angle ijm = %f\n",
750 param->ai()+1, RAD2DEG*aijk, RAD2DEG*aijl, RAD2DEG*aijm);
751 gmx_fatal(FARGS, "invalid construction in calc_vsite4fdn for atom %d: "
752 "pl=%f, pm=%f\n", param->ai()+1, pl, pm);
769 int set_vsites(bool bVerbose, t_atoms *atoms, gpp_atomtype *atype,
773 int nvsite, nrbond, nrang, nridih, nrset;
774 bool bFirst, bSet, bERROR;
775 at2vsitebond_t *at2vb;
783 /* Make a reverse list to avoid ninteractions^2 operations */
784 at2vb = make_at2vsitebond(atoms->nr, plist);
786 for (ftype = 0; (ftype < F_NRE); ftype++)
788 if (interaction_function[ftype].flags & IF_VSITE)
790 nvsite += plist[ftype].nr;
792 if (ftype == F_VSITEN)
794 /* We don't calculate parameters for VSITEN */
799 for (i = 0; (i < plist[ftype].nr); i++)
801 /* check if all parameters are set */
803 for (j = 0; j < NRFP(ftype) && bSet; j++)
805 bSet = plist[ftype].param[i].c[j] != NOTSET;
810 fprintf(debug, "bSet=%s ", gmx::boolToString(bSet));
811 print_param(debug, ftype, i, &plist[ftype].param[i]);
815 if (bVerbose && bFirst)
817 fprintf(stderr, "Calculating parameters for virtual sites\n");
821 nrbond = nrang = nridih = 0;
826 /* now set the vsite parameters: */
827 get_bondeds(NRAL(ftype), plist[ftype].param[i].a, at2vb,
828 &nrbond, &bonds, &nrang, &angles, &nridih, &idihs);
831 fprintf(debug, "Found %d bonds, %d angles and %d idihs "
832 "for virtual site %d (%s)\n", nrbond, nrang, nridih,
833 plist[ftype].param[i].ai()+1,
834 interaction_function[ftype].longname);
835 print_bad(debug, nrbond, bonds, nrang, angles, nridih, idihs);
841 calc_vsite3_param(atype, &(plist[ftype].param[i]), atoms,
842 nrbond, bonds, nrang, angles);
846 calc_vsite3fd_param(&(plist[ftype].param[i]),
847 nrbond, bonds, nrang, angles);
851 calc_vsite3fad_param(&(plist[ftype].param[i]),
852 nrbond, bonds, nrang, angles);
856 calc_vsite3out_param(atype, &(plist[ftype].param[i]), atoms,
857 nrbond, bonds, nrang, angles);
861 calc_vsite4fd_param(&(plist[ftype].param[i]),
862 nrbond, bonds, nrang, angles);
866 calc_vsite4fdn_param(&(plist[ftype].param[i]),
867 nrbond, bonds, nrang, angles);
870 gmx_fatal(FARGS, "Automatic parameter generation not supported "
872 interaction_function[ftype].longname,
873 plist[ftype].param[i].ai()+1);
878 gmx_fatal(FARGS, "Automatic parameter generation not supported "
879 "for %s atom %d for this bonding configuration",
880 interaction_function[ftype].longname,
881 plist[ftype].param[i].ai()+1);
891 done_at2vsitebond(atoms->nr, at2vb);
896 void set_vsites_ptype(bool bVerbose, gmx_moltype_t *molt)
902 fprintf(stderr, "Setting particle type to V for virtual sites\n");
904 for (ftype = 0; ftype < F_NRE; ftype++)
906 InteractionList *il = &molt->ilist[ftype];
907 if (interaction_function[ftype].flags & IF_VSITE)
909 const int nra = interaction_function[ftype].nratoms;
910 const int nrd = il->size();
911 gmx::ArrayRef<const int> ia = il->iatoms;
915 fprintf(stderr, "doing %d %s virtual sites\n",
916 (nrd / (nra+1)), interaction_function[ftype].longname);
919 for (i = 0; (i < nrd); )
921 /* The virtual site */
922 int avsite = ia[i + 1];
923 molt->atoms.atom[avsite].ptype = eptVSite;
936 static void check_vsite_constraints(t_params *plist,
937 int cftype, const int vsite_type[])
944 ps = &(plist[cftype]);
945 for (i = 0; (i < ps->nr); i++)
947 for (k = 0; k < 2; k++)
949 atom = ps->param[i].a[k];
950 if (vsite_type[atom] != NOTSET)
952 fprintf(stderr, "ERROR: Cannot have constraint (%d-%d) with virtual site (%d)\n",
953 ps->param[i].ai()+1, ps->param[i].aj()+1, atom+1);
960 gmx_fatal(FARGS, "There were %d virtual sites involved in constraints", n);
964 static void clean_vsite_bonds(t_params *plist, t_pindex pindex[],
965 int cftype, const int vsite_type[])
967 int ftype, i, j, k, m, n, nvsite, nOut, kept_i;
968 int nconverted, nremoved;
969 int atom, oatom, at1, at2;
970 bool bKeep, bRemove, bUsed, bPresent, bThisFD, bThisOUT, bAllFD, bFirstTwo;
973 if (cftype == F_CONNBONDS)
978 ps = &(plist[cftype]);
983 for (i = 0; (i < ps->nr); i++) /* for all bonds in the plist */
986 const int *first_atoms = nullptr;
991 /* check if all virtual sites are constructed from the same atoms */
993 for (k = 0; (k < 2) && !bKeep && !bRemove; k++)
995 /* for all atoms in the bond */
996 atom = ps->param[i].a[k];
997 if (vsite_type[atom] != NOTSET && vsite_type[atom] != F_VSITEN)
1000 bThisFD = ( (pindex[atom].ftype == F_VSITE3FD ) ||
1001 (pindex[atom].ftype == F_VSITE3FAD) ||
1002 (pindex[atom].ftype == F_VSITE4FD ) ||
1003 (pindex[atom].ftype == F_VSITE4FDN ) );
1004 bThisOUT = ( (pindex[atom].ftype == F_VSITE3OUT) &&
1005 ((interaction_function[cftype].flags & IF_CONSTRAINT) != 0u) );
1006 bAllFD = bAllFD && bThisFD;
1007 if (bThisFD || bThisOUT)
1009 oatom = ps->param[i].a[1-k]; /* the other atom */
1010 if (vsite_type[oatom] == NOTSET &&
1011 oatom == plist[pindex[atom].ftype].param[pindex[atom].parnr].aj())
1013 /* if the other atom isn't a vsite, and it is AI */
1023 /* TODO This fragment, and corresponding logic in
1024 clean_vsite_angles and clean_vsite_dihs should
1025 be refactored into a common function */
1028 /* if this is the first vsite we encounter then
1029 store construction atoms */
1030 /* TODO This would be nicer to implement with
1031 a C++ "vector view" class" with an
1032 STL-container-like interface. */
1033 vsnral = NRAL(pindex[atom].ftype) - 1;
1034 first_atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1038 GMX_ASSERT(vsnral != 0, "nvsite > 1 must have vsnral != 0");
1039 GMX_ASSERT(first_atoms != nullptr, "nvsite > 1 must have first_atoms != NULL");
1040 /* if it is not the first then
1041 check if this vsite is constructed from the same atoms */
1042 if (vsnral == NRAL(pindex[atom].ftype)-1)
1044 for (m = 0; (m < vsnral) && !bKeep; m++)
1049 atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1050 for (n = 0; (n < vsnral) && !bPresent; n++)
1052 if (atoms[m] == first_atoms[n])
1078 /* if we have no virtual sites in this bond, keep it */
1084 /* TODO This loop and the corresponding loop in
1085 check_vsite_angles should be refactored into a common
1087 /* check if all non-vsite atoms are used in construction: */
1089 for (k = 0; (k < 2) && !bKeep; k++) /* for all atoms in the bond */
1091 atom = ps->param[i].a[k];
1092 if (vsite_type[atom] == NOTSET)
1095 for (m = 0; (m < vsnral) && !bUsed; m++)
1097 GMX_ASSERT(first_atoms != nullptr, "If we've seen a vsite before, we know what its first atom index was");
1099 if (atom == first_atoms[m])
1102 bFirstTwo = bFirstTwo && m < 2;
1112 if (!( bAllFD && bFirstTwo ) )
1114 /* Two atom bonded interactions include constraints.
1115 * We need to remove constraints between vsite pairs that have
1116 * a fixed distance due to being constructed from the same
1117 * atoms, since this can be numerically unstable.
1119 for (m = 0; m < vsnral && !bKeep; m++) /* all constr. atoms */
1121 at1 = first_atoms[m];
1122 at2 = first_atoms[(m+1) % vsnral];
1124 for (ftype = 0; ftype < F_NRE; ftype++)
1126 if (interaction_function[ftype].flags & IF_CONSTRAINT)
1128 for (j = 0; (j < plist[ftype].nr) && !bPresent; j++)
1130 /* all constraints until one matches */
1131 bPresent = ( ( (plist[ftype].param[j].ai() == at1) &&
1132 (plist[ftype].param[j].aj() == at2) ) ||
1133 ( (plist[ftype].param[j].ai() == at2) &&
1134 (plist[ftype].param[j].aj() == at1) ) );
1148 /* now copy the bond to the new array */
1149 ps->param[kept_i] = ps->param[i];
1152 else if (IS_CHEMBOND(cftype))
1154 srenew(plist[F_CONNBONDS].param, plist[F_CONNBONDS].nr+1);
1155 plist[F_CONNBONDS].param[plist[F_CONNBONDS].nr] = ps->param[i];
1156 plist[F_CONNBONDS].nr++;
1167 fprintf(stderr, "Removed %4d %15ss with virtual sites, %5d left\n",
1168 nremoved, interaction_function[cftype].longname, kept_i);
1172 fprintf(stderr, "Converted %4d %15ss with virtual sites to connections, %5d left\n",
1173 nconverted, interaction_function[cftype].longname, kept_i);
1177 fprintf(stderr, "Warning: removed %d %ss with vsite with %s construction\n"
1178 " This vsite construction does not guarantee constant "
1180 " If the constructions were generated by pdb2gmx ignore "
1182 nOut, interaction_function[cftype].longname,
1183 interaction_function[F_VSITE3OUT].longname );
1188 static void clean_vsite_angles(t_params *plist, t_pindex pindex[],
1189 int cftype, const int vsite_type[],
1190 at2vsitecon_t *at2vc)
1192 int i, j, k, m, n, nvsite, kept_i;
1194 bool bKeep, bUsed, bPresent, bAll3FAD, bFirstTwo;
1197 ps = &(plist[cftype]);
1199 for (i = 0; (i < ps->nr); i++) /* for all angles in the plist */
1202 const int *first_atoms = nullptr;
1206 /* check if all virtual sites are constructed from the same atoms */
1208 for (k = 0; (k < 3) && !bKeep; k++) /* for all atoms in the angle */
1210 atom = ps->param[i].a[k];
1211 if (vsite_type[atom] != NOTSET && vsite_type[atom] != F_VSITEN)
1214 bAll3FAD = bAll3FAD && (pindex[atom].ftype == F_VSITE3FAD);
1217 /* store construction atoms of first vsite */
1218 vsnral = NRAL(pindex[atom].ftype) - 1;
1219 first_atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1223 GMX_ASSERT(vsnral != 0, "If we've seen a vsite before, we know how many constructing atoms it had");
1224 GMX_ASSERT(first_atoms != nullptr, "If we've seen a vsite before, we know what its first atom index was");
1225 /* check if this vsite is constructed from the same atoms */
1226 if (vsnral == NRAL(pindex[atom].ftype)-1)
1228 for (m = 0; (m < vsnral) && !bKeep; m++)
1233 atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1234 for (n = 0; (n < vsnral) && !bPresent; n++)
1236 if (atoms[m] == first_atoms[n])
1255 /* keep all angles with no virtual sites in them or
1256 with virtual sites with more than 3 constr. atoms */
1257 if (nvsite == 0 && vsnral > 3)
1262 /* check if all non-vsite atoms are used in construction: */
1264 for (k = 0; (k < 3) && !bKeep; k++) /* for all atoms in the angle */
1266 atom = ps->param[i].a[k];
1267 if (vsite_type[atom] == NOTSET)
1270 for (m = 0; (m < vsnral) && !bUsed; m++)
1272 GMX_ASSERT(first_atoms != nullptr, "If we've seen a vsite before, we know what its first atom index was");
1274 if (atom == first_atoms[m])
1277 bFirstTwo = bFirstTwo && m < 2;
1287 if (!( bAll3FAD && bFirstTwo ) )
1289 /* check if all constructing atoms are constrained together */
1290 for (m = 0; m < vsnral && !bKeep; m++) /* all constr. atoms */
1292 at1 = first_atoms[m];
1293 at2 = first_atoms[(m+1) % vsnral];
1295 for (j = 0; j < at2vc[at1].nr; j++)
1297 if (at2vc[at1].aj[j] == at2)
1311 /* now copy the angle to the new array */
1312 ps->param[kept_i] = ps->param[i];
1317 if (ps->nr != kept_i)
1319 fprintf(stderr, "Removed %4d %15ss with virtual sites, %5d left\n",
1320 ps->nr-kept_i, interaction_function[cftype].longname, kept_i);
1325 static void clean_vsite_dihs(t_params *plist, t_pindex pindex[],
1326 int cftype, const int vsite_type[])
1331 ps = &(plist[cftype]);
1334 for (i = 0; (i < ps->nr); i++) /* for all dihedrals in the plist */
1336 int k, m, n, nvsite;
1338 const int *first_atoms = nullptr;
1340 bool bKeep, bUsed, bPresent;
1344 /* check if all virtual sites are constructed from the same atoms */
1346 for (k = 0; (k < 4) && !bKeep; k++) /* for all atoms in the dihedral */
1348 atom = ps->param[i].a[k];
1349 if (vsite_type[atom] != NOTSET && vsite_type[atom] != F_VSITEN)
1353 /* store construction atoms of first vsite */
1354 vsnral = NRAL(pindex[atom].ftype) - 1;
1355 first_atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1359 GMX_ASSERT(vsnral != 0, "If we've seen a vsite before, we know how many constructing atoms it had");
1360 GMX_ASSERT(first_atoms != nullptr, "If we've seen a vsite before, we know what its first atom index was");
1361 /* check if this vsite is constructed from the same atoms */
1362 if (vsnral == NRAL(pindex[atom].ftype)-1)
1364 for (m = 0; (m < vsnral) && !bKeep; m++)
1369 atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1370 for (n = 0; (n < vsnral) && !bPresent; n++)
1372 if (atoms[m] == first_atoms[n])
1384 /* TODO clean_site_bonds and _angles do this increment
1385 at the top of the loop. Refactor this for
1391 /* keep all dihedrals with no virtual sites in them */
1397 /* check if all atoms in dihedral are either virtual sites, or used in
1398 construction of virtual sites. If so, keep it, if not throw away: */
1399 for (k = 0; (k < 4) && !bKeep; k++) /* for all atoms in the dihedral */
1401 GMX_ASSERT(vsnral != 0, "If we've seen a vsite before, we know how many constructing atoms it had");
1402 GMX_ASSERT(first_atoms != nullptr, "If we've seen a vsite before, we know what its first atom index was");
1403 atom = ps->param[i].a[k];
1404 if (vsite_type[atom] == NOTSET)
1406 /* vsnral will be set here, we don't get here with nvsite==0 */
1408 for (m = 0; (m < vsnral) && !bUsed; m++)
1410 if (atom == first_atoms[m])
1424 ps->param[kept_i] = ps->param[i];
1429 if (ps->nr != kept_i)
1431 fprintf(stderr, "Removed %4d %15ss with virtual sites, %5d left\n",
1432 ps->nr-kept_i, interaction_function[cftype].longname, kept_i);
1437 void clean_vsite_bondeds(t_params *plist, int natoms, bool bRmVSiteBds)
1439 int i, k, nvsite, ftype, vsite, parnr;
1442 at2vsitecon_t *at2vc;
1444 pindex = nullptr; /* avoid warnings */
1445 /* make vsite_type array */
1446 snew(vsite_type, natoms);
1447 for (i = 0; i < natoms; i++)
1449 vsite_type[i] = NOTSET;
1452 for (ftype = 0; ftype < F_NRE; ftype++)
1454 if (interaction_function[ftype].flags & IF_VSITE)
1456 nvsite += plist[ftype].nr;
1458 while (i < plist[ftype].nr)
1460 vsite = plist[ftype].param[i].ai();
1461 if (vsite_type[vsite] == NOTSET)
1463 vsite_type[vsite] = ftype;
1467 gmx_fatal(FARGS, "multiple vsite constructions for atom %d", vsite+1);
1469 if (ftype == F_VSITEN)
1471 while (i < plist[ftype].nr && plist[ftype].param[i].ai() == vsite)
1484 /* the rest only if we have virtual sites: */
1487 fprintf(stderr, "Cleaning up constraints %swith virtual sites\n",
1488 bRmVSiteBds ? "and constant bonded interactions " : "");
1490 /* Make a reverse list to avoid ninteractions^2 operations */
1491 at2vc = make_at2vsitecon(natoms, plist);
1493 snew(pindex, natoms);
1494 for (ftype = 0; ftype < F_NRE; ftype++)
1496 /* Here we skip VSITEN. In neary all practical use cases this
1497 * is not an issue, since VSITEN is intended for constructing
1498 * additional interaction sites, not for replacing normal atoms
1499 * with bonded interactions. Thus we do not expect constant
1500 * bonded interactions. If VSITEN does get used with constant
1501 * bonded interactions, these are not removed which only leads
1502 * to very minor extra computation and constant energy.
1503 * The only problematic case is onstraints between VSITEN
1504 * constructions with fixed distance (which is anyhow useless).
1505 * This will generate a fatal error in check_vsite_constraints.
1507 if ((interaction_function[ftype].flags & IF_VSITE) &&
1510 for (parnr = 0; (parnr < plist[ftype].nr); parnr++)
1512 k = plist[ftype].param[parnr].ai();
1513 pindex[k].ftype = ftype;
1514 pindex[k].parnr = parnr;
1519 /* remove interactions that include virtual sites */
1520 for (ftype = 0; ftype < F_NRE; ftype++)
1522 if ( ( ( interaction_function[ftype].flags & IF_BOND ) && bRmVSiteBds ) ||
1523 ( interaction_function[ftype].flags & IF_CONSTRAINT ) )
1525 if (interaction_function[ftype].flags & (IF_BTYPE | IF_CONSTRAINT) )
1527 clean_vsite_bonds (plist, pindex, ftype, vsite_type);
1529 else if (interaction_function[ftype].flags & IF_ATYPE)
1531 clean_vsite_angles(plist, pindex, ftype, vsite_type, at2vc);
1533 else if ( (ftype == F_PDIHS) || (ftype == F_IDIHS) )
1535 clean_vsite_dihs (plist, pindex, ftype, vsite_type);
1539 /* check that no remaining constraints include virtual sites */
1540 for (ftype = 0; ftype < F_NRE; ftype++)
1542 if (interaction_function[ftype].flags & IF_CONSTRAINT)
1544 check_vsite_constraints(plist, ftype, vsite_type);
1548 done_at2vsitecon(natoms, at2vc);