1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROwing Monsters And Cloning Shrimps
44 #include "gromacs/fileio/futil.h"
46 #include "gromacs/fileio/gmxfio.h"
50 #include "gromacs/fileio/filenm.h"
58 #include "mtop_util.h"
60 #include "gmx_ga2la.h"
64 static void pull_print_group_x(FILE *out, ivec dim, const t_pull_group *pgrp)
68 for (m = 0; m < DIM; m++)
72 fprintf(out, "\t%g", pgrp->x[m]);
77 static void pull_print_coord_dr(FILE *out, ivec dim, const t_pull_coord *pcrd)
81 for (m = 0; m < DIM; m++)
85 fprintf(out, "\t%g", pcrd->dr[m]);
90 static void pull_print_x(FILE *out, t_pull *pull, double t)
93 const t_pull_coord *pcrd;
95 fprintf(out, "%.4f", t);
97 for (c = 0; c < pull->ncoord; c++)
99 pcrd = &pull->coord[c];
105 pull_print_group_x(out, pull->dim, &pull->dyna[c]);
109 pull_print_group_x(out, pull->dim, &pull->group[pcrd->group[0]]);
112 pull_print_coord_dr(out, pull->dim, pcrd);
117 static void pull_print_f(FILE *out, t_pull *pull, double t)
121 fprintf(out, "%.4f", t);
123 for (c = 0; c < pull->ncoord; c++)
125 fprintf(out, "\t%g", pull->coord[c].f_scal);
130 void pull_print_output(t_pull *pull, gmx_int64_t step, double time)
132 if ((pull->nstxout != 0) && (step % pull->nstxout == 0))
134 pull_print_x(pull->out_x, pull, time);
137 if ((pull->nstfout != 0) && (step % pull->nstfout == 0))
139 pull_print_f(pull->out_f, pull, time);
143 static FILE *open_pull_out(const char *fn, t_pull *pull, const output_env_t oenv,
144 gmx_bool bCoord, unsigned long Flags)
148 char **setname, buf[10];
150 if (Flags & MD_APPENDFILES)
152 fp = gmx_fio_fopen(fn, "a+");
156 fp = gmx_fio_fopen(fn, "w+");
159 xvgr_header(fp, "Pull COM", "Time (ps)", "Position (nm)",
164 xvgr_header(fp, "Pull force", "Time (ps)", "Force (kJ/mol/nm)",
168 snew(setname, 2*pull->ncoord*DIM);
170 for (c = 0; c < pull->ncoord; c++)
176 for (m = 0; m < DIM; m++)
180 sprintf(buf, "%d %s%c", c+1, "c", 'X'+m);
181 setname[nsets] = strdup(buf);
186 for (m = 0; m < DIM; m++)
190 sprintf(buf, "%d %s%c", c+1, "d", 'X'+m);
191 setname[nsets] = strdup(buf);
198 sprintf(buf, "%d", c+1);
199 setname[nsets] = strdup(buf);
205 xvgr_legend(fp, nsets, (const char**)setname, oenv);
207 for (c = 0; c < nsets; c++)
217 /* Apply forces in a mass weighted fashion */
218 static void apply_forces_grp(const t_pull_group *pgrp, const t_mdatoms *md,
219 const dvec f_pull, int sign, rvec *f)
221 int i, ii, m, start, end;
222 double wmass, inv_wm;
225 end = md->homenr + start;
227 inv_wm = pgrp->wscale*pgrp->invtm;
229 for (i = 0; i < pgrp->nat_loc; i++)
231 ii = pgrp->ind_loc[i];
232 wmass = md->massT[ii];
233 if (pgrp->weight_loc)
235 wmass *= pgrp->weight_loc[i];
238 for (m = 0; m < DIM; m++)
240 f[ii][m] += sign * wmass * f_pull[m] * inv_wm;
245 /* Apply forces in a mass weighted fashion */
246 static void apply_forces(t_pull * pull, t_mdatoms * md, rvec *f)
249 const t_pull_coord *pcrd;
251 for (c = 0; c < pull->ncoord; c++)
253 pcrd = &pull->coord[c];
257 apply_forces_grp(&pull->dyna[c], md, pcrd->f, -1, f);
261 if (pull->group[pcrd->group[0]].nat > 0)
263 apply_forces_grp(&pull->group[pcrd->group[0]], md, pcrd->f, -1, f);
266 apply_forces_grp(&pull->group[pcrd->group[1]], md, pcrd->f, 1, f);
270 static double max_pull_distance2(const t_pull *pull, const t_pbc *pbc)
275 max_d2 = GMX_DOUBLE_MAX;
277 if (pull->eGeom != epullgDIRPBC)
279 for (m = 0; m < pbc->ndim_ePBC; m++)
281 if (pull->dim[m] != 0)
283 max_d2 = min(max_d2, norm2(pbc->box[m]));
291 static void low_get_pull_coord_dr(const t_pull *pull,
292 const t_pull_coord *pcrd,
293 const t_pbc *pbc, double t,
294 dvec xg, dvec xref, double max_dist2,
297 const t_pull_group *pgrp0, *pgrp1;
299 dvec xrefr, dref = {0, 0, 0};
302 pgrp0 = &pull->group[pcrd->group[0]];
303 pgrp1 = &pull->group[pcrd->group[1]];
305 /* Only the first group can be an absolute reference, in that case nat=0 */
308 for (m = 0; m < DIM; m++)
310 xref[m] = pcrd->origin[m];
314 copy_dvec(xref, xrefr);
316 if (pull->eGeom == epullgDIRPBC)
318 for (m = 0; m < DIM; m++)
320 dref[m] = (pcrd->init + pcrd->rate*t)*pcrd->vec[m];
322 /* Add the reference position, so we use the correct periodic image */
323 dvec_inc(xrefr, dref);
326 pbc_dx_d(pbc, xg, xrefr, dr);
328 for (m = 0; m < DIM; m++)
330 dr[m] *= pull->dim[m];
333 if (max_dist2 >= 0 && dr2 > 0.98*0.98*max_dist2)
335 gmx_fatal(FARGS, "Distance between pull groups %d and %d (%f nm) is larger than 0.49 times the box size (%f)",
336 pcrd->group[0], pcrd->group[1], sqrt(dr2), sqrt(max_dist2));
339 if (pull->eGeom == epullgDIRPBC)
345 static void get_pull_coord_dr(const t_pull *pull,
347 const t_pbc *pbc, double t,
351 const t_pull_coord *pcrd;
353 if (pull->eGeom == epullgDIRPBC)
359 md2 = max_pull_distance2(pull, pbc);
362 pcrd = &pull->coord[coord_ind];
364 low_get_pull_coord_dr(pull, pcrd, pbc, t,
365 pull->group[pcrd->group[1]].x,
366 PULL_CYL(pull) ? pull->dyna[coord_ind].x : pull->group[pcrd->group[0]].x,
371 void get_pull_coord_distance(const t_pull *pull,
373 const t_pbc *pbc, double t,
374 dvec dr, double *dev)
376 static gmx_bool bWarned = FALSE; /* TODO: this should be fixed for thread-safety,
377 but is fairly benign */
378 const t_pull_coord *pcrd;
380 double ref, drs, inpr;
382 pcrd = &pull->coord[coord_ind];
384 get_pull_coord_dr(pull, coord_ind, pbc, t, dr);
386 ref = pcrd->init + pcrd->rate*t;
391 /* Pull along the vector between the com's */
392 if (ref < 0 && !bWarned)
394 fprintf(stderr, "\nPull reference distance for coordinate %d is negative (%f)\n", coord_ind+1, ref);
400 /* With no vector we can not determine the direction for the force,
401 * so we set the force to zero.
407 /* Determine the deviation */
416 for (m = 0; m < DIM; m++)
418 inpr += pcrd->vec[m]*dr[m];
425 void clear_pull_forces(t_pull *pull)
429 /* Zeroing the forces is only required for constraint pulling.
430 * It can happen that multiple constraint steps need to be applied
431 * and therefore the constraint forces need to be accumulated.
433 for (i = 0; i < pull->ncoord; i++)
435 clear_dvec(pull->coord[i].f);
436 pull->coord[i].f_scal = 0;
440 /* Apply constraint using SHAKE */
441 static void do_constraint(t_pull *pull, t_pbc *pbc,
443 gmx_bool bMaster, tensor vir,
447 dvec *r_ij; /* x[i] com of i in prev. step. Obeys constr. -> r_ij[i] */
448 dvec unc_ij; /* xp[i] com of i this step, before constr. -> unc_ij */
449 dvec *rnew; /* current 'new' positions of the groups */
450 double *dr_tot; /* the total update of the coords */
454 double lambda, rm, mass, invdt = 0;
455 gmx_bool bConverged_all, bConverged = FALSE;
456 int niter = 0, g, c, ii, j, m, max_iter = 100;
458 dvec f; /* the pull force */
460 t_pull_group *pdyna, *pgrp0, *pgrp1;
463 snew(r_ij, pull->ncoord);
464 snew(dr_tot, pull->ncoord);
466 snew(rnew, pull->ngroup);
468 /* copy the current unconstrained positions for use in iterations. We
469 iterate until rinew[i] and rjnew[j] obey the constraints. Then
470 rinew - pull.x_unc[i] is the correction dr to group i */
471 for (g = 0; g < pull->ngroup; g++)
473 copy_dvec(pull->group[g].xp, rnew[g]);
477 /* There is only one pull coordinate and reference group */
478 copy_dvec(pull->dyna[0].xp, rnew[pull->coord[0].group[0]]);
481 /* Determine the constraint directions from the old positions */
482 for (c = 0; c < pull->ncoord; c++)
484 get_pull_coord_dr(pull, c, pbc, t, r_ij[c]);
485 /* Store the difference vector at time t for printing */
486 copy_dvec(r_ij[c], pull->coord[c].dr);
489 fprintf(debug, "Pull coord %d dr %f %f %f\n",
490 c, r_ij[c][XX], r_ij[c][YY], r_ij[c][ZZ]);
493 if (pull->eGeom == epullgDIR || pull->eGeom == epullgDIRPBC)
495 /* Select the component along vec */
497 for (m = 0; m < DIM; m++)
499 a += pull->coord[c].vec[m]*r_ij[c][m];
501 for (m = 0; m < DIM; m++)
503 r_ij[c][m] = a*pull->coord[c].vec[m];
508 bConverged_all = FALSE;
509 while (!bConverged_all && niter < max_iter)
511 bConverged_all = TRUE;
513 /* loop over all constraints */
514 for (c = 0; c < pull->ncoord; c++)
518 pcrd = &pull->coord[c];
519 pgrp0 = &pull->group[pcrd->group[0]];
520 pgrp1 = &pull->group[pcrd->group[1]];
522 /* Get the current difference vector */
523 low_get_pull_coord_dr(pull, pcrd, pbc, t,
524 rnew[pcrd->group[1]],
525 rnew[pcrd->group[0]],
528 ref = pcrd->init + pcrd->rate*t;
532 fprintf(debug, "Pull coord %d, iteration %d\n", c, niter);
535 rm = 1.0/(pgrp0->invtm + pgrp1->invtm);
542 gmx_fatal(FARGS, "The pull constraint reference distance for group %d is <= 0 (%f)", c, ref);
546 double q, c_a, c_b, c_c;
548 c_a = diprod(r_ij[c], r_ij[c]);
549 c_b = diprod(unc_ij, r_ij[c])*2;
550 c_c = diprod(unc_ij, unc_ij) - dsqr(ref);
554 q = -0.5*(c_b - sqrt(c_b*c_b - 4*c_a*c_c));
559 q = -0.5*(c_b + sqrt(c_b*c_b - 4*c_a*c_c));
566 "Pull ax^2+bx+c=0: a=%e b=%e c=%e lambda=%e\n",
567 c_a, c_b, c_c, lambda);
571 /* The position corrections dr due to the constraints */
572 dsvmul(-lambda*rm*pgrp1->invtm, r_ij[c], dr1);
573 dsvmul( lambda*rm*pgrp0->invtm, r_ij[c], dr0);
574 dr_tot[c] += -lambda*dnorm(r_ij[c]);
579 /* A 1-dimensional constraint along a vector */
581 for (m = 0; m < DIM; m++)
583 vec[m] = pcrd->vec[m];
584 a += unc_ij[m]*vec[m];
586 /* Select only the component along the vector */
587 dsvmul(a, vec, unc_ij);
591 fprintf(debug, "Pull inpr %e lambda: %e\n", a, lambda);
594 /* The position corrections dr due to the constraints */
595 dsvmul(-lambda*rm*pgrp1->invtm, vec, dr1);
596 dsvmul( lambda*rm*pgrp0->invtm, vec, dr0);
597 dr_tot[c] += -lambda;
608 low_get_pull_coord_dr(pull, pcrd, pbc, t, rnew[g1], rnew[g0], -1, tmp);
609 low_get_pull_coord_dr(pull, pcrd, pbc, t, dr1, dr0, -1, tmp3);
611 "Pull cur %8.5f %8.5f %8.5f j:%8.5f %8.5f %8.5f d: %8.5f\n",
612 rnew[g0][0], rnew[g0][1], rnew[g0][2],
613 rnew[g1][0], rnew[g1][1], rnew[g1][2], dnorm(tmp));
615 "Pull ref %8s %8s %8s %8s %8s %8s d: %8.5f\n",
616 "", "", "", "", "", "", ref);
618 "Pull cor %8.5f %8.5f %8.5f j:%8.5f %8.5f %8.5f d: %8.5f\n",
619 dr0[0], dr0[1], dr0[2],
620 dr1[0], dr1[1], dr1[2],
624 /* Update the COMs with dr */
625 dvec_inc(rnew[pcrd->group[1]], dr1);
626 dvec_inc(rnew[pcrd->group[0]], dr0);
629 /* Check if all constraints are fullfilled now */
630 for (c = 0; c < pull->ncoord; c++)
632 pcrd = &pull->coord[c];
634 low_get_pull_coord_dr(pull, pcrd, pbc, t,
635 rnew[pcrd->group[1]],
636 rnew[pcrd->group[0]],
642 bConverged = fabs(dnorm(unc_ij) - ref) < pull->constr_tol;
647 for (m = 0; m < DIM; m++)
649 vec[m] = pcrd->vec[m];
651 inpr = diprod(unc_ij, vec);
652 dsvmul(inpr, vec, unc_ij);
654 fabs(diprod(unc_ij, vec) - ref) < pull->constr_tol;
662 fprintf(debug, "NOT CONVERGED YET: Group %d:"
663 "d_ref = %f, current d = %f\n",
664 g, ref, dnorm(unc_ij));
667 bConverged_all = FALSE;
672 /* if after all constraints are dealt with and bConverged is still TRUE
673 we're finished, if not we do another iteration */
675 if (niter > max_iter)
677 gmx_fatal(FARGS, "Too many iterations for constraint run: %d", niter);
680 /* DONE ITERATING, NOW UPDATE COORDINATES AND CALC. CONSTRAINT FORCES */
687 /* update atoms in the groups */
688 for (g = 0; g < pull->ngroup; g++)
690 const t_pull_group *pgrp;
693 if (PULL_CYL(pull) && g == pull->coord[0].group[0])
695 pgrp = &pull->dyna[0];
699 pgrp = &pull->group[g];
702 /* get the final constraint displacement dr for group g */
703 dvec_sub(rnew[g], pgrp->xp, dr);
704 /* select components of dr */
705 for (m = 0; m < DIM; m++)
707 dr[m] *= pull->dim[m];
710 /* update the atom positions */
712 for (j = 0; j < pgrp->nat_loc; j++)
714 ii = pgrp->ind_loc[j];
715 if (pgrp->weight_loc)
717 dsvmul(pgrp->wscale*pgrp->weight_loc[j], dr, tmp);
719 for (m = 0; m < DIM; m++)
725 for (m = 0; m < DIM; m++)
727 v[ii][m] += invdt*tmp[m];
733 /* calculate the constraint forces, used for output and virial only */
734 for (c = 0; c < pull->ncoord; c++)
736 pcrd = &pull->coord[c];
737 pcrd->f_scal = dr_tot[c]/((pull->group[pcrd->group[0]].invtm + pull->group[pcrd->group[1]].invtm)*dt*dt);
743 /* Add the pull contribution to the virial */
744 f_invr = pcrd->f_scal/dnorm(r_ij[c]);
746 for (j = 0; j < DIM; j++)
748 for (m = 0; m < DIM; m++)
750 vir[j][m] -= 0.5*f_invr*r_ij[c][j]*r_ij[c][m];
756 /* finished! I hope. Give back some memory */
762 /* Pulling with a harmonic umbrella potential or constant force */
763 static void do_pull_pot(int ePull,
764 t_pull *pull, t_pbc *pbc, double t, real lambda,
765 real *V, tensor vir, real *dVdl)
768 double dev, ndr, invdr;
772 /* loop over the pull coordinates */
775 for (c = 0; c < pull->ncoord; c++)
777 pcrd = &pull->coord[c];
779 get_pull_coord_distance(pull, c, pbc, t, pcrd->dr, &dev);
781 k = (1.0 - lambda)*pcrd->k + lambda*pcrd->kB;
782 dkdl = pcrd->kB - pcrd->k;
787 ndr = dnorm(pcrd->dr);
789 if (ePull == epullUMBRELLA)
791 pcrd->f_scal = -k*dev;
792 *V += 0.5* k*dsqr(dev);
793 *dVdl += 0.5*dkdl*dsqr(dev);
801 for (m = 0; m < DIM; m++)
803 pcrd->f[m] = pcrd->f_scal*pcrd->dr[m]*invdr;
809 if (ePull == epullUMBRELLA)
811 pcrd->f_scal = -k*dev;
812 *V += 0.5* k*dsqr(dev);
813 *dVdl += 0.5*dkdl*dsqr(dev);
818 for (m = 0; m < DIM; m++)
820 ndr += pcrd->vec[m]*pcrd->dr[m];
826 for (m = 0; m < DIM; m++)
828 pcrd->f[m] = pcrd->f_scal*pcrd->vec[m];
835 /* Add the pull contribution to the virial */
836 for (j = 0; j < DIM; j++)
838 for (m = 0; m < DIM; m++)
840 vir[j][m] -= 0.5*pcrd->f[j]*pcrd->dr[m];
847 real pull_potential(int ePull, t_pull *pull, t_mdatoms *md, t_pbc *pbc,
848 t_commrec *cr, double t, real lambda,
849 rvec *x, rvec *f, tensor vir, real *dvdlambda)
853 pull_calc_coms(cr, pull, md, pbc, t, x, NULL);
855 do_pull_pot(ePull, pull, pbc, t, lambda,
856 &V, pull->bVirial && MASTER(cr) ? vir : NULL, &dVdl);
858 /* Distribute forces over pulled groups */
859 apply_forces(pull, md, f);
866 return (MASTER(cr) ? V : 0.0);
869 void pull_constraint(t_pull *pull, t_mdatoms *md, t_pbc *pbc,
870 t_commrec *cr, double dt, double t,
871 rvec *x, rvec *xp, rvec *v, tensor vir)
873 pull_calc_coms(cr, pull, md, pbc, t, x, xp);
875 do_constraint(pull, pbc, xp, v, pull->bVirial && MASTER(cr), vir, dt, t);
878 static void make_local_pull_group(gmx_ga2la_t ga2la,
879 t_pull_group *pg, int start, int end)
884 for (i = 0; i < pg->nat; i++)
889 if (!ga2la_get_home(ga2la, ii, &ii))
894 if (ii >= start && ii < end)
896 /* This is a home atom, add it to the local pull group */
897 if (pg->nat_loc >= pg->nalloc_loc)
899 pg->nalloc_loc = over_alloc_dd(pg->nat_loc+1);
900 srenew(pg->ind_loc, pg->nalloc_loc);
901 if (pg->epgrppbc == epgrppbcCOS || pg->weight)
903 srenew(pg->weight_loc, pg->nalloc_loc);
906 pg->ind_loc[pg->nat_loc] = ii;
909 pg->weight_loc[pg->nat_loc] = pg->weight[i];
916 void dd_make_local_pull_groups(gmx_domdec_t *dd, t_pull *pull, t_mdatoms *md)
930 for (g = 0; g < pull->ngroup; g++)
932 make_local_pull_group(ga2la, &pull->group[g],
933 md->start, md->start+md->homenr);
937 static void init_pull_group_index(FILE *fplog, t_commrec *cr,
939 int g, t_pull_group *pg, ivec pulldims,
940 gmx_mtop_t *mtop, t_inputrec *ir, real lambda)
942 int i, ii, d, nfrozen, ndim;
944 double tmass, wmass, wwmass;
946 gmx_ga2la_t ga2la = NULL;
947 gmx_groups_t *groups;
948 gmx_mtop_atomlookup_t alook;
951 bDomDec = (cr && DOMAINDECOMP(cr));
954 ga2la = cr->dd->ga2la;
957 if (EI_ENERGY_MINIMIZATION(ir->eI) || ir->eI == eiBD)
959 /* There are no masses in the integrator.
960 * But we still want to have the correct mass-weighted COMs.
961 * So we store the real masses in the weights.
962 * We do not set nweight, so these weights do not end up in the tpx file.
964 if (pg->nweight == 0)
966 snew(pg->weight, pg->nat);
975 pg->weight_loc = NULL;
979 pg->nat_loc = pg->nat;
980 pg->ind_loc = pg->ind;
981 if (pg->epgrppbc == epgrppbcCOS)
983 snew(pg->weight_loc, pg->nat);
987 pg->weight_loc = pg->weight;
991 groups = &mtop->groups;
993 alook = gmx_mtop_atomlookup_init(mtop);
999 for (i = 0; i < pg->nat; i++)
1002 gmx_mtop_atomnr_to_atom(alook, ii, &atom);
1003 if (cr && PAR(cr) && !bDomDec && ii >= start && ii < end)
1005 pg->ind_loc[pg->nat_loc++] = ii;
1007 if (ir->opts.nFreeze)
1009 for (d = 0; d < DIM; d++)
1011 if (pulldims[d] && ir->opts.nFreeze[ggrpnr(groups, egcFREEZE, ii)][d])
1017 if (ir->efep == efepNO)
1023 m = (1 - lambda)*atom->m + lambda*atom->mB;
1025 if (pg->nweight > 0)
1033 if (EI_ENERGY_MINIMIZATION(ir->eI))
1035 /* Move the mass to the weight */
1040 else if (ir->eI == eiBD)
1044 mbd = ir->bd_fric*ir->delta_t;
1048 if (groups->grpnr[egcTC] == NULL)
1050 mbd = ir->delta_t/ir->opts.tau_t[0];
1054 mbd = ir->delta_t/ir->opts.tau_t[groups->grpnr[egcTC][ii]];
1066 gmx_mtop_atomlookup_destroy(alook);
1070 gmx_fatal(FARGS, "The total%s mass of pull group %d is zero",
1071 pg->weight ? " weighted" : "", g);
1076 "Pull group %d: %5d atoms, mass %9.3f", g, pg->nat, tmass);
1077 if (pg->weight || EI_ENERGY_MINIMIZATION(ir->eI) || ir->eI == eiBD)
1079 fprintf(fplog, ", weighted mass %9.3f", wmass*wmass/wwmass);
1081 if (pg->epgrppbc == epgrppbcCOS)
1083 fprintf(fplog, ", cosine weighting will be used");
1085 fprintf(fplog, "\n");
1090 /* A value > 0 signals not frozen, it is updated later */
1096 for (d = 0; d < DIM; d++)
1098 ndim += pulldims[d]*pg->nat;
1100 if (fplog && nfrozen > 0 && nfrozen < ndim)
1103 "\nWARNING: In pull group %d some, but not all of the degrees of freedom\n"
1104 " that are subject to pulling are frozen.\n"
1105 " For pulling the whole group will be frozen.\n\n",
1113 void init_pull(FILE *fplog, t_inputrec *ir, int nfile, const t_filenm fnm[],
1114 gmx_mtop_t *mtop, t_commrec *cr, const output_env_t oenv, real lambda,
1115 gmx_bool bOutFile, unsigned long Flags)
1119 int c, g, start = 0, end = 0, m;
1123 pull->ePBC = ir->ePBC;
1126 case epbcNONE: pull->npbcdim = 0; break;
1127 case epbcXY: pull->npbcdim = 2; break;
1128 default: pull->npbcdim = 3; break;
1133 gmx_bool bAbs, bCos;
1136 for (c = 0; c < pull->ncoord; c++)
1138 if (pull->group[pull->coord[c].group[0]].nat == 0 ||
1139 pull->group[pull->coord[c].group[1]].nat == 0)
1145 fprintf(fplog, "\nWill apply %s COM pulling in geometry '%s'\n",
1146 EPULLTYPE(ir->ePull), EPULLGEOM(pull->eGeom));
1147 fprintf(fplog, "with %d pull coordinate%s and %d group%s\n",
1148 pull->ncoord, pull->ncoord == 1 ? "" : "s",
1149 pull->ngroup, pull->ngroup == 1 ? "" : "s");
1152 fprintf(fplog, "with an absolute reference\n");
1155 for (g = 0; g < pull->ngroup; g++)
1157 if (pull->group[g].nat > 1 &&
1158 pull->group[g].pbcatom < 0)
1160 /* We are using cosine weighting */
1161 fprintf(fplog, "Cosine weighting is used for group %d\n", g);
1167 please_cite(fplog, "Engin2010");
1171 /* We always add the virial contribution,
1172 * except for geometry = direction_periodic where this is impossible.
1174 pull->bVirial = (pull->eGeom != epullgDIRPBC);
1175 if (getenv("GMX_NO_PULLVIR") != NULL)
1179 fprintf(fplog, "Found env. var., will not add the virial contribution of the COM pull forces\n");
1181 pull->bVirial = FALSE;
1184 if (cr && PARTDECOMP(cr))
1186 pd_at_range(cr, &start, &end);
1190 pull->dbuf_cyl = NULL;
1191 pull->bRefAt = FALSE;
1193 for (g = 0; g < pull->ngroup; g++)
1195 pgrp = &pull->group[g];
1196 pgrp->epgrppbc = epgrppbcNONE;
1199 /* Determine if we need to take PBC into account for calculating
1200 * the COM's of the pull groups.
1202 for (m = 0; m < pull->npbcdim; m++)
1204 if (pull->dim[m] && pgrp->nat > 1)
1206 if (pgrp->pbcatom >= 0)
1208 pgrp->epgrppbc = epgrppbcREFAT;
1209 pull->bRefAt = TRUE;
1215 gmx_fatal(FARGS, "Pull groups can not have relative weights and cosine weighting at same time");
1217 pgrp->epgrppbc = epgrppbcCOS;
1218 if (pull->cosdim >= 0 && pull->cosdim != m)
1220 gmx_fatal(FARGS, "Can only use cosine weighting with pulling in one dimension (use mdp option pull_dim)");
1226 /* Set the indices */
1227 init_pull_group_index(fplog, cr, start, end, g, pgrp, pull->dim, mtop, ir, lambda);
1228 if (PULL_CYL(pull) && pgrp->invtm == 0)
1230 gmx_fatal(FARGS, "Can not have frozen atoms in a cylinder pull group");
1235 /* Absolute reference, set the inverse mass to zero */
1241 /* if we use dynamic reference groups, do some initialising for them */
1244 if (ir->ePull == epullCONSTRAINT && pull->ncoord > 1)
1246 /* We can't easily update the single reference group with multiple
1247 * constraints. This would require recalculating COMs.
1249 gmx_fatal(FARGS, "Constraint COM pulling supports only one coordinate with geometry=cylinder, you can use umbrella pulling with multiple coordinates");
1252 for (c = 0; c < pull->ncoord; c++)
1254 if (pull->group[pull->coord[c].group[0]].nat == 0)
1256 gmx_fatal(FARGS, "Dynamic reference groups are not supported when using absolute reference!\n");
1260 snew(pull->dyna, pull->ncoord);
1263 /* Only do I/O when we are doing dynamics and if we are the MASTER */
1268 if (pull->nstxout > 0)
1270 pull->out_x = open_pull_out(opt2fn("-px", nfile, fnm), pull, oenv, TRUE, Flags);
1272 if (pull->nstfout > 0)
1274 pull->out_f = open_pull_out(opt2fn("-pf", nfile, fnm), pull, oenv,
1280 void finish_pull(t_pull *pull)
1284 gmx_fio_fclose(pull->out_x);
1288 gmx_fio_fclose(pull->out_f);