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