Use frame objects in AnalysisDataModuleInterface.
[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 bool
60 AbstractAnalysisArrayData::getDataWErr(int index, real *x, real *dx,
61                                        const real **y, const real **dy,
62                                        const bool **present) const
63 {
64     if (index < 0)
65     {
66         index += frameCount() - 1;
67         if (index < 0)
68         {
69             return false;
70         }
71     }
72     if (index >= frameCount())
73     {
74         return false;
75     }
76     if (x != NULL)
77     {
78         *x = xvalue(index);
79     }
80     if (dx != NULL)
81     {
82         *dx = 0.0;
83     }
84     if (y != NULL)
85     {
86         *y = &_value[index * columnCount()];
87     }
88     if (dy != NULL)
89     {
90         // TODO: Implement
91         *dy = NULL;
92     }
93     if (present != NULL)
94     {
95         // TODO: Implement
96         *present = NULL;
97     }
98     return true;
99 }
100
101
102 bool
103 AbstractAnalysisArrayData::requestStorage(int /*nframes*/)
104 {
105     return true;
106 }
107
108
109 void
110 AbstractAnalysisArrayData::setColumnCount(int ncols)
111 {
112     GMX_RELEASE_ASSERT(!isAllocated(),
113                        "Cannot change column count after data has been allocated");
114     AbstractAnalysisData::setColumnCount(ncols);
115 }
116
117
118 void
119 AbstractAnalysisArrayData::setRowCount(int nrows)
120 {
121     GMX_RELEASE_ASSERT(nrows > 0, "Invalid number of rows");
122     GMX_RELEASE_ASSERT(!isAllocated(),
123                        "Cannot change row count after data has been allocated");
124     _nrows = nrows;
125 }
126
127
128 void
129 AbstractAnalysisArrayData::allocateValues()
130 {
131     GMX_RELEASE_ASSERT(!isAllocated(), "Can only allocate values once");
132     GMX_RELEASE_ASSERT(rowCount() > 0 && columnCount() > 0,
133                        "Row and column counts must be set before allocating values");
134     _value.resize(rowCount() * columnCount());
135 }
136
137
138 void
139 AbstractAnalysisArrayData::setXAxis(real start, real step)
140 {
141     GMX_RELEASE_ASSERT(!_bReady, "X axis cannot be set after data is finished");
142     _xstart = start;
143     _xstep = step;
144 }
145
146
147 void
148 AbstractAnalysisArrayData::valuesReady()
149 {
150     GMX_RELEASE_ASSERT(isAllocated(), "There must be some data");
151     if (_bReady)
152     {
153         return;
154     }
155     _bReady = true;
156
157     notifyDataStart();
158     for (int i = 0; i < rowCount(); ++i)
159     {
160         AnalysisDataFrameHeader header(i, xvalue(i), 0);
161         notifyFrameStart(header);
162         notifyPointsAdd(AnalysisDataPointSetRef(header, 0, columnCount(),
163                                                 &_value[i * columnCount()],
164                                                 NULL, NULL));
165         notifyFrameFinish();
166     }
167     notifyDataFinish();
168 }
169
170
171 void
172 AbstractAnalysisArrayData::copyContents(const AbstractAnalysisArrayData *src,
173                                         AbstractAnalysisArrayData *dest)
174 {
175     GMX_RELEASE_ASSERT(src->isAllocated(), "Source data must not be empty");
176     GMX_RELEASE_ASSERT(!dest->isAllocated(),
177                        "Destination data must not be allocated");
178     dest->setColumnCount(src->columnCount());
179     dest->setRowCount(src->rowCount());
180     dest->allocateValues();
181     dest->setXAxis(src->xstart(), src->xstep());
182     std::copy(src->_value.begin(), src->_value.end(), dest->_value.begin());
183 }
184
185 } // namespace gmx