Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / readpull.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014, 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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <string.h>
42 #include <stdlib.h>
43
44 #include "gromacs/utility/cstringutil.h"
45 #include "gromacs/utility/futil.h"
46 #include "gromacs/math/vec.h"
47 #include "gromacs/utility/smalloc.h"
48 #include "typedefs.h"
49 #include "names.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "macros.h"
52 #include "readinp.h"
53 #include "readir.h"
54 #include "mdatoms.h"
55 #include "gromacs/pbcutil/pbc.h"
56 #include "gromacs/pulling/pull.h"
57
58
59 static char pulldim[STRLEN];
60
61 static void string2dvec(const char buf[], dvec nums)
62 {
63     double dum;
64
65     if (sscanf(buf, "%lf%lf%lf%lf", &nums[0], &nums[1], &nums[2], &dum) != 3)
66     {
67         gmx_fatal(FARGS, "Expected three numbers at input line %s", buf);
68     }
69 }
70
71 static void init_pull_group(t_pull_group *pg,
72                             const char   *wbuf)
73 {
74     double d;
75     int    n, m;
76
77     pg->nweight = 0;
78     while (sscanf(wbuf, "%lf %n", &d, &n) == 1)
79     {
80         if (pg->nweight % 100 == 0)
81         {
82             srenew(pg->weight, pg->nweight+100);
83         }
84         pg->weight[pg->nweight++] = d;
85         wbuf += n;
86     }
87 }
88
89 static void init_pull_coord(t_pull_coord *pcrd, int eGeom,
90                             const char *origin_buf, const char *vec_buf)
91 {
92     int    m;
93     dvec   origin, vec;
94
95     string2dvec(origin_buf, origin);
96     if (pcrd->group[0] != 0 && dnorm(origin) > 0)
97     {
98         gmx_fatal(FARGS, "The pull origin can only be set with an absolute reference");
99     }
100
101     if (eGeom == epullgDIST)
102     {
103         clear_dvec(vec);
104     }
105     else
106     {
107         string2dvec(vec_buf, vec);
108         if (eGeom == epullgDIR || eGeom == epullgCYL)
109         {
110             /* Normalize the direction vector */
111             dsvmul(1/dnorm(vec), vec, vec);
112         }
113     }
114     for (m = 0; m < DIM; m++)
115     {
116         pcrd->origin[m] = origin[m];
117         pcrd->vec[m]    = vec[m];
118     }
119 }
120
121 char **read_pullparams(int *ninp_p, t_inpfile **inp_p,
122                        t_pull *pull, gmx_bool *bStart,
123                        warninp_t wi)
124 {
125     int           ninp, nerror = 0, i, nchar, nscan, m, idum;
126     t_inpfile    *inp;
127     const char   *tmp;
128     char        **grpbuf;
129     char          dummy[STRLEN], buf[STRLEN], groups[STRLEN], init[STRLEN];
130     const char   *init_def1 = "0.0", *init_def3 = "0.0 0.0 0.0";
131     char          wbuf[STRLEN], origin_buf[STRLEN], vec_buf[STRLEN];
132
133     t_pull_group *pgrp;
134     t_pull_coord *pcrd;
135
136     ninp   = *ninp_p;
137     inp    = *inp_p;
138
139     /* read pull parameters */
140     CTYPE("Pull geometry: distance, direction, direction-periodic or cylinder");
141     EETYPE("pull-geometry",   pull->eGeom, epullg_names);
142     CTYPE("Select components for the pull vector. default: Y Y Y");
143     STYPE("pull-dim",         pulldim, "Y Y Y");
144     CTYPE("Cylinder radius for dynamic reaction force groups (nm)");
145     RTYPE("pull-r1",          pull->cyl_r1, 1.0);
146     CTYPE("Switch from r1 to r0 in case of dynamic reaction force");
147     RTYPE("pull-r0",          pull->cyl_r0, 1.5);
148     RTYPE("pull_constr_tol",  pull->constr_tol, 1E-6);
149     EETYPE("pull-start",      *bStart, yesno_names);
150     EETYPE("pull-print-reference", pull->bPrintRef, yesno_names);
151     ITYPE("pull-nstxout",     pull->nstxout, 10);
152     ITYPE("pull-nstfout",     pull->nstfout,  1);
153     CTYPE("Number of pull groups");
154     ITYPE("pull-ngroups",     pull->ngroup, 1);
155     CTYPE("Number of pull coordinates");
156     ITYPE("pull-ncoords",     pull->ncoord, 1);
157
158     if (pull->cyl_r1 > pull->cyl_r0)
159     {
160         warning_error(wi, "pull-r1 > pull_r0");
161     }
162
163     if (pull->ngroup < 1)
164     {
165         gmx_fatal(FARGS, "pull-ngroups should be >= 1");
166     }
167     /* We always add an absolute reference group (index 0), even if not used */
168     pull->ngroup += 1;
169
170     if (pull->ncoord < 1)
171     {
172         gmx_fatal(FARGS, "pull-ncoords should be >= 1");
173     }
174
175     snew(pull->group, pull->ngroup);
176
177     snew(pull->coord, pull->ncoord);
178
179     /* pull group options */
180     CTYPE("Group name, weight (default all 1), vector, init, rate (nm/ps), kJ/(mol*nm^2)");
181
182     /* Read the pull groups */
183     snew(grpbuf, pull->ngroup);
184     /* Group 0 is the absolute reference, we don't read anything for 0 */
185     for (i = 1; i < pull->ngroup; i++)
186     {
187         pgrp = &pull->group[i];
188         snew(grpbuf[i], STRLEN);
189         sprintf(buf, "pull-group%d-name", i);
190         STYPE(buf,              grpbuf[i], "");
191         sprintf(buf, "pull-group%d-weights", i);
192         STYPE(buf,              wbuf, "");
193         sprintf(buf, "pull-group%d-pbcatom", i);
194         ITYPE(buf,              pgrp->pbcatom, 0);
195
196         /* Initialize the pull group */
197         init_pull_group(pgrp, wbuf);
198     }
199
200     /* Read the pull coordinates */
201     for (i = 1; i < pull->ncoord + 1; i++)
202     {
203         pcrd = &pull->coord[i-1];
204         sprintf(buf, "pull-coord%d-groups", i);
205         STYPE(buf,              groups, "");
206         nscan = sscanf(groups, "%d %d %d", &pcrd->group[0], &pcrd->group[1], &idum);
207         if (nscan != 2)
208         {
209             fprintf(stderr, "ERROR: %s should have %d components\n", buf, 2);
210             nerror++;
211         }
212         sprintf(buf, "pull-coord%d-origin", i);
213         STYPE(buf,              origin_buf, "0.0 0.0 0.0");
214         sprintf(buf, "pull-coord%d-vec", i);
215         STYPE(buf,              vec_buf,    "0.0 0.0 0.0");
216         sprintf(buf, "pull-coord%d-init", i);
217         RTYPE(buf,              pcrd->init, 0.0);
218         sprintf(buf, "pull-coord%d-rate", i);
219         RTYPE(buf,              pcrd->rate, 0.0);
220         sprintf(buf, "pull-coord%d-k", i);
221         RTYPE(buf,              pcrd->k, 0.0);
222         sprintf(buf, "pull-coord%d-kB", i);
223         RTYPE(buf,              pcrd->kB, pcrd->k);
224
225         /* Initialize the pull coordinate */
226         init_pull_coord(pcrd, pull->eGeom, origin_buf, vec_buf);
227     }
228
229     *ninp_p   = ninp;
230     *inp_p    = inp;
231
232     return grpbuf;
233 }
234
235 void make_pull_groups(t_pull *pull,
236                       char **pgnames,
237                       const t_blocka *grps, char **gnames)
238 {
239     int           g, ig = -1, i;
240     t_pull_group *pgrp;
241
242     /* Absolute reference group (might not be used) is special */
243     pgrp          = &pull->group[0];
244     pgrp->nat     = 0;
245     pgrp->pbcatom = -1;
246
247     for (g = 1; g < pull->ngroup; g++)
248     {
249         pgrp = &pull->group[g];
250
251         ig        = search_string(pgnames[g], grps->nr, gnames);
252         pgrp->nat = grps->index[ig+1] - grps->index[ig];
253
254         fprintf(stderr, "Pull group %d '%s' has %d atoms\n",
255                 g, pgnames[g], pgrp->nat);
256
257         if (pgrp->nat == 0)
258         {
259             gmx_fatal(FARGS, "Pull group %d '%s' is empty", g, pgnames[g]);
260         }
261
262         snew(pgrp->ind, pgrp->nat);
263         for (i = 0; i < pgrp->nat; i++)
264         {
265             pgrp->ind[i] = grps->a[grps->index[ig]+i];
266         }
267
268         if (pull->eGeom == epullgCYL && g == 1 && pgrp->nweight > 0)
269         {
270             gmx_fatal(FARGS, "Weights are not supported for the reference group with cylinder pulling");
271         }
272         if (pgrp->nweight > 0 && pgrp->nweight != pgrp->nat)
273         {
274             gmx_fatal(FARGS, "Number of weights (%d) for pull group %d '%s' does not match the number of atoms (%d)",
275                       pgrp->nweight, g, pgnames[g], pgrp->nat);
276         }
277
278         if (pgrp->nat == 1)
279         {
280             /* No pbc is required for this group */
281             pgrp->pbcatom = -1;
282         }
283         else
284         {
285             if (pgrp->pbcatom > 0)
286             {
287                 pgrp->pbcatom -= 1;
288             }
289             else if (pgrp->pbcatom == 0)
290             {
291                 pgrp->pbcatom = pgrp->ind[(pgrp->nat-1)/2];
292             }
293             else
294             {
295                 /* Use cosine weighting */
296                 pgrp->pbcatom = -1;
297             }
298         }
299     }
300 }
301
302 void make_pull_coords(t_pull *pull)
303 {
304     int           ndim, d, nchar, c;
305     char         *ptr, pulldim1[STRLEN];
306     t_pull_coord *pcrd;
307
308     ptr  = pulldim;
309     ndim = 0;
310     for (d = 0; d < DIM; d++)
311     {
312         if (sscanf(ptr, "%s%n", pulldim1, &nchar) != 1)
313         {
314             gmx_fatal(FARGS, "Less than 3 pull dimensions given in pull_dim: '%s'",
315                       pulldim);
316         }
317
318         if (gmx_strncasecmp(pulldim1, "N", 1) == 0)
319         {
320             pull->dim[d] = 0;
321         }
322         else if (gmx_strncasecmp(pulldim1, "Y", 1) == 0)
323         {
324             pull->dim[d] = 1;
325             ndim++;
326         }
327         else
328         {
329             gmx_fatal(FARGS, "Please use Y(ES) or N(O) for pull_dim only (not %s)",
330                       pulldim1);
331         }
332         ptr += nchar;
333     }
334     if (ndim == 0)
335     {
336         gmx_fatal(FARGS, "All entries in pull_dim are N");
337     }
338
339     for (c = 0; c < pull->ncoord; c++)
340     {
341         pcrd = &pull->coord[c];
342
343         if (pcrd->group[0] < 0 || pcrd->group[0] >= pull->ngroup ||
344             pcrd->group[1] < 0 || pcrd->group[1] >= pull->ngroup)
345         {
346             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);
347         }
348
349         if (pcrd->group[0] == pcrd->group[1])
350         {
351             gmx_fatal(FARGS, "Identical pull group indices in pull-coord%d-groups", c+1);
352         }
353
354         if (pull->eGeom == epullgCYL && pcrd->group[0] != 1)
355         {
356             gmx_fatal(FARGS, "With pull geometry %s, the first pull group should always be 1", EPULLGEOM(pull->eGeom));
357         }
358
359         if (pull->eGeom != epullgDIST)
360         {
361             for (d = 0; d < DIM; d++)
362             {
363                 if (pcrd->vec[d] != 0 && pull->dim[d] == 0)
364                 {
365                     gmx_fatal(FARGS, "ERROR: pull-group%d-vec has non-zero %c-component while pull_dim for the %c-dimension is N\n", c+1, 'x'+d, 'x'+d);
366                 }
367             }
368         }
369
370         if ((pull->eGeom == epullgDIR || pull->eGeom == epullgCYL) &&
371             norm2(pcrd->vec) == 0)
372         {
373             gmx_fatal(FARGS, "pull-group%d-vec can not be zero with geometry %s",
374                       c+1, EPULLGEOM(pull->eGeom));
375         }
376     }
377 }
378
379 void set_pull_init(t_inputrec *ir, gmx_mtop_t *mtop, rvec *x, matrix box, real lambda,
380                    const output_env_t oenv, gmx_bool bStart)
381 {
382     t_mdatoms    *md;
383     t_pull       *pull;
384     t_pull_coord *pcrd;
385     t_pull_group *pgrp0, *pgrp1;
386     t_pbc         pbc;
387     int           c, m;
388     double        t_start, tinvrate;
389     real          init;
390     dvec          dr;
391     double        dev;
392
393     init_pull(NULL, ir, 0, NULL, mtop, NULL, oenv, lambda, FALSE, 0);
394     md = init_mdatoms(NULL, mtop, ir->efep);
395     atoms2md(mtop, ir, 0, NULL, mtop->natoms, md);
396     if (ir->efep)
397     {
398         update_mdatoms(md, lambda);
399     }
400     pull = ir->pull;
401
402     set_pbc(&pbc, ir->ePBC, box);
403
404     t_start = ir->init_t + ir->init_step*ir->delta_t;
405
406     pull_calc_coms(NULL, pull, md, &pbc, t_start, x, NULL);
407
408     fprintf(stderr, "Pull group  natoms  pbc atom  distance at start     reference at t=0\n");
409     for (c = 0; c < pull->ncoord; c++)
410     {
411         pcrd  = &pull->coord[c];
412
413         pgrp0 = &pull->group[pcrd->group[0]];
414         pgrp1 = &pull->group[pcrd->group[1]];
415         fprintf(stderr, "%8d  %8d  %8d\n",
416                 pcrd->group[0], pgrp0->nat, pgrp0->pbcatom+1);
417         fprintf(stderr, "%8d  %8d  %8d ",
418                 pcrd->group[1], pgrp1->nat, pgrp1->pbcatom+1);
419
420         init       = pcrd->init;
421         pcrd->init = 0;
422
423         if (pcrd->rate == 0)
424         {
425             tinvrate = 0;
426         }
427         else
428         {
429             tinvrate = t_start/pcrd->rate;
430         }
431         get_pull_coord_distance(pull, c, &pbc, 0, dr, &dev);
432         fprintf(stderr, " %6.3f ", dev);
433
434         if (bStart)
435         {
436             pcrd->init = init + dev - tinvrate;
437         }
438         else
439         {
440             pcrd->init = init;
441         }
442         fprintf(stderr, " %6.3f\n", pcrd->init);
443     }
444 }