Support for multiple analysis data sets in modules.
[alexxy/gromacs.git] / src / gromacs / analysisdata / arraydata.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::AbstractAnalysisArrayData and gmx::AnalysisArrayData.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inpublicapi
41  * \ingroup module_analysisdata
42  */
43 #ifndef GMX_ANALYSISDATA_ARRAYDATA_H
44 #define GMX_ANALYSISDATA_ARRAYDATA_H
45
46 #include <vector>
47
48 #include "../utility/gmxassert.h"
49
50 #include "abstractdata.h"
51 #include "dataframe.h"
52
53 namespace gmx
54 {
55
56 /*! \brief
57  * Abstract base class for data objects that present in-memory data.
58  *
59  * This class implements a subclass of AbstractAnalysisData that presents an
60  * in-memory array through the AbstractAnalysisData interface.  Subclasses
61  * should initialize the in-memory array through the provided protected member
62  * functions.  This class provides public accessor methods for read access to
63  * the data.
64  *
65  * Public accessor methods in this class do not throw, but assert if data is
66  * accessed before it is available.
67  *
68  * \todo
69  * Add support for multiple data sets.
70  *
71  * \inlibraryapi
72  * \ingroup module_analysisdata
73  */
74 class AbstractAnalysisArrayData : public AbstractAnalysisData
75 {
76     public:
77         virtual ~AbstractAnalysisArrayData();
78
79         /*! \brief
80          * Returns the number of rows in the data array.
81          *
82          * This function is identical to frameCount(), except that frameCount()
83          * returns 0 before valuesReady() has been called.
84          */
85         int rowCount() const { return rowCount_; }
86         //! Returns true if values have been allocated.
87         bool isAllocated() const { return !value_.empty(); }
88         //! Returns the x value of the first frame.
89         real xstart() const { return xstart_; }
90         //! Returns the step between frame x values.
91         real xstep() const { return xstep_; }
92         //! Returns the x value of a row.
93         real xvalue(int row) const
94         {
95             GMX_ASSERT(row >= 0 && row < rowCount(), "Row index out of range");
96             return xstart() + row * xstep();
97         }
98         //! Returns a given array element.
99         const AnalysisDataValue &value(int row, int col) const
100         {
101             GMX_ASSERT(row >= 0 && row < rowCount(), "Row index out of range");
102             GMX_ASSERT(col >= 0 && col < columnCount(), "Column index out of range");
103             GMX_ASSERT(isAllocated(), "Data array not allocated");
104             return value_[row * columnCount() + col];
105         }
106
107     protected:
108         /*! \brief
109          * Initializes an empty array data object.
110          *
111          * \throws std::bad_alloc if out of memory.
112          */
113         AbstractAnalysisArrayData();
114
115         /*! \brief
116          * Sets the number of columns in the data array.
117          *
118          * \param[in] ncols  Number of columns in the data.
119          *
120          * Cannot be called after allocateValues().
121          *
122          * See AbstractAnalysisData::setColumnCount() for exception behavior.
123          */
124         void setColumnCount(int ncols);
125         /*! \brief
126          * Sets the number of rows in the data array.
127          *
128          * \param[in] rowCount  Number of rows in the data.
129          *
130          * Cannot be called after allocateValues().
131          *
132          * Does not throw.
133          */
134         void setRowCount(int rowCount);
135         /*! \brief
136          * Allocates memory for the values.
137          *
138          * \throws std::bad_alloc if memory allocation fails.
139          *
140          * setColumnCount() and setRowCount() must have been called.
141          *
142          * Strong exception safety guarantee.
143          */
144         void allocateValues();
145         /*! \brief
146          * Sets the values reported as x values for frames.
147          *
148          * \param[in] start  x value for the first frame.
149          * \param[in] step   Step between x values of successive frames.
150          *
151          * Must not be called after valuesReady().
152          *
153          * Does not throw.
154          */
155         void setXAxis(real start, real step);
156         //! Returns a reference to a given array element.
157         AnalysisDataValue &value(int row, int col)
158         {
159             GMX_ASSERT(row >= 0 && row < rowCount(), "Row index out of range");
160             GMX_ASSERT(col >= 0 && col < columnCount(), "Column index out of range");
161             GMX_ASSERT(isAllocated(), "Data array not allocated");
162             return value_[row * columnCount() + col];
163         }
164         /*! \brief
165          * Notifies modules of the data.
166          *
167          * \throws    unspecified Any exception thrown by attached data modules
168          *      in data notification methods.
169          *
170          * This function should be called once the values in the array
171          * have been initialized.  The values should not be changed after this
172          * function has been called.
173          */
174         void valuesReady();
175
176         /*! \brief
177          * Copies the contents into a new object.
178          *
179          * \param[in]     src  Object to copy data from.
180          * \param[in,out] dest Empty array data object to copy data to.
181          * \throws std::bad_alloc if memory allocation for \p dest fails.
182          *
183          * \p dest should not have previous contents.
184          */
185         static void copyContents(const AbstractAnalysisArrayData *src,
186                                  AbstractAnalysisArrayData       *dest);
187
188     private:
189         virtual AnalysisDataFrameRef tryGetDataFrameInternal(int index) const;
190         virtual bool requestStorageInternal(int nframes);
191
192         int                            rowCount_;
193         AnalysisDataPointSetInfo       pointSetInfo_;
194         std::vector<AnalysisDataValue> value_;
195         real                           xstart_;
196         real                           xstep_;
197         bool                           bReady_;
198
199         // Copy and assign disallowed by base.
200 };
201
202 /*! \brief
203  * Simple in-memory data array.
204  *
205  * This class is a simple alternative to AnalysisData for in-memory data arrays
206  * that are constructed in-place.
207  *
208  * Public accessor methods in this class do not throw, but assert if data is
209  * accessed before it is available.
210  *
211  * \if libapi
212  * This class exposes the protected functions of AbstractAnalysisArrayData for
213  * users.
214  * \endif
215  *
216  * \inpublicapi
217  * \ingroup module_analysisdata
218  */
219 class AnalysisArrayData : public AbstractAnalysisArrayData
220 {
221     public:
222         /*! \brief
223          * Initializes an empty array data object.
224          *
225          * \throws std::bad_alloc if out of memory.
226          */
227         AnalysisArrayData() {}
228
229         // TODO: These statements cause Doxygen to generate confusing
230         // documentation.
231         using AbstractAnalysisArrayData::setColumnCount;
232         using AbstractAnalysisArrayData::setRowCount;
233         using AbstractAnalysisArrayData::allocateValues;
234         using AbstractAnalysisArrayData::setXAxis;
235         using AbstractAnalysisArrayData::value;
236         using AbstractAnalysisArrayData::valuesReady;
237
238         // Copy and assign disallowed by base.
239 };
240
241 } // namespace gmx
242
243 #endif