Update instructions in containers.rst
[gromacs.git] / src / gromacs / analysisdata / analysisdata.cpp
blob5fe4e5fa7b4cb2be77f2f60d39eb999334c402a4
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
36 /*! \internal \file
37 * \brief
38 * Implements classes in analysisdata.h.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_analysisdata
43 #include "gmxpre.h"
45 #include "analysisdata.h"
47 #include <memory>
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"
55 namespace gmx
58 /********************************************************************
59 * AnalysisDataHandleImpl
62 namespace internal
65 /*! \internal \brief
66 * Private implementation class for AnalysisDataHandle.
68 * \ingroup module_analysisdata
70 class AnalysisDataHandleImpl
72 public:
73 //! Creates a handle associated with the given data object.
74 explicit AnalysisDataHandleImpl(AnalysisData* data) : data_(*data), currentFrame_(nullptr) {}
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_;
82 } // namespace internal
84 /********************************************************************
85 * AnalysisData::Impl
88 /*! \internal \brief
89 * Private implementation class for AnalysisData.
91 * \ingroup module_analysisdata
93 class AnalysisData::Impl
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;
101 //! Storage implementation.
102 AnalysisDataStorage storage_;
103 /*! \brief
104 * List of handles for this data object.
106 * Note that AnalysisDataHandle objects also contain (raw) pointers
107 * to these objects.
109 HandleList handles_;
112 /********************************************************************
113 * AnalysisData
116 AnalysisData::AnalysisData() : impl_(new Impl) {}
119 AnalysisData::~AnalysisData() {}
122 void AnalysisData::setDataSetCount(int dataSetCount)
124 GMX_RELEASE_ASSERT(impl_->handles_.empty(),
125 "Cannot change data dimensionality after creating handles");
126 AbstractAnalysisData::setDataSetCount(dataSetCount);
130 void AnalysisData::setColumnCount(int dataSet, int columnCount)
132 GMX_RELEASE_ASSERT(impl_->handles_.empty(),
133 "Cannot change data dimensionality after creating handles");
134 AbstractAnalysisData::setColumnCount(dataSet, columnCount);
138 void AnalysisData::setMultipoint(bool bMultipoint)
140 GMX_RELEASE_ASSERT(impl_->handles_.empty(), "Cannot change data type after creating handles");
141 AbstractAnalysisData::setMultipoint(bMultipoint);
145 int AnalysisData::frameCount() const
147 return impl_->storage_.frameCount();
151 AnalysisDataHandle AnalysisData::startData(const AnalysisDataParallelOptions& opt)
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())
157 impl_->storage_.startParallelDataStorage(this, &moduleManager(), opt);
160 Impl::HandlePointer handle(new internal::AnalysisDataHandleImpl(this));
161 impl_->handles_.push_back(std::move(handle));
162 return AnalysisDataHandle(impl_->handles_.back().get());
166 void AnalysisData::finishFrameSerial(int frameIndex)
168 impl_->storage_.finishFrameSerial(frameIndex);
172 void AnalysisData::finishData(AnalysisDataHandle handle)
174 Impl::HandleList::iterator i;
176 for (i = impl_->handles_.begin(); i != impl_->handles_.end(); ++i)
178 if (i->get() == handle.impl_)
180 break;
183 GMX_RELEASE_ASSERT(i != impl_->handles_.end(), "finishData() called for an unknown handle");
185 impl_->handles_.erase(i);
187 if (impl_->handles_.empty())
189 impl_->storage_.finishDataStorage();
194 AnalysisDataFrameRef AnalysisData::tryGetDataFrameInternal(int index) const
196 return impl_->storage_.tryGetDataFrame(index);
200 bool AnalysisData::requestStorageInternal(int nframes)
202 return impl_->storage_.requestStorage(nframes);
206 /********************************************************************
207 * AnalysisDataHandle
210 AnalysisDataHandle::AnalysisDataHandle() : impl_(nullptr) {}
213 AnalysisDataHandle::AnalysisDataHandle(internal::AnalysisDataHandleImpl* impl) : impl_(impl) {}
216 void AnalysisDataHandle::startFrame(int index, real x, real dx)
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);
225 void AnalysisDataHandle::selectDataSet(int index)
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);
234 void AnalysisDataHandle::setPoint(int column, real value, bool bPresent)
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);
243 void AnalysisDataHandle::setPoint(int column, real value, real error, bool bPresent)
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);
252 void AnalysisDataHandle::setPoints(int firstColumn, int count, const real* values, bool bPresent)
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)
259 impl_->currentFrame_->setValue(firstColumn + i, values[i], bPresent);
264 void AnalysisDataHandle::finishPointSet()
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();
275 void AnalysisDataHandle::finishFrame()
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();
286 void AnalysisDataHandle::finishData()
288 GMX_RELEASE_ASSERT(impl_ != nullptr, "Invalid data handle used");
289 // Deletes the implementation pointer.
290 impl_->data_.finishData(*this);
291 impl_ = nullptr;
294 } // namespace gmx