Merge branch 'release-4-6'
[alexxy/gromacs.git] / src / gromacs / analysisdata / analysisdata.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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 /*! \file
36  * \brief
37  * Declares gmx::AnalysisData and gmx::AnalysisDataHandle.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inpublicapi
41  * \ingroup module_analysisdata
42  */
43 #ifndef GMX_ANALYSISDATA_ANALYSISDATA_H
44 #define GMX_ANALYSISDATA_ANALYSISDATA_H
45
46 #include "abstractdata.h"
47
48 namespace gmx
49 {
50
51 class AnalysisDataHandle;
52 class AnalysisDataParallelOptions;
53
54 /*! \brief
55  * Parallelizable data container for raw data.
56  *
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
68  * expect them.
69  *
70  * \todo
71  * Currently, multiple handles with multipoint data are not implemented.
72  *
73  * \todo
74  * Parallel implementation is not complete.
75  *
76  * \if internal
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
80  * interface.
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.
85  * \endif
86  *
87  * \inpublicapi
88  * \ingroup module_analysisdata
89  */
90 class AnalysisData : public AbstractAnalysisData
91 {
92     public:
93         /*! \brief
94          * Creates an empty analysis data object.
95          *
96          * \throws std::bad_alloc if out of memory.
97          */
98         AnalysisData();
99         virtual ~AnalysisData();
100
101         /*! \brief
102          * Sets the number of columns in the data.
103          *
104          * \param[in] ncol  Number of columns in the data (must be > 0).
105          *
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.
109          *
110          * Does not currently throw, but this may change for the case that
111          * modules have already been added.
112          */
113         void setColumnCount(int ncol);
114         /*! \brief
115          * Sets whether the data contains multiple points per column per frame.
116          *
117          * \param[in] bMultipoint  Whether the data will allow multiple points
118          *      per column within a single frame.
119          *
120          * If this method is not called, the data is not multipoint.
121          *
122          * Must not be called after modules have been added or startData() has
123          * been called.
124          *
125          * Does not currently throw, but this may change for the case that
126          * modules have already been added.
127          *
128          * \see isMultipoint()
129          */
130         void setMultipoint(bool bMultipoint);
131
132         /*! \brief
133          * Create a handle for adding data.
134          *
135          * \param[in]  opt     Options for setting how this handle will be
136          *     used.
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().
142          *
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.
148          *
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
151          * defined in \p opt.
152          */
153         AnalysisDataHandle startData(const AnalysisDataParallelOptions &opt);
154         /*! \brief
155          * Destroy a handle after all data has been added.
156          *
157          * \param[in]  handle  Handle to destroy.
158          * \throws  unspecified  Any exception thrown by attached data modules
159          *      in AnalysisDataModuleInterface::dataFinished().
160          *
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.
164          *
165          * The \p handle (and any copies) are invalid after the call.
166          */
167         void finishData(AnalysisDataHandle handle);
168
169     private:
170         virtual AnalysisDataFrameRef tryGetDataFrameInternal(int index) const;
171         virtual bool requestStorageInternal(int nframes);
172
173         class Impl;
174
175         PrivateImplPointer<Impl> impl_;
176
177         friend class AnalysisDataHandle;
178 };
179
180 namespace internal
181 {
182 class AnalysisDataHandleImpl;
183 }   // namespace internal
184
185 /*! \brief
186  * Handle for inserting data into AnalysisData.
187  *
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
193  * called.
194  *
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.
199  *
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().
205  *
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.
212  *
213  * \inpublicapi
214  * \ingroup module_analysisdata
215  */
216 class AnalysisDataHandle
217 {
218     public:
219         /*! \brief
220          * Constructs an invalid data handle.
221          *
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
226          * causes an assert.
227          *
228          * Does not throw.
229          */
230         AnalysisDataHandle();
231
232         /*! \brief
233          * Start data for a new frame.
234          *
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.
238          *
239          * \throws    unspecified  Any exception thrown by attached data
240          *      modules in AnalysisDataModuleInterface::frameStarted().
241          *
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
247          * is not finished).
248          */
249         void startFrame(int index, real x, real dx = 0.0);
250         /*! \brief
251          * Set a value for a single column for the current frame.
252          *
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.
256          *
257          * If called multiple times for a column (within one point set for
258          * multipoint data), old values are overwritten.
259          *
260          * Does not throw.
261          */
262         void setPoint(int column, real value, bool bPresent = true);
263         /*! \brief
264          * Set a value and its error estimate for a single column for the
265          * current frame.
266          *
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.
271          *
272          * If called multiple times for a column (within one point set for
273          * multipoint data), old values are overwritten.
274          *
275          * Does not throw.
276          */
277         void setPoint(int column, real value, real error, bool bPresent = true);
278         /*! \brief
279          * Set values for consecutive columns for the current frame.
280          *
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.
284          *
285          * Equivalent to calling setPoint(firstColumn + i, values[i]) for
286          * i from 0 to count.
287          *
288          * Does not throw.
289          */
290         void setPoints(int firstColumn, int count, const real *values);
291         /*! \brief
292          * Finish data for the current point set.
293          *
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().
297          *
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.
302          */
303         void finishPointSet();
304         /*! \brief
305          * Finish data for the current frame.
306          *
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.
310          */
311         void finishFrame();
312         //! Calls AnalysisData::finishData() for this handle.
313         void finishData();
314
315     private:
316         /*! \brief
317          * Creates a new data handle associated with \p data.
318          *
319          * \param  data Data to associate the handle with.
320          *
321          * The constructor is private because data handles should only be
322          * constructed through AnalysisData::startData().
323          *
324          * Does not throw.
325          */
326         explicit AnalysisDataHandle(internal::AnalysisDataHandleImpl *impl);
327
328         /*! \brief
329          * Pointer to the internal implementation class.
330          *
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.
334          */
335         internal::AnalysisDataHandleImpl *impl_;
336
337         /*! \brief
338          * Needed to access the non-public implementation.
339          */
340         friend class AnalysisData;
341 };
342
343 } // namespace gmx
344
345 #endif