2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2018, 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 Declares interface of a class for tests that want to inspect
38 * trajectories produced by mdrun.
40 * Intended usage is to create a TrajectoryFrameReader. Successive
41 * calls to its readNextFrameStub and frame methods will return a handle
42 * to a t_trxframe object for each frame.
44 * \author Mark Abraham <mark.j.abraham@gmail.com>
45 * \ingroup module_mdrun_integration_tests
47 #ifndef GMX_PROGRAMS_MDRUN_TESTS_TRAJECTORYREADER_H
48 #define GMX_PROGRAMS_MDRUN_TESTS_TRAJECTORYREADER_H
53 #include "gromacs/fileio/oenv.h"
54 #include "gromacs/fileio/trxio.h"
55 #include "gromacs/trajectory/trajectoryframe.h"
56 #include "gromacs/utility/classhelpers.h"
57 #include "gromacs/utility/unique_cptr.h"
62 class TrajectoryFrame
;
67 //! Convenience smart pointer typedef
68 typedef unique_cptr
<gmx_output_env_t
, output_env_done
> oenv_ptr
;
69 //! Convenience smart pointer typedef
70 typedef unique_cptr
<t_trxstatus
, close_trx
> trxstatus_file_ptr
;
71 //! Helper function to free all resources
72 void done_trxframe(t_trxframe
*fr
);
73 //! Convenience smart pointer typedef
74 typedef unique_cptr
<t_trxframe
, done_trxframe
> trxframe_ptr
;
77 * \brief Manages returning a t_trxframe whose contents were read from
78 * successive frames of an trajectory file. */
79 class TrajectoryFrameReader
82 /*! \brief Attempt to read the next frame from the trajectory file.
84 * \return Whether a frame was available to read.
86 * This call wraps the read_first_frame()/read_next_frame()
87 * API, which does the file opening as a side effect of
88 * reading the first frame.
90 * If true is returned, then TrajectoryFrame frame() should be called
91 * to get access to the data. If false is returned, then no
92 * further data exists and no further call to
93 * readNextFrameStub or TrajectoryFrame frame() should occur.
95 * \throws FileIOError upon reading the first frame, if the trajectory file cannot be opened
96 * \throws APIError if an earlier probe has not been properly handled
97 * (by calling frame(), or stopping trying to read
100 /*! \brief Return the next frame from the trajectory file.
102 * If the next frame has not been probed for, then probe for
103 * it. If no next frame exists, then throw APIError, because
104 * user code should have called readNextFrameStub itself if this
105 * is possible. (This permits user code to avoid making calls
106 * to readNextFrameStub in a case where it already knows that
109 * \throws APIError if no next frame exists, or if it lacks either time or step number. */
110 TrajectoryFrame
frame();
111 /*! \brief Constructor
113 * \param[in] filename Name of trajectory file to open and read. */
114 explicit TrajectoryFrameReader(const std::string
&filename
);
116 //! Name of trajectory file to open and read
117 std::string filename_
;
118 //! Owning handle of output environment object
120 //! Owning handle of an open trajectory file ready to read frames.
121 trxstatus_file_ptr trajectoryFileGuard_
;
122 //! Owning handle of contents of trajectory file frame after reading.
123 const trxframe_ptr trxframeGuard_
;
124 //! Whether the first frame has been read
125 bool haveReadFirstFrame_
;
126 //! Whether the API has been used properly (ie. probe before reading).
127 bool haveProbedForNextFrame_
;
128 //! Whether there has been a probe that found a next frame.
129 bool nextFrameExists_
;
131 // Multiple owners of these resources isn't very sensible, so prevent it
132 GMX_DISALLOW_COPY_AND_ASSIGN(TrajectoryFrameReader
);