2 * This file is part of the GROMACS molecular simulation package.
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.
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.
38 #include "energyhistory.h"
40 #include "gromacs/utility/exceptions.h"
41 #include "gromacs/utility/stringutil.h"
43 #include "checkpointdata.h"
46 // mirroring the \cond from energyhistory.h to avoid Doxygen errors
51 * \brief Enum describing the contents delta_h_history_t writes to modular checkpoint
53 * When changing the checkpoint content, add a new element just above Count, and adjust the
54 * checkpoint functionality.
56 enum class DeltaHHistoryCheckpointVersion
58 Base, //!< First version of modular checkpointing
59 Count //!< Number of entries. Add new versions right above this!
61 constexpr auto c_currentVersionDeltaHH =
62 DeltaHHistoryCheckpointVersion(int(DeltaHHistoryCheckpointVersion::Count) - 1);
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)
71 auto size = static_cast<int64_t>(vector->size());
72 checkpointData->scalar(name, &size);
73 if (operation == gmx::CheckpointDataOperation::Read)
79 template<gmx::CheckpointDataOperation operation>
80 void delta_h_history_t::doCheckpoint(gmx::CheckpointData<operation> checkpointData)
82 auto version = c_currentVersionDeltaHH;
83 checkpointData.enumScalar("version", &version);
84 if (version != c_currentVersionDeltaHH)
86 throw gmx::FileIOError("delta_h_history_t checkpoint version mismatch.");
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)
95 checkpointVectorSize(&checkpointData, gmx::formatString("vecSize %zu", idx), &dh[idx]);
96 checkpointData.arrayRef(gmx::formatString("vec %zu", idx),
97 gmx::makeCheckpointArrayRef<operation>(dh[idx]));
104 * \brief Enum describing the contents energyhistory_t writes to modular checkpoint
106 * When changing the checkpoint content, add a new element just above Count, and adjust the
107 * checkpoint functionality.
109 enum class EnergyHistoryCheckpointVersion
111 Base, //!< First version of modular checkpointing
112 Count //!< Number of entries. Add new versions right above this!
114 constexpr auto c_currentVersionEnergyHistory =
115 EnergyHistoryCheckpointVersion(int(EnergyHistoryCheckpointVersion::Count) - 1);
118 template<gmx::CheckpointDataOperation operation>
119 void energyhistory_t::doCheckpoint(gmx::CheckpointData<operation> checkpointData)
121 auto version = c_currentVersionEnergyHistory;
122 checkpointData.enumScalar("version", &version);
123 if (version != c_currentVersionEnergyHistory)
125 throw gmx::FileIOError("energyhistory_t checkpoint version mismatch.");
128 bool useCheckpoint = (nsum <= 0 && nsum_sim <= 0);
129 checkpointData.scalar("useCheckpoint", &useCheckpoint);
136 checkpointVectorSize(&checkpointData, "enerAveSize", &ener_ave);
137 checkpointVectorSize(&checkpointData, "enerSumSize", &ener_sum);
138 checkpointVectorSize(&checkpointData, "enerSumSimSize", &ener_sum_sim);
140 checkpointData.scalar("nsteps", &nsteps);
141 checkpointData.scalar("nsteps_sim", &nsteps_sim);
143 checkpointData.scalar("nsum", &nsum);
144 checkpointData.scalar("nsum_sim", &nsum_sim);
146 auto hasForeignLambdas = (deltaHForeignLambdas != nullptr);
147 checkpointData.scalar("has foreign lambdas", &hasForeignLambdas);
148 if (hasForeignLambdas && deltaHForeignLambdas == nullptr)
150 deltaHForeignLambdas = std::make_unique<delta_h_history_t>();
155 checkpointData.arrayRef("ener_ave", gmx::makeCheckpointArrayRef<operation>(ener_ave));
156 checkpointData.arrayRef("ener_sum", gmx::makeCheckpointArrayRef<operation>(ener_sum));
160 checkpointData.arrayRef("ener_sum_sim", gmx::makeCheckpointArrayRef<operation>(ener_sum_sim));
162 if (hasForeignLambdas)
164 deltaHForeignLambdas->doCheckpoint<operation>(
165 checkpointData.subCheckpointData("deltaHForeignLambdas"));
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);