Simplify energy-file comparisons in tests
[gromacs.git] / src / programs / mdrun / tests / mdruncomparison.h
blob323822454a8fd638a085f53216e69924c351211e
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,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.
35 /*! \libinternal
37 * \brief Functionality for testing whether calls to mdrun produce the
38 * same energy and force quantities when they should do so.
40 #ifndef GMX_MDRUN_TESTS_MDRUNCOMPARISON_H
41 #define GMX_MDRUN_TESTS_MDRUNCOMPARISON_H
43 #include <functional>
44 #include <memory>
46 #include <gtest/gtest.h>
48 namespace gmx
51 namespace test
54 /*! \internal
55 * \brief Manages returning a pair of frames from two
56 * equivalent simulations that are meaningful to compare.
58 * \todo This is largely duplicated by ContinuationFrameManager. These
59 * could be refactored into components that compare iterators to
60 * frames.
62 * \tparam FrameReader Has readNextFrame() and frame() methods
63 * useful for returning successive Frame objects */
64 template <class FrameReader>
65 class FramePairManager
67 public:
68 //! Convenience typedef
69 typedef std::unique_ptr<FrameReader> FrameReaderPtr;
70 //! Constructor
71 FramePairManager(FrameReaderPtr first,
72 FrameReaderPtr second) :
73 first_(std::move(first)),
74 second_(std::move(second))
76 private:
77 /*! \brief Probe for a pair of valid frames, and return true if both are found.
79 * Give a test failure if exactly one frame is found, because
80 * that file is longer than the other one, and this is not
81 * expected behaviour. */
82 bool shouldContinueComparing()
84 if (first_->readNextFrame())
86 if (second_->readNextFrame())
88 // Two valid next frames exist, so we should continue comparing.
89 return true;
91 else
93 ADD_FAILURE() << "first file had at least one more frame than second file";
96 else
98 if (second_->readNextFrame())
100 ADD_FAILURE() << "second file had at least one more frame than first file";
102 else
104 // Both files ran out of frames at the same time, which is the expected behaviour.
107 // At least one file is out of frames, so should not continue comparing.
108 return false;
110 public:
111 /*! \brief Compare all possible pairs of frames using \c compareTwoFrames.
113 * \tparam Frame The type of frame used in the comparison (returned
114 * by FrameReader and used by compareTwoFrames). */
115 template <class Frame>
116 void compareAllFramePairs(std::function<void(const Frame &, const Frame &)> compareTwoFrames)
118 while (shouldContinueComparing())
120 Frame firstFrame = first_->frame();
121 Frame secondFrame = second_->frame();
122 SCOPED_TRACE("Comparing frames from two runs '" + firstFrame.frameName() + "' and '" + secondFrame.frameName() + "'");
123 compareTwoFrames(firstFrame, secondFrame);
127 private:
128 FrameReaderPtr first_;
129 FrameReaderPtr second_;
132 } // namespace test
133 } // namespace gmx
135 #endif