Simplified uniform GPU selection in CMake
[gromacs.git] / src / programs / mdrun / tests / mdruncomparison.h
blob58809a1f542e496f5a52d7685370fa96f271a8d9
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, FrameReaderPtr second) :
72 first_(std::move(first)),
73 second_(std::move(second))
77 private:
78 /*! \brief Probe for a pair of valid frames, and return true if both are found.
80 * Give a test failure if exactly one frame is found, because
81 * that file is longer than the other one, and this is not
82 * expected behaviour. */
83 bool shouldContinueComparing()
85 if (first_->readNextFrame())
87 if (second_->readNextFrame())
89 // Two valid next frames exist, so we should continue comparing.
90 return true;
92 else
94 ADD_FAILURE() << "first file had at least one more frame than second file";
97 else
99 if (second_->readNextFrame())
101 ADD_FAILURE() << "second file had at least one more frame than first file";
103 else
105 // Both files ran out of frames at the same time, which is the expected behaviour.
108 // At least one file is out of frames, so should not continue comparing.
109 return false;
112 public:
113 /*! \brief Compare all possible pairs of frames using \c compareTwoFrames.
115 * \tparam Frame The type of frame used in the comparison (returned
116 * by FrameReader and used by compareTwoFrames). */
117 template<class Frame>
118 void compareAllFramePairs(std::function<void(const Frame&, const Frame&)> compareTwoFrames)
120 while (shouldContinueComparing())
122 Frame firstFrame = first_->frame();
123 Frame secondFrame = second_->frame();
124 SCOPED_TRACE("Comparing frames from two runs '" + firstFrame.frameName() + "' and '"
125 + secondFrame.frameName() + "'");
126 compareTwoFrames(firstFrame, secondFrame);
130 private:
131 FrameReaderPtr first_;
132 FrameReaderPtr second_;
135 } // namespace test
136 } // namespace gmx
138 #endif