Base support for multiple analysisdata data sets.
[alexxy/gromacs.git] / src / gromacs / analysisdata / analysisdata.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2013, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements classes in analysisdata.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_analysisdata
41  */
42 #include "gromacs/analysisdata/analysisdata.h"
43
44 #include "gromacs/analysisdata/dataframe.h"
45 #include "gromacs/analysisdata/datastorage.h"
46 #include "gromacs/analysisdata/paralleloptions.h"
47 #include "gromacs/utility/exceptions.h"
48 #include "gromacs/utility/gmxassert.h"
49 #include "gromacs/utility/uniqueptr.h"
50
51 namespace gmx
52 {
53
54 /********************************************************************
55  * AnalysisDataHandleImpl
56  */
57
58 namespace internal
59 {
60
61 /*! \internal \brief
62  * Private implementation class for AnalysisDataHandle.
63  *
64  * \ingroup module_analysisdata
65  */
66 class AnalysisDataHandleImpl
67 {
68     public:
69         //! Creates a handle associated with the given data object.
70         explicit AnalysisDataHandleImpl(AnalysisData *data)
71             : data_(*data), currentFrame_(NULL)
72         {
73         }
74
75         //! The data object that this handle belongs to.
76         AnalysisData             &data_;
77         //! Current storage frame object, or NULL if no current frame.
78         AnalysisDataStorageFrame *currentFrame_;
79 };
80
81 }   // namespace internal
82
83 /********************************************************************
84  * AnalysisData::Impl
85  */
86
87 /*! \internal \brief
88  * Private implementation class for AnalysisData.
89  *
90  * \ingroup module_analysisdata
91  */
92 class AnalysisData::Impl
93 {
94     public:
95         //! Smart pointer type to manage a data handle implementation.
96         typedef gmx_unique_ptr<internal::AnalysisDataHandleImpl>::type
97             HandlePointer;
98         //! Shorthand for a list of data handles.
99         typedef std::vector<HandlePointer> HandleList;
100
101         //! Storage implementation.
102         AnalysisDataStorage     storage_;
103         /*! \brief
104          * List of handles for this data object.
105          *
106          * Note that AnalysisDataHandle objects also contain (raw) pointers
107          * to these objects.
108          */
109         HandleList              handles_;
110 };
111
112 /********************************************************************
113  * AnalysisData
114  */
115
116 AnalysisData::AnalysisData()
117     : impl_(new Impl)
118 {
119 }
120
121
122 AnalysisData::~AnalysisData()
123 {
124 }
125
126
127 void
128 AnalysisData::setDataSetCount(int dataSetCount)
129 {
130     GMX_RELEASE_ASSERT(impl_->handles_.empty(),
131                        "Cannot change data dimensionality after creating handles");
132     AbstractAnalysisData::setDataSetCount(dataSetCount);
133 }
134
135
136 void
137 AnalysisData::setColumnCount(int dataSet, int columnCount)
138 {
139     GMX_RELEASE_ASSERT(impl_->handles_.empty(),
140                        "Cannot change data dimensionality after creating handles");
141     AbstractAnalysisData::setColumnCount(dataSet, columnCount);
142 }
143
144
145 void
146 AnalysisData::setMultipoint(bool bMultipoint)
147 {
148     GMX_RELEASE_ASSERT(impl_->handles_.empty(),
149                        "Cannot change data type after creating handles");
150     AbstractAnalysisData::setMultipoint(bMultipoint);
151 }
152
153
154 AnalysisDataHandle
155 AnalysisData::startData(const AnalysisDataParallelOptions &opt)
156 {
157     GMX_RELEASE_ASSERT(impl_->handles_.size() < static_cast<unsigned>(opt.parallelizationFactor()),
158                        "Too many calls to startData() compared to provided options");
159     if (impl_->handles_.empty())
160     {
161         notifyDataStart();
162         impl_->storage_.setParallelOptions(opt);
163         impl_->storage_.startDataStorage(this);
164     }
165
166     Impl::HandlePointer handle(new internal::AnalysisDataHandleImpl(this));
167     impl_->handles_.push_back(move(handle));
168     return AnalysisDataHandle(impl_->handles_.back().get());
169 }
170
171
172 void
173 AnalysisData::finishData(AnalysisDataHandle handle)
174 {
175     Impl::HandleList::iterator i;
176
177     for (i = impl_->handles_.begin(); i != impl_->handles_.end(); ++i)
178     {
179         if (i->get() == handle.impl_)
180         {
181             break;
182         }
183     }
184     GMX_RELEASE_ASSERT(i != impl_->handles_.end(),
185                        "finishData() called for an unknown handle");
186
187     impl_->handles_.erase(i);
188
189     if (impl_->handles_.empty())
190     {
191         notifyDataFinish();
192     }
193 }
194
195
196 AnalysisDataFrameRef
197 AnalysisData::tryGetDataFrameInternal(int index) const
198 {
199     return impl_->storage_.tryGetDataFrame(index);
200 }
201
202
203 bool
204 AnalysisData::requestStorageInternal(int nframes)
205 {
206     return impl_->storage_.requestStorage(nframes);
207 }
208
209
210 /********************************************************************
211  * AnalysisDataHandle
212  */
213
214 AnalysisDataHandle::AnalysisDataHandle()
215     : impl_(NULL)
216 {
217 }
218
219
220 AnalysisDataHandle::AnalysisDataHandle(internal::AnalysisDataHandleImpl *impl)
221     : impl_(impl)
222 {
223 }
224
225
226 void
227 AnalysisDataHandle::startFrame(int index, real x, real dx)
228 {
229     GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
230     GMX_RELEASE_ASSERT(impl_->currentFrame_ == NULL,
231                        "startFrame() called twice without calling finishFrame()");
232     impl_->currentFrame_ =
233         &impl_->data_.impl_->storage_.startFrame(index, x, dx);
234 }
235
236
237 void
238 AnalysisDataHandle::selectDataSet(int index)
239 {
240     GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
241     GMX_RELEASE_ASSERT(impl_->currentFrame_ != NULL,
242                        "selectDataSet() called without calling startFrame()");
243     impl_->currentFrame_->selectDataSet(index);
244 }
245
246
247 void
248 AnalysisDataHandle::setPoint(int column, real value, bool bPresent)
249 {
250     GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
251     GMX_RELEASE_ASSERT(impl_->currentFrame_ != NULL,
252                        "setPoint() called without calling startFrame()");
253     impl_->currentFrame_->setValue(column, value, bPresent);
254 }
255
256
257 void
258 AnalysisDataHandle::setPoint(int column, real value, real error, bool bPresent)
259 {
260     GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
261     GMX_RELEASE_ASSERT(impl_->currentFrame_ != NULL,
262                        "setPoint() called without calling startFrame()");
263     impl_->currentFrame_->setValue(column, value, error, bPresent);
264 }
265
266
267 void
268 AnalysisDataHandle::setPoints(int firstColumn, int count, const real *values)
269 {
270     GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
271     GMX_RELEASE_ASSERT(impl_->currentFrame_ != NULL,
272                        "setPoints() called without calling startFrame()");
273     for (int i = 0; i < count; ++i)
274     {
275         impl_->currentFrame_->setValue(firstColumn + i, values[i]);
276     }
277 }
278
279
280 void
281 AnalysisDataHandle::finishPointSet()
282 {
283     GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
284     GMX_RELEASE_ASSERT(impl_->data_.isMultipoint(),
285                        "finishPointSet() called for non-multipoint data");
286     GMX_RELEASE_ASSERT(impl_->currentFrame_ != NULL,
287                        "finishPointSet() called without calling startFrame()");
288     impl_->currentFrame_->finishPointSet();
289 }
290
291
292 void
293 AnalysisDataHandle::finishFrame()
294 {
295     GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
296     GMX_RELEASE_ASSERT(impl_->currentFrame_ != NULL,
297                        "finishFrame() called without calling startFrame()");
298     AnalysisDataStorageFrame *frame = impl_->currentFrame_;
299     impl_->currentFrame_ = NULL;
300     frame->finishFrame();
301 }
302
303
304 void
305 AnalysisDataHandle::finishData()
306 {
307     GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
308     // Deletes the implementation pointer.
309     impl_->data_.finishData(*this);
310     impl_ = NULL;
311 }
312
313 } // namespace gmx