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