2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * 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 * Declares gmx::AnalysisData and gmx::AnalysisDataHandle.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_analysisdata
43 #ifndef GMX_ANALYSISDATA_ANALYSISDATA_H
44 #define GMX_ANALYSISDATA_ANALYSISDATA_H
46 #include "abstractdata.h"
51 class AnalysisDataHandle;
52 class AnalysisDataParallelOptions;
55 * Parallelizable data container for raw data.
57 * This is the main class used to implement parallelizable data processing in
58 * analysis tools. It is used by first creating an object and setting its
59 * properties using setColumnCount() and setMultipoint(), and attaching
60 * necessary modules using addModule() etc. Then one or more
61 * AnalysisDataHandle objects can be created using startData(). Each data
62 * handle can then be independently used to provide data frames (each frame
63 * must be provided by a single handle, but different frames can be freely
64 * mixed between the handles). When all data has been provided, the handles
65 * are destroyed using finishData() (or AnalysisDataHandle::finishData()).
66 * The AnalysisData object takes care of internally sorting the frames and
67 * passing them to the attached modules in the order in which the modules
71 * Parallel implementation is not complete.
74 * Special note for MPI implementation: assuming that the initialization of
75 * data objects is identical in all processes, associating the data objects
76 * in different MPI processes should be possible without changes in the
78 * Alternative, more robust implementation could get a unique ID as parameter
79 * to the constructor or a separate function, but would require all tools to
80 * provide it. With the current registration mechanism in
81 * TrajectoryAnalysisModule, this should be straightforward.
85 * \ingroup module_analysisdata
87 class AnalysisData : public AbstractAnalysisData
91 * Creates an empty analysis data object.
93 * \throws std::bad_alloc if out of memory.
96 virtual ~AnalysisData();
99 * Sets the number of columns in the data.
101 * \param[in] ncol Number of columns in the data (must be > 0).
103 * Must be called before startData(), and can be called multiple times
104 * before modules are added.
105 * Must not be called after startData() has been called.
107 * Does not currently throw, but this may change for the case that
108 * modules have already been added.
110 void setColumnCount(int ncol);
112 * Sets whether the data contains multiple points per column per frame.
114 * \param[in] bMultipoint Whether the data will allow multiple points
115 * per column within a single frame.
117 * If this method is not called, the data is not multipoint.
119 * Must not be called after modules have been added or startData() has
122 * Does not currently throw, but this may change for the case that
123 * modules have already been added.
125 * \see isMultipoint()
127 void setMultipoint(bool bMultipoint);
130 * Create a handle for adding data.
132 * \param[in] opt Options for setting how this handle will be
134 * \returns The created handle.
135 * \throws std::bad_alloc if out of memory.
136 * \throws APIError if any attached data module is not compatible.
137 * \throws unspecified Any exception thrown by attached data modules
138 * in AnalysisDataModuleInterface::dataStarted().
140 * The caller should retain the returned handle (or a copy of it), and
141 * pass it to finishData() after successfully adding all data.
142 * The caller should discard the returned handle if an error occurs;
143 * memory allocated for the handle will be freed when the AnalysisData
144 * object is destroyed.
146 * The \p opt options should be the same for all calls to this method,
147 * and the number of calls should match the parallelization factor
150 AnalysisDataHandle startData(const AnalysisDataParallelOptions &opt);
152 * Destroy a handle after all data has been added.
154 * \param[in] handle Handle to destroy.
155 * \throws unspecified Any exception thrown by attached data modules
156 * in AnalysisDataModuleInterface::dataFinished().
158 * \p handle must have been obtained from startData() of this object.
159 * The order of the calls with respect to the corresponding startData()
160 * calls is not important.
162 * The \p handle (and any copies) are invalid after the call.
164 void finishData(AnalysisDataHandle handle);
167 virtual AnalysisDataFrameRef tryGetDataFrameInternal(int index) const;
168 virtual bool requestStorageInternal(int nframes);
172 PrivateImplPointer<Impl> impl_;
174 friend class AnalysisDataHandle;
179 class AnalysisDataHandleImpl;
180 } // namespace internal
183 * Handle for inserting data into AnalysisData.
185 * This class provides an interface for adding data frames into an AnalysisData
186 * object. After a handle is obtained from AnalysisData::startData(), new
187 * frames can be added using startFrame(). Then values for that frame are set
188 * using provided methods (see below), and finishFrame() is called. After all
189 * frames have been added, finishData() (or AnalysisData::finishData()) must be
192 * For simple (non-multipoint) data, within a frame values can be set using
193 * setPoint() and setPoints(). Setting the same column multiple times
194 * overrides previously set values. When the frame is finished, attached
195 * modules are notified.
197 * Multipoint data works otherwise similarly, but requires finishPointSet() to
198 * be called for each set of points for which the modules need to be notified.
199 * Each point set starts empty (after startFrame() or finishPointSet()), and
200 * values can be set using setPoint()/setPoints(). finishPointSet() must also
201 * be called for the last point set just before finishFrame().
203 * This class works like a pointer type: copying and assignment is lightweight,
204 * and all copies work interchangeably, accessing the same internal handle.
205 * However, normally you should only keep one copy of a handle, i.e., treat
206 * this type as movable.
207 * Several handles created from the same AnalysisData object can exist
208 * concurrently, but must currently operate on separate frames.
211 * \ingroup module_analysisdata
213 class AnalysisDataHandle
217 * Constructs an invalid data handle.
219 * This constructor is provided for convenience in cases where it is
220 * easiest to declare an AnalysisDataHandle without immediately
221 * assigning a value to it. Any attempt to call methods without first
222 * assigning a value from AnalysisData::startData() to the handle
227 AnalysisDataHandle();
230 * Start data for a new frame.
232 * \param[in] index Zero-based index for the frame to start.
233 * \param[in] x x value for the frame.
234 * \param[in] dx Error in x for the frame if applicable.
236 * \throws unspecified Any exception thrown by attached data
237 * modules in AnalysisDataModuleInterface::frameStarted().
239 * Each \p index value 0, 1, ..., N (where N is the total number of
240 * frames) should be started exactly once by exactly one handle of an
241 * AnalysisData object. The frames may be started out of order, but
242 * currently the implementation places some limitations on how far
243 * the index can be in the future (as counted from the first frame that
246 void startFrame(int index, real x, real dx = 0.0);
248 * Set a value for a single column for the current frame.
250 * \param[in] column Zero-based column index.
251 * \param[in] value Value to set for the column.
252 * \param[in] bPresent Present flag to set for the column.
254 * If called multiple times for a column (within one point set for
255 * multipoint data), old values are overwritten.
259 void setPoint(int column, real value, bool bPresent = true);
261 * Set a value and its error estimate for a single column for the
264 * \param[in] column Zero-based column index.
265 * \param[in] value Value to set for the column.
266 * \param[in] error Error estimate to set for the column.
267 * \param[in] bPresent Present flag to set for the column.
269 * If called multiple times for a column (within one point set for
270 * multipoint data), old values are overwritten.
274 void setPoint(int column, real value, real error, bool bPresent = true);
276 * Set values for consecutive columns for the current frame.
278 * \param[in] firstColumn Zero-based column index.
279 * \param[in] count Number of columns to set.
280 * \param[in] values Value array of \p column items.
282 * Equivalent to calling setPoint(firstColumn + i, values[i]) for
287 void setPoints(int firstColumn, int count, const real *values);
289 * Finish data for the current point set.
291 * \throws APIError if any attached data module is not compatible.
292 * \throws unspecified Any exception thrown by attached data
293 * modules in AnalysisDataModuleInterface::pointsAdded().
295 * Must be called after each point set for multipoint data, including
296 * the last (i.e., no values must be set between the last call to this
297 * method and AnalysisDataStorage::finishFrame()).
298 * Must not be called for non-multipoint data.
300 void finishPointSet();
302 * Finish data for the current frame.
304 * \throws APIError if any attached data module is not compatible.
305 * \throws unspecified Any exception thrown by attached data
306 * modules in frame notification methods.
309 //! Calls AnalysisData::finishData() for this handle.
314 * Creates a new data handle associated with \p data.
316 * \param data Data to associate the handle with.
318 * The constructor is private because data handles should only be
319 * constructed through AnalysisData::startData().
323 explicit AnalysisDataHandle(internal::AnalysisDataHandleImpl *impl);
326 * Pointer to the internal implementation class.
328 * The memory for this object is managed by the AnalysisData object,
329 * and AnalysisDataHandle simply provides a public interface for
330 * accessing the implementation.
332 internal::AnalysisDataHandleImpl *impl_;
335 * Needed to access the non-public implementation.
337 friend class AnalysisData;