Merge branch release-4-6 into master
[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,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.
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  * Parallel implementation is not complete.
72  *
73  * \if internal
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
77  * interface.
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.
82  * \endif
83  *
84  * \inpublicapi
85  * \ingroup module_analysisdata
86  */
87 class AnalysisData : public AbstractAnalysisData
88 {
89     public:
90         /*! \brief
91          * Creates an empty analysis data object.
92          *
93          * \throws std::bad_alloc if out of memory.
94          */
95         AnalysisData();
96         virtual ~AnalysisData();
97
98         /*! \brief
99          * Sets the number of columns in the data.
100          *
101          * \param[in] ncol  Number of columns in the data (must be > 0).
102          *
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.
106          *
107          * Does not currently throw, but this may change for the case that
108          * modules have already been added.
109          */
110         void setColumnCount(int ncol);
111         /*! \brief
112          * Sets whether the data contains multiple points per column per frame.
113          *
114          * \param[in] bMultipoint  Whether the data will allow multiple points
115          *      per column within a single frame.
116          *
117          * If this method is not called, the data is not multipoint.
118          *
119          * Must not be called after modules have been added or startData() has
120          * been called.
121          *
122          * Does not currently throw, but this may change for the case that
123          * modules have already been added.
124          *
125          * \see isMultipoint()
126          */
127         void setMultipoint(bool bMultipoint);
128
129         /*! \brief
130          * Create a handle for adding data.
131          *
132          * \param[in]  opt     Options for setting how this handle will be
133          *     used.
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().
139          *
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.
145          *
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
148          * defined in \p opt.
149          */
150         AnalysisDataHandle startData(const AnalysisDataParallelOptions &opt);
151         /*! \brief
152          * Destroy a handle after all data has been added.
153          *
154          * \param[in]  handle  Handle to destroy.
155          * \throws  unspecified  Any exception thrown by attached data modules
156          *      in AnalysisDataModuleInterface::dataFinished().
157          *
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.
161          *
162          * The \p handle (and any copies) are invalid after the call.
163          */
164         void finishData(AnalysisDataHandle handle);
165
166     private:
167         virtual AnalysisDataFrameRef tryGetDataFrameInternal(int index) const;
168         virtual bool requestStorageInternal(int nframes);
169
170         class Impl;
171
172         PrivateImplPointer<Impl> impl_;
173
174         friend class AnalysisDataHandle;
175 };
176
177 namespace internal
178 {
179 class AnalysisDataHandleImpl;
180 }   // namespace internal
181
182 /*! \brief
183  * Handle for inserting data into AnalysisData.
184  *
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
190  * called.
191  *
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.
196  *
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().
202  *
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.
209  *
210  * \inpublicapi
211  * \ingroup module_analysisdata
212  */
213 class AnalysisDataHandle
214 {
215     public:
216         /*! \brief
217          * Constructs an invalid data handle.
218          *
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
223          * causes an assert.
224          *
225          * Does not throw.
226          */
227         AnalysisDataHandle();
228
229         /*! \brief
230          * Start data for a new frame.
231          *
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.
235          *
236          * \throws    unspecified  Any exception thrown by attached data
237          *      modules in AnalysisDataModuleInterface::frameStarted().
238          *
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
244          * is not finished).
245          */
246         void startFrame(int index, real x, real dx = 0.0);
247         /*! \brief
248          * Set a value for a single column for the current frame.
249          *
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.
253          *
254          * If called multiple times for a column (within one point set for
255          * multipoint data), old values are overwritten.
256          *
257          * Does not throw.
258          */
259         void setPoint(int column, real value, bool bPresent = true);
260         /*! \brief
261          * Set a value and its error estimate for a single column for the
262          * current frame.
263          *
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.
268          *
269          * If called multiple times for a column (within one point set for
270          * multipoint data), old values are overwritten.
271          *
272          * Does not throw.
273          */
274         void setPoint(int column, real value, real error, bool bPresent = true);
275         /*! \brief
276          * Set values for consecutive columns for the current frame.
277          *
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.
281          *
282          * Equivalent to calling setPoint(firstColumn + i, values[i]) for
283          * i from 0 to count.
284          *
285          * Does not throw.
286          */
287         void setPoints(int firstColumn, int count, const real *values);
288         /*! \brief
289          * Finish data for the current point set.
290          *
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().
294          *
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.
299          */
300         void finishPointSet();
301         /*! \brief
302          * Finish data for the current frame.
303          *
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.
307          */
308         void finishFrame();
309         //! Calls AnalysisData::finishData() for this handle.
310         void finishData();
311
312     private:
313         /*! \brief
314          * Creates a new data handle associated with \p data.
315          *
316          * \param  data Data to associate the handle with.
317          *
318          * The constructor is private because data handles should only be
319          * constructed through AnalysisData::startData().
320          *
321          * Does not throw.
322          */
323         explicit AnalysisDataHandle(internal::AnalysisDataHandleImpl *impl);
324
325         /*! \brief
326          * Pointer to the internal implementation class.
327          *
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.
331          */
332         internal::AnalysisDataHandleImpl *impl_;
333
334         /*! \brief
335          * Needed to access the non-public implementation.
336          */
337         friend class AnalysisData;
338 };
339
340 } // namespace gmx
341
342 #endif