Fix Visual Studio build
[alexxy/gromacs.git] / src / gromacs / mdtypes / energyhistory.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020, 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 #include "gmxpre.h"
37
38 #include "energyhistory.h"
39
40 #include "gromacs/utility/exceptions.h"
41 #include "gromacs/utility/stringutil.h"
42
43 #include "checkpointdata.h"
44
45 //! \cond INTERNAL
46 // mirroring the \cond from energyhistory.h to avoid Doxygen errors
47
48 namespace
49 {
50 /*!
51  * \brief Enum describing the contents delta_h_history_t writes to modular checkpoint
52  *
53  * When changing the checkpoint content, add a new element just above Count, and adjust the
54  * checkpoint functionality.
55  */
56 enum class DeltaHHistoryCheckpointVersion
57 {
58     Base, //!< First version of modular checkpointing
59     Count //!< Number of entries. Add new versions right above this!
60 };
61 constexpr auto c_currentVersionDeltaHH =
62         DeltaHHistoryCheckpointVersion(int(DeltaHHistoryCheckpointVersion::Count) - 1);
63 } // namespace
64
65 //! Read / write vector size from / to checkpoint and resize vector if reading
66 template<gmx::CheckpointDataOperation operation, typename T>
67 static void checkpointVectorSize(gmx::CheckpointData<operation>* checkpointData,
68                                  const std::string&              name,
69                                  std::vector<T>*                 vector)
70 {
71     auto size = static_cast<int64_t>(vector->size());
72     checkpointData->scalar(name, &size);
73     if (operation == gmx::CheckpointDataOperation::Read)
74     {
75         vector->resize(size);
76     }
77 };
78
79 template<gmx::CheckpointDataOperation operation>
80 void delta_h_history_t::doCheckpoint(gmx::CheckpointData<operation> checkpointData)
81 {
82     auto version = c_currentVersionDeltaHH;
83     checkpointData.enumScalar("version", &version);
84     if (version != c_currentVersionDeltaHH)
85     {
86         throw gmx::FileIOError("delta_h_history_t checkpoint version mismatch.");
87     }
88
89     checkpointVectorSize(&checkpointData, "numDeltaH", &dh);
90     checkpointData.scalar("start_time", &start_time);
91     checkpointData.scalar("start_lambda", &start_lambda);
92     checkpointData.scalar("start_lambda_set", &start_lambda_set);
93     for (std::size_t idx = 0; idx < dh.size(); ++idx)
94     {
95         checkpointVectorSize(&checkpointData, gmx::formatString("vecSize %zu", idx), &dh[idx]);
96         checkpointData.arrayRef(gmx::formatString("vec %zu", idx),
97                                 gmx::makeCheckpointArrayRef<operation>(dh[idx]));
98     }
99 }
100
101 namespace
102 {
103 /*!
104  * \brief Enum describing the contents energyhistory_t writes to modular checkpoint
105  *
106  * When changing the checkpoint content, add a new element just above Count, and adjust the
107  * checkpoint functionality.
108  */
109 enum class EnergyHistoryCheckpointVersion
110 {
111     Base, //!< First version of modular checkpointing
112     Count //!< Number of entries. Add new versions right above this!
113 };
114 constexpr auto c_currentVersionEnergyHistory =
115         EnergyHistoryCheckpointVersion(int(EnergyHistoryCheckpointVersion::Count) - 1);
116 } // namespace
117
118 template<gmx::CheckpointDataOperation operation>
119 void energyhistory_t::doCheckpoint(gmx::CheckpointData<operation> checkpointData)
120 {
121     auto version = c_currentVersionEnergyHistory;
122     checkpointData.enumScalar("version", &version);
123     if (version != c_currentVersionEnergyHistory)
124     {
125         throw gmx::FileIOError("energyhistory_t checkpoint version mismatch.");
126     }
127
128     bool useCheckpoint = (nsum <= 0 && nsum_sim <= 0);
129     checkpointData.scalar("useCheckpoint", &useCheckpoint);
130
131     if (!useCheckpoint)
132     {
133         return;
134     }
135
136     checkpointVectorSize(&checkpointData, "enerAveSize", &ener_ave);
137     checkpointVectorSize(&checkpointData, "enerSumSize", &ener_sum);
138     checkpointVectorSize(&checkpointData, "enerSumSimSize", &ener_sum_sim);
139
140     checkpointData.scalar("nsteps", &nsteps);
141     checkpointData.scalar("nsteps_sim", &nsteps_sim);
142
143     checkpointData.scalar("nsum", &nsum);
144     checkpointData.scalar("nsum_sim", &nsum_sim);
145
146     auto hasForeignLambdas = (deltaHForeignLambdas != nullptr);
147     checkpointData.scalar("has foreign lambdas", &hasForeignLambdas);
148     if (hasForeignLambdas && deltaHForeignLambdas == nullptr)
149     {
150         deltaHForeignLambdas = std::make_unique<delta_h_history_t>();
151     }
152
153     if (nsum > 0)
154     {
155         checkpointData.arrayRef("ener_ave", gmx::makeCheckpointArrayRef<operation>(ener_ave));
156         checkpointData.arrayRef("ener_sum", gmx::makeCheckpointArrayRef<operation>(ener_sum));
157     }
158     if (nsum_sim > 0)
159     {
160         checkpointData.arrayRef("ener_sum_sim", gmx::makeCheckpointArrayRef<operation>(ener_sum_sim));
161     }
162     if (hasForeignLambdas)
163     {
164         deltaHForeignLambdas->doCheckpoint<operation>(
165                 checkpointData.subCheckpointData("deltaHForeignLambdas"));
166     }
167 }
168
169 // explicit template instatiation
170 template void energyhistory_t::doCheckpoint(gmx::CheckpointData<gmx::CheckpointDataOperation::Read> checkpointData);
171 template void energyhistory_t::doCheckpoint(gmx::CheckpointData<gmx::CheckpointDataOperation::Write> checkpointData);
172
173
174 //! \endcond