Make AWH parameters proper C++
[alexxy/gromacs.git] / src / gromacs / mdtypes / awh_params.h
index debbb846728fff2816f39523c2fe3e6b1b26326c..729707e0e01050fb820fc0ce390366d56dda2a98 100644 (file)
 #ifndef GMX_MDTYPES_AWH_PARAMS_H
 #define GMX_MDTYPES_AWH_PARAMS_H
 
+#include <vector>
+
 #include "gromacs/mdtypes/md_enums.h"
+#include "gromacs/utility/arrayref.h"
 #include "gromacs/utility/basedefinitions.h"
+#include "gromacs/utility/classhelpers.h"
+
+struct t_inpfile;
+struct t_inputrec;
+struct pull_params_t;
+using warninp_t = struct warninp*;
 
 namespace gmx
 {
 
+class ISerializer;
 //! Target distribution enum.
 enum class AwhTargetType : int
 {
@@ -103,53 +113,196 @@ enum class AwhCoordinateProviderType : int
 //! String for AWH bias reaction coordinate provider.
 const char* enumValueToString(AwhCoordinateProviderType enumValue);
 
-/*! \cond INTERNAL */
-
-//! Parameters for an AWH coordinate dimension.
-struct AwhDimParams
+class AwhDimParams
 {
-    AwhCoordinateProviderType eCoordProvider; /**< The module providing the reaction coordinate. */
-    int                       coordIndex;     /**< Index of reaction coordinate in the provider. */
-    double                    origin;         /**< Start value of the interval. */
-    double                    end;            /**< End value of the interval. */
-    double                    period; /**< The period of this dimension (= 0 if not periodic). */
-    double                    forceConstant; /**< The force constant in kJ/mol/nm^2, kJ/mol/rad^2 */
-    double diffusion; /**< Estimated diffusion constant in units of nm^2/ps, rad^2/ps or ps^-1. */
-    double coordValueInit; /**< The initial coordinate value. */
-    double coverDiameter; /**< The diameter that needs to be sampled around a point before it is considered covered. */
-};
+public:
+    //! Constructor from input file.
+    AwhDimParams(std::vector<t_inpfile>* inp,
+                 const std::string&      prefix,
+                 const t_inputrec&       ir,
+                 warninp_t               wi,
+                 bool                    bComment);
+    //! Constructor to generate from file reading.
+    explicit AwhDimParams(ISerializer* serializer);
 
-//! Parameters for an AWH bias.
-struct AwhBiasParams
-{
-    // TODO: Turn dimParams into a std::vector when moved into AWH module
-    int           ndim;       /**< Dimension of the coordinate space. */
-    AwhDimParams* dimParams;  /**< AWH parameters per dimension. */
-    AwhTargetType eTarget;    /**< Type of target distribution. */
-    double targetBetaScaling; /**< Beta scaling value for Boltzmann type target distributions. */
-    double targetCutoff; /**< Free energy cutoff value for cutoff type target distribution in kJ/mol.*/
-    AwhHistogramGrowthType eGrowth; /**< How the biasing histogram grows. */
-    bool     bUserData;    /**< Is there a user-defined initial PMF estimate and target estimate? */
-    double   errorInitial; /**< Estimated initial free energy error in kJ/mol. */
-    int      shareGroup; /**< When >0, the bias is shared with biases of the same group and across multiple simulations when shareBiasMultisim=true */
-    gmx_bool equilibrateHistogram; /**< True if the simulation starts out by equilibrating the histogram. */
+    //! Move constructor.
+    AwhDimParams(AwhDimParams&&) = default;
+    //! Move assignment operator.
+    AwhDimParams& operator=(AwhDimParams&&) = default;
+    //! Delete copy constructor.
+    AwhDimParams(const AwhDimParams&) = delete;
+    //! Delete copy assignment.
+    AwhDimParams& operator=(const AwhDimParams&) = delete;
+
+    //! Which module is providing the reaction coordinate.
+    AwhCoordinateProviderType coordinateProvider() const { return eCoordProvider_; }
+    //! Index for reaction coordinate in provider.
+    int coordinateIndex() const { return coordIndex_; }
+    //! Start value for interval.
+    double origin() const { return origin_; }
+    //! End value for interval.
+    double end() const { return end_; }
+    //! Period for the dimension.
+    double period() const { return period_; }
+    //! Set period value dependent on state.
+    void setPeriod(double period) { period_ = period; }
+    //! Force constant for this dimension.
+    double forceConstant() const { return forceConstant_; }
+    //! Estimated diffusion constant.
+    double diffusion() const { return diffusion_; }
+    //! Initial value for coordinate.
+    double initialCoordinate() const { return coordValueInit_; }
+    //! Set initial coordinate value dependent on state.
+    void setInitialCoordinate(double initialCoordinate) { coordValueInit_ = initialCoordinate; }
+    //! Diameter needed to be sampled.
+    double coverDiameter() const { return coverDiameter_; }
+    //! Write datastructure.
+    void serialize(ISerializer* serializer);
+
+private:
+    //! The module providing the reaction coordinate.
+    AwhCoordinateProviderType eCoordProvider_;
+    //! Index of reaction coordinate in the provider.
+    int coordIndex_;
+    //! Start value of the interval.
+    double origin_;
+    //! End value of the interval.
+    double end_;
+    //! The period of this dimension (= 0 if not periodic).
+    double period_;
+    //! The force constant in kJ/mol/nm^2, kJ/mol/rad^2
+    double forceConstant_;
+    //! Estimated diffusion constant in units of nm^2/ps or rad^2/ps or ps^-1.
+    double diffusion_;
+    //! The initial coordinate value.
+    double coordValueInit_;
+    //! The diameter that needs to be sampled around a point before it is considered covered.
+    double coverDiameter_;
 };
 
-//! Parameters for AWH.
-struct AwhParams
+class AwhBiasParams
 {
-    // TODO: Turn awhBiasParams into a std::vector when moved into AWH module
-    int            numBias;       /**< The number of AWH biases.*/
-    AwhBiasParams* awhBiasParams; /**< AWH bias parameters.*/
-    int64_t        seed;          /**< Random seed.*/
-    int            nstOut;        /**< Output step interval.*/
-    int nstSampleCoord; /**< Number of samples per coordinate sample (also used for PMF) */
-    int numSamplesUpdateFreeEnergy; /**< Number of samples per free energy update. */
-    AwhPotentialType ePotential;    /**< Type of potential. */
-    gmx_bool         shareBiasMultisim; /**< When true, share biases with shareGroup>0 between multi-simulations */
+public:
+    //! Constructor from input file.
+    AwhBiasParams(std::vector<t_inpfile>* inp,
+                  const std::string&      prefix,
+                  const t_inputrec&       ir,
+                  warninp_t               wi,
+                  bool                    bComment);
+    //! Constructor to generate from file reading.
+    explicit AwhBiasParams(ISerializer* serializer);
+
+    //! Move constructor.
+    AwhBiasParams(AwhBiasParams&&) = default;
+    //! Move assignment operator.
+    AwhBiasParams& operator=(AwhBiasParams&&) = default;
+    //! Delete copy constructor.
+    AwhBiasParams(const AwhBiasParams&) = delete;
+    //! Delete copy assignment.
+    AwhBiasParams& operator=(const AwhBiasParams&) = delete;
+
+    //! Which target distribution is searched.
+    AwhTargetType targetDistribution() const { return eTarget_; }
+    //! Beta scaling to reach target distribution.
+    double targetBetaScaling() const { return targetBetaScaling_; }
+    //! Cutoff for target.
+    double targetCutoff() const { return targetCutoff_; }
+    //! Which kind of growth to use.
+    AwhHistogramGrowthType growthType() const { return eGrowth_; }
+    //! User provided PMF estimate.
+    bool userPMFEstimate() const { return bUserData_; }
+    //! Estimated initial free energy error in kJ/mol.
+    double initialErrorEstimate() const { return errorInitial_; }
+    //! Dimensions of coordinate space.
+    int ndim() const { return dimParams_.size(); }
+    //! Number of groups to share this bias with.
+    int shareGroup() const { return shareGroup_; }
+    //! If the simulation starts with equilibrating histogram.
+    bool equilibrateHistogram() const { return equilibrateHistogram_; }
+    //! Access to dimension parameters.
+    ArrayRef<AwhDimParams> dimParams() { return dimParams_; }
+    //! Const access to dimension parameters.
+    ArrayRef<const AwhDimParams> dimParams() const { return dimParams_; }
+    //! Write datastructure.
+    void serialize(ISerializer* serializer);
+
+private:
+    //! AWH parameters per dimension.
+    std::vector<AwhDimParams> dimParams_;
+    //! Type of target distribution.
+    AwhTargetType eTarget_;
+    //! Beta scaling value for Boltzmann type target distributions.
+    double targetBetaScaling_;
+    //! Free energy cutoff value for cutoff type target distribution in kJ/mol.
+    double targetCutoff_;
+    //! How the biasing histogram grows.
+    AwhHistogramGrowthType eGrowth_;
+    //! Is there a user-defined initial PMF estimate and target estimate?
+    bool bUserData_;
+    //! Estimated initial free energy error in kJ/mol.
+    double errorInitial_;
+    //! When >0, the bias is shared with biases of the same group and across multiple simulations when shareBiasMultisim=true
+    int shareGroup_;
+    //! True if the simulation starts out by equilibrating the histogram.
+    bool equilibrateHistogram_;
 };
+/*! \brief
+ * Structure holding parameter information for AWH.
+ */
+class AwhParams
+{
+public:
+    //! Constructor from input file.
+    AwhParams(std::vector<t_inpfile>* inp, const t_inputrec& ir, warninp_t wi);
+    //! Constructor used to generate awh parameter from file reading.
+    explicit AwhParams(ISerializer* serializer);
 
-/*! \endcond */
+    //! Move constructor.
+    AwhParams(AwhParams&&) = default;
+    //! Move assignment operator.
+    AwhParams& operator=(AwhParams&&) = default;
+    //! Delete copy constructor.
+    AwhParams(const AwhParams&) = delete;
+    //! Delete copy assignment.
+    AwhParams& operator=(const AwhParams&) = delete;
+
+    //! Get number of biases.
+    int numBias() const { return awhBiasParams_.size(); }
+    //! Get access to bias parameters.
+    ArrayRef<AwhBiasParams> awhBiasParams() { return awhBiasParams_; }
+    //! Const access to bias parameters.
+    ArrayRef<const AwhBiasParams> awhBiasParams() const { return awhBiasParams_; }
+    //! What king of potential is being used. \todo should use actual enum class.
+    AwhPotentialType potential() const { return potentialEnum_; }
+    //! Seed used for starting AWH.
+    int64_t seed() const { return seed_; }
+    //! Output step interval.
+    int nstout() const { return nstOut_; }
+    //! Number of samples per coordinate sample.
+    int nstSampleCoord() const { return nstSampleCoord_; }
+    //! Number of samples per free energy update.
+    int numSamplesUpdateFreeEnergy() const { return numSamplesUpdateFreeEnergy_; }
+    //! If biases are shared in multisim.
+    bool shareBiasMultisim() const { return shareBiasMultisim_; }
+    //! Serialize awh parameters.
+    void serialize(ISerializer* serializer);
+
+private:
+    //! AWH bias parameters.
+    std::vector<AwhBiasParams> awhBiasParams_;
+    //! Random seed.
+    int64_t seed_;
+    //! Output step interval.
+    int nstOut_;
+    //! Number of samples per coordinate sample (also used for PMF)
+    int nstSampleCoord_;
+    //! Number of samples per free energy update.
+    int numSamplesUpdateFreeEnergy_;
+    //! Type of potential.
+    AwhPotentialType potentialEnum_;
+    //! Whether to share biases with shareGroup>0 between multi-simulations.
+    bool shareBiasMultisim_;
+};
 
 } // namespace gmx