Fix random typos
[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     gmx::checkpointVersion(&checkpointData, "delta_h_history_t version", c_currentVersionDeltaHH);
83
84     checkpointVectorSize(&checkpointData, "numDeltaH", &dh);
85     checkpointData.scalar("start_time", &start_time);
86     checkpointData.scalar("start_lambda", &start_lambda);
87     checkpointData.scalar("start_lambda_set", &start_lambda_set);
88     for (std::size_t idx = 0; idx < dh.size(); ++idx)
89     {
90         checkpointVectorSize(&checkpointData, gmx::formatString("vecSize %zu", idx), &dh[idx]);
91         checkpointData.arrayRef(gmx::formatString("vec %zu", idx),
92                                 gmx::makeCheckpointArrayRef<operation>(dh[idx]));
93     }
94 }
95
96 namespace
97 {
98 /*!
99  * \brief Enum describing the contents energyhistory_t writes to modular checkpoint
100  *
101  * When changing the checkpoint content, add a new element just above Count, and adjust the
102  * checkpoint functionality.
103  */
104 enum class EnergyHistoryCheckpointVersion
105 {
106     Base, //!< First version of modular checkpointing
107     Count //!< Number of entries. Add new versions right above this!
108 };
109 constexpr auto c_currentVersionEnergyHistory =
110         EnergyHistoryCheckpointVersion(int(EnergyHistoryCheckpointVersion::Count) - 1);
111 } // namespace
112
113 template<gmx::CheckpointDataOperation operation>
114 void energyhistory_t::doCheckpoint(gmx::CheckpointData<operation> checkpointData)
115 {
116     gmx::checkpointVersion(&checkpointData, "energyhistory_t version", c_currentVersionEnergyHistory);
117
118     bool useCheckpoint = (nsum <= 0 && nsum_sim <= 0);
119     checkpointData.scalar("useCheckpoint", &useCheckpoint);
120
121     if (!useCheckpoint)
122     {
123         return;
124     }
125
126     checkpointVectorSize(&checkpointData, "enerAveSize", &ener_ave);
127     checkpointVectorSize(&checkpointData, "enerSumSize", &ener_sum);
128     checkpointVectorSize(&checkpointData, "enerSumSimSize", &ener_sum_sim);
129
130     checkpointData.scalar("nsteps", &nsteps);
131     checkpointData.scalar("nsteps_sim", &nsteps_sim);
132
133     checkpointData.scalar("nsum", &nsum);
134     checkpointData.scalar("nsum_sim", &nsum_sim);
135
136     auto hasForeignLambdas = (deltaHForeignLambdas != nullptr);
137     checkpointData.scalar("has foreign lambdas", &hasForeignLambdas);
138     if (hasForeignLambdas && deltaHForeignLambdas == nullptr)
139     {
140         deltaHForeignLambdas = std::make_unique<delta_h_history_t>();
141     }
142
143     if (nsum > 0)
144     {
145         checkpointData.arrayRef("ener_ave", gmx::makeCheckpointArrayRef<operation>(ener_ave));
146         checkpointData.arrayRef("ener_sum", gmx::makeCheckpointArrayRef<operation>(ener_sum));
147     }
148     if (nsum_sim > 0)
149     {
150         checkpointData.arrayRef("ener_sum_sim", gmx::makeCheckpointArrayRef<operation>(ener_sum_sim));
151     }
152     if (hasForeignLambdas)
153     {
154         deltaHForeignLambdas->doCheckpoint<operation>(
155                 checkpointData.subCheckpointData("deltaHForeignLambdas"));
156     }
157 }
158
159 // explicit template instatiation
160 template void energyhistory_t::doCheckpoint(gmx::CheckpointData<gmx::CheckpointDataOperation::Read> checkpointData);
161 template void energyhistory_t::doCheckpoint(gmx::CheckpointData<gmx::CheckpointDataOperation::Write> checkpointData);
162
163
164 //! \endcond