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"
47 #include "gromacs/fileio/gmxfio.h"
51 #include "gromacs/fileio/filenm.h"
59 #include "mtop_util.h"
61 #include "gmx_ga2la.h"
65 static void pull_print_group_x(FILE *out, ivec dim, const t_pull_group *pgrp)
69 for (m = 0; m < DIM; m++)
73 fprintf(out, "\t%g", pgrp->x[m]);
78 static void pull_print_coord_dr(FILE *out, ivec dim, const t_pull_coord *pcrd)
82 for (m = 0; m < DIM; m++)
86 fprintf(out, "\t%g", pcrd->dr[m]);
91 static void pull_print_x(FILE *out, t_pull *pull, double t)
94 const t_pull_coord *pcrd;
96 fprintf(out, "%.4f", t);
98 for (c = 0; c < pull->ncoord; c++)
100 pcrd = &pull->coord[c];
106 pull_print_group_x(out, pull->dim, &pull->dyna[c]);
110 pull_print_group_x(out, pull->dim, &pull->group[pcrd->group[0]]);
113 pull_print_coord_dr(out, pull->dim, pcrd);
118 static void pull_print_f(FILE *out, t_pull *pull, double t)
122 fprintf(out, "%.4f", t);
124 for (c = 0; c < pull->ncoord; c++)
126 fprintf(out, "\t%g", pull->coord[c].f_scal);
131 void pull_print_output(t_pull *pull, gmx_large_int_t step, double time)
133 if ((pull->nstxout != 0) && (step % pull->nstxout == 0))
135 pull_print_x(pull->out_x, pull, time);
138 if ((pull->nstfout != 0) && (step % pull->nstfout == 0))
140 pull_print_f(pull->out_f, pull, time);
144 static FILE *open_pull_out(const char *fn, t_pull *pull, const output_env_t oenv,
145 gmx_bool bCoord, unsigned long Flags)
149 char **setname, buf[10];
151 if (Flags & MD_APPENDFILES)
153 fp = gmx_fio_fopen(fn, "a+");
157 fp = gmx_fio_fopen(fn, "w+");
160 xvgr_header(fp, "Pull COM", "Time (ps)", "Position (nm)",
165 xvgr_header(fp, "Pull force", "Time (ps)", "Force (kJ/mol/nm)",
169 snew(setname, 2*pull->ncoord*DIM);
171 for (c = 0; c < pull->ncoord; c++)
177 for (m = 0; m < DIM; m++)
181 sprintf(buf, "%d %s%c", c+1, "c", 'X'+m);
182 setname[nsets] = strdup(buf);
187 for (m = 0; m < DIM; m++)
191 sprintf(buf, "%d %s%c", c+1, "d", 'X'+m);
192 setname[nsets] = strdup(buf);
199 sprintf(buf, "%d", c+1);
200 setname[nsets] = strdup(buf);
206 xvgr_legend(fp, nsets, (const char**)setname, oenv);
208 for (c = 0; c < nsets; c++)
218 /* Apply forces in a mass weighted fashion */
219 static void apply_forces_grp(const t_pull_group *pgrp, const t_mdatoms *md,
220 const dvec f_pull, int sign, rvec *f)
222 int i, ii, m, start, end;
223 double wmass, inv_wm;
226 end = md->homenr + start;
228 inv_wm = pgrp->wscale*pgrp->invtm;
230 for (i = 0; i < pgrp->nat_loc; i++)
232 ii = pgrp->ind_loc[i];
233 wmass = md->massT[ii];
234 if (pgrp->weight_loc)
236 wmass *= pgrp->weight_loc[i];
239 for (m = 0; m < DIM; m++)
241 f[ii][m] += sign * wmass * f_pull[m] * inv_wm;
246 /* Apply forces in a mass weighted fashion */
247 static void apply_forces(t_pull * pull, t_mdatoms * md, rvec *f)
250 const t_pull_coord *pcrd;
252 for (c = 0; c < pull->ncoord; c++)
254 pcrd = &pull->coord[c];
258 apply_forces_grp(&pull->dyna[c], md, pcrd->f, -1, f);
262 if (pull->group[pcrd->group[0]].nat > 0)
264 apply_forces_grp(&pull->group[pcrd->group[0]], md, pcrd->f, -1, f);
267 apply_forces_grp(&pull->group[pcrd->group[1]], md, pcrd->f, 1, f);
271 static double max_pull_distance2(const t_pull *pull, const t_pbc *pbc)
276 max_d2 = GMX_DOUBLE_MAX;
278 if (pull->eGeom != epullgDIRPBC)
280 for (m = 0; m < pbc->ndim_ePBC; m++)
282 if (pull->dim[m] != 0)
284 max_d2 = min(max_d2, norm2(pbc->box[m]));
292 static void low_get_pull_coord_dr(const t_pull *pull,
293 const t_pull_coord *pcrd,
294 const t_pbc *pbc, double t,
295 dvec xg, dvec xref, double max_dist2,
298 const t_pull_group *pgrp0, *pgrp1;
300 dvec xrefr, dref = {0, 0, 0};
303 pgrp0 = &pull->group[pcrd->group[0]];
304 pgrp1 = &pull->group[pcrd->group[1]];
306 /* Only the first group can be an absolute reference, in that case nat=0 */
309 for (m = 0; m < DIM; m++)
311 xref[m] = pcrd->origin[m];
315 copy_dvec(xref, xrefr);
317 if (pull->eGeom == epullgDIRPBC)
319 for (m = 0; m < DIM; m++)
321 dref[m] = (pcrd->init + pcrd->rate*t)*pcrd->vec[m];
323 /* Add the reference position, so we use the correct periodic image */
324 dvec_inc(xrefr, dref);
327 pbc_dx_d(pbc, xg, xrefr, dr);
329 for (m = 0; m < DIM; m++)
331 dr[m] *= pull->dim[m];
334 if (max_dist2 >= 0 && dr2 > 0.98*0.98*max_dist2)
336 gmx_fatal(FARGS, "Distance between pull groups %d and %d (%f nm) is larger than 0.49 times the box size (%f)",
337 pcrd->group[0], pcrd->group[1], sqrt(dr2), sqrt(max_dist2));
340 if (pull->eGeom == epullgDIRPBC)
346 static void get_pull_coord_dr(const t_pull *pull,
348 const t_pbc *pbc, double t,
352 const t_pull_coord *pcrd;
354 if (pull->eGeom == epullgDIRPBC)
360 md2 = max_pull_distance2(pull, pbc);
363 pcrd = &pull->coord[coord_ind];
365 low_get_pull_coord_dr(pull, pcrd, pbc, t,
366 pull->group[pcrd->group[1]].x,
367 PULL_CYL(pull) ? pull->dyna[coord_ind].x : pull->group[pcrd->group[0]].x,
372 void get_pull_coord_distance(const t_pull *pull,
374 const t_pbc *pbc, double t,
375 dvec dr, double *dev)
377 static gmx_bool bWarned = FALSE; /* TODO: this should be fixed for thread-safety,
378 but is fairly benign */
379 const t_pull_coord *pcrd;
381 double ref, drs, inpr;
383 pcrd = &pull->coord[coord_ind];
385 get_pull_coord_dr(pull, coord_ind, pbc, t, dr);
387 ref = pcrd->init + pcrd->rate*t;
392 /* Pull along the vector between the com's */
393 if (ref < 0 && !bWarned)
395 fprintf(stderr, "\nPull reference distance for coordinate %d is negative (%f)\n", coord_ind+1, ref);
401 /* With no vector we can not determine the direction for the force,
402 * so we set the force to zero.
408 /* Determine the deviation */
417 for (m = 0; m < DIM; m++)
419 inpr += pcrd->vec[m]*dr[m];
426 void clear_pull_forces(t_pull *pull)
430 /* Zeroing the forces is only required for constraint pulling.
431 * It can happen that multiple constraint steps need to be applied
432 * and therefore the constraint forces need to be accumulated.
434 for (i = 0; i < pull->ncoord; i++)
436 clear_dvec(pull->coord[i].f);
437 pull->coord[i].f_scal = 0;
441 /* Apply constraint using SHAKE */
442 static void do_constraint(t_pull *pull, t_pbc *pbc,
444 gmx_bool bMaster, tensor vir,
448 dvec *r_ij; /* x[i] com of i in prev. step. Obeys constr. -> r_ij[i] */
449 dvec unc_ij; /* xp[i] com of i this step, before constr. -> unc_ij */
450 dvec *rnew; /* current 'new' positions of the groups */
451 double *dr_tot; /* the total update of the coords */
455 double lambda, rm, mass, invdt = 0;
456 gmx_bool bConverged_all, bConverged = FALSE;
457 int niter = 0, g, c, ii, j, m, max_iter = 100;
459 dvec f; /* the pull force */
461 t_pull_group *pdyna, *pgrp0, *pgrp1;
464 snew(r_ij, pull->ncoord);
465 snew(dr_tot, pull->ncoord);
467 snew(rnew, pull->ngroup);
469 /* copy the current unconstrained positions for use in iterations. We
470 iterate until rinew[i] and rjnew[j] obey the constraints. Then
471 rinew - pull.x_unc[i] is the correction dr to group i */
472 for (g = 0; g < pull->ngroup; g++)
474 copy_dvec(pull->group[g].xp, rnew[g]);
478 /* There is only one pull coordinate and reference group */
479 copy_dvec(pull->dyna[0].xp, rnew[pull->coord[0].group[0]]);
482 /* Determine the constraint directions from the old positions */
483 for (c = 0; c < pull->ncoord; c++)
485 get_pull_coord_dr(pull, c, pbc, t, r_ij[c]);
486 /* Store the difference vector at time t for printing */
487 copy_dvec(r_ij[c], pull->coord[c].dr);
490 fprintf(debug, "Pull coord %d dr %f %f %f\n",
491 c, r_ij[c][XX], r_ij[c][YY], r_ij[c][ZZ]);
494 if (pull->eGeom == epullgDIR || pull->eGeom == epullgDIRPBC)
496 /* Select the component along vec */
498 for (m = 0; m < DIM; m++)
500 a += pull->coord[c].vec[m]*r_ij[c][m];
502 for (m = 0; m < DIM; m++)
504 r_ij[c][m] = a*pull->coord[c].vec[m];
509 bConverged_all = FALSE;
510 while (!bConverged_all && niter < max_iter)
512 bConverged_all = TRUE;
514 /* loop over all constraints */
515 for (c = 0; c < pull->ncoord; c++)
519 pcrd = &pull->coord[c];
520 pgrp0 = &pull->group[pcrd->group[0]];
521 pgrp1 = &pull->group[pcrd->group[1]];
523 /* Get the current difference vector */
524 low_get_pull_coord_dr(pull, pcrd, pbc, t,
525 rnew[pcrd->group[1]],
526 rnew[pcrd->group[0]],
529 ref = pcrd->init + pcrd->rate*t;
533 fprintf(debug, "Pull coord %d, iteration %d\n", c, niter);
536 rm = 1.0/(pgrp0->invtm + pgrp1->invtm);
543 gmx_fatal(FARGS, "The pull constraint reference distance for group %d is <= 0 (%f)", c, ref);
547 double q, c_a, c_b, c_c;
549 c_a = diprod(r_ij[c], r_ij[c]);
550 c_b = diprod(unc_ij, r_ij[c])*2;
551 c_c = diprod(unc_ij, unc_ij) - dsqr(ref);
555 q = -0.5*(c_b - sqrt(c_b*c_b - 4*c_a*c_c));
560 q = -0.5*(c_b + sqrt(c_b*c_b - 4*c_a*c_c));
567 "Pull ax^2+bx+c=0: a=%e b=%e c=%e lambda=%e\n",
568 c_a, c_b, c_c, lambda);
572 /* The position corrections dr due to the constraints */
573 dsvmul(-lambda*rm*pgrp1->invtm, r_ij[c], dr1);
574 dsvmul( lambda*rm*pgrp0->invtm, r_ij[c], dr0);
575 dr_tot[c] += -lambda*dnorm(r_ij[c]);
580 /* A 1-dimensional constraint along a vector */
582 for (m = 0; m < DIM; m++)
584 vec[m] = pcrd->vec[m];
585 a += unc_ij[m]*vec[m];
587 /* Select only the component along the vector */
588 dsvmul(a, vec, unc_ij);
592 fprintf(debug, "Pull inpr %e lambda: %e\n", a, lambda);
595 /* The position corrections dr due to the constraints */
596 dsvmul(-lambda*rm*pgrp1->invtm, vec, dr1);
597 dsvmul( lambda*rm*pgrp0->invtm, vec, dr0);
598 dr_tot[c] += -lambda;
609 low_get_pull_coord_dr(pull, pcrd, pbc, t, rnew[g1], rnew[g0], -1, tmp);
610 low_get_pull_coord_dr(pull, pcrd, pbc, t, dr1, dr0, -1, tmp3);
612 "Pull cur %8.5f %8.5f %8.5f j:%8.5f %8.5f %8.5f d: %8.5f\n",
613 rnew[g0][0], rnew[g0][1], rnew[g0][2],
614 rnew[g1][0], rnew[g1][1], rnew[g1][2], dnorm(tmp));
616 "Pull ref %8s %8s %8s %8s %8s %8s d: %8.5f\n",
617 "", "", "", "", "", "", ref);
619 "Pull cor %8.5f %8.5f %8.5f j:%8.5f %8.5f %8.5f d: %8.5f\n",
620 dr0[0], dr0[1], dr0[2],
621 dr1[0], dr1[1], dr1[2],
625 /* Update the COMs with dr */
626 dvec_inc(rnew[pcrd->group[1]], dr1);
627 dvec_inc(rnew[pcrd->group[0]], dr0);
630 /* Check if all constraints are fullfilled now */
631 for (c = 0; c < pull->ncoord; c++)
633 pcrd = &pull->coord[c];
635 low_get_pull_coord_dr(pull, pcrd, pbc, t,
636 rnew[pcrd->group[1]],
637 rnew[pcrd->group[0]],
643 bConverged = fabs(dnorm(unc_ij) - ref) < pull->constr_tol;
648 for (m = 0; m < DIM; m++)
650 vec[m] = pcrd->vec[m];
652 inpr = diprod(unc_ij, vec);
653 dsvmul(inpr, vec, unc_ij);
655 fabs(diprod(unc_ij, vec) - ref) < pull->constr_tol;
663 fprintf(debug, "NOT CONVERGED YET: Group %d:"
664 "d_ref = %f, current d = %f\n",
665 g, ref, dnorm(unc_ij));
668 bConverged_all = FALSE;
673 /* if after all constraints are dealt with and bConverged is still TRUE
674 we're finished, if not we do another iteration */
676 if (niter > max_iter)
678 gmx_fatal(FARGS, "Too many iterations for constraint run: %d", niter);
681 /* DONE ITERATING, NOW UPDATE COORDINATES AND CALC. CONSTRAINT FORCES */
688 /* update atoms in the groups */
689 for (g = 0; g < pull->ngroup; g++)
691 const t_pull_group *pgrp;
694 if (PULL_CYL(pull) && g == pull->coord[0].group[0])
696 pgrp = &pull->dyna[0];
700 pgrp = &pull->group[g];
703 /* get the final constraint displacement dr for group g */
704 dvec_sub(rnew[g], pgrp->xp, dr);
705 /* select components of dr */
706 for (m = 0; m < DIM; m++)
708 dr[m] *= pull->dim[m];
711 /* update the atom positions */
713 for (j = 0; j < pgrp->nat_loc; j++)
715 ii = pgrp->ind_loc[j];
716 if (pgrp->weight_loc)
718 dsvmul(pgrp->wscale*pgrp->weight_loc[j], dr, tmp);
720 for (m = 0; m < DIM; m++)
726 for (m = 0; m < DIM; m++)
728 v[ii][m] += invdt*tmp[m];
734 /* calculate the constraint forces, used for output and virial only */
735 for (c = 0; c < pull->ncoord; c++)
737 pcrd = &pull->coord[c];
738 pcrd->f_scal = dr_tot[c]/((pull->group[pcrd->group[0]].invtm + pull->group[pcrd->group[1]].invtm)*dt*dt);
744 /* Add the pull contribution to the virial */
745 f_invr = pcrd->f_scal/dnorm(r_ij[c]);
747 for (j = 0; j < DIM; j++)
749 for (m = 0; m < DIM; m++)
751 vir[j][m] -= 0.5*f_invr*r_ij[c][j]*r_ij[c][m];
757 /* finished! I hope. Give back some memory */
763 /* Pulling with a harmonic umbrella potential or constant force */
764 static void do_pull_pot(int ePull,
765 t_pull *pull, t_pbc *pbc, double t, real lambda,
766 real *V, tensor vir, real *dVdl)
769 double dev, ndr, invdr;
773 /* loop over the pull coordinates */
776 for (c = 0; c < pull->ncoord; c++)
778 pcrd = &pull->coord[c];
780 get_pull_coord_distance(pull, c, pbc, t, pcrd->dr, &dev);
782 k = (1.0 - lambda)*pcrd->k + lambda*pcrd->kB;
783 dkdl = pcrd->kB - pcrd->k;
788 ndr = dnorm(pcrd->dr);
790 if (ePull == epullUMBRELLA)
792 pcrd->f_scal = -k*dev;
793 *V += 0.5* k*dsqr(dev);
794 *dVdl += 0.5*dkdl*dsqr(dev);
802 for (m = 0; m < DIM; m++)
804 pcrd->f[m] = pcrd->f_scal*pcrd->dr[m]*invdr;
810 if (ePull == epullUMBRELLA)
812 pcrd->f_scal = -k*dev;
813 *V += 0.5* k*dsqr(dev);
814 *dVdl += 0.5*dkdl*dsqr(dev);
819 for (m = 0; m < DIM; m++)
821 ndr += pcrd->vec[m]*pcrd->dr[m];
827 for (m = 0; m < DIM; m++)
829 pcrd->f[m] = pcrd->f_scal*pcrd->vec[m];
836 /* Add the pull contribution to the virial */
837 for (j = 0; j < DIM; j++)
839 for (m = 0; m < DIM; m++)
841 vir[j][m] -= 0.5*pcrd->f[j]*pcrd->dr[m];
848 real pull_potential(int ePull, t_pull *pull, t_mdatoms *md, t_pbc *pbc,
849 t_commrec *cr, double t, real lambda,
850 rvec *x, rvec *f, tensor vir, real *dvdlambda)
854 pull_calc_coms(cr, pull, md, pbc, t, x, NULL);
856 do_pull_pot(ePull, pull, pbc, t, lambda,
857 &V, pull->bVirial && MASTER(cr) ? vir : NULL, &dVdl);
859 /* Distribute forces over pulled groups */
860 apply_forces(pull, md, f);
867 return (MASTER(cr) ? V : 0.0);
870 void pull_constraint(t_pull *pull, t_mdatoms *md, t_pbc *pbc,
871 t_commrec *cr, double dt, double t,
872 rvec *x, rvec *xp, rvec *v, tensor vir)
874 pull_calc_coms(cr, pull, md, pbc, t, x, xp);
876 do_constraint(pull, pbc, xp, v, pull->bVirial && MASTER(cr), vir, dt, t);
879 static void make_local_pull_group(gmx_ga2la_t ga2la,
880 t_pull_group *pg, int start, int end)
885 for (i = 0; i < pg->nat; i++)
890 if (!ga2la_get_home(ga2la, ii, &ii))
895 if (ii >= start && ii < end)
897 /* This is a home atom, add it to the local pull group */
898 if (pg->nat_loc >= pg->nalloc_loc)
900 pg->nalloc_loc = over_alloc_dd(pg->nat_loc+1);
901 srenew(pg->ind_loc, pg->nalloc_loc);
902 if (pg->epgrppbc == epgrppbcCOS || pg->weight)
904 srenew(pg->weight_loc, pg->nalloc_loc);
907 pg->ind_loc[pg->nat_loc] = ii;
910 pg->weight_loc[pg->nat_loc] = pg->weight[i];
917 void dd_make_local_pull_groups(gmx_domdec_t *dd, t_pull *pull, t_mdatoms *md)
931 for (g = 0; g < pull->ngroup; g++)
933 make_local_pull_group(ga2la, &pull->group[g],
934 md->start, md->start+md->homenr);
938 static void init_pull_group_index(FILE *fplog, t_commrec *cr,
940 int g, t_pull_group *pg, ivec pulldims,
941 gmx_mtop_t *mtop, t_inputrec *ir, real lambda)
943 int i, ii, d, nfrozen, ndim;
945 double tmass, wmass, wwmass;
947 gmx_ga2la_t ga2la = NULL;
948 gmx_groups_t *groups;
949 gmx_mtop_atomlookup_t alook;
952 bDomDec = (cr && DOMAINDECOMP(cr));
955 ga2la = cr->dd->ga2la;
958 if (EI_ENERGY_MINIMIZATION(ir->eI) || ir->eI == eiBD)
960 /* There are no masses in the integrator.
961 * But we still want to have the correct mass-weighted COMs.
962 * So we store the real masses in the weights.
963 * We do not set nweight, so these weights do not end up in the tpx file.
965 if (pg->nweight == 0)
967 snew(pg->weight, pg->nat);
976 pg->weight_loc = NULL;
980 pg->nat_loc = pg->nat;
981 pg->ind_loc = pg->ind;
982 if (pg->epgrppbc == epgrppbcCOS)
984 snew(pg->weight_loc, pg->nat);
988 pg->weight_loc = pg->weight;
992 groups = &mtop->groups;
994 alook = gmx_mtop_atomlookup_init(mtop);
1000 for (i = 0; i < pg->nat; i++)
1003 gmx_mtop_atomnr_to_atom(alook, ii, &atom);
1004 if (cr && PAR(cr) && !bDomDec && ii >= start && ii < end)
1006 pg->ind_loc[pg->nat_loc++] = ii;
1008 if (ir->opts.nFreeze)
1010 for (d = 0; d < DIM; d++)
1012 if (pulldims[d] && ir->opts.nFreeze[ggrpnr(groups, egcFREEZE, ii)][d])
1018 if (ir->efep == efepNO)
1024 m = (1 - lambda)*atom->m + lambda*atom->mB;
1026 if (pg->nweight > 0)
1034 if (EI_ENERGY_MINIMIZATION(ir->eI))
1036 /* Move the mass to the weight */
1041 else if (ir->eI == eiBD)
1045 mbd = ir->bd_fric*ir->delta_t;
1049 if (groups->grpnr[egcTC] == NULL)
1051 mbd = ir->delta_t/ir->opts.tau_t[0];
1055 mbd = ir->delta_t/ir->opts.tau_t[groups->grpnr[egcTC][ii]];
1067 gmx_mtop_atomlookup_destroy(alook);
1071 gmx_fatal(FARGS, "The total%s mass of pull group %d is zero",
1072 pg->weight ? " weighted" : "", g);
1077 "Pull group %d: %5d atoms, mass %9.3f", g, pg->nat, tmass);
1078 if (pg->weight || EI_ENERGY_MINIMIZATION(ir->eI) || ir->eI == eiBD)
1080 fprintf(fplog, ", weighted mass %9.3f", wmass*wmass/wwmass);
1082 if (pg->epgrppbc == epgrppbcCOS)
1084 fprintf(fplog, ", cosine weighting will be used");
1086 fprintf(fplog, "\n");
1091 /* A value > 0 signals not frozen, it is updated later */
1097 for (d = 0; d < DIM; d++)
1099 ndim += pulldims[d]*pg->nat;
1101 if (fplog && nfrozen > 0 && nfrozen < ndim)
1104 "\nWARNING: In pull group %d some, but not all of the degrees of freedom\n"
1105 " that are subject to pulling are frozen.\n"
1106 " For pulling the whole group will be frozen.\n\n",
1114 void init_pull(FILE *fplog, t_inputrec *ir, int nfile, const t_filenm fnm[],
1115 gmx_mtop_t *mtop, t_commrec *cr, const output_env_t oenv, real lambda,
1116 gmx_bool bOutFile, unsigned long Flags)
1120 int c, g, start = 0, end = 0, m;
1124 pull->ePBC = ir->ePBC;
1127 case epbcNONE: pull->npbcdim = 0; break;
1128 case epbcXY: pull->npbcdim = 2; break;
1129 default: pull->npbcdim = 3; break;
1134 gmx_bool bAbs, bCos;
1137 for (c = 0; c < pull->ncoord; c++)
1139 if (pull->group[pull->coord[c].group[0]].nat == 0 ||
1140 pull->group[pull->coord[c].group[1]].nat == 0)
1146 fprintf(fplog, "\nWill apply %s COM pulling in geometry '%s'\n",
1147 EPULLTYPE(ir->ePull), EPULLGEOM(pull->eGeom));
1148 fprintf(fplog, "with %d pull coordinate%s and %d group%s\n",
1149 pull->ncoord, pull->ncoord == 1 ? "" : "s",
1150 pull->ngroup, pull->ngroup == 1 ? "" : "s");
1153 fprintf(fplog, "with an absolute reference\n");
1156 for (g = 0; g < pull->ngroup; g++)
1158 if (pull->group[g].nat > 1 &&
1159 pull->group[g].pbcatom < 0)
1161 /* We are using cosine weighting */
1162 fprintf(fplog, "Cosine weighting is used for group %d\n", g);
1168 please_cite(fplog, "Engin2010");
1172 /* We always add the virial contribution,
1173 * except for geometry = direction_periodic where this is impossible.
1175 pull->bVirial = (pull->eGeom != epullgDIRPBC);
1176 if (getenv("GMX_NO_PULLVIR") != NULL)
1180 fprintf(fplog, "Found env. var., will not add the virial contribution of the COM pull forces\n");
1182 pull->bVirial = FALSE;
1185 if (cr && PARTDECOMP(cr))
1187 pd_at_range(cr, &start, &end);
1191 pull->dbuf_cyl = NULL;
1192 pull->bRefAt = FALSE;
1194 for (g = 0; g < pull->ngroup; g++)
1196 pgrp = &pull->group[g];
1197 pgrp->epgrppbc = epgrppbcNONE;
1200 /* Determine if we need to take PBC into account for calculating
1201 * the COM's of the pull groups.
1203 for (m = 0; m < pull->npbcdim; m++)
1205 if (pull->dim[m] && pgrp->nat > 1)
1207 if (pgrp->pbcatom >= 0)
1209 pgrp->epgrppbc = epgrppbcREFAT;
1210 pull->bRefAt = TRUE;
1216 gmx_fatal(FARGS, "Pull groups can not have relative weights and cosine weighting at same time");
1218 pgrp->epgrppbc = epgrppbcCOS;
1219 if (pull->cosdim >= 0 && pull->cosdim != m)
1221 gmx_fatal(FARGS, "Can only use cosine weighting with pulling in one dimension (use mdp option pull_dim)");
1227 /* Set the indices */
1228 init_pull_group_index(fplog, cr, start, end, g, pgrp, pull->dim, mtop, ir, lambda);
1229 if (PULL_CYL(pull) && pgrp->invtm == 0)
1231 gmx_fatal(FARGS, "Can not have frozen atoms in a cylinder pull group");
1236 /* Absolute reference, set the inverse mass to zero */
1242 /* if we use dynamic reference groups, do some initialising for them */
1245 if (ir->ePull == epullCONSTRAINT && pull->ncoord > 1)
1247 /* We can't easily update the single reference group with multiple
1248 * constraints. This would require recalculating COMs.
1250 gmx_fatal(FARGS, "Constraint COM pulling supports only one coordinate with geometry=cylinder, you can use umbrella pulling with multiple coordinates");
1253 for (c = 0; c < pull->ncoord; c++)
1255 if (pull->group[pull->coord[c].group[0]].nat == 0)
1257 gmx_fatal(FARGS, "Dynamic reference groups are not supported when using absolute reference!\n");
1261 snew(pull->dyna, pull->ncoord);
1264 /* Only do I/O when we are doing dynamics and if we are the MASTER */
1269 if (pull->nstxout > 0)
1271 pull->out_x = open_pull_out(opt2fn("-px", nfile, fnm), pull, oenv, TRUE, Flags);
1273 if (pull->nstfout > 0)
1275 pull->out_f = open_pull_out(opt2fn("-pf", nfile, fnm), pull, oenv,
1281 void finish_pull(t_pull *pull)
1285 gmx_fio_fclose(pull->out_x);
1289 gmx_fio_fclose(pull->out_f);