2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * Implements gmx::AnalysisDataDisplacementModule.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_analysisdata
42 #include "gromacs/analysisdata/modules/displacement.h"
44 #include "gromacs/analysisdata/dataframe.h"
45 #include "gromacs/analysisdata/datamodulemanager.h"
46 #include "gromacs/analysisdata/modules/histogram.h"
47 #include "gromacs/math/utilities.h"
48 #include "gromacs/utility/exceptions.h"
49 #include "gromacs/utility/gmxassert.h"
50 #include "gromacs/utility/smalloc.h"
55 /********************************************************************
56 * AnalysisDataDisplacementModule::Impl
60 * Private implementation class for AnalysisDataDisplacementModule.
62 * \ingroup module_analysisdata
64 class AnalysisDataDisplacementModule::Impl
70 //! Maximum number of particles for which the displacements are calculated.
72 //! Maximum time for which the displacements are needed.
74 //! Number of dimensions per data point.
77 //! true if no frames have been read.
79 //! Stores the time of the first frame.
81 //! Stores the time interval between frames.
83 //! Stores the time of the current frame.
85 //! Stores the index in the store for the current positions.
88 //! Maximum number of positions to store for a particle.
90 //! The total number of positions ever stored (can be larger than \p max_store).
94 //! The most recently calculated displacements.
95 std::vector<AnalysisDataValue> currValues_;
97 //! Histogram module for calculating MSD histograms, or NULL if not set.
98 AnalysisDataBinAverageModule *histm;
101 AnalysisDataDisplacementModule::Impl::Impl()
102 : nmax(0), tmax(0.0), ndim(3),
103 bFirst(true), t0(0.0), dt(0.0), t(0.0), ci(0),
104 max_store(-1), nstored(0), oldval(NULL),
109 AnalysisDataDisplacementModule::Impl::~Impl()
114 /********************************************************************
115 * AnalysisDataDisplacementModule
118 AnalysisDataDisplacementModule::AnalysisDataDisplacementModule()
125 AnalysisDataDisplacementModule::~AnalysisDataDisplacementModule()
131 AnalysisDataDisplacementModule::setMaxTime(real tmax)
138 AnalysisDataDisplacementModule::setMSDHistogram(
139 AnalysisDataBinAverageModulePointer histm)
141 GMX_RELEASE_ASSERT(_impl->histm == NULL, "Can only set MSD histogram once");
142 _impl->histm = histm.get();
148 AnalysisDataDisplacementModule::tryGetDataFrameInternal(int /*index*/) const
150 return AnalysisDataFrameRef();
155 AnalysisDataDisplacementModule::requestStorageInternal(int /*nframes*/)
162 AnalysisDataDisplacementModule::flags() const
164 return efAllowMulticolumn;
169 AnalysisDataDisplacementModule::dataStarted(AbstractAnalysisData *data)
171 if (data->columnCount() % _impl->ndim != 0)
173 GMX_THROW(APIError("Data has incorrect number of columns"));
175 _impl->nmax = data->columnCount();
176 snew(_impl->oldval, _impl->nmax);
177 _impl->ci = -_impl->nmax;
179 int ncol = _impl->nmax / _impl->ndim + 1;
180 _impl->currValues_.reserve(ncol);
181 setColumnCount(0, ncol);
186 AnalysisDataDisplacementModule::frameStarted(const AnalysisDataFrameHeader &header)
191 _impl->t0 = header.x();
193 else if (_impl->dt <= 0)
195 _impl->dt = header.x() - _impl->t0;
196 if (_impl->dt < 0 || gmx_within_tol(_impl->dt, 0.0, GMX_REAL_EPS))
198 GMX_THROW(APIError("Identical or decreasing frame times"));
203 if (!gmx_within_tol(header.x() - _impl->t, _impl->dt, GMX_REAL_EPS))
205 GMX_THROW(APIError("Frames not evenly spaced"));
208 _impl->t = header.x();
210 // Allocate memory for all the positions once it is possible.
211 if (_impl->max_store == -1 && !_impl->bFirst)
213 _impl->max_store = _impl->nmax * (int)(_impl->tmax/_impl->dt + 1);
214 srenew(_impl->oldval, _impl->max_store);
217 // Increment the index where current positions are stored.
218 _impl->ci += _impl->nmax;
219 if (_impl->ci >= _impl->max_store)
225 for (int i = 0; i < _impl->nmax; ++i)
227 _impl->p[_impl->ci + i].bPres = false;
231 _impl->bFirst = false;
236 AnalysisDataDisplacementModule::pointsAdded(const AnalysisDataPointSetRef &points)
238 if (points.firstColumn() % _impl->ndim != 0
239 || points.columnCount() % _impl->ndim != 0)
241 GMX_THROW(APIError("Partial data points"));
243 for (int i = 0; i < points.columnCount(); ++i)
245 _impl->oldval[_impl->ci + points.firstColumn() + i] = points.y(i);
251 AnalysisDataDisplacementModule::frameFinished(const AnalysisDataFrameHeader & /*header*/)
253 if (_impl->nstored <= 1)
260 if (_impl->nstored == 2)
264 _impl->histm->init(histogramFromBins(0, _impl->max_store / _impl->nmax,
265 _impl->dt).integerBins());
267 moduleManager().notifyDataStart(this);
269 AnalysisDataFrameHeader header(_impl->nstored - 2, _impl->t, 0);
270 moduleManager().notifyFrameStart(header);
272 for (i = _impl->ci - _impl->nmax, step = 1;
273 step < _impl->nstored && i != _impl->ci;
274 i -= _impl->nmax, ++step)
278 i += _impl->max_store;
280 _impl->currValues_.clear();
281 _impl->currValues_.push_back(AnalysisDataValue(step * _impl->dt));
283 for (int j = 0; j < _impl->nmax; j += _impl->ndim, ++k)
287 for (int d = 0; d < _impl->ndim; ++d)
289 real displ = _impl->oldval[_impl->ci + j + d]
290 - _impl->oldval[i + j + d];
291 dist2 += displ * displ;
293 _impl->currValues_.push_back(AnalysisDataValue(dist2));
295 moduleManager().notifyPointsAdd(AnalysisDataPointSetRef(header, _impl->currValues_));
298 moduleManager().notifyFrameFinish(header);
303 AnalysisDataDisplacementModule::dataFinished()
305 if (_impl->nstored >= 2)
307 moduleManager().notifyDataFinish();