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.
46 #include "gromacs/fileio/filenm.h"
47 #include "gromacs/fileio/gmxfio.h"
48 #include "gromacs/fileio/xvgr.h"
49 #include "gromacs/legacyheaders/copyrite.h"
50 #include "gromacs/legacyheaders/gmx_ga2la.h"
51 #include "gromacs/legacyheaders/macros.h"
52 #include "gromacs/legacyheaders/mdrun.h"
53 #include "gromacs/legacyheaders/names.h"
54 #include "gromacs/legacyheaders/network.h"
55 #include "gromacs/legacyheaders/typedefs.h"
56 #include "gromacs/legacyheaders/types/commrec.h"
57 #include "gromacs/math/vec.h"
58 #include "gromacs/pbcutil/pbc.h"
59 #include "gromacs/topology/mtop_util.h"
60 #include "gromacs/utility/futil.h"
61 #include "gromacs/utility/smalloc.h"
63 static void pull_print_group_x(FILE *out, ivec dim, const t_pull_group *pgrp)
67 for (m = 0; m < DIM; m++)
71 fprintf(out, "\t%g", pgrp->x[m]);
76 static void pull_print_coord_dr(FILE *out, const t_pull_coord *pcrd,
77 gmx_bool bPrintRefValue, double t,
78 gmx_bool bPrintComponents)
80 double distance, distance2;
83 if (pcrd->eGeom == epullgDIST)
85 /* Geometry=distance: print the length of the distance vector */
87 for (m = 0; m < DIM; m++)
91 distance2 += pcrd->dr[m]*pcrd->dr[m];
94 distance = sqrt(distance2);
98 /* Geometry=direction-like: print distance along the pull vector */
100 for (m = 0; m < DIM; m++)
104 distance += pcrd->dr[m]*pcrd->vec[m];
108 fprintf(out, "\t%g", distance);
112 fprintf(out, "\t%g", pcrd->init + pcrd->rate*t);
115 if (bPrintComponents)
117 for (m = 0; m < DIM; m++)
121 fprintf(out, "\t%g", pcrd->dr[m]);
127 static void pull_print_x(FILE *out, t_pull *pull, double t)
132 fprintf(out, "%.4f", t);
134 for (c = 0; c < pull->ncoord; c++)
136 pcrd = &pull->coord[c];
138 if (pull->bPrintCOM1)
140 if (pcrd->eGeom == epullgCYL)
142 pull_print_group_x(out, pcrd->dim, &pull->dyna[c]);
146 pull_print_group_x(out, pcrd->dim, &pull->group[pcrd->group[0]]);
149 if (pull->bPrintCOM2)
151 pull_print_group_x(out, pcrd->dim, &pull->group[pcrd->group[1]]);
153 pull_print_coord_dr(out, pcrd,
154 pull->bPrintRefValue, t, pull->bPrintComp);
159 static void pull_print_f(FILE *out, t_pull *pull, double t)
163 fprintf(out, "%.4f", t);
165 for (c = 0; c < pull->ncoord; c++)
167 fprintf(out, "\t%g", pull->coord[c].f_scal);
172 void pull_print_output(t_pull *pull, gmx_int64_t step, double time)
174 if ((pull->nstxout != 0) && (step % pull->nstxout == 0))
176 pull_print_x(pull->out_x, pull, time);
179 if ((pull->nstfout != 0) && (step % pull->nstfout == 0))
181 pull_print_f(pull->out_f, pull, time);
185 static FILE *open_pull_out(const char *fn, t_pull *pull, const output_env_t oenv,
186 gmx_bool bCoord, unsigned long Flags)
190 char **setname, buf[10];
192 if (Flags & MD_APPENDFILES)
194 fp = gmx_fio_fopen(fn, "a+");
198 fp = gmx_fio_fopen(fn, "w+");
201 xvgr_header(fp, "Pull COM", "Time (ps)", "Position (nm)",
206 xvgr_header(fp, "Pull force", "Time (ps)", "Force (kJ/mol/nm)",
210 /* With default mdp options only the actual distance is printed,
211 * but optionally 2 COMs, the reference distance and distance
212 * components can also be printed.
214 snew(setname, pull->ncoord*(DIM + DIM + 1 + 1 + DIM));
216 for (c = 0; c < pull->ncoord; c++)
220 /* The order of this legend should match the order of printing
221 * the data in print_pull_x above.
224 if (pull->bPrintCOM1)
226 /* Legend for reference group position */
227 for (m = 0; m < DIM; m++)
229 if (pull->coord[c].dim[m])
231 sprintf(buf, "%d %s%d%c", c+1, "c", 1, 'X'+m);
232 setname[nsets] = gmx_strdup(buf);
237 if (pull->bPrintCOM2)
239 /* Legend for reference group position */
240 for (m = 0; m < DIM; m++)
242 if (pull->coord[c].dim[m])
244 sprintf(buf, "%d %s%d%c", c+1, "c", 2, 'X'+m);
245 setname[nsets] = gmx_strdup(buf);
250 /* The pull coord distance */
251 sprintf(buf, "%d", c+1);
252 setname[nsets] = gmx_strdup(buf);
254 if (pull->bPrintRefValue)
256 sprintf(buf, "%c%d", 'r', c+1);
257 setname[nsets] = gmx_strdup(buf);
260 if (pull->bPrintComp)
262 /* The pull coord distance components */
263 for (m = 0; m < DIM; m++)
265 if (pull->coord[c].dim[m])
267 sprintf(buf, "%d %s%c", c+1, "d", 'X'+m);
268 setname[nsets] = gmx_strdup(buf);
276 /* For the pull force we always only use one scalar */
277 sprintf(buf, "%d", c+1);
278 setname[nsets] = gmx_strdup(buf);
284 xvgr_legend(fp, nsets, (const char**)setname, oenv);
286 for (c = 0; c < nsets; c++)
296 /* Apply forces in a mass weighted fashion */
297 static void apply_forces_grp(const t_pull_group *pgrp, const t_mdatoms *md,
298 const dvec f_pull, int sign, rvec *f)
301 double wmass, inv_wm;
303 inv_wm = pgrp->mwscale;
305 for (i = 0; i < pgrp->nat_loc; i++)
307 ii = pgrp->ind_loc[i];
308 wmass = md->massT[ii];
309 if (pgrp->weight_loc)
311 wmass *= pgrp->weight_loc[i];
314 for (m = 0; m < DIM; m++)
316 f[ii][m] += sign * wmass * f_pull[m] * inv_wm;
321 /* Apply forces in a mass weighted fashion to a cylinder group */
322 static void apply_forces_cyl_grp(const t_pull_group *pgrp, double dv_corr,
324 const dvec f_pull, double f_scal,
328 double mass, weight, inv_wm, dv_com;
330 inv_wm = pgrp->mwscale;
332 for (i = 0; i < pgrp->nat_loc; i++)
334 ii = pgrp->ind_loc[i];
335 mass = md->massT[ii];
336 weight = pgrp->weight_loc[i];
337 /* The stored axial distance from the cylinder center (dv) needs
338 * to be corrected for an offset (dv_corr), which was unknown when
341 dv_com = pgrp->dv[i] + dv_corr;
343 /* Here we not only add the pull force working along vec (f_pull),
344 * but also a radial component, due to the dependence of the weights
345 * on the radial distance.
347 for (m = 0; m < DIM; m++)
349 f[ii][m] += sign*inv_wm*(mass*weight*f_pull[m] +
350 pgrp->mdw[i][m]*dv_com*f_scal);
355 /* Apply forces in a mass weighted fashion */
356 static void apply_forces(t_pull * pull, t_mdatoms * md, rvec *f)
359 const t_pull_coord *pcrd;
361 for (c = 0; c < pull->ncoord; c++)
363 pcrd = &pull->coord[c];
365 if (pcrd->eGeom == epullgCYL)
370 apply_forces_cyl_grp(&pull->dyna[c], pcrd->cyl_dev, md,
371 pcrd->f, pcrd->f_scal, -1, f);
373 /* Sum the force along the vector and the radial force */
374 for (m = 0; m < DIM; m++)
376 f_tot[m] = pcrd->f[m] + pcrd->f_scal*pcrd->ffrad[m];
378 apply_forces_grp(&pull->group[pcrd->group[1]], md, f_tot, 1, f);
382 if (pull->group[pcrd->group[0]].nat > 0)
384 apply_forces_grp(&pull->group[pcrd->group[0]], md, pcrd->f, -1, f);
386 apply_forces_grp(&pull->group[pcrd->group[1]], md, pcrd->f, 1, f);
391 static double max_pull_distance2(const t_pull_coord *pcrd, const t_pbc *pbc)
396 max_d2 = GMX_DOUBLE_MAX;
398 for (m = 0; m < pbc->ndim_ePBC; m++)
400 if (pcrd->dim[m] != 0)
402 max_d2 = min(max_d2, norm2(pbc->box[m]));
409 static void low_get_pull_coord_dr(const t_pull *pull,
410 const t_pull_coord *pcrd,
411 const t_pbc *pbc, double t,
412 dvec xg, dvec xref, double max_dist2,
415 const t_pull_group *pgrp0, *pgrp1;
417 dvec xrefr, dref = {0, 0, 0};
420 pgrp0 = &pull->group[pcrd->group[0]];
421 pgrp1 = &pull->group[pcrd->group[1]];
423 /* Only the first group can be an absolute reference, in that case nat=0 */
426 for (m = 0; m < DIM; m++)
428 xref[m] = pcrd->origin[m];
432 copy_dvec(xref, xrefr);
434 if (pcrd->eGeom == epullgDIRPBC)
436 for (m = 0; m < DIM; m++)
438 dref[m] = (pcrd->init + pcrd->rate*t)*pcrd->vec[m];
440 /* Add the reference position, so we use the correct periodic image */
441 dvec_inc(xrefr, dref);
444 pbc_dx_d(pbc, xg, xrefr, dr);
446 for (m = 0; m < DIM; m++)
448 dr[m] *= pcrd->dim[m];
451 if (max_dist2 >= 0 && dr2 > 0.98*0.98*max_dist2)
453 gmx_fatal(FARGS, "Distance between pull groups %d and %d (%f nm) is larger than 0.49 times the box size (%f).\nYou might want to consider using \"pull-geometry = direction-periodic\" instead.\n",
454 pcrd->group[0], pcrd->group[1], sqrt(dr2), sqrt(max_dist2));
457 if (pcrd->eGeom == epullgDIRPBC)
463 static void get_pull_coord_dr(const t_pull *pull,
465 const t_pbc *pbc, double t,
469 const t_pull_coord *pcrd;
471 pcrd = &pull->coord[coord_ind];
473 if (pcrd->eGeom == epullgDIRPBC)
479 md2 = max_pull_distance2(pcrd, pbc);
482 low_get_pull_coord_dr(pull, pcrd, pbc, t,
483 pull->group[pcrd->group[1]].x,
484 pcrd->eGeom == epullgCYL ? pull->dyna[coord_ind].x : pull->group[pcrd->group[0]].x,
489 void get_pull_coord_distance(const t_pull *pull,
491 const t_pbc *pbc, double t,
492 dvec dr, double *dev)
494 static gmx_bool bWarned = FALSE; /* TODO: this should be fixed for thread-safety,
495 but is fairly benign */
496 const t_pull_coord *pcrd;
498 double ref, drs, inpr;
500 pcrd = &pull->coord[coord_ind];
502 get_pull_coord_dr(pull, coord_ind, pbc, t, dr);
504 ref = pcrd->init + pcrd->rate*t;
509 /* Pull along the vector between the com's */
510 if (ref < 0 && !bWarned)
512 fprintf(stderr, "\nPull reference distance for coordinate %d is negative (%f)\n", coord_ind+1, ref);
518 /* With no vector we can not determine the direction for the force,
519 * so we set the force to zero.
525 /* Determine the deviation */
534 for (m = 0; m < DIM; m++)
536 inpr += pcrd->vec[m]*dr[m];
543 void clear_pull_forces(t_pull *pull)
547 /* Zeroing the forces is only required for constraint pulling.
548 * It can happen that multiple constraint steps need to be applied
549 * and therefore the constraint forces need to be accumulated.
551 for (i = 0; i < pull->ncoord; i++)
553 clear_dvec(pull->coord[i].f);
554 pull->coord[i].f_scal = 0;
558 /* Apply constraint using SHAKE */
559 static void do_constraint(t_pull *pull, t_pbc *pbc,
561 gmx_bool bMaster, tensor vir,
565 dvec *r_ij; /* x[i] com of i in prev. step. Obeys constr. -> r_ij[i] */
566 dvec unc_ij; /* xp[i] com of i this step, before constr. -> unc_ij */
567 dvec *rnew; /* current 'new' positions of the groups */
568 double *dr_tot; /* the total update of the coords */
572 double lambda, rm, invdt = 0;
573 gmx_bool bConverged_all, bConverged = FALSE;
574 int niter = 0, g, c, ii, j, m, max_iter = 100;
576 dvec f; /* the pull force */
578 t_pull_group *pgrp0, *pgrp1;
581 snew(r_ij, pull->ncoord);
582 snew(dr_tot, pull->ncoord);
584 snew(rnew, pull->ngroup);
586 /* copy the current unconstrained positions for use in iterations. We
587 iterate until rinew[i] and rjnew[j] obey the constraints. Then
588 rinew - pull.x_unc[i] is the correction dr to group i */
589 for (g = 0; g < pull->ngroup; g++)
591 copy_dvec(pull->group[g].xp, rnew[g]);
594 /* Determine the constraint directions from the old positions */
595 for (c = 0; c < pull->ncoord; c++)
597 pcrd = &pull->coord[c];
599 if (pcrd->eType != epullCONSTRAINT)
604 get_pull_coord_dr(pull, c, pbc, t, r_ij[c]);
605 /* Store the difference vector at time t for printing */
606 copy_dvec(r_ij[c], pcrd->dr);
609 fprintf(debug, "Pull coord %d dr %f %f %f\n",
610 c, r_ij[c][XX], r_ij[c][YY], r_ij[c][ZZ]);
613 if (pcrd->eGeom == epullgDIR || pcrd->eGeom == epullgDIRPBC)
615 /* Select the component along vec */
617 for (m = 0; m < DIM; m++)
619 a += pcrd->vec[m]*r_ij[c][m];
621 for (m = 0; m < DIM; m++)
623 r_ij[c][m] = a*pcrd->vec[m];
627 if (dnorm2(r_ij[c]) == 0)
629 gmx_fatal(FARGS, "Distance for pull coordinate %d is zero with constraint pulling, which is not allowed.", c + 1);
633 bConverged_all = FALSE;
634 while (!bConverged_all && niter < max_iter)
636 bConverged_all = TRUE;
638 /* loop over all constraints */
639 for (c = 0; c < pull->ncoord; c++)
643 pcrd = &pull->coord[c];
645 if (pcrd->eType != epullCONSTRAINT)
650 pgrp0 = &pull->group[pcrd->group[0]];
651 pgrp1 = &pull->group[pcrd->group[1]];
653 /* Get the current difference vector */
654 low_get_pull_coord_dr(pull, pcrd, pbc, t,
655 rnew[pcrd->group[1]],
656 rnew[pcrd->group[0]],
659 ref = pcrd->init + pcrd->rate*t;
663 fprintf(debug, "Pull coord %d, iteration %d\n", c, niter);
666 rm = 1.0/(pgrp0->invtm + pgrp1->invtm);
673 gmx_fatal(FARGS, "The pull constraint reference distance for group %d is <= 0 (%f)", c, ref);
677 double q, c_a, c_b, c_c;
679 c_a = diprod(r_ij[c], r_ij[c]);
680 c_b = diprod(unc_ij, r_ij[c])*2;
681 c_c = diprod(unc_ij, unc_ij) - dsqr(ref);
685 q = -0.5*(c_b - sqrt(c_b*c_b - 4*c_a*c_c));
690 q = -0.5*(c_b + sqrt(c_b*c_b - 4*c_a*c_c));
697 "Pull ax^2+bx+c=0: a=%e b=%e c=%e lambda=%e\n",
698 c_a, c_b, c_c, lambda);
702 /* The position corrections dr due to the constraints */
703 dsvmul(-lambda*rm*pgrp1->invtm, r_ij[c], dr1);
704 dsvmul( lambda*rm*pgrp0->invtm, r_ij[c], dr0);
705 dr_tot[c] += -lambda*dnorm(r_ij[c]);
710 /* A 1-dimensional constraint along a vector */
712 for (m = 0; m < DIM; m++)
714 vec[m] = pcrd->vec[m];
715 a += unc_ij[m]*vec[m];
717 /* Select only the component along the vector */
718 dsvmul(a, vec, unc_ij);
722 fprintf(debug, "Pull inpr %e lambda: %e\n", a, lambda);
725 /* The position corrections dr due to the constraints */
726 dsvmul(-lambda*rm*pgrp1->invtm, vec, dr1);
727 dsvmul( lambda*rm*pgrp0->invtm, vec, dr0);
728 dr_tot[c] += -lambda;
739 low_get_pull_coord_dr(pull, pcrd, pbc, t, rnew[g1], rnew[g0], -1, tmp);
740 low_get_pull_coord_dr(pull, pcrd, pbc, t, dr1, dr0, -1, tmp3);
742 "Pull cur %8.5f %8.5f %8.5f j:%8.5f %8.5f %8.5f d: %8.5f\n",
743 rnew[g0][0], rnew[g0][1], rnew[g0][2],
744 rnew[g1][0], rnew[g1][1], rnew[g1][2], dnorm(tmp));
746 "Pull ref %8s %8s %8s %8s %8s %8s d: %8.5f\n",
747 "", "", "", "", "", "", ref);
749 "Pull cor %8.5f %8.5f %8.5f j:%8.5f %8.5f %8.5f d: %8.5f\n",
750 dr0[0], dr0[1], dr0[2],
751 dr1[0], dr1[1], dr1[2],
755 /* Update the COMs with dr */
756 dvec_inc(rnew[pcrd->group[1]], dr1);
757 dvec_inc(rnew[pcrd->group[0]], dr0);
760 /* Check if all constraints are fullfilled now */
761 for (c = 0; c < pull->ncoord; c++)
763 pcrd = &pull->coord[c];
765 if (pcrd->eType != epullCONSTRAINT)
770 ref = pcrd->init + pcrd->rate*t;
772 low_get_pull_coord_dr(pull, pcrd, pbc, t,
773 rnew[pcrd->group[1]],
774 rnew[pcrd->group[0]],
780 bConverged = fabs(dnorm(unc_ij) - ref) < pull->constr_tol;
785 for (m = 0; m < DIM; m++)
787 vec[m] = pcrd->vec[m];
789 inpr = diprod(unc_ij, vec);
790 dsvmul(inpr, vec, unc_ij);
792 fabs(diprod(unc_ij, vec) - ref) < pull->constr_tol;
800 fprintf(debug, "NOT CONVERGED YET: Group %d:"
801 "d_ref = %f, current d = %f\n",
802 g, ref, dnorm(unc_ij));
805 bConverged_all = FALSE;
810 /* if after all constraints are dealt with and bConverged is still TRUE
811 we're finished, if not we do another iteration */
813 if (niter > max_iter)
815 gmx_fatal(FARGS, "Too many iterations for constraint run: %d", niter);
818 /* DONE ITERATING, NOW UPDATE COORDINATES AND CALC. CONSTRAINT FORCES */
825 /* update atoms in the groups */
826 for (g = 0; g < pull->ngroup; g++)
828 const t_pull_group *pgrp;
831 pgrp = &pull->group[g];
833 /* get the final constraint displacement dr for group g */
834 dvec_sub(rnew[g], pgrp->xp, dr);
838 /* No displacement, no update necessary */
842 /* update the atom positions */
844 for (j = 0; j < pgrp->nat_loc; j++)
846 ii = pgrp->ind_loc[j];
847 if (pgrp->weight_loc)
849 dsvmul(pgrp->wscale*pgrp->weight_loc[j], dr, tmp);
851 for (m = 0; m < DIM; m++)
857 for (m = 0; m < DIM; m++)
859 v[ii][m] += invdt*tmp[m];
865 /* calculate the constraint forces, used for output and virial only */
866 for (c = 0; c < pull->ncoord; c++)
868 pcrd = &pull->coord[c];
870 if (pcrd->eType != epullCONSTRAINT)
875 pcrd->f_scal = dr_tot[c]/((pull->group[pcrd->group[0]].invtm + pull->group[pcrd->group[1]].invtm)*dt*dt);
877 if (vir != NULL && pcrd->eGeom != epullgDIRPBC && bMaster)
881 /* Add the pull contribution to the virial */
882 /* We have already checked above that r_ij[c] != 0 */
883 f_invr = pcrd->f_scal/dnorm(r_ij[c]);
885 for (j = 0; j < DIM; j++)
887 for (m = 0; m < DIM; m++)
889 vir[j][m] -= 0.5*f_invr*r_ij[c][j]*r_ij[c][m];
895 /* finished! I hope. Give back some memory */
901 /* Pulling with a harmonic umbrella potential or constant force */
902 static void do_pull_pot(t_pull *pull, t_pbc *pbc, double t, real lambda,
903 real *V, tensor vir, real *dVdl)
906 double dev, ndr, invdr = 0;
910 /* loop over the pull coordinates */
913 for (c = 0; c < pull->ncoord; c++)
915 pcrd = &pull->coord[c];
917 if (pcrd->eType == epullCONSTRAINT)
922 get_pull_coord_distance(pull, c, pbc, t, pcrd->dr, &dev);
924 k = (1.0 - lambda)*pcrd->k + lambda*pcrd->kB;
925 dkdl = pcrd->kB - pcrd->k;
927 if (pcrd->eGeom == epullgDIST)
929 ndr = dnorm(pcrd->dr);
936 /* With an harmonic umbrella, the force is 0 at r=0,
937 * so we can set invdr to any value.
938 * With a constant force, the force at r=0 is not defined,
939 * so we zero it (this is anyhow a very rare event).
947 for (m = 0; m < DIM; m++)
949 ndr += pcrd->vec[m]*pcrd->dr[m];
956 case epullFLATBOTTOM:
957 /* The only difference between an umbrella and a flat-bottom
958 * potential is that a flat-bottom is zero below zero.
960 if (pcrd->eType == epullFLATBOTTOM && dev < 0)
965 pcrd->f_scal = -k*dev;
966 *V += 0.5* k*dsqr(dev);
967 *dVdl += 0.5*dkdl*dsqr(dev);
975 gmx_incons("Unsupported pull type in do_pull_pot");
978 if (pcrd->eGeom == epullgDIST)
980 for (m = 0; m < DIM; m++)
982 pcrd->f[m] = pcrd->f_scal*pcrd->dr[m]*invdr;
987 for (m = 0; m < DIM; m++)
989 pcrd->f[m] = pcrd->f_scal*pcrd->vec[m];
993 if (vir != NULL && pcrd->eGeom != epullgDIRPBC)
995 /* Add the pull contribution to the virial */
996 for (j = 0; j < DIM; j++)
998 for (m = 0; m < DIM; m++)
1000 vir[j][m] -= 0.5*pcrd->f[j]*pcrd->dr[m];
1007 real pull_potential(t_pull *pull, t_mdatoms *md, t_pbc *pbc,
1008 t_commrec *cr, double t, real lambda,
1009 rvec *x, rvec *f, tensor vir, real *dvdlambda)
1013 pull_calc_coms(cr, pull, md, pbc, t, x, NULL);
1015 do_pull_pot(pull, pbc, t, lambda,
1016 &V, MASTER(cr) ? vir : NULL, &dVdl);
1018 /* Distribute forces over pulled groups */
1019 apply_forces(pull, md, f);
1026 return (MASTER(cr) ? V : 0.0);
1029 void pull_constraint(t_pull *pull, t_mdatoms *md, t_pbc *pbc,
1030 t_commrec *cr, double dt, double t,
1031 rvec *x, rvec *xp, rvec *v, tensor vir)
1033 pull_calc_coms(cr, pull, md, pbc, t, x, xp);
1035 do_constraint(pull, pbc, xp, v, MASTER(cr), vir, dt, t);
1038 static void make_local_pull_group(gmx_ga2la_t ga2la,
1039 t_pull_group *pg, int start, int end)
1044 for (i = 0; i < pg->nat; i++)
1049 if (!ga2la_get_home(ga2la, ii, &ii))
1054 if (ii >= start && ii < end)
1056 /* This is a home atom, add it to the local pull group */
1057 if (pg->nat_loc >= pg->nalloc_loc)
1059 pg->nalloc_loc = over_alloc_dd(pg->nat_loc+1);
1060 srenew(pg->ind_loc, pg->nalloc_loc);
1061 if (pg->epgrppbc == epgrppbcCOS || pg->weight)
1063 srenew(pg->weight_loc, pg->nalloc_loc);
1066 pg->ind_loc[pg->nat_loc] = ii;
1069 pg->weight_loc[pg->nat_loc] = pg->weight[i];
1076 void dd_make_local_pull_groups(gmx_domdec_t *dd, t_pull *pull, t_mdatoms *md)
1090 for (g = 0; g < pull->ngroup; g++)
1092 make_local_pull_group(ga2la, &pull->group[g],
1096 /* Since the PBC of atoms might have changed, we need to update the PBC */
1097 pull->bSetPBCatoms = TRUE;
1100 static void init_pull_group_index(FILE *fplog, t_commrec *cr,
1101 int g, t_pull_group *pg,
1102 gmx_bool bConstraint, ivec pulldim_con,
1103 gmx_mtop_t *mtop, t_inputrec *ir, real lambda)
1105 int i, ii, d, nfrozen, ndim;
1107 double tmass, wmass, wwmass;
1108 gmx_groups_t *groups;
1109 gmx_mtop_atomlookup_t alook;
1112 if (EI_ENERGY_MINIMIZATION(ir->eI) || ir->eI == eiBD)
1114 /* There are no masses in the integrator.
1115 * But we still want to have the correct mass-weighted COMs.
1116 * So we store the real masses in the weights.
1117 * We do not set nweight, so these weights do not end up in the tpx file.
1119 if (pg->nweight == 0)
1121 snew(pg->weight, pg->nat);
1130 pg->weight_loc = NULL;
1134 pg->nat_loc = pg->nat;
1135 pg->ind_loc = pg->ind;
1136 if (pg->epgrppbc == epgrppbcCOS)
1138 snew(pg->weight_loc, pg->nat);
1142 pg->weight_loc = pg->weight;
1146 groups = &mtop->groups;
1148 alook = gmx_mtop_atomlookup_init(mtop);
1154 for (i = 0; i < pg->nat; i++)
1157 gmx_mtop_atomnr_to_atom(alook, ii, &atom);
1158 if (bConstraint && ir->opts.nFreeze)
1160 for (d = 0; d < DIM; d++)
1162 if (pulldim_con[d] == 1 &&
1163 ir->opts.nFreeze[ggrpnr(groups, egcFREEZE, ii)][d])
1169 if (ir->efep == efepNO)
1175 m = (1 - lambda)*atom->m + lambda*atom->mB;
1177 if (pg->nweight > 0)
1185 if (EI_ENERGY_MINIMIZATION(ir->eI))
1187 /* Move the mass to the weight */
1192 else if (ir->eI == eiBD)
1196 mbd = ir->bd_fric*ir->delta_t;
1200 if (groups->grpnr[egcTC] == NULL)
1202 mbd = ir->delta_t/ir->opts.tau_t[0];
1206 mbd = ir->delta_t/ir->opts.tau_t[groups->grpnr[egcTC][ii]];
1218 gmx_mtop_atomlookup_destroy(alook);
1222 gmx_fatal(FARGS, "The total%s mass of pull group %d is zero",
1223 pg->weight ? " weighted" : "", g);
1228 "Pull group %d: %5d atoms, mass %9.3f", g, pg->nat, tmass);
1229 if (pg->weight || EI_ENERGY_MINIMIZATION(ir->eI) || ir->eI == eiBD)
1231 fprintf(fplog, ", weighted mass %9.3f", wmass*wmass/wwmass);
1233 if (pg->epgrppbc == epgrppbcCOS)
1235 fprintf(fplog, ", cosine weighting will be used");
1237 fprintf(fplog, "\n");
1242 /* A value != 0 signals not frozen, it is updated later */
1248 for (d = 0; d < DIM; d++)
1250 ndim += pulldim_con[d]*pg->nat;
1252 if (fplog && nfrozen > 0 && nfrozen < ndim)
1255 "\nWARNING: In pull group %d some, but not all of the degrees of freedom\n"
1256 " that are subject to pulling are frozen.\n"
1257 " For constraint pulling the whole group will be frozen.\n\n",
1265 void init_pull(FILE *fplog, t_inputrec *ir, int nfile, const t_filenm fnm[],
1266 gmx_mtop_t *mtop, t_commrec *cr, const output_env_t oenv, real lambda,
1267 gmx_bool bOutFile, unsigned long Flags)
1275 pull->bPotential = FALSE;
1276 pull->bConstraint = FALSE;
1277 pull->bCylinder = FALSE;
1278 for (c = 0; c < pull->ncoord; c++)
1282 pcrd = &pull->coord[c];
1284 if (pcrd->eType == epullCONSTRAINT)
1286 pull->bConstraint = TRUE;
1290 pull->bPotential = TRUE;
1293 if (pcrd->eGeom == epullgCYL)
1295 pull->bCylinder = TRUE;
1297 if (pcrd->eType == epullCONSTRAINT)
1299 gmx_fatal(FARGS, "Pulling of type %s can not be combined with geometry %s. Consider using pull type %s.",
1300 epull_names[pcrd->eType],
1301 epullg_names[pcrd->eGeom],
1302 epull_names[epullUMBRELLA]);
1307 /* We only need to calculate the plain COM of a group
1308 * when it is not only used as a cylinder group.
1310 if (pull->group[pcrd->group[0]].nat > 0)
1312 pull->group[pcrd->group[0]].bCalcCOM = TRUE;
1315 if (pull->group[pcrd->group[1]].nat > 0)
1317 pull->group[pcrd->group[1]].bCalcCOM = TRUE;
1321 pull->ePBC = ir->ePBC;
1324 case epbcNONE: pull->npbcdim = 0; break;
1325 case epbcXY: pull->npbcdim = 2; break;
1326 default: pull->npbcdim = 3; break;
1331 gmx_bool bAbs, bCos;
1334 for (c = 0; c < pull->ncoord; c++)
1336 if (pull->group[pull->coord[c].group[0]].nat == 0 ||
1337 pull->group[pull->coord[c].group[1]].nat == 0)
1343 fprintf(fplog, "\n");
1344 if (pull->bPotential)
1346 fprintf(fplog, "Will apply potential COM pulling\n");
1348 if (pull->bConstraint)
1350 fprintf(fplog, "Will apply constraint COM pulling\n");
1352 fprintf(fplog, "with %d pull coordinate%s and %d group%s\n",
1353 pull->ncoord, pull->ncoord == 1 ? "" : "s",
1354 pull->ngroup, pull->ngroup == 1 ? "" : "s");
1357 fprintf(fplog, "with an absolute reference\n");
1360 for (g = 0; g < pull->ngroup; g++)
1362 if (pull->group[g].nat > 1 &&
1363 pull->group[g].pbcatom < 0)
1365 /* We are using cosine weighting */
1366 fprintf(fplog, "Cosine weighting is used for group %d\n", g);
1372 please_cite(fplog, "Engin2010");
1378 pull->dbuf_cyl = NULL;
1379 pull->bRefAt = FALSE;
1381 for (g = 0; g < pull->ngroup; g++)
1383 pgrp = &pull->group[g];
1384 pgrp->epgrppbc = epgrppbcNONE;
1387 gmx_bool bConstraint;
1388 ivec pulldim, pulldim_con;
1390 /* There is an issue when a group is used in multiple coordinates
1391 * and constraints are applied in different dimensions with atoms
1392 * frozen in some, but not all dimensions.
1393 * Since there is only one mass array per group, we can't have
1394 * frozen/non-frozen atoms for different coords at the same time.
1395 * But since this is a very exotic case, we don't check for this.
1396 * A warning is printed in init_pull_group_index.
1398 bConstraint = FALSE;
1399 clear_ivec(pulldim);
1400 clear_ivec(pulldim_con);
1401 for (c = 0; c < pull->ncoord; c++)
1403 if (pull->coord[c].group[0] == g ||
1404 pull->coord[c].group[1] == g)
1406 for (m = 0; m < DIM; m++)
1408 if (pull->coord[c].dim[m] == 1)
1412 if (pull->coord[c].eType == epullCONSTRAINT)
1422 /* Determine if we need to take PBC into account for calculating
1423 * the COM's of the pull groups.
1425 for (m = 0; m < pull->npbcdim; m++)
1427 if (pulldim[m] == 1 && pgrp->nat > 1)
1429 if (pgrp->pbcatom >= 0)
1431 pgrp->epgrppbc = epgrppbcREFAT;
1432 pull->bRefAt = TRUE;
1438 gmx_fatal(FARGS, "Pull groups can not have relative weights and cosine weighting at same time");
1440 pgrp->epgrppbc = epgrppbcCOS;
1441 if (pull->cosdim >= 0 && pull->cosdim != m)
1443 gmx_fatal(FARGS, "Can only use cosine weighting with pulling in one dimension (use mdp option pull-coord?-dim)");
1449 /* Set the indices */
1450 init_pull_group_index(fplog, cr, g, pgrp,
1451 bConstraint, pulldim_con,
1456 /* Absolute reference, set the inverse mass to zero.
1457 * This is only relevant (and used) with constraint pulling.
1464 /* If we use cylinder coordinates, do some initialising for them */
1465 if (pull->bCylinder)
1467 snew(pull->dyna, pull->ncoord);
1469 for (c = 0; c < pull->ncoord; c++)
1471 const t_pull_coord *pcrd;
1473 pcrd = &pull->coord[c];
1475 if (pcrd->eGeom == epullgCYL)
1477 if (pull->group[pcrd->group[0]].nat == 0)
1479 gmx_fatal(FARGS, "A cylinder pull group is not supported when using absolute reference!\n");
1485 /* We still need to initialize the PBC reference coordinates */
1486 pull->bSetPBCatoms = TRUE;
1488 /* Only do I/O when we are doing dynamics and if we are the MASTER */
1493 if (pull->nstxout > 0)
1495 pull->out_x = open_pull_out(opt2fn("-px", nfile, fnm), pull, oenv,
1498 if (pull->nstfout > 0)
1500 pull->out_f = open_pull_out(opt2fn("-pf", nfile, fnm), pull, oenv,
1506 void finish_pull(t_pull *pull)
1510 gmx_fio_fclose(pull->out_x);
1514 gmx_fio_fclose(pull->out_f);