Move pull work struct out of inputrec
[alexxy/gromacs.git] / src / gromacs / mdlib / tests / energyoutput.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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 #include "gmxpre.h"
36
37 #include "gromacs/mdlib/energyoutput.h"
38
39 #include <cstdio>
40
41 #include <gtest/gtest.h>
42
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"
50
51 #include "testutils/refdata.h"
52 #include "testutils/testasserts.h"
53 #include "testutils/testfilemanager.h"
54
55 namespace gmx
56 {
57 namespace test
58 {
59 namespace
60 {
61
62 //! Wraps fclose to discard the return value to use it as a deleter with gmx::unique_cptr.
63 void fcloseWrapper(FILE *fp)
64 {
65     fclose(fp);
66 }
67
68 class EnergyOutputTest : public ::testing::Test
69 {
70     public:
71         TestFileManager fileManager_;
72
73         // Objects needed to make EnergyOutput
74         t_inputrec   inputrec_;
75         gmx_mtop_t   mtop_;
76         EnergyOutput energyOutput_;
77
78         // Objects needed for default energy output behavior.
79         t_mdatoms                    mdatoms_;
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_;
84
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
88         // running tests.
89         std::string logFilename_;
90         FILE       *log_;
91         unique_cptr<FILE, fcloseWrapper> logFileGuard_;
92
93         TestReferenceData                refData_;
94         TestReferenceChecker             checker_;
95
96         EnergyOutputTest() :
97             logFilename_(fileManager_.getTemporaryFilePath(".log")),
98             log_(std::fopen(logFilename_.c_str(), "w")), logFileGuard_(log_),
99             checker_(refData_.rootChecker())
100         {
101             energyOutput_.prepare(nullptr, &mtop_, &inputrec_, nullptr, nullptr, false);
102             constraints_ = makeConstraints(mtop_, inputrec_, nullptr, false, log_, mdatoms_, nullptr,
103                                            nullptr, nullptr, nullptr, false);
104         }
105         //! Helper function to generate synthetic data to output
106         void setStepData(real testValue)
107         {
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);
133         }
134
135 };
136
137 TEST_F(EnergyOutputTest, HandlesEmptyAverages)
138 {
139     ASSERT_NE(log_, nullptr);
140
141     // Test printing values
142     energyOutput_.printStepToEnergyFile(nullptr, false, false, false, log_,
143                                         0, 0, eprNORMAL,
144                                         nullptr, nullptr, nullptr, nullptr);
145     // Test printing averages
146     energyOutput_.printStepToEnergyFile(nullptr, false, false, false, log_,
147                                         0, 0, eprAVER,
148                                         nullptr, nullptr, nullptr, nullptr);
149
150     // We need to close the file before the contents are available.
151     logFileGuard_.reset(nullptr);
152
153     checker_.checkInteger(energyOutput_.numEnergyTerms(), "Number of Energy Terms");
154     checker_.checkString(TextReader::readFileToString(logFilename_), "log");
155 }
156
157 TEST_F(EnergyOutputTest, HandlesSingleStep)
158 {
159     ASSERT_NE(log_, nullptr);
160
161     // Add synthetic data for a single step
162     real time      = 1.0;
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());
169
170     // Test printing values
171     energyOutput_.printStepToEnergyFile(nullptr, false, false, false, log_,
172                                         0, 0, eprNORMAL,
173                                         nullptr, nullptr, nullptr, nullptr);
174
175     // Test printing averages
176     energyOutput_.printStepToEnergyFile(nullptr, false, false, false, log_,
177                                         0, 0, eprAVER,
178                                         nullptr, nullptr, nullptr, nullptr);
179
180     // We need to close the file before the contents are available.
181     logFileGuard_.reset(nullptr);
182
183     checker_.checkInteger(energyOutput_.numEnergyTerms(), "Number of Energy Terms");
184     checker_.checkString(TextReader::readFileToString(logFilename_), "log");
185 }
186
187 TEST_F(EnergyOutputTest, HandlesTwoSteps)
188 {
189     ASSERT_NE(log_, nullptr);
190
191     // Add synthetic data for the first step
192     real time      = 1.0;
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());
199
200     // Test printing values
201     energyOutput_.printStepToEnergyFile(nullptr, false, false, false, log_,
202                                         0, 0, eprNORMAL,
203                                         nullptr, nullptr, nullptr, nullptr);
204
205     // Add synthetic data for the second step
206     time += 0.005;
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());
212
213     // Test printing values
214     energyOutput_.printStepToEnergyFile(nullptr, false, false, false, log_,
215                                         0, 0, eprNORMAL,
216                                         nullptr, nullptr, nullptr, nullptr);
217
218     // Test printing averages
219     energyOutput_.printStepToEnergyFile(nullptr, false, false, false, log_,
220                                         0, 0, eprAVER,
221                                         nullptr, nullptr, nullptr, nullptr);
222
223     // We need to close the file before the contents are available.
224     logFileGuard_.reset(nullptr);
225
226     checker_.checkInteger(energyOutput_.numEnergyTerms(), "Number of Energy Terms");
227     checker_.checkString(TextReader::readFileToString(logFilename_), "log");
228 }
229
230 }  // namespace
231 }  // namespace test
232 }  // namespace gmx