Merge remote-tracking branch 'gerrit/release-4-6'
[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 int frameCount() const;
68         virtual bool getDataWErr(int index, real *x, real *dx,
69                                  const real **y, const real **dy,
70                                  const bool **present = 0) const;
71         virtual bool requestStorage(int nframes = -1);
72
73     protected:
74         AbstractAnalysisArrayData();
75
76         /*! \brief
77          * Returns the number of rows in the data array.
78          *
79          * This function is identical to frameCount(), except that frameCount()
80          * returns 0 before valuesReady() has been called.
81          */
82         int rowCount() const { return _nrows; }
83         /*! \brief
84          * Sets the number of columns in the data array.
85          */
86         void setColumnCount(int ncols);
87         /*! \brief
88          * Sets the number of rows in the data array.
89          */
90         void setRowCount(int nrows);
91         //! Returns the x value of the first frame.
92         real xstart() const { return _xstart; }
93         //! Returns the step between frame x values.
94         real xstep() const { return _xstep; }
95         /*! \brief
96          * Sets the values reported as x values for frames.
97          */
98         void setXAxis(real start, real step);
99         //! Returns a reference to a given array element.
100         real &value(int row, int col)
101         {
102             GMX_ASSERT(row >= 0 && row < _nrows, "Row index out of range");
103             GMX_ASSERT(col >= 0 && col < columnCount(), "Column index out of range");
104             GMX_ASSERT(_value != NULL, "Data array not allocated");
105             return _value[row * columnCount() + col];
106         }
107         //! Returns a given array element.
108         const real &value(int row, int col) const
109         {
110             GMX_ASSERT(row >= 0 && row < _nrows, "Row index out of range");
111             GMX_ASSERT(col >= 0 && col < columnCount(), "Column index out of range");
112             GMX_ASSERT(_value != NULL, "Data array not allocated");
113             return _value[row * columnCount() + col];
114         }
115         /*! \brief
116          * Sets the value of an element in the array.
117          */
118         void setValue(int row, int col, real val)
119         {
120             value(row, col) = val;
121         }
122         /*! \brief
123          * Notifies modules of the data.
124          *
125          * This function should be called once the values in the array
126          * have been initialized. The values should not be changed after this
127          * function has been called.
128          */
129         void valuesReady();
130
131         /*! \brief
132          * Copies the contents into a new object.
133          *
134          * \param[in]     src  Object to copy data from.
135          * \param[in,out] dest Empty array data object to copy data to.
136          *
137          * \p dest should not have previous contents.
138          */
139         static void copyContents(const AbstractAnalysisArrayData *src,
140                                  AbstractAnalysisArrayData *dest);
141
142     private:
143         int                  _nrows;
144         real                *_value;
145         real                 _xstart;
146         real                 _xstep;
147         bool                 _bReady;
148
149         // Copy and assign disallowed by base.
150 };
151
152 /*! \brief
153  * Simple in-memory data array.
154  *
155  * This class simply exposes the protected functions of
156  * AbstractAnalysisArrayData to allow construction of simple in-memory data
157  * arrays for input into data modules.
158  *
159  * \inpublicapi
160  * \ingroup module_analysisdata
161  */
162 class AnalysisArrayData : public AbstractAnalysisArrayData
163 {
164     public:
165         AnalysisArrayData() {}
166
167         // TODO: These statements cause Doxygen to generate confusing
168         // documentation.
169         using AbstractAnalysisArrayData::rowCount;
170         using AbstractAnalysisArrayData::setColumnCount;
171         using AbstractAnalysisArrayData::setRowCount;
172         using AbstractAnalysisArrayData::xstart;
173         using AbstractAnalysisArrayData::xstep;
174         using AbstractAnalysisArrayData::setXAxis;
175         using AbstractAnalysisArrayData::value;
176         using AbstractAnalysisArrayData::setValue;
177         using AbstractAnalysisArrayData::valuesReady;
178
179         // Copy and assign disallowed by base.
180 };
181
182 } // namespace gmx
183
184 #endif