Apply clang-format to source tree
[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-2017, The GROMACS development team.
5  * Copyright (c) 2019, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \internal \file
37  * \brief
38  * Implements classes in analysisdata.h.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_analysisdata
42  */
43 #include "gmxpre.h"
44
45 #include "analysisdata.h"
46
47 #include <memory>
48
49 #include "gromacs/analysisdata/dataframe.h"
50 #include "gromacs/analysisdata/datastorage.h"
51 #include "gromacs/analysisdata/paralleloptions.h"
52 #include "gromacs/utility/exceptions.h"
53 #include "gromacs/utility/gmxassert.h"
54
55 namespace gmx
56 {
57
58 /********************************************************************
59  * AnalysisDataHandleImpl
60  */
61
62 namespace internal
63 {
64
65 /*! \internal \brief
66  * Private implementation class for AnalysisDataHandle.
67  *
68  * \ingroup module_analysisdata
69  */
70 class AnalysisDataHandleImpl
71 {
72 public:
73     //! Creates a handle associated with the given data object.
74     explicit AnalysisDataHandleImpl(AnalysisData* data) : data_(*data), currentFrame_(nullptr) {}
75
76     //! The data object that this handle belongs to.
77     AnalysisData& data_;
78     //! Current storage frame object, or NULL if no current frame.
79     AnalysisDataStorageFrame* currentFrame_;
80 };
81
82 } // namespace internal
83
84 /********************************************************************
85  * AnalysisData::Impl
86  */
87
88 /*! \internal \brief
89  * Private implementation class for AnalysisData.
90  *
91  * \ingroup module_analysisdata
92  */
93 class AnalysisData::Impl
94 {
95 public:
96     //! Smart pointer type to manage a data handle implementation.
97     typedef std::unique_ptr<internal::AnalysisDataHandleImpl> 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() : impl_(new Impl) {}
117
118
119 AnalysisData::~AnalysisData() {}
120
121
122 void AnalysisData::setDataSetCount(int dataSetCount)
123 {
124     GMX_RELEASE_ASSERT(impl_->handles_.empty(),
125                        "Cannot change data dimensionality after creating handles");
126     AbstractAnalysisData::setDataSetCount(dataSetCount);
127 }
128
129
130 void AnalysisData::setColumnCount(int dataSet, int columnCount)
131 {
132     GMX_RELEASE_ASSERT(impl_->handles_.empty(),
133                        "Cannot change data dimensionality after creating handles");
134     AbstractAnalysisData::setColumnCount(dataSet, columnCount);
135 }
136
137
138 void AnalysisData::setMultipoint(bool bMultipoint)
139 {
140     GMX_RELEASE_ASSERT(impl_->handles_.empty(), "Cannot change data type after creating handles");
141     AbstractAnalysisData::setMultipoint(bMultipoint);
142 }
143
144
145 int AnalysisData::frameCount() const
146 {
147     return impl_->storage_.frameCount();
148 }
149
150
151 AnalysisDataHandle AnalysisData::startData(const AnalysisDataParallelOptions& opt)
152 {
153     GMX_RELEASE_ASSERT(impl_->handles_.size() < static_cast<unsigned>(opt.parallelizationFactor()),
154                        "Too many calls to startData() compared to provided options");
155     if (impl_->handles_.empty())
156     {
157         impl_->storage_.startParallelDataStorage(this, &moduleManager(), opt);
158     }
159
160     Impl::HandlePointer handle(new internal::AnalysisDataHandleImpl(this));
161     impl_->handles_.push_back(std::move(handle));
162     return AnalysisDataHandle(impl_->handles_.back().get());
163 }
164
165
166 void AnalysisData::finishFrameSerial(int frameIndex)
167 {
168     impl_->storage_.finishFrameSerial(frameIndex);
169 }
170
171
172 void AnalysisData::finishData(AnalysisDataHandle handle)
173 {
174     Impl::HandleList::iterator i;
175
176     for (i = impl_->handles_.begin(); i != impl_->handles_.end(); ++i)
177     {
178         if (i->get() == handle.impl_)
179         {
180             break;
181         }
182     }
183     GMX_RELEASE_ASSERT(i != impl_->handles_.end(), "finishData() called for an unknown handle");
184
185     impl_->handles_.erase(i);
186
187     if (impl_->handles_.empty())
188     {
189         impl_->storage_.finishDataStorage();
190     }
191 }
192
193
194 AnalysisDataFrameRef AnalysisData::tryGetDataFrameInternal(int index) const
195 {
196     return impl_->storage_.tryGetDataFrame(index);
197 }
198
199
200 bool AnalysisData::requestStorageInternal(int nframes)
201 {
202     return impl_->storage_.requestStorage(nframes);
203 }
204
205
206 /********************************************************************
207  * AnalysisDataHandle
208  */
209
210 AnalysisDataHandle::AnalysisDataHandle() : impl_(nullptr) {}
211
212
213 AnalysisDataHandle::AnalysisDataHandle(internal::AnalysisDataHandleImpl* impl) : impl_(impl) {}
214
215
216 void AnalysisDataHandle::startFrame(int index, real x, real dx)
217 {
218     GMX_RELEASE_ASSERT(impl_ != nullptr, "Invalid data handle used");
219     GMX_RELEASE_ASSERT(impl_->currentFrame_ == nullptr,
220                        "startFrame() called twice without calling finishFrame()");
221     impl_->currentFrame_ = &impl_->data_.impl_->storage_.startFrame(index, x, dx);
222 }
223
224
225 void AnalysisDataHandle::selectDataSet(int index)
226 {
227     GMX_RELEASE_ASSERT(impl_ != nullptr, "Invalid data handle used");
228     GMX_RELEASE_ASSERT(impl_->currentFrame_ != nullptr,
229                        "selectDataSet() called without calling startFrame()");
230     impl_->currentFrame_->selectDataSet(index);
231 }
232
233
234 void AnalysisDataHandle::setPoint(int column, real value, bool bPresent)
235 {
236     GMX_RELEASE_ASSERT(impl_ != nullptr, "Invalid data handle used");
237     GMX_RELEASE_ASSERT(impl_->currentFrame_ != nullptr,
238                        "setPoint() called without calling startFrame()");
239     impl_->currentFrame_->setValue(column, value, bPresent);
240 }
241
242
243 void AnalysisDataHandle::setPoint(int column, real value, real error, bool bPresent)
244 {
245     GMX_RELEASE_ASSERT(impl_ != nullptr, "Invalid data handle used");
246     GMX_RELEASE_ASSERT(impl_->currentFrame_ != nullptr,
247                        "setPoint() called without calling startFrame()");
248     impl_->currentFrame_->setValue(column, value, error, bPresent);
249 }
250
251
252 void AnalysisDataHandle::setPoints(int firstColumn, int count, const real* values, bool bPresent)
253 {
254     GMX_RELEASE_ASSERT(impl_ != nullptr, "Invalid data handle used");
255     GMX_RELEASE_ASSERT(impl_->currentFrame_ != nullptr,
256                        "setPoints() called without calling startFrame()");
257     for (int i = 0; i < count; ++i)
258     {
259         impl_->currentFrame_->setValue(firstColumn + i, values[i], bPresent);
260     }
261 }
262
263
264 void AnalysisDataHandle::finishPointSet()
265 {
266     GMX_RELEASE_ASSERT(impl_ != nullptr, "Invalid data handle used");
267     GMX_RELEASE_ASSERT(impl_->data_.isMultipoint(),
268                        "finishPointSet() called for non-multipoint data");
269     GMX_RELEASE_ASSERT(impl_->currentFrame_ != nullptr,
270                        "finishPointSet() called without calling startFrame()");
271     impl_->currentFrame_->finishPointSet();
272 }
273
274
275 void AnalysisDataHandle::finishFrame()
276 {
277     GMX_RELEASE_ASSERT(impl_ != nullptr, "Invalid data handle used");
278     GMX_RELEASE_ASSERT(impl_->currentFrame_ != nullptr,
279                        "finishFrame() called without calling startFrame()");
280     AnalysisDataStorageFrame* frame = impl_->currentFrame_;
281     impl_->currentFrame_            = nullptr;
282     frame->finishFrame();
283 }
284
285
286 void AnalysisDataHandle::finishData()
287 {
288     GMX_RELEASE_ASSERT(impl_ != nullptr, "Invalid data handle used");
289     // Deletes the implementation pointer.
290     impl_->data_.finishData(*this);
291     impl_ = nullptr;
292 }
293
294 } // namespace gmx