Added pull coordinate geometry angle-axis
[gromacs.git] / src / gromacs / gmxpreprocess / readpull.cpp
blob15da010e0277db427c761f0a89165bc1db8db33b
1 /*
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,2016, 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.
37 #include "gmxpre.h"
39 #include <assert.h>
40 #include <stdlib.h>
41 #include <string.h>
43 #include "gromacs/fileio/readinp.h"
44 #include "gromacs/gmxpreprocess/readir.h"
45 #include "gromacs/math/vec.h"
46 #include "gromacs/mdlib/mdatoms.h"
47 #include "gromacs/mdtypes/inputrec.h"
48 #include "gromacs/mdtypes/md_enums.h"
49 #include "gromacs/mdtypes/pull-params.h"
50 #include "gromacs/pbcutil/pbc.h"
51 #include "gromacs/pulling/pull.h"
52 #include "gromacs/topology/topology.h"
53 #include "gromacs/utility/cstringutil.h"
54 #include "gromacs/utility/fatalerror.h"
55 #include "gromacs/utility/futil.h"
56 #include "gromacs/utility/smalloc.h"
59 static void string2dvec(const char buf[], dvec nums)
61 double dum;
63 if (sscanf(buf, "%lf%lf%lf%lf", &nums[0], &nums[1], &nums[2], &dum) != 3)
65 gmx_fatal(FARGS, "Expected three numbers at input line %s", buf);
69 static void init_pull_group(t_pull_group *pg,
70 const char *wbuf)
72 double d;
73 int n;
75 pg->nweight = 0;
76 while (sscanf(wbuf, "%lf %n", &d, &n) == 1)
78 if (pg->nweight % 100 == 0)
80 srenew(pg->weight, pg->nweight+100);
82 pg->weight[pg->nweight++] = d;
83 wbuf += n;
87 static void process_pull_dim(char *dim_buf, ivec dim, const t_pull_coord *pcrd)
89 int ndim, d, nchar;
90 char *ptr, pulldim1[STRLEN];
92 ptr = dim_buf;
93 ndim = 0;
94 for (d = 0; d < DIM; d++)
96 if (sscanf(ptr, "%s%n", pulldim1, &nchar) != 1)
98 gmx_fatal(FARGS, "Less than 3 pull dimensions given in pull_dim: '%s'",
99 dim_buf);
102 if (gmx_strncasecmp(pulldim1, "N", 1) == 0)
104 dim[d] = 0;
106 else if (gmx_strncasecmp(pulldim1, "Y", 1) == 0)
108 dim[d] = 1;
109 ndim++;
111 else
113 gmx_fatal(FARGS, "Please use Y(ES) or N(O) for pull_dim only (not %s)",
114 pulldim1);
116 ptr += nchar;
118 if (ndim == 0)
120 gmx_fatal(FARGS, "All entries in pull dim are N");
122 if ((pcrd->eGeom == epullgDIHEDRAL) && (ndim < 3))
124 gmx_fatal(FARGS, "Pull geometry dihedral is only useful with pull-dim = Y Y Y");
126 if ((pcrd->eGeom == epullgANGLE || pcrd->eGeom == epullgANGLEAXIS ) && (ndim < 2))
128 gmx_fatal(FARGS, "Pull geometry %s is only useful with pull-dim = Y for at least 2 dimensions",
129 EPULLGEOM(pcrd->eGeom));
133 static void init_pull_coord(t_pull_coord *pcrd,
134 char *dim_buf,
135 const char *origin_buf, const char *vec_buf,
136 warninp_t wi)
138 int m;
139 dvec origin, vec;
140 char buf[STRLEN];
142 if (pcrd->eType == epullCONSTRAINT && (pcrd->eGeom == epullgCYL ||
143 pcrd->eGeom == epullgDIRRELATIVE ||
144 pcrd->eGeom == epullgANGLE ||
145 pcrd->eGeom == epullgANGLEAXIS ||
146 pcrd->eGeom == epullgDIHEDRAL))
148 gmx_fatal(FARGS, "Pulling of type %s can not be combined with geometry %s. Consider using pull type %s.",
149 epull_names[pcrd->eType],
150 epullg_names[pcrd->eGeom],
151 epull_names[epullUMBRELLA]);
154 process_pull_dim(dim_buf, pcrd->dim, pcrd);
156 string2dvec(origin_buf, origin);
157 if (pcrd->group[0] != 0 && dnorm(origin) > 0)
159 gmx_fatal(FARGS, "The pull origin can only be set with an absolute reference");
162 /* Check the given initial reference value and warn for dangerous values */
163 if (pcrd->eGeom == epullgDIST)
165 if (pcrd->bStart && pcrd->init < 0)
167 sprintf(buf, "The initial reference distance set by pull-coord-init is set to a negative value (%g) with geometry %s while distances need to be non-negative. "
168 "This may work, since you have set pull-coord-start to 'yes' which modifies this value, but only for certain starting distances. "
169 "If this is a mistake you may want to use geometry %s instead.",
170 pcrd->init, EPULLGEOM(pcrd->eGeom), EPULLGEOM(epullgDIR));
171 warning(wi, buf);
174 else if (pcrd->eGeom == epullgANGLE || pcrd->eGeom == epullgANGLEAXIS)
176 if (pcrd->bStart && (pcrd->init < 0 || pcrd->init > 180))
178 /* This value of pcrd->init may be ok depending on pcrd->bStart which modifies pcrd->init later on */
179 sprintf(buf, "The initial reference angle set by pull-coord-init (%g) is outside of the allowed range [0, 180] degrees for geometry (%s). "
180 "This may work, since you have set pull-coord-start to 'yes' which modifies this value, but only for certain starting angles.",
181 pcrd->init, EPULLGEOM(pcrd->eGeom));
182 warning(wi, buf);
185 else if (pcrd->eGeom == epullgDIHEDRAL)
187 if (pcrd->bStart && (pcrd->init < -180 || pcrd->init > 180))
189 sprintf(buf, "The initial reference angle set by pull-coord-init (%g) is outside of the allowed range [-180, 180] degrees for geometry (%s). "
190 "This may work, since you have set pull-coord-start to 'yes' which modifies this value, but only for certain starting angles.",
191 pcrd->init, EPULLGEOM(pcrd->eGeom));
192 warning(wi, buf);
196 /* Check and set the pull vector */
197 clear_dvec(vec);
198 string2dvec(vec_buf, vec);
200 if (pcrd->eGeom == epullgDIR || pcrd->eGeom == epullgCYL || pcrd->eGeom == epullgDIRPBC || pcrd->eGeom == epullgANGLEAXIS)
202 if (dnorm2(vec) == 0)
204 gmx_fatal(FARGS, "With pull geometry %s the pull vector can not be 0,0,0",
205 epullg_names[pcrd->eGeom]);
207 for (int d = 0; d < DIM; d++)
209 if (vec[d] != 0 && pcrd->dim[d] == 0)
211 gmx_fatal(FARGS, "pull-coord-vec has non-zero %c-component while pull_dim for the %c-dimension is set to N", 'x'+d, 'x'+d);
215 /* Normalize the direction vector */
216 dsvmul(1/dnorm(vec), vec, vec);
218 else /* This case is for are all the geometries where the pull vector is not used */
220 if (dnorm2(vec) > 0)
222 sprintf(buf, "A pull vector is given (%g %g %g) but will not be used with geometry %s. If you really want to use this "
223 "vector, consider using geometry %s instead.",
224 vec[0], vec[1], vec[2], EPULLGEOM(pcrd->eGeom),
225 pcrd->eGeom == epullgANGLE ? EPULLGEOM(epullgANGLEAXIS) : EPULLGEOM(epullgDIR));
226 warning(wi, buf);
229 for (m = 0; m < DIM; m++)
231 pcrd->origin[m] = origin[m];
232 pcrd->vec[m] = vec[m];
236 char **read_pullparams(int *ninp_p, t_inpfile **inp_p,
237 pull_params_t *pull,
238 warninp_t wi)
240 int ninp, i, nscan, idum;
241 t_inpfile *inp;
242 const char *tmp;
243 char **grpbuf;
244 char buf[STRLEN], groups[STRLEN], dim_buf[STRLEN];
245 char wbuf[STRLEN], origin_buf[STRLEN], vec_buf[STRLEN];
247 t_pull_group *pgrp;
248 t_pull_coord *pcrd;
250 ninp = *ninp_p;
251 inp = *inp_p;
253 /* read pull parameters */
254 CTYPE("Cylinder radius for dynamic reaction force groups (nm)");
255 RTYPE("pull-cylinder-r", pull->cylinder_r, 1.5);
256 RTYPE("pull-constr-tol", pull->constr_tol, 1E-6);
257 EETYPE("pull-print-com", pull->bPrintCOM, yesno_names);
258 EETYPE("pull-print-ref-value", pull->bPrintRefValue, yesno_names);
259 EETYPE("pull-print-components", pull->bPrintComp, yesno_names);
260 ITYPE("pull-nstxout", pull->nstxout, 50);
261 ITYPE("pull-nstfout", pull->nstfout, 50);
262 CTYPE("Number of pull groups");
263 ITYPE("pull-ngroups", pull->ngroup, 1);
264 CTYPE("Number of pull coordinates");
265 ITYPE("pull-ncoords", pull->ncoord, 1);
267 if (pull->ngroup < 1)
269 gmx_fatal(FARGS, "pull-ngroups should be >= 1");
271 /* We always add an absolute reference group (index 0), even if not used */
272 pull->ngroup += 1;
274 if (pull->ncoord < 1)
276 gmx_fatal(FARGS, "pull-ncoords should be >= 1");
279 snew(pull->group, pull->ngroup);
281 snew(pull->coord, pull->ncoord);
283 /* pull group options */
284 CTYPE("Group and coordinate parameters");
286 /* Read the pull groups */
287 snew(grpbuf, pull->ngroup);
288 /* Group 0 is the absolute reference, we don't read anything for 0 */
289 for (i = 1; i < pull->ngroup; i++)
291 pgrp = &pull->group[i];
292 snew(grpbuf[i], STRLEN);
293 sprintf(buf, "pull-group%d-name", i);
294 STYPE(buf, grpbuf[i], "");
295 sprintf(buf, "pull-group%d-weights", i);
296 STYPE(buf, wbuf, "");
297 sprintf(buf, "pull-group%d-pbcatom", i);
298 ITYPE(buf, pgrp->pbcatom, 0);
300 /* Initialize the pull group */
301 init_pull_group(pgrp, wbuf);
304 /* Read the pull coordinates */
305 for (i = 1; i < pull->ncoord + 1; i++)
307 pcrd = &pull->coord[i-1];
308 sprintf(buf, "pull-coord%d-type", i);
309 EETYPE(buf, pcrd->eType, epull_names);
310 sprintf(buf, "pull-coord%d-geometry", i);
311 EETYPE(buf, pcrd->eGeom, epullg_names);
312 sprintf(buf, "pull-coord%d-groups", i);
313 STYPE(buf, groups, "");
315 switch (pcrd->eGeom)
317 case epullgDIHEDRAL:
318 pcrd->ngroup = 6; break;
319 case epullgDIRRELATIVE:
320 case epullgANGLE:
321 pcrd->ngroup = 4; break;
322 default:
323 pcrd->ngroup = 2; break;
326 nscan = sscanf(groups, "%d %d %d %d %d %d %d",
327 &pcrd->group[0], &pcrd->group[1], &pcrd->group[2], &pcrd->group[3],
328 &pcrd->group[4], &pcrd->group[5], &idum);
329 if (nscan != pcrd->ngroup)
331 sprintf(wbuf, "%s should contain %d pull group indices with geometry %s",
332 buf, pcrd->ngroup, epullg_names[pcrd->eGeom]);
333 set_warning_line(wi, NULL, -1);
334 warning_error(wi, wbuf);
337 sprintf(buf, "pull-coord%d-dim", i);
338 STYPE(buf, dim_buf, "Y Y Y");
339 sprintf(buf, "pull-coord%d-origin", i);
340 STYPE(buf, origin_buf, "0.0 0.0 0.0");
341 sprintf(buf, "pull-coord%d-vec", i);
342 STYPE(buf, vec_buf, "0.0 0.0 0.0");
343 sprintf(buf, "pull-coord%d-start", i);
344 EETYPE(buf, pcrd->bStart, yesno_names);
345 sprintf(buf, "pull-coord%d-init", i);
346 RTYPE(buf, pcrd->init, 0.0);
347 sprintf(buf, "pull-coord%d-rate", i);
348 RTYPE(buf, pcrd->rate, 0.0);
349 sprintf(buf, "pull-coord%d-k", i);
350 RTYPE(buf, pcrd->k, 0.0);
351 sprintf(buf, "pull-coord%d-kB", i);
352 RTYPE(buf, pcrd->kB, pcrd->k);
354 /* Initialize the pull coordinate */
355 init_pull_coord(pcrd, dim_buf, origin_buf, vec_buf, wi);
358 *ninp_p = ninp;
359 *inp_p = inp;
361 return grpbuf;
364 void make_pull_groups(pull_params_t *pull,
365 char **pgnames,
366 const t_blocka *grps, char **gnames)
368 int g, ig = -1, i;
369 t_pull_group *pgrp;
371 /* Absolute reference group (might not be used) is special */
372 pgrp = &pull->group[0];
373 pgrp->nat = 0;
374 pgrp->pbcatom = -1;
376 for (g = 1; g < pull->ngroup; g++)
378 pgrp = &pull->group[g];
380 if (strcmp(pgnames[g], "") == 0)
382 gmx_fatal(FARGS, "Pull option pull_group%d required by grompp has not been set.", g);
385 ig = search_string(pgnames[g], grps->nr, gnames);
386 pgrp->nat = grps->index[ig+1] - grps->index[ig];
388 fprintf(stderr, "Pull group %d '%s' has %d atoms\n",
389 g, pgnames[g], pgrp->nat);
391 if (pgrp->nat == 0)
393 gmx_fatal(FARGS, "Pull group %d '%s' is empty", g, pgnames[g]);
396 snew(pgrp->ind, pgrp->nat);
397 for (i = 0; i < pgrp->nat; i++)
399 pgrp->ind[i] = grps->a[grps->index[ig]+i];
402 if (pgrp->nweight > 0 && pgrp->nweight != pgrp->nat)
404 gmx_fatal(FARGS, "Number of weights (%d) for pull group %d '%s' does not match the number of atoms (%d)",
405 pgrp->nweight, g, pgnames[g], pgrp->nat);
408 if (pgrp->nat == 1)
410 /* No pbc is required for this group */
411 pgrp->pbcatom = -1;
413 else
415 if (pgrp->pbcatom > 0)
417 pgrp->pbcatom -= 1;
419 else if (pgrp->pbcatom == 0)
421 pgrp->pbcatom = pgrp->ind[(pgrp->nat-1)/2];
423 else
425 /* Use cosine weighting */
426 pgrp->pbcatom = -1;
432 void make_pull_coords(pull_params_t *pull)
434 int c;
435 t_pull_coord *pcrd;
437 for (c = 0; c < pull->ncoord; c++)
439 pcrd = &pull->coord[c];
441 if (pcrd->group[0] < 0 || pcrd->group[0] >= pull->ngroup ||
442 pcrd->group[1] < 0 || pcrd->group[1] >= pull->ngroup)
444 gmx_fatal(FARGS, "Pull group index in pull-coord%d-groups out of range, should be between %d and %d", c+1, 0, pull->ngroup+1);
447 if (pcrd->group[0] == pcrd->group[1])
449 gmx_fatal(FARGS, "Identical pull group indices in pull-coord%d-groups", c+1);
452 if (pcrd->eGeom == epullgCYL)
454 if (pull->group[pcrd->group[0]].nweight > 0)
456 gmx_fatal(FARGS, "Weights are not supported for the reference group with cylinder pulling");
462 void set_pull_init(t_inputrec *ir, gmx_mtop_t *mtop, rvec *x, matrix box, real lambda,
463 const gmx_output_env_t *oenv)
465 pull_params_t *pull;
466 struct pull_t *pull_work;
467 t_mdatoms *md;
468 t_pbc pbc;
469 int c;
470 double t_start;
472 pull = ir->pull;
473 pull_work = init_pull(NULL, pull, ir, 0, NULL, mtop, NULL, oenv, lambda, FALSE, 0);
474 md = init_mdatoms(NULL, mtop, ir->efep);
475 atoms2md(mtop, ir, 0, NULL, mtop->natoms, md);
476 if (ir->efep)
478 update_mdatoms(md, lambda);
481 set_pbc(&pbc, ir->ePBC, box);
483 t_start = ir->init_t + ir->init_step*ir->delta_t;
485 pull_calc_coms(NULL, pull_work, md, &pbc, t_start, x, NULL);
487 fprintf(stderr, "Pull group natoms pbc atom distance at start reference at t=0\n");
488 for (c = 0; c < pull->ncoord; c++)
490 t_pull_coord *pcrd;
491 t_pull_group *pgrp0, *pgrp1;
492 double value;
493 real init = 0;
495 pcrd = &pull->coord[c];
497 pgrp0 = &pull->group[pcrd->group[0]];
498 pgrp1 = &pull->group[pcrd->group[1]];
499 fprintf(stderr, "%8d %8d %8d\n",
500 pcrd->group[0], pgrp0->nat, pgrp0->pbcatom+1);
501 fprintf(stderr, "%8d %8d %8d ",
502 pcrd->group[1], pgrp1->nat, pgrp1->pbcatom+1);
504 if (pcrd->bStart)
506 init = pcrd->init;
507 pcrd->init = 0;
510 get_pull_coord_value(pull_work, c, &pbc, &value);
512 value *= pull_conversion_factor_internal2userinput(pcrd);
513 fprintf(stderr, " %10.3f %s", value, pull_coordinate_units(pcrd));
515 if (pcrd->bStart)
517 pcrd->init = value + init;
520 if (pcrd->eGeom == epullgDIST)
522 if (pcrd->init < 0)
524 gmx_fatal(FARGS, "The initial pull distance (%g) needs to be non-negative with geometry %s. If you want a signed distance, use geometry %s instead.",
525 pcrd->init, EPULLGEOM(pcrd->eGeom), EPULLGEOM(epullgDIR));
528 /* TODO: With a positive init but a negative rate things could still
529 * go wrong, but it might be fine if you don't pull too far.
530 * We should give a warning or note when there is only one pull dim
531 * active, since that is usually the problematic case when you should
532 * be using direction. We will do this later, since an already planned
533 * generalization of the pull code makes pull dim available here.
536 else if (pcrd->eGeom == epullgANGLE || pcrd->eGeom == epullgANGLEAXIS)
538 if (pcrd->init < 0 || pcrd->init > 180)
540 gmx_fatal(FARGS, "The initial pull reference angle (%g) is outside of the allowed range [0, 180] degrees.", pcrd->init);
543 else if (pcrd->eGeom == epullgDIHEDRAL)
545 if (pcrd->init < -180 || pcrd->init > 180)
547 gmx_fatal(FARGS, "The initial pull reference angle (%g) is outside of the allowed range [-180, 180] degrees.",
548 pcrd->init);
553 fprintf(stderr, " %10.3f %s\n", pcrd->init, pull_coordinate_units(pcrd));
556 finish_pull(pull_work);