2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012, 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 * Currently, multiple handles with multipoint data are not implemented.
74 * Parallel implementation is not complete.
77 * Special note for MPI implementation: assuming that the initialization of
78 * data objects is identical in all processes, associating the data objects
79 * in different MPI processes should be possible without changes in the
81 * Alternative, more robust implementation could get a unique ID as parameter
82 * to the constructor or a separate function, but would require all tools to
83 * provide it. With the current registration mechanism in
84 * TrajectoryAnalysisModule, this should be straightforward.
88 * \ingroup module_analysisdata
90 class AnalysisData : public AbstractAnalysisData
94 * Creates an empty analysis data object.
96 * \throws std::bad_alloc if out of memory.
99 virtual ~AnalysisData();
102 * Sets the number of columns in the data.
104 * \param[in] ncol Number of columns in the data (must be > 0).
106 * Must be called before startData(), and can be called multiple times
107 * before modules are added.
108 * Must not be called after startData() has been called.
110 * Does not currently throw, but this may change for the case that
111 * modules have already been added.
113 void setColumnCount(int ncol);
115 * Sets whether the data contains multiple points per column per frame.
117 * \param[in] bMultipoint Whether the data will allow multiple points
118 * per column within a single frame.
120 * If this method is not called, the data is not multipoint.
122 * Must not be called after modules have been added or startData() has
125 * Does not currently throw, but this may change for the case that
126 * modules have already been added.
128 * \see isMultipoint()
130 void setMultipoint(bool bMultipoint);
133 * Create a handle for adding data.
135 * \param[in] opt Options for setting how this handle will be
137 * \returns The created handle.
138 * \throws std::bad_alloc if out of memory.
139 * \throws APIError if any attached data module is not compatible.
140 * \throws unspecified Any exception thrown by attached data modules
141 * in AnalysisDataModuleInterface::dataStarted().
143 * The caller should retain the returned handle (or a copy of it), and
144 * pass it to finishData() after successfully adding all data.
145 * The caller should discard the returned handle if an error occurs;
146 * memory allocated for the handle will be freed when the AnalysisData
147 * object is destroyed.
149 * The \p opt options should be the same for all calls to this method,
150 * and the number of calls should match the parallelization factor
153 AnalysisDataHandle startData(const AnalysisDataParallelOptions &opt);
155 * Destroy a handle after all data has been added.
157 * \param[in] handle Handle to destroy.
158 * \throws unspecified Any exception thrown by attached data modules
159 * in AnalysisDataModuleInterface::dataFinished().
161 * \p handle must have been obtained from startData() of this object.
162 * The order of the calls with respect to the corresponding startData()
163 * calls is not important.
165 * The \p handle (and any copies) are invalid after the call.
167 void finishData(AnalysisDataHandle handle);
170 virtual AnalysisDataFrameRef tryGetDataFrameInternal(int index) const;
171 virtual bool requestStorageInternal(int nframes);
175 PrivateImplPointer<Impl> impl_;
177 friend class AnalysisDataHandle;
182 class AnalysisDataHandleImpl;
183 } // namespace internal
186 * Handle for inserting data into AnalysisData.
188 * This class provides an interface for adding data frames into an AnalysisData
189 * object. After a handle is obtained from AnalysisData::startData(), new
190 * frames can be added using startFrame(). Then values for that frame are set
191 * using provided methods (see below), and finishFrame() is called. After all
192 * frames have been added, finishData() (or AnalysisData::finishData()) must be
195 * For simple (non-multipoint) data, within a frame values can be set using
196 * setPoint() and setPoints(). Setting the same column multiple times
197 * overrides previously set values. When the frame is finished, attached
198 * modules are notified.
200 * Multipoint data works otherwise similarly, but requires finishPointSet() to
201 * be called for each set of points for which the modules need to be notified.
202 * Each point set starts empty (after startFrame() or finishPointSet()), and
203 * values can be set using setPoint()/setPoints(). finishPointSet() must also
204 * be called for the last point set just before finishFrame().
206 * This class works like a pointer type: copying and assignment is lightweight,
207 * and all copies work interchangeably, accessing the same internal handle.
208 * However, normally you should only keep one copy of a handle, i.e., treat
209 * this type as movable.
210 * Several handles created from the same AnalysisData object can exist
211 * concurrently, but must currently operate on separate frames.
214 * \ingroup module_analysisdata
216 class AnalysisDataHandle
220 * Constructs an invalid data handle.
222 * This constructor is provided for convenience in cases where it is
223 * easiest to declare an AnalysisDataHandle without immediately
224 * assigning a value to it. Any attempt to call methods without first
225 * assigning a value from AnalysisData::startData() to the handle
230 AnalysisDataHandle();
233 * Start data for a new frame.
235 * \param[in] index Zero-based index for the frame to start.
236 * \param[in] x x value for the frame.
237 * \param[in] dx Error in x for the frame if applicable.
239 * \throws unspecified Any exception thrown by attached data
240 * modules in AnalysisDataModuleInterface::frameStarted().
242 * Each \p index value 0, 1, ..., N (where N is the total number of
243 * frames) should be started exactly once by exactly one handle of an
244 * AnalysisData object. The frames may be started out of order, but
245 * currently the implementation places some limitations on how far
246 * the index can be in the future (as counted from the first frame that
249 void startFrame(int index, real x, real dx = 0.0);
251 * Set a value for a single column for the current frame.
253 * \param[in] column Zero-based column index.
254 * \param[in] value Value to set for the column.
255 * \param[in] bPresent Present flag to set for the column.
257 * If called multiple times for a column (within one point set for
258 * multipoint data), old values are overwritten.
262 void setPoint(int column, real value, bool bPresent = true);
264 * Set a value and its error estimate for a single column for the
267 * \param[in] column Zero-based column index.
268 * \param[in] value Value to set for the column.
269 * \param[in] error Error estimate to set for the column.
270 * \param[in] bPresent Present flag to set for the column.
272 * If called multiple times for a column (within one point set for
273 * multipoint data), old values are overwritten.
277 void setPoint(int column, real value, real error, bool bPresent = true);
279 * Set values for consecutive columns for the current frame.
281 * \param[in] firstColumn Zero-based column index.
282 * \param[in] count Number of columns to set.
283 * \param[in] values Value array of \p column items.
285 * Equivalent to calling setPoint(firstColumn + i, values[i]) for
290 void setPoints(int firstColumn, int count, const real *values);
292 * Finish data for the current point set.
294 * \throws APIError if any attached data module is not compatible.
295 * \throws unspecified Any exception thrown by attached data
296 * modules in AnalysisDataModuleInterface::pointsAdded().
298 * Must be called after each point set for multipoint data, including
299 * the last (i.e., no values must be set between the last call to this
300 * method and AnalysisDataStorage::finishFrame()).
301 * Must not be called for non-multipoint data.
303 void finishPointSet();
305 * Finish data for the current frame.
307 * \throws APIError if any attached data module is not compatible.
308 * \throws unspecified Any exception thrown by attached data
309 * modules in frame notification methods.
312 //! Calls AnalysisData::finishData() for this handle.
317 * Creates a new data handle associated with \p data.
319 * \param data Data to associate the handle with.
321 * The constructor is private because data handles should only be
322 * constructed through AnalysisData::startData().
326 explicit AnalysisDataHandle(internal::AnalysisDataHandleImpl *impl);
329 * Pointer to the internal implementation class.
331 * The memory for this object is managed by the AnalysisData object,
332 * and AnalysisDataHandle simply provides a public interface for
333 * accessing the implementation.
335 internal::AnalysisDataHandleImpl *impl_;
338 * Needed to access the non-public implementation.
340 friend class AnalysisData;