2a6837f6bdf790128c1be1e94f4c5b68b6211af5
[alexxy/gromacs.git] / src / gromacs / mdlib / expanded.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
9  *
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.
14  *
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.
19  *
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.
24  *
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.
32  *
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.
35  */
36 #include "gmxpre.h"
37
38 #include "expanded.h"
39
40 #include <cmath>
41 #include <cstdio>
42
43 #include <algorithm>
44
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/utilities.h"
56 #include "gromacs/math/vec.h"
57 #include "gromacs/mdlib/calcmu.h"
58 #include "gromacs/mdlib/constr.h"
59 #include "gromacs/mdlib/force.h"
60 #include "gromacs/mdlib/update.h"
61 #include "gromacs/mdtypes/enerdata.h"
62 #include "gromacs/mdtypes/forcerec.h"
63 #include "gromacs/mdtypes/inputrec.h"
64 #include "gromacs/mdtypes/md_enums.h"
65 #include "gromacs/mdtypes/mdatom.h"
66 #include "gromacs/mdtypes/state.h"
67 #include "gromacs/random/threefry.h"
68 #include "gromacs/random/uniformrealdistribution.h"
69 #include "gromacs/timing/wallcycle.h"
70 #include "gromacs/utility/enumerationhelpers.h"
71 #include "gromacs/utility/fatalerror.h"
72 #include "gromacs/utility/gmxmpi.h"
73 #include "gromacs/utility/smalloc.h"
74
75 #include "expanded_internal.h"
76
77 static void init_df_history_weights(df_history_t* dfhist, const t_expanded* expand, int nlim)
78 {
79     int i;
80     dfhist->wl_delta = expand->init_wl_delta;
81     for (i = 0; i < nlim; i++)
82     {
83         dfhist->sum_weights[i] = expand->init_lambda_weights[i];
84         dfhist->sum_dg[i]      = expand->init_lambda_weights[i];
85     }
86 }
87
88 /* Eventually should contain all the functions needed to initialize expanded ensemble
89    before the md loop starts */
90 void init_expanded_ensemble(gmx_bool bStateFromCP, const t_inputrec* ir, df_history_t* dfhist)
91 {
92     if (!bStateFromCP)
93     {
94         init_df_history_weights(dfhist, ir->expandedvals.get(), ir->fepvals->n_lambda);
95     }
96 }
97
98 static void GenerateGibbsProbabilities(const real* ene, double* p_k, double* pks, int minfep, int maxfep)
99 {
100
101     int  i;
102     real maxene;
103
104     *pks   = 0.0;
105     maxene = ene[minfep];
106     /* find the maximum value */
107     for (i = minfep; i <= maxfep; i++)
108     {
109         if (ene[i] > maxene)
110         {
111             maxene = ene[i];
112         }
113     }
114     /* find the denominator */
115     for (i = minfep; i <= maxfep; i++)
116     {
117         *pks += std::exp(ene[i] - maxene);
118     }
119     /*numerators*/
120     for (i = minfep; i <= maxfep; i++)
121     {
122         p_k[i] = std::exp(ene[i] - maxene) / *pks;
123     }
124 }
125
126 static void
127 GenerateWeightedGibbsProbabilities(const real* ene, double* p_k, double* pks, int nlim, real* nvals, real delta)
128 {
129
130     int   i;
131     real  maxene;
132     real* nene;
133     *pks = 0.0;
134
135     snew(nene, nlim);
136     for (i = 0; i < nlim; i++)
137     {
138         if (nvals[i] == 0)
139         {
140             /* add the delta, since we need to make sure it's greater than zero, and
141                we need a non-arbitrary number? */
142             nene[i] = ene[i] + std::log(nvals[i] + delta);
143         }
144         else
145         {
146             nene[i] = ene[i] + std::log(nvals[i]);
147         }
148     }
149
150     /* find the maximum value */
151     maxene = nene[0];
152     for (i = 0; i < nlim; i++)
153     {
154         if (nene[i] > maxene)
155         {
156             maxene = nene[i];
157         }
158     }
159
160     /* subtract off the maximum, avoiding overflow */
161     for (i = 0; i < nlim; i++)
162     {
163         nene[i] -= maxene;
164     }
165
166     /* find the denominator */
167     for (i = 0; i < nlim; i++)
168     {
169         *pks += std::exp(nene[i]);
170     }
171
172     /*numerators*/
173     for (i = 0; i < nlim; i++)
174     {
175         p_k[i] = std::exp(nene[i]) / *pks;
176     }
177     sfree(nene);
178 }
179
180 static int FindMinimum(const real* min_metric, int N)
181 {
182
183     real min_val;
184     int  min_nval, nval;
185
186     min_nval = 0;
187     min_val  = min_metric[0];
188
189     for (nval = 0; nval < N; nval++)
190     {
191         if (min_metric[nval] < min_val)
192         {
193             min_val  = min_metric[nval];
194             min_nval = nval;
195         }
196     }
197     return min_nval;
198 }
199
200 static gmx_bool CheckHistogramRatios(int nhisto, const real* histo, real ratio)
201 {
202
203     int      i;
204     real     nmean;
205     gmx_bool bIfFlat;
206
207     nmean = 0;
208     for (i = 0; i < nhisto; i++)
209     {
210         nmean += histo[i];
211     }
212
213     if (nmean == 0)
214     {
215         /* no samples! is bad!*/
216         bIfFlat = FALSE;
217         return bIfFlat;
218     }
219     nmean /= static_cast<real>(nhisto);
220
221     bIfFlat = TRUE;
222     for (i = 0; i < nhisto; i++)
223     {
224         /* make sure that all points are in the ratio < x <  1/ratio range  */
225         if (!((histo[i] / nmean < 1.0 / ratio) && (histo[i] / nmean > ratio)))
226         {
227             bIfFlat = FALSE;
228             break;
229         }
230     }
231     return bIfFlat;
232 }
233
234 static gmx_bool CheckIfDoneEquilibrating(int nlim, const t_expanded* expand, const df_history_t* dfhist, int64_t step)
235 {
236
237     int      i, totalsamples;
238     gmx_bool bDoneEquilibrating = TRUE;
239     gmx_bool bIfFlat;
240
241     /* If we are doing slow growth to get initial values, we haven't finished equilibrating */
242     if (expand->lmc_forced_nstart > 0)
243     {
244         for (i = 0; i < nlim; i++)
245         {
246             if (dfhist->n_at_lam[i]
247                 < expand->lmc_forced_nstart) /* we are still doing the initial sweep, so we're
248                                                 definitely not done equilibrating*/
249             {
250                 bDoneEquilibrating = FALSE;
251                 break;
252             }
253         }
254     }
255     else
256     {
257         /* assume we have equilibrated the weights, then check to see if any of the conditions are not met */
258         bDoneEquilibrating = TRUE;
259
260         /* calculate the total number of samples */
261         switch (expand->elmceq)
262         {
263             case LambdaWeightWillReachEquilibrium::No:
264                 /* We have not equilibrated, and won't, ever. */
265                 bDoneEquilibrating = FALSE;
266                 break;
267             case LambdaWeightWillReachEquilibrium::Yes:
268                 /* we have equilibrated -- we're done */
269                 bDoneEquilibrating = TRUE;
270                 break;
271             case LambdaWeightWillReachEquilibrium::Steps:
272                 /* first, check if we are equilibrating by steps, if we're still under */
273                 if (step < expand->equil_steps)
274                 {
275                     bDoneEquilibrating = FALSE;
276                 }
277                 break;
278             case LambdaWeightWillReachEquilibrium::Samples:
279                 totalsamples = 0;
280                 for (i = 0; i < nlim; i++)
281                 {
282                     totalsamples += dfhist->n_at_lam[i];
283                 }
284                 if (totalsamples < expand->equil_samples)
285                 {
286                     bDoneEquilibrating = FALSE;
287                 }
288                 break;
289             case LambdaWeightWillReachEquilibrium::NumAtLambda:
290                 for (i = 0; i < nlim; i++)
291                 {
292                     if (dfhist->n_at_lam[i]
293                         < expand->equil_n_at_lam) /* we are still doing the initial sweep, so we're
294                                                      definitely not done equilibrating*/
295                     {
296                         bDoneEquilibrating = FALSE;
297                         break;
298                     }
299                 }
300                 break;
301             case LambdaWeightWillReachEquilibrium::WLDelta:
302                 if (EWL(expand->elamstats)) /* This check is in readir as well, but
303                                                just to be sure */
304                 {
305                     if (dfhist->wl_delta > expand->equil_wl_delta)
306                     {
307                         bDoneEquilibrating = FALSE;
308                     }
309                 }
310                 break;
311             case LambdaWeightWillReachEquilibrium::Ratio:
312                 /* we can use the flatness as a judge of good weights, as long as
313                    we're not doing minvar, or Wang-Landau.
314                    But turn off for now until we figure out exactly how we do this.
315                  */
316
317                 if (!(EWL(expand->elamstats) || expand->elamstats == LambdaWeightCalculation::Minvar))
318                 {
319                     /* we want to use flatness -avoiding- the forced-through samples.  Plus, we need
320                        to convert to floats for this histogram function. */
321
322                     real* modhisto;
323                     snew(modhisto, nlim);
324                     for (i = 0; i < nlim; i++)
325                     {
326                         modhisto[i] = 1.0 * (dfhist->n_at_lam[i] - expand->lmc_forced_nstart);
327                     }
328                     bIfFlat = CheckHistogramRatios(nlim, modhisto, expand->equil_ratio);
329                     sfree(modhisto);
330                     if (!bIfFlat)
331                     {
332                         bDoneEquilibrating = FALSE;
333                     }
334                 }
335                 break;
336             default: bDoneEquilibrating = TRUE; break;
337         }
338     }
339     return bDoneEquilibrating;
340 }
341
342 static gmx_bool UpdateWeights(int           nlim,
343                               t_expanded*   expand,
344                               df_history_t* dfhist,
345                               int           fep_state,
346                               const real*   scaled_lamee,
347                               const real*   weighted_lamee,
348                               int64_t       step)
349 {
350     gmx_bool bSufficientSamples;
351     real     acceptanceWeight;
352     int      i;
353     int      min_nvalm, min_nvalp, maxc;
354     real     omega_m1_0, omega_p1_0;
355     real     zero_sum_weights;
356     real *omegam_array, *weightsm_array, *omegap_array, *weightsp_array, *varm_array, *varp_array,
357             *dwp_array, *dwm_array;
358     real    clam_varm, clam_varp, clam_osum, clam_weightsm, clam_weightsp, clam_minvar;
359     real *  lam_variance, *lam_dg;
360     double* p_k;
361     double  pks = 0;
362
363     /* Future potential todos for this function (see #3848):
364      *  - Update the names in the dhist structure to be clearer. Not done for now since this
365      *    a bugfix update and we are mininizing other code changes.
366      *  - Modularize the code some more.
367      *  - potentially merge with accelerated weight histogram functionality, since it's very similar.
368      */
369     /*  if we have equilibrated the expanded ensemble weights, we are not updating them, so exit now */
370     if (dfhist->bEquil)
371     {
372         return FALSE;
373     }
374
375     if (CheckIfDoneEquilibrating(nlim, expand, dfhist, step))
376     {
377         dfhist->bEquil = TRUE;
378         /* zero out the visited states so we know how many equilibrated states we have
379            from here on out.*/
380         for (i = 0; i < nlim; i++)
381         {
382             dfhist->n_at_lam[i] = 0;
383         }
384         return TRUE;
385     }
386
387     /* If we reached this far, we have not equilibrated yet, keep on
388        going resetting the weights */
389
390     if (EWL(expand->elamstats))
391     {
392         if (expand->elamstats
393             == LambdaWeightCalculation::WL) /* Using standard Wang-Landau for weight updates */
394         {
395             dfhist->sum_weights[fep_state] -= dfhist->wl_delta;
396             dfhist->wl_histo[fep_state] += 1.0;
397         }
398         else if (expand->elamstats == LambdaWeightCalculation::WWL)
399         /* Using weighted Wang-Landau for weight updates.
400          * Very closly equivalent to accelerated weight histogram approach
401          * applied to expanded ensemble. */
402         {
403             snew(p_k, nlim);
404
405             /* first increment count */
406             GenerateGibbsProbabilities(weighted_lamee, p_k, &pks, 0, nlim - 1);
407             for (i = 0; i < nlim; i++)
408             {
409                 dfhist->wl_histo[i] += static_cast<real>(p_k[i]);
410             }
411
412             /* then increment weights (uses count) */
413             pks = 0.0;
414             GenerateWeightedGibbsProbabilities(
415                     weighted_lamee, p_k, &pks, nlim, dfhist->wl_histo, dfhist->wl_delta);
416
417             for (i = 0; i < nlim; i++)
418             {
419                 dfhist->sum_weights[i] -= dfhist->wl_delta * static_cast<real>(p_k[i]);
420             }
421             /* Alternate definition, using logarithms. Shouldn't make very much difference! */
422             /*
423                real di;
424                for (i=0;i<nlim;i++)
425                {
426                 di = (real)1.0 + dfhist->wl_delta*(real)p_k[i];
427                 dfhist->sum_weights[i] -= log(di);
428                }
429              */
430             sfree(p_k);
431         }
432
433         zero_sum_weights = dfhist->sum_weights[0];
434         for (i = 0; i < nlim; i++)
435         {
436             dfhist->sum_weights[i] -= zero_sum_weights;
437         }
438     }
439
440     if (expand->elamstats == LambdaWeightCalculation::Barker
441         || expand->elamstats == LambdaWeightCalculation::Metropolis
442         || expand->elamstats == LambdaWeightCalculation::Minvar)
443     {
444         maxc = 2 * expand->c_range + 1;
445
446         snew(lam_dg, nlim);
447         snew(lam_variance, nlim);
448
449         snew(omegap_array, maxc);
450         snew(weightsp_array, maxc);
451         snew(varp_array, maxc);
452         snew(dwp_array, maxc);
453
454         snew(omegam_array, maxc);
455         snew(weightsm_array, maxc);
456         snew(varm_array, maxc);
457         snew(dwm_array, maxc);
458
459         /* unpack the values of the free energy differences and the
460          * variance in their estimates between nearby lambdas. We will
461          * only actually update 2 of these, the state we are currently
462          * at and the one we end up moving to
463          */
464
465         for (i = 0; i < nlim - 1; i++)
466         { /* only through the second to last */
467             lam_dg[i] = dfhist->sum_dg[i + 1] - dfhist->sum_dg[i];
468             lam_variance[i] =
469                     gmx::square(dfhist->sum_variance[i + 1]) - gmx::square(dfhist->sum_variance[i]);
470         }
471
472         /* accumulate running averages of thermodynamic averages for Bennett Acceptance Ratio-based
473          * estimates of the free energy .
474          * Rather than peforming self-consistent estimation of the free energies at each step,
475          * we keep track of an array of possible different free energies (cnvals),
476          * and we self-consistently choose the best one. The one that leads to a free energy estimate
477          * that is closest to itself is the best estimate of the free energy.  It is essentially a
478          * parallellized version of self-consistent iteration.  maxc is the number of these constants. */
479
480         for (int nval = 0; nval < maxc; nval++)
481         {
482             const real cnval = static_cast<real>(nval - expand->c_range);
483
484             /* Compute acceptance criterion weight to the state below this one for use in averages.
485              * Note we do not have to have just moved from that state to use this free energy
486              * estimate; these are essentially "virtual" moves. */
487
488             if (fep_state > 0)
489             {
490                 const auto lambdaEnergyDifference =
491                         cnval - (scaled_lamee[fep_state] - scaled_lamee[fep_state - 1]);
492                 acceptanceWeight =
493                         gmx::calculateAcceptanceWeight(expand->elamstats, lambdaEnergyDifference);
494                 dfhist->accum_m[fep_state][nval] += acceptanceWeight;
495                 dfhist->accum_m2[fep_state][nval] += acceptanceWeight * acceptanceWeight;
496             }
497
498             // Compute acceptance criterion weight to transition to the next state
499             if (fep_state < nlim - 1)
500             {
501                 const auto lambdaEnergyDifference =
502                         -cnval + (scaled_lamee[fep_state + 1] - scaled_lamee[fep_state]);
503                 acceptanceWeight =
504                         gmx::calculateAcceptanceWeight(expand->elamstats, lambdaEnergyDifference);
505                 dfhist->accum_p[fep_state][nval] += acceptanceWeight;
506                 dfhist->accum_p2[fep_state][nval] += acceptanceWeight * acceptanceWeight;
507             }
508
509             /* Determination of Metropolis transition and Barker transition weights */
510
511             int numObservationsCurrentState = dfhist->n_at_lam[fep_state];
512             /* determine the number of observations above and below the current state */
513             int numObservationsLowerState = 0;
514             if (fep_state > 0)
515             {
516                 numObservationsLowerState = dfhist->n_at_lam[fep_state - 1];
517             }
518             int numObservationsHigherState = 0;
519             if (fep_state < nlim - 1)
520             {
521                 numObservationsHigherState = dfhist->n_at_lam[fep_state + 1];
522             }
523
524             /* Calculate the biases for each expanded ensemble state that minimize the total
525              * variance, as implemented in Martinez-Veracoechea and Escobedo,
526              * J. Phys. Chem. B 2008, 112, 8120-8128
527              *
528              * The variance associated with the free energy estimate between two states i and j
529              * is calculated as
530              *     Var(i,j) = {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1} / numObservations(i->j)
531              *              + {avg[xi(j->i)^2] / avg[xi(j->i)]^2 - 1} / numObservations(j->i)
532              * where xi(i->j) is the acceptance factor / weight associated with moving from state i to j
533              * As we are calculating the acceptance factor to the neighbors every time we're visiting
534              * a state, numObservations(i->j) == numObservations(i) and numObservations(j->i) == numObservations(j)
535              */
536
537             /* Accumulation of acceptance weight averages between the current state and the
538              * states +1 (p1) and -1 (m1), averaged at current state (0)
539              */
540             real avgAcceptanceCurrentToLower  = 0;
541             real avgAcceptanceCurrentToHigher = 0;
542             /* Accumulation of acceptance weight averages quantities between states 0
543              *  and states +1 and -1, squared
544              */
545             real avgAcceptanceCurrentToLowerSquared  = 0;
546             real avgAcceptanceCurrentToHigherSquared = 0;
547             /* Accumulation of free energy quantities from lower state (m1) to current state (0) and squared */
548             real avgAcceptanceLowerToCurrent        = 0;
549             real avgAcceptanceLowerToCurrentSquared = 0;
550             /* Accumulation of free energy quantities from upper state (p1) to current state (0) and squared */
551             real avgAcceptanceHigherToCurrent        = 0;
552             real avgAcceptanceHigherToCurrentSquared = 0;
553
554             if (numObservationsCurrentState > 0)
555             {
556                 avgAcceptanceCurrentToLower = dfhist->accum_m[fep_state][nval] / numObservationsCurrentState;
557                 avgAcceptanceCurrentToHigher =
558                         dfhist->accum_p[fep_state][nval] / numObservationsCurrentState;
559                 avgAcceptanceCurrentToLowerSquared =
560                         dfhist->accum_m2[fep_state][nval] / numObservationsCurrentState;
561                 avgAcceptanceCurrentToHigherSquared =
562                         dfhist->accum_p2[fep_state][nval] / numObservationsCurrentState;
563             }
564
565             if ((fep_state > 0) && (numObservationsLowerState > 0))
566             {
567                 avgAcceptanceLowerToCurrent =
568                         dfhist->accum_p[fep_state - 1][nval] / numObservationsLowerState;
569                 avgAcceptanceLowerToCurrentSquared =
570                         dfhist->accum_p2[fep_state - 1][nval] / numObservationsLowerState;
571             }
572
573             if ((fep_state < nlim - 1) && (numObservationsHigherState > 0))
574             {
575                 avgAcceptanceHigherToCurrent =
576                         dfhist->accum_m[fep_state + 1][nval] / numObservationsHigherState;
577                 avgAcceptanceHigherToCurrentSquared =
578                         dfhist->accum_m2[fep_state + 1][nval] / numObservationsHigherState;
579             }
580             /* These are accumulation of positive values (see definition of acceptance functions
581              * above), or of squares of positive values.
582              * We're taking this for granted in the following calculation, so make sure
583              * here that nothing weird happened. Although technically all values should be positive,
584              * because of floating point precisions, they might be numerically zero. */
585             GMX_RELEASE_ASSERT(
586                     avgAcceptanceCurrentToLower >= 0 && avgAcceptanceCurrentToLowerSquared >= 0
587                             && avgAcceptanceCurrentToHigher >= 0
588                             && avgAcceptanceCurrentToHigherSquared >= 0 && avgAcceptanceLowerToCurrent >= 0
589                             && avgAcceptanceLowerToCurrentSquared >= 0 && avgAcceptanceHigherToCurrent >= 0
590                             && avgAcceptanceHigherToCurrentSquared >= 0,
591                     "By definition, the acceptance factors should all be nonnegative.");
592
593             real varianceCurrentToLower   = 0;
594             real varianceCurrentToHigher  = 0;
595             real weightDifferenceToLower  = 0;
596             real weightDifferenceToHigher = 0;
597             real varianceToLower          = 0;
598             real varianceToHigher         = 0;
599
600             if (fep_state > 0)
601             {
602                 if (numObservationsCurrentState > 0)
603                 {
604                     /* Calculate {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1}
605                      *
606                      * Note that if avg[xi(i->j)] == 0, also avg[xi(i->j)^2] == 0 (since the
607                      * acceptances are all positive!), and hence
608                      *     {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1} -> 0  for  avg[xi(i->j)] -> 0
609                      * We're catching that case explicitly to avoid numerical
610                      * problems dividing by zero when the overlap between states is small (#3304)
611                      */
612                     if (avgAcceptanceCurrentToLower > 0)
613                     {
614                         varianceCurrentToLower =
615                                 avgAcceptanceCurrentToLowerSquared
616                                         / (avgAcceptanceCurrentToLower * avgAcceptanceCurrentToLower)
617                                 - 1.0;
618                     }
619                     if (numObservationsLowerState > 0)
620                     {
621                         /* Calculate {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1}
622                          *
623                          * Note that if avg[xi(i->j)] == 0, also avg[xi(i->j)^2] == 0 (since the
624                          * acceptances are all positive!), and hence
625                          *     {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1} -> 0  for  avg[xi(i->j)] -> 0
626                          * We're catching that case explicitly to avoid numerical
627                          * problems dividing by zero when the overlap between states is small (#3304)
628                          */
629                         real varianceLowerToCurrent = 0;
630                         if (avgAcceptanceLowerToCurrent > 0)
631                         {
632                             varianceLowerToCurrent =
633                                     avgAcceptanceLowerToCurrentSquared
634                                             / (avgAcceptanceLowerToCurrent * avgAcceptanceLowerToCurrent)
635                                     - 1.0;
636                         }
637                         /* Free energy difference to the state one state lower */
638                         /* if these either of these quantities are zero, the energies are */
639                         /* way too large for the dynamic range.  We need an alternate guesstimate */
640                         if ((avgAcceptanceCurrentToLower == 0) || (avgAcceptanceLowerToCurrent == 0))
641                         {
642                             weightDifferenceToLower =
643                                     (scaled_lamee[fep_state] - scaled_lamee[fep_state - 1]);
644                         }
645                         else
646                         {
647                             weightDifferenceToLower = (std::log(avgAcceptanceCurrentToLower)
648                                                        - std::log(avgAcceptanceLowerToCurrent))
649                                                       + cnval;
650                         }
651                         /* Variance of the free energy difference to the one state lower */
652                         varianceToLower =
653                                 (1.0 / numObservationsCurrentState) * (varianceCurrentToLower)
654                                 + (1.0 / numObservationsLowerState) * (varianceLowerToCurrent);
655                     }
656                 }
657             }
658
659             if (fep_state < nlim - 1)
660             {
661                 if (numObservationsCurrentState > 0)
662                 {
663                     /* Calculate {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1}
664                      *
665                      * Note that if avg[xi(i->j)] == 0, also avg[xi(i->j)^2] == 0 (since the
666                      * acceptances are all positive!), and hence
667                      *     {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1} -> 0  for  avg[xi(i->j)] -> 0
668                      * We're catching that case explicitly to avoid numerical
669                      * problems dividing by zero when the overlap between states is small (#3304)
670                      */
671
672                     if (avgAcceptanceCurrentToHigher < 0)
673                     {
674                         varianceCurrentToHigher =
675                                 avgAcceptanceCurrentToHigherSquared
676                                         / (avgAcceptanceCurrentToHigher * avgAcceptanceCurrentToHigher)
677                                 - 1.0;
678                     }
679                     if (numObservationsHigherState > 0)
680                     {
681                         /* Calculate {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1}
682                          *
683                          * Note that if avg[xi(i->j)] == 0, also avg[xi(i->j)^2] == 0 (since the
684                          * acceptances are all positive!), and hence
685                          *     {avg[xi(i->j)^2] / avg[xi(i->j)]^2 - 1} -> 0  for  avg[xi(i->j)] -> 0
686                          * We're catching that case explicitly to avoid numerical
687                          * problems dividing by zero when the overlap between states is small (#3304)
688                          */
689                         real varianceHigherToCurrent = 0;
690                         if (avgAcceptanceHigherToCurrent > 0)
691                         {
692                             varianceHigherToCurrent =
693                                     avgAcceptanceHigherToCurrentSquared
694                                             / (avgAcceptanceHigherToCurrent * avgAcceptanceHigherToCurrent)
695                                     - 1.0;
696                         }
697                         /* Free energy difference to the state one state higher */
698                         /* if these either of these quantities are zero, the energies are */
699                         /* way too large for the dynamic range.  We need an alternate guesstimate */
700                         if ((avgAcceptanceHigherToCurrent == 0) || (avgAcceptanceCurrentToHigher == 0))
701                         {
702                             weightDifferenceToHigher =
703                                     (scaled_lamee[fep_state + 1] - scaled_lamee[fep_state]);
704                         }
705                         else
706                         {
707                             weightDifferenceToHigher = (std::log(avgAcceptanceHigherToCurrent)
708                                                         - std::log(avgAcceptanceCurrentToHigher))
709                                                        + cnval;
710                         }
711                         /* Variance of the free energy difference to the one state higher */
712                         varianceToHigher =
713                                 (1.0 / numObservationsHigherState) * (varianceHigherToCurrent)
714                                 + (1.0 / numObservationsCurrentState) * (varianceCurrentToHigher);
715                     }
716                 }
717             }
718
719             if (numObservationsCurrentState > 0)
720             {
721                 omegam_array[nval] = varianceCurrentToLower;
722             }
723             else
724             {
725                 omegam_array[nval] = 0;
726             }
727             weightsm_array[nval] = weightDifferenceToLower;
728             varm_array[nval]     = varianceToLower;
729             if (numObservationsLowerState > 0)
730             {
731                 dwm_array[nval] =
732                         fabs((cnval + std::log((1.0 * numObservationsCurrentState) / numObservationsLowerState))
733                              - lam_dg[fep_state - 1]);
734             }
735             else
736             {
737                 dwm_array[nval] = std::fabs(cnval - lam_dg[fep_state - 1]);
738             }
739
740             if (numObservationsCurrentState > 0)
741             {
742                 omegap_array[nval] = varianceCurrentToHigher;
743             }
744             else
745             {
746                 omegap_array[nval] = 0;
747             }
748             weightsp_array[nval] = weightDifferenceToHigher;
749             varp_array[nval]     = varianceToHigher;
750             if ((numObservationsHigherState > 0) && (numObservationsCurrentState > 0))
751             {
752                 dwp_array[nval] =
753                         fabs((cnval + std::log((1.0 * numObservationsHigherState) / numObservationsCurrentState))
754                              - lam_dg[fep_state]);
755             }
756             else
757             {
758                 dwp_array[nval] = std::fabs(cnval - lam_dg[fep_state]);
759             }
760         }
761
762         /* find the free energy estimate closest to the guessed weight's value */
763
764         min_nvalm     = FindMinimum(dwm_array, maxc);
765         omega_m1_0    = omegam_array[min_nvalm];
766         clam_weightsm = weightsm_array[min_nvalm];
767         clam_varm     = varm_array[min_nvalm];
768
769         min_nvalp     = FindMinimum(dwp_array, maxc);
770         omega_p1_0    = omegap_array[min_nvalp];
771         clam_weightsp = weightsp_array[min_nvalp];
772         clam_varp     = varp_array[min_nvalp];
773
774         clam_osum   = omega_m1_0 + omega_p1_0;
775         clam_minvar = 0;
776         if (clam_osum > 0)
777         {
778             clam_minvar = 0.5 * std::log(clam_osum);
779         }
780
781         if (fep_state > 0)
782         {
783             lam_dg[fep_state - 1]       = clam_weightsm;
784             lam_variance[fep_state - 1] = clam_varm;
785         }
786
787         if (fep_state < nlim - 1)
788         {
789             lam_dg[fep_state]       = clam_weightsp;
790             lam_variance[fep_state] = clam_varp;
791         }
792
793         if (expand->elamstats == LambdaWeightCalculation::Minvar)
794         {
795             bSufficientSamples = TRUE;
796             /* make sure the number of samples in each state are all
797              * past a user-specified threshold
798              */
799             for (i = 0; i < nlim; i++)
800             {
801                 if (dfhist->n_at_lam[i] < expand->minvarmin)
802                 {
803                     bSufficientSamples = FALSE;
804                 }
805             }
806             if (bSufficientSamples)
807             {
808                 dfhist->sum_minvar[fep_state] = clam_minvar;
809                 if (fep_state == 0)
810                 {
811                     for (i = 0; i < nlim; i++)
812                     {
813                         dfhist->sum_minvar[i] += (expand->minvar_const - clam_minvar);
814                     }
815                     expand->minvar_const          = clam_minvar;
816                     dfhist->sum_minvar[fep_state] = 0.0;
817                 }
818                 else
819                 {
820                     dfhist->sum_minvar[fep_state] -= expand->minvar_const;
821                 }
822             }
823         }
824
825         /* we need to rezero minvar now, since it could change at fep_state = 0 */
826         dfhist->sum_dg[0]       = 0.0;
827         dfhist->sum_variance[0] = 0.0;
828         dfhist->sum_weights[0]  = dfhist->sum_dg[0] + dfhist->sum_minvar[0]; /* should be zero */
829
830         for (i = 1; i < nlim; i++)
831         {
832             dfhist->sum_dg[i] = lam_dg[i - 1] + dfhist->sum_dg[i - 1];
833             dfhist->sum_variance[i] =
834                     std::sqrt(lam_variance[i - 1] + gmx::square(dfhist->sum_variance[i - 1]));
835             dfhist->sum_weights[i] = dfhist->sum_dg[i] + dfhist->sum_minvar[i];
836         }
837
838         sfree(lam_dg);
839         sfree(lam_variance);
840
841         sfree(omegam_array);
842         sfree(weightsm_array);
843         sfree(varm_array);
844         sfree(dwm_array);
845
846         sfree(omegap_array);
847         sfree(weightsp_array);
848         sfree(varp_array);
849         sfree(dwp_array);
850     }
851     return FALSE;
852 }
853
854 static int ChooseNewLambda(int               nlim,
855                            const t_expanded* expand,
856                            df_history_t*     dfhist,
857                            int               fep_state,
858                            const real*       weighted_lamee,
859                            double*           p_k,
860                            int64_t           seed,
861                            int64_t           step)
862 {
863     /* Choose new lambda value, and update transition matrix */
864
865     int                  i, ifep, minfep, maxfep, lamnew, lamtrial, starting_fep_state;
866     real                 r1, r2, de, trialprob, tprob = 0;
867     double *             propose, *accept, *remainder;
868     double               pks;
869     real                 pnorm;
870     gmx::ThreeFry2x64<0> rng(
871             seed, gmx::RandomDomain::ExpandedEnsemble); // We only draw once, so zero bits internal counter is fine
872     gmx::UniformRealDistribution<real> dist;
873
874     starting_fep_state = fep_state;
875     lamnew             = fep_state; /* so that there is a default setting -- stays the same */
876
877     if (!EWL(expand->elamstats)) /* ignore equilibrating the weights if using WL */
878     {
879         if ((expand->lmc_forced_nstart > 0) && (dfhist->n_at_lam[nlim - 1] <= expand->lmc_forced_nstart))
880         {
881             /* Use a marching method to run through the lambdas and get preliminary free energy data,
882                before starting 'free' sampling.  We start free sampling when we have enough at each lambda */
883
884             /* if we have enough at this lambda, move on to the next one */
885
886             if (dfhist->n_at_lam[fep_state] == expand->lmc_forced_nstart)
887             {
888                 lamnew = fep_state + 1;
889                 if (lamnew == nlim) /* whoops, stepped too far! */
890                 {
891                     lamnew -= 1;
892                 }
893             }
894             else
895             {
896                 lamnew = fep_state;
897             }
898             return lamnew;
899         }
900     }
901
902     snew(propose, nlim);
903     snew(accept, nlim);
904     snew(remainder, nlim);
905
906     for (i = 0; i < expand->lmc_repeats; i++)
907     {
908         rng.restart(step, i);
909         dist.reset();
910
911         for (ifep = 0; ifep < nlim; ifep++)
912         {
913             propose[ifep] = 0;
914             accept[ifep]  = 0;
915         }
916
917         if ((expand->elmcmove == LambdaMoveCalculation::Gibbs)
918             || (expand->elmcmove == LambdaMoveCalculation::MetropolisGibbs))
919         {
920             /* use the Gibbs sampler, with restricted range */
921             if (expand->gibbsdeltalam < 0)
922             {
923                 minfep = 0;
924                 maxfep = nlim - 1;
925             }
926             else
927             {
928                 minfep = fep_state - expand->gibbsdeltalam;
929                 maxfep = fep_state + expand->gibbsdeltalam;
930                 if (minfep < 0)
931                 {
932                     minfep = 0;
933                 }
934                 if (maxfep > nlim - 1)
935                 {
936                     maxfep = nlim - 1;
937                 }
938             }
939
940             GenerateGibbsProbabilities(weighted_lamee, p_k, &pks, minfep, maxfep);
941
942             if (expand->elmcmove == LambdaMoveCalculation::Gibbs)
943             {
944                 for (ifep = minfep; ifep <= maxfep; ifep++)
945                 {
946                     propose[ifep] = p_k[ifep];
947                     accept[ifep]  = 1.0;
948                 }
949                 /* Gibbs sampling */
950                 r1 = dist(rng);
951                 for (lamnew = minfep; lamnew <= maxfep; lamnew++)
952                 {
953                     if (r1 <= p_k[lamnew])
954                     {
955                         break;
956                     }
957                     r1 -= p_k[lamnew];
958                 }
959             }
960             else if (expand->elmcmove == LambdaMoveCalculation::MetropolisGibbs)
961             {
962
963                 /* Metropolized Gibbs sampling */
964                 for (ifep = minfep; ifep <= maxfep; ifep++)
965                 {
966                     remainder[ifep] = 1 - p_k[ifep];
967                 }
968
969                 /* find the proposal probabilities */
970
971                 if (remainder[fep_state] == 0)
972                 {
973                     /* only the current state has any probability */
974                     /* we have to stay at the current state */
975                     lamnew = fep_state;
976                 }
977                 else
978                 {
979                     for (ifep = minfep; ifep <= maxfep; ifep++)
980                     {
981                         if (ifep != fep_state)
982                         {
983                             propose[ifep] = p_k[ifep] / remainder[fep_state];
984                         }
985                         else
986                         {
987                             propose[ifep] = 0;
988                         }
989                     }
990
991                     r1 = dist(rng);
992                     for (lamtrial = minfep; lamtrial <= maxfep; lamtrial++)
993                     {
994                         pnorm = p_k[lamtrial] / remainder[fep_state];
995                         if (lamtrial != fep_state)
996                         {
997                             if (r1 <= pnorm)
998                             {
999                                 break;
1000                             }
1001                             r1 -= pnorm;
1002                         }
1003                     }
1004
1005                     /* we have now selected lamtrial according to p(lamtrial)/1-p(fep_state) */
1006                     tprob = 1.0;
1007                     /* trial probability is min{1,\frac{1 - p(old)}{1-p(new)} MRS 1/8/2008 */
1008                     trialprob = (remainder[fep_state]) / (remainder[lamtrial]);
1009                     if (trialprob < tprob)
1010                     {
1011                         tprob = trialprob;
1012                     }
1013                     r2 = dist(rng);
1014                     if (r2 < tprob)
1015                     {
1016                         lamnew = lamtrial;
1017                     }
1018                     else
1019                     {
1020                         lamnew = fep_state;
1021                     }
1022                 }
1023
1024                 /* now figure out the acceptance probability for each */
1025                 for (ifep = minfep; ifep <= maxfep; ifep++)
1026                 {
1027                     tprob = 1.0;
1028                     if (remainder[ifep] != 0)
1029                     {
1030                         trialprob = (remainder[fep_state]) / (remainder[ifep]);
1031                     }
1032                     else
1033                     {
1034                         trialprob = 1.0; /* this state is the only choice! */
1035                     }
1036                     if (trialprob < tprob)
1037                     {
1038                         tprob = trialprob;
1039                     }
1040                     /* probability for fep_state=0, but that's fine, it's never proposed! */
1041                     accept[ifep] = tprob;
1042                 }
1043             }
1044
1045             if (lamnew > maxfep)
1046             {
1047                 /* it's possible some rounding is failing */
1048                 if (gmx_within_tol(remainder[fep_state], 0, 50 * GMX_DOUBLE_EPS))
1049                 {
1050                     /* numerical rounding error -- no state other than the original has weight */
1051                     lamnew = fep_state;
1052                 }
1053                 else
1054                 {
1055                     /* probably not a numerical issue */
1056                     int   loc    = 0;
1057                     int   nerror = 200 + (maxfep - minfep + 1) * 60;
1058                     char* errorstr;
1059                     snew(errorstr, nerror);
1060                     /* if its greater than maxfep, then something went wrong -- probably underflow
1061                        in the calculation of sum weights. Generated detailed info for failure */
1062                     loc += sprintf(
1063                             errorstr,
1064                             "Something wrong in choosing new lambda state with a Gibbs move -- "
1065                             "probably underflow in weight determination.\nDenominator is: "
1066                             "%3d%17.10e\n  i                dE        numerator          weights\n",
1067                             0,
1068                             pks);
1069                     for (ifep = minfep; ifep <= maxfep; ifep++)
1070                     {
1071                         loc += sprintf(&errorstr[loc],
1072                                        "%3d %17.10e%17.10e%17.10e\n",
1073                                        ifep,
1074                                        weighted_lamee[ifep],
1075                                        p_k[ifep],
1076                                        dfhist->sum_weights[ifep]);
1077                     }
1078                     gmx_fatal(FARGS, "%s", errorstr);
1079                 }
1080             }
1081         }
1082         else if ((expand->elmcmove == LambdaMoveCalculation::Metropolis)
1083                  || (expand->elmcmove == LambdaMoveCalculation::Barker))
1084         {
1085             /* use the metropolis sampler with trial +/- 1 */
1086             r1 = dist(rng);
1087             if (r1 < 0.5)
1088             {
1089                 if (fep_state == 0)
1090                 {
1091                     lamtrial = fep_state;
1092                 }
1093                 else
1094                 {
1095                     lamtrial = fep_state - 1;
1096                 }
1097             }
1098             else
1099             {
1100                 if (fep_state == nlim - 1)
1101                 {
1102                     lamtrial = fep_state;
1103                 }
1104                 else
1105                 {
1106                     lamtrial = fep_state + 1;
1107                 }
1108             }
1109
1110             de = weighted_lamee[lamtrial] - weighted_lamee[fep_state];
1111             if (expand->elmcmove == LambdaMoveCalculation::Metropolis)
1112             {
1113                 tprob = 1.0;
1114                 if (de < 0)
1115                 {
1116                     tprob = std::exp(de);
1117                 }
1118                 propose[fep_state] = 0;
1119                 propose[lamtrial]  = 1.0; /* note that this overwrites the above line if fep_state = ntrial, which only occurs at the ends */
1120                 accept[fep_state] =
1121                         1.0; /* doesn't actually matter, never proposed unless fep_state = ntrial, in which case it's 1.0 anyway */
1122                 accept[lamtrial] = tprob;
1123             }
1124             else if (expand->elmcmove == LambdaMoveCalculation::Barker)
1125             {
1126                 if (de > 0) /* Numerically stable version */
1127                 {
1128                     tprob = 1.0 / (1.0 + std::exp(-de));
1129                 }
1130                 else if (de < 0)
1131                 {
1132                     tprob = std::exp(de) / (std::exp(de) + 1.0);
1133                 }
1134                 propose[fep_state] = (1 - tprob);
1135                 propose[lamtrial] +=
1136                         tprob; /* we add, to account for the fact that at the end, they might be the same point */
1137                 accept[fep_state] = 1.0;
1138                 accept[lamtrial]  = 1.0;
1139             }
1140
1141             r2 = dist(rng);
1142             if (r2 < tprob)
1143             {
1144                 lamnew = lamtrial;
1145             }
1146             else
1147             {
1148                 lamnew = fep_state;
1149             }
1150         }
1151
1152         for (ifep = 0; ifep < nlim; ifep++)
1153         {
1154             dfhist->Tij[fep_state][ifep] += propose[ifep] * accept[ifep];
1155             dfhist->Tij[fep_state][fep_state] += propose[ifep] * (1.0 - accept[ifep]);
1156         }
1157         fep_state = lamnew;
1158     }
1159
1160     dfhist->Tij_empirical[starting_fep_state][lamnew] += 1.0;
1161
1162     sfree(propose);
1163     sfree(accept);
1164     sfree(remainder);
1165
1166     return lamnew;
1167 }
1168
1169 /* print out the weights to the log, along with current state */
1170 void PrintFreeEnergyInfoToFile(FILE*               outfile,
1171                                const t_lambda*     fep,
1172                                const t_expanded*   expand,
1173                                const t_simtemp*    simtemp,
1174                                const df_history_t* dfhist,
1175                                int                 fep_state,
1176                                int                 frequency,
1177                                int64_t             step)
1178 {
1179     int      nlim, ifep, jfep;
1180     real     dw, dg, dv, Tprint;
1181     gmx_bool bSimTemp = FALSE;
1182
1183     nlim = fep->n_lambda;
1184     if (simtemp != nullptr)
1185     {
1186         bSimTemp = TRUE;
1187     }
1188
1189     if (step % frequency == 0)
1190     {
1191         fprintf(outfile, "             MC-lambda information\n");
1192         if (EWL(expand->elamstats) && (!(dfhist->bEquil)))
1193         {
1194             fprintf(outfile, "  Wang-Landau incrementor is: %11.5g\n", dfhist->wl_delta);
1195         }
1196         fprintf(outfile, "  N");
1197         for (auto i : keysOf(fep->separate_dvdl))
1198         {
1199             if (fep->separate_dvdl[i])
1200             {
1201                 fprintf(outfile, "%7s", enumValueToString(i));
1202             }
1203             else if ((i == FreeEnergyPerturbationCouplingType::Temperature) && bSimTemp)
1204             {
1205                 fprintf(outfile, "%10s", enumValueToString(i)); /* more space for temperature formats */
1206             }
1207         }
1208         fprintf(outfile, "    Count   ");
1209         if (expand->elamstats == LambdaWeightCalculation::Minvar)
1210         {
1211             fprintf(outfile, "W(in kT)   G(in kT)  dG(in kT)  dV(in kT)\n");
1212         }
1213         else
1214         {
1215             fprintf(outfile, "G(in kT)  dG(in kT)\n");
1216         }
1217         for (ifep = 0; ifep < nlim; ifep++)
1218         {
1219             if (ifep == nlim - 1)
1220             {
1221                 dw = 0.0;
1222                 dg = 0.0;
1223                 dv = 0.0;
1224             }
1225             else
1226             {
1227                 dw = dfhist->sum_weights[ifep + 1] - dfhist->sum_weights[ifep];
1228                 dg = dfhist->sum_dg[ifep + 1] - dfhist->sum_dg[ifep];
1229                 dv = std::sqrt(gmx::square(dfhist->sum_variance[ifep + 1])
1230                                - gmx::square(dfhist->sum_variance[ifep]));
1231             }
1232             fprintf(outfile, "%3d", (ifep + 1));
1233             for (auto i : keysOf(fep->separate_dvdl))
1234             {
1235                 if (fep->separate_dvdl[i])
1236                 {
1237                     fprintf(outfile, "%7.3f", fep->all_lambda[i][ifep]);
1238                 }
1239                 else if (i == FreeEnergyPerturbationCouplingType::Temperature && bSimTemp)
1240                 {
1241                     fprintf(outfile, "%9.3f", simtemp->temperatures[ifep]);
1242                 }
1243             }
1244             if (EWL(expand->elamstats)
1245                 && (!(dfhist->bEquil))) /* if performing WL and still haven't equilibrated */
1246             {
1247                 if (expand->elamstats == LambdaWeightCalculation::WL)
1248                 {
1249                     fprintf(outfile, " %8d", static_cast<int>(dfhist->wl_histo[ifep]));
1250                 }
1251                 else
1252                 {
1253                     fprintf(outfile, " %8.3f", dfhist->wl_histo[ifep]);
1254                 }
1255             }
1256             else /* we have equilibrated weights */
1257             {
1258                 fprintf(outfile, " %8d", dfhist->n_at_lam[ifep]);
1259             }
1260             if (expand->elamstats == LambdaWeightCalculation::Minvar)
1261             {
1262                 fprintf(outfile,
1263                         " %10.5f %10.5f %10.5f %10.5f",
1264                         dfhist->sum_weights[ifep],
1265                         dfhist->sum_dg[ifep],
1266                         dg,
1267                         dv);
1268             }
1269             else
1270             {
1271                 fprintf(outfile, " %10.5f %10.5f", dfhist->sum_weights[ifep], dw);
1272             }
1273             if (ifep == fep_state)
1274             {
1275                 fprintf(outfile, " <<\n");
1276             }
1277             else
1278             {
1279                 fprintf(outfile, "   \n");
1280             }
1281         }
1282         fprintf(outfile, "\n");
1283
1284         if ((step % expand->nstTij == 0) && (expand->nstTij > 0) && (step > 0))
1285         {
1286             fprintf(outfile, "                     Transition Matrix\n");
1287             for (ifep = 0; ifep < nlim; ifep++)
1288             {
1289                 fprintf(outfile, "%12d", (ifep + 1));
1290             }
1291             fprintf(outfile, "\n");
1292             for (ifep = 0; ifep < nlim; ifep++)
1293             {
1294                 for (jfep = 0; jfep < nlim; jfep++)
1295                 {
1296                     if (dfhist->n_at_lam[ifep] > 0)
1297                     {
1298                         if (expand->bSymmetrizedTMatrix)
1299                         {
1300                             Tprint = (dfhist->Tij[ifep][jfep] + dfhist->Tij[jfep][ifep])
1301                                      / (dfhist->n_at_lam[ifep] + dfhist->n_at_lam[jfep]);
1302                         }
1303                         else
1304                         {
1305                             Tprint = (dfhist->Tij[ifep][jfep]) / (dfhist->n_at_lam[ifep]);
1306                         }
1307                     }
1308                     else
1309                     {
1310                         Tprint = 0.0;
1311                     }
1312                     fprintf(outfile, "%12.8f", Tprint);
1313                 }
1314                 fprintf(outfile, "%3d\n", (ifep + 1));
1315             }
1316
1317             fprintf(outfile, "                  Empirical Transition Matrix\n");
1318             for (ifep = 0; ifep < nlim; ifep++)
1319             {
1320                 fprintf(outfile, "%12d", (ifep + 1));
1321             }
1322             fprintf(outfile, "\n");
1323             for (ifep = 0; ifep < nlim; ifep++)
1324             {
1325                 for (jfep = 0; jfep < nlim; jfep++)
1326                 {
1327                     if (dfhist->n_at_lam[ifep] > 0)
1328                     {
1329                         if (expand->bSymmetrizedTMatrix)
1330                         {
1331                             Tprint = (dfhist->Tij_empirical[ifep][jfep] + dfhist->Tij_empirical[jfep][ifep])
1332                                      / (dfhist->n_at_lam[ifep] + dfhist->n_at_lam[jfep]);
1333                         }
1334                         else
1335                         {
1336                             Tprint = dfhist->Tij_empirical[ifep][jfep] / (dfhist->n_at_lam[ifep]);
1337                         }
1338                     }
1339                     else
1340                     {
1341                         Tprint = 0.0;
1342                     }
1343                     fprintf(outfile, "%12.8f", Tprint);
1344                 }
1345                 fprintf(outfile, "%3d\n", (ifep + 1));
1346             }
1347         }
1348     }
1349 }
1350
1351 int ExpandedEnsembleDynamics(FILE*                 log,
1352                              t_inputrec*           ir,
1353                              const gmx_enerdata_t* enerd,
1354                              t_state*              state,
1355                              t_extmass*            MassQ,
1356                              int                   fep_state,
1357                              df_history_t*         dfhist,
1358                              int64_t               step,
1359                              rvec*                 v,
1360                              const t_mdatoms*      mdatoms)
1361 /* Note that the state variable is only needed for simulated tempering, not
1362    Hamiltonian expanded ensemble.  May be able to remove it after integrator refactoring. */
1363 {
1364     real *      pfep_lamee, *scaled_lamee, *weighted_lamee;
1365     double*     p_k;
1366     int         i, nlim, lamnew, totalsamples;
1367     real        oneovert, maxscaled = 0, maxweighted = 0;
1368     t_expanded* expand;
1369     t_simtemp*  simtemp;
1370     gmx_bool    bIfReset, bSwitchtoOneOverT, bDoneEquilibrating = FALSE;
1371
1372     expand  = ir->expandedvals.get();
1373     simtemp = ir->simtempvals.get();
1374     nlim    = ir->fepvals->n_lambda;
1375
1376     snew(scaled_lamee, nlim);
1377     snew(weighted_lamee, nlim);
1378     snew(pfep_lamee, nlim);
1379     snew(p_k, nlim);
1380
1381     /* update the count at the current lambda*/
1382     dfhist->n_at_lam[fep_state]++;
1383
1384     /* need to calculate the PV term somewhere, but not needed here? Not until there's a lambda
1385        state that's pressure controlled.*/
1386     /*
1387        pVTerm = 0;
1388        where does this PV term go?
1389        for (i=0;i<nlim;i++)
1390        {
1391        fep_lamee[i] += pVTerm;
1392        }
1393      */
1394
1395     /* determine the minimum value to avoid overflow.  Probably a better way to do this */
1396     /* we don't need to include the pressure term, since the volume is the same between the two.
1397        is there some term we are neglecting, however? */
1398
1399     if (ir->efep != FreeEnergyPerturbationType::No)
1400     {
1401         for (i = 0; i < nlim; i++)
1402         {
1403             if (ir->bSimTemp)
1404             {
1405                 /* Note -- this assumes no mass changes, since kinetic energy is not added  . . . */
1406                 scaled_lamee[i] =
1407                         enerd->foreignLambdaTerms.deltaH(i) / (simtemp->temperatures[i] * gmx::c_boltz)
1408                         + enerd->term[F_EPOT]
1409                                   * (1.0 / (simtemp->temperatures[i])
1410                                      - 1.0 / (simtemp->temperatures[fep_state]))
1411                                   / gmx::c_boltz;
1412             }
1413             else
1414             {
1415                 scaled_lamee[i] = enerd->foreignLambdaTerms.deltaH(i) / (expand->mc_temp * gmx::c_boltz);
1416                 /* mc_temp is currently set to the system reft unless otherwise defined */
1417             }
1418
1419             /* save these energies for printing, so they don't get overwritten by the next step */
1420             /* they aren't overwritten in the non-free energy case, but we always print with these
1421                for simplicity */
1422         }
1423     }
1424     else
1425     {
1426         if (ir->bSimTemp)
1427         {
1428             for (i = 0; i < nlim; i++)
1429             {
1430                 scaled_lamee[i] =
1431                         enerd->term[F_EPOT]
1432                         * (1.0 / simtemp->temperatures[i] - 1.0 / simtemp->temperatures[fep_state])
1433                         / gmx::c_boltz;
1434             }
1435         }
1436     }
1437
1438     for (i = 0; i < nlim; i++)
1439     {
1440         pfep_lamee[i] = scaled_lamee[i];
1441
1442         weighted_lamee[i] = dfhist->sum_weights[i] - scaled_lamee[i];
1443         if (i == 0)
1444         {
1445             maxscaled   = scaled_lamee[i];
1446             maxweighted = weighted_lamee[i];
1447         }
1448         else
1449         {
1450             if (scaled_lamee[i] > maxscaled)
1451             {
1452                 maxscaled = scaled_lamee[i];
1453             }
1454             if (weighted_lamee[i] > maxweighted)
1455             {
1456                 maxweighted = weighted_lamee[i];
1457             }
1458         }
1459     }
1460
1461     for (i = 0; i < nlim; i++)
1462     {
1463         scaled_lamee[i] -= maxscaled;
1464         weighted_lamee[i] -= maxweighted;
1465     }
1466
1467     /* update weights - we decide whether or not to actually do this inside */
1468
1469     bDoneEquilibrating =
1470             UpdateWeights(nlim, expand, dfhist, fep_state, scaled_lamee, weighted_lamee, step);
1471     if (bDoneEquilibrating)
1472     {
1473         if (log)
1474         {
1475             fprintf(log,
1476                     "\nStep %" PRId64 ": Weights have equilibrated, using criteria: %s\n",
1477                     step,
1478                     enumValueToString(expand->elmceq));
1479         }
1480     }
1481
1482     lamnew = ChooseNewLambda(
1483             nlim, expand, dfhist, fep_state, weighted_lamee, p_k, ir->expandedvals->lmc_seed, step);
1484     /* if using simulated tempering, we need to adjust the temperatures */
1485     if (ir->bSimTemp && (lamnew != fep_state)) /* only need to change the temperatures if we change the state */
1486     {
1487         int   i, j, n, d;
1488         real* buf_ngtc;
1489         real  told;
1490         int   nstart, nend, gt;
1491
1492         snew(buf_ngtc, ir->opts.ngtc);
1493
1494         for (i = 0; i < ir->opts.ngtc; i++)
1495         {
1496             if (ir->opts.ref_t[i] > 0)
1497             {
1498                 told              = ir->opts.ref_t[i];
1499                 ir->opts.ref_t[i] = simtemp->temperatures[lamnew];
1500                 buf_ngtc[i]       = std::sqrt(ir->opts.ref_t[i] / told); /* using the buffer as temperature scaling */
1501             }
1502         }
1503
1504         /* we don't need to manipulate the ekind information, as it isn't due to be reset until the next step anyway */
1505
1506         nstart = 0;
1507         nend   = mdatoms->homenr;
1508         for (n = nstart; n < nend; n++)
1509         {
1510             gt = 0;
1511             if (mdatoms->cTC)
1512             {
1513                 gt = mdatoms->cTC[n];
1514             }
1515             for (d = 0; d < DIM; d++)
1516             {
1517                 v[n][d] *= buf_ngtc[gt];
1518             }
1519         }
1520
1521         if (inputrecNptTrotter(ir) || inputrecNphTrotter(ir) || inputrecNvtTrotter(ir))
1522         {
1523             /* we need to recalculate the masses if the temperature has changed */
1524             init_npt_masses(ir, state, MassQ, FALSE);
1525             for (i = 0; i < state->nnhpres; i++)
1526             {
1527                 for (j = 0; j < ir->opts.nhchainlength; j++)
1528                 {
1529                     state->nhpres_vxi[i + j] *= buf_ngtc[i];
1530                 }
1531             }
1532             for (i = 0; i < ir->opts.ngtc; i++)
1533             {
1534                 for (j = 0; j < ir->opts.nhchainlength; j++)
1535                 {
1536                     state->nosehoover_vxi[i + j] *= buf_ngtc[i];
1537                 }
1538             }
1539         }
1540         sfree(buf_ngtc);
1541     }
1542
1543     /* now check on the Wang-Landau updating critera */
1544
1545     if (EWL(expand->elamstats))
1546     {
1547         bSwitchtoOneOverT = FALSE;
1548         if (expand->bWLoneovert)
1549         {
1550             totalsamples = 0;
1551             for (i = 0; i < nlim; i++)
1552             {
1553                 totalsamples += dfhist->n_at_lam[i];
1554             }
1555             oneovert = (1.0 * nlim) / totalsamples;
1556             /* oneovert has decreasd by a bit since last time, so we actually make sure its within one of this number */
1557             /* switch to 1/t incrementing when wl_delta has decreased at least once, and wl_delta is now less than 1/t */
1558             if ((dfhist->wl_delta <= ((totalsamples) / (totalsamples - 1.00001)) * oneovert)
1559                 && (dfhist->wl_delta < expand->init_wl_delta))
1560             {
1561                 bSwitchtoOneOverT = TRUE;
1562             }
1563         }
1564         if (bSwitchtoOneOverT)
1565         {
1566             dfhist->wl_delta =
1567                     oneovert; /* now we reduce by this each time, instead of only at flatness */
1568         }
1569         else
1570         {
1571             bIfReset = CheckHistogramRatios(nlim, dfhist->wl_histo, expand->wl_ratio);
1572             if (bIfReset)
1573             {
1574                 for (i = 0; i < nlim; i++)
1575                 {
1576                     dfhist->wl_histo[i] = 0;
1577                 }
1578                 dfhist->wl_delta *= expand->wl_scale;
1579                 if (log)
1580                 {
1581                     fprintf(log, "\nStep %d: weights are now:", static_cast<int>(step));
1582                     for (i = 0; i < nlim; i++)
1583                     {
1584                         fprintf(log, " %.5f", dfhist->sum_weights[i]);
1585                     }
1586                     fprintf(log, "\n");
1587                 }
1588             }
1589         }
1590     }
1591     sfree(pfep_lamee);
1592     sfree(scaled_lamee);
1593     sfree(weighted_lamee);
1594     sfree(p_k);
1595
1596     return lamnew;
1597 }