Replace scoped_cptr with unique_cptr
[alexxy/gromacs.git] / src / programs / mdrun / tests / trajectoryreader.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35
36 /*! \internal \file
37  * \brief Implementions of related classes for tests that want to
38  * inspect trajectories produced by mdrun.
39  *
40  * \author Mark Abraham <mark.j.abraham@gmail.com>
41  * \ingroup module_mdrun_integration_tests
42  */
43 #include "gmxpre.h"
44
45 #include "trajectoryreader.h"
46
47 #include <memory>
48 #include <string>
49
50 #include "gromacs/fileio/oenv.h"
51 #include "gromacs/fileio/trxio.h"
52 #include "gromacs/trajectory/trajectoryframe.h"
53 #include "gromacs/utility/exceptions.h"
54 #include "gromacs/utility/stringutil.h"
55
56 #include "testutils/testasserts.h"
57
58 namespace gmx
59 {
60 namespace test
61 {
62
63 //! Helper function to obtain resources
64 t_trxframe *make_trxframe()
65 {
66     t_trxframe *frame;
67
68     snew(frame, 1);
69     clear_trxframe(frame, true);
70
71     return frame;
72 }
73
74 //! Helper function to clean up resources
75 void done_trxframe(t_trxframe *fr)
76 {
77     // Free the contents, then the pointer itself
78     sfree(fr->x);
79     sfree(fr->v);
80     sfree(fr->f);
81     sfree(fr->index);
82     sfree(fr);
83 }
84
85 // === TrajectoryFrameReader ===
86
87 TrajectoryFrameReader::TrajectoryFrameReader(const std::string &filename)
88     : filename_(filename),
89       trajectoryFileGuard_(nullptr),
90       trxframeGuard_(make_trxframe()),
91       haveReadFirstFrame_(false),
92       haveProbedForNextFrame_(false),
93       nextFrameExists_(false)
94 {
95     gmx_output_env_t *oenv;
96     output_env_init_default(&oenv);
97     oenvGuard_.reset(oenv);
98 }
99
100 bool
101 TrajectoryFrameReader::readNextFrame()
102 {
103     if (haveProbedForNextFrame_)
104     {
105         if (nextFrameExists_)
106         {
107             GMX_THROW(APIError("This frame has already been probed for, it should be used before probing again."));
108         }
109         else
110         {
111             GMX_THROW(APIError("This frame has already been probed for, it doesn't exist, so there should not be subsequent attempts to probe for it."));
112         }
113     }
114     haveProbedForNextFrame_ = true;
115     // If there's a next frame, read it into trxframe_, and report the result.
116     if (!haveReadFirstFrame_)
117     {
118         t_trxstatus *trajectoryFile;
119         int          flags = TRX_READ_X | TRX_READ_V | TRX_READ_F;
120         nextFrameExists_ = read_first_frame(oenvGuard_.get(),
121                                             &trajectoryFile,
122                                             filename_.c_str(),
123                                             trxframeGuard_.get(),
124                                             flags);
125         if (!trajectoryFile)
126         {
127             GMX_THROW(FileIOError("Could not open trajectory file " + filename_ + " for reading"));
128         }
129         trajectoryFileGuard_.reset(trajectoryFile);
130         haveReadFirstFrame_ = true;
131     }
132     else
133     {
134         nextFrameExists_ = read_next_frame(oenvGuard_.get(),
135                                            trajectoryFileGuard_.get(),
136                                            trxframeGuard_.get());
137     }
138     return nextFrameExists_;
139 }
140
141 TrajectoryFrame
142 TrajectoryFrameReader::frame()
143 {
144     TrajectoryFrame frame;
145
146     if (!haveProbedForNextFrame_)
147     {
148         readNextFrame();
149     }
150     if (!nextFrameExists_)
151     {
152         GMX_THROW(APIError("There is no next frame, so there should have been no attempt to use the data, e.g. by reacting to a call to readNextFrame()."));
153     }
154
155     // Prepare for reading future frames
156     haveProbedForNextFrame_ = false;
157     nextFrameExists_        = false;
158
159     // The probe filled trxframeGuard_ with new data, so return it
160     frame.frame_ = trxframeGuard_.get();
161
162     if (!frame.frame_->bStep)
163     {
164         GMX_THROW(APIError("Cannot handle trajectory frame that lacks a step number"));
165     }
166
167     if (!frame.frame_->bTime)
168     {
169         GMX_THROW(APIError("Cannot handle trajectory frame that lacks a time"));
170     }
171
172     return frame;
173 }
174
175 // === TrajectoryFrame ===
176
177 TrajectoryFrame::TrajectoryFrame() : frame_(nullptr) {};
178
179 std::string TrajectoryFrame::getFrameName() const
180 {
181     GMX_RELEASE_ASSERT(frame_, "Cannot get name of invalid frame");
182     return formatString("Time %f Step %" GMX_PRId64, frame_->time, frame_->step);
183 }
184
185 void compareFrames(const std::pair<TrajectoryFrame, TrajectoryFrame> &frames,
186                    FloatingPointTolerance tolerance)
187 {
188     auto &reference = frames.first;
189     auto &test      = frames.second;
190
191     // NB We checked earlier for both frames that bStep and bTime are set
192
193     EXPECT_EQ(reference.frame_->step, test.frame_->step)
194     << "step didn't match between reference run " << reference.getFrameName() << " and test run " << test.getFrameName();
195
196     EXPECT_EQ(reference.frame_->time, test.frame_->time)
197     << "time didn't match between reference run " << reference.getFrameName() << " and test run " << test.getFrameName();
198
199     for (int i = 0; i < reference.frame_->natoms && i < test.frame_->natoms; ++i)
200     {
201         for (int d = 0; d < DIM; ++d)
202         {
203             if (reference.frame_->bX && test.frame_->bX)
204             {
205                 EXPECT_REAL_EQ_TOL(reference.frame_->x[i][d], test.frame_->x[i][d], tolerance)
206                 << " x[" << i << "][" << d <<"] didn't match between reference run " << reference.getFrameName() << " and test run " << test.getFrameName();
207             }
208             if (reference.frame_->bV && test.frame_->bV)
209             {
210                 EXPECT_REAL_EQ_TOL(reference.frame_->v[i][d], test.frame_->v[i][d], tolerance)
211                 << " v[" << i << "][" << d <<"] didn't match between reference run " << reference.getFrameName() << " and test run " << test.getFrameName();
212             }
213             if (reference.frame_->bF && test.frame_->bF)
214             {
215                 EXPECT_REAL_EQ_TOL(reference.frame_->f[i][d], test.frame_->f[i][d], tolerance)
216                 << " f[" << i << "][" << d <<"] didn't match between reference run " << reference.getFrameName() << " and test run " << test.getFrameName();
217             }
218         }
219     }
220 }
221
222 } // namespace
223 } // namespace