Added some more analysisdata unit tests.
[alexxy/gromacs.git] / src / gromacs / analysisdata / arraydata.h
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \file
32  * \brief
33  * Declares gmx::AbstractAnalysisArrayData and gmx::AnalysisArrayData.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \inpublicapi
37  * \ingroup module_analysisdata
38  */
39 #ifndef GMX_ANALYSISDATA_ARRAYDATA_H
40 #define GMX_ANALYSISDATA_ARRAYDATA_H
41
42 #include <cstddef>
43
44 #include "../fatalerror/gmxassert.h"
45
46 #include "abstractdata.h"
47
48 namespace gmx
49 {
50
51 /*! \brief
52  * Abstract base class for data objects that present in-memory data.
53  *
54  * This class implements a subclass of AbstractAnalysisData that presents an
55  * in-memory array through the AbstractAnalysisData interface.  Subclasses
56  * should initialize the in-memory array through the provided protected member
57  * functions.
58  *
59  * \inlibraryapi
60  * \ingroup module_analysisdata
61  */
62 class AbstractAnalysisArrayData : public AbstractAnalysisData
63 {
64     public:
65         virtual ~AbstractAnalysisArrayData();
66
67         virtual bool getDataWErr(int index, real *x, real *dx,
68                                  const real **y, const real **dy,
69                                  const bool **present = 0) const;
70         virtual bool requestStorage(int nframes = -1);
71
72     protected:
73         AbstractAnalysisArrayData();
74
75         /*! \brief
76          * Returns the number of rows in the data array.
77          *
78          * This function is identical to frameCount(), except that frameCount()
79          * returns 0 before valuesReady() has been called.
80          */
81         int rowCount() const { return _nrows; }
82         /*! \brief
83          * Sets the number of columns in the data array.
84          */
85         void setColumnCount(int ncols);
86         /*! \brief
87          * Sets the number of rows in the data array.
88          */
89         void setRowCount(int nrows);
90         //! Returns the x value of the first frame.
91         real xstart() const { return _xstart; }
92         //! Returns the step between frame x values.
93         real xstep() const { return _xstep; }
94         /*! \brief
95          * Sets the values reported as x values for frames.
96          */
97         void setXAxis(real start, real step);
98         //! Returns a reference to a given array element.
99         real &value(int row, int col)
100         {
101             GMX_ASSERT(row >= 0 && row < _nrows, "Row index out of range");
102             GMX_ASSERT(col >= 0 && col < columnCount(), "Column index out of range");
103             GMX_ASSERT(_value != NULL, "Data array not allocated");
104             return _value[row * columnCount() + col];
105         }
106         //! Returns a given array element.
107         const real &value(int row, int col) const
108         {
109             GMX_ASSERT(row >= 0 && row < _nrows, "Row index out of range");
110             GMX_ASSERT(col >= 0 && col < columnCount(), "Column index out of range");
111             GMX_ASSERT(_value != NULL, "Data array not allocated");
112             return _value[row * columnCount() + col];
113         }
114         /*! \brief
115          * Sets the value of an element in the array.
116          */
117         void setValue(int row, int col, real val)
118         {
119             value(row, col) = val;
120         }
121         /*! \brief
122          * Notifies modules of the data.
123          *
124          * This function should be called once the values in the array
125          * have been initialized. The values should not be changed after this
126          * function has been called.
127          */
128         void valuesReady();
129
130         /*! \brief
131          * Copies the contents into a new object.
132          *
133          * \param[in]     src  Object to copy data from.
134          * \param[in,out] dest Empty array data object to copy data to.
135          *
136          * \p dest should not have previous contents.
137          */
138         static void copyContents(const AbstractAnalysisArrayData *src,
139                                  AbstractAnalysisArrayData *dest);
140
141     private:
142         int                  _nrows;
143         real                *_value;
144         real                 _xstart;
145         real                 _xstep;
146         bool                 _bReady;
147
148         // Copy and assign disallowed by base.
149 };
150
151 /*! \brief
152  * Simple in-memory data array.
153  *
154  * This class simply exposes the protected functions of
155  * AbstractAnalysisArrayData to allow construction of simple in-memory data
156  * arrays for input into data modules.
157  *
158  * \inpublicapi
159  * \ingroup module_analysisdata
160  */
161 class AnalysisArrayData : public AbstractAnalysisArrayData
162 {
163     public:
164         AnalysisArrayData() {}
165
166         // TODO: These statements cause Doxygen to generate confusing
167         // documentation.
168         using AbstractAnalysisArrayData::rowCount;
169         using AbstractAnalysisArrayData::setColumnCount;
170         using AbstractAnalysisArrayData::setRowCount;
171         using AbstractAnalysisArrayData::xstart;
172         using AbstractAnalysisArrayData::xstep;
173         using AbstractAnalysisArrayData::setXAxis;
174         using AbstractAnalysisArrayData::value;
175         using AbstractAnalysisArrayData::setValue;
176         using AbstractAnalysisArrayData::valuesReady;
177
178         // Copy and assign disallowed by base.
179 };
180
181 } // namespace gmx
182
183 #endif