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