2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010-2018, The GROMACS development team.
5 * Copyright (c) 2019,2021, 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.
38 * Implements gmx::AnalysisDataDisplacementModule.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_analysisdata
45 #include "displacement.h"
47 #include "gromacs/analysisdata/dataframe.h"
48 #include "gromacs/analysisdata/datamodulemanager.h"
49 #include "gromacs/analysisdata/modules/histogram.h"
50 #include "gromacs/math/utilities.h"
51 #include "gromacs/utility/exceptions.h"
52 #include "gromacs/utility/gmxassert.h"
53 #include "gromacs/utility/smalloc.h"
58 /********************************************************************
59 * AnalysisDataDisplacementModule::Impl
63 * Private implementation class for AnalysisDataDisplacementModule.
65 * \ingroup module_analysisdata
67 class AnalysisDataDisplacementModule::Impl
73 //! Maximum number of particles for which the displacements are calculated.
75 //! Maximum time for which the displacements are needed.
77 //! Number of dimensions per data point.
80 //! true if no frames have been read.
82 //! Stores the time of the first frame.
84 //! Stores the time interval between frames.
86 //! Stores the time of the current frame.
88 //! Stores the index in the store for the current positions.
91 //! Maximum number of positions to store for a particle.
93 //! The total number of positions ever stored (can be larger than \p max_store).
97 //! The most recently calculated displacements.
98 std::vector<AnalysisDataValue> currValues_;
100 //! Histogram module for calculating MSD histograms, or NULL if not set.
101 AnalysisDataBinAverageModule* histm;
104 AnalysisDataDisplacementModule::Impl::Impl() :
120 AnalysisDataDisplacementModule::Impl::~Impl()
125 /********************************************************************
126 * AnalysisDataDisplacementModule
129 AnalysisDataDisplacementModule::AnalysisDataDisplacementModule() : _impl(new Impl())
135 AnalysisDataDisplacementModule::~AnalysisDataDisplacementModule() {}
138 void AnalysisDataDisplacementModule::setMaxTime(real tmax)
144 void AnalysisDataDisplacementModule::setMSDHistogram(const AnalysisDataBinAverageModulePointer& histm)
146 GMX_RELEASE_ASSERT(_impl->histm == nullptr, "Can only set MSD histogram once");
147 _impl->histm = histm.get();
152 AnalysisDataFrameRef AnalysisDataDisplacementModule::tryGetDataFrameInternal(int /*index*/) const
154 return AnalysisDataFrameRef();
158 bool AnalysisDataDisplacementModule::requestStorageInternal(int /*nframes*/)
164 int AnalysisDataDisplacementModule::flags() const
166 return efAllowMulticolumn;
170 void AnalysisDataDisplacementModule::dataStarted(AbstractAnalysisData* data)
172 if (data->columnCount() % _impl->ndim != 0)
174 GMX_THROW(APIError("Data has incorrect number of columns"));
176 _impl->nmax = data->columnCount();
177 snew(_impl->oldval, _impl->nmax);
178 _impl->ci = -_impl->nmax;
180 int ncol = _impl->nmax / _impl->ndim + 1;
181 _impl->currValues_.reserve(ncol);
182 setColumnCount(0, ncol);
186 void 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 * static_cast<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;
235 void AnalysisDataDisplacementModule::pointsAdded(const AnalysisDataPointSetRef& points)
237 if (points.firstColumn() % _impl->ndim != 0 || points.columnCount() % _impl->ndim != 0)
239 GMX_THROW(APIError("Partial data points"));
241 for (int i = 0; i < points.columnCount(); ++i)
243 _impl->oldval[_impl->ci + points.firstColumn() + i] = points.y(i);
248 void AnalysisDataDisplacementModule::frameFinished(const AnalysisDataFrameHeader& /*header*/)
250 if (_impl->nstored <= 1)
255 if (_impl->nstored == 2)
260 histogramFromBins(0, _impl->max_store / _impl->nmax, _impl->dt).integerBins());
262 moduleManager().notifyDataStart(this);
264 AnalysisDataFrameHeader header(_impl->nstored - 2, _impl->t, 0);
265 moduleManager().notifyFrameStart(header);
267 for (int i = _impl->ci - _impl->nmax, step = 1; step < _impl->nstored && i != _impl->ci;
268 i -= _impl->nmax, ++step)
272 i += _impl->max_store;
274 _impl->currValues_.clear();
275 _impl->currValues_.emplace_back(step * _impl->dt);
277 for (int j = 0; j < _impl->nmax; j += _impl->ndim, ++k)
281 for (int d = 0; d < _impl->ndim; ++d)
283 real displ = _impl->oldval[_impl->ci + j + d] - _impl->oldval[i + j + d];
284 dist2 += displ * displ;
286 _impl->currValues_.emplace_back(dist2);
288 moduleManager().notifyPointsAdd(AnalysisDataPointSetRef(header, _impl->currValues_));
291 moduleManager().notifyFrameFinish(header);
295 void AnalysisDataDisplacementModule::dataFinished()
297 if (_impl->nstored >= 2)
299 moduleManager().notifyDataFinish();