Modernize AWH enumerations
[alexxy/gromacs.git] / src / gromacs / applied_forces / awh / biasparams.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015,2016,2017,2018,2019,2020,2021, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35
36 /*! \internal \file
37  *
38  * \brief
39  * Declares the BiasParams class.
40  *
41  * This class holds the parameters for the bias. Most are direct copies
42  * of the input that the user provided. Some are a combination of user
43  * input and properties of the simulated system.
44  *
45  * \author Viveca Lindahl
46  * \author Berk Hess <hess@kth.se>
47  * \ingroup module_awh
48  */
49
50 #ifndef GMX_AWH_BIASPARAMS_H
51 #define GMX_AWH_BIASPARAMS_H
52
53 #include <vector>
54
55 #include "gromacs/math/vectypes.h"
56 #include "gromacs/utility/basedefinitions.h"
57
58 #include "dimparams.h"
59
60 namespace gmx
61 {
62
63 struct AwhBiasParams;
64 struct AwhParams;
65 struct DimParams;
66 class GridAxis;
67 enum class AwhTargetType : int;
68
69 /*! \internal \brief Constant parameters for the bias.
70  */
71 class BiasParams
72 {
73 public:
74     /*! \brief Switch to turn off update skips, useful for testing.
75      */
76     enum class DisableUpdateSkips
77     {
78         no, /**< Allow update skips (when supported by the method) */
79         yes /**< Disable update skips */
80     };
81
82     /*! \brief
83      * Check if the parameters permit skipping updates.
84      *
85      * Generally, we can skip updates of points that are non-local
86      * at the time of the update if we for later times, when the points
87      * with skipped updates have become local, know exactly how to apply
88      * the previous updates. The free energy updates only depend
89      * on local sampling, but the histogram rescaling factors
90      * generally depend on the histogram size (all samples).
91      * If the histogram size is kept constant or the scaling factors
92      * are trivial, this is not a problem. However, if the histogram growth
93      * is scaled down by some factor the size at the time of the update
94      * needs to be known. It would be fairly simple to, for a deterministically
95      * growing histogram, backtrack and calculate this value, but currently
96      * we just disallow this case. This is not a restriction because it
97      * only affects the local Boltzmann target type for which every update
98      * is currently anyway global because the target is always updated globally.
99      *
100      * \returns true when we can skip updates.
101      */
102     inline bool skipUpdates() const { return (!disableUpdateSkips_ && localWeightScaling == 1); }
103
104     /*! \brief
105      * Returns the radius that needs to be sampled around a point before it is considered covered.
106      */
107     inline const awh_ivec& coverRadius() const { return coverRadius_; }
108
109     /*! \brief
110      * Returns whether we should sample the coordinate.
111      *
112      * \param[in] step  The MD step number.
113      */
114     inline bool isSampleCoordStep(int64_t step) const
115     {
116         return (step > 0 && step % numStepsSampleCoord_ == 0);
117     }
118
119     /*! \brief
120      * Returns whether we should update the free energy.
121      *
122      * \param[in] step  The MD step number.
123      */
124     inline bool isUpdateFreeEnergyStep(int64_t step) const
125     {
126         int stepIntervalUpdateFreeEnergy = numSamplesUpdateFreeEnergy_ * numStepsSampleCoord_;
127         return (step > 0 && step % stepIntervalUpdateFreeEnergy == 0);
128     }
129
130     /*! \brief
131      * Returns whether we should update the target distribution.
132      *
133      * \param[in] step  The MD step number.
134      */
135     inline bool isUpdateTargetStep(int64_t step) const { return step % numStepsUpdateTarget_ == 0; }
136
137     /*! \brief
138      * Returns if to do checks for covering in the initial stage.
139      *
140      * To avoid overhead due to expensive checks, we do not check
141      * at every free energy update. However, if checks are
142      * performed too rarely the detection of coverings will be
143      * delayed, ultimately affecting free energy convergence.
144      *
145      * \param[in] step                  Time step.
146      * \returns true at steps where checks should be performed.
147      * \note  Only returns true at free energy update steps.
148      */
149     bool isCheckCoveringStep(int64_t step) const
150     {
151         return step > 0 && (step % numStepsCheckCovering_ == 0);
152     }
153
154     /*! \brief
155      * Returns if to perform checks for anomalies in the histogram.
156      *
157      * To avoid overhead due to expensive checks, we do not check
158      * at every free energy update. These checks are only used for
159      * warning the user and can be made as infrequently as
160      * neccessary without affecting the algorithm itself.
161      *
162      * \param[in] step                  Time step.
163      * \returns true at steps where checks should be performed.
164      * \note Only returns true at free energy update steps.
165      * \todo Currently this function just calls isCheckCoveringStep but the checks could be done less frequently.
166      */
167     bool isCheckHistogramForAnomaliesStep(int64_t step) const { return isCheckCoveringStep(step); }
168
169     /*! \brief Constructor.
170      *
171      * The local Boltzmann target distibution is defined by
172      * 1) Adding the sampled weights instead of the target weights to the reference weight histogram.
173      * 2) Scaling the weights of these samples by the beta scaling factor.
174      * 3) Setting the target distribution equal the reference weight histogram.
175      * This requires the following special update settings:
176      *   localWeightScaling      = targetParam
177      *   idealWeighthistUpdate   = false
178      * Note: these variables could in principle be set to something else also for other target distribution types.
179      * However, localWeightScaling < 1  is in general expected to give lower efficiency and, except for local Boltzmann,
180      * idealWeightHistUpdate = false gives (in my experience) unstable, non-converging results.
181      *
182      * \param[in] awhParams              AWH parameters.
183      * \param[in] awhBiasParams          Bias parameters.
184      * \param[in] dimParams              Bias dimension parameters.
185      * \param[in] beta                   1/(k_B T) in units of 1/(kJ/mol), should be > 0.
186      * \param[in] mdTimeStep             The MD time step.
187      * \param[in] numSharingSimulations  The number of simulations to share the bias across.
188      * \param[in] gridAxis               The grid axes.
189      * \param[in] disableUpdateSkips     If to disable update skips, useful for testing.
190      * \param[in] biasIndex              Index of the bias.
191      */
192     BiasParams(const AwhParams&              awhParams,
193                const AwhBiasParams&          awhBiasParams,
194                const std::vector<DimParams>& dimParams,
195                double                        beta,
196                double                        mdTimeStep,
197                DisableUpdateSkips            disableUpdateSkips,
198                int                           numSharingSimulations,
199                const std::vector<GridAxis>&  gridAxis,
200                int                           biasIndex);
201
202     /* Data members */
203     const double invBeta; /**< 1/beta = kT in kJ/mol */
204 private:
205     const int64_t numStepsSampleCoord_; /**< Number of steps per coordinate value sample. */
206 public:
207     const int numSamplesUpdateFreeEnergy_; /**< Number of samples per free energy update. */
208 private:
209     const int64_t numStepsUpdateTarget_; /**< Number of steps per updating the target distribution. */
210     const int64_t numStepsCheckCovering_; /**< Number of steps per checking for covering. */
211 public:
212     const AwhTargetType eTarget;              /**< Type of target distribution. */
213     const double        freeEnergyCutoffInKT; /**< Free energy cut-off in kT for cut-off target distribution. */
214     const double        temperatureScaleFactor; /**< Temperature scaling factor for temperature scaled targed distributions. */
215     const bool          idealWeighthistUpdate; /**< Update reference weighthistogram using the target distribution? Otherwise use the realized distribution. */
216     const int    numSharedUpdate; /**< The number of (multi-)simulations sharing the bias update */
217     const double updateWeight;    /**< The probability weight accumulated for each update. */
218     const double localWeightScaling; /**< Scaling factor applied to a sample before adding it to the reference weight histogram (= 1, usually). */
219     const double initialErrorInKT;   /**< Estimated initial free energy error in kT. */
220     const double initialHistogramSize; /**< Initial reference weight histogram size. */
221 private:
222     awh_ivec coverRadius_; /**< The radius (in points) that needs to be sampled around a point before it is considered covered. */
223 public:
224     const bool convolveForce; /**< True if we convolve the force, false means use MC between umbrellas. */
225     const int  biasIndex; /**< Index of the bias, used as a second random seed and for priting. */
226 private:
227     const bool disableUpdateSkips_; /**< If true, we disallow update skips, even when the method supports it. */
228 };
229
230 } // namespace gmx
231
232 #endif /* GMX_AWH_BIASPARAMS_H */