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