2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018,2019, 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.
37 #include "gromacs/mdlib/energyoutput.h"
41 #include <gtest/gtest.h>
43 #include "gromacs/mdlib/ebin.h"
44 #include "gromacs/mdlib/makeconstraints.h"
45 #include "gromacs/mdtypes/inputrec.h"
46 #include "gromacs/mdtypes/mdatom.h"
47 #include "gromacs/topology/topology.h"
48 #include "gromacs/utility/textreader.h"
49 #include "gromacs/utility/unique_cptr.h"
51 #include "testutils/refdata.h"
52 #include "testutils/testasserts.h"
53 #include "testutils/testfilemanager.h"
62 //! Wraps fclose to discard the return value to use it as a deleter with gmx::unique_cptr.
63 void fcloseWrapper(FILE *fp)
68 class EnergyOutputTest : public ::testing::Test
71 TestFileManager fileManager_;
73 // Objects needed to make EnergyOutput
76 EnergyOutput energyOutput_;
78 // Objects needed for default energy output behavior.
80 std::unique_ptr<Constraints> constraints_;
81 matrix box_ = {{10, 0, 0}, {0, 10, 0}, {0, 0, 10}};
82 gmx_enerdata_t enerdata_;
83 tensor totalVirial_, pressure_;
85 // TODO This will be more elegant (and run faster) when we
86 // refactor the output routines to write to a stream
87 // interface, which can already be handled in-memory when
89 std::string logFilename_;
91 unique_cptr<FILE, fcloseWrapper> logFileGuard_;
93 TestReferenceData refData_;
94 TestReferenceChecker checker_;
97 logFilename_(fileManager_.getTemporaryFilePath(".log")),
98 log_(std::fopen(logFilename_.c_str(), "w")), logFileGuard_(log_),
99 checker_(refData_.rootChecker())
101 energyOutput_.prepare(nullptr, &mtop_, &inputrec_, nullptr, nullptr, false);
102 constraints_ = makeConstraints(mtop_, inputrec_, nullptr, false, log_, mdatoms_, nullptr,
103 nullptr, nullptr, nullptr, false);
105 //! Helper function to generate synthetic data to output
106 void setStepData(real testValue)
108 enerdata_.term[F_LJ] = (testValue += 0.1);
109 enerdata_.term[F_COUL_SR] = (testValue += 0.1);
110 enerdata_.term[F_EPOT] = (testValue += 0.1);
111 enerdata_.term[F_EKIN] = (testValue += 0.1);
112 enerdata_.term[F_ETOT] = (testValue += 0.1);
113 enerdata_.term[F_TEMP] = (testValue += 0.1);
114 enerdata_.term[F_PRES] = (testValue += 0.1);
115 totalVirial_[XX][XX] = (testValue += 0.1);
116 totalVirial_[XX][YY] = 0.0;
117 totalVirial_[XX][ZZ] = 0.0;
118 totalVirial_[YY][XX] = 0.0;
119 totalVirial_[YY][YY] = (testValue += 0.1);
120 totalVirial_[YY][ZZ] = 0.0;
121 totalVirial_[ZZ][XX] = 0.0;
122 totalVirial_[ZZ][YY] = 0.0;
123 totalVirial_[ZZ][ZZ] = (testValue += 0.1);
124 pressure_[XX][XX] = (testValue += 0.1);
125 pressure_[XX][YY] = 0.0;
126 pressure_[XX][ZZ] = 0.0;
127 pressure_[YY][XX] = 0.0;
128 pressure_[YY][YY] = (testValue += 0.1);
129 pressure_[YY][ZZ] = 0.0;
130 pressure_[ZZ][XX] = 0.0;
131 pressure_[ZZ][YY] = 0.0;
132 pressure_[ZZ][ZZ] = (testValue += 0.1);
137 TEST_F(EnergyOutputTest, HandlesEmptyAverages)
139 ASSERT_NE(log_, nullptr);
141 // Test printing values
142 energyOutput_.printStepToEnergyFile(nullptr, false, false, false, log_,
144 nullptr, nullptr, nullptr, nullptr);
145 // Test printing averages
146 energyOutput_.printStepToEnergyFile(nullptr, false, false, false, log_,
148 nullptr, nullptr, nullptr, nullptr);
150 // We need to close the file before the contents are available.
151 logFileGuard_.reset(nullptr);
153 checker_.checkInteger(energyOutput_.numEnergyTerms(), "Number of Energy Terms");
154 checker_.checkString(TextReader::readFileToString(logFilename_), "log");
157 TEST_F(EnergyOutputTest, HandlesSingleStep)
159 ASSERT_NE(log_, nullptr);
161 // Add synthetic data for a single step
163 real testValue = 1.0;
164 setStepData(testValue);
165 energyOutput_.addDataAtEnergyStep(false, true, time, 0.0, &enerdata_,
166 nullptr, nullptr, nullptr, box_,
167 nullptr, nullptr, totalVirial_, pressure_,
168 nullptr, nullptr, constraints_.get());
170 // Test printing values
171 energyOutput_.printStepToEnergyFile(nullptr, false, false, false, log_,
173 nullptr, nullptr, nullptr, nullptr);
175 // Test printing averages
176 energyOutput_.printStepToEnergyFile(nullptr, false, false, false, log_,
178 nullptr, nullptr, nullptr, nullptr);
180 // We need to close the file before the contents are available.
181 logFileGuard_.reset(nullptr);
183 checker_.checkInteger(energyOutput_.numEnergyTerms(), "Number of Energy Terms");
184 checker_.checkString(TextReader::readFileToString(logFilename_), "log");
187 TEST_F(EnergyOutputTest, HandlesTwoSteps)
189 ASSERT_NE(log_, nullptr);
191 // Add synthetic data for the first step
193 real testValue = 1.0;
194 setStepData(testValue);
195 energyOutput_.addDataAtEnergyStep(false, true, time, 0.0, &enerdata_,
196 nullptr, nullptr, nullptr, box_,
197 nullptr, nullptr, totalVirial_, pressure_,
198 nullptr, nullptr, constraints_.get());
200 // Test printing values
201 energyOutput_.printStepToEnergyFile(nullptr, false, false, false, log_,
203 nullptr, nullptr, nullptr, nullptr);
205 // Add synthetic data for the second step
207 setStepData(testValue += 1.0);
208 energyOutput_.addDataAtEnergyStep(false, true, time, 0.0, &enerdata_,
209 nullptr, nullptr, nullptr, box_,
210 nullptr, nullptr, totalVirial_, pressure_,
211 nullptr, nullptr, constraints_.get());
213 // Test printing values
214 energyOutput_.printStepToEnergyFile(nullptr, false, false, false, log_,
216 nullptr, nullptr, nullptr, nullptr);
218 // Test printing averages
219 energyOutput_.printStepToEnergyFile(nullptr, false, false, false, log_,
221 nullptr, nullptr, nullptr, nullptr);
223 // We need to close the file before the contents are available.
224 logFileGuard_.reset(nullptr);
226 checker_.checkInteger(energyOutput_.numEnergyTerms(), "Number of Energy Terms");
227 checker_.checkString(TextReader::readFileToString(logFilename_), "log");