2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012-2018, The GROMACS development team.
5 * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
45 #include "gromacs/domdec/domdec.h"
46 #include "gromacs/fileio/confio.h"
47 #include "gromacs/fileio/gmxfio.h"
48 #include "gromacs/fileio/xtcio.h"
49 #include "gromacs/gmxlib/network.h"
50 #include "gromacs/gmxlib/nrnb.h"
51 #include "gromacs/listed_forces/disre.h"
52 #include "gromacs/listed_forces/orires.h"
53 #include "gromacs/math/functions.h"
54 #include "gromacs/math/units.h"
55 #include "gromacs/math/vec.h"
56 #include "gromacs/mdlib/calcmu.h"
57 #include "gromacs/mdlib/constr.h"
58 #include "gromacs/mdlib/force.h"
59 #include "gromacs/mdlib/update.h"
60 #include "gromacs/mdtypes/enerdata.h"
61 #include "gromacs/mdtypes/forcerec.h"
62 #include "gromacs/mdtypes/inputrec.h"
63 #include "gromacs/mdtypes/md_enums.h"
64 #include "gromacs/mdtypes/mdatom.h"
65 #include "gromacs/mdtypes/state.h"
66 #include "gromacs/random/threefry.h"
67 #include "gromacs/random/uniformrealdistribution.h"
68 #include "gromacs/timing/wallcycle.h"
69 #include "gromacs/utility/fatalerror.h"
70 #include "gromacs/utility/gmxmpi.h"
71 #include "gromacs/utility/smalloc.h"
73 #include "expanded_internal.h"
75 static void init_df_history_weights(df_history_t* dfhist, const t_expanded* expand, int nlim)
78 dfhist->wl_delta = expand->init_wl_delta;
79 for (i = 0; i < nlim; i++)
81 dfhist->sum_weights[i] = expand->init_lambda_weights[i];
82 dfhist->sum_dg[i] = expand->init_lambda_weights[i];
86 /* Eventually should contain all the functions needed to initialize expanded ensemble
87 before the md loop starts */
88 void init_expanded_ensemble(gmx_bool bStateFromCP, const t_inputrec* ir, df_history_t* dfhist)
92 init_df_history_weights(dfhist, ir->expandedvals, ir->fepvals->n_lambda);
96 static void GenerateGibbsProbabilities(const real* ene, double* p_k, double* pks, int minfep, int maxfep)
103 maxene = ene[minfep];
104 /* find the maximum value */
105 for (i = minfep; i <= maxfep; i++)
112 /* find the denominator */
113 for (i = minfep; i <= maxfep; i++)
115 *pks += std::exp(ene[i] - maxene);
118 for (i = minfep; i <= maxfep; i++)
120 p_k[i] = std::exp(ene[i] - maxene) / *pks;
125 GenerateWeightedGibbsProbabilities(const real* ene, double* p_k, double* pks, int nlim, real* nvals, real delta)
134 for (i = 0; i < nlim; i++)
138 /* add the delta, since we need to make sure it's greater than zero, and
139 we need a non-arbitrary number? */
140 nene[i] = ene[i] + std::log(nvals[i] + delta);
144 nene[i] = ene[i] + std::log(nvals[i]);
148 /* find the maximum value */
150 for (i = 0; i < nlim; i++)
152 if (nene[i] > maxene)
158 /* subtract off the maximum, avoiding overflow */
159 for (i = 0; i < nlim; i++)
164 /* find the denominator */
165 for (i = 0; i < nlim; i++)
167 *pks += std::exp(nene[i]);
171 for (i = 0; i < nlim; i++)
173 p_k[i] = std::exp(nene[i]) / *pks;
178 static int FindMinimum(const real* min_metric, int N)
185 min_val = min_metric[0];
187 for (nval = 0; nval < N; nval++)
189 if (min_metric[nval] < min_val)
191 min_val = min_metric[nval];
198 static gmx_bool CheckHistogramRatios(int nhisto, const real* histo, real ratio)
206 for (i = 0; i < nhisto; i++)
213 /* no samples! is bad!*/
217 nmean /= static_cast<real>(nhisto);
220 for (i = 0; i < nhisto; i++)
222 /* make sure that all points are in the ratio < x < 1/ratio range */
223 if (!((histo[i] / nmean < 1.0 / ratio) && (histo[i] / nmean > ratio)))
232 static gmx_bool CheckIfDoneEquilibrating(int nlim, const t_expanded* expand, const df_history_t* dfhist, int64_t step)
236 gmx_bool bDoneEquilibrating = TRUE;
239 /* If we are doing slow growth to get initial values, we haven't finished equilibrating */
240 if (expand->lmc_forced_nstart > 0)
242 for (i = 0; i < nlim; i++)
244 if (dfhist->n_at_lam[i]
245 < expand->lmc_forced_nstart) /* we are still doing the initial sweep, so we're
246 definitely not done equilibrating*/
248 bDoneEquilibrating = FALSE;
255 /* assume we have equilibrated the weights, then check to see if any of the conditions are not met */
256 bDoneEquilibrating = TRUE;
258 /* calculate the total number of samples */
259 switch (expand->elmceq)
262 /* We have not equilibrated, and won't, ever. */
263 bDoneEquilibrating = FALSE;
266 /* we have equilibrated -- we're done */
267 bDoneEquilibrating = TRUE;
270 /* first, check if we are equilibrating by steps, if we're still under */
271 if (step < expand->equil_steps)
273 bDoneEquilibrating = FALSE;
278 for (i = 0; i < nlim; i++)
280 totalsamples += dfhist->n_at_lam[i];
282 if (totalsamples < expand->equil_samples)
284 bDoneEquilibrating = FALSE;
288 for (i = 0; i < nlim; i++)
290 if (dfhist->n_at_lam[i]
291 < expand->equil_n_at_lam) /* we are still doing the initial sweep, so we're
292 definitely not done equilibrating*/
294 bDoneEquilibrating = FALSE;
300 if (EWL(expand->elamstats)) /* This check is in readir as well, but
303 if (dfhist->wl_delta > expand->equil_wl_delta)
305 bDoneEquilibrating = FALSE;
310 /* we can use the flatness as a judge of good weights, as long as
311 we're not doing minvar, or Wang-Landau.
312 But turn off for now until we figure out exactly how we do this.
315 if (!(EWL(expand->elamstats) || expand->elamstats == elamstatsMINVAR))
317 /* we want to use flatness -avoiding- the forced-through samples. Plus, we need
318 to convert to floats for this histogram function. */
321 snew(modhisto, nlim);
322 for (i = 0; i < nlim; i++)
324 modhisto[i] = 1.0 * (dfhist->n_at_lam[i] - expand->lmc_forced_nstart);
326 bIfFlat = CheckHistogramRatios(nlim, modhisto, expand->equil_ratio);
330 bDoneEquilibrating = FALSE;
334 default: bDoneEquilibrating = TRUE; break;
337 return bDoneEquilibrating;
340 static gmx_bool UpdateWeights(int nlim,
342 df_history_t* dfhist,
344 const real* scaled_lamee,
345 const real* weighted_lamee,
348 gmx_bool bSufficientSamples;
349 real acceptanceWeight;
351 int min_nvalm, min_nvalp, maxc;
352 real omega_m1_0, omega_p1_0;
353 real zero_sum_weights;
354 real *omegam_array, *weightsm_array, *omegap_array, *weightsp_array, *varm_array, *varp_array,
355 *dwp_array, *dwm_array;
356 real clam_varm, clam_varp, clam_osum, clam_weightsm, clam_weightsp, clam_minvar;
357 real * lam_variance, *lam_dg;
361 /* Future potential todos for this function (see #3848):
362 * - Update the names in the dhist structure to be clearer. Not done for now since this
363 * a bugfix update and we are mininizing other code changes.
364 * - Modularize the code some more.
365 * - potentially merge with accelerated weight histogram functionality, since it's very similar.
367 /* if we have equilibrated the expanded ensemble weights, we are not updating them, so exit now */
373 if (CheckIfDoneEquilibrating(nlim, expand, dfhist, step))
375 dfhist->bEquil = TRUE;
376 /* zero out the visited states so we know how many equilibrated states we have
378 for (i = 0; i < nlim; i++)
380 dfhist->n_at_lam[i] = 0;
385 /* If we reached this far, we have not equilibrated yet, keep on
386 going resetting the weights */
388 if (EWL(expand->elamstats))
390 if (expand->elamstats == elamstatsWL) /* Using standard Wang-Landau for weight updates */
392 dfhist->sum_weights[fep_state] -= dfhist->wl_delta;
393 dfhist->wl_histo[fep_state] += 1.0;
395 else if (expand->elamstats == elamstatsWWL)
396 /* Using weighted Wang-Landau for weight updates.
397 * Very closly equivalent to accelerated weight histogram approach
398 * applied to expanded ensemble. */
402 /* first increment count */
403 GenerateGibbsProbabilities(weighted_lamee, p_k, &pks, 0, nlim - 1);
404 for (i = 0; i < nlim; i++)
406 dfhist->wl_histo[i] += static_cast<real>(p_k[i]);
409 /* then increment weights (uses count) */
411 GenerateWeightedGibbsProbabilities(
412 weighted_lamee, p_k, &pks, nlim, dfhist->wl_histo, dfhist->wl_delta);
414 for (i = 0; i < nlim; i++)
416 dfhist->sum_weights[i] -= dfhist->wl_delta * static_cast<real>(p_k[i]);
418 /* Alternate definition, using logarithms. Shouldn't make very much difference! */
423 di = (real)1.0 + dfhist->wl_delta*(real)p_k[i];
424 dfhist->sum_weights[i] -= log(di);
430 zero_sum_weights = dfhist->sum_weights[0];
431 for (i = 0; i < nlim; i++)
433 dfhist->sum_weights[i] -= zero_sum_weights;
437 if (expand->elamstats == elamstatsBARKER || expand->elamstats == elamstatsMETROPOLIS
438 || expand->elamstats == elamstatsMINVAR)
440 maxc = 2 * expand->c_range + 1;
443 snew(lam_variance, nlim);
445 snew(omegap_array, maxc);
446 snew(weightsp_array, maxc);
447 snew(varp_array, maxc);
448 snew(dwp_array, maxc);
450 snew(omegam_array, maxc);
451 snew(weightsm_array, maxc);
452 snew(varm_array, maxc);
453 snew(dwm_array, maxc);
455 /* unpack the values of the free energy differences and the
456 * variance in their estimates between nearby lambdas. We will
457 * only actually update 2 of these, the state we are currently
458 * at and the one we end up moving to
461 for (i = 0; i < nlim - 1; i++)
462 { /* only through the second to last */
463 lam_dg[i] = dfhist->sum_dg[i + 1] - dfhist->sum_dg[i];
465 gmx::square(dfhist->sum_variance[i + 1]) - gmx::square(dfhist->sum_variance[i]);
468 /* accumulate running averages of thermodynamic averages for Bennett Acceptance Ratio-based
469 * estimates of the free energy .
470 * Rather than peforming self-consistent estimation of the free energies at each step,
471 * we keep track of an array of possible different free energies (cnvals),
472 * and we self-consistently choose the best one. The one that leads to a free energy estimate
473 * that is closest to itself is the best estimate of the free energy. It is essentially a
474 * parallellized version of self-consistent iteration. maxc is the number of these constants. */
476 for (int nval = 0; nval < maxc; nval++)
478 const real cnval = static_cast<real>(nval - expand->c_range);
480 /* Compute acceptance criterion weight to the state below this one for use in averages.
481 * Note we do not have to have just moved from that state to use this free energy
482 * estimate; these are essentially "virtual" moves. */
486 const auto lambdaEnergyDifference =
487 cnval - (scaled_lamee[fep_state] - scaled_lamee[fep_state - 1]);
489 gmx::calculateAcceptanceWeight(expand->elamstats, lambdaEnergyDifference);
490 dfhist->accum_m[fep_state][nval] += acceptanceWeight;
491 dfhist->accum_m2[fep_state][nval] += acceptanceWeight * acceptanceWeight;
494 // Compute acceptance criterion weight to transition to the next state
495 if (fep_state < nlim - 1)
497 const auto lambdaEnergyDifference =
498 -cnval + (scaled_lamee[fep_state + 1] - scaled_lamee[fep_state]);
500 gmx::calculateAcceptanceWeight(expand->elamstats, lambdaEnergyDifference);
501 dfhist->accum_p[fep_state][nval] += acceptanceWeight;
502 dfhist->accum_p2[fep_state][nval] += acceptanceWeight * acceptanceWeight;
505 /* Determination of Metropolis transition and Barker transition weights */
507 int numObservationsCurrentState = dfhist->n_at_lam[fep_state];
508 /* determine the number of observations above and below the current state */
509 int numObservationsLowerState = 0;
512 numObservationsLowerState = dfhist->n_at_lam[fep_state - 1];
514 int numObservationsHigherState = 0;
515 if (fep_state < nlim - 1)
517 numObservationsHigherState = dfhist->n_at_lam[fep_state + 1];
520 /* Calculate the biases for each expanded ensemble state that minimize the total
521 * variance, as implemented in Martinez-Veracoechea and Escobedo,
522 * J. Phys. Chem. B 2008, 112, 8120-8128
524 * The variance associated with the free energy estimate between two states i and j
526 * Var(i,j) = {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1} / numObservations(i->j)
527 * + {avg[xi(j->i)^2] / avg[xi(j->i)]^2 - 1} / numObservations(j->i)
528 * where xi(i->j) is the acceptance factor / weight associated with moving from state i to j
529 * As we are calculating the acceptance factor to the neighbors every time we're visiting
530 * a state, numObservations(i->j) == numObservations(i) and numObservations(j->i) == numObservations(j)
533 /* Accumulation of acceptance weight averages between the current state and the
534 * states +1 (p1) and -1 (m1), averaged at current state (0)
536 real avgAcceptanceCurrentToLower = 0;
537 real avgAcceptanceCurrentToHigher = 0;
538 /* Accumulation of acceptance weight averages quantities between states 0
539 * and states +1 and -1, squared
541 real avgAcceptanceCurrentToLowerSquared = 0;
542 real avgAcceptanceCurrentToHigherSquared = 0;
543 /* Accumulation of free energy quantities from lower state (m1) to current state (0) and squared */
544 real avgAcceptanceLowerToCurrent = 0;
545 real avgAcceptanceLowerToCurrentSquared = 0;
546 /* Accumulation of free energy quantities from upper state (p1) to current state (0) and squared */
547 real avgAcceptanceHigherToCurrent = 0;
548 real avgAcceptanceHigherToCurrentSquared = 0;
550 if (numObservationsCurrentState > 0)
552 avgAcceptanceCurrentToLower = dfhist->accum_m[fep_state][nval] / numObservationsCurrentState;
553 avgAcceptanceCurrentToHigher =
554 dfhist->accum_p[fep_state][nval] / numObservationsCurrentState;
555 avgAcceptanceCurrentToLowerSquared =
556 dfhist->accum_m2[fep_state][nval] / numObservationsCurrentState;
557 avgAcceptanceCurrentToHigherSquared =
558 dfhist->accum_p2[fep_state][nval] / numObservationsCurrentState;
561 if ((fep_state > 0) && (numObservationsLowerState > 0))
563 avgAcceptanceLowerToCurrent =
564 dfhist->accum_p[fep_state - 1][nval] / numObservationsLowerState;
565 avgAcceptanceLowerToCurrentSquared =
566 dfhist->accum_p2[fep_state - 1][nval] / numObservationsLowerState;
569 if ((fep_state < nlim - 1) && (numObservationsHigherState > 0))
571 avgAcceptanceHigherToCurrent =
572 dfhist->accum_m[fep_state + 1][nval] / numObservationsHigherState;
573 avgAcceptanceHigherToCurrentSquared =
574 dfhist->accum_m2[fep_state + 1][nval] / numObservationsHigherState;
576 /* These are accumulation of positive values (see definition of acceptance functions
577 * above), or of squares of positive values.
578 * We're taking this for granted in the following calculation, so make sure
579 * here that nothing weird happened. Although technically all values should be positive,
580 * because of floating point precisions, they might be numerically zero. */
582 avgAcceptanceCurrentToLower >= 0 && avgAcceptanceCurrentToLowerSquared >= 0
583 && avgAcceptanceCurrentToHigher >= 0
584 && avgAcceptanceCurrentToHigherSquared >= 0 && avgAcceptanceLowerToCurrent >= 0
585 && avgAcceptanceLowerToCurrentSquared >= 0 && avgAcceptanceHigherToCurrent >= 0
586 && avgAcceptanceHigherToCurrentSquared >= 0,
587 "By definition, the acceptance factors should all be nonnegative.");
589 real varianceCurrentToLower = 0;
590 real varianceCurrentToHigher = 0;
591 real weightDifferenceToLower = 0;
592 real weightDifferenceToHigher = 0;
593 real varianceToLower = 0;
594 real varianceToHigher = 0;
598 if (numObservationsCurrentState > 0)
600 /* Calculate {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1}
602 * Note that if avg[xi(i->j)] == 0, also avg[xi(i->j)^2] == 0 (since the
603 * acceptances are all positive!), and hence
604 * {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1} -> 0 for avg[xi(i->j)] -> 0
605 * We're catching that case explicitly to avoid numerical
606 * problems dividing by zero when the overlap between states is small (#3304)
608 if (avgAcceptanceCurrentToLower > 0)
610 varianceCurrentToLower =
611 avgAcceptanceCurrentToLowerSquared
612 / (avgAcceptanceCurrentToLower * avgAcceptanceCurrentToLower)
615 if (numObservationsLowerState > 0)
617 /* Calculate {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1}
619 * Note that if avg[xi(i->j)] == 0, also avg[xi(i->j)^2] == 0 (since the
620 * acceptances are all positive!), and hence
621 * {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1} -> 0 for avg[xi(i->j)] -> 0
622 * We're catching that case explicitly to avoid numerical
623 * problems dividing by zero when the overlap between states is small (#3304)
625 real varianceLowerToCurrent = 0;
626 if (avgAcceptanceLowerToCurrent > 0)
628 varianceLowerToCurrent =
629 avgAcceptanceLowerToCurrentSquared
630 / (avgAcceptanceLowerToCurrent * avgAcceptanceLowerToCurrent)
633 /* Free energy difference to the state one state lower */
634 /* if these either of these quantities are zero, the energies are */
635 /* way too large for the dynamic range. We need an alternate guesstimate */
636 if ((avgAcceptanceCurrentToLower == 0) || (avgAcceptanceLowerToCurrent == 0))
638 weightDifferenceToLower =
639 (scaled_lamee[fep_state] - scaled_lamee[fep_state - 1]);
643 weightDifferenceToLower = (std::log(avgAcceptanceCurrentToLower)
644 - std::log(avgAcceptanceLowerToCurrent))
647 /* Variance of the free energy difference to the one state lower */
649 (1.0 / numObservationsCurrentState) * (varianceCurrentToLower)
650 + (1.0 / numObservationsLowerState) * (varianceLowerToCurrent);
655 if (fep_state < nlim - 1)
657 if (numObservationsCurrentState > 0)
659 /* Calculate {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1}
661 * Note that if avg[xi(i->j)] == 0, also avg[xi(i->j)^2] == 0 (since the
662 * acceptances are all positive!), and hence
663 * {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1} -> 0 for avg[xi(i->j)] -> 0
664 * We're catching that case explicitly to avoid numerical
665 * problems dividing by zero when the overlap between states is small (#3304)
668 if (avgAcceptanceCurrentToHigher < 0)
670 varianceCurrentToHigher =
671 avgAcceptanceCurrentToHigherSquared
672 / (avgAcceptanceCurrentToHigher * avgAcceptanceCurrentToHigher)
675 if (numObservationsHigherState > 0)
677 /* Calculate {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1}
679 * Note that if avg[xi(i->j)] == 0, also avg[xi(i->j)^2] == 0 (since the
680 * acceptances are all positive!), and hence
681 * {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1} -> 0 for avg[xi(i->j)] -> 0
682 * We're catching that case explicitly to avoid numerical
683 * problems dividing by zero when the overlap between states is small (#3304)
685 real varianceHigherToCurrent = 0;
686 if (avgAcceptanceHigherToCurrent > 0)
688 varianceHigherToCurrent =
689 avgAcceptanceHigherToCurrentSquared
690 / (avgAcceptanceHigherToCurrent * avgAcceptanceHigherToCurrent)
693 /* Free energy difference to the state one state higher */
694 /* if these either of these quantities are zero, the energies are */
695 /* way too large for the dynamic range. We need an alternate guesstimate */
696 if ((avgAcceptanceHigherToCurrent == 0) || (avgAcceptanceCurrentToHigher == 0))
698 weightDifferenceToHigher =
699 (scaled_lamee[fep_state + 1] - scaled_lamee[fep_state]);
703 weightDifferenceToHigher = (std::log(avgAcceptanceHigherToCurrent)
704 - std::log(avgAcceptanceCurrentToHigher))
707 /* Variance of the free energy difference to the one state higher */
709 (1.0 / numObservationsHigherState) * (varianceHigherToCurrent)
710 + (1.0 / numObservationsCurrentState) * (varianceCurrentToHigher);
715 if (numObservationsCurrentState > 0)
717 omegam_array[nval] = varianceCurrentToLower;
721 omegam_array[nval] = 0;
723 weightsm_array[nval] = weightDifferenceToLower;
724 varm_array[nval] = varianceToLower;
725 if (numObservationsLowerState > 0)
728 fabs((cnval + std::log((1.0 * numObservationsCurrentState) / numObservationsLowerState))
729 - lam_dg[fep_state - 1]);
733 dwm_array[nval] = std::fabs(cnval - lam_dg[fep_state - 1]);
736 if (numObservationsCurrentState > 0)
738 omegap_array[nval] = varianceCurrentToHigher;
742 omegap_array[nval] = 0;
744 weightsp_array[nval] = weightDifferenceToHigher;
745 varp_array[nval] = varianceToHigher;
746 if ((numObservationsHigherState > 0) && (numObservationsCurrentState > 0))
749 fabs((cnval + std::log((1.0 * numObservationsHigherState) / numObservationsCurrentState))
750 - lam_dg[fep_state]);
754 dwp_array[nval] = std::fabs(cnval - lam_dg[fep_state]);
758 /* find the free energy estimate closest to the guessed weight's value */
760 min_nvalm = FindMinimum(dwm_array, maxc);
761 omega_m1_0 = omegam_array[min_nvalm];
762 clam_weightsm = weightsm_array[min_nvalm];
763 clam_varm = varm_array[min_nvalm];
765 min_nvalp = FindMinimum(dwp_array, maxc);
766 omega_p1_0 = omegap_array[min_nvalp];
767 clam_weightsp = weightsp_array[min_nvalp];
768 clam_varp = varp_array[min_nvalp];
770 clam_osum = omega_m1_0 + omega_p1_0;
774 clam_minvar = 0.5 * std::log(clam_osum);
779 lam_dg[fep_state - 1] = clam_weightsm;
780 lam_variance[fep_state - 1] = clam_varm;
783 if (fep_state < nlim - 1)
785 lam_dg[fep_state] = clam_weightsp;
786 lam_variance[fep_state] = clam_varp;
789 if (expand->elamstats == elamstatsMINVAR)
791 bSufficientSamples = TRUE;
792 /* make sure the number of samples in each state are all
793 * past a user-specified threshold
795 for (i = 0; i < nlim; i++)
797 if (dfhist->n_at_lam[i] < expand->minvarmin)
799 bSufficientSamples = FALSE;
802 if (bSufficientSamples)
804 dfhist->sum_minvar[fep_state] = clam_minvar;
807 for (i = 0; i < nlim; i++)
809 dfhist->sum_minvar[i] += (expand->minvar_const - clam_minvar);
811 expand->minvar_const = clam_minvar;
812 dfhist->sum_minvar[fep_state] = 0.0;
816 dfhist->sum_minvar[fep_state] -= expand->minvar_const;
821 /* we need to rezero minvar now, since it could change at fep_state = 0 */
822 dfhist->sum_dg[0] = 0.0;
823 dfhist->sum_variance[0] = 0.0;
824 dfhist->sum_weights[0] = dfhist->sum_dg[0] + dfhist->sum_minvar[0]; /* should be zero */
826 for (i = 1; i < nlim; i++)
828 dfhist->sum_dg[i] = lam_dg[i - 1] + dfhist->sum_dg[i - 1];
829 dfhist->sum_variance[i] =
830 std::sqrt(lam_variance[i - 1] + gmx::square(dfhist->sum_variance[i - 1]));
831 dfhist->sum_weights[i] = dfhist->sum_dg[i] + dfhist->sum_minvar[i];
838 sfree(weightsm_array);
843 sfree(weightsp_array);
850 static int ChooseNewLambda(int nlim,
851 const t_expanded* expand,
852 df_history_t* dfhist,
854 const real* weighted_lamee,
859 /* Choose new lambda value, and update transition matrix */
861 int i, ifep, minfep, maxfep, lamnew, lamtrial, starting_fep_state;
862 real r1, r2, de, trialprob, tprob = 0;
863 double * propose, *accept, *remainder;
866 gmx::ThreeFry2x64<0> rng(
867 seed, gmx::RandomDomain::ExpandedEnsemble); // We only draw once, so zero bits internal counter is fine
868 gmx::UniformRealDistribution<real> dist;
870 starting_fep_state = fep_state;
871 lamnew = fep_state; /* so that there is a default setting -- stays the same */
873 if (!EWL(expand->elamstats)) /* ignore equilibrating the weights if using WL */
875 if ((expand->lmc_forced_nstart > 0) && (dfhist->n_at_lam[nlim - 1] <= expand->lmc_forced_nstart))
877 /* Use a marching method to run through the lambdas and get preliminary free energy data,
878 before starting 'free' sampling. We start free sampling when we have enough at each lambda */
880 /* if we have enough at this lambda, move on to the next one */
882 if (dfhist->n_at_lam[fep_state] == expand->lmc_forced_nstart)
884 lamnew = fep_state + 1;
885 if (lamnew == nlim) /* whoops, stepped too far! */
900 snew(remainder, nlim);
902 for (i = 0; i < expand->lmc_repeats; i++)
904 rng.restart(step, i);
907 for (ifep = 0; ifep < nlim; ifep++)
913 if ((expand->elmcmove == elmcmoveGIBBS) || (expand->elmcmove == elmcmoveMETGIBBS))
915 /* use the Gibbs sampler, with restricted range */
916 if (expand->gibbsdeltalam < 0)
923 minfep = fep_state - expand->gibbsdeltalam;
924 maxfep = fep_state + expand->gibbsdeltalam;
929 if (maxfep > nlim - 1)
935 GenerateGibbsProbabilities(weighted_lamee, p_k, &pks, minfep, maxfep);
937 if (expand->elmcmove == elmcmoveGIBBS)
939 for (ifep = minfep; ifep <= maxfep; ifep++)
941 propose[ifep] = p_k[ifep];
946 for (lamnew = minfep; lamnew <= maxfep; lamnew++)
948 if (r1 <= p_k[lamnew])
955 else if (expand->elmcmove == elmcmoveMETGIBBS)
958 /* Metropolized Gibbs sampling */
959 for (ifep = minfep; ifep <= maxfep; ifep++)
961 remainder[ifep] = 1 - p_k[ifep];
964 /* find the proposal probabilities */
966 if (remainder[fep_state] == 0)
968 /* only the current state has any probability */
969 /* we have to stay at the current state */
974 for (ifep = minfep; ifep <= maxfep; ifep++)
976 if (ifep != fep_state)
978 propose[ifep] = p_k[ifep] / remainder[fep_state];
987 for (lamtrial = minfep; lamtrial <= maxfep; lamtrial++)
989 pnorm = p_k[lamtrial] / remainder[fep_state];
990 if (lamtrial != fep_state)
1000 /* we have now selected lamtrial according to p(lamtrial)/1-p(fep_state) */
1002 /* trial probability is min{1,\frac{1 - p(old)}{1-p(new)} MRS 1/8/2008 */
1003 trialprob = (remainder[fep_state]) / (remainder[lamtrial]);
1004 if (trialprob < tprob)
1019 /* now figure out the acceptance probability for each */
1020 for (ifep = minfep; ifep <= maxfep; ifep++)
1023 if (remainder[ifep] != 0)
1025 trialprob = (remainder[fep_state]) / (remainder[ifep]);
1029 trialprob = 1.0; /* this state is the only choice! */
1031 if (trialprob < tprob)
1035 /* probability for fep_state=0, but that's fine, it's never proposed! */
1036 accept[ifep] = tprob;
1040 if (lamnew > maxfep)
1042 /* it's possible some rounding is failing */
1043 if (gmx_within_tol(remainder[fep_state], 0, 50 * GMX_DOUBLE_EPS))
1045 /* numerical rounding error -- no state other than the original has weight */
1050 /* probably not a numerical issue */
1052 int nerror = 200 + (maxfep - minfep + 1) * 60;
1054 snew(errorstr, nerror);
1055 /* if its greater than maxfep, then something went wrong -- probably underflow
1056 in the calculation of sum weights. Generated detailed info for failure */
1059 "Something wrong in choosing new lambda state with a Gibbs move -- "
1060 "probably underflow in weight determination.\nDenominator is: "
1061 "%3d%17.10e\n i dE numerator weights\n",
1064 for (ifep = minfep; ifep <= maxfep; ifep++)
1066 loc += sprintf(&errorstr[loc],
1067 "%3d %17.10e%17.10e%17.10e\n",
1069 weighted_lamee[ifep],
1071 dfhist->sum_weights[ifep]);
1073 gmx_fatal(FARGS, "%s", errorstr);
1077 else if ((expand->elmcmove == elmcmoveMETROPOLIS) || (expand->elmcmove == elmcmoveBARKER))
1079 /* use the metropolis sampler with trial +/- 1 */
1085 lamtrial = fep_state;
1089 lamtrial = fep_state - 1;
1094 if (fep_state == nlim - 1)
1096 lamtrial = fep_state;
1100 lamtrial = fep_state + 1;
1104 de = weighted_lamee[lamtrial] - weighted_lamee[fep_state];
1105 if (expand->elmcmove == elmcmoveMETROPOLIS)
1110 tprob = std::exp(de);
1112 propose[fep_state] = 0;
1113 propose[lamtrial] = 1.0; /* note that this overwrites the above line if fep_state = ntrial, which only occurs at the ends */
1115 1.0; /* doesn't actually matter, never proposed unless fep_state = ntrial, in which case it's 1.0 anyway */
1116 accept[lamtrial] = tprob;
1118 else if (expand->elmcmove == elmcmoveBARKER)
1120 if (de > 0) /* Numerically stable version */
1122 tprob = 1.0 / (1.0 + std::exp(-de));
1126 tprob = std::exp(de) / (std::exp(de) + 1.0);
1128 propose[fep_state] = (1 - tprob);
1129 propose[lamtrial] +=
1130 tprob; /* we add, to account for the fact that at the end, they might be the same point */
1131 accept[fep_state] = 1.0;
1132 accept[lamtrial] = 1.0;
1146 for (ifep = 0; ifep < nlim; ifep++)
1148 dfhist->Tij[fep_state][ifep] += propose[ifep] * accept[ifep];
1149 dfhist->Tij[fep_state][fep_state] += propose[ifep] * (1.0 - accept[ifep]);
1154 dfhist->Tij_empirical[starting_fep_state][lamnew] += 1.0;
1163 /* print out the weights to the log, along with current state */
1164 void PrintFreeEnergyInfoToFile(FILE* outfile,
1165 const t_lambda* fep,
1166 const t_expanded* expand,
1167 const t_simtemp* simtemp,
1168 const df_history_t* dfhist,
1173 int nlim, i, ifep, jfep;
1174 real dw, dg, dv, Tprint;
1175 const char* print_names[efptNR] = { " FEPL", "MassL", "CoulL", " VdwL",
1176 "BondL", "RestT", "Temp.(K)" };
1177 gmx_bool bSimTemp = FALSE;
1179 nlim = fep->n_lambda;
1180 if (simtemp != nullptr)
1185 if (step % frequency == 0)
1187 fprintf(outfile, " MC-lambda information\n");
1188 if (EWL(expand->elamstats) && (!(dfhist->bEquil)))
1190 fprintf(outfile, " Wang-Landau incrementor is: %11.5g\n", dfhist->wl_delta);
1192 fprintf(outfile, " N");
1193 for (i = 0; i < efptNR; i++)
1195 if (fep->separate_dvdl[i])
1197 fprintf(outfile, "%7s", print_names[i]);
1199 else if ((i == efptTEMPERATURE) && bSimTemp)
1201 fprintf(outfile, "%10s", print_names[i]); /* more space for temperature formats */
1204 fprintf(outfile, " Count ");
1205 if (expand->elamstats == elamstatsMINVAR)
1207 fprintf(outfile, "W(in kT) G(in kT) dG(in kT) dV(in kT)\n");
1211 fprintf(outfile, "G(in kT) dG(in kT)\n");
1213 for (ifep = 0; ifep < nlim; ifep++)
1215 if (ifep == nlim - 1)
1223 dw = dfhist->sum_weights[ifep + 1] - dfhist->sum_weights[ifep];
1224 dg = dfhist->sum_dg[ifep + 1] - dfhist->sum_dg[ifep];
1225 dv = std::sqrt(gmx::square(dfhist->sum_variance[ifep + 1])
1226 - gmx::square(dfhist->sum_variance[ifep]));
1228 fprintf(outfile, "%3d", (ifep + 1));
1229 for (i = 0; i < efptNR; i++)
1231 if (fep->separate_dvdl[i])
1233 fprintf(outfile, "%7.3f", fep->all_lambda[i][ifep]);
1235 else if (i == efptTEMPERATURE && bSimTemp)
1237 fprintf(outfile, "%9.3f", simtemp->temperatures[ifep]);
1240 if (EWL(expand->elamstats)
1241 && (!(dfhist->bEquil))) /* if performing WL and still haven't equilibrated */
1243 if (expand->elamstats == elamstatsWL)
1245 fprintf(outfile, " %8d", static_cast<int>(dfhist->wl_histo[ifep]));
1249 fprintf(outfile, " %8.3f", dfhist->wl_histo[ifep]);
1252 else /* we have equilibrated weights */
1254 fprintf(outfile, " %8d", dfhist->n_at_lam[ifep]);
1256 if (expand->elamstats == elamstatsMINVAR)
1259 " %10.5f %10.5f %10.5f %10.5f",
1260 dfhist->sum_weights[ifep],
1261 dfhist->sum_dg[ifep],
1267 fprintf(outfile, " %10.5f %10.5f", dfhist->sum_weights[ifep], dw);
1269 if (ifep == fep_state)
1271 fprintf(outfile, " <<\n");
1275 fprintf(outfile, " \n");
1278 fprintf(outfile, "\n");
1280 if ((step % expand->nstTij == 0) && (expand->nstTij > 0) && (step > 0))
1282 fprintf(outfile, " Transition Matrix\n");
1283 for (ifep = 0; ifep < nlim; ifep++)
1285 fprintf(outfile, "%12d", (ifep + 1));
1287 fprintf(outfile, "\n");
1288 for (ifep = 0; ifep < nlim; ifep++)
1290 for (jfep = 0; jfep < nlim; jfep++)
1292 if (dfhist->n_at_lam[ifep] > 0)
1294 if (expand->bSymmetrizedTMatrix)
1296 Tprint = (dfhist->Tij[ifep][jfep] + dfhist->Tij[jfep][ifep])
1297 / (dfhist->n_at_lam[ifep] + dfhist->n_at_lam[jfep]);
1301 Tprint = (dfhist->Tij[ifep][jfep]) / (dfhist->n_at_lam[ifep]);
1308 fprintf(outfile, "%12.8f", Tprint);
1310 fprintf(outfile, "%3d\n", (ifep + 1));
1313 fprintf(outfile, " Empirical Transition Matrix\n");
1314 for (ifep = 0; ifep < nlim; ifep++)
1316 fprintf(outfile, "%12d", (ifep + 1));
1318 fprintf(outfile, "\n");
1319 for (ifep = 0; ifep < nlim; ifep++)
1321 for (jfep = 0; jfep < nlim; jfep++)
1323 if (dfhist->n_at_lam[ifep] > 0)
1325 if (expand->bSymmetrizedTMatrix)
1327 Tprint = (dfhist->Tij_empirical[ifep][jfep] + dfhist->Tij_empirical[jfep][ifep])
1328 / (dfhist->n_at_lam[ifep] + dfhist->n_at_lam[jfep]);
1332 Tprint = dfhist->Tij_empirical[ifep][jfep] / (dfhist->n_at_lam[ifep]);
1339 fprintf(outfile, "%12.8f", Tprint);
1341 fprintf(outfile, "%3d\n", (ifep + 1));
1347 int ExpandedEnsembleDynamics(FILE* log,
1349 const gmx_enerdata_t* enerd,
1353 df_history_t* dfhist,
1356 const t_mdatoms* mdatoms)
1357 /* Note that the state variable is only needed for simulated tempering, not
1358 Hamiltonian expanded ensemble. May be able to remove it after integrator refactoring. */
1360 real * pfep_lamee, *scaled_lamee, *weighted_lamee;
1362 int i, nlim, lamnew, totalsamples;
1363 real oneovert, maxscaled = 0, maxweighted = 0;
1366 gmx_bool bIfReset, bSwitchtoOneOverT, bDoneEquilibrating = FALSE;
1368 expand = ir->expandedvals;
1369 simtemp = ir->simtempvals;
1370 nlim = ir->fepvals->n_lambda;
1372 snew(scaled_lamee, nlim);
1373 snew(weighted_lamee, nlim);
1374 snew(pfep_lamee, nlim);
1377 /* update the count at the current lambda*/
1378 dfhist->n_at_lam[fep_state]++;
1380 /* need to calculate the PV term somewhere, but not needed here? Not until there's a lambda
1381 state that's pressure controlled.*/
1384 where does this PV term go?
1385 for (i=0;i<nlim;i++)
1387 fep_lamee[i] += pVTerm;
1391 /* determine the minimum value to avoid overflow. Probably a better way to do this */
1392 /* we don't need to include the pressure term, since the volume is the same between the two.
1393 is there some term we are neglecting, however? */
1395 if (ir->efep != efepNO)
1397 for (i = 0; i < nlim; i++)
1401 /* Note -- this assumes no mass changes, since kinetic energy is not added . . . */
1402 scaled_lamee[i] = enerd->foreignLambdaTerms.deltaH(i) / (simtemp->temperatures[i] * BOLTZ)
1403 + enerd->term[F_EPOT]
1404 * (1.0 / (simtemp->temperatures[i])
1405 - 1.0 / (simtemp->temperatures[fep_state]))
1410 scaled_lamee[i] = enerd->foreignLambdaTerms.deltaH(i) / (expand->mc_temp * BOLTZ);
1411 /* mc_temp is currently set to the system reft unless otherwise defined */
1414 /* save these energies for printing, so they don't get overwritten by the next step */
1415 /* they aren't overwritten in the non-free energy case, but we always print with these
1423 for (i = 0; i < nlim; i++)
1427 * (1.0 / simtemp->temperatures[i] - 1.0 / simtemp->temperatures[fep_state]) / BOLTZ;
1432 for (i = 0; i < nlim; i++)
1434 pfep_lamee[i] = scaled_lamee[i];
1436 weighted_lamee[i] = dfhist->sum_weights[i] - scaled_lamee[i];
1439 maxscaled = scaled_lamee[i];
1440 maxweighted = weighted_lamee[i];
1444 if (scaled_lamee[i] > maxscaled)
1446 maxscaled = scaled_lamee[i];
1448 if (weighted_lamee[i] > maxweighted)
1450 maxweighted = weighted_lamee[i];
1455 for (i = 0; i < nlim; i++)
1457 scaled_lamee[i] -= maxscaled;
1458 weighted_lamee[i] -= maxweighted;
1461 /* update weights - we decide whether or not to actually do this inside */
1463 bDoneEquilibrating =
1464 UpdateWeights(nlim, expand, dfhist, fep_state, scaled_lamee, weighted_lamee, step);
1465 if (bDoneEquilibrating)
1470 "\nStep %" PRId64 ": Weights have equilibrated, using criteria: %s\n",
1472 elmceq_names[expand->elmceq]);
1476 lamnew = ChooseNewLambda(
1477 nlim, expand, dfhist, fep_state, weighted_lamee, p_k, ir->expandedvals->lmc_seed, step);
1478 /* if using simulated tempering, we need to adjust the temperatures */
1479 if (ir->bSimTemp && (lamnew != fep_state)) /* only need to change the temperatures if we change the state */
1484 int nstart, nend, gt;
1486 snew(buf_ngtc, ir->opts.ngtc);
1488 for (i = 0; i < ir->opts.ngtc; i++)
1490 if (ir->opts.ref_t[i] > 0)
1492 told = ir->opts.ref_t[i];
1493 ir->opts.ref_t[i] = simtemp->temperatures[lamnew];
1494 buf_ngtc[i] = std::sqrt(ir->opts.ref_t[i] / told); /* using the buffer as temperature scaling */
1498 /* we don't need to manipulate the ekind information, as it isn't due to be reset until the next step anyway */
1501 nend = mdatoms->homenr;
1502 for (n = nstart; n < nend; n++)
1507 gt = mdatoms->cTC[n];
1509 for (d = 0; d < DIM; d++)
1511 v[n][d] *= buf_ngtc[gt];
1515 if (inputrecNptTrotter(ir) || inputrecNphTrotter(ir) || inputrecNvtTrotter(ir))
1517 /* we need to recalculate the masses if the temperature has changed */
1518 init_npt_masses(ir, state, MassQ, FALSE);
1519 for (i = 0; i < state->nnhpres; i++)
1521 for (j = 0; j < ir->opts.nhchainlength; j++)
1523 state->nhpres_vxi[i + j] *= buf_ngtc[i];
1526 for (i = 0; i < ir->opts.ngtc; i++)
1528 for (j = 0; j < ir->opts.nhchainlength; j++)
1530 state->nosehoover_vxi[i + j] *= buf_ngtc[i];
1537 /* now check on the Wang-Landau updating critera */
1539 if (EWL(expand->elamstats))
1541 bSwitchtoOneOverT = FALSE;
1542 if (expand->bWLoneovert)
1545 for (i = 0; i < nlim; i++)
1547 totalsamples += dfhist->n_at_lam[i];
1549 oneovert = (1.0 * nlim) / totalsamples;
1550 /* oneovert has decreasd by a bit since last time, so we actually make sure its within one of this number */
1551 /* switch to 1/t incrementing when wl_delta has decreased at least once, and wl_delta is now less than 1/t */
1552 if ((dfhist->wl_delta <= ((totalsamples) / (totalsamples - 1.00001)) * oneovert)
1553 && (dfhist->wl_delta < expand->init_wl_delta))
1555 bSwitchtoOneOverT = TRUE;
1558 if (bSwitchtoOneOverT)
1561 oneovert; /* now we reduce by this each time, instead of only at flatness */
1565 bIfReset = CheckHistogramRatios(nlim, dfhist->wl_histo, expand->wl_ratio);
1568 for (i = 0; i < nlim; i++)
1570 dfhist->wl_histo[i] = 0;
1572 dfhist->wl_delta *= expand->wl_scale;
1575 fprintf(log, "\nStep %d: weights are now:", static_cast<int>(step));
1576 for (i = 0; i < nlim; i++)
1578 fprintf(log, " %.5f", dfhist->sum_weights[i]);
1586 sfree(scaled_lamee);
1587 sfree(weighted_lamee);