Fix #3655
[gromacs.git] / src / programs / mdrun / tests / energyreader.h
blob0012c02fb983d1df11c70f3162a927e952e90e87
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2018,2019,2020, 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.
36 /*! \internal \file
37 * \brief Interfaces of related classes for tests that want to
38 * inspect energies produced by mdrun.
40 * The responsibilities for reading and sharing information from .edr
41 * files in an exception-safe manner are shared between two
42 * classes. Intended usage is to call openEnergyFileToReadFields() to
43 * return an EnergyFrameReader that knows it expects to read the named
44 * .edr fields from each frame. Successive calls to its
45 * EnergyFrameReader::readNextFrame() and EnergyFrameReader::frame()
46 * methods will return EnergyFrame objects that contain the values for
47 * the .edr fields registered earlier.
49 * \author Mark Abraham <mark.j.abraham@gmail.com>
50 * \ingroup module_mdrun_integration_tests
52 #ifndef GMX_PROGRAMS_MDRUN_TESTS_ENERGYREADER_H
53 #define GMX_PROGRAMS_MDRUN_TESTS_ENERGYREADER_H
55 #include <cstdint>
57 #include <memory>
58 #include <string>
59 #include <unordered_map>
60 #include <vector>
62 #include "gromacs/fileio/enxio.h"
63 #include "gromacs/utility/classhelpers.h"
64 #include "gromacs/utility/unique_cptr.h"
66 #include "testutils/testasserts.h"
68 namespace gmx
71 class EnergyFrame;
73 namespace test
76 class EnergyFrameReader;
77 //! Convenience smart pointer typedef
78 typedef std::unique_ptr<EnergyFrameReader> EnergyFrameReaderPtr;
80 /*! \brief Open the file and return an object that can read the required terms from frames of an .edr file.
82 * \param[in] filename Name of the energy file to use
83 * \param[in] requiredEnergyTermNames Names of the energy terms that the caller requires to
84 * be present for an .edr file frame to be considered valid
85 * \throws FileIOError If the .edr file cannot be opened
86 * \throws APIError If any required energy term is not present in the file
87 * \throws std::bad_alloc When out of memory
89 * This function is intended to have the main responsibility for
90 * making EnergyFrameReader objects. */
91 EnergyFrameReaderPtr openEnergyFileToReadTerms(const std::string& filename,
92 const std::vector<std::string>& requiredEnergyTermNames);
94 //! Convenience smart pointer typedef
95 typedef unique_cptr<ener_file, done_ener_file> ener_file_ptr;
96 //! Helper function to free resources (NB free_enxframe only frees the contents, not the pointer itself)
97 void done_enxframe(t_enxframe* fr);
98 //! Convenience smart pointer typedef
99 typedef unique_cptr<t_enxframe, done_enxframe> enxframe_ptr;
101 /*! \internal
102 * \brief Manages returning an EnergyFrame containing required energy
103 * term values read from successive frames of an .edr file. */
104 class EnergyFrameReader
106 public:
107 /*! \brief Attempt to read the next frame from the energy file.
109 * \return Whether a frame was available to read.
111 * If true is returned, then frame() should be called
112 * to get access to the data. If false is returned, then no
113 * further data exists and no further call to
114 * readNextFrame() or frame() should occur.
116 * \throws APIError if an earlier probe has not been properly handled
117 * (by calling frame(), or stopping trying to read
118 * from the file). */
119 bool readNextFrame();
120 /*! \brief Make an EnergyFrame from the contents of the next frame in the energy file.
122 * If the next frame has not been probed for, then probe for
123 * it. If no next frame exists, then throw APIError, because
124 * user code should have called readNextFrame() itself if this
125 * is possible. (This permits user code to avoid making calls
126 * to readNextFrame() in a case where it already knows that
127 * the frame exists.)
129 * \throws APIError if no next frame exists.
130 * \throws std::bad_alloc when out of memory. */
131 EnergyFrame frame();
132 /*! \brief Constructor
134 * \param[in] indicesOfEnergyTerms Looks up energy terms by name to get the index into a t_enxframe structure read by the legacy API.
135 * \param[in] energyFile Open energy file object to manage, and from which to read frames */
136 explicit EnergyFrameReader(const std::map<std::string, int>& indicesOfEnergyTerms, ener_file* energyFile);
138 private:
139 //! Convert energy term name to its index within a t_enxframe from this file.
140 std::map<std::string, int> indicesOfEnergyTerms_;
141 //! Owning handle of an open energy file ready to read frames.
142 const ener_file_ptr energyFileGuard_;
143 //! Owning handle of contents of .edr file frame after reading.
144 const enxframe_ptr enxframeGuard_;
145 //! Whether the API has been used properly (ie. probe before reading).
146 bool haveProbedForNextFrame_;
147 //! Whether there has been a probe that found a next frame.
148 bool nextFrameExists_;
150 // Multiple owners of these resources isn't very sensible, so prevent it
151 GMX_DISALLOW_COPY_AND_ASSIGN(EnergyFrameReader);
154 } // namespace test
155 } // namespace gmx
157 #endif