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 gmx::checkpointVersion(&checkpointData, "delta_h_history_t version", c_currentVersionDeltaHH);
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)
90 checkpointVectorSize(&checkpointData, gmx::formatString("vecSize %zu", idx), &dh[idx]);
91 checkpointData.arrayRef(gmx::formatString("vec %zu", idx),
92 gmx::makeCheckpointArrayRef<operation>(dh[idx]));
99 * \brief Enum describing the contents energyhistory_t writes to modular checkpoint
101 * When changing the checkpoint content, add a new element just above Count, and adjust the
102 * checkpoint functionality.
104 enum class EnergyHistoryCheckpointVersion
106 Base, //!< First version of modular checkpointing
107 Count //!< Number of entries. Add new versions right above this!
109 constexpr auto c_currentVersionEnergyHistory =
110 EnergyHistoryCheckpointVersion(int(EnergyHistoryCheckpointVersion::Count) - 1);
113 template<gmx::CheckpointDataOperation operation>
114 void energyhistory_t::doCheckpoint(gmx::CheckpointData<operation> checkpointData)
116 gmx::checkpointVersion(&checkpointData, "energyhistory_t version", c_currentVersionEnergyHistory);
118 bool useCheckpoint = (nsum <= 0 && nsum_sim <= 0);
119 checkpointData.scalar("useCheckpoint", &useCheckpoint);
126 checkpointVectorSize(&checkpointData, "enerAveSize", &ener_ave);
127 checkpointVectorSize(&checkpointData, "enerSumSize", &ener_sum);
128 checkpointVectorSize(&checkpointData, "enerSumSimSize", &ener_sum_sim);
130 checkpointData.scalar("nsteps", &nsteps);
131 checkpointData.scalar("nsteps_sim", &nsteps_sim);
133 checkpointData.scalar("nsum", &nsum);
134 checkpointData.scalar("nsum_sim", &nsum_sim);
136 auto hasForeignLambdas = (deltaHForeignLambdas != nullptr);
137 checkpointData.scalar("has foreign lambdas", &hasForeignLambdas);
138 if (hasForeignLambdas && deltaHForeignLambdas == nullptr)
140 deltaHForeignLambdas = std::make_unique<delta_h_history_t>();
145 checkpointData.arrayRef("ener_ave", gmx::makeCheckpointArrayRef<operation>(ener_ave));
146 checkpointData.arrayRef("ener_sum", gmx::makeCheckpointArrayRef<operation>(ener_sum));
150 checkpointData.arrayRef("ener_sum_sim", gmx::makeCheckpointArrayRef<operation>(ener_sum_sim));
152 if (hasForeignLambdas)
154 deltaHForeignLambdas->doCheckpoint<operation>(
155 checkpointData.subCheckpointData("deltaHForeignLambdas"));
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);