Split simulationWork.useGpuBufferOps into separate x and f flags
[alexxy/gromacs.git] / src / gromacs / mdtypes / awh_history.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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  * \brief Defines functions of the AWH history used by modular checkpointing
38  *
39  * \author Pascal Merz <pascal.merz@me.com>
40  */
41
42 #include "gmxpre.h"
43
44 #include "awh_history.h"
45
46 #include "gromacs/utility/strconvert.h"
47
48 #include "checkpointdata.h"
49
50 namespace gmx
51 {
52
53 namespace
54 {
55 /*!
56  * \brief Enum describing the contents all AWH history classes implemented in this file
57  *        write to modular checkpoint
58  *
59  * When changing the checkpoint content, add a new element just above Count, and adjust the
60  * checkpoint functionality.
61  */
62 enum class CheckpointVersion
63 {
64     Base, //!< First version of modular checkpointing
65     Count //!< Number of entries. Add new versions right above this!
66 };
67 constexpr auto c_currentVersion = CheckpointVersion(int(CheckpointVersion::Count) - 1);
68
69 template<CheckpointDataOperation operation>
70 static void doCheckpoint(CheckpointData<operation> checkpointData, AwhPointStateHistory* awhPointStateHistory)
71 {
72     checkpointData.scalar("bias", &awhPointStateHistory->bias);
73     checkpointData.scalar("free_energy", &awhPointStateHistory->free_energy);
74     checkpointData.scalar("target", &awhPointStateHistory->target);
75     checkpointData.scalar("weightsum_iteration", &awhPointStateHistory->weightsum_iteration);
76     checkpointData.scalar("weightsum_covering", &awhPointStateHistory->weightsum_covering);
77     checkpointData.scalar("weightsum_tot", &awhPointStateHistory->weightsum_tot);
78     checkpointData.scalar("weightsum_ref", &awhPointStateHistory->weightsum_ref);
79     checkpointData.scalar("last_update_index", &awhPointStateHistory->last_update_index);
80     checkpointData.scalar("log_pmfsum", &awhPointStateHistory->log_pmfsum);
81     checkpointData.scalar("visits_iteration", &awhPointStateHistory->visits_iteration);
82     checkpointData.scalar("visits_tot", &awhPointStateHistory->visits_tot);
83 }
84
85 template<CheckpointDataOperation operation>
86 static void doCheckpoint(CheckpointData<operation> checkpointData, AwhBiasStateHistory* awhBiasStateHistory)
87 {
88     checkpointData.scalar("umbrellaGridpoint", &awhBiasStateHistory->umbrellaGridpoint);
89     checkpointData.scalar("origin_index_updatelist", &awhBiasStateHistory->origin_index_updatelist);
90     checkpointData.scalar("end_index_updatelist", &awhBiasStateHistory->end_index_updatelist);
91     checkpointData.scalar("in_initial", &awhBiasStateHistory->in_initial);
92     checkpointData.scalar("equilibrateHistogram", &awhBiasStateHistory->equilibrateHistogram);
93     checkpointData.scalar("histSize", &awhBiasStateHistory->histSize);
94     checkpointData.scalar("logScaledSampleWeight", &awhBiasStateHistory->logScaledSampleWeight);
95     checkpointData.scalar("maxLogScaledSampleWeight", &awhBiasStateHistory->maxLogScaledSampleWeight);
96     checkpointData.scalar("numUpdates", &awhBiasStateHistory->numUpdates);
97 }
98
99 template<CheckpointDataOperation operation>
100 void doCheckpoint(CheckpointData<operation>    checkpointData,
101                   CorrelationBlockDataHistory* correlationBlockDataHistory)
102 {
103     checkpointData.scalar("blockSumWeight", &correlationBlockDataHistory->blockSumWeight);
104     checkpointData.scalar("blockSumSquareWeight", &correlationBlockDataHistory->blockSumSquareWeight);
105     checkpointData.scalar("blockSumWeightX", &correlationBlockDataHistory->blockSumWeightX);
106     checkpointData.scalar("blockSumWeightY", &correlationBlockDataHistory->blockSumWeightY);
107     checkpointData.scalar("sumOverBlocksSquareBlockWeight",
108                           &correlationBlockDataHistory->sumOverBlocksSquareBlockWeight);
109     checkpointData.scalar("sumOverBlocksBlockSquareWeight",
110                           &correlationBlockDataHistory->sumOverBlocksBlockSquareWeight);
111     checkpointData.scalar("sumOverBlocksBlockWeightBlockWeightX",
112                           &correlationBlockDataHistory->sumOverBlocksBlockWeightBlockWeightX);
113     checkpointData.scalar("sumOverBlocksBlockWeightBlockWeightY",
114                           &correlationBlockDataHistory->sumOverBlocksBlockWeightBlockWeightY);
115     checkpointData.scalar("blockLength", &correlationBlockDataHistory->blockLength);
116     checkpointData.scalar("previousBlockIndex", &correlationBlockDataHistory->previousBlockIndex);
117     checkpointData.scalar("correlationIntegral", &correlationBlockDataHistory->correlationIntegral);
118 }
119
120 template<CheckpointDataOperation operation>
121 static void doCheckpoint(CheckpointData<operation> checkpointData, CorrelationGridHistory* correlationGridHistory)
122 {
123     checkpointData.scalar("numCorrelationTensors", &correlationGridHistory->numCorrelationTensors);
124     checkpointData.scalar("tensorSize", &correlationGridHistory->tensorSize);
125     checkpointData.scalar("blockDataListSize", &correlationGridHistory->blockDataListSize);
126
127     int blockDataBufferSize = correlationGridHistory->blockDataBuffer.size();
128     checkpointData.scalar("blockDataBufferSize", &blockDataBufferSize);
129     if (operation == CheckpointDataOperation::Read)
130     {
131         correlationGridHistory->blockDataBuffer.resize(blockDataBufferSize);
132     }
133     int counter = 0;
134     for (auto& buffer : correlationGridHistory->blockDataBuffer)
135     {
136         doCheckpoint(checkpointData.subCheckpointData("blockDataBuffer " + toString(counter)), &buffer);
137         counter++;
138     }
139 }
140
141 template<CheckpointDataOperation operation>
142 void doCheckpointData(CheckpointData<operation> checkpointData, AwhBiasHistory* awhBiasHistory)
143 {
144     int pointStateSize = awhBiasHistory->pointState.size();
145     checkpointData.scalar("pointStateSize", &pointStateSize);
146     if (operation == CheckpointDataOperation::Read)
147     {
148         awhBiasHistory->pointState.resize(pointStateSize);
149     }
150
151     int counter = 0;
152     for (auto& point : awhBiasHistory->pointState)
153     {
154         doCheckpoint(checkpointData.subCheckpointData("pointState " + toString(counter)), &point);
155         counter++;
156     }
157     doCheckpoint(checkpointData.subCheckpointData("state"), &awhBiasHistory->state);
158     doCheckpoint(checkpointData.subCheckpointData("forceCorrelationGrid"),
159                  &awhBiasHistory->forceCorrelationGrid);
160 }
161 } // namespace
162
163 // This trips doxygen up
164 //! \cond
165 template<CheckpointDataOperation operation>
166 void AwhHistory::doCheckpoint(CheckpointData<operation> checkpointData)
167 {
168     // Keep one version for all objects in this file
169     checkpointVersion(&checkpointData, "AwhHistory version", c_currentVersion);
170
171     checkpointData.scalar("potentialOffset", &potentialOffset);
172
173     int biasSize = bias.size();
174     checkpointData.scalar("biasSize", &biasSize);
175     if (operation == CheckpointDataOperation::Read)
176     {
177         bias.resize(biasSize);
178     }
179     int counter = 0;
180     for (auto& b : bias)
181     {
182         doCheckpointData(checkpointData.subCheckpointData("bias " + toString(counter)), &b);
183         counter++;
184     }
185 }
186
187 // explicit template instantiation
188 template void AwhHistory::doCheckpoint(CheckpointData<CheckpointDataOperation::Read>);
189 template void AwhHistory::doCheckpoint(CheckpointData<CheckpointDataOperation::Write>);
190 //! \endcond
191
192 } // namespace gmx