Improve AbstractAnalysisData data access interface.
[alexxy/gromacs.git] / src / gromacs / analysisdata / arraydata.cpp
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 /*! \internal \file
32  * \brief
33  * Implements classes in arraydata.h.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \ingroup module_analysisdata
37  */
38 #include "gromacs/analysisdata/arraydata.h"
39
40 #include <algorithm>
41
42 #include "gromacs/analysisdata/dataframe.h"
43 #include "gromacs/fatalerror/exceptions.h"
44 #include "gromacs/fatalerror/gmxassert.h"
45
46 namespace gmx
47 {
48
49 AbstractAnalysisArrayData::AbstractAnalysisArrayData()
50     : _nrows(0), _xstart(0.0), _xstep(1.0), _bReady(false)
51 {
52 }
53
54 AbstractAnalysisArrayData::~AbstractAnalysisArrayData()
55 {
56 }
57
58
59 AnalysisDataFrameRef
60 AbstractAnalysisArrayData::tryGetDataFrameInternal(int index) const
61 {
62     return AnalysisDataFrameRef(index, xvalue(index), 0.0, columnCount(),
63                                 &_value[index * columnCount()], NULL, NULL);
64 }
65
66
67 bool
68 AbstractAnalysisArrayData::requestStorageInternal(int /*nframes*/)
69 {
70     return true;
71 }
72
73
74 void
75 AbstractAnalysisArrayData::setColumnCount(int ncols)
76 {
77     GMX_RELEASE_ASSERT(!isAllocated(),
78                        "Cannot change column count after data has been allocated");
79     AbstractAnalysisData::setColumnCount(ncols);
80 }
81
82
83 void
84 AbstractAnalysisArrayData::setRowCount(int nrows)
85 {
86     GMX_RELEASE_ASSERT(nrows > 0, "Invalid number of rows");
87     GMX_RELEASE_ASSERT(!isAllocated(),
88                        "Cannot change row count after data has been allocated");
89     _nrows = nrows;
90 }
91
92
93 void
94 AbstractAnalysisArrayData::allocateValues()
95 {
96     GMX_RELEASE_ASSERT(!isAllocated(), "Can only allocate values once");
97     GMX_RELEASE_ASSERT(rowCount() > 0 && columnCount() > 0,
98                        "Row and column counts must be set before allocating values");
99     _value.resize(rowCount() * columnCount());
100 }
101
102
103 void
104 AbstractAnalysisArrayData::setXAxis(real start, real step)
105 {
106     GMX_RELEASE_ASSERT(!_bReady, "X axis cannot be set after data is finished");
107     _xstart = start;
108     _xstep = step;
109 }
110
111
112 void
113 AbstractAnalysisArrayData::valuesReady()
114 {
115     GMX_RELEASE_ASSERT(isAllocated(), "There must be some data");
116     if (_bReady)
117     {
118         return;
119     }
120     _bReady = true;
121
122     notifyDataStart();
123     for (int i = 0; i < rowCount(); ++i)
124     {
125         AnalysisDataFrameHeader header(i, xvalue(i), 0);
126         notifyFrameStart(header);
127         notifyPointsAdd(AnalysisDataPointSetRef(header, 0, columnCount(),
128                                                 &_value[i * columnCount()],
129                                                 NULL, NULL));
130         notifyFrameFinish(header);
131     }
132     notifyDataFinish();
133 }
134
135
136 void
137 AbstractAnalysisArrayData::copyContents(const AbstractAnalysisArrayData *src,
138                                         AbstractAnalysisArrayData *dest)
139 {
140     GMX_RELEASE_ASSERT(src->isAllocated(), "Source data must not be empty");
141     GMX_RELEASE_ASSERT(!dest->isAllocated(),
142                        "Destination data must not be allocated");
143     dest->setColumnCount(src->columnCount());
144     dest->setRowCount(src->rowCount());
145     dest->allocateValues();
146     dest->setXAxis(src->xstart(), src->xstep());
147     std::copy(src->_value.begin(), src->_value.end(), dest->_value.begin());
148 }
149
150 } // namespace gmx