3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements gmx::AbstractAnalysisData.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_analysisdata
38 #include "gromacs/analysisdata/abstractdata.h"
40 #include "gromacs/fatalerror/exceptions.h"
41 #include "gromacs/fatalerror/gmxassert.h"
43 #include "abstractdata-impl.h"
44 #include "dataframe.h"
45 #include "dataproxy.h"
50 /********************************************************************
51 * AbstractAnalysisData::Impl
54 AbstractAnalysisData::Impl::Impl()
55 : _bAllowMissing(true), _bDataStart(false), _bInData(false), _bInFrame(false),
56 _currIndex(-1), _nframes(0)
60 AbstractAnalysisData::Impl::~Impl()
66 AbstractAnalysisData::Impl::presentData(AbstractAnalysisData *data,
67 AnalysisDataModuleInterface *module)
69 module->dataStarted(data);
70 bool bCheckMissing = _bAllowMissing
71 && !(module->flags() & AnalysisDataModuleInterface::efAllowMissing);
72 int ncol = data->columnCount();
73 for (int i = 0; i < data->frameCount(); ++i)
75 AnalysisDataFrameRef frame = data->getDataFrame(i);
76 GMX_RELEASE_ASSERT(frame.isValid(), "Invalid data frame returned");
77 if (bCheckMissing && !frame.allPresent())
79 GMX_THROW(APIError("Missing data not supported by a module"));
81 module->frameStarted(frame.header());
82 module->pointsAdded(frame.points());
83 module->frameFinished(frame.header());
87 module->dataFinished();
92 /********************************************************************
93 * AbstractAnalysisData
96 AbstractAnalysisData::AbstractAnalysisData()
97 : _impl(new Impl()), _ncol(0), _bMultiPoint(false)
102 AbstractAnalysisData::~AbstractAnalysisData()
108 AbstractAnalysisData::frameCount() const
110 return _impl->_nframes;
115 AbstractAnalysisData::tryGetDataFrame(int index) const
117 if (index < 0 || index >= frameCount())
119 return AnalysisDataFrameRef();
121 return tryGetDataFrameInternal(index);
126 AbstractAnalysisData::getDataFrame(int index) const
128 AnalysisDataFrameRef frame = tryGetDataFrame(index);
129 if (!frame.isValid())
131 GMX_THROW(APIError("Invalid frame accessed"));
138 AbstractAnalysisData::requestStorage(int nframes)
140 GMX_RELEASE_ASSERT(nframes >= -1, "Invalid number of frames requested");
145 return requestStorageInternal(nframes);
150 AbstractAnalysisData::addModule(AnalysisDataModulePointer module)
152 if ((columnCount() > 1 && !(module->flags() & AnalysisDataModuleInterface::efAllowMulticolumn))
153 || (isMultipoint() && !(module->flags() & AnalysisDataModuleInterface::efAllowMultipoint))
154 || (!isMultipoint() && (module->flags() & AnalysisDataModuleInterface::efOnlyMultipoint)))
156 GMX_THROW(APIError("Data module not compatible with data object properties"));
159 if (_impl->_bDataStart)
161 GMX_RELEASE_ASSERT(!_impl->_bInFrame,
162 "Cannot add data modules in mid-frame");
163 _impl->presentData(this, module.get());
165 if (!(module->flags() & AnalysisDataModuleInterface::efAllowMissing))
167 _impl->_bAllowMissing = false;
169 _impl->_modules.push_back(move(module));
174 AbstractAnalysisData::addColumnModule(int col, int span,
175 AnalysisDataModulePointer module)
177 GMX_RELEASE_ASSERT(col >= 0 && span >= 1 && col + span <= _ncol,
178 "Invalid columns specified for a column module");
179 if (_impl->_bDataStart)
181 GMX_THROW(NotImplementedError("Cannot add column modules after data"));
184 boost::shared_ptr<AnalysisDataProxy> proxy(
185 new AnalysisDataProxy(col, span, this));
186 proxy->addModule(module);
192 AbstractAnalysisData::applyModule(AnalysisDataModuleInterface *module)
194 if ((columnCount() > 1 && !(module->flags() & AnalysisDataModuleInterface::efAllowMulticolumn))
195 || (isMultipoint() && !(module->flags() & AnalysisDataModuleInterface::efAllowMultipoint))
196 || (!isMultipoint() && (module->flags() & AnalysisDataModuleInterface::efOnlyMultipoint)))
198 GMX_THROW(APIError("Data module not compatible with data object properties"));
200 GMX_RELEASE_ASSERT(_impl->_bDataStart && !_impl->_bInData,
201 "Data module can only be applied to ready data");
203 _impl->presentData(this, module);
208 AbstractAnalysisData::setColumnCount(int ncol)
210 GMX_RELEASE_ASSERT(ncol > 0, "Invalid data column count");
211 GMX_RELEASE_ASSERT(_ncol == 0 || _impl->_modules.empty(),
212 "Data column count cannot be changed after modules are added");
213 GMX_RELEASE_ASSERT(!_impl->_bDataStart,
214 "Data column count cannot be changed after data has been added");
220 AbstractAnalysisData::setMultipoint(bool multipoint)
222 GMX_RELEASE_ASSERT(_impl->_modules.empty(),
223 "Data type cannot be changed after modules are added");
224 GMX_RELEASE_ASSERT(!_impl->_bDataStart,
225 "Data type cannot be changed after data has been added");
226 _bMultiPoint = multipoint;
231 * This method is not const because the dataStarted() methods of the attached
232 * modules can request storage of the data.
235 AbstractAnalysisData::notifyDataStart()
237 GMX_RELEASE_ASSERT(!_impl->_bDataStart,
238 "notifyDataStart() called more than once");
239 GMX_RELEASE_ASSERT(_ncol > 0, "Data column count is not set");
240 _impl->_bDataStart = _impl->_bInData = true;
242 Impl::ModuleList::const_iterator i;
243 for (i = _impl->_modules.begin(); i != _impl->_modules.end(); ++i)
245 if (_ncol > 1 && !((*i)->flags() & AnalysisDataModuleInterface::efAllowMulticolumn))
247 GMX_THROW(APIError("Data module not compatible with data object properties"));
249 (*i)->dataStarted(this);
255 AbstractAnalysisData::notifyFrameStart(const AnalysisDataFrameHeader &header) const
257 GMX_ASSERT(_impl->_bInData, "notifyDataStart() not called");
258 GMX_ASSERT(!_impl->_bInFrame,
259 "notifyFrameStart() called while inside a frame");
260 GMX_ASSERT(header.index() == _impl->_nframes,
261 "Out of order frames");
262 _impl->_bInFrame = true;
263 _impl->_currIndex = header.index();
265 Impl::ModuleList::const_iterator i;
266 for (i = _impl->_modules.begin(); i != _impl->_modules.end(); ++i)
268 (*i)->frameStarted(header);
274 AbstractAnalysisData::notifyPointsAdd(const AnalysisDataPointSetRef &points) const
276 GMX_ASSERT(_impl->_bInData, "notifyDataStart() not called");
277 GMX_ASSERT(_impl->_bInFrame, "notifyFrameStart() not called");
278 GMX_ASSERT(points.lastColumn() < columnCount(), "Invalid columns");
279 GMX_ASSERT(points.frameIndex() == _impl->_currIndex,
280 "Points do not correspond to current frame");
281 if (!_impl->_bAllowMissing && !points.allPresent())
283 GMX_THROW(APIError("Missing data not supported by a module"));
286 Impl::ModuleList::const_iterator i;
287 for (i = _impl->_modules.begin(); i != _impl->_modules.end(); ++i)
289 (*i)->pointsAdded(points);
295 AbstractAnalysisData::notifyFrameFinish(const AnalysisDataFrameHeader &header)
297 GMX_ASSERT(_impl->_bInData, "notifyDataStart() not called");
298 GMX_ASSERT(_impl->_bInFrame, "notifyFrameStart() not called");
299 GMX_ASSERT(header.index() == _impl->_currIndex,
300 "Header does not correspond to current frame");
301 _impl->_bInFrame = false;
302 _impl->_currIndex = -1;
304 // Increment the counter before notifications to allow frame access from
308 Impl::ModuleList::const_iterator i;
309 for (i = _impl->_modules.begin(); i != _impl->_modules.end(); ++i)
311 (*i)->frameFinished(header);
317 AbstractAnalysisData::notifyDataFinish() const
319 GMX_RELEASE_ASSERT(_impl->_bInData, "notifyDataStart() not called");
320 GMX_RELEASE_ASSERT(!_impl->_bInFrame,
321 "notifyDataFinish() called while inside a frame");
322 _impl->_bInData = false;
324 Impl::ModuleList::const_iterator i;
325 for (i = _impl->_modules.begin(); i != _impl->_modules.end(); ++i)
327 (*i)->dataFinished();