4de2c116bb112bba5ffd68a6e32267901675481d
[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,2014, 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 /*! \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 "../utility/real.h"
47
48 #include "abstractdata.h"
49
50 namespace gmx
51 {
52
53 class AnalysisDataHandle;
54 class AnalysisDataParallelOptions;
55
56 /*! \brief
57  * Parallelizable data container for raw data.
58  *
59  * This is the main class used to implement parallelizable data processing in
60  * analysis tools.  It is used by first creating an object and setting its
61  * properties using setDataSetCount(), setColumnCount() and setMultipoint(),
62  * and attaching necessary modules using addModule() etc.  Then one or more
63  * AnalysisDataHandle objects can be created using startData().  Each data
64  * handle can then be independently used to provide data frames (each frame
65  * must be provided by a single handle, but different frames can be freely
66  * mixed between the handles).  When all data has been provided, the handles
67  * are destroyed using finishData() (or AnalysisDataHandle::finishData()).
68  * The AnalysisData object takes care of internally sorting the frames and
69  * passing them to the attached modules in the order in which the modules
70  * expect them.
71  *
72  * \todo
73  * Parallel implementation is not complete.
74  *
75  * \if internal
76  * Special note for MPI implementation: assuming that the initialization of
77  * data objects is identical in all processes, associating the data objects
78  * in different MPI processes should be possible without changes in the
79  * interface.
80  * Alternative, more robust implementation could get a unique ID as parameter
81  * to the constructor or a separate function, but would require all tools to
82  * provide it.  With the current registration mechanism in
83  * TrajectoryAnalysisModule, this should be straightforward.
84  * \endif
85  *
86  * \inpublicapi
87  * \ingroup module_analysisdata
88  */
89 class AnalysisData : public AbstractAnalysisData
90 {
91     public:
92         /*! \brief
93          * Creates an empty analysis data object.
94          *
95          * \throws std::bad_alloc if out of memory.
96          */
97         AnalysisData();
98         virtual ~AnalysisData();
99
100         /*! \brief
101          * Sets the number of data sets.
102          *
103          * \param[in] dataSetCount  Number of data sets (must be > 0).
104          * \throws    std::bad_alloc if out of memory.
105          * \throws    APIError if modules have been added that are not
106          *      compatible with the new data set count.
107          *
108          * Must not be called after startData() has been called.
109          * If not called, a single data set is assumed.
110          * If called multiple times, the last call takes effect.
111          */
112         void setDataSetCount(int dataSetCount);
113         /*! \brief
114          * Sets the number of columns in a data set.
115          *
116          * \param[in] dataSet      Zero-based data set index.
117          * \param[in] columnCount  Number of columns in the data (must be > 0).
118          * \throws    APIError if modules have been added that are not
119          *      compatible with the new column count.
120          *
121          * Must be called before startData() for each data set.
122          * Must not be called after startData() has been called.
123          * If called multiple times for a data set, the last call takes effect.
124          */
125         void setColumnCount(int dataSet, int columnCount);
126         /*! \brief
127          * Sets whether the data contains multiple points per column per frame.
128          *
129          * \param[in] bMultipoint  Whether the data will allow multiple points
130          *      per column within a single frame.
131          * \throws    APIError if modules have been added that are not
132          *      compatible with the new setting.
133          *
134          * If this method is not called, the data is not multipoint.
135          *
136          * Must not be called after startData() has been called.
137          *
138          * \see isMultipoint()
139          */
140         void setMultipoint(bool bMultipoint);
141
142         virtual int frameCount() const;
143
144         /*! \brief
145          * Create a handle for adding data.
146          *
147          * \param[in]  opt     Options for setting how this handle will be
148          *     used.
149          * \returns The created handle.
150          * \throws  std::bad_alloc if out of memory.
151          * \throws  APIError if any attached data module is not compatible.
152          * \throws  unspecified  Any exception thrown by attached data modules
153          *      in AnalysisDataModuleInterface::dataStarted().
154          *
155          * The caller should retain the returned handle (or a copy of it), and
156          * pass it to finishData() after successfully adding all data.
157          * The caller should discard the returned handle if an error occurs;
158          * memory allocated for the handle will be freed when the AnalysisData
159          * object is destroyed.
160          *
161          * The \p opt options should be the same for all calls to this method,
162          * and the number of calls should match the parallelization factor
163          * defined in \p opt.
164          */
165         AnalysisDataHandle startData(const AnalysisDataParallelOptions &opt);
166         /*! \brief
167          * Destroy a handle after all data has been added.
168          *
169          * \param[in]  handle  Handle to destroy.
170          * \throws  unspecified  Any exception thrown by attached data modules
171          *      in AnalysisDataModuleInterface::dataFinished().
172          *
173          * \p handle must have been obtained from startData() of this object.
174          * The order of the calls with respect to the corresponding startData()
175          * calls is not important.
176          *
177          * The \p handle (and any copies) are invalid after the call.
178          */
179         void finishData(AnalysisDataHandle handle);
180
181     private:
182         virtual AnalysisDataFrameRef tryGetDataFrameInternal(int index) const;
183         virtual bool requestStorageInternal(int nframes);
184
185         class Impl;
186
187         PrivateImplPointer<Impl> impl_;
188
189         friend class AnalysisDataHandle;
190 };
191
192 namespace internal
193 {
194 class AnalysisDataHandleImpl;
195 }   // namespace internal
196
197 /*! \brief
198  * Handle for inserting data into AnalysisData.
199  *
200  * This class provides an interface for adding data frames into an AnalysisData
201  * object.  After a handle is obtained from AnalysisData::startData(), new
202  * frames can be added using startFrame().  Then values for that frame are set
203  * using provided methods (see below), and finishFrame() is called.  After all
204  * frames have been added, finishData() (or AnalysisData::finishData()) must be
205  * called.
206  *
207  * For simple (non-multipoint) data, within a frame values can be set using
208  * selectDataSet(), setPoint() and setPoints().  Setting the same column in the
209  * same data set multiple times overrides previously set values.
210  * When the frame is finished, attached modules are notified.
211  *
212  * Multipoint data works otherwise similarly, but requires finishPointSet() to
213  * be called for each set of points for which the modules need to be notified.
214  * Each point set starts empty (after startFrame() or finishPointSet()), and
215  * values can be set using setPoint()/setPoints().
216  * A single point set can contain values only for a single data set, which must
217  * be selected with selectDataSet() before setting any values.
218  * finishPointSet() must also be called for the last point set just before
219  * finishFrame().
220  *
221  * This class works like a pointer type: copying and assignment is lightweight,
222  * and all copies work interchangeably, accessing the same internal handle.
223  * However, normally you should only keep one copy of a handle, i.e., treat
224  * this type as movable.
225  * Several handles created from the same AnalysisData object can exist
226  * concurrently, but must currently operate on separate frames.
227  *
228  * \inpublicapi
229  * \ingroup module_analysisdata
230  */
231 class AnalysisDataHandle
232 {
233     public:
234         /*! \brief
235          * Constructs an invalid data handle.
236          *
237          * This constructor is provided for convenience in cases where it is
238          * easiest to declare an AnalysisDataHandle without immediately
239          * assigning a value to it.  Any attempt to call methods without first
240          * assigning a value from AnalysisData::startData() to the handle
241          * causes an assert.
242          *
243          * Does not throw.
244          */
245         AnalysisDataHandle();
246
247         //! Returns whether this data handle is valid.
248         bool isValid() const { return impl_ != NULL; }
249
250         /*! \brief
251          * Start data for a new frame.
252          *
253          * \param[in] index  Zero-based index for the frame to start.
254          * \param[in] x      x value for the frame.
255          * \param[in] dx     Error in x for the frame if applicable.
256          *
257          * \throws    unspecified  Any exception thrown by attached data
258          *      modules in AnalysisDataModuleInterface::frameStarted().
259          *
260          * Each \p index value 0, 1, ..., N (where N is the total number of
261          * frames) should be started exactly once by exactly one handle of an
262          * AnalysisData object.  The frames may be started out of order, but
263          * currently the implementation places some limitations on how far
264          * the index can be in the future (as counted from the first frame that
265          * is not finished).
266          */
267         void startFrame(int index, real x, real dx = 0.0);
268         /*! \brief
269          * Selects a data set for subsequent setPoint()/setPoints() calls.
270          *
271          * \param[in] index  Zero-based data set index.
272          *
273          * After startFrame(), the first data set is always selected.
274          * The set value is remembered until the end of the current frame, also
275          * across finishPointSet() calls.
276          *
277          * Does not throw.
278          */
279         void selectDataSet(int index);
280         /*! \brief
281          * Set a value for a single column for the current frame.
282          *
283          * \param[in] column  Zero-based column index.
284          * \param[in] value   Value to set for the column.
285          * \param[in] bPresent Present flag to set for the column.
286          *
287          * If called multiple times for a column (within one point set for
288          * multipoint data), old values are overwritten.
289          *
290          * Does not throw.
291          */
292         void setPoint(int column, real value, bool bPresent = true);
293         /*! \brief
294          * Set a value and its error estimate for a single column for the
295          * current frame.
296          *
297          * \param[in] column  Zero-based column index.
298          * \param[in] value   Value to set for the column.
299          * \param[in] error   Error estimate to set for the column.
300          * \param[in] bPresent Present flag to set for the column.
301          *
302          * If called multiple times for a column (within one point set for
303          * multipoint data), old values are overwritten.
304          *
305          * Does not throw.
306          */
307         void setPoint(int column, real value, real error, bool bPresent = true);
308         /*! \brief
309          * Set values for consecutive columns for the current frame.
310          *
311          * \param[in] firstColumn  Zero-based column index.
312          * \param[in] count        Number of columns to set.
313          * \param[in] values       Value array of \p column items.
314          *
315          * Equivalent to calling setPoint(firstColumn + i, values[i]) for
316          * i from 0 to count.
317          *
318          * Does not throw.
319          */
320         void setPoints(int firstColumn, int count, const real *values);
321         /*! \brief
322          * Finish data for the current point set.
323          *
324          * \throws    APIError if any attached data module is not compatible.
325          * \throws    unspecified  Any exception thrown by attached data
326          *      modules in AnalysisDataModuleInterface::pointsAdded().
327          *
328          * Must be called after each point set for multipoint data, including
329          * the last (i.e., no values must be set between the last call to this
330          * method and AnalysisDataStorage::finishFrame()).
331          * Must not be called for non-multipoint data.
332          */
333         void finishPointSet();
334         /*! \brief
335          * Finish data for the current frame.
336          *
337          * \throws    APIError if any attached data module is not compatible.
338          * \throws    unspecified  Any exception thrown by attached data
339          *      modules in frame notification methods.
340          */
341         void finishFrame();
342         //! Calls AnalysisData::finishData() for this handle.
343         void finishData();
344
345     private:
346         /*! \brief
347          * Creates a new data handle associated with \p data.
348          *
349          * \param  impl Data to associate the handle with.
350          *
351          * The constructor is private because data handles should only be
352          * constructed through AnalysisData::startData().
353          *
354          * Does not throw.
355          */
356         explicit AnalysisDataHandle(internal::AnalysisDataHandleImpl *impl);
357
358         /*! \brief
359          * Pointer to the internal implementation class.
360          *
361          * The memory for this object is managed by the AnalysisData object,
362          * and AnalysisDataHandle simply provides a public interface for
363          * accessing the implementation.
364          */
365         internal::AnalysisDataHandleImpl *impl_;
366
367         /*! \brief
368          * Needed to access the non-public implementation.
369          */
370         friend class AnalysisData;
371 };
372
373 } // namespace gmx
374
375 #endif