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, 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/notset.h"
49 #include "gromacs/gmxpreprocess/resall.h"
50 #include "gromacs/gmxpreprocess/toputil.h"
51 #include "gromacs/math/functions.h"
52 #include "gromacs/math/units.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/mdtypes/md_enums.h"
55 #include "gromacs/topology/ifunc.h"
56 #include "gromacs/topology/topology.h"
57 #include "gromacs/utility/cstringutil.h"
58 #include "gromacs/utility/fatalerror.h"
59 #include "gromacs/utility/gmxassert.h"
60 #include "gromacs/utility/smalloc.h"
61 #include "gromacs/utility/strconvert.h"
66 t_iatom &ai() { return a[0]; }
67 t_iatom &aj() { return a[1]; }
68 t_iatom &ak() { return a[2]; }
69 t_iatom &al() { return a[3]; }
80 vsitebondparam_t *vsbp;
88 static int vsite_bond_nrcheck(int ftype)
92 if ((interaction_function[ftype].flags & (IF_BTYPE | IF_CONSTRAINT | IF_ATYPE)) || (ftype == F_IDIHS))
94 nrcheck = NRAL(ftype);
104 static void enter_bonded(int nratoms, int *nrbonded, t_mybonded **bondeds,
109 srenew(*bondeds, *nrbonded+1);
111 /* copy atom numbers */
112 for (j = 0; j < nratoms; j++)
114 (*bondeds)[*nrbonded].a[j] = param->a[j];
117 (*bondeds)[*nrbonded].c = param->c0();
122 static void get_bondeds(int nrat, const t_iatom atoms[],
123 at2vsitebond_t *at2vb,
124 int *nrbond, t_mybonded **bonds,
125 int *nrang, t_mybonded **angles,
126 int *nridih, t_mybonded **idihs )
128 int k, i, ftype, nrcheck;
131 for (k = 0; k < nrat; k++)
133 for (i = 0; i < at2vb[atoms[k]].nr; i++)
135 ftype = at2vb[atoms[k]].vsbp[i].ftype;
136 param = at2vb[atoms[k]].vsbp[i].param;
137 nrcheck = vsite_bond_nrcheck(ftype);
138 /* abuse nrcheck to see if we're adding bond, angle or idih */
141 case 2: enter_bonded(nrcheck, nrbond, bonds, param); break;
142 case 3: enter_bonded(nrcheck, nrang, angles, param); break;
143 case 4: enter_bonded(nrcheck, nridih, idihs, param); break;
149 static at2vsitebond_t *make_at2vsitebond(int natoms, t_params plist[])
152 int ftype, i, j, nrcheck, nr;
154 at2vsitebond_t *at2vb;
159 for (ftype = 0; (ftype < F_NRE); ftype++)
161 if ((interaction_function[ftype].flags & IF_VSITE) && ftype != F_VSITEN)
163 for (i = 0; (i < plist[ftype].nr); i++)
165 for (j = 0; j < NRAL(ftype); j++)
167 bVSI[plist[ftype].param[i].a[j]] = TRUE;
173 for (ftype = 0; (ftype < F_NRE); ftype++)
175 nrcheck = vsite_bond_nrcheck(ftype);
178 for (i = 0; (i < plist[ftype].nr); i++)
180 aa = plist[ftype].param[i].a;
181 for (j = 0; j < nrcheck; j++)
185 nr = at2vb[aa[j]].nr;
188 srenew(at2vb[aa[j]].vsbp, nr+10);
190 at2vb[aa[j]].vsbp[nr].ftype = ftype;
191 at2vb[aa[j]].vsbp[nr].param = &plist[ftype].param[i];
203 static void done_at2vsitebond(int natoms, at2vsitebond_t *at2vb)
207 for (i = 0; i < natoms; i++)
211 sfree(at2vb[i].vsbp);
217 static at2vsitecon_t *make_at2vsitecon(int natoms, t_params plist[])
220 int ftype, i, j, ai, aj, nr;
221 at2vsitecon_t *at2vc;
226 for (ftype = 0; (ftype < F_NRE); ftype++)
228 if ((interaction_function[ftype].flags & IF_VSITE) && ftype != F_VSITEN)
230 for (i = 0; (i < plist[ftype].nr); i++)
232 for (j = 0; j < NRAL(ftype); j++)
234 bVSI[plist[ftype].param[i].a[j]] = TRUE;
240 for (ftype = 0; (ftype < F_NRE); ftype++)
242 if (interaction_function[ftype].flags & IF_CONSTRAINT)
244 for (i = 0; (i < plist[ftype].nr); i++)
246 ai = plist[ftype].param[i].ai();
247 aj = plist[ftype].param[i].aj();
248 if (bVSI[ai] && bVSI[aj])
250 /* Store forward direction */
254 srenew(at2vc[ai].aj, nr+10);
256 at2vc[ai].aj[nr] = aj;
258 /* Store backward direction */
262 srenew(at2vc[aj].aj, nr+10);
264 at2vc[aj].aj[nr] = ai;
275 static void done_at2vsitecon(int natoms, at2vsitecon_t *at2vc)
279 for (i = 0; i < natoms; i++)
290 static void print_bad(FILE *fp,
291 int nrbond, t_mybonded *bonds,
292 int nrang, t_mybonded *angles,
293 int nridih, t_mybonded *idihs )
299 fprintf(fp, "bonds:");
300 for (i = 0; i < nrbond; i++)
302 fprintf(fp, " %d-%d (%g)",
303 bonds[i].ai()+1, bonds[i].aj()+1, bonds[i].c);
309 fprintf(fp, "angles:");
310 for (i = 0; i < nrang; i++)
312 fprintf(fp, " %d-%d-%d (%g)",
313 angles[i].ai()+1, angles[i].aj()+1,
314 angles[i].ak()+1, angles[i].c);
320 fprintf(fp, "idihs:");
321 for (i = 0; i < nridih; i++)
323 fprintf(fp, " %d-%d-%d-%d (%g)",
324 idihs[i].ai()+1, idihs[i].aj()+1,
325 idihs[i].ak()+1, idihs[i].al()+1, idihs[i].c);
331 static void print_param(FILE *fp, int ftype, int i, t_param *param)
334 static int prev_ftype = NOTSET;
335 static int prev_i = NOTSET;
338 if ( (ftype != prev_ftype) || (i != prev_i) )
344 fprintf(fp, "(%d) plist[%s].param[%d]",
345 pass, interaction_function[ftype].name, i);
346 for (j = 0; j < NRFP(ftype); j++)
348 fprintf(fp, ".c[%d]=%g ", j, param->c[j]);
354 static real get_bond_length(int nrbond, t_mybonded bonds[],
355 t_iatom ai, t_iatom aj)
361 for (i = 0; i < nrbond && (bondlen == NOTSET); i++)
363 /* check both ways */
364 if ( ( (ai == bonds[i].ai()) && (aj == bonds[i].aj()) ) ||
365 ( (ai == bonds[i].aj()) && (aj == bonds[i].ai()) ) )
367 bondlen = bonds[i].c; /* note: bonds[i].c might be NOTSET */
373 static real get_angle(int nrang, t_mybonded angles[],
374 t_iatom ai, t_iatom aj, t_iatom ak)
380 for (i = 0; i < nrang && (angle == NOTSET); i++)
382 /* check both ways */
383 if ( ( (ai == angles[i].ai()) && (aj == angles[i].aj()) && (ak == angles[i].ak()) ) ||
384 ( (ai == angles[i].ak()) && (aj == angles[i].aj()) && (ak == angles[i].ai()) ) )
386 angle = DEG2RAD*angles[i].c;
392 static char *get_atomtype_name_AB(t_atom *atom, gpp_atomtype_t atype)
396 name = get_atomtype_name(atom->type, atype);
398 /* When using the decoupling option, atom types are changed
399 * to decoupled for the non-bonded interactions, but the virtual
400 * sites constructions should be based on the "normal" interactions.
401 * So we return the state B atom type name if the state A atom
402 * type is the decoupled one. We should actually check for the atom
403 * type number, but that's not passed here. So we check for
404 * the decoupled atom type name. This should not cause trouble
405 * as this code is only used for topologies with v-sites without
406 * parameters generated by pdb2gmx.
408 if (strcmp(name, "decoupled") == 0)
410 name = get_atomtype_name(atom->typeB, atype);
416 static bool calc_vsite3_param(gpp_atomtype_t atype,
417 t_param *param, t_atoms *at,
418 int nrbond, t_mybonded *bonds,
419 int nrang, t_mybonded *angles )
421 /* i = virtual site | ,k
422 * j = 1st bonded heavy atom | i-j
423 * k,l = 2nd bonded atoms | `l
427 real bjk, bjl, a = -1, b = -1;
428 /* check if this is part of a NH3 , NH2-umbrella or CH3 group,
429 * i.e. if atom k and l are dummy masses (MNH* or MCH3*) */
431 ( (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->ak()], atype), "MNH", 3) == 0) &&
432 (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->al()], atype), "MNH", 3) == 0) ) ||
433 ( (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->ak()], atype), "MCH3", 4) == 0) &&
434 (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->al()], atype), "MCH3", 4) == 0) );
436 bjk = get_bond_length(nrbond, bonds, param->aj(), param->ak());
437 bjl = get_bond_length(nrbond, bonds, param->aj(), param->al());
438 bError = (bjk == NOTSET) || (bjl == NOTSET);
441 /* now we get some XH2/XH3 group specific construction */
442 /* note: we call the heavy atom 'C' and the X atom 'N' */
443 real bMM, bCM, bCN, bNH, aCNH, dH, rH, dM, rM;
446 /* check if bonds from heavy atom (j) to dummy masses (k,l) are equal: */
447 bError = bError || (bjk != bjl);
449 /* the X atom (C or N) in the XH2/XH3 group is the first after the masses: */
450 aN = std::max(param->ak(), param->al())+1;
452 /* get common bonds */
453 bMM = get_bond_length(nrbond, bonds, param->ak(), param->al());
455 bCN = get_bond_length(nrbond, bonds, param->aj(), aN);
456 bError = bError || (bMM == NOTSET) || (bCN == NOTSET);
458 /* calculate common things */
460 dM = std::sqrt( gmx::square(bCM) - gmx::square(rM) );
462 /* are we dealing with the X atom? */
463 if (param->ai() == aN)
465 /* this is trivial */
466 a = b = 0.5 * bCN/dM;
471 /* get other bondlengths and angles: */
472 bNH = get_bond_length(nrbond, bonds, aN, param->ai());
473 aCNH = get_angle (nrang, angles, param->aj(), aN, param->ai());
474 bError = bError || (bNH == NOTSET) || (aCNH == NOTSET);
477 dH = bCN - bNH * std::cos(aCNH);
478 rH = bNH * std::sin(aCNH);
480 a = 0.5 * ( dH/dM + rH/rM );
481 b = 0.5 * ( dH/dM - rH/rM );
486 gmx_fatal(FARGS, "calc_vsite3_param not implemented for the general case "
487 "(atom %d)", param->ai()+1);
496 static bool calc_vsite3fd_param(t_param *param,
497 int nrbond, t_mybonded *bonds,
498 int nrang, t_mybonded *angles)
500 /* i = virtual site | ,k
501 * j = 1st bonded heavy atom | i-j
502 * k,l = 2nd bonded atoms | `l
506 real bij, bjk, bjl, aijk, aijl, rk, rl;
508 bij = get_bond_length(nrbond, bonds, param->ai(), param->aj());
509 bjk = get_bond_length(nrbond, bonds, param->aj(), param->ak());
510 bjl = get_bond_length(nrbond, bonds, param->aj(), param->al());
511 aijk = get_angle (nrang, angles, param->ai(), param->aj(), param->ak());
512 aijl = get_angle (nrang, angles, param->ai(), param->aj(), param->al());
513 bError = (bij == NOTSET) || (bjk == NOTSET) || (bjl == NOTSET) ||
514 (aijk == NOTSET) || (aijl == NOTSET);
516 rk = bjk * std::sin(aijk);
517 rl = bjl * std::sin(aijl);
518 param->c0() = rk / (rk + rl);
519 param->c1() = -bij; /* 'bond'-length for fixed distance vsite */
524 static bool calc_vsite3fad_param(t_param *param,
525 int nrbond, t_mybonded *bonds,
526 int nrang, t_mybonded *angles)
528 /* i = virtual site |
529 * j = 1st bonded heavy atom | i-j
530 * k = 2nd bonded heavy atom | `k-l
531 * l = 3d bonded heavy atom |
534 bool bSwapParity, bError;
537 bSwapParity = ( param->c1() == -1 );
539 bij = get_bond_length(nrbond, bonds, param->ai(), param->aj());
540 aijk = get_angle (nrang, angles, param->ai(), param->aj(), param->ak());
541 bError = (bij == NOTSET) || (aijk == NOTSET);
543 param->c1() = bij; /* 'bond'-length for fixed distance vsite */
544 param->c0() = RAD2DEG*aijk; /* 'bond'-angle for fixed angle vsite */
548 param->c0() = 360 - param->c0();
554 static bool calc_vsite3out_param(gpp_atomtype_t atype,
555 t_param *param, t_atoms *at,
556 int nrbond, t_mybonded *bonds,
557 int nrang, t_mybonded *angles)
559 /* i = virtual site | ,k
560 * j = 1st bonded heavy atom | i-j
561 * k,l = 2nd bonded atoms | `l
562 * NOTE: i is out of the j-k-l plane!
565 bool bXH3, bError, bSwapParity;
566 real bij, bjk, bjl, aijk, aijl, akjl, pijk, pijl, a, b, c;
568 /* check if this is part of a NH2-umbrella, NH3 or CH3 group,
569 * i.e. if atom k and l are dummy masses (MNH* or MCH3*) */
571 ( (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->ak()], atype), "MNH", 3) == 0) &&
572 (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->al()], atype), "MNH", 3) == 0) ) ||
573 ( (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->ak()], atype), "MCH3", 4) == 0) &&
574 (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->al()], atype), "MCH3", 4) == 0) );
576 /* check if construction parity must be swapped */
577 bSwapParity = ( param->c1() == -1 );
579 bjk = get_bond_length(nrbond, bonds, param->aj(), param->ak());
580 bjl = get_bond_length(nrbond, bonds, param->aj(), param->al());
581 bError = (bjk == NOTSET) || (bjl == NOTSET);
584 /* now we get some XH3 group specific construction */
585 /* note: we call the heavy atom 'C' and the X atom 'N' */
586 real bMM, bCM, bCN, bNH, aCNH, dH, rH, rHx, rHy, dM, rM;
589 /* check if bonds from heavy atom (j) to dummy masses (k,l) are equal: */
590 bError = bError || (bjk != bjl);
592 /* the X atom (C or N) in the XH3 group is the first after the masses: */
593 aN = std::max(param->ak(), param->al())+1;
595 /* get all bondlengths and angles: */
596 bMM = get_bond_length(nrbond, bonds, param->ak(), param->al());
598 bCN = get_bond_length(nrbond, bonds, param->aj(), aN);
599 bNH = get_bond_length(nrbond, bonds, aN, param->ai());
600 aCNH = get_angle (nrang, angles, param->aj(), aN, param->ai());
602 (bMM == NOTSET) || (bCN == NOTSET) || (bNH == NOTSET) || (aCNH == NOTSET);
605 dH = bCN - bNH * std::cos(aCNH);
606 rH = bNH * std::sin(aCNH);
607 /* we assume the H's are symmetrically distributed */
608 rHx = rH*std::cos(DEG2RAD*30);
609 rHy = rH*std::sin(DEG2RAD*30);
611 dM = std::sqrt( gmx::square(bCM) - gmx::square(rM) );
612 a = 0.5*( (dH/dM) - (rHy/rM) );
613 b = 0.5*( (dH/dM) + (rHy/rM) );
619 /* this is the general construction */
621 bij = get_bond_length(nrbond, bonds, param->ai(), param->aj());
622 aijk = get_angle (nrang, angles, param->ai(), param->aj(), param->ak());
623 aijl = get_angle (nrang, angles, param->ai(), param->aj(), param->al());
624 akjl = get_angle (nrang, angles, param->ak(), param->aj(), param->al());
626 (bij == NOTSET) || (aijk == NOTSET) || (aijl == NOTSET) || (akjl == NOTSET);
628 pijk = std::cos(aijk)*bij;
629 pijl = std::cos(aijl)*bij;
630 a = ( pijk + (pijk*std::cos(akjl)-pijl) * std::cos(akjl) / gmx::square(std::sin(akjl)) ) / bjk;
631 b = ( pijl + (pijl*std::cos(akjl)-pijk) * std::cos(akjl) / gmx::square(std::sin(akjl)) ) / bjl;
632 c = -std::sqrt( gmx::square(bij) -
633 ( gmx::square(pijk) - 2*pijk*pijl*std::cos(akjl) + gmx::square(pijl) )
634 / gmx::square(std::sin(akjl)) )
635 / ( bjk*bjl*std::sin(akjl) );
651 static bool calc_vsite4fd_param(t_param *param,
652 int nrbond, t_mybonded *bonds,
653 int nrang, t_mybonded *angles)
655 /* i = virtual site | ,k
656 * j = 1st bonded heavy atom | i-j-m
657 * k,l,m = 2nd bonded atoms | `l
661 real bij, bjk, bjl, bjm, aijk, aijl, aijm, akjm, akjl;
662 real pk, pl, pm, cosakl, cosakm, sinakl, sinakm, cl, cm;
664 bij = get_bond_length(nrbond, bonds, param->ai(), param->aj());
665 bjk = get_bond_length(nrbond, bonds, param->aj(), param->ak());
666 bjl = get_bond_length(nrbond, bonds, param->aj(), param->al());
667 bjm = get_bond_length(nrbond, bonds, param->aj(), param->am());
668 aijk = get_angle (nrang, angles, param->ai(), param->aj(), param->ak());
669 aijl = get_angle (nrang, angles, param->ai(), param->aj(), param->al());
670 aijm = get_angle (nrang, angles, param->ai(), param->aj(), param->am());
671 akjm = get_angle (nrang, angles, param->ak(), param->aj(), param->am());
672 akjl = get_angle (nrang, angles, param->ak(), param->aj(), param->al());
673 bError = (bij == NOTSET) || (bjk == NOTSET) || (bjl == NOTSET) || (bjm == NOTSET) ||
674 (aijk == NOTSET) || (aijl == NOTSET) || (aijm == NOTSET) || (akjm == NOTSET) ||
679 pk = bjk*std::sin(aijk);
680 pl = bjl*std::sin(aijl);
681 pm = bjm*std::sin(aijm);
682 cosakl = (std::cos(akjl) - std::cos(aijk)*std::cos(aijl)) / (std::sin(aijk)*std::sin(aijl));
683 cosakm = (std::cos(akjm) - std::cos(aijk)*std::cos(aijm)) / (std::sin(aijk)*std::sin(aijm));
684 if (cosakl < -1 || cosakl > 1 || cosakm < -1 || cosakm > 1)
686 fprintf(stderr, "virtual site %d: angle ijk = %f, angle ijl = %f, angle ijm = %f\n",
687 param->ai()+1, RAD2DEG*aijk, RAD2DEG*aijl, RAD2DEG*aijm);
688 gmx_fatal(FARGS, "invalid construction in calc_vsite4fd for atom %d: "
689 "cosakl=%f, cosakm=%f\n", param->ai()+1, cosakl, cosakm);
691 sinakl = std::sqrt(1-gmx::square(cosakl));
692 sinakm = std::sqrt(1-gmx::square(cosakm));
694 /* note: there is a '+' because of the way the sines are calculated */
695 cl = -pk / ( pl*cosakl - pk + pl*sinakl*(pm*cosakm-pk)/(pm*sinakm) );
696 cm = -pk / ( pm*cosakm - pk + pm*sinakm*(pl*cosakl-pk)/(pl*sinakl) );
708 calc_vsite4fdn_param(t_param *param,
709 int nrbond, t_mybonded *bonds,
710 int nrang, t_mybonded *angles)
712 /* i = virtual site | ,k
713 * j = 1st bonded heavy atom | i-j-m
714 * k,l,m = 2nd bonded atoms | `l
718 real bij, bjk, bjl, bjm, aijk, aijl, aijm;
719 real pk, pl, pm, a, b;
721 bij = get_bond_length(nrbond, bonds, param->ai(), param->aj());
722 bjk = get_bond_length(nrbond, bonds, param->aj(), param->ak());
723 bjl = get_bond_length(nrbond, bonds, param->aj(), param->al());
724 bjm = get_bond_length(nrbond, bonds, param->aj(), param->am());
725 aijk = get_angle (nrang, angles, param->ai(), param->aj(), param->ak());
726 aijl = get_angle (nrang, angles, param->ai(), param->aj(), param->al());
727 aijm = get_angle (nrang, angles, param->ai(), param->aj(), param->am());
729 bError = (bij == NOTSET) || (bjk == NOTSET) || (bjl == NOTSET) || (bjm == NOTSET) ||
730 (aijk == NOTSET) || (aijl == NOTSET) || (aijm == NOTSET);
735 /* Calculate component of bond j-k along the direction i-j */
736 pk = -bjk*std::cos(aijk);
738 /* Calculate component of bond j-l along the direction i-j */
739 pl = -bjl*std::cos(aijl);
741 /* Calculate component of bond j-m along the direction i-j */
742 pm = -bjm*std::cos(aijm);
744 if (fabs(pl) < 1000*GMX_REAL_MIN || fabs(pm) < 1000*GMX_REAL_MIN)
746 fprintf(stderr, "virtual site %d: angle ijk = %f, angle ijl = %f, angle ijm = %f\n",
747 param->ai()+1, RAD2DEG*aijk, RAD2DEG*aijl, RAD2DEG*aijm);
748 gmx_fatal(FARGS, "invalid construction in calc_vsite4fdn for atom %d: "
749 "pl=%f, pm=%f\n", param->ai()+1, pl, pm);
766 int set_vsites(bool bVerbose, t_atoms *atoms, gpp_atomtype_t atype,
770 int nvsite, nrbond, nrang, nridih, nrset;
771 bool bFirst, bSet, bERROR;
772 at2vsitebond_t *at2vb;
780 /* Make a reverse list to avoid ninteractions^2 operations */
781 at2vb = make_at2vsitebond(atoms->nr, plist);
783 for (ftype = 0; (ftype < F_NRE); ftype++)
785 if (interaction_function[ftype].flags & IF_VSITE)
787 nvsite += plist[ftype].nr;
789 if (ftype == F_VSITEN)
791 /* We don't calculate parameters for VSITEN */
796 for (i = 0; (i < plist[ftype].nr); i++)
798 /* check if all parameters are set */
800 for (j = 0; j < NRFP(ftype) && bSet; j++)
802 bSet = plist[ftype].param[i].c[j] != NOTSET;
807 fprintf(debug, "bSet=%s ", gmx::boolToString(bSet));
808 print_param(debug, ftype, i, &plist[ftype].param[i]);
812 if (bVerbose && bFirst)
814 fprintf(stderr, "Calculating parameters for virtual sites\n");
818 nrbond = nrang = nridih = 0;
823 /* now set the vsite parameters: */
824 get_bondeds(NRAL(ftype), plist[ftype].param[i].a, at2vb,
825 &nrbond, &bonds, &nrang, &angles, &nridih, &idihs);
828 fprintf(debug, "Found %d bonds, %d angles and %d idihs "
829 "for virtual site %d (%s)\n", nrbond, nrang, nridih,
830 plist[ftype].param[i].ai()+1,
831 interaction_function[ftype].longname);
832 print_bad(debug, nrbond, bonds, nrang, angles, nridih, idihs);
838 calc_vsite3_param(atype, &(plist[ftype].param[i]), atoms,
839 nrbond, bonds, nrang, angles);
843 calc_vsite3fd_param(&(plist[ftype].param[i]),
844 nrbond, bonds, nrang, angles);
848 calc_vsite3fad_param(&(plist[ftype].param[i]),
849 nrbond, bonds, nrang, angles);
853 calc_vsite3out_param(atype, &(plist[ftype].param[i]), atoms,
854 nrbond, bonds, nrang, angles);
858 calc_vsite4fd_param(&(plist[ftype].param[i]),
859 nrbond, bonds, nrang, angles);
863 calc_vsite4fdn_param(&(plist[ftype].param[i]),
864 nrbond, bonds, nrang, angles);
867 gmx_fatal(FARGS, "Automatic parameter generation not supported "
869 interaction_function[ftype].longname,
870 plist[ftype].param[i].ai()+1);
875 gmx_fatal(FARGS, "Automatic parameter generation not supported "
876 "for %s atom %d for this bonding configuration",
877 interaction_function[ftype].longname,
878 plist[ftype].param[i].ai()+1);
888 done_at2vsitebond(atoms->nr, at2vb);
893 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 il = &molt->ilist[ftype];
907 if (interaction_function[ftype].flags & IF_VSITE)
909 nra = interaction_function[ftype].nratoms;
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 */
923 molt->atoms.atom[avsite].ptype = eptVSite;
937 static void check_vsite_constraints(t_params *plist,
938 int cftype, const int vsite_type[])
945 ps = &(plist[cftype]);
946 for (i = 0; (i < ps->nr); i++)
948 for (k = 0; k < 2; k++)
950 atom = ps->param[i].a[k];
951 if (vsite_type[atom] != NOTSET)
953 fprintf(stderr, "ERROR: Cannot have constraint (%d-%d) with virtual site (%d)\n",
954 ps->param[i].ai()+1, ps->param[i].aj()+1, atom+1);
961 gmx_fatal(FARGS, "There were %d virtual sites involved in constraints", n);
965 static void clean_vsite_bonds(t_params *plist, t_pindex pindex[],
966 int cftype, const int vsite_type[])
968 int ftype, i, j, k, m, n, nvsite, nOut, kept_i;
969 int nconverted, nremoved;
970 int atom, oatom, at1, at2;
971 bool bKeep, bRemove, bUsed, bPresent, bThisFD, bThisOUT, bAllFD, bFirstTwo;
974 if (cftype == F_CONNBONDS)
979 ps = &(plist[cftype]);
984 for (i = 0; (i < ps->nr); i++) /* for all bonds in the plist */
987 const int *first_atoms = nullptr;
992 /* check if all virtual sites are constructed from the same atoms */
994 for (k = 0; (k < 2) && !bKeep && !bRemove; k++)
996 /* for all atoms in the bond */
997 atom = ps->param[i].a[k];
998 if (vsite_type[atom] != NOTSET && vsite_type[atom] != F_VSITEN)
1001 bThisFD = ( (pindex[atom].ftype == F_VSITE3FD ) ||
1002 (pindex[atom].ftype == F_VSITE3FAD) ||
1003 (pindex[atom].ftype == F_VSITE4FD ) ||
1004 (pindex[atom].ftype == F_VSITE4FDN ) );
1005 bThisOUT = ( (pindex[atom].ftype == F_VSITE3OUT) &&
1006 ((interaction_function[cftype].flags & IF_CONSTRAINT) != 0u) );
1007 bAllFD = bAllFD && bThisFD;
1008 if (bThisFD || bThisOUT)
1010 oatom = ps->param[i].a[1-k]; /* the other atom */
1011 if (vsite_type[oatom] == NOTSET &&
1012 oatom == plist[pindex[atom].ftype].param[pindex[atom].parnr].aj())
1014 /* if the other atom isn't a vsite, and it is AI */
1024 /* TODO This fragment, and corresponding logic in
1025 clean_vsite_angles and clean_vsite_dihs should
1026 be refactored into a common function */
1029 /* if this is the first vsite we encounter then
1030 store construction atoms */
1031 /* TODO This would be nicer to implement with
1032 a C++ "vector view" class" with an
1033 STL-container-like interface. */
1034 vsnral = NRAL(pindex[atom].ftype) - 1;
1035 first_atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1039 GMX_ASSERT(vsnral != 0, "nvsite > 1 must have vsnral != 0");
1040 GMX_ASSERT(first_atoms != nullptr, "nvsite > 1 must have first_atoms != NULL");
1041 /* if it is not the first then
1042 check if this vsite is constructed from the same atoms */
1043 if (vsnral == NRAL(pindex[atom].ftype)-1)
1045 for (m = 0; (m < vsnral) && !bKeep; m++)
1050 atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1051 for (n = 0; (n < vsnral) && !bPresent; n++)
1053 if (atoms[m] == first_atoms[n])
1079 /* if we have no virtual sites in this bond, keep it */
1085 /* TODO This loop and the corresponding loop in
1086 check_vsite_angles should be refactored into a common
1088 /* check if all non-vsite atoms are used in construction: */
1090 for (k = 0; (k < 2) && !bKeep; k++) /* for all atoms in the bond */
1092 atom = ps->param[i].a[k];
1093 if (vsite_type[atom] == NOTSET)
1096 for (m = 0; (m < vsnral) && !bUsed; m++)
1098 GMX_ASSERT(first_atoms != nullptr, "If we've seen a vsite before, we know what its first atom index was");
1100 if (atom == first_atoms[m])
1103 bFirstTwo = bFirstTwo && m < 2;
1113 if (!( bAllFD && bFirstTwo ) )
1115 /* Two atom bonded interactions include constraints.
1116 * We need to remove constraints between vsite pairs that have
1117 * a fixed distance due to being constructed from the same
1118 * atoms, since this can be numerically unstable.
1120 for (m = 0; m < vsnral && !bKeep; m++) /* all constr. atoms */
1122 at1 = first_atoms[m];
1123 at2 = first_atoms[(m+1) % vsnral];
1125 for (ftype = 0; ftype < F_NRE; ftype++)
1127 if (interaction_function[ftype].flags & IF_CONSTRAINT)
1129 for (j = 0; (j < plist[ftype].nr) && !bPresent; j++)
1131 /* all constraints until one matches */
1132 bPresent = ( ( (plist[ftype].param[j].ai() == at1) &&
1133 (plist[ftype].param[j].aj() == at2) ) ||
1134 ( (plist[ftype].param[j].ai() == at2) &&
1135 (plist[ftype].param[j].aj() == at1) ) );
1149 /* now copy the bond to the new array */
1150 ps->param[kept_i] = ps->param[i];
1153 else if (IS_CHEMBOND(cftype))
1155 srenew(plist[F_CONNBONDS].param, plist[F_CONNBONDS].nr+1);
1156 plist[F_CONNBONDS].param[plist[F_CONNBONDS].nr] = ps->param[i];
1157 plist[F_CONNBONDS].nr++;
1168 fprintf(stderr, "Removed %4d %15ss with virtual sites, %5d left\n",
1169 nremoved, interaction_function[cftype].longname, kept_i);
1173 fprintf(stderr, "Converted %4d %15ss with virtual sites to connections, %5d left\n",
1174 nconverted, interaction_function[cftype].longname, kept_i);
1178 fprintf(stderr, "Warning: removed %d %ss with vsite with %s construction\n"
1179 " This vsite construction does not guarantee constant "
1181 " If the constructions were generated by pdb2gmx ignore "
1183 nOut, interaction_function[cftype].longname,
1184 interaction_function[F_VSITE3OUT].longname );
1189 static void clean_vsite_angles(t_params *plist, t_pindex pindex[],
1190 int cftype, const int vsite_type[],
1191 at2vsitecon_t *at2vc)
1193 int i, j, k, m, n, nvsite, kept_i;
1195 bool bKeep, bUsed, bPresent, bAll3FAD, bFirstTwo;
1198 ps = &(plist[cftype]);
1200 for (i = 0; (i < ps->nr); i++) /* for all angles in the plist */
1203 const int *first_atoms = nullptr;
1207 /* check if all virtual sites are constructed from the same atoms */
1209 for (k = 0; (k < 3) && !bKeep; k++) /* for all atoms in the angle */
1211 atom = ps->param[i].a[k];
1212 if (vsite_type[atom] != NOTSET && vsite_type[atom] != F_VSITEN)
1215 bAll3FAD = bAll3FAD && (pindex[atom].ftype == F_VSITE3FAD);
1218 /* store construction atoms of first vsite */
1219 vsnral = NRAL(pindex[atom].ftype) - 1;
1220 first_atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1224 GMX_ASSERT(vsnral != 0, "If we've seen a vsite before, we know how many constructing atoms it had");
1225 GMX_ASSERT(first_atoms != nullptr, "If we've seen a vsite before, we know what its first atom index was");
1226 /* check if this vsite is constructed from the same atoms */
1227 if (vsnral == NRAL(pindex[atom].ftype)-1)
1229 for (m = 0; (m < vsnral) && !bKeep; m++)
1234 atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1235 for (n = 0; (n < vsnral) && !bPresent; n++)
1237 if (atoms[m] == first_atoms[n])
1256 /* keep all angles with no virtual sites in them or
1257 with virtual sites with more than 3 constr. atoms */
1258 if (nvsite == 0 && vsnral > 3)
1263 /* check if all non-vsite atoms are used in construction: */
1265 for (k = 0; (k < 3) && !bKeep; k++) /* for all atoms in the angle */
1267 atom = ps->param[i].a[k];
1268 if (vsite_type[atom] == NOTSET)
1271 for (m = 0; (m < vsnral) && !bUsed; m++)
1273 GMX_ASSERT(first_atoms != nullptr, "If we've seen a vsite before, we know what its first atom index was");
1275 if (atom == first_atoms[m])
1278 bFirstTwo = bFirstTwo && m < 2;
1288 if (!( bAll3FAD && bFirstTwo ) )
1290 /* check if all constructing atoms are constrained together */
1291 for (m = 0; m < vsnral && !bKeep; m++) /* all constr. atoms */
1293 at1 = first_atoms[m];
1294 at2 = first_atoms[(m+1) % vsnral];
1296 for (j = 0; j < at2vc[at1].nr; j++)
1298 if (at2vc[at1].aj[j] == at2)
1312 /* now copy the angle to the new array */
1313 ps->param[kept_i] = ps->param[i];
1318 if (ps->nr != kept_i)
1320 fprintf(stderr, "Removed %4d %15ss with virtual sites, %5d left\n",
1321 ps->nr-kept_i, interaction_function[cftype].longname, kept_i);
1326 static void clean_vsite_dihs(t_params *plist, t_pindex pindex[],
1327 int cftype, const int vsite_type[])
1332 ps = &(plist[cftype]);
1335 for (i = 0; (i < ps->nr); i++) /* for all dihedrals in the plist */
1337 int k, m, n, nvsite;
1339 const int *first_atoms = nullptr;
1341 bool bKeep, bUsed, bPresent;
1345 /* check if all virtual sites are constructed from the same atoms */
1347 for (k = 0; (k < 4) && !bKeep; k++) /* for all atoms in the dihedral */
1349 atom = ps->param[i].a[k];
1350 if (vsite_type[atom] != NOTSET && vsite_type[atom] != F_VSITEN)
1354 /* store construction atoms of first vsite */
1355 vsnral = NRAL(pindex[atom].ftype) - 1;
1356 first_atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1360 GMX_ASSERT(vsnral != 0, "If we've seen a vsite before, we know how many constructing atoms it had");
1361 GMX_ASSERT(first_atoms != nullptr, "If we've seen a vsite before, we know what its first atom index was");
1362 /* check if this vsite is constructed from the same atoms */
1363 if (vsnral == NRAL(pindex[atom].ftype)-1)
1365 for (m = 0; (m < vsnral) && !bKeep; m++)
1370 atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1371 for (n = 0; (n < vsnral) && !bPresent; n++)
1373 if (atoms[m] == first_atoms[n])
1385 /* TODO clean_site_bonds and _angles do this increment
1386 at the top of the loop. Refactor this for
1392 /* keep all dihedrals with no virtual sites in them */
1398 /* check if all atoms in dihedral are either virtual sites, or used in
1399 construction of virtual sites. If so, keep it, if not throw away: */
1400 for (k = 0; (k < 4) && !bKeep; k++) /* for all atoms in the dihedral */
1402 GMX_ASSERT(vsnral != 0, "If we've seen a vsite before, we know how many constructing atoms it had");
1403 GMX_ASSERT(first_atoms != nullptr, "If we've seen a vsite before, we know what its first atom index was");
1404 atom = ps->param[i].a[k];
1405 if (vsite_type[atom] == NOTSET)
1407 /* vsnral will be set here, we don't get here with nvsite==0 */
1409 for (m = 0; (m < vsnral) && !bUsed; m++)
1411 if (atom == first_atoms[m])
1425 ps->param[kept_i] = ps->param[i];
1430 if (ps->nr != kept_i)
1432 fprintf(stderr, "Removed %4d %15ss with virtual sites, %5d left\n",
1433 ps->nr-kept_i, interaction_function[cftype].longname, kept_i);
1438 void clean_vsite_bondeds(t_params *plist, int natoms, bool bRmVSiteBds)
1440 int i, k, nvsite, ftype, vsite, parnr;
1443 at2vsitecon_t *at2vc;
1445 pindex = nullptr; /* avoid warnings */
1446 /* make vsite_type array */
1447 snew(vsite_type, natoms);
1448 for (i = 0; i < natoms; i++)
1450 vsite_type[i] = NOTSET;
1453 for (ftype = 0; ftype < F_NRE; ftype++)
1455 if (interaction_function[ftype].flags & IF_VSITE)
1457 nvsite += plist[ftype].nr;
1459 while (i < plist[ftype].nr)
1461 vsite = plist[ftype].param[i].ai();
1462 if (vsite_type[vsite] == NOTSET)
1464 vsite_type[vsite] = ftype;
1468 gmx_fatal(FARGS, "multiple vsite constructions for atom %d", vsite+1);
1470 if (ftype == F_VSITEN)
1472 while (i < plist[ftype].nr && plist[ftype].param[i].ai() == vsite)
1485 /* the rest only if we have virtual sites: */
1488 fprintf(stderr, "Cleaning up constraints %swith virtual sites\n",
1489 bRmVSiteBds ? "and constant bonded interactions " : "");
1491 /* Make a reverse list to avoid ninteractions^2 operations */
1492 at2vc = make_at2vsitecon(natoms, plist);
1494 snew(pindex, natoms);
1495 for (ftype = 0; ftype < F_NRE; ftype++)
1497 /* Here we skip VSITEN. In neary all practical use cases this
1498 * is not an issue, since VSITEN is intended for constructing
1499 * additional interaction sites, not for replacing normal atoms
1500 * with bonded interactions. Thus we do not expect constant
1501 * bonded interactions. If VSITEN does get used with constant
1502 * bonded interactions, these are not removed which only leads
1503 * to very minor extra computation and constant energy.
1504 * The only problematic case is onstraints between VSITEN
1505 * constructions with fixed distance (which is anyhow useless).
1506 * This will generate a fatal error in check_vsite_constraints.
1508 if ((interaction_function[ftype].flags & IF_VSITE) &&
1511 for (parnr = 0; (parnr < plist[ftype].nr); parnr++)
1513 k = plist[ftype].param[parnr].ai();
1514 pindex[k].ftype = ftype;
1515 pindex[k].parnr = parnr;
1520 /* remove interactions that include virtual sites */
1521 for (ftype = 0; ftype < F_NRE; ftype++)
1523 if ( ( ( interaction_function[ftype].flags & IF_BOND ) && bRmVSiteBds ) ||
1524 ( interaction_function[ftype].flags & IF_CONSTRAINT ) )
1526 if (interaction_function[ftype].flags & (IF_BTYPE | IF_CONSTRAINT) )
1528 clean_vsite_bonds (plist, pindex, ftype, vsite_type);
1530 else if (interaction_function[ftype].flags & IF_ATYPE)
1532 clean_vsite_angles(plist, pindex, ftype, vsite_type, at2vc);
1534 else if ( (ftype == F_PDIHS) || (ftype == F_IDIHS) )
1536 clean_vsite_dihs (plist, pindex, ftype, vsite_type);
1540 /* check that no remaining constraints include virtual sites */
1541 for (ftype = 0; ftype < F_NRE; ftype++)
1543 if (interaction_function[ftype].flags & IF_CONSTRAINT)
1545 check_vsite_constraints(plist, ftype, vsite_type);
1549 done_at2vsitecon(natoms, at2vc);