OpenMPCD
NormalModeAutocorrelation.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Defines the
4  * `OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation`
5  * class.
6  */
7 
8 #ifndef OPENMPCD_CUDA_MPCFLUID_INSTRUMENTATION_NORMALMODEAUTOCORRELATION_HPP
9 #define OPENMPCD_CUDA_MPCFLUID_INSTRUMENTATION_NORMALMODEAUTOCORRELATION_HPP
10 
11 
14 #include <OpenMPCD/Types.hpp>
15 
16 #include <ostream>
17 #include <vector>
18 
19 namespace OpenMPCD
20 {
21 namespace CUDA
22 {
23 namespace MPCFluid
24 {
25 
26 class Base;
27 
28 namespace Instrumentation
29 {
30 
31 /**
32  * Measures the autocorrelation of normal coordinates in linear polymers.
33  *
34  * This class assumes that the MPC fluid it measures consists of linear polymer
35  * chains. It can only be used if
36  * `OpenMPCD::CUDA::MPCFluid::Base::numberOfParticlesPerLogicalEntityIsConstant`
37  * is true.
38  *
39  * Let \f$ N_C \f$ correspond to the number of polymer chains in the fluid,
40  * as returned by
41  * `OpenMPCD::CUDA::MPCFluid::Base::getNumberOfLogicalEntities`,
42  * and let the number of particles in each chain be \f$ N \f$, as returned by
43  * `OpenMPCD::CUDA::MPCFluid::Base::getNumberOfParticlesPerLogicalEntity`.
44  * Then, let \f$ \vec{q}_i^j \left( t \right) \f$ be the normal mode coordinate
45  * of normal mode \f$ i \f$ of the chain \f$ j \f$ at simulation time \f$ t \f$,
46  * as defined in `OpenMPCD::NormalMode`, with shift parameter \f$ S \f$.
47  *
48  * Then, this class samples, for all modes \f$ i \f$ and for configurable
49  * correlation times \f$ \Delta t \f$, the quantity
50  * \f[
51  * C \left( t, t + \Delta t, i \right)
52  * =
53  * \frac{1}{N_C}
54  * \sum_{j=1}^{N_C}
55  * \vec{q}_i^j \left( t + \Delta t \right)
56  * \cdot
57  * \vec{q}_i^j \left( t \right)
58  * \f]
59  * via `OpenMPCD::CUDA::NormalMode::getAverageNormalCoordinateAutocorrelation`.
60  *
61  * In order to avoid floating-point arithmetic in specifying times, and in order
62  * to decouple this class from the `CUDA::Simulation` class, time is measured
63  * in this class as the number of times `measure` has completed, not counting
64  * those calls that have no effect because of the `measureEveryNthSweep`
65  * configuration option (see below).
66  * As an example, take `measureEveryNthSweep` to be `3`. Then, the first
67  * execution of `measure` will perform a measurement, and that point in
68  * simulation time will be referred to as `measurement time 0`. The next two
69  * calls to `measure` will again have no effect, while the following (i.e. the
70  * fourth) call will be at `measurement time 1`. The fifth and sixth calls
71  * will again have no effect, and so forth.
72  *
73  * This class is configured via the `instrumentation.normalModeAutocorrelation`
74  * configuration group. Within that group, `measureEveryNthSweep` defines
75  * \f$ \tau \f$, and `autocorrelationArgumentCount` defines \f$ N_A \f$; given
76  * those, the autocorrelation is measured at (simulation) correlation times
77  * \f[
78  * \Delta t \in
79  * \left\{
80  * 0, \tau \Delta T, 2 \tau \Delta T,
81  * \ldots, \left( N_A - 1 \right) \tau \Delta T
82  * \right\}
83  * \f]
84  * where \f$ \Delta T \f$ is the simulation time between consecutive sweeps.
85  * Consequently, in `measurement time`,
86  * \f$ \Delta t \in \left\{ 0, 1, 2, \ldots, N_A - 1 \right\} \f$.
87  *
88  * Furthermore, the key `shift` within the
89  * `instrumentation.normalModeAutocorrelation` configuration group specifies
90  * the shift parameter \f$ S \f$ to be used (see `OpenMPCD::NormalMode` for the
91  * definition of this parameter). If this key is not specified, it defaults to
92  * `0.0`.
93  *
94  * For analysis of the produced results, see
95  * `MPCDAnalysis.NormalModeAutocorrelation`.
96  */
98 {
99 public:
100  /**
101  * The constructor.
102  *
103  * @throw OpenMPCD::NULLPointerException
104  * If `OPENMPCD_DEBUG` is defined, throws if `mpcFluid_ == nullptr`.
105  * @throw OpenMPCD::InvalidConfigurationException
106  * Throws if
107  * `!isValidConfiguration(
108  * configuration.getSetting(
109  * "instrumentation.normalModeAutocorrelation"))`.
110  * @throw OpenMPCD::InvalidArgumentException
111  * If `OPENMPCD_DEBUG` is defined, throws if
112  * `!mpcFluid_->numberOfParticlesPerLogicalEntityIsConstant()`.
113  *
114  * @param[in] configuration
115  * The simulation configuration.
116  * @param[in] mpcFluid_
117  * The fluid to measure. Must not be `nullptr`, and it must
118  * return `true` when its
119  * `numberOfParticlesPerLogicalEntityIsConstant` member is
120  * called.
121  */
123  const OpenMPCD::Configuration& configuration,
124  const OpenMPCD::CUDA::MPCFluid::Base* const mpcFluid_);
125 
126 private:
128  ///< The copy constructor.
129 
130 public:
131  /**
132  * The destructor.
133  */
135 
136 public:
137  /**
138  * Returns whether the an attempt has been made to configure this class,
139  * i.e. whether the `instrumentation.normalModeAutocorrelation`
140  * configuration group exists.
141  *
142  * @param[in] config
143  * The configuration to query.
144  */
145  static bool isConfigured(const Configuration& config);
146 
147  /**
148  * Returns whether the given configuration group is a valid configuration.
149  *
150  * For a configuration group to be valid, it must
151  * - contain the key `measureEveryNthSweep`, which must be a positive
152  * integer
153  * - contain the key `autocorrelationArgumentCount`, which must be a
154  * positive integer.
155  *
156  * @param[in] group
157  * The configuration group to query.
158  */
159  static bool isValidConfiguration(const Configuration::Setting& group);
160 
161 
162 public:
163  /**
164  * Takes measurement data.
165  *
166  * This function is to be called by the `OpenMPCD::CUDA::Simulation` instance
167  * after every sweep.
168  */
169  void measure();
170 
171  /**
172  * Returns, in units of `measurement time`, the maximum correlation time
173  * that is configured to be measured, i.e. \f$ N_A - 1 \f$.
174  */
175  unsigned int getMaximumCorrelationTime() const;
176 
177  /**
178  * Returns `1` plus the maximum number `t` may take in `getAutocorrelation`.
179  */
180  unsigned int getMeasurementCount() const;
181 
182  /**
183  * Returns the measured value of the autocorrelation at measurement times
184  * \f$ t \f$ and \f$ T \f$, and for normal mode index \f$ i \f$.
185  *
186  * @note
187  * The times are given in units of the `measurement time`, which is
188  * described in the documentation of this class.
189  *
190  * @throw OpenMPCD::InvalidArgumentException
191  * If `OPENMPCD_DEBUG` is defined, throws if
192  * `t >= getMeasurementCount()`.
193  * @throw OpenMPCD::InvalidArgumentException
194  * If `OPENMPCD_DEBUG` is defined, throws if
195  * `T >= getMeasurementCount()` or `T < t` or
196  * `T - t > getMaximumCorrelationTime()`.
197  * @throw OpenMPCD::InvalidArgumentException
198  * Throws if `normalMode` is out of range.
199  *
200  * @param[in] t
201  * The first measurement time \f$ t \f$. This value must be
202  * smaller than `getMeasurementCount()`.
203  * @param[in] T
204  * The second measurement time \f$ T \f$. This value must be
205  * smaller than `getMeasurementCount()` and larger than or equal
206  * to `t`. Also, `T - t` must not be larger than
207  * `getMaximumCorrelationTime()`.
208  * @param[in] normalMode
209  * The normal mode index \f$ i \f$, which must be less than or
210  * equal to `mpcFluid->getNumberOfParticlesPerLogicalEntity()`,
211  * where `mpcFluid` is the fluid passed to the constructor of
212  * this instance.
213  */
215  const unsigned int t,
216  const unsigned int T,
217  const unsigned int normalMode) const;
218 
219  /**
220  * Saves the result to the given stream.
221  *
222  * For each value `t` in the range `[0, getMeasurementCount())`, and for
223  * each value `T` in the range `[t, t + getMaximumCorrelationTime()]`
224  * (except for those where `T >= getMeasurementCount()`),
225  * a line will be written to the output stream, with the following fields,
226  * separated by tab characters:
227  * First, the current value of `t`, followed by the value of `T - t`,
228  * followed by, for each normal mode index `n`, the value of
229  * `getAutocorrelation(t, T, n)`.
230  *
231  * The numeric values will be written with precision
232  * <c>std::numeric_limits<OpenMPCD::FP>::digits10 + 2</c>.
233  *
234  * @param[out] stream
235  * The stream to write to.
236  */
237  void save(std::ostream& stream);
238 
239  /**
240  * Saves the result to the given run directory.
241  *
242  * The file within the given directory will be named
243  * `normalModeAutocorrelations.data`, and will be created if it does not
244  * exist, or truncated if it does. Parent directories will be created as
245  * needed.
246  * The file's contents will correspond to the output of
247  * `save(std::ostream&)`.
248  *
249  * @param[in] rundir
250  * Path to the directory, into which the result file will be
251  * written.
252  */
253  void save(const std::string& rundir);
254 
255 private:
256  const NormalModeAutocorrelation& operator=(
258  ///< The assignment operator.
259 
260 private:
261  const OpenMPCD::CUDA::MPCFluid::Base* const mpcFluid;
262  ///< The fluid measured.
263 
264  unsigned int measureEveryNthSweep;
265  ///< Specifies the snapshot interval.
266  unsigned int autocorrelationArgumentCount;
267  ///< Specifies the snapshot count.
268 
269 
270  unsigned int sweepsSinceLastMeasurement;
271  ///< Counts the sweeps since the last measurement.
272 
273  std::vector<DeviceBuffer<MPCParticlePositionType>*> snapshots;
274  ///< The stored normal mode coordinate snapshots.
275 
276  std::vector<std::vector<std::vector<MPCParticlePositionType> > >
277  autocorrelations;
278  /**< The measured autocorrelations. The first index specifies the later
279  measurement time, the second index the difference between later
280  and prior measurement time, and the third the normal mode index.*/
281 
283  ///< The normal mode shift parameter \f$ S \f$.
284 
285 }; //class NormalModeAutocorrelation
286 } //namespace Instrumentation
287 } //namespace MPCFluid
288 } //namespace CUDA
289 } //namespace OpenMPCD
290 
291 #endif //OPENMPCD_CUDA_MPCFLUID_INSTRUMENTATION_NORMALMODEAUTOCORRELATION_HPP
OpenMPCD::CUDA::MPCFluid::Base
Base class for MPC fluids.
Definition: CUDA/MPCFluid/Base.hpp:40
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::getAutocorrelation
MPCParticlePositionType getAutocorrelation(const unsigned int t, const unsigned int T, const unsigned int normalMode) const
Returns the measured value of the autocorrelation at measurement times and , and for normal mode ind...
Definition: NormalModeAutocorrelation.cpp:178
OpenMPCD::Configuration
Represents the configuration of the simulation.
Definition: Configuration.hpp:28
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::~NormalModeAutocorrelation
~NormalModeAutocorrelation()
The destructor.
Definition: NormalModeAutocorrelation.cpp:62
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::NormalModeAutocorrelation
NormalModeAutocorrelation(const OpenMPCD::Configuration &configuration, const OpenMPCD::CUDA::MPCFluid::Base *const mpcFluid_)
The constructor.
Definition: NormalModeAutocorrelation.cpp:23
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::measure
void measure()
Takes measurement data.
Definition: NormalModeAutocorrelation.cpp:106
OpenMPCD::MPCParticlePositionType
FP MPCParticlePositionType
The data type for the positions of MPC particles.
Definition: Types.hpp:15
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::getMaximumCorrelationTime
unsigned int getMaximumCorrelationTime() const
Returns, in units of measurement time, the maximum correlation time that is configured to be measured...
Definition: NormalModeAutocorrelation.cpp:171
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation
Measures the autocorrelation of normal coordinates in linear polymers.
Definition: NormalModeAutocorrelation.hpp:97
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::getMeasurementCount
unsigned int getMeasurementCount() const
Returns 1 plus the maximum number t may take in getAutocorrelation.
Definition: NormalModeAutocorrelation.cpp:166
DeviceBuffer.hpp
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::isConfigured
static bool isConfigured(const Configuration &config)
Returns whether the an attempt has been made to configure this class, i.e.
Definition: NormalModeAutocorrelation.cpp:68
Types.hpp
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::save
void save(std::ostream &stream)
Saves the result to the given stream.
Definition: NormalModeAutocorrelation.cpp:201
OpenMPCD::Configuration::Setting
Represents a setting in the configuration.
Definition: Configuration.hpp:36
Configuration.hpp
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::isValidConfiguration
static bool isValidConfiguration(const Configuration::Setting &group)
Returns whether the given configuration group is a valid configuration.
Definition: NormalModeAutocorrelation.cpp:73