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, 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"
46 #include "gromacs/gmxpreprocess/add_par.h"
47 #include "gromacs/gmxpreprocess/resall.h"
48 #include "gromacs/gmxpreprocess/toputil.h"
49 #include "gromacs/legacyheaders/macros.h"
50 #include "gromacs/legacyheaders/names.h"
51 #include "gromacs/math/units.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/utility/cstringutil.h"
54 #include "gromacs/utility/fatalerror.h"
55 #include "gromacs/utility/smalloc.h"
70 vsitebondparam_t *vsbp;
78 static int vsite_bond_nrcheck(int ftype)
82 if ((interaction_function[ftype].flags & (IF_BTYPE | IF_CONSTRAINT | IF_ATYPE)) || (ftype == F_IDIHS))
84 nrcheck = NRAL(ftype);
94 static void enter_bonded(int nratoms, int *nrbonded, t_mybonded **bondeds,
99 srenew(*bondeds, *nrbonded+1);
101 /* copy atom numbers */
102 for (j = 0; j < nratoms; j++)
104 (*bondeds)[*nrbonded].a[j] = param->a[j];
107 (*bondeds)[*nrbonded].c = param->C0;
112 static void get_bondeds(int nrat, t_iatom atoms[],
113 at2vsitebond_t *at2vb,
114 int *nrbond, t_mybonded **bonds,
115 int *nrang, t_mybonded **angles,
116 int *nridih, t_mybonded **idihs )
118 int k, i, ftype, nrcheck;
121 for (k = 0; k < nrat; k++)
123 for (i = 0; i < at2vb[atoms[k]].nr; i++)
125 ftype = at2vb[atoms[k]].vsbp[i].ftype;
126 param = at2vb[atoms[k]].vsbp[i].param;
127 nrcheck = vsite_bond_nrcheck(ftype);
128 /* abuse nrcheck to see if we're adding bond, angle or idih */
131 case 2: enter_bonded(nrcheck, nrbond, bonds, param); break;
132 case 3: enter_bonded(nrcheck, nrang, angles, param); break;
133 case 4: enter_bonded(nrcheck, nridih, idihs, param); break;
139 static at2vsitebond_t *make_at2vsitebond(int natoms, t_params plist[])
142 int ftype, i, j, nrcheck, nr;
144 at2vsitebond_t *at2vb;
149 for (ftype = 0; (ftype < F_NRE); ftype++)
151 if ((interaction_function[ftype].flags & IF_VSITE) && ftype != F_VSITEN)
153 for (i = 0; (i < plist[ftype].nr); i++)
155 for (j = 0; j < NRAL(ftype); j++)
157 bVSI[plist[ftype].param[i].a[j]] = TRUE;
163 for (ftype = 0; (ftype < F_NRE); ftype++)
165 nrcheck = vsite_bond_nrcheck(ftype);
168 for (i = 0; (i < plist[ftype].nr); i++)
170 aa = plist[ftype].param[i].a;
171 for (j = 0; j < nrcheck; j++)
175 nr = at2vb[aa[j]].nr;
178 srenew(at2vb[aa[j]].vsbp, nr+10);
180 at2vb[aa[j]].vsbp[nr].ftype = ftype;
181 at2vb[aa[j]].vsbp[nr].param = &plist[ftype].param[i];
193 static void done_at2vsitebond(int natoms, at2vsitebond_t *at2vb)
197 for (i = 0; i < natoms; i++)
201 sfree(at2vb[i].vsbp);
207 static at2vsitecon_t *make_at2vsitecon(int natoms, t_params plist[])
210 int ftype, i, j, ai, aj, nr;
211 at2vsitecon_t *at2vc;
216 for (ftype = 0; (ftype < F_NRE); ftype++)
218 if ((interaction_function[ftype].flags & IF_VSITE) && ftype != F_VSITEN)
220 for (i = 0; (i < plist[ftype].nr); i++)
222 for (j = 0; j < NRAL(ftype); j++)
224 bVSI[plist[ftype].param[i].a[j]] = TRUE;
230 for (ftype = 0; (ftype < F_NRE); ftype++)
232 if (interaction_function[ftype].flags & IF_CONSTRAINT)
234 for (i = 0; (i < plist[ftype].nr); i++)
236 ai = plist[ftype].param[i].AI;
237 aj = plist[ftype].param[i].AJ;
238 if (bVSI[ai] && bVSI[aj])
240 /* Store forward direction */
244 srenew(at2vc[ai].aj, nr+10);
246 at2vc[ai].aj[nr] = aj;
248 /* Store backward direction */
252 srenew(at2vc[aj].aj, nr+10);
254 at2vc[aj].aj[nr] = ai;
265 static void done_at2vsitecon(int natoms, at2vsitecon_t *at2vc)
269 for (i = 0; i < natoms; i++)
280 static void print_bad(FILE *fp,
281 int nrbond, t_mybonded *bonds,
282 int nrang, t_mybonded *angles,
283 int nridih, t_mybonded *idihs )
289 fprintf(fp, "bonds:");
290 for (i = 0; i < nrbond; i++)
292 fprintf(fp, " %d-%d (%g)",
293 bonds[i].AI+1, bonds[i].AJ+1, bonds[i].c);
299 fprintf(fp, "angles:");
300 for (i = 0; i < nrang; i++)
302 fprintf(fp, " %d-%d-%d (%g)",
303 angles[i].AI+1, angles[i].AJ+1,
304 angles[i].AK+1, angles[i].c);
310 fprintf(fp, "idihs:");
311 for (i = 0; i < nridih; i++)
313 fprintf(fp, " %d-%d-%d-%d (%g)",
314 idihs[i].AI+1, idihs[i].AJ+1,
315 idihs[i].AK+1, idihs[i].AL+1, idihs[i].c);
321 static void print_param(FILE *fp, int ftype, int i, t_param *param)
324 static int prev_ftype = NOTSET;
325 static int prev_i = NOTSET;
328 if ( (ftype != prev_ftype) || (i != prev_i) )
334 fprintf(fp, "(%d) plist[%s].param[%d]",
335 pass, interaction_function[ftype].name, i);
336 for (j = 0; j < NRFP(ftype); j++)
338 fprintf(fp, ".c[%d]=%g ", j, param->c[j]);
344 static real get_bond_length(int nrbond, t_mybonded bonds[],
345 t_iatom ai, t_iatom aj)
351 for (i = 0; i < nrbond && (bondlen == NOTSET); i++)
353 /* check both ways */
354 if ( ( (ai == bonds[i].AI) && (aj == bonds[i].AJ) ) ||
355 ( (ai == bonds[i].AJ) && (aj == bonds[i].AI) ) )
357 bondlen = bonds[i].c; /* note: bonds[i].c might be NOTSET */
363 static real get_angle(int nrang, t_mybonded angles[],
364 t_iatom ai, t_iatom aj, t_iatom ak)
370 for (i = 0; i < nrang && (angle == NOTSET); i++)
372 /* check both ways */
373 if ( ( (ai == angles[i].AI) && (aj == angles[i].AJ) && (ak == angles[i].AK) ) ||
374 ( (ai == angles[i].AK) && (aj == angles[i].AJ) && (ak == angles[i].AI) ) )
376 angle = DEG2RAD*angles[i].c;
382 static char *get_atomtype_name_AB(t_atom *atom, gpp_atomtype_t atype)
386 name = get_atomtype_name(atom->type, atype);
388 /* When using the decoupling option, atom types are changed
389 * to decoupled for the non-bonded interactions, but the virtual
390 * sites constructions should be based on the "normal" interactions.
391 * So we return the state B atom type name if the state A atom
392 * type is the decoupled one. We should actually check for the atom
393 * type number, but that's not passed here. So we check for
394 * the decoupled atom type name. This should not cause trouble
395 * as this code is only used for topologies with v-sites without
396 * parameters generated by pdb2gmx.
398 if (strcmp(name, "decoupled") == 0)
400 name = get_atomtype_name(atom->typeB, atype);
406 static gmx_bool calc_vsite3_param(gpp_atomtype_t atype,
407 t_param *param, t_atoms *at,
408 int nrbond, t_mybonded *bonds,
409 int nrang, t_mybonded *angles )
411 /* i = virtual site | ,k
412 * j = 1st bonded heavy atom | i-j
413 * k,l = 2nd bonded atoms | `l
416 gmx_bool bXH3, bError;
417 real bjk, bjl, a = -1, b = -1;
418 /* check if this is part of a NH3 , NH2-umbrella or CH3 group,
419 * i.e. if atom k and l are dummy masses (MNH* or MCH3*) */
423 for (i = 0; i < 4; i++)
425 fprintf(debug, "atom %d type %s ",
427 get_atomtype_name_AB(&at->atom[param->a[i]], atype));
429 fprintf(debug, "\n");
432 ( (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->AK], atype), "MNH", 3) == 0) &&
433 (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->AL], atype), "MNH", 3) == 0) ) ||
434 ( (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->AK], atype), "MCH3", 4) == 0) &&
435 (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->AL], atype), "MCH3", 4) == 0) );
437 bjk = get_bond_length(nrbond, bonds, param->AJ, param->AK);
438 bjl = get_bond_length(nrbond, bonds, param->AJ, param->AL);
439 bError = (bjk == NOTSET) || (bjl == NOTSET);
442 /* now we get some XH2/XH3 group specific construction */
443 /* note: we call the heavy atom 'C' and the X atom 'N' */
444 real bMM, bCM, bCN, bNH, aCNH, dH, rH, dM, rM;
447 /* check if bonds from heavy atom (j) to dummy masses (k,l) are equal: */
448 bError = bError || (bjk != bjl);
450 /* the X atom (C or N) in the XH2/XH3 group is the first after the masses: */
451 aN = max(param->AK, param->AL)+1;
453 /* get common bonds */
454 bMM = get_bond_length(nrbond, bonds, param->AK, param->AL);
456 bCN = get_bond_length(nrbond, bonds, param->AJ, aN);
457 bError = bError || (bMM == NOTSET) || (bCN == NOTSET);
459 /* calculate common things */
461 dM = sqrt( sqr(bCM) - sqr(rM) );
463 /* are we dealing with the X atom? */
466 /* this is trivial */
467 a = b = 0.5 * bCN/dM;
472 /* get other bondlengths and angles: */
473 bNH = get_bond_length(nrbond, bonds, aN, param->AI);
474 aCNH = get_angle (nrang, angles, param->AJ, aN, param->AI);
475 bError = bError || (bNH == NOTSET) || (aCNH == NOTSET);
478 dH = bCN - bNH * cos(aCNH);
479 rH = bNH * sin(aCNH);
481 a = 0.5 * ( dH/dM + rH/rM );
482 b = 0.5 * ( dH/dM - rH/rM );
487 gmx_fatal(FARGS, "calc_vsite3_param not implemented for the general case "
488 "(atom %d)", param->AI+1);
496 fprintf(debug, "params for vsite3 %d: %g %g\n",
497 param->AI+1, param->C0, param->C1);
503 static gmx_bool calc_vsite3fd_param(t_param *param,
504 int nrbond, t_mybonded *bonds,
505 int nrang, t_mybonded *angles)
507 /* i = virtual site | ,k
508 * j = 1st bonded heavy atom | i-j
509 * k,l = 2nd bonded atoms | `l
513 real bij, bjk, bjl, aijk, aijl, rk, rl;
515 bij = get_bond_length(nrbond, bonds, param->AI, param->AJ);
516 bjk = get_bond_length(nrbond, bonds, param->AJ, param->AK);
517 bjl = get_bond_length(nrbond, bonds, param->AJ, param->AL);
518 aijk = get_angle (nrang, angles, param->AI, param->AJ, param->AK);
519 aijl = get_angle (nrang, angles, param->AI, param->AJ, param->AL);
520 bError = (bij == NOTSET) || (bjk == NOTSET) || (bjl == NOTSET) ||
521 (aijk == NOTSET) || (aijl == NOTSET);
523 rk = bjk * sin(aijk);
524 rl = bjl * sin(aijl);
525 param->C0 = rk / (rk + rl);
526 param->C1 = -bij; /* 'bond'-length for fixed distance vsite */
530 fprintf(debug, "params for vsite3fd %d: %g %g\n",
531 param->AI+1, param->C0, param->C1);
536 static gmx_bool calc_vsite3fad_param(t_param *param,
537 int nrbond, t_mybonded *bonds,
538 int nrang, t_mybonded *angles)
540 /* i = virtual site |
541 * j = 1st bonded heavy atom | i-j
542 * k = 2nd bonded heavy atom | `k-l
543 * l = 3d bonded heavy atom |
546 gmx_bool bSwapParity, bError;
549 bSwapParity = ( param->C1 == -1 );
551 bij = get_bond_length(nrbond, bonds, param->AI, param->AJ);
552 aijk = get_angle (nrang, angles, param->AI, param->AJ, param->AK);
553 bError = (bij == NOTSET) || (aijk == NOTSET);
555 param->C1 = bij; /* 'bond'-length for fixed distance vsite */
556 param->C0 = RAD2DEG*aijk; /* 'bond'-angle for fixed angle vsite */
560 param->C0 = 360 - param->C0;
565 fprintf(debug, "params for vsite3fad %d: %g %g\n",
566 param->AI+1, param->C0, param->C1);
571 static gmx_bool calc_vsite3out_param(gpp_atomtype_t atype,
572 t_param *param, t_atoms *at,
573 int nrbond, t_mybonded *bonds,
574 int nrang, t_mybonded *angles)
576 /* i = virtual site | ,k
577 * j = 1st bonded heavy atom | i-j
578 * k,l = 2nd bonded atoms | `l
579 * NOTE: i is out of the j-k-l plane!
582 gmx_bool bXH3, bError, bSwapParity;
583 real bij, bjk, bjl, aijk, aijl, akjl, pijk, pijl, a, b, c;
585 /* check if this is part of a NH2-umbrella, NH3 or CH3 group,
586 * i.e. if atom k and l are dummy masses (MNH* or MCH3*) */
590 for (i = 0; i < 4; i++)
592 fprintf(debug, "atom %d type %s ",
593 param->a[i]+1, get_atomtype_name_AB(&at->atom[param->a[i]], atype));
595 fprintf(debug, "\n");
598 ( (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->AK], atype), "MNH", 3) == 0) &&
599 (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->AL], atype), "MNH", 3) == 0) ) ||
600 ( (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->AK], atype), "MCH3", 4) == 0) &&
601 (gmx_strncasecmp(get_atomtype_name_AB(&at->atom[param->AL], atype), "MCH3", 4) == 0) );
603 /* check if construction parity must be swapped */
604 bSwapParity = ( param->C1 == -1 );
606 bjk = get_bond_length(nrbond, bonds, param->AJ, param->AK);
607 bjl = get_bond_length(nrbond, bonds, param->AJ, param->AL);
608 bError = (bjk == NOTSET) || (bjl == NOTSET);
611 /* now we get some XH3 group specific construction */
612 /* note: we call the heavy atom 'C' and the X atom 'N' */
613 real bMM, bCM, bCN, bNH, aCNH, dH, rH, rHx, rHy, dM, rM;
616 /* check if bonds from heavy atom (j) to dummy masses (k,l) are equal: */
617 bError = bError || (bjk != bjl);
619 /* the X atom (C or N) in the XH3 group is the first after the masses: */
620 aN = max(param->AK, param->AL)+1;
622 /* get all bondlengths and angles: */
623 bMM = get_bond_length(nrbond, bonds, param->AK, param->AL);
625 bCN = get_bond_length(nrbond, bonds, param->AJ, aN);
626 bNH = get_bond_length(nrbond, bonds, aN, param->AI);
627 aCNH = get_angle (nrang, angles, param->AJ, aN, param->AI);
629 (bMM == NOTSET) || (bCN == NOTSET) || (bNH == NOTSET) || (aCNH == NOTSET);
632 dH = bCN - bNH * cos(aCNH);
633 rH = bNH * sin(aCNH);
634 /* we assume the H's are symmetrically distributed */
635 rHx = rH*cos(DEG2RAD*30);
636 rHy = rH*sin(DEG2RAD*30);
638 dM = sqrt( sqr(bCM) - sqr(rM) );
639 a = 0.5*( (dH/dM) - (rHy/rM) );
640 b = 0.5*( (dH/dM) + (rHy/rM) );
646 /* this is the general construction */
648 bij = get_bond_length(nrbond, bonds, param->AI, param->AJ);
649 aijk = get_angle (nrang, angles, param->AI, param->AJ, param->AK);
650 aijl = get_angle (nrang, angles, param->AI, param->AJ, param->AL);
651 akjl = get_angle (nrang, angles, param->AK, param->AJ, param->AL);
653 (bij == NOTSET) || (aijk == NOTSET) || (aijl == NOTSET) || (akjl == NOTSET);
655 pijk = cos(aijk)*bij;
656 pijl = cos(aijl)*bij;
657 a = ( pijk + (pijk*cos(akjl)-pijl) * cos(akjl) / sqr(sin(akjl)) ) / bjk;
658 b = ( pijl + (pijl*cos(akjl)-pijk) * cos(akjl) / sqr(sin(akjl)) ) / bjl;
659 c = -sqrt( sqr(bij) -
660 ( sqr(pijk) - 2*pijk*pijl*cos(akjl) + sqr(pijl) )
662 / ( bjk*bjl*sin(akjl) );
677 fprintf(debug, "params for vsite3out %d: %g %g %g\n",
678 param->AI+1, param->C0, param->C1, param->C2);
683 static gmx_bool calc_vsite4fd_param(t_param *param,
684 int nrbond, t_mybonded *bonds,
685 int nrang, t_mybonded *angles)
687 /* i = virtual site | ,k
688 * j = 1st bonded heavy atom | i-j-m
689 * k,l,m = 2nd bonded atoms | `l
693 real bij, bjk, bjl, bjm, aijk, aijl, aijm, akjm, akjl;
694 real pk, pl, pm, cosakl, cosakm, sinakl, sinakm, cl, cm;
696 bij = get_bond_length(nrbond, bonds, param->AI, param->AJ);
697 bjk = get_bond_length(nrbond, bonds, param->AJ, param->AK);
698 bjl = get_bond_length(nrbond, bonds, param->AJ, param->AL);
699 bjm = get_bond_length(nrbond, bonds, param->AJ, param->AM);
700 aijk = get_angle (nrang, angles, param->AI, param->AJ, param->AK);
701 aijl = get_angle (nrang, angles, param->AI, param->AJ, param->AL);
702 aijm = get_angle (nrang, angles, param->AI, param->AJ, param->AM);
703 akjm = get_angle (nrang, angles, param->AK, param->AJ, param->AM);
704 akjl = get_angle (nrang, angles, param->AK, param->AJ, param->AL);
705 bError = (bij == NOTSET) || (bjk == NOTSET) || (bjl == NOTSET) || (bjm == NOTSET) ||
706 (aijk == NOTSET) || (aijl == NOTSET) || (aijm == NOTSET) || (akjm == NOTSET) ||
714 cosakl = (cos(akjl) - cos(aijk)*cos(aijl)) / (sin(aijk)*sin(aijl));
715 cosakm = (cos(akjm) - cos(aijk)*cos(aijm)) / (sin(aijk)*sin(aijm));
716 if (cosakl < -1 || cosakl > 1 || cosakm < -1 || cosakm > 1)
718 fprintf(stderr, "virtual site %d: angle ijk = %f, angle ijl = %f, angle ijm = %f\n",
719 param->AI+1, RAD2DEG*aijk, RAD2DEG*aijl, RAD2DEG*aijm);
720 gmx_fatal(FARGS, "invalid construction in calc_vsite4fd for atom %d: "
721 "cosakl=%f, cosakm=%f\n", param->AI+1, cosakl, cosakm);
723 sinakl = sqrt(1-sqr(cosakl));
724 sinakm = sqrt(1-sqr(cosakm));
726 /* note: there is a '+' because of the way the sines are calculated */
727 cl = -pk / ( pl*cosakl - pk + pl*sinakl*(pm*cosakm-pk)/(pm*sinakm) );
728 cm = -pk / ( pm*cosakm - pk + pm*sinakm*(pl*cosakl-pk)/(pl*sinakl) );
735 fprintf(debug, "params for vsite4fd %d: %g %g %g\n",
736 param->AI+1, param->C0, param->C1, param->C2);
745 calc_vsite4fdn_param(t_param *param,
746 int nrbond, t_mybonded *bonds,
747 int nrang, t_mybonded *angles)
749 /* i = virtual site | ,k
750 * j = 1st bonded heavy atom | i-j-m
751 * k,l,m = 2nd bonded atoms | `l
755 real bij, bjk, bjl, bjm, aijk, aijl, aijm;
756 real pk, pl, pm, a, b;
758 bij = get_bond_length(nrbond, bonds, param->AI, param->AJ);
759 bjk = get_bond_length(nrbond, bonds, param->AJ, param->AK);
760 bjl = get_bond_length(nrbond, bonds, param->AJ, param->AL);
761 bjm = get_bond_length(nrbond, bonds, param->AJ, param->AM);
762 aijk = get_angle (nrang, angles, param->AI, param->AJ, param->AK);
763 aijl = get_angle (nrang, angles, param->AI, param->AJ, param->AL);
764 aijm = get_angle (nrang, angles, param->AI, param->AJ, param->AM);
766 bError = (bij == NOTSET) || (bjk == NOTSET) || (bjl == NOTSET) || (bjm == NOTSET) ||
767 (aijk == NOTSET) || (aijl == NOTSET) || (aijm == NOTSET);
772 /* Calculate component of bond j-k along the direction i-j */
775 /* Calculate component of bond j-l along the direction i-j */
778 /* Calculate component of bond j-m along the direction i-j */
781 if (fabs(pl) < 1000*GMX_REAL_MIN || fabs(pm) < 1000*GMX_REAL_MIN)
783 fprintf(stderr, "virtual site %d: angle ijk = %f, angle ijl = %f, angle ijm = %f\n",
784 param->AI+1, RAD2DEG*aijk, RAD2DEG*aijl, RAD2DEG*aijm);
785 gmx_fatal(FARGS, "invalid construction in calc_vsite4fdn for atom %d: "
786 "pl=%f, pm=%f\n", param->AI+1, pl, pm);
798 fprintf(debug, "params for vsite4fdn %d: %g %g %g\n",
799 param->AI+1, param->C0, param->C1, param->C2);
808 int set_vsites(gmx_bool bVerbose, t_atoms *atoms, gpp_atomtype_t atype,
812 int nvsite, nrbond, nrang, nridih, nrset;
813 gmx_bool bFirst, bSet, bERROR;
814 at2vsitebond_t *at2vb;
824 fprintf(debug, "\nCalculating parameters for virtual sites\n");
827 /* Make a reverse list to avoid ninteractions^2 operations */
828 at2vb = make_at2vsitebond(atoms->nr, plist);
830 for (ftype = 0; (ftype < F_NRE); ftype++)
832 if (interaction_function[ftype].flags & IF_VSITE)
834 nvsite += plist[ftype].nr;
836 if (ftype == F_VSITEN)
838 /* We don't calculate parameters for VSITEN */
843 for (i = 0; (i < plist[ftype].nr); i++)
845 /* check if all parameters are set */
847 for (j = 0; j < NRFP(ftype) && bSet; j++)
849 bSet = plist[ftype].param[i].c[j] != NOTSET;
854 fprintf(debug, "bSet=%s ", bool_names[bSet]);
855 print_param(debug, ftype, i, &plist[ftype].param[i]);
859 if (bVerbose && bFirst)
861 fprintf(stderr, "Calculating parameters for virtual sites\n");
865 nrbond = nrang = nridih = 0;
870 /* now set the vsite parameters: */
871 get_bondeds(NRAL(ftype), plist[ftype].param[i].a, at2vb,
872 &nrbond, &bonds, &nrang, &angles, &nridih, &idihs);
875 fprintf(debug, "Found %d bonds, %d angles and %d idihs "
876 "for virtual site %d (%s)\n", nrbond, nrang, nridih,
877 plist[ftype].param[i].AI+1,
878 interaction_function[ftype].longname);
879 print_bad(debug, nrbond, bonds, nrang, angles, nridih, idihs);
885 calc_vsite3_param(atype, &(plist[ftype].param[i]), atoms,
886 nrbond, bonds, nrang, angles);
890 calc_vsite3fd_param(&(plist[ftype].param[i]),
891 nrbond, bonds, nrang, angles);
895 calc_vsite3fad_param(&(plist[ftype].param[i]),
896 nrbond, bonds, nrang, angles);
900 calc_vsite3out_param(atype, &(plist[ftype].param[i]), atoms,
901 nrbond, bonds, nrang, angles);
905 calc_vsite4fd_param(&(plist[ftype].param[i]),
906 nrbond, bonds, nrang, angles);
910 calc_vsite4fdn_param(&(plist[ftype].param[i]),
911 nrbond, bonds, nrang, angles);
914 gmx_fatal(FARGS, "Automatic parameter generation not supported "
916 interaction_function[ftype].longname,
917 plist[ftype].param[i].AI+1);
921 gmx_fatal(FARGS, "Automatic parameter generation not supported "
922 "for %s atom %d for this bonding configuration",
923 interaction_function[ftype].longname,
924 plist[ftype].param[i].AI+1);
931 if (debug && plist[ftype].nr)
933 fprintf(stderr, "Calculated parameters for %d out of %d %s atoms\n",
934 nrset, plist[ftype].nr, interaction_function[ftype].longname);
939 done_at2vsitebond(atoms->nr, at2vb);
944 void set_vsites_ptype(gmx_bool bVerbose, gmx_moltype_t *molt)
953 fprintf(stderr, "Setting particle type to V for virtual sites\n");
957 fprintf(stderr, "checking %d functypes\n", F_NRE);
959 for (ftype = 0; ftype < F_NRE; ftype++)
961 il = &molt->ilist[ftype];
962 if (interaction_function[ftype].flags & IF_VSITE)
964 nra = interaction_function[ftype].nratoms;
970 fprintf(stderr, "doing %d %s virtual sites\n",
971 (nrd / (nra+1)), interaction_function[ftype].longname);
974 for (i = 0; (i < nrd); )
976 /* The virtual site */
978 molt->atoms.atom[avsite].ptype = eptVSite;
992 static void check_vsite_constraints(t_params *plist,
993 int cftype, int vsite_type[])
1000 ps = &(plist[cftype]);
1001 for (i = 0; (i < ps->nr); i++)
1003 for (k = 0; k < 2; k++)
1005 atom = ps->param[i].a[k];
1006 if (vsite_type[atom] != NOTSET)
1008 fprintf(stderr, "ERROR: Cannot have constraint (%d-%d) with virtual site (%d)\n",
1009 ps->param[i].AI+1, ps->param[i].AJ+1, atom+1);
1016 gmx_fatal(FARGS, "There were %d virtual sites involved in constraints", n);
1020 static void clean_vsite_bonds(t_params *plist, t_pindex pindex[],
1021 int cftype, int vsite_type[])
1023 int ftype, i, j, parnr, k, l, m, n, nvsite, nOut, kept_i, vsitetype;
1024 int nconverted, nremoved;
1025 atom_id atom, oatom, at1, at2;
1026 gmx_bool bKeep, bRemove, bUsed, bPresent, bThisFD, bThisOUT, bAllFD, bFirstTwo;
1029 if (cftype == F_CONNBONDS)
1034 ps = &(plist[cftype]);
1039 for (i = 0; (i < ps->nr); i++) /* for all bonds in the plist */
1042 const atom_id *first_atoms = NULL;
1047 /* check if all virtual sites are constructed from the same atoms */
1051 fprintf(debug, "constr %d %d:", ps->param[i].AI+1, ps->param[i].AJ+1);
1053 for (k = 0; (k < 2) && !bKeep && !bRemove; k++)
1055 /* for all atoms in the bond */
1056 atom = ps->param[i].a[k];
1057 if (vsite_type[atom] != NOTSET && vsite_type[atom] != F_VSITEN)
1060 bThisFD = ( (pindex[atom].ftype == F_VSITE3FD ) ||
1061 (pindex[atom].ftype == F_VSITE3FAD) ||
1062 (pindex[atom].ftype == F_VSITE4FD ) ||
1063 (pindex[atom].ftype == F_VSITE4FDN ) );
1064 bThisOUT = ( (pindex[atom].ftype == F_VSITE3OUT) &&
1065 (interaction_function[cftype].flags & IF_CONSTRAINT) );
1066 bAllFD = bAllFD && bThisFD;
1067 if (bThisFD || bThisOUT)
1071 fprintf(debug, " %s", bThisOUT ? "out" : "fd");
1073 oatom = ps->param[i].a[1-k]; /* the other atom */
1074 if (vsite_type[oatom] == NOTSET &&
1075 vsite_type[oatom] != F_VSITEN &&
1076 oatom == plist[pindex[atom].ftype].param[pindex[atom].parnr].AJ)
1078 /* if the other atom isn't a vsite, and it is AI */
1086 fprintf(debug, " D-AI");
1092 /* TODO This fragment, and corresponding logic in
1093 clean_vsite_angles and clean_vsite_dihs should
1094 be refactored into a common function */
1097 /* if this is the first vsite we encounter then
1098 store construction atoms */
1099 /* TODO This would be nicer to implement with
1100 a C++ "vector view" class" with an
1101 STL-container-like interface. */
1102 vsnral = NRAL(pindex[atom].ftype) - 1;
1103 first_atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1107 assert(vsnral != 0);
1108 assert(first_atoms != NULL);
1109 /* if it is not the first then
1110 check if this vsite is constructed from the same atoms */
1111 if (vsnral == NRAL(pindex[atom].ftype)-1)
1113 for (m = 0; (m < vsnral) && !bKeep; m++)
1115 const atom_id *atoms;
1118 atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1119 for (n = 0; (n < vsnral) && !bPresent; n++)
1121 if (atoms[m] == first_atoms[n])
1131 fprintf(debug, " !present");
1141 fprintf(debug, " !same#at");
1155 /* if we have no virtual sites in this bond, keep it */
1160 fprintf(debug, " no vsite");
1165 /* TODO This loop and the corresponding loop in
1166 check_vsite_angles should be refactored into a common
1168 /* check if all non-vsite atoms are used in construction: */
1170 for (k = 0; (k < 2) && !bKeep; k++) /* for all atoms in the bond */
1172 atom = ps->param[i].a[k];
1173 if (vsite_type[atom] == NOTSET && vsite_type[atom] != F_VSITEN)
1176 for (m = 0; (m < vsnral) && !bUsed; m++)
1178 assert(first_atoms != NULL);
1180 if (atom == first_atoms[m])
1183 bFirstTwo = bFirstTwo && m < 2;
1191 fprintf(debug, " !used");
1197 if (!( bAllFD && bFirstTwo ) )
1199 /* Two atom bonded interactions include constraints.
1200 * We need to remove constraints between vsite pairs that have
1201 * a fixed distance due to being constructed from the same
1202 * atoms, since this can be numerically unstable.
1204 for (m = 0; m < vsnral && !bKeep; m++) /* all constr. atoms */
1206 at1 = first_atoms[m];
1207 at2 = first_atoms[(m+1) % vsnral];
1209 for (ftype = 0; ftype < F_NRE; ftype++)
1211 if (interaction_function[ftype].flags & IF_CONSTRAINT)
1213 for (j = 0; (j < plist[ftype].nr) && !bPresent; j++)
1215 /* all constraints until one matches */
1216 bPresent = ( ( (plist[ftype].param[j].AI == at1) &&
1217 (plist[ftype].param[j].AJ == at2) ) ||
1218 ( (plist[ftype].param[j].AI == at2) &&
1219 (plist[ftype].param[j].AJ == at1) ) );
1228 fprintf(debug, " !bonded");
1239 fprintf(debug, " keeping");
1241 /* now copy the bond to the new array */
1242 ps->param[kept_i] = ps->param[i];
1245 else if (IS_CHEMBOND(cftype))
1247 srenew(plist[F_CONNBONDS].param, plist[F_CONNBONDS].nr+1);
1248 plist[F_CONNBONDS].param[plist[F_CONNBONDS].nr] = ps->param[i];
1249 plist[F_CONNBONDS].nr++;
1258 fprintf(debug, "\n");
1264 fprintf(stderr, "Removed %4d %15ss with virtual sites, %5d left\n",
1265 nremoved, interaction_function[cftype].longname, kept_i);
1269 fprintf(stderr, "Converted %4d %15ss with virtual sites to connections, %5d left\n",
1270 nconverted, interaction_function[cftype].longname, kept_i);
1274 fprintf(stderr, "Warning: removed %d %ss with vsite with %s construction\n"
1275 " This vsite construction does not guarantee constant "
1277 " If the constructions were generated by pdb2gmx ignore "
1279 nOut, interaction_function[cftype].longname,
1280 interaction_function[F_VSITE3OUT].longname );
1285 static void clean_vsite_angles(t_params *plist, t_pindex pindex[],
1286 int cftype, int vsite_type[],
1287 at2vsitecon_t *at2vc)
1289 int i, j, parnr, k, l, m, n, nvsite, kept_i, vsitetype;
1290 atom_id atom, at1, at2;
1291 gmx_bool bKeep, bUsed, bPresent, bAll3FAD, bFirstTwo;
1294 ps = &(plist[cftype]);
1296 for (i = 0; (i < ps->nr); i++) /* for all angles in the plist */
1299 const atom_id *first_atoms = NULL;
1303 /* check if all virtual sites are constructed from the same atoms */
1305 for (k = 0; (k < 3) && !bKeep; k++) /* for all atoms in the angle */
1307 atom = ps->param[i].a[k];
1308 if (vsite_type[atom] != NOTSET && vsite_type[atom] != F_VSITEN)
1311 bAll3FAD = bAll3FAD && (pindex[atom].ftype == F_VSITE3FAD);
1314 /* store construction atoms of first vsite */
1315 vsnral = NRAL(pindex[atom].ftype) - 1;
1316 first_atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1320 assert(vsnral != 0);
1321 assert(first_atoms != NULL);
1322 /* check if this vsite is constructed from the same atoms */
1323 if (vsnral == NRAL(pindex[atom].ftype)-1)
1325 for (m = 0; (m < vsnral) && !bKeep; m++)
1327 const atom_id *atoms;
1330 atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1331 for (n = 0; (n < vsnral) && !bPresent; n++)
1333 if (atoms[m] == first_atoms[n])
1352 /* keep all angles with no virtual sites in them or
1353 with virtual sites with more than 3 constr. atoms */
1354 if (nvsite == 0 && vsnral > 3)
1359 /* check if all non-vsite atoms are used in construction: */
1361 for (k = 0; (k < 3) && !bKeep; k++) /* for all atoms in the angle */
1363 atom = ps->param[i].a[k];
1364 if (vsite_type[atom] == NOTSET && vsite_type[atom] != F_VSITEN)
1367 for (m = 0; (m < vsnral) && !bUsed; m++)
1369 assert(first_atoms != NULL);
1371 if (atom == first_atoms[m])
1374 bFirstTwo = bFirstTwo && m < 2;
1384 if (!( bAll3FAD && bFirstTwo ) )
1386 /* check if all constructing atoms are constrained together */
1387 for (m = 0; m < vsnral && !bKeep; m++) /* all constr. atoms */
1389 at1 = first_atoms[m];
1390 at2 = first_atoms[(m+1) % vsnral];
1392 for (j = 0; j < at2vc[at1].nr; j++)
1394 if (at2vc[at1].aj[j] == at2)
1408 /* now copy the angle to the new array */
1409 ps->param[kept_i] = ps->param[i];
1414 if (ps->nr != kept_i)
1416 fprintf(stderr, "Removed %4d %15ss with virtual sites, %5d left\n",
1417 ps->nr-kept_i, interaction_function[cftype].longname, kept_i);
1422 static void clean_vsite_dihs(t_params *plist, t_pindex pindex[],
1423 int cftype, int vsite_type[])
1428 ps = &(plist[cftype]);
1431 for (i = 0; (i < ps->nr); i++) /* for all dihedrals in the plist */
1433 int ftype, parnr, k, l, m, n, nvsite;
1435 const atom_id *first_atoms = NULL;
1437 gmx_bool bKeep, bUsed, bPresent;
1441 /* check if all virtual sites are constructed from the same atoms */
1443 for (k = 0; (k < 4) && !bKeep; k++) /* for all atoms in the dihedral */
1445 atom = ps->param[i].a[k];
1446 if (vsite_type[atom] != NOTSET && vsite_type[atom] != F_VSITEN)
1450 /* store construction atoms of first vsite */
1451 vsnral = NRAL(pindex[atom].ftype) - 1;
1452 first_atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1455 fprintf(debug, "dih w. vsite: %d %d %d %d\n",
1456 ps->param[i].AI+1, ps->param[i].AJ+1,
1457 ps->param[i].AK+1, ps->param[i].AL+1);
1458 fprintf(debug, "vsite %d from: %d %d %d\n",
1459 atom+1, first_atoms[0]+1, first_atoms[1]+1, first_atoms[2]+1);
1464 assert(vsnral != 0);
1465 assert(first_atoms != NULL);
1467 /* check if this vsite is constructed from the same atoms */
1468 if (vsnral == NRAL(pindex[atom].ftype)-1)
1470 for (m = 0; (m < vsnral) && !bKeep; m++)
1472 const atom_id *atoms;
1475 atoms = plist[pindex[atom].ftype].param[pindex[atom].parnr].a + 1;
1476 for (n = 0; (n < vsnral) && !bPresent; n++)
1478 if (atoms[m] == first_atoms[n])
1490 /* TODO clean_site_bonds and _angles do this increment
1491 at the top of the loop. Refactor this for
1497 /* keep all dihedrals with no virtual sites in them */
1503 /* check if all atoms in dihedral are either virtual sites, or used in
1504 construction of virtual sites. If so, keep it, if not throw away: */
1505 for (k = 0; (k < 4) && !bKeep; k++) /* for all atoms in the dihedral */
1507 assert(vsnral != 0);
1508 assert(first_atoms != NULL);
1510 atom = ps->param[i].a[k];
1511 if (vsite_type[atom] == NOTSET && vsite_type[atom] != F_VSITEN)
1513 /* vsnral will be set here, we don't get here with nvsite==0 */
1515 for (m = 0; (m < vsnral) && !bUsed; m++)
1517 if (atom == first_atoms[m])
1527 fprintf(debug, "unused atom in dih: %d\n", atom+1);
1535 ps->param[kept_i] = ps->param[i];
1540 if (ps->nr != kept_i)
1542 fprintf(stderr, "Removed %4d %15ss with virtual sites, %5d left\n",
1543 ps->nr-kept_i, interaction_function[cftype].longname, kept_i);
1548 void clean_vsite_bondeds(t_params *plist, int natoms, gmx_bool bRmVSiteBds)
1550 int i, k, nvsite, ftype, vsite, parnr;
1553 at2vsitecon_t *at2vc;
1555 pindex = 0; /* avoid warnings */
1556 /* make vsite_type array */
1557 snew(vsite_type, natoms);
1558 for (i = 0; i < natoms; i++)
1560 vsite_type[i] = NOTSET;
1563 for (ftype = 0; ftype < F_NRE; ftype++)
1565 if (interaction_function[ftype].flags & IF_VSITE)
1567 nvsite += plist[ftype].nr;
1569 while (i < plist[ftype].nr)
1571 vsite = plist[ftype].param[i].AI;
1572 if (vsite_type[vsite] == NOTSET)
1574 vsite_type[vsite] = ftype;
1578 gmx_fatal(FARGS, "multiple vsite constructions for atom %d", vsite+1);
1580 if (ftype == F_VSITEN)
1582 while (i < plist[ftype].nr && plist[ftype].param[i].AI == vsite)
1595 /* the rest only if we have virtual sites: */
1598 fprintf(stderr, "Cleaning up constraints %swith virtual sites\n",
1599 bRmVSiteBds ? "and constant bonded interactions " : "");
1601 /* Make a reverse list to avoid ninteractions^2 operations */
1602 at2vc = make_at2vsitecon(natoms, plist);
1604 snew(pindex, natoms);
1605 for (ftype = 0; ftype < F_NRE; ftype++)
1607 /* Here we skip VSITEN. In neary all practical use cases this
1608 * is not an issue, since VSITEN is intended for constructing
1609 * additional interaction sites, not for replacing normal atoms
1610 * with bonded interactions. Thus we do not expect constant
1611 * bonded interactions. If VSITEN does get used with constant
1612 * bonded interactions, these are not removed which only leads
1613 * to very minor extra computation and constant energy.
1614 * The only problematic case is onstraints between VSITEN
1615 * constructions with fixed distance (which is anyhow useless).
1616 * This will generate a fatal error in check_vsite_constraints.
1618 if ((interaction_function[ftype].flags & IF_VSITE) &&
1621 for (parnr = 0; (parnr < plist[ftype].nr); parnr++)
1623 k = plist[ftype].param[parnr].AI;
1624 pindex[k].ftype = ftype;
1625 pindex[k].parnr = parnr;
1632 for (i = 0; i < natoms; i++)
1634 fprintf(debug, "atom %d vsite_type %s\n", i,
1635 vsite_type[i] == NOTSET ? "NOTSET" :
1636 interaction_function[vsite_type[i]].name);
1640 /* remove interactions that include virtual sites */
1641 for (ftype = 0; ftype < F_NRE; ftype++)
1643 if ( ( ( interaction_function[ftype].flags & IF_BOND ) && bRmVSiteBds ) ||
1644 ( interaction_function[ftype].flags & IF_CONSTRAINT ) )
1646 if (interaction_function[ftype].flags & (IF_BTYPE | IF_CONSTRAINT) )
1648 clean_vsite_bonds (plist, pindex, ftype, vsite_type);
1650 else if (interaction_function[ftype].flags & IF_ATYPE)
1652 clean_vsite_angles(plist, pindex, ftype, vsite_type, at2vc);
1654 else if ( (ftype == F_PDIHS) || (ftype == F_IDIHS) )
1656 clean_vsite_dihs (plist, pindex, ftype, vsite_type);
1660 /* check that no remaining constraints include virtual sites */
1661 for (ftype = 0; ftype < F_NRE; ftype++)
1663 if (interaction_function[ftype].flags & IF_CONSTRAINT)
1665 check_vsite_constraints(plist, ftype, vsite_type);
1669 done_at2vsitecon(natoms, at2vc);