Add reading and writing to AWH module
[gromacs.git] / src / gromacs / awh / biaswriter.h
blob137b4b32d466a45de2b23eae34cc820ac77576bc
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2017, 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.
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.
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.
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.
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.
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.
36 /*! \internal \file
38 * \brief
39 * This file contains the BiasWriter class that prepares and writes data of a Bias to an energy file.
41 * \author Viveca Lindahl
42 * \author Berk Hess <hess@kth.se>
43 * \ingroup module_awh
46 #ifndef GMX_AWH_BIASWRITER_H
47 #define GMX_AWH_BIASWRITER_H
49 #include <map>
50 #include <vector>
52 #include "gromacs/fileio/enxio.h"
53 #include "gromacs/utility/arrayref.h"
54 #include "gromacs/utility/basedefinitions.h"
56 struct gmx_multisim_t;
57 struct t_enxsubblock;
59 namespace gmx
61 class Bias;
63 /* TODO: the post-simulations AWH reader and this AWH writer are totally
64 * disconnected although they read/write the same data. I'm not sure how
65 * to handle that or if it should be left as it is until the writing is done
66 * in a differen format (i.e. TNG) than the current energy file.
69 //! Enum with the AWH variables to write.
70 enum class AwhOutputEntryType
72 MetaData, //!< Meta data.
73 CoordValue, //!< Coordinate value.
74 Pmf, //!< The pmf.
75 Bias, //!< The bias.
76 Visits, //!< The number of visits.
77 Weights, //!< The weights.
78 Target //!< The target distribition.
81 //! Enum with the types of metadata to write.
82 enum class AwhOutputMetaData
84 NumBlock, //!< The number of blocks.
85 TargetError, //!< The target error.
86 ScaledSampleWeight, //!< The logarithm of the sample weight relative to a sample weight of 1 at the initial time.
87 Count //!< The number of enum values, not including Count.
90 //! Enum with different ways of normalizing the output.
91 enum class Normalization
93 None, //!< No normalization.
94 Coordinate, //!< Scale using the internal/user input coordinate scaling factor.
95 FreeEnergy, //!< Normalize free energy values by subtracting the minimum value.
96 Distribution //!< Normalize the distribution to 1.
99 /*! \internal \brief AWH output data block that can be written to an energy file block.
101 class AwhEnergyBlock
103 public:
104 /*! \brief Constructor
106 * \param[in] numPoints Number of points in block.
107 * \param[in] normalizationType Value for normalization type enum.
108 * \param[in] normalizationValue Normalization value.
110 AwhEnergyBlock(int numPoints,
111 Normalization normalizationType,
112 float normalizationValue);
114 /*! \brief Returns an ArrarRef to the data in the block.
116 gmx::ArrayRef<float> data()
118 return data_;
121 const Normalization normalizationType; /**< How to normalize the output data */
122 const float normalizationValue; /**< The normalization value */
123 private:
124 std::vector<float> data_; /**< The data, always float which is enough since this is statistical data */
127 /*! \internal \brief Class organizing the output data storing and writing of an AWH bias.
129 class BiasWriter
131 private:
132 /*! \brief Query if the writer has a block for the given variable.
134 * \param[in] outputType Output entry type.
136 bool hasVarBlock(AwhOutputEntryType outputType) const
138 return outputTypeToBlock_.find(outputType)->second >= 0;
141 /*! \brief* Find the first block containing the given variable.
143 * \param[in] outputType Output entry type.
144 * \returns the first block index for the variable, or -1 there is no block.
146 int getVarStartBlock(AwhOutputEntryType outputType) const
148 return outputTypeToBlock_.find(outputType)->second;
151 /*! \brief Transfer AWH point data to writer data blocks.
153 * \param[in] metaDataIndex Meta data block index.
154 * \param[in] metaDataType The type of meta data to write.
155 * \param[in] bias The AWH Bias.
157 void transferMetaDataToWriter(size_t metaDataIndex,
158 AwhOutputMetaData metaDataType,
159 const Bias &bias);
161 /*! \brief Transfer AWH point data to writer data blocks.
163 * \param[in] outputType Output entry type.
164 * \param[in] pointIndex The point index.
165 * \param[in] bias The AWH Bias.
166 * \param[in] pmf PMF values.
168 void transferPointDataToWriter(AwhOutputEntryType outputType,
169 int pointIndex,
170 const Bias &bias,
171 gmx::ArrayRef<const float> pmf);
173 public:
174 /*! \brief Constructor.
176 * \param[in] bias The AWH bias.
178 BiasWriter(const Bias &bias);
180 /*! \brief Returns the number of data blocks.
182 * \returns the number of data blocks.
184 int numBlocks() const
186 return block_.size();
189 private:
190 /*! \brief
191 * Prepare the bias output data.
193 * \param[in] bias The AWH Bias.
195 void prepareBiasOutput(const Bias &bias);
197 public:
198 /*! \brief Collect AWH bias data in blocks and write to energy subblocks.
200 * \param[in] bias The AWH Bias.
201 * \param[in,out] subblock Energy subblocks to write to.
202 * \returns the number of blocks written.
204 int writeToEnergySubblocks(const Bias &bias, t_enxsubblock *subblock);
206 private:
207 std::vector<AwhEnergyBlock> block_; /**< The data blocks */
208 std::map<AwhOutputEntryType, int> outputTypeToBlock_; /**< Start block index for each variable, -1 when variable should not be written */
211 } // namespace gmx
213 #endif /* GMX_AWH_BIASWRITER_H */