OpenMPCD
CUDA/MPCFluid/Instrumentation/VelocityAutocorrelation/Base.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Defines the OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base class.
4  */
5 
6 #ifndef OPENMPCD_CUDA_MPCFLUID_INSTRUMENTATION_VELOCITYAUTOCORRELATION_BASE_HPP
7 #define OPENMPCD_CUDA_MPCFLUID_INSTRUMENTATION_VELOCITYAUTOCORRELATION_BASE_HPP
8 
10 #include <OpenMPCD/Vector3D.hpp>
11 
12 #include <boost/tuple/tuple.hpp>
13 #include <deque>
14 #include <vector>
15 
16 namespace OpenMPCD
17 {
18 namespace CUDA
19 {
20 
21 class Simulation;
22 
23 namespace MPCFluid
24 {
25 
26 class Base;
27 
28 namespace Instrumentation
29 {
30 /**
31  * Namespace for classes that measure the velocity autocorrelation in MPC fluids.
32  */
33 namespace VelocityAutocorrelation
34 {
35 /**
36  * Base class for measurements of velocity autocorrelation in MPC fluids.
37  *
38  * This class and its subclasses aid in measuring the velocity autocorrelation
39  * function of the fluid constituents' centers of mass, i.e.
40  * \f[
41  * \left< \vec{v} \left( t \right) \cdot \vec{v} \left( 0 \right) \right>
42  * =
43  * \frac{ 1 }{ N_{\textrm{logical entites}} }
44  * \sum_{k = 0}^{N_{\textrm{logical entites}} - 1}
45  * \left<
46  * \vec{v}_k \left( t \right) \cdot \vec{v}_k \left( 0 \right)
47  * \right>
48  * \f]
49  * where \f$ N_{\textrm{logical entites}} \f$ is the number of logical entities
50  * in the fluid (see `OpenMPCD::CUDA::MPCFluid::Base`),
51  * \f$ \vec{v}_k \left( t \right) \f$ is the velocity of the center of
52  * mass of the logical entity with index \f$ k \f$ at simulation time
53  * \f$ t \f$, and the angle brackets denote expectation values.
54  *
55  * To approximate this quantity, the center of mass velocities of the logical
56  * entities are stored in snapshots periodically.
57  * Then, each time `measure` is called, the quantity
58  * \f[
59  * V \left( t_i, t \right)
60  * =
61  * \frac{ 1 }{ N_{\textrm{logical entites}} }
62  * \sum_{k = 0}^{N_{\textrm{logical entites}} - 1}
63  * \vec{v}_k \left( t_i \right)
64  * \cdot
65  * \vec{v}_k \left( t \right)
66  * \f]
67  * is calculated, i.e. the sample average of the inner product of a logical
68  * entity's center of mass velocity at the current time \f$ t \f$ with that same
69  * logical entity's center of mass velocity at snapshot time
70  * \f$ t_i \f$, for all currently available snapshots \f$ i \f$.
71  *
72  * At most, `snapshotCount` (see configuration options below) many snapshots are
73  * stored at any point in time.
74  * Every time `measure` is called, before the \f$ V \left( t_i, t \right) \f$
75  * are calculated as described above, the following procedure takes place:
76  * - If no snapshots have been taken yet, a snapshot it taken.
77  * - Otherwise, if fewer than `snapshotCount` snapshots have already been
78  * created, the latest snapshot time is subtracted from the current
79  * simulation time. If that difference is larger than
80  * `measurementTime / snapshotCount - 1e-6`, a new snapshot is taken and
81  * stored after the previously taken snapshot.
82  * - If there have already been `snapshotCount` many snapshots before the call
83  * to `measure`, all of the snapshots are iterated over. For each such
84  * snapshot, if the difference between the current simulation time and that
85  * snapshot's time is greater than `measurementTime + 1e-6`, that snapshot is
86  * overridden in place with the current state and simulation time.
87  * The order of snapshots in memory, as described above, is of consequence for
88  * the order in which measurements are written by the `save` function.
89  *
90  * Given this, the velocity autocorrelation function
91  * \f$
92  * \left< \vec{v} \left( t \right) \cdot \vec{v} \left( 0 \right) \right>
93  * \f$
94  * can be approximated as the average of all \f$ V \left( t_i, t_j \right) \f$
95  * where \f$ t_j - t_i = t \f$.
96  *
97  * However, this last step is currently not performed by this class;
98  * instead, it computes \f$ V \left( t_i, t_j \right) \f$ and stores them, along
99  * with \f$ t_i \f$ and \f$ t_j \f$.
100  *
101  * This class and its subclasses are configured via the
102  * `instrumentation.velocityAutocorrelation` configuration group, which consists
103  * of the following settings:
104  * - `snapshotCount`, a positive integer, determines the maximum number
105  * of snapshots stored in memory at any given time.
106  * - `measurementTime`, which is a floating-point value that is approximately
107  * the maximal time for which the correlation function is measured;
108  * see above for details.
109  */
110 class Base
111 {
112  protected:
113  /**
114  * Represents a snapshot of the MPC fluid constituent velocities.
115  */
116  class Snapshot
117  {
118  public:
119  /**
120  * The constructor.
121  * @param[in] numberOfConstituents The number of logical constituents (e.g. pairs, triplets, ...)
122  * in the fluid.
123  * @param[in] devMemMgr The Device memory manager.
124  */
125  Snapshot(const unsigned int numberOfConstituents, DeviceMemoryManager* const devMemMgr);
126 
127  private:
128  Snapshot(const Snapshot&); ///< The copy constructor.
129 
130  public:
131  /**
132  * The destructor.
133  */
134  ~Snapshot();
135 
136  public:
137  /**
138  * Returns the MPC timestamp of the last snapshot.
139  */
141  {
142  return snapshotTime;
143  }
144 
145  /**
146  * Sets the MPC timestamp of the last snapshot.
147  * @param[in] time The time this snapshot has last been updated.
148  */
149  void setSnapshotTime(const FP time)
150  {
151  snapshotTime = time;
152  }
153 
154  /**
155  * Returns the Device buffer used to the save MPC fluid constituent velocities.
156  */
158  {
159  return buffer;
160  }
161 
162  private:
163  const Snapshot& operator=(const Snapshot&); ///< The assignment operator.
164 
165  private:
166  FP snapshotTime; ///< The time this snapshot was taken.
167  MPCParticleVelocityType* buffer; ///< The Device buffer for the MPC fluid constituent velocities.
168  DeviceMemoryManager* const deviceMemoryManager; ///< The simulation instance.
169  }; //class Snapshot
170 
171  protected:
172  /**
173  * The constructor.
174  *
175  * @throw OpenMPCD::InvalidConfigurationException
176  * Throws if the configuration is invalid.
177  * @throw OpenMPCD::NULLPointerException
178  * If `OPENMPCD_DEBUG` is defined, throws if any of the pointer
179  * arguments is a `nullptr`.
180  * @throw OpenMPCD::InvalidArgumentException
181  * If `OPENMPCD_DEBUG` is defined, throws if
182  * `numberOfConstituents_ == 0`.
183  *
184  * @param[in] sim The simulation instance.
185  * @param[in] devMemMgr The Device memory manager instance.
186  * @param[in] mpcFluid_ The MPC fluid to measure.
187  * @param[in] numberOfConstituents_ The number of logical constituents (e.g. pairs, triplets, ...) in the fluid.
188  */
189  Base(
190  const Simulation* const sim, DeviceMemoryManager* const devMemMgr,
191  const MPCFluid::Base* const mpcFluid_,
192  const unsigned int numberOfConstituents_);
193 
194  private:
195  Base(const Base&); ///< The copy constructor.
196 
197  public:
198  /**
199  * The destructor.
200  */
201  virtual ~Base();
202 
203  public:
204  /**
205  * Performs measurements.
206  */
207  void measure();
208 
209  /**
210  * Saves the data to the given run directory.
211  *
212  * The given run directory is created if necessary, and so is the data
213  * file therein; if the data file already exists, it will be truncated.
214  *
215  * Multiple lines will be written to the file, each consisting of three
216  * tab-separated columns:
217  * The first column corresponds to the simulation time
218  * \f$ t_i \f$ of a snapshot \f$ i \f$; the second
219  * column to the simulation time \f$ t_j \ge t_i \f$ ; and finally,
220  * the third column contains \f$ V \left( t_i, t_j \right) \f$.
221  * One such line will be written for every measured \f$ V \f$ (see class
222  * documentation for details).
223  *
224  * The lines are ordered by ascending \f$ t_j \f$
225  * first, and then by ascending snapshot position in memory (see class
226  * documentation for details).
227  *
228  * @param[in] rundir
229  * The path to the run directory. Within that directory,
230  * the data will be saved to the file
231  * `velocityAutocorrelations.data`.
232  */
233  void save(const std::string& rundir) const;
234 
235  public:
236  /**
237  * Returns whether the given simulation configured this instrumentation.
238  * @param[in] sim The simulation instance.
239  * @throw NULLPointerException Throws if `sim` is `nullptr`.
240  */
241  static bool isConfigured(const Simulation* const sim);
242 
243  protected:
244  /**
245  * Populates the Device buffer with the current velocities of the fluid constituents.
246  */
247  virtual void populateCurrentVelocities() = 0;
248 
249  private:
250  /**
251  * Reads the configuration.
252  */
253  void readConfig();
254 
255  /**
256  * Updates the snapshots as necessary.
257  */
258  void updateSnapshots();
259 
260  /**
261  * Updates the given snapshot.
262  * @param[in] snapshot The snapshot to update.
263  */
264  void updateSnapshot(Snapshot* const snapshot);
265 
266  /**
267  * Checks whether the given snapshot should be initialized, and does so if appropriate.
268  * @param[in] snapshot The snapshot index.
269  * @return Returns true if the snapshot has been initialized (possibly during previous calls)
270  * and is ready for measurement, false otherwise.
271  */
272  bool initializeSnapshotIfAppropriate(const unsigned int snapshotID);
273 
274  /**
275  * Performs a measurement for the given snapshot.
276  * @param[in] snapshot The snapshot to measure with.
277  */
278  void measureWithSnapshot(Snapshot* const snapshot);
279 
280  /**
281  * Returns the value of the correlation function for the given snapshot,
282  * performing all calculations on the CPU in a naive way.
283  * Note that this function is intended to be used as a verification of the GPU
284  * result, and it is in no way optimized for performance.
285  * @param[in] snapshot The snapshot to measure with.
286  */
287  MPCParticleVelocityType getCorrelationForSnapshot_CPU_naive(Snapshot* const snapshot);
288 
289  /**
290  * Returns the value of the correlation function for the given snapshot,
291  * performing all calculations on the CPU using the thrust library.
292  * Note that this function is intended to be used as a verification of the GPU
293  * result, and it is in no way optimized for performance.
294  * @param[in] snapshot The snapshot to measure with.
295  */
296  MPCParticleVelocityType getCorrelationForSnapshot_CPU_thrust(Snapshot* const snapshot);
297 
298  /**
299  * Saves the data to the given file.
300  *
301  * See `save` for details on the output format.
302  *
303  * @param[in] path The file path to save the data to.
304  */
305  void saveAutocorrelations(const std::string& path) const;
306 
307  protected:
308  const Simulation* const simulation; ///< The simulation instance.
309  DeviceMemoryManager* const deviceMemoryManager; ///< The Device memory manager.
310  const MPCFluid::Base* const mpcFluid; ///< The fluid to measure.
311 
312  const unsigned int numberOfConstituents;
313  ///< The number of logical constituents (e.g. pairs, triplets, ...) in the fluid.
314 
315  FP measurementTime; ///< The maximum time between snapshots.
316 
317  std::vector<Snapshot*> snapshots; ///< The snapshots of the fluid constituent velocities.
318  MPCParticleVelocityType* currentVelocities; ///< Current velocities of the fluid constituents.
319 
320  std::deque<boost::tuple<FP, FP, MPCParticleVelocityType> > autocorrelations;
321  ///< Collection of tuples consisting of, in that order,
322  /// \f$ t_i \f$, \f$ t_j \f$, and
323  /// \f$ V \left( t_i, t_j \right) \f$.
324 }; //class Base
325 } //namespace VelocityAutocorrelation
326 } //namespace Instrumentation
327 } //namespace MPCFluid
328 } //namespace CUDA
329 } //namespace OpenMPCD
330 
331 #endif
OpenMPCD::CUDA::MPCFluid::Base
Base class for MPC fluids.
Definition: CUDA/MPCFluid/Base.hpp:40
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::mpcFluid
const MPCFluid::Base *const mpcFluid
The fluid to measure.
Definition: CUDA/MPCFluid/Instrumentation/VelocityAutocorrelation/Base.hpp:310
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::Snapshot::getBuffer
MPCParticleVelocityType * getBuffer()
Returns the Device buffer used to the save MPC fluid constituent velocities.
Definition: CUDA/MPCFluid/Instrumentation/VelocityAutocorrelation/Base.hpp:157
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::currentVelocities
MPCParticleVelocityType * currentVelocities
Current velocities of the fluid constituents.
Definition: CUDA/MPCFluid/Instrumentation/VelocityAutocorrelation/Base.hpp:318
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::Snapshot::Snapshot
Snapshot(const unsigned int numberOfConstituents, DeviceMemoryManager *const devMemMgr)
The constructor.
OpenMPCD::CUDA::DeviceMemoryManager
Class for managing memory on the CUDA Device.
Definition: DeviceMemoryManager.hpp:21
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::isConfigured
static bool isConfigured(const Simulation *const sim)
Returns whether the given simulation configured this instrumentation.
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::Snapshot
Represents a snapshot of the MPC fluid constituent velocities.
Definition: CUDA/MPCFluid/Instrumentation/VelocityAutocorrelation/Base.hpp:116
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::Base
Base(const Simulation *const sim, DeviceMemoryManager *const devMemMgr, const MPCFluid::Base *const mpcFluid_, const unsigned int numberOfConstituents_)
The constructor.
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::Snapshot::setSnapshotTime
void setSnapshotTime(const FP time)
Sets the MPC timestamp of the last snapshot.
Definition: CUDA/MPCFluid/Instrumentation/VelocityAutocorrelation/Base.hpp:149
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::Snapshot::~Snapshot
~Snapshot()
The destructor.
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::save
void save(const std::string &rundir) const
Saves the data to the given run directory.
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::Snapshot::getSnapshotTime
FP getSnapshotTime() const
Returns the MPC timestamp of the last snapshot.
Definition: CUDA/MPCFluid/Instrumentation/VelocityAutocorrelation/Base.hpp:140
OpenMPCD::CUDA::Simulation
MPCD simulation with Molecular Dynamics on CUDA-capable GPUs.
Definition: CUDA/Simulation.hpp:48
DeviceMemoryManager.hpp
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::measure
void measure()
Performs measurements.
Vector3D.hpp
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::snapshots
std::vector< Snapshot * > snapshots
The snapshots of the fluid constituent velocities.
Definition: CUDA/MPCFluid/Instrumentation/VelocityAutocorrelation/Base.hpp:317
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::measurementTime
FP measurementTime
The maximum time between snapshots.
Definition: CUDA/MPCFluid/Instrumentation/VelocityAutocorrelation/Base.hpp:315
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::~Base
virtual ~Base()
The destructor.
OpenMPCD::FP
double FP
Default floating point type.
Definition: Types.hpp:13
OpenMPCD::MPCParticleVelocityType
FP MPCParticleVelocityType
The data type for the velocities of MPC particles.
Definition: Types.hpp:16
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::numberOfConstituents
const unsigned int numberOfConstituents
The number of logical constituents (e.g. pairs, triplets, ...) in the fluid.
Definition: CUDA/MPCFluid/Instrumentation/VelocityAutocorrelation/Base.hpp:312
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::autocorrelations
std::deque< boost::tuple< FP, FP, MPCParticleVelocityType > > autocorrelations
Collection of tuples consisting of, in that order, , , and .
Definition: CUDA/MPCFluid/Instrumentation/VelocityAutocorrelation/Base.hpp:320
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::populateCurrentVelocities
virtual void populateCurrentVelocities()=0
Populates the Device buffer with the current velocities of the fluid constituents.
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base
Base class for measurements of velocity autocorrelation in MPC fluids.
Definition: CUDA/MPCFluid/Instrumentation/VelocityAutocorrelation/Base.hpp:110
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::simulation
const Simulation *const simulation
The simulation instance.
Definition: CUDA/MPCFluid/Instrumentation/VelocityAutocorrelation/Base.hpp:308
OpenMPCD::CUDA::MPCFluid::Instrumentation::VelocityAutocorrelation::Base::deviceMemoryManager
DeviceMemoryManager *const deviceMemoryManager
The Device memory manager.
Definition: CUDA/MPCFluid/Instrumentation/VelocityAutocorrelation/Base.hpp:309