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