ecfb5288d7684b76714a79f83afa9f20bde727c9
[alexxy/gromacs.git] / src / gromacs / awh / correlationgrid.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015,2016,2017,2018, 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 CorrelationGrid class to collect correlation statistics on a grid, using several block lengths.
40  *
41  * \author Viveca Lindahl
42  * \author Berk Hess <hess@kth.se>
43  * \ingroup module_awh
44  */
45
46 #ifndef GMX_AWH_CORRELATIONGRID_H
47 #define GMX_AWH_CORRELATIONGRID_H
48
49 #include <cstddef>
50
51 #include <vector>
52
53 #include "gromacs/utility/arrayref.h"
54 #include "gromacs/utility/basedefinitions.h"
55 #include "gromacs/utility/gmxassert.h"
56
57 #include "correlationtensor.h"
58
59 namespace gmx
60 {
61
62 struct CorrelationGridHistory;
63
64 /*! \internal
65  * \brief Grid of local correlation tensors.
66  *
67  * This class provides the means for a bias to interaction with the grid
68  * of correlation tensors. The grid should have the same number of points
69  * and the same dimensionality as the bias grid.
70  */
71 class CorrelationGrid
72 {
73     public:
74         //! Enum that sets how we measure block length.
75         enum class BlockLengthMeasure
76         {
77             Time,   //!< Measure block length in time.
78             Weight  //!< Measure block length in sampled weight.
79         };
80
81         /*! \brief Constructor.
82          *
83          * \param[in] numPoints           Number of points in the grid.
84          * \param[in] numDims             Number of dimensions of the grid.
85          * \param[in] blockLengthInit     Initial length of the blocks used for block averaging.
86          * \param[in] blockLengthMeasure  Sets how we measure block length.
87          * \param[in] dtSample            Time step for sampling correlations.
88          */
89         CorrelationGrid(int                numPoints,
90                         int                numDims,
91                         double             blockLengthInit,
92                         BlockLengthMeasure blockLengthMeasure,
93                         double             dtSample);
94
95         /*! \brief Adds a weighted data vector to one point in the correlation grid.
96          *
97          * \param[in] pointIndex  Index of the point to add data to.
98          * \param[in] weight      Weight to assign to the data.
99          * \param[in] data        One data point for each grid dimension.
100          * \param[in] t           The time when the data was sampled.
101          */
102         void addData(int                          pointIndex,
103                      double                       weight,
104                      gmx::ArrayRef<const double>  data,
105                      double                       t)
106         {
107             tensors_[pointIndex].addData(weight, data, blockLengthMeasure == BlockLengthMeasure::Weight, t);
108         }
109
110         /*! \brief Restores the correlation grid state from the correlation grid history.
111          *
112          * The setup in the history should match that of this simulation.
113          * If this is not the case, an exception is thrown.
114          *
115          * \param[in] correlationGridHistory  The correlation grid state history.
116          */
117         void restoreStateFromHistory(const CorrelationGridHistory &correlationGridHistory);
118
119         /*! \brief Returns the number of elements in the tensor: dim*(dim+1)/2.
120          */
121         int tensorSize() const
122         {
123             GMX_RELEASE_ASSERT(tensors_.size() > 0, "Should only call tensorSize on a valid grid");
124
125             return tensors_[0].blockDataList()[0].correlationIntegral().size();
126         }
127
128         /*! \brief Returns the size of the block data list.
129          */
130         int blockDataListSize() const
131         {
132             GMX_RELEASE_ASSERT(tensors_.size() > 0, "Should only call tensorSize on a valid grid");
133
134             return tensors_[0].blockDataList().size();
135         }
136
137         /*! \brief Get a const reference to the correlation grid data.
138          */
139         const std::vector<CorrelationTensor> &tensors() const
140         {
141             return tensors_;
142         }
143
144         /* Right now the below functions are only used for an initial log printing. */
145
146         /*! \brief Get the current blocklength.
147          */
148         double getBlockLength() const;
149
150         /*! \brief Get the current number of blocks.
151          *
152          * If we have a finite block span we have a constant number of blocks,
153          * otherwise we are always adding more blocks (and we don't keep
154          * track of the number), so we return -1.
155          */
156         int getNumBlocks() const;
157
158     public:
159         const double                   dtSample;           /**< Time in between samples. */
160         const BlockLengthMeasure       blockLengthMeasure; /**< The measure for the block length. */
161     private:
162         std::vector<CorrelationTensor> tensors_;           /**< Correlation tensor grid */
163 };
164
165 }      // namespace gmx
166
167 #endif /* GMX_AWH_CORRELATIONGRID_H */