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::AnalysisDataDisplacementModule.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_analysisdata
38 #include "gromacs/analysisdata/modules/displacement.h"
47 #include "gromacs/basicmath.h"
48 #include "gromacs/analysisdata/dataframe.h"
49 #include "gromacs/analysisdata/modules/histogram.h"
50 #include "gromacs/fatalerror/exceptions.h"
51 #include "gromacs/fatalerror/gmxassert.h"
53 #include "displacement-impl.h"
58 /********************************************************************
59 * AnalysisDataDisplacementModule::Impl
62 AnalysisDataDisplacementModule::Impl::Impl()
63 : nmax(0), tmax(0.0), ndim(3),
64 bFirst(true), t0(0.0), dt(0.0), t(0.0),
65 max_store(-1), nstored(0), oldval(NULL),
70 AnalysisDataDisplacementModule::Impl::~Impl()
75 /********************************************************************
76 * AnalysisDataDisplacementModule
79 AnalysisDataDisplacementModule::AnalysisDataDisplacementModule()
86 AnalysisDataDisplacementModule::~AnalysisDataDisplacementModule()
92 AnalysisDataDisplacementModule::setMaxTime(real tmax)
99 AnalysisDataDisplacementModule::setMSDHistogram(AnalysisDataBinAverageModule *histm)
101 GMX_RELEASE_ASSERT(!_impl->histm, "Can only set MSD histogram once");
102 _impl->histm = histm;
108 AnalysisDataDisplacementModule::tryGetDataFrameInternal(int /*index*/) const
110 return AnalysisDataFrameRef();
115 AnalysisDataDisplacementModule::requestStorageInternal(int /*nframes*/)
122 AnalysisDataDisplacementModule::flags() const
124 return efAllowMulticolumn;
129 AnalysisDataDisplacementModule::dataStarted(AbstractAnalysisData *data)
131 if (data->columnCount() % _impl->ndim != 0)
133 GMX_THROW(APIError("Data has incorrect number of columns"));
135 _impl->nmax = data->columnCount();
136 snew(_impl->oldval, _impl->nmax);
137 _impl->ci = -_impl->nmax;
139 int ncol = _impl->nmax / _impl->ndim + 1;
140 _impl->currValues_.reserve(ncol);
141 setColumnCount(ncol);
146 AnalysisDataDisplacementModule::frameStarted(const AnalysisDataFrameHeader &header)
151 _impl->t0 = header.x();
153 else if (_impl->dt <= 0)
155 _impl->dt = header.x() - _impl->t0;
156 if (_impl->dt < 0 || gmx_within_tol(_impl->dt, 0.0, GMX_REAL_EPS))
158 GMX_THROW(APIError("Identical or decreasing frame times"));
163 if (!gmx_within_tol(header.x() - _impl->t, _impl->dt, GMX_REAL_EPS))
165 GMX_THROW(APIError("Frames not evenly spaced"));
168 _impl->t = header.x();
170 // Allocate memory for all the positions once it is possible.
171 if (_impl->max_store == -1 && !_impl->bFirst)
173 _impl->max_store = _impl->nmax * (int)(_impl->tmax/_impl->dt + 1);
174 srenew(_impl->oldval, _impl->max_store);
177 // Increment the index where current positions are stored.
178 _impl->ci += _impl->nmax;
179 if (_impl->ci >= _impl->max_store)
185 for (int i = 0; i < _impl->nmax; ++i)
187 _impl->p[_impl->ci + i].bPres = false;
191 _impl->bFirst = false;
196 AnalysisDataDisplacementModule::pointsAdded(const AnalysisDataPointSetRef &points)
198 if (points.firstColumn() % _impl->ndim != 0
199 || points.columnCount() % _impl->ndim != 0)
201 GMX_THROW(APIError("Partial data points"));
203 for (int i = 0; i < points.columnCount(); ++i)
205 _impl->oldval[_impl->ci + points.firstColumn() + i] = points.y(i);
211 AnalysisDataDisplacementModule::frameFinished(const AnalysisDataFrameHeader & /*header*/)
213 if (_impl->nstored <= 1)
221 if (_impl->nstored == 2)
225 _impl->histm->init(histogramFromBins(0, _impl->max_store / _impl->nmax,
226 _impl->dt).integerBins());
230 AnalysisDataFrameHeader header(_impl->nstored - 2, _impl->t, 0);
231 notifyFrameStart(header);
233 for (i = _impl->ci - _impl->nmax, step = 1;
234 step < _impl->nstored && i != _impl->ci;
235 i -= _impl->nmax, ++step)
239 i += _impl->max_store;
241 _impl->currValues_.clear();
242 _impl->currValues_.push_back(AnalysisDataValue(step * _impl->dt));
244 for (int j = 0; j < _impl->nmax; j += _impl->ndim, ++k)
248 for (int d = 0; d < _impl->ndim; ++d)
250 dist2 += sqr(_impl->oldval[_impl->ci + j + d]
251 - _impl->oldval[i + j + d]);
253 _impl->currValues_.push_back(AnalysisDataValue(dist2));
255 notifyPointsAdd(AnalysisDataPointSetRef(header, _impl->currValues_));
258 notifyFrameFinish(header);
263 AnalysisDataDisplacementModule::dataFinished()
265 if (_impl->nstored >= 2)