2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018,2019,2020,2021, 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 * \brief Implemention of functions for comparing trajectories
40 * \author Mark Abraham <mark.j.abraham@gmail.com>
41 * \author Pascal Merz <pascal.merz@me.com>
42 * \ingroup module_mdrun_integration_tests
46 #include "trajectorycomparison.h"
48 #include <gmock/gmock.h>
49 #include <gtest/gtest-spi.h>
51 #include "gromacs/pbcutil/pbc.h"
52 #include "gromacs/trajectory/trajectoryframe.h"
53 #include "gromacs/utility/strconvert.h"
55 #include "testutils/refdata.h"
56 #include "testutils/testasserts.h"
57 #include "testutils/testmatchers.h"
59 #include "trajectoryreader.h"
66 using ::testing::Pointwise;
68 /*! \brief Compares the box from \c reference and \c test
69 * according to the \c matchSettings and \c tolerance.
71 * \todo This could be streamlined when we have a proper 3D matrix
73 static void compareBox(const TrajectoryFrame& reference,
74 const TrajectoryFrame& test,
75 const TrajectoryFrameMatchSettings& matchSettings,
76 const FloatingPointTolerance tolerance)
78 if (!matchSettings.mustCompareBox)
82 bool canCompareBox = true;
83 if (!reference.hasBox())
85 ADD_FAILURE() << "Comparing the box was required, "
86 "but the reference frame did not have one";
87 canCompareBox = false;
91 ADD_FAILURE() << "Comparing the box was required, "
92 "but the test frame did not have one";
93 canCompareBox = false;
101 for (int d = 0; d < DIM; ++d)
103 for (int dd = 0; dd < DIM; ++dd)
105 EXPECT_REAL_EQ_TOL(reference.box()[d][dd], test.box()[d][dd], tolerance);
110 /*! \brief Help put all atom coordinates in \c frame into its box.
112 * This can perhaps go away when frame->x is a container. */
113 static std::vector<RVec> putAtomsInBox(const TrajectoryFrame& frame)
115 std::vector<RVec> x(frame.x().begin(), frame.x().end());
117 for (int d = 0; d < DIM; ++d)
119 for (int dd = 0; dd < DIM; ++dd)
121 box[d][dd] = frame.box()[d][dd];
124 // Note we don't need to compare bPBC because put_atoms_in_box
125 // implements a fallback if nothing specific was set in the
127 put_atoms_in_box(frame.pbc(), box, x);
131 /*! \brief Return whether the \c comparisonConditions and emptiness of
132 * reference and test frames means that a comparison should be
135 * This allows the framework to determine whether it is an error if a
136 * comparison cannot be made. */
137 static bool shouldDoComparison(const ComparisonConditions comparisonConditions,
138 const bool referenceIsEmpty,
139 const bool testIsEmpty)
141 if (comparisonConditions == ComparisonConditions::NoComparison)
146 bool doComparison = true;
149 if (comparisonConditions == ComparisonConditions::MustCompare
150 || comparisonConditions == ComparisonConditions::CompareIfBothFound
151 || (!referenceIsEmpty && comparisonConditions == ComparisonConditions::CompareIfReferenceFound))
153 ADD_FAILURE() << "Test frame lacked quantity for required comparison";
155 doComparison = false;
157 if (referenceIsEmpty)
159 if (comparisonConditions == ComparisonConditions::MustCompare
160 || comparisonConditions == ComparisonConditions::CompareIfBothFound
161 || (!testIsEmpty && comparisonConditions == ComparisonConditions::CompareIfTestFound))
163 ADD_FAILURE() << "Reference frame lacked quantity for required comparison";
165 doComparison = false;
170 /*! \brief Compares the position coordinates from \c reference and \c test
171 * according to the \c matchSettings and \c tolerance. */
172 static void compareCoordinates(const TrajectoryFrame& reference,
173 const TrajectoryFrame& test,
174 const TrajectoryFrameMatchSettings& matchSettings,
175 const FloatingPointTolerance tolerance)
177 SCOPED_TRACE("Comparing coordinates");
178 if (!shouldDoComparison(matchSettings.coordinatesComparison, reference.x().empty(), test.x().empty()))
183 bool canHandlePbc = true;
184 if (!reference.hasBox())
186 ADD_FAILURE() << "Comparing positions required PBC handling, "
187 "but the reference frame did not have a box";
188 canHandlePbc = false;
192 ADD_FAILURE() << "Comparing positions required PBC handling, "
193 "but the test frame did not have a box";
194 canHandlePbc = false;
197 if (matchSettings.requirePbcHandling && !canHandlePbc)
199 ADD_FAILURE() << "Cannot compare positions for the above reason(s)";
203 if ((matchSettings.handlePbcIfPossible || matchSettings.requirePbcHandling) && canHandlePbc)
205 EXPECT_THAT(putAtomsInBox(test), Pointwise(RVecEq(tolerance), putAtomsInBox(reference)));
209 EXPECT_THAT(test.x(), Pointwise(RVecEq(tolerance), reference.x()));
213 /*! \brief Compares the velocities from \c reference and \c test
214 * according to the \c matchSettings and \c tolerance. */
215 static void compareVelocities(const TrajectoryFrame& reference,
216 const TrajectoryFrame& test,
217 const TrajectoryFrameMatchSettings& matchSettings,
218 const FloatingPointTolerance tolerance)
220 SCOPED_TRACE("Comparing velocities");
221 if (!shouldDoComparison(matchSettings.velocitiesComparison, reference.v().empty(), test.v().empty()))
225 EXPECT_THAT(test.v(), Pointwise(RVecEq(tolerance), reference.v()));
228 /*! \brief Compares the forces from \c reference and \c test
229 * according to the \c matchSettings and \c tolerance. */
230 static void compareForces(const TrajectoryFrame& reference,
231 const TrajectoryFrame& test,
232 const TrajectoryFrameMatchSettings& matchSettings,
233 const FloatingPointTolerance tolerance)
235 SCOPED_TRACE("Comparing forces");
236 if (!shouldDoComparison(matchSettings.forcesComparison, reference.f().empty(), test.f().empty()))
240 EXPECT_THAT(test.f(), Pointwise(RVecEq(tolerance), reference.f()));
244 const TrajectoryTolerances TrajectoryComparison::s_defaultTrajectoryTolerances{
245 defaultRealTolerance(), // box
246 relativeToleranceAsFloatingPoint(1.0, 1.0e-3), // positions
247 defaultRealTolerance(), // velocities
248 relativeToleranceAsFloatingPoint(100.0, GMX_DOUBLE ? 1.0e-7 : 5.0e-5) // forces
252 TrajectoryComparison::TrajectoryComparison(const TrajectoryFrameMatchSettings& matchSettings,
253 const TrajectoryTolerances& tolerances) :
254 matchSettings_(matchSettings),
255 tolerances_(tolerances)
259 void TrajectoryComparison::operator()(const TrajectoryFrame& reference, const TrajectoryFrame& test) const
261 if (numComparedFrames_ >= matchSettings_.maxNumTrajectoryFrames)
263 // Nothing should be compared
266 SCOPED_TRACE("Comparing trajectory reference frame " + reference.frameName()
267 + " and test frame " + test.frameName());
268 EXPECT_EQ(reference.step(), test.step());
269 EXPECT_EQ(reference.time(), test.time());
270 compareBox(reference, test, matchSettings_, tolerances_.box);
271 compareCoordinates(reference, test, matchSettings_, tolerances_.coordinates);
272 compareVelocities(reference, test, matchSettings_, tolerances_.velocities);
273 compareForces(reference, test, matchSettings_, tolerances_.forces);
274 numComparedFrames_++;
277 /*! \brief Return whether the \c comparisonConditions and emptiness of
278 * the test frame means that a comparison should be attempted.
280 * This allows the framework to determine whether it is an error if a
281 * comparison cannot be made. */
282 static bool shouldDoComparison(ArrayRef<const RVec> values, ComparisonConditions comparisonConditions)
284 if (comparisonConditions == ComparisonConditions::NoComparison)
290 // Empty array ref is only acceptable if comparison is not required
291 if (comparisonConditions != ComparisonConditions::CompareIfTestFound
292 && comparisonConditions != ComparisonConditions::CompareIfBothFound)
294 ADD_FAILURE() << "Test frame lacked quantity for required comparison";
301 /*! \brief Compares the positions from \c frame to reference data
302 * according to the \c matchSettings and \c tolerance.
304 static void checkPositionsAgainstReference(const TrajectoryFrame& frame,
305 const TrajectoryFrameMatchSettings& matchSettings,
306 const TrajectoryTolerances& tolerance,
307 TestReferenceChecker* checker)
309 SCOPED_TRACE("Comparing positions");
310 if (shouldDoComparison(frame.x(), matchSettings.coordinatesComparison))
312 ArrayRef<const RVec> positions{};
313 std::vector<RVec> shiftedPositions{};
314 if (frame.hasBox() && (matchSettings.handlePbcIfPossible || matchSettings.requirePbcHandling))
316 shiftedPositions = putAtomsInBox(frame);
317 positions = shiftedPositions;
321 positions = frame.x();
324 if (!frame.hasBox() && matchSettings.requirePbcHandling)
326 ADD_FAILURE() << "Comparing positions required PBC handling, "
327 "but the test frame did not have a box";
330 checker->setDefaultTolerance(tolerance.coordinates);
331 std::string id = frame.frameName() + " X";
332 checker->checkSequence(std::begin(positions), std::end(positions), id.c_str());
336 /*! \brief Compares the velocities from \c frame to reference data
337 * according to the \c matchSettings and \c tolerance.
339 static void checkVelocitiesAgainstReference(const TrajectoryFrame& frame,
340 const TrajectoryFrameMatchSettings& matchSettings,
341 const TrajectoryTolerances& tolerance,
342 TestReferenceChecker* checker)
344 SCOPED_TRACE("Comparing velocities");
345 if (shouldDoComparison(frame.v(), matchSettings.velocitiesComparison))
347 checker->setDefaultTolerance(tolerance.velocities);
348 std::string id = frame.frameName() + " V";
349 checker->checkSequence(std::begin(frame.v()), std::end(frame.v()), id.c_str());
353 /*! \brief Compares the forces from \c frame to reference data
354 * according to the \c matchSettings and \c tolerance.
356 static void checkForcesAgainstReference(const TrajectoryFrame& frame,
357 const TrajectoryFrameMatchSettings& matchSettings,
358 const TrajectoryTolerances& tolerance,
359 TestReferenceChecker* checker)
361 SCOPED_TRACE("Comparing forces");
362 if (shouldDoComparison(frame.f(), matchSettings.forcesComparison))
364 checker->setDefaultTolerance(tolerance.forces);
365 std::string id = frame.frameName() + " F";
366 checker->checkSequence(std::begin(frame.f()), std::end(frame.f()), id.c_str());
370 /*! \brief Compares the box from \c frame to reference data
371 * according to the \c matchSettings and \c tolerance.
373 static void checkBoxAgainstReference(const TrajectoryFrame& frame,
374 const TrajectoryFrameMatchSettings& matchSettings,
375 const TrajectoryTolerances& tolerance,
376 TestReferenceChecker* checker)
378 SCOPED_TRACE("Comparing box");
379 if (!matchSettings.mustCompareBox)
385 ADD_FAILURE() << "Comparing the box was required, "
386 "but the test frame did not have one";
389 checker->setDefaultTolerance(tolerance.box);
391 for (int d = 0; d < DIM; ++d)
393 for (int dd = 0; dd < DIM; ++dd)
397 (frame.frameName() + " Box[" + toString(d) + "][" + toString(dd) + "]").c_str());
402 void TrajectoryComparison::operator()(const TrajectoryFrame& frame, TestReferenceChecker* checker) const
404 SCOPED_TRACE("Comparing trajectory frame " + frame.frameName() + " against reference data");
405 checkBoxAgainstReference(frame, matchSettings_, tolerances_, checker);
406 checkPositionsAgainstReference(frame, matchSettings_, tolerances_, checker);
407 checkVelocitiesAgainstReference(frame, matchSettings_, tolerances_, checker);
408 checkForcesAgainstReference(frame, matchSettings_, tolerances_, checker);
411 void checkTrajectoryAgainstReferenceData(const std::string& trajectoryFilename,
412 const TrajectoryComparison& trajectoryComparison,
413 TestReferenceChecker* checker)
415 checkTrajectoryAgainstReferenceData(
416 trajectoryFilename, trajectoryComparison, checker, MaxNumFrames::compareAllFrames());
419 void checkTrajectoryAgainstReferenceData(const std::string& trajectoryFilename,
420 const TrajectoryComparison& trajectoryComparison,
421 TestReferenceChecker* checker,
422 MaxNumFrames maxNumFrames)
425 TrajectoryFrameReader reader(trajectoryFilename);
426 unsigned int counter = 0;
427 for (counter = 0; counter < maxNumFrames && reader.readNextFrame(); counter++)
429 auto frame = reader.frame();
430 trajectoryComparison(frame, checker);
433 if (counter == maxNumFrames && reader.readNextFrame())
435 // There would have been at least one more frame!
436 checker->disableUnusedEntriesCheck();