2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012, 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.
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.
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.
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.
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.
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.
37 * Declares gmx::AbstractAnalysisArrayData and gmx::AnalysisArrayData.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_analysisdata
43 #ifndef GMX_ANALYSISDATA_ARRAYDATA_H
44 #define GMX_ANALYSISDATA_ARRAYDATA_H
48 #include "../utility/gmxassert.h"
50 #include "abstractdata.h"
51 #include "dataframe.h"
57 * Abstract base class for data objects that present in-memory data.
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
65 * Public accessor methods in this class do not throw, but assert if data is
66 * accessed before it is available.
69 * Add methods to take full advantage of AnalysisDataValue features.
72 * \ingroup module_analysisdata
74 class AbstractAnalysisArrayData : public AbstractAnalysisData
77 virtual ~AbstractAnalysisArrayData();
80 * Returns the number of rows in the data array.
82 * This function is identical to frameCount(), except that frameCount()
83 * returns 0 before valuesReady() has been called.
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
95 GMX_ASSERT(row >= 0 && row < rowCount(), "Row index out of range");
96 return xstart() + row * xstep();
98 //! Returns a given array element.
99 real value(int row, int col) const
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].value();
109 * Initializes an empty array data object.
111 * \throws std::bad_alloc if out of memory.
113 AbstractAnalysisArrayData();
116 * Sets the number of columns in the data array.
118 * \param[in] ncols Number of columns in the data.
120 * Cannot be called after allocateValues().
122 * See AbstractAnalysisData::setColumnCount() for exception behavior.
124 void setColumnCount(int ncols);
126 * Sets the number of rows in the data array.
128 * \param[in] rowCount Number of rows in the data.
130 * Cannot be called after allocateValues().
134 void setRowCount(int rowCount);
136 * Allocates memory for the values.
138 * \throws std::bad_alloc if memory allocation fails.
140 * setColumnCount() and setRowCount() must have been called.
142 * Strong exception safety guarantee.
144 void allocateValues();
146 * Sets the values reported as x values for frames.
148 * \param[in] start x value for the first frame.
149 * \param[in] step Step between x values of successive frames.
151 * Must not be called after valuesReady().
155 void setXAxis(real start, real step);
156 //! Returns a reference to a given array element.
157 real &value(int row, int col)
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].value();
165 * Sets the value of an element in the array.
167 * \param[in] row Zero-based row index for the value.
168 * \param[in] col Zero-based column index for the value.
169 * \param[in] val Value to set in the given location.
173 void setValue(int row, int col, real val)
175 value(row, col) = val;
178 * Notifies modules of the data.
180 * \throws unspecified Any exception thrown by attached data modules
181 * in data notification methods.
183 * This function should be called once the values in the array
184 * have been initialized. The values should not be changed after this
185 * function has been called.
190 * Copies the contents into a new object.
192 * \param[in] src Object to copy data from.
193 * \param[in,out] dest Empty array data object to copy data to.
194 * \throws std::bad_alloc if memory allocation for \p dest fails.
196 * \p dest should not have previous contents.
198 static void copyContents(const AbstractAnalysisArrayData *src,
199 AbstractAnalysisArrayData *dest);
202 virtual AnalysisDataFrameRef tryGetDataFrameInternal(int index) const;
203 virtual bool requestStorageInternal(int nframes);
206 std::vector<AnalysisDataValue> value_;
211 // Copy and assign disallowed by base.
215 * Simple in-memory data array.
217 * This class is a simple alternative to AnalysisData for in-memory data arrays
218 * that are constructed in-place.
220 * Public accessor methods in this class do not throw, but assert if data is
221 * accessed before it is available.
224 * This class exposes the protected functions of AbstractAnalysisArrayData for
229 * \ingroup module_analysisdata
231 class AnalysisArrayData : public AbstractAnalysisArrayData
235 * Initializes an empty array data object.
237 * \throws std::bad_alloc if out of memory.
239 AnalysisArrayData() {}
241 // TODO: These statements cause Doxygen to generate confusing
243 using AbstractAnalysisArrayData::setColumnCount;
244 using AbstractAnalysisArrayData::setRowCount;
245 using AbstractAnalysisArrayData::allocateValues;
246 using AbstractAnalysisArrayData::setXAxis;
247 using AbstractAnalysisArrayData::value;
248 using AbstractAnalysisArrayData::setValue;
249 using AbstractAnalysisArrayData::valuesReady;
251 // Copy and assign disallowed by base.