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