Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / readpull.cpp
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,2016,2017 by the GROMACS development team.
7  * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #include "gmxpre.h"
39
40 #include <cassert>
41 #include <cstdlib>
42 #include <cstring>
43
44 #include "gromacs/domdec/localatomsetmanager.h"
45 #include "gromacs/fileio/readinp.h"
46 #include "gromacs/fileio/warninp.h"
47 #include "gromacs/gmxpreprocess/readir.h"
48 #include "gromacs/math/vec.h"
49 #include "gromacs/mdlib/mdatoms.h"
50 #include "gromacs/mdtypes/inputrec.h"
51 #include "gromacs/mdtypes/md_enums.h"
52 #include "gromacs/mdtypes/mdatom.h"
53 #include "gromacs/mdtypes/pull_params.h"
54 #include "gromacs/pbcutil/pbc.h"
55 #include "gromacs/pulling/pull.h"
56 #include "gromacs/topology/topology.h"
57 #include "gromacs/utility/arrayref.h"
58 #include "gromacs/utility/basedefinitions.h"
59 #include "gromacs/utility/cstringutil.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/futil.h"
62 #include "gromacs/utility/smalloc.h"
63
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 std::vector<real> setupPullGroupWeights(const char* wbuf)
76 {
77     double d;
78     int    n;
79
80     std::vector<real> weight;
81     while (sscanf(wbuf, "%lf %n", &d, &n) == 1)
82     {
83         weight.push_back(d);
84         wbuf += n;
85     }
86     return weight;
87 }
88
89 static void process_pull_dim(char* dim_buf, ivec dim, const t_pull_coord* pcrd)
90 {
91     int   ndim, d, nchar;
92     char *ptr, pulldim1[STRLEN];
93
94     ptr  = dim_buf;
95     ndim = 0;
96     for (d = 0; d < DIM; d++)
97     {
98         if (sscanf(ptr, "%s%n", pulldim1, &nchar) != 1)
99         {
100             gmx_fatal(FARGS, "Less than 3 pull dimensions given in pull_dim: '%s'", dim_buf);
101         }
102
103         if (gmx::equalCaseInsensitive(pulldim1, "N", 1))
104         {
105             dim[d] = 0;
106         }
107         else if (gmx::equalCaseInsensitive(pulldim1, "Y", 1))
108         {
109             dim[d] = 1;
110             ndim++;
111         }
112         else
113         {
114             gmx_fatal(FARGS, "Please use Y(ES) or N(O) for pull_dim only (not %s)", pulldim1);
115         }
116         ptr += nchar;
117     }
118     if (ndim == 0)
119     {
120         gmx_fatal(FARGS, "All entries in pull dim are N");
121     }
122     if ((pcrd->eGeom == epullgDIHEDRAL) && (ndim < 3))
123     {
124         gmx_fatal(FARGS, "Pull geometry dihedral is only useful with pull-dim = Y Y Y");
125     }
126     if ((pcrd->eGeom == epullgANGLE || pcrd->eGeom == epullgANGLEAXIS) && (ndim < 2))
127     {
128         gmx_fatal(FARGS,
129                   "Pull geometry %s is only useful with pull-dim = Y for at least 2 dimensions",
130                   EPULLGEOM(pcrd->eGeom));
131     }
132 }
133
134 static void init_pull_coord(t_pull_coord* pcrd,
135                             int           coord_index_for_output,
136                             char*         dim_buf,
137                             const char*   origin_buf,
138                             const char*   vec_buf,
139                             warninp_t     wi)
140 {
141     int  m;
142     dvec origin, vec;
143     char buf[STRLEN];
144
145     if (pcrd->eType == epullCONSTRAINT
146         && (pcrd->eGeom == epullgCYL || pcrd->eGeom == epullgDIRRELATIVE || pcrd->eGeom == epullgANGLE
147             || pcrd->eGeom == epullgANGLEAXIS || pcrd->eGeom == epullgDIHEDRAL))
148     {
149         gmx_fatal(FARGS,
150                   "Pulling of type %s can not be combined with geometry %s. Consider using pull "
151                   "type %s.",
152                   epull_names[pcrd->eType],
153                   epullg_names[pcrd->eGeom],
154                   epull_names[epullUMBRELLA]);
155     }
156
157     if (pcrd->eType == epullEXTERNAL)
158     {
159         if (pcrd->externalPotentialProvider[0] == '\0')
160         {
161             sprintf(buf,
162                     "The use of pull type '%s' for pull coordinate %d requires that the name of "
163                     "the module providing the potential external is set with the option %s%d%s",
164                     epull_names[pcrd->eType],
165                     coord_index_for_output,
166                     "pull-coord",
167                     coord_index_for_output,
168                     "-potential-provider");
169             warning_error(wi, buf);
170         }
171
172         if (pcrd->rate != 0)
173         {
174             sprintf(buf,
175                     "The use of pull type '%s' for pull coordinate %d requires that the pull rate "
176                     "is zero",
177                     epull_names[pcrd->eType],
178                     coord_index_for_output);
179             warning_error(wi, buf);
180         }
181
182         if (pcrd->eGeom == epullgCYL)
183         {
184             /* Warn the user of a PBC restriction, caused by the fact that
185              * there is no reference value with an external pull potential.
186              */
187             sprintf(buf,
188                     "With pull type '%s' and geometry '%s', the distance component along the "
189                     "cylinder axis between atoms in the cylinder group and the COM of the pull "
190                     "group should be smaller than half the box length",
191                     epull_names[pcrd->eType],
192                     epullg_names[pcrd->eGeom]);
193             warning_note(wi, buf);
194         }
195     }
196
197     process_pull_dim(dim_buf, pcrd->dim, pcrd);
198
199     string2dvec(origin_buf, origin);
200     if (pcrd->group[0] != 0 && dnorm(origin) > 0)
201     {
202         gmx_fatal(FARGS, "The pull origin can only be set with an absolute reference");
203     }
204
205     /* Check the given initial reference value and warn for dangerous values */
206     if (pcrd->eGeom == epullgDIST)
207     {
208         if (pcrd->bStart && pcrd->init < 0)
209         {
210             sprintf(buf,
211                     "The initial reference distance set by pull-coord-init is set to a negative "
212                     "value (%g) with geometry %s while distances need to be non-negative. "
213                     "This may work, since you have set pull-coord-start to 'yes' which modifies "
214                     "this value, but only for certain starting distances. "
215                     "If this is a mistake you may want to use geometry %s instead.",
216                     pcrd->init,
217                     EPULLGEOM(pcrd->eGeom),
218                     EPULLGEOM(epullgDIR));
219             warning(wi, buf);
220         }
221     }
222     else if (pcrd->eGeom == epullgANGLE || pcrd->eGeom == epullgANGLEAXIS)
223     {
224         if (pcrd->bStart && (pcrd->init < 0 || pcrd->init > 180))
225         {
226             /* This value of pcrd->init may be ok depending on pcrd->bStart which modifies pcrd->init later on */
227             sprintf(buf,
228                     "The initial reference angle set by pull-coord-init (%g) is outside of the "
229                     "allowed range [0, 180] degrees for geometry (%s). "
230                     "This may work, since you have set pull-coord-start to 'yes' which modifies "
231                     "this value, but only for certain starting angles.",
232                     pcrd->init,
233                     EPULLGEOM(pcrd->eGeom));
234             warning(wi, buf);
235         }
236     }
237     else if (pcrd->eGeom == epullgDIHEDRAL)
238     {
239         if (pcrd->bStart && (pcrd->init < -180 || pcrd->init > 180))
240         {
241             sprintf(buf,
242                     "The initial reference angle set by pull-coord-init (%g) is outside of the "
243                     "allowed range [-180, 180] degrees for geometry (%s). "
244                     "This may work, since you have set pull-coord-start to 'yes' which modifies "
245                     "this value, but only for certain starting angles.",
246                     pcrd->init,
247                     EPULLGEOM(pcrd->eGeom));
248             warning(wi, buf);
249         }
250     }
251
252     /* Check and set the pull vector */
253     clear_dvec(vec);
254     string2dvec(vec_buf, vec);
255
256     if (pcrd->eGeom == epullgDIR || pcrd->eGeom == epullgCYL || pcrd->eGeom == epullgDIRPBC
257         || pcrd->eGeom == epullgANGLEAXIS)
258     {
259         if (dnorm2(vec) == 0)
260         {
261             gmx_fatal(FARGS,
262                       "With pull geometry %s the pull vector can not be 0,0,0",
263                       epullg_names[pcrd->eGeom]);
264         }
265         for (int d = 0; d < DIM; d++)
266         {
267             if (vec[d] != 0 && pcrd->dim[d] == 0)
268             {
269                 gmx_fatal(FARGS,
270                           "pull-coord-vec has non-zero %c-component while pull_dim for the "
271                           "%c-dimension is set to N",
272                           'x' + d,
273                           'x' + d);
274             }
275         }
276
277         /* Normalize the direction vector */
278         dsvmul(1 / dnorm(vec), vec, vec);
279     }
280     else /* This case is for are all the geometries where the pull vector is not used */
281     {
282         if (dnorm2(vec) > 0)
283         {
284             sprintf(buf,
285                     "A pull vector is given (%g  %g  %g) but will not be used with geometry %s. If "
286                     "you really want to use this "
287                     "vector, consider using geometry %s instead.",
288                     vec[0],
289                     vec[1],
290                     vec[2],
291                     EPULLGEOM(pcrd->eGeom),
292                     pcrd->eGeom == epullgANGLE ? EPULLGEOM(epullgANGLEAXIS) : EPULLGEOM(epullgDIR));
293             warning(wi, buf);
294         }
295     }
296     for (m = 0; m < DIM; m++)
297     {
298         pcrd->origin[m] = origin[m];
299         pcrd->vec[m]    = vec[m];
300     }
301 }
302
303 std::vector<std::string> read_pullparams(std::vector<t_inpfile>* inp, pull_params_t* pull, warninp_t wi)
304 {
305     int  nscan, idum;
306     char buf[STRLEN];
307     char provider[STRLEN], groups[STRLEN], dim_buf[STRLEN];
308     char wbuf[STRLEN], origin_buf[STRLEN], vec_buf[STRLEN];
309
310     /* read pull parameters */
311     printStringNoNewline(inp, "Cylinder radius for dynamic reaction force groups (nm)");
312     pull->cylinder_r     = get_ereal(inp, "pull-cylinder-r", 1.5, wi);
313     pull->constr_tol     = get_ereal(inp, "pull-constr-tol", 1E-6, wi);
314     pull->bPrintCOM      = (get_eeenum(inp, "pull-print-com", yesno_names, wi) != 0);
315     pull->bPrintRefValue = (get_eeenum(inp, "pull-print-ref-value", yesno_names, wi) != 0);
316     pull->bPrintComp     = (get_eeenum(inp, "pull-print-components", yesno_names, wi) != 0);
317     pull->nstxout        = get_eint(inp, "pull-nstxout", 50, wi);
318     pull->nstfout        = get_eint(inp, "pull-nstfout", 50, wi);
319     pull->bSetPbcRefToPrevStepCOM = (get_eeenum(inp, "pull-pbc-ref-prev-step-com", yesno_names, wi) != 0);
320     pull->bXOutAverage            = (get_eeenum(inp, "pull-xout-average", yesno_names, wi) != 0);
321     pull->bFOutAverage            = (get_eeenum(inp, "pull-fout-average", yesno_names, wi) != 0);
322     printStringNoNewline(inp, "Number of pull groups");
323     pull->ngroup = get_eint(inp, "pull-ngroups", 1, wi);
324     printStringNoNewline(inp, "Number of pull coordinates");
325     pull->ncoord = get_eint(inp, "pull-ncoords", 1, wi);
326
327     if (pull->ngroup < 1)
328     {
329         gmx_fatal(FARGS, "pull-ngroups should be >= 1");
330     }
331     /* We always add an absolute reference group (index 0), even if not used */
332     pull->ngroup += 1;
333
334     if (pull->ncoord < 1)
335     {
336         gmx_fatal(FARGS, "pull-ncoords should be >= 1");
337     }
338
339     /* pull group options */
340     printStringNoNewline(inp, "Group and coordinate parameters");
341
342     /* Read the pull groups */
343     std::vector<std::string> pullGroups(pull->ngroup);
344     char                     readBuffer[STRLEN];
345     /* Group 0 is the absolute reference, we don't read anything for 0 */
346     pull->group.emplace_back(t_pull_group());
347     for (int groupNum = 1; groupNum < pull->ngroup; groupNum++)
348     {
349         t_pull_group pullGroup; //= &pull->group[groupNum];
350         sprintf(buf, "pull-group%d-name", groupNum);
351         setStringEntry(inp, buf, readBuffer, "");
352         pullGroups[groupNum] = readBuffer;
353         sprintf(buf, "pull-group%d-weights", groupNum);
354         setStringEntry(inp, buf, wbuf, "");
355         sprintf(buf, "pull-group%d-pbcatom", groupNum);
356         pullGroup.pbcatom = get_eint(inp, buf, 0, wi);
357
358         /* Initialize the pull group */
359         pullGroup.weight = setupPullGroupWeights(wbuf);
360         pull->group.emplace_back(pullGroup);
361     }
362
363     /* Read the pull coordinates */
364     for (int coordNum = 1; coordNum < pull->ncoord + 1; coordNum++)
365     {
366         t_pull_coord pullCoord; // = &pull->coord[coordNum - 1];
367         sprintf(buf, "pull-coord%d-type", coordNum);
368         pullCoord.eType = get_eeenum(inp, buf, epull_names, wi);
369         sprintf(buf, "pull-coord%d-potential-provider", coordNum);
370         setStringEntry(inp, buf, provider, "");
371         pullCoord.externalPotentialProvider = gmx_strdup(provider);
372         sprintf(buf, "pull-coord%d-geometry", coordNum);
373         pullCoord.eGeom = get_eeenum(inp, buf, epullg_names, wi);
374         sprintf(buf, "pull-coord%d-groups", coordNum);
375         setStringEntry(inp, buf, groups, "");
376
377         switch (pullCoord.eGeom)
378         {
379             case epullgDIHEDRAL: pullCoord.ngroup = 6; break;
380             case epullgDIRRELATIVE:
381             case epullgANGLE: pullCoord.ngroup = 4; break;
382             default: pullCoord.ngroup = 2; break;
383         }
384
385         nscan = sscanf(groups,
386                        "%d %d %d %d %d %d %d",
387                        &pullCoord.group[0],
388                        &pullCoord.group[1],
389                        &pullCoord.group[2],
390                        &pullCoord.group[3],
391                        &pullCoord.group[4],
392                        &pullCoord.group[5],
393                        &idum);
394         if (nscan != pullCoord.ngroup)
395         {
396             auto message =
397                     gmx::formatString("%s should contain %d pull group indices with geometry %s",
398                                       buf,
399                                       pullCoord.ngroup,
400                                       epullg_names[pullCoord.eGeom]);
401             set_warning_line(wi, nullptr, -1);
402             warning_error(wi, message);
403         }
404         for (int g = 0; g < pullCoord.ngroup; g++)
405         {
406             if (pullCoord.group[g] < 0 || pullCoord.group[g] >= pull->ngroup)
407             {
408                 /* Quit with a fatal error to avoid invalid memory access */
409                 gmx_fatal(FARGS,
410                           "%s contains an invalid pull group %d, you should have %d <= group <= %d",
411                           buf,
412                           pullCoord.group[g],
413                           0,
414                           pull->ngroup - 1);
415             }
416         }
417
418         sprintf(buf, "pull-coord%d-dim", coordNum);
419         setStringEntry(inp, buf, dim_buf, "Y Y Y");
420         sprintf(buf, "pull-coord%d-origin", coordNum);
421         setStringEntry(inp, buf, origin_buf, "0.0 0.0 0.0");
422         sprintf(buf, "pull-coord%d-vec", coordNum);
423         setStringEntry(inp, buf, vec_buf, "0.0 0.0 0.0");
424         sprintf(buf, "pull-coord%d-start", coordNum);
425         pullCoord.bStart = (get_eeenum(inp, buf, yesno_names, wi) != 0);
426         sprintf(buf, "pull-coord%d-init", coordNum);
427         pullCoord.init = get_ereal(inp, buf, 0.0, wi);
428         sprintf(buf, "pull-coord%d-rate", coordNum);
429         pullCoord.rate = get_ereal(inp, buf, 0.0, wi);
430         sprintf(buf, "pull-coord%d-k", coordNum);
431         pullCoord.k = get_ereal(inp, buf, 0.0, wi);
432         sprintf(buf, "pull-coord%d-kB", coordNum);
433         pullCoord.kB = get_ereal(inp, buf, pullCoord.k, wi);
434
435         /* Initialize the pull coordinate */
436         init_pull_coord(&pullCoord, coordNum, dim_buf, origin_buf, vec_buf, wi);
437         pull->coord.emplace_back(pullCoord);
438     }
439
440     return pullGroups;
441 }
442
443 void process_pull_groups(gmx::ArrayRef<t_pull_group>      pullGroups,
444                          gmx::ArrayRef<const std::string> pullGroupNames,
445                          const t_blocka*                  grps,
446                          char**                           gnames)
447 {
448     /* Absolute reference group (might not be used) is special */
449     pullGroups.front().pbcatom       = -1;
450     pullGroups.front().pbcatom_input = -1;
451
452     // Skip pull group 0 here, as that is the absolute reference
453     for (int g = 1; g < int(pullGroups.size()); g++)
454     {
455         auto& pullGroup = pullGroups[g];
456
457         if (pullGroupNames[g].empty())
458         {
459             gmx_fatal(FARGS, "Pull option pull_group%d required by grompp has not been set.", g);
460         }
461
462         int ig                = search_string(pullGroupNames[g].c_str(), grps->nr, gnames);
463         int numPullGroupAtoms = grps->index[ig + 1] - grps->index[ig];
464
465         fprintf(stderr, "Pull group %d '%s' has %d atoms\n", g, pullGroupNames[g].c_str(), numPullGroupAtoms);
466
467         if (numPullGroupAtoms == 0)
468         {
469             gmx_fatal(FARGS, "Pull group %d '%s' is empty", g, pullGroupNames[g].c_str());
470         }
471
472         for (int i = 0; i < numPullGroupAtoms; i++)
473         {
474             pullGroup.ind.push_back(grps->a[grps->index[ig] + i]);
475         }
476
477         if (!pullGroup.weight.empty() && pullGroup.weight.size() != pullGroup.ind.size())
478         {
479             gmx_fatal(FARGS,
480                       "Number of weights (%ld) for pull group %d '%s' does not match the number of "
481                       "atoms (%ld)",
482                       gmx::ssize(pullGroup.weight),
483                       g,
484                       pullGroupNames[g].c_str(),
485                       gmx::ssize(pullGroup.ind));
486         }
487
488         pullGroup.pbcatom_input = pullGroup.pbcatom;
489         if (pullGroup.ind.size() == 1)
490         {
491             /* No pbc is required for this group */
492             pullGroup.pbcatom = -1;
493         }
494         else
495         {
496             if (pullGroup.pbcatom > 0)
497             {
498                 pullGroup.pbcatom -= 1;
499             }
500             else if (pullGroup.pbcatom == 0)
501             {
502                 pullGroup.pbcatom = pullGroup.ind[(pullGroup.ind.size() - 1) / 2];
503             }
504             else
505             {
506                 /* Use cosine weighting */
507                 pullGroup.pbcatom = -1;
508             }
509         }
510     }
511 }
512
513 void checkPullCoords(gmx::ArrayRef<const t_pull_group> pullGroups, gmx::ArrayRef<const t_pull_coord> pullCoords)
514 {
515     for (int c = 0; c < int(pullCoords.size()); c++)
516     {
517         t_pull_coord pcrd = pullCoords[c];
518
519         if (pcrd.group[0] < 0 || pcrd.group[0] >= int(pullGroups.size()) || pcrd.group[1] < 0
520             || pcrd.group[1] >= int(pullGroups.size()))
521         {
522             gmx_fatal(FARGS,
523                       "Pull group index in pull-coord%d-groups out of range, should be between %d "
524                       "and %d",
525                       c + 1,
526                       0,
527                       int(pullGroups.size()) + 1);
528         }
529
530         if (pcrd.group[0] == pcrd.group[1])
531         {
532             gmx_fatal(FARGS, "Identical pull group indices in pull-coord%d-groups", c + 1);
533         }
534
535         if (pcrd.eGeom == epullgCYL)
536         {
537             if (!pullGroups[pcrd.group[0]].weight.empty())
538             {
539                 gmx_fatal(
540                         FARGS,
541                         "Weights are not supported for the reference group with cylinder pulling");
542             }
543         }
544     }
545 }
546
547 pull_t* set_pull_init(t_inputrec* ir, const gmx_mtop_t* mtop, rvec* x, matrix box, real lambda, warninp_t wi)
548 {
549     pull_t* pull_work;
550     t_pbc   pbc;
551     int     c;
552     double  t_start;
553
554     pull_params_t*           pull = ir->pull.get();
555     gmx::LocalAtomSetManager atomSets;
556     pull_work    = init_pull(nullptr, pull, ir, mtop, nullptr, &atomSets, lambda);
557     auto mdAtoms = gmx::makeMDAtoms(nullptr, *mtop, *ir, false);
558     auto md      = mdAtoms->mdatoms();
559     atoms2md(mtop, ir, -1, {}, mtop->natoms, mdAtoms.get());
560     if (ir->efep)
561     {
562         update_mdatoms(md, lambda);
563     }
564
565     set_pbc(&pbc, ir->pbcType, box);
566
567     t_start = ir->init_t + ir->init_step * ir->delta_t;
568
569     if (pull->bSetPbcRefToPrevStepCOM)
570     {
571         initPullComFromPrevStep(nullptr, pull_work, md->massT, &pbc, x);
572     }
573     pull_calc_coms(nullptr, pull_work, md->massT, &pbc, t_start, x, nullptr);
574
575     for (int g = 0; g < pull->ngroup; g++)
576     {
577         bool groupObeysPbc = pullCheckPbcWithinGroup(
578                 *pull_work,
579                 gmx::arrayRefFromArray(reinterpret_cast<gmx::RVec*>(x), mtop->natoms),
580                 pbc,
581                 g,
582                 c_pullGroupSmallGroupThreshold);
583         if (!groupObeysPbc)
584         {
585             char buf[STRLEN];
586             if (pull->group[g].pbcatom_input == 0)
587             {
588                 sprintf(buf,
589                         "When the maximum distance from a pull group reference atom to other atoms "
590                         "in the "
591                         "group is larger than %g times half the box size a centrally placed "
592                         "atom should be chosen as pbcatom. Pull group %d is larger than that and "
593                         "does not have "
594                         "a specific atom selected as reference atom.",
595                         c_pullGroupSmallGroupThreshold,
596                         g);
597                 warning_error(wi, buf);
598             }
599             else if (!pull->bSetPbcRefToPrevStepCOM)
600             {
601                 sprintf(buf,
602                         "The maximum distance from the chosen PBC atom (%d) of pull group %d to "
603                         "other "
604                         "atoms in the group is larger than %g times half the box size. "
605                         "Set the pull-pbc-ref-prev-step-com option to yes.",
606                         pull->group[g].pbcatom + 1,
607                         g,
608                         c_pullGroupSmallGroupThreshold);
609                 warning_error(wi, buf);
610             }
611         }
612         if (groupObeysPbc)
613         {
614             groupObeysPbc = pullCheckPbcWithinGroup(
615                     *pull_work,
616                     gmx::arrayRefFromArray(reinterpret_cast<gmx::RVec*>(x), mtop->natoms),
617                     pbc,
618                     g,
619                     c_pullGroupPbcMargin);
620             if (!groupObeysPbc)
621             {
622                 char buf[STRLEN];
623                 sprintf(buf,
624                         "Pull group %d has atoms at a distance larger than %g times half the box "
625                         "size from the PBC atom (%d). "
626                         "If atoms are or will more beyond half the box size from the PBC atom, the "
627                         "COM will be ill defined.",
628                         g,
629                         c_pullGroupPbcMargin,
630                         pull->group[g].pbcatom + 1);
631                 set_warning_line(wi, nullptr, -1);
632                 warning(wi, buf);
633             }
634         }
635     }
636
637     fprintf(stderr, "Pull group  natoms  pbc atom  distance at start  reference at t=0\n");
638     for (c = 0; c < pull->ncoord; c++)
639     {
640         t_pull_coord* pcrd;
641         t_pull_group *pgrp0, *pgrp1;
642         double        value;
643         real          init = 0;
644
645         pcrd = &pull->coord[c];
646
647         pgrp0 = &pull->group[pcrd->group[0]];
648         pgrp1 = &pull->group[pcrd->group[1]];
649         fprintf(stderr, "%8d  %8zu  %8d\n", pcrd->group[0], pgrp0->ind.size(), pgrp0->pbcatom + 1);
650         fprintf(stderr, "%8d  %8zu  %8d ", pcrd->group[1], pgrp1->ind.size(), pgrp1->pbcatom + 1);
651
652         if (pcrd->bStart)
653         {
654             init       = pcrd->init;
655             pcrd->init = 0;
656         }
657
658         value = get_pull_coord_value(pull_work, c, &pbc);
659
660         value *= pull_conversion_factor_internal2userinput(pcrd);
661         fprintf(stderr, " %10.3f %s", value, pull_coordinate_units(pcrd));
662
663         if (pcrd->bStart)
664         {
665             pcrd->init = value + init;
666         }
667
668         if (pcrd->eGeom == epullgDIST)
669         {
670             if (pcrd->init < 0)
671             {
672                 gmx_fatal(FARGS,
673                           "The initial pull distance (%g) needs to be non-negative with geometry "
674                           "%s. If you want a signed distance, use geometry %s instead.",
675                           pcrd->init,
676                           EPULLGEOM(pcrd->eGeom),
677                           EPULLGEOM(epullgDIR));
678             }
679
680             /* TODO: With a positive init but a negative rate things could still
681              * go wrong, but it might be fine if you don't pull too far.
682              * We should give a warning or note when there is only one pull dim
683              * active, since that is usually the problematic case when you should
684              * be using direction. We will do this later, since an already planned
685              * generalization of the pull code makes pull dim available here.
686              */
687         }
688         else if (pcrd->eGeom == epullgANGLE || pcrd->eGeom == epullgANGLEAXIS)
689         {
690             if (pcrd->init < 0 || pcrd->init > 180)
691             {
692                 gmx_fatal(FARGS,
693                           "The initial pull reference angle (%g) is outside of the allowed range "
694                           "[0, 180] degrees.",
695                           pcrd->init);
696             }
697         }
698         else if (pcrd->eGeom == epullgDIHEDRAL)
699         {
700             if (pcrd->init < -180 || pcrd->init > 180)
701             {
702                 gmx_fatal(FARGS,
703                           "The initial pull reference angle (%g) is outside of the allowed range "
704                           "[-180, 180] degrees.",
705                           pcrd->init);
706             }
707         }
708
709
710         fprintf(stderr, "     %10.3f %s\n", pcrd->init, pull_coordinate_units(pcrd));
711     }
712
713     return pull_work;
714 }