2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016, 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 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
62 #include "gromacs/fileio/enxio.h"
63 #include "gromacs/utility/scoped_cptr.h"
65 #include "testutils/testasserts.h"
72 //! Forward declaration
73 class EnergyFrameReader
;
74 //! Convenience smart pointer typedef
75 typedef std::unique_ptr
<EnergyFrameReader
> EnergyFrameReaderPtr
;
77 /*! \brief Open the file and return an object that can read the required fields from frames of an .edr file.
79 * \param[in] filename Name of the energy file to use
80 * \param[in] requiredEnergyFieldNames Names of the energy fields that the caller requires to
81 * be present for an .edr file frame to be considered valid
82 * \throws FileIOError If the .edr file cannot be opened
83 * \throws APIError If any required energy field is not present in the file
84 * \throws std::bad_alloc When out of memory
86 * This function is intended to have the main responsibility for
87 * making EnergyFrameReader objects. */
88 EnergyFrameReaderPtr
openEnergyFileToReadFields(const std::string
&filename
,
89 const std::vector
<std::string
> &requiredEnergyFieldNames
);
93 //! Convenience smart pointer typedef
94 typedef scoped_cptr
<ener_file
, done_ener_file
> ener_file_ptr
;
95 //! Helper function to free resources (NB free_enxframe only frees the contents, not the pointer itself)
96 void done_enxframe(t_enxframe
*fr
);
97 //! Convenience smart pointer typedef
98 typedef scoped_cptr
<t_enxframe
, done_enxframe
> enxframe_ptr
;
101 * \brief Manages returning an EnergyFrame containing required energy
102 * field values read from successive frames of an .edr file. */
103 class EnergyFrameReader
106 /*! \brief Attempt to read the next frame from the energy file.
108 * \return Whether a frame was available to read.
110 * If true is returned, then frame() should be called
111 * to get access to the data. If false is returned, then no
112 * further data exists and no further call to
113 * readNextFrame() or frame() should occur.
115 * \throws APIError if an earlier probe has not been properly handled
116 * (by calling frame(), or stopping trying to read
118 bool readNextFrame();
119 /*! \brief Make an EnergyFrame from the contents of the next frame in the energy file.
121 * If the next frame has not been probed for, then probe for
122 * it. If no next frame exists, then throw APIError, because
123 * user code should have called readNextFrame() itself if this
124 * is possible. (This permits user code to avoid making calls
125 * to readNextFrame() in a case where it already knows that
128 * \throws APIError if no next frame exists.
129 * \throws std::bad_alloc when out of memory. */
131 /*! \brief Constructor
133 * \param[in] indicesOfEnergyFields Looks up energy fields by name to get the index into a t_enxframe structure read by the legacy API.
134 * \param[in] energyFile Open energy file object to manage, and from which to read frames */
135 explicit EnergyFrameReader(const std::map
<std::string
, int> &indicesOfEnergyFields
,
136 ener_file
*energyFile
);
138 //! Convert energy field name to its index within a t_enxframe from this file.
139 std::map
<std::string
, int> indicesOfEnergyFields_
;
140 //! Owning handle of an open energy file ready to read frames.
141 ener_file_ptr energyFileGuard_
;
142 //! Owning handle of contents of .edr file frame after reading.
143 enxframe_ptr enxframeGuard_
;
144 //! Whether the API has been used properly (ie. probe before reading).
145 bool haveProbedForNextFrame_
;
146 //! Whether there has been a probe that found a next frame.
147 bool nextFrameExists_
;
149 // Multiple owners of these resources isn't very sensible, so prevent it
150 GMX_DISALLOW_COPY_AND_ASSIGN(EnergyFrameReader
);
153 /*! \brief Compare all fields of reference with all matching fields from test
155 * Ignore any key found in either \c reference or \c test that is not
156 * found in the other. For all keys found in both frames, compare the
157 * values with EXPECT_REAL_EQ_TOL and the given tolerance. */
158 void compareFrames(const std::pair
<EnergyFrame
, EnergyFrame
> &frames
,
159 FloatingPointTolerance tolerance
);
162 * \brief Contains the content of an .edr frame read by an EnergyFrameReader
164 * The interface of this class is intended to resemble a subset of std::map.
166 * Objects of this type are intended to be constructed by
167 * EnergyFrameReader objects, and as such will always contain valid
168 * data from an .edr file frame. */
172 /*! \brief Return string that helps users identify this frame, containing time and step number.
174 * \throws std::bad_alloc when out of memory */
175 std::string
getFrameName() const;
176 /*! \brief Return the value read for energy \c name.
178 * \throws APIError if \c name was not registered with EnergyFileReader. */
179 const real
&at(const std::string
&name
) const;
183 //! Container for energy values, indexed by name
184 std::map
<std::string
, real
> values_
;
185 //! Step number read from the .edr file frame
187 //! Time read from the .edr file frame
190 friend class EnergyFrameReader
;
191 friend void compareFrames(const std::pair
<EnergyFrame
, EnergyFrame
> &frames
,
192 FloatingPointTolerance tolerance
);