2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
40 #include "read_params.h"
42 #include "gromacs/awh/awh.h"
43 #include "gromacs/fileio/readinp.h"
44 #include "gromacs/fileio/warninp.h"
45 #include "gromacs/math/units.h"
46 #include "gromacs/math/utilities.h"
47 #include "gromacs/math/vec.h"
48 #include "gromacs/mdtypes/awh_params.h"
49 #include "gromacs/mdtypes/inputrec.h"
50 #include "gromacs/mdtypes/md_enums.h"
51 #include "gromacs/mdtypes/pull_params.h"
52 #include "gromacs/pbcutil/pbc.h"
53 #include "gromacs/pulling/pull.h"
54 #include "gromacs/random/seed.h"
55 #include "gromacs/topology/mtop_util.h"
56 #include "gromacs/utility/cstringutil.h"
57 #include "gromacs/utility/fatalerror.h"
58 #include "gromacs/utility/smalloc.h"
59 #include "gromacs/utility/stringutil.h"
61 #include "biasparams.h"
62 #include "biassharing.h"
67 const char* eawhtarget_names[eawhtargetNR + 1] = { "constant", "cutoff", "boltzmann",
68 "local-boltzmann", nullptr };
70 const char* eawhgrowth_names[eawhgrowthNR + 1] = { "exp-linear", "linear", nullptr };
72 const char* eawhpotential_names[eawhpotentialNR + 1] = { "convolved", "umbrella", nullptr };
74 const char* eawhcoordprovider_names[eawhcoordproviderNR + 1] = { "pull", "fep-lambda", nullptr };
79 * Check the parameters of an AWH bias pull dimension.
81 * \param[in] prefix Prefix for dimension parameters.
82 * \param[in,out] dimParams AWH dimensional parameters.
83 * \param[in] pull_params Pull parameters.
84 * \param[in,out] wi Struct for bookeeping warnings.
86 void checkPullDimParams(const std::string& prefix,
87 AwhDimParams* dimParams,
88 const pull_params_t* pull_params,
91 if (dimParams->coordIndex < 0)
94 "Failed to read a valid coordinate index for %s-coord-index. "
95 "Note that the pull coordinate indexing starts at 1.",
98 if (dimParams->coordIndex >= pull_params->ncoord)
101 "The given AWH coordinate index (%d) is larger than the number of pull "
103 dimParams->coordIndex + 1, pull_params->ncoord);
105 if (pull_params->coord[dimParams->coordIndex].rate != 0)
107 auto message = formatString(
108 "Setting pull-coord%d-rate (%g) is incompatible with AWH biasing this coordinate",
109 dimParams->coordIndex + 1, pull_params->coord[dimParams->coordIndex].rate);
110 warning_error(wi, message);
113 if (gmx_within_tol(dimParams->end - dimParams->origin, 0, GMX_REAL_EPS))
115 auto message = formatString(
116 "The given interval length given by %s-start (%g) and %s-end (%g) is zero. "
117 "This will result in only one point along this axis in the coordinate value grid.",
118 prefix.c_str(), dimParams->origin, prefix.c_str(), dimParams->end);
119 warning(wi, message);
122 if (dimParams->forceConstant <= 0)
124 warning_error(wi, "The force AWH bias force constant should be > 0");
127 /* Grid params for each axis */
128 int eGeom = pull_params->coord[dimParams->coordIndex].eGeom;
130 /* Check that the requested interval is in allowed range */
131 if (eGeom == epullgDIST)
133 if (dimParams->origin < 0 || dimParams->end < 0)
136 "%s-start (%g) or %s-end (%g) set to a negative value. With pull "
137 "geometry distance coordinate values are non-negative. "
138 "Perhaps you want to use geometry %s instead?",
139 prefix.c_str(), dimParams->origin, prefix.c_str(), dimParams->end,
140 EPULLGEOM(epullgDIR));
143 else if (eGeom == epullgANGLE || eGeom == epullgANGLEAXIS)
145 if (dimParams->origin < 0 || dimParams->end > 180)
148 "%s-start (%g) and %s-end (%g) are outside of the allowed range "
149 "0 to 180 deg for pull geometries %s and %s ",
150 prefix.c_str(), dimParams->origin, prefix.c_str(), dimParams->end,
151 EPULLGEOM(epullgANGLE), EPULLGEOM(epullgANGLEAXIS));
154 else if (eGeom == epullgDIHEDRAL)
156 if (dimParams->origin < -180 || dimParams->end > 180)
159 "%s-start (%g) and %s-end (%g) are outside of the allowed range "
160 "-180 to 180 deg for pull geometry %s. ",
161 prefix.c_str(), dimParams->origin, prefix.c_str(), dimParams->end,
162 EPULLGEOM(epullgDIHEDRAL));
168 * Check parameters of an AWH bias in a free energy lambda state dimension.
170 * \param[in] prefix Prefix for dimension parameters.
171 * \param[in,out] dimParams AWH dimensional parameters.
172 * \param[in] lambdaParams The free energy lambda related parameters.
173 * \param[in] efep This is the type of FEP calculation (efep enumerator).
174 * \param[in,out] wi Struct for bookeeping warnings.
176 void checkFepLambdaDimParams(const std::string& prefix,
177 const AwhDimParams* dimParams,
178 const t_lambda* lambdaParams,
187 "There must be free energy input if using AWH to steer the free energy lambda "
191 if (lambdaParams->lambda_neighbors != -1)
194 "When running AWH coupled to the free energy lambda state all lambda states "
195 "should be used as neighbors in order to get correct probabilities, i.e. "
196 "calc-lambda-neighbors (%d) must be %d.",
197 lambdaParams->lambda_neighbors, -1);
200 if (efep == efepSLOWGROWTH || lambdaParams->delta_lambda != 0)
203 "AWH coupled to the free energy lambda state is not compatible with slow-growth "
204 "and delta-lambda must be 0.");
207 if (efep == efepEXPANDED)
210 "AWH is not treated like other expanded ensemble methods. Do not use expanded.");
213 if (dimParams->origin < 0)
215 opt = prefix + "-start";
217 "When running AWH coupled to the free energy lambda state the lower lambda state "
218 "for AWH, %s (%.0f), must be >= 0.",
219 opt.c_str(), dimParams->origin);
221 if (dimParams->end >= lambdaParams->n_lambda)
223 opt = prefix + "-end";
225 "When running AWH coupled to the free energy lambda state the upper lambda state "
226 "for AWH, %s (%.0f), must be < n_lambda (%d).",
227 opt.c_str(), dimParams->origin, lambdaParams->n_lambda);
229 if (gmx_within_tol(dimParams->end - dimParams->origin, 0, GMX_REAL_EPS))
231 auto message = formatString(
232 "The given interval length given by %s-start (%g) and %s-end (%g) is zero. "
233 "This will result in only one lambda point along this free energy lambda state "
234 "axis in the coordinate value grid.",
235 prefix.c_str(), dimParams->origin, prefix.c_str(), dimParams->end);
236 warning(wi, message);
239 if (dimParams->forceConstant != 0)
243 "The force AWH bias force constant is not used with free energy lambda state as "
244 "coordinate provider.");
249 * Check that AWH FEP is not combined with incompatible decoupling.
251 * \param[in] mtop System topology.
252 * \param[in,out] wi Struct for bookeeping warnings.
254 void checkFepLambdaDimDecouplingConsistency(const gmx_mtop_t& mtop, warninp_t wi)
256 if (haveFepPerturbedMasses(mtop))
259 "Masses may not be perturbed when using the free energy lambda state as AWH "
260 "coordinate provider. If you are using fep-lambdas to specify lambda states "
261 "make sure that you also specify mass-lambdas without perturbation.");
263 if (havePerturbedConstraints(mtop))
266 "Constraints may not be perturbed when using the free energy lambda state as AWH "
267 "coordinate provider. If you are using fep-lambdas to specify lambda states "
268 "make sure that you also specify mass-lambdas without perturbation.");
273 * Read parameters of an AWH bias dimension.
275 * \param[in,out] inp Input file entries.
276 * \param[in] prefix Prefix for dimension parameters.
277 * \param[in,out] dimParams AWH dimensional parameters.
278 * \param[in,out] wi Struct for bookeeping warnings.
279 * \param[in] bComment True if comments should be printed.
281 void readDimParams(std::vector<t_inpfile>* inp,
282 const std::string& prefix,
283 AwhDimParams* dimParams,
290 printStringNoNewline(
292 "The provider of the reaction coordinate, "
293 "currently only 'pull' and 'fep-lambda' (free energy lambda state) is supported");
295 opt = prefix + "-coord-provider";
296 dimParams->eCoordProvider = get_eeenum(inp, opt, eawhcoordprovider_names, wi);
300 printStringNoNewline(inp, "The coordinate index for this dimension");
302 opt = prefix + "-coord-index";
304 coordIndexInput = get_eint(inp, opt, 1, wi);
306 /* The pull coordinate indices start at 1 in the input file, at 0 internally */
307 dimParams->coordIndex = coordIndexInput - 1;
311 printStringNoNewline(inp, "Start and end values for each coordinate dimension");
313 opt = prefix + "-start";
314 dimParams->origin = get_ereal(inp, opt, 0., wi);
315 opt = prefix + "-end";
316 dimParams->end = get_ereal(inp, opt, 0., wi);
320 printStringNoNewline(
321 inp, "The force constant for this coordinate (kJ/mol/nm^2 or kJ/mol/rad^2)");
323 opt = prefix + "-force-constant";
324 dimParams->forceConstant = get_ereal(inp, opt, 0, wi);
328 printStringNoNewline(inp, "Estimated diffusion constant (nm^2/ps, rad^2/ps or ps^-1)");
330 opt = prefix + "-diffusion";
331 dimParams->diffusion = get_ereal(inp, opt, 0, wi);
335 printStringNoNewline(inp,
336 "Diameter that needs to be sampled around a point before it is "
337 "considered covered. In FEP dimensions the cover diameter is "
338 "specified in lambda states.");
340 opt = prefix + "-cover-diameter";
341 dimParams->coverDiameter = get_ereal(inp, opt, 0, wi);
345 * Check the parameters of an AWH bias dimension.
347 * \param[in] prefix Prefix for dimension parameters.
348 * \param[in,out] dimParams AWH dimensional parameters.
349 * \param[in] ir Input parameter struct.
350 * \param[in,out] wi Struct for bookeeping warnings.
352 void checkDimParams(const std::string& prefix, AwhDimParams* dimParams, const t_inputrec* ir, warninp_t wi)
354 if (dimParams->eCoordProvider == eawhcoordproviderPULL)
359 "AWH biasing along a pull dimension is only compatible with COM pulling "
362 checkPullDimParams(prefix, dimParams, ir->pull, wi);
364 else if (dimParams->eCoordProvider == eawhcoordproviderFREE_ENERGY_LAMBDA)
366 if (ir->efep == efepNO)
369 "AWH biasing along a free energy lambda state dimension is only compatible "
370 "with free energy turned on");
372 checkFepLambdaDimParams(prefix, dimParams, ir->fepvals, ir->efep, wi);
377 "AWH biasing can only be applied to pull and free energy lambda state "
383 * Check consistency of input at the AWH bias level.
385 * \param[in] awhBiasParams AWH bias parameters.
386 * \param[in,out] wi Struct for bookkeeping warnings.
388 void checkInputConsistencyAwhBias(const AwhBiasParams& awhBiasParams, warninp_t wi)
390 /* Covering diameter and sharing warning. */
391 for (int d = 0; d < awhBiasParams.ndim; d++)
393 double coverDiameter = awhBiasParams.dimParams[d].coverDiameter;
394 if (awhBiasParams.shareGroup <= 0 && coverDiameter > 0)
397 "The covering diameter is only relevant to set for bias sharing simulations.");
403 * Read parameters of an AWH bias.
405 * \param[in,out] inp Input file entries.
406 * \param[in,out] awhBiasParams AWH dimensional parameters.
407 * \param[in] prefix Prefix for bias parameters.
408 * \param[in,out] wi Struct for bookeeping warnings.
409 * \param[in] bComment True if comments should be printed.
411 void readBiasParams(std::vector<t_inpfile>* inp,
412 AwhBiasParams* awhBiasParams,
413 const std::string& prefix,
419 printStringNoNewline(inp, "Estimated initial PMF error (kJ/mol)");
422 std::string opt = prefix + "-error-init";
423 awhBiasParams->errorInitial = get_ereal(inp, opt, 10, wi);
427 printStringNoNewline(inp,
428 "Growth rate of the reference histogram determining the bias update "
429 "size: exp-linear or linear");
431 opt = prefix + "-growth";
432 awhBiasParams->eGrowth = get_eeenum(inp, opt, eawhgrowth_names, wi);
436 printStringNoNewline(inp,
437 "Start the simulation by equilibrating histogram towards the target "
438 "distribution: no or yes");
440 opt = prefix + "-equilibrate-histogram";
441 awhBiasParams->equilibrateHistogram = (get_eeenum(inp, opt, yesno_names, wi) != 0);
445 printStringNoNewline(
446 inp, "Target distribution type: constant, cutoff, boltzmann or local-boltzmann");
448 opt = prefix + "-target";
449 awhBiasParams->eTarget = get_eeenum(inp, opt, eawhtarget_names, wi);
453 printStringNoNewline(inp,
454 "Boltzmann beta scaling factor for target distribution types "
455 "'boltzmann' and 'boltzmann-local'");
457 opt = prefix + "-target-beta-scaling";
458 awhBiasParams->targetBetaScaling = get_ereal(inp, opt, 0, wi);
462 printStringNoNewline(inp, "Free energy cutoff value for target distribution type 'cutoff'");
464 opt = prefix + "-target-cutoff";
465 awhBiasParams->targetCutoff = get_ereal(inp, opt, 0, wi);
469 printStringNoNewline(inp, "Initialize PMF and target with user data: no or yes");
471 opt = prefix + "-user-data";
472 awhBiasParams->bUserData = get_eeenum(inp, opt, yesno_names, wi);
476 printStringNoNewline(inp, "Group index to share the bias with, 0 means not shared");
478 opt = prefix + "-share-group";
479 awhBiasParams->shareGroup = get_eint(inp, opt, 0, wi);
483 printStringNoNewline(inp, "Dimensionality of the coordinate");
485 opt = prefix + "-ndim";
486 awhBiasParams->ndim = get_eint(inp, opt, 0, wi);
488 /* Check this before starting to read the AWH dimension parameters. */
489 if (awhBiasParams->ndim <= 0 || awhBiasParams->ndim > c_biasMaxNumDim)
491 gmx_fatal(FARGS, "%s (%d) needs to be > 0 and at most %d\n", opt.c_str(),
492 awhBiasParams->ndim, c_biasMaxNumDim);
494 snew(awhBiasParams->dimParams, awhBiasParams->ndim);
495 for (int d = 0; d < awhBiasParams->ndim; d++)
497 bComment = bComment && d == 0;
498 std::string prefixdim = prefix + formatString("-dim%d", d + 1);
499 readDimParams(inp, prefixdim, &awhBiasParams->dimParams[d], wi, bComment);
504 * Check the parameters of an AWH bias.
506 * \param[in] awhBiasParams AWH dimensional parameters.
507 * \param[in] prefix Prefix for bias parameters.
508 * \param[in] ir Input parameter struct.
509 * \param[in,out] wi Struct for bookeeping warnings.
511 void checkBiasParams(const AwhBiasParams* awhBiasParams, const std::string& prefix, const t_inputrec* ir, warninp_t wi)
513 std::string opt = prefix + "-error-init";
514 if (awhBiasParams->errorInitial <= 0)
516 gmx_fatal(FARGS, "%s needs to be > 0.", opt.c_str());
519 opt = prefix + "-equilibrate-histogram";
520 if (awhBiasParams->equilibrateHistogram && awhBiasParams->eGrowth != eawhgrowthEXP_LINEAR)
523 formatString("Option %s will only have an effect for histogram growth type '%s'.",
524 opt.c_str(), EAWHGROWTH(eawhgrowthEXP_LINEAR));
525 warning(wi, message);
528 if ((awhBiasParams->eTarget == eawhtargetLOCALBOLTZMANN)
529 && (awhBiasParams->eGrowth == eawhgrowthEXP_LINEAR))
531 auto message = formatString(
532 "Target type '%s' combined with histogram growth type '%s' is not "
533 "expected to give stable bias updates. You probably want to use growth type "
535 EAWHTARGET(eawhtargetLOCALBOLTZMANN), EAWHGROWTH(eawhgrowthEXP_LINEAR),
536 EAWHGROWTH(eawhgrowthLINEAR));
537 warning(wi, message);
540 opt = prefix + "-target-beta-scaling";
541 switch (awhBiasParams->eTarget)
543 case eawhtargetBOLTZMANN:
544 case eawhtargetLOCALBOLTZMANN:
545 if (awhBiasParams->targetBetaScaling < 0 || awhBiasParams->targetBetaScaling > 1)
547 gmx_fatal(FARGS, "%s = %g is not useful for target type %s.", opt.c_str(),
548 awhBiasParams->targetBetaScaling, EAWHTARGET(awhBiasParams->eTarget));
552 if (awhBiasParams->targetBetaScaling != 0)
556 "Value for %s (%g) set explicitly but will not be used for target type %s.",
557 opt.c_str(), awhBiasParams->targetBetaScaling,
558 EAWHTARGET(awhBiasParams->eTarget));
563 opt = prefix + "-target-cutoff";
564 switch (awhBiasParams->eTarget)
566 case eawhtargetCUTOFF:
567 if (awhBiasParams->targetCutoff <= 0)
569 gmx_fatal(FARGS, "%s = %g is not useful for target type %s.", opt.c_str(),
570 awhBiasParams->targetCutoff, EAWHTARGET(awhBiasParams->eTarget));
574 if (awhBiasParams->targetCutoff != 0)
578 "Value for %s (%g) set explicitly but will not be used for target type %s.",
579 opt.c_str(), awhBiasParams->targetCutoff, EAWHTARGET(awhBiasParams->eTarget));
584 opt = prefix + "-share-group";
585 if (awhBiasParams->shareGroup < 0)
587 warning_error(wi, "AWH bias share-group should be >= 0");
590 opt = prefix + "-ndim";
591 if (awhBiasParams->ndim <= 0 || awhBiasParams->ndim > c_biasMaxNumDim)
593 gmx_fatal(FARGS, "%s (%d) needs to be > 0 and at most %d\n", opt.c_str(),
594 awhBiasParams->ndim, c_biasMaxNumDim);
596 if (awhBiasParams->ndim > 2)
599 "For awh-dim > 2 the estimate based on the diffusion and the initial error is "
600 "currently only a rough guideline."
601 " You should verify its usefulness for your system before production runs!");
603 for (int d = 0; d < awhBiasParams->ndim; d++)
605 std::string prefixdim = prefix + formatString("-dim%d", d + 1);
606 checkDimParams(prefixdim, &awhBiasParams->dimParams[d], ir, wi);
609 /* Check consistencies here that cannot be checked at read time at a lower level. */
610 checkInputConsistencyAwhBias(*awhBiasParams, wi);
614 * Check consistency of input at the AWH level.
616 * \param[in] awhParams AWH parameters.
617 * \param[in,out] wi Struct for bookkeeping warnings.
619 void checkInputConsistencyAwh(const AwhParams& awhParams, warninp_t wi)
621 /* Each pull coord can map to at most 1 AWH coord.
622 * Check that we have a shared bias when requesting multisim sharing.
624 bool haveSharedBias = false;
625 for (int k1 = 0; k1 < awhParams.numBias; k1++)
627 const AwhBiasParams& awhBiasParams1 = awhParams.awhBiasParams[k1];
629 if (awhBiasParams1.shareGroup > 0)
631 haveSharedBias = true;
634 /* k1 is the reference AWH, k2 is the AWH we compare with (can be equal to k1) */
635 for (int k2 = k1; k2 < awhParams.numBias; k2++)
637 for (int d1 = 0; d1 < awhBiasParams1.ndim; d1++)
639 if (awhBiasParams1.dimParams[d1].eCoordProvider == eawhcoordproviderFREE_ENERGY_LAMBDA)
643 const AwhBiasParams& awhBiasParams2 = awhParams.awhBiasParams[k2];
645 /* d1 is the reference dimension of the reference AWH. d2 is the dim index of the AWH to compare with. */
646 for (int d2 = 0; d2 < awhBiasParams2.ndim; d2++)
648 if (awhBiasParams2.dimParams[d2].eCoordProvider == eawhcoordproviderFREE_ENERGY_LAMBDA)
652 /* Give an error if (d1, k1) is different from (d2, k2) but the pull coordinate is the same */
653 if ((d1 != d2 || k1 != k2)
654 && (awhBiasParams1.dimParams[d1].coordIndex == awhBiasParams2.dimParams[d2].coordIndex))
656 char errormsg[STRLEN];
658 "One pull coordinate (%d) cannot be mapped to two separate AWH "
659 "dimensions (awh%d-dim%d and awh%d-dim%d). "
660 "If this is really what you want to do you will have to duplicate "
661 "this pull coordinate.",
662 awhBiasParams1.dimParams[d1].coordIndex + 1, k1 + 1, d1 + 1, k2 + 1,
664 gmx_fatal(FARGS, "%s", errormsg);
671 if (awhParams.shareBiasMultisim && !haveSharedBias)
674 "Sharing of biases over multiple simulations is requested, but no bias is marked "
675 "as shared (share-group > 0)");
678 /* mdrun does not support this (yet), but will check again */
679 if (haveBiasSharingWithinSimulation(awhParams))
682 "You have shared biases within a single simulation, but mdrun does not support "
688 AwhParams* readAwhParams(std::vector<t_inpfile>* inp, warninp_t wi)
690 AwhParams* awhParams;
694 /* Parameters common for all biases */
696 printStringNoNewline(inp, "The way to apply the biasing potential: convolved or umbrella");
697 opt = "awh-potential";
698 awhParams->ePotential = get_eeenum(inp, opt, eawhpotential_names, wi);
700 printStringNoNewline(inp,
701 "The random seed used for sampling the umbrella center in the case of "
702 "umbrella type potential");
704 awhParams->seed = get_eint(inp, opt, -1, wi);
705 if (awhParams->seed == -1)
707 awhParams->seed = static_cast<int>(gmx::makeRandomSeed());
708 fprintf(stderr, "Setting the AWH bias MC random seed to %" PRId64 "\n", awhParams->seed);
711 printStringNoNewline(inp, "Data output interval in number of steps");
713 awhParams->nstOut = get_eint(inp, opt, 100000, wi);
715 printStringNoNewline(inp, "Coordinate sampling interval in number of steps");
716 opt = "awh-nstsample";
717 awhParams->nstSampleCoord = get_eint(inp, opt, 10, wi);
719 printStringNoNewline(inp, "Free energy and bias update interval in number of samples");
720 opt = "awh-nsamples-update";
721 awhParams->numSamplesUpdateFreeEnergy = get_eint(inp, opt, 10, wi);
723 printStringNoNewline(
724 inp, "When true, biases with share-group>0 are shared between multiple simulations");
725 opt = "awh-share-multisim";
726 awhParams->shareBiasMultisim = (get_eeenum(inp, opt, yesno_names, wi) != 0);
728 printStringNoNewline(inp, "The number of independent AWH biases");
730 awhParams->numBias = get_eint(inp, opt, 1, wi);
731 /* Check this before starting to read the AWH biases. */
732 if (awhParams->numBias <= 0)
734 gmx_fatal(FARGS, "%s needs to be an integer > 0", opt.c_str());
737 /* Read the parameters specific to each AWH bias */
738 snew(awhParams->awhBiasParams, awhParams->numBias);
740 for (int k = 0; k < awhParams->numBias; k++)
742 bool bComment = (k == 0);
743 std::string prefixawh = formatString("awh%d", k + 1);
744 readBiasParams(inp, &awhParams->awhBiasParams[k], prefixawh, wi, bComment);
750 void checkAwhParams(const AwhParams* awhParams, const t_inputrec* ir, warninp_t wi)
755 if (awhParams->nstOut <= 0)
757 auto message = formatString("Not writing AWH output with AWH (%s = %d) does not make sense",
758 opt.c_str(), awhParams->nstOut);
759 warning_error(wi, message);
761 /* This restriction can be removed by changing a flag of print_ebin() */
762 if (ir->nstenergy == 0 || awhParams->nstOut % ir->nstenergy != 0)
764 auto message = formatString("%s (%d) should be a multiple of nstenergy (%d)", opt.c_str(),
765 awhParams->nstOut, ir->nstenergy);
766 warning_error(wi, message);
769 opt = "awh-nsamples-update";
770 if (awhParams->numSamplesUpdateFreeEnergy <= 0)
772 warning_error(wi, opt + " needs to be an integer > 0");
775 for (int k = 0; k < awhParams->numBias; k++)
777 std::string prefixawh = formatString("awh%d", k + 1);
778 checkBiasParams(&awhParams->awhBiasParams[k], prefixawh, ir, wi);
781 /* Do a final consistency check before returning */
782 checkInputConsistencyAwh(*awhParams, wi);
784 if (ir->init_step != 0)
786 warning_error(wi, "With AWH init-step should be 0");
791 * Gets the period of a pull coordinate.
793 * \param[in] pullCoordParams The parameters for the pull coordinate.
794 * \param[in] pbc The PBC setup
795 * \param[in] intervalLength The length of the AWH interval for this pull coordinate
796 * \returns the period (or 0 if not periodic).
798 static double get_pull_coord_period(const t_pull_coord& pullCoordParams, const t_pbc& pbc, const real intervalLength)
802 if (pullCoordParams.eGeom == epullgDIR)
804 const real margin = 0.001;
805 // Make dims periodic when the interval covers > 95%
806 const real periodicFraction = 0.95;
808 // Check if the pull direction is along a box vector
809 for (int dim = 0; dim < pbc.ndim_ePBC; dim++)
811 const real boxLength = norm(pbc.box[dim]);
812 const real innerProduct = iprod(pullCoordParams.vec, pbc.box[dim]);
813 if (innerProduct >= (1 - margin) * boxLength && innerProduct <= (1 + margin) * boxLength)
815 if (intervalLength > (1 + margin) * boxLength)
818 "The AWH interval (%f nm) for a pull coordinate is larger than the "
820 intervalLength, boxLength);
823 if (intervalLength > periodicFraction * boxLength)
830 else if (pullCoordParams.eGeom == epullgDIHEDRAL)
832 /* The dihedral angle is periodic in -180 to 180 deg */
840 * Checks if the given interval is defined in the correct periodic interval.
842 * \param[in] origin Start value of interval.
843 * \param[in] end End value of interval.
844 * \param[in] period Period (or 0 if not periodic).
845 * \returns true if the end point values are in the correct periodic interval.
847 static bool intervalIsInPeriodicInterval(double origin, double end, double period)
849 return (period == 0) || (std::fabs(origin) <= 0.5 * period && std::fabs(end) <= 0.5 * period);
853 * Checks if a value is within an interval.
855 * \param[in] origin Start value of interval.
856 * \param[in] end End value of interval.
857 * \param[in] period Period (or 0 if not periodic).
858 * \param[in] value Value to check.
859 * \returns true if the value is within the interval.
861 static bool valueIsInInterval(double origin, double end, double period, double value)
869 /* The interval closes within the periodic interval */
870 bIn_interval = (value >= origin) && (value <= end);
874 /* The interval wraps around the periodic boundary */
875 bIn_interval = ((value >= origin) && (value <= 0.5 * period))
876 || ((value >= -0.5 * period) && (value <= end));
881 bIn_interval = (value >= origin) && (value <= end);
888 * Check if the starting configuration is consistent with the given interval.
890 * \param[in] awhParams AWH parameters.
891 * \param[in,out] wi Struct for bookeeping warnings.
893 static void checkInputConsistencyInterval(const AwhParams* awhParams, warninp_t wi)
895 for (int k = 0; k < awhParams->numBias; k++)
897 AwhBiasParams* awhBiasParams = &awhParams->awhBiasParams[k];
898 for (int d = 0; d < awhBiasParams->ndim; d++)
900 AwhDimParams* dimParams = &awhBiasParams->dimParams[d];
901 int coordIndex = dimParams->coordIndex;
902 double origin = dimParams->origin, end = dimParams->end, period = dimParams->period;
903 double coordValueInit = dimParams->coordValueInit;
905 if ((period == 0) && (origin > end))
908 "For the non-periodic pull coordinates awh%d-dim%d-start (%f) cannot be "
909 "larger than awh%d-dim%d-end (%f)",
910 k + 1, d + 1, origin, k + 1, d + 1, end);
913 /* Currently we assume symmetric periodic intervals, meaning we use [-period/2, period/2] as the reference interval.
914 Make sure the AWH interval is within this reference interval.
916 Note: we could fairly simply allow using a more general interval (e.g. [x, x + period]) but it complicates
917 things slightly and I don't see that there is a great need for it. It would also mean that the interval would
918 depend on AWH input. Also, for dihedral angles you would always want the reference interval to be -180, +180,
919 independent of AWH parameters.
921 if (!intervalIsInPeriodicInterval(origin, end, period))
924 "When using AWH with periodic pull coordinate geometries "
925 "awh%d-dim%d-start (%.8g) and "
926 "awh%d-dim%d-end (%.8g) should cover at most one period (%.8g) and take "
928 "minus half a period and plus half a period, i.e. in the interval [%.8g, "
930 k + 1, d + 1, origin, k + 1, d + 1, end, period, -0.5 * period, 0.5 * period);
933 /* Warn if the pull initial coordinate value is not in the grid */
934 if (!valueIsInInterval(origin, end, period, coordValueInit))
936 auto message = formatString(
937 "The initial coordinate value (%.8g) for pull coordinate index %d falls "
939 "of the sampling nterval awh%d-dim%d-start (%.8g) to awh%d-dim%d-end "
941 "This can lead to large initial forces pulling the coordinate towards the "
942 "sampling interval.",
943 coordValueInit, coordIndex + 1, k + 1, d + 1, origin, k + 1, d + 1, end);
944 warning(wi, message);
951 * Sets AWH parameters, for one AWH pull dimension.
953 * \param[in,out] dimParams AWH dimension parameters.
954 * \param[in] biasIndex The index of the bias containing this AWH pull dimension.
955 * \param[in] dimIndex The index of this AWH pull dimension.
956 * \param[in] pull_params Pull parameters.
957 * \param[in,out] pull_work Pull working struct to register AWH bias in.
958 * \param[in] pbc A pbc information structure.
959 * \param[in] compressibility Compressibility matrix for pressure coupling,
960 * pass all 0 without pressure coupling.
961 * \param[in,out] wi Struct for bookeeping warnings.
964 static void setStateDependentAwhPullDimParams(AwhDimParams* dimParams,
967 const pull_params_t* pull_params,
970 const tensor& compressibility,
973 const t_pull_coord& pullCoordParams = pull_params->coord[dimParams->coordIndex];
975 if (pullCoordParams.eGeom == epullgDIRPBC)
978 "AWH does not support pull geometry '%s'. "
979 "If the maximum distance between the groups is always "
980 "less than half the box size, "
981 "you can use geometry '%s' instead.",
982 EPULLGEOM(epullgDIRPBC), EPULLGEOM(epullgDIR));
985 dimParams->period = get_pull_coord_period(pullCoordParams, pbc, dimParams->end - dimParams->origin);
986 // We would like to check for scaling, but we don't have the full inputrec available here
987 if (dimParams->period > 0
988 && !(pullCoordParams.eGeom == epullgANGLE || pullCoordParams.eGeom == epullgDIHEDRAL))
990 bool coordIsScaled = false;
991 for (int d2 = 0; d2 < DIM; d2++)
993 if (pullCoordParams.vec[d2] != 0 && norm2(compressibility[d2]) != 0)
995 coordIsScaled = true;
1000 std::string mesg = gmx::formatString(
1001 "AWH dimension %d of bias %d is periodic with pull geometry '%s', "
1002 "while you should are applying pressure scaling to the "
1003 "corresponding box vector, this is not supported.",
1004 dimIndex + 1, biasIndex + 1, EPULLGEOM(pullCoordParams.eGeom));
1005 warning(wi, mesg.c_str());
1009 /* The initial coordinate value, converted to external user units. */
1010 dimParams->coordValueInit = get_pull_coord_value(pull_work, dimParams->coordIndex, &pbc);
1012 dimParams->coordValueInit *= pull_conversion_factor_internal2userinput(&pullCoordParams);
1015 void setStateDependentAwhParams(AwhParams* awhParams,
1016 const pull_params_t* pull_params,
1020 const tensor& compressibility,
1021 const t_grpopts* inputrecGroupOptions,
1022 const real initLambda,
1023 const gmx_mtop_t& mtop,
1026 /* The temperature is not really state depenendent but is not known
1027 * when read_awhParams is called (in get ir).
1028 * It is known first after do_index has been called in grompp.cpp.
1030 if (inputrecGroupOptions->ref_t == nullptr || inputrecGroupOptions->ref_t[0] <= 0)
1032 gmx_fatal(FARGS, "AWH biasing is only supported for temperatures > 0");
1034 for (int i = 1; i < inputrecGroupOptions->ngtc; i++)
1036 if (inputrecGroupOptions->ref_t[i] != inputrecGroupOptions->ref_t[0])
1039 "AWH biasing is currently only supported for identical temperatures for all "
1040 "temperature coupling groups");
1045 set_pbc(&pbc, pbcType, box);
1047 for (int k = 0; k < awhParams->numBias; k++)
1049 AwhBiasParams* awhBiasParams = &awhParams->awhBiasParams[k];
1050 for (int d = 0; d < awhBiasParams->ndim; d++)
1052 AwhDimParams* dimParams = &awhBiasParams->dimParams[d];
1053 if (dimParams->eCoordProvider == eawhcoordproviderPULL)
1055 setStateDependentAwhPullDimParams(dimParams, k, d, pull_params, pull_work, pbc,
1056 compressibility, wi);
1060 dimParams->coordValueInit = initLambda;
1061 checkFepLambdaDimDecouplingConsistency(mtop, wi);
1065 checkInputConsistencyInterval(awhParams, wi);
1067 /* Register AWH as external potential with pull (for AWH dimensions that use pull as
1068 * reaction coordinate provider) to check consistency. */
1069 Awh::registerAwhWithPull(*awhParams, pull_work);