OpenMPCD
CUDA/MPCFluid/Base.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Defines the OpenMPCD::CUDA::MPCFluid::Base class.
4  */
5 
6 #ifndef OPENMPCD_CUDA_MPCFLUID_BASE_HPP
7 #define OPENMPCD_CUDA_MPCFLUID_BASE_HPP
8 
11 #include <OpenMPCD/Exceptions.hpp>
13 #include <OpenMPCD/Vector3D.hpp>
15 
16 namespace OpenMPCD
17 {
18 namespace CUDA
19 {
20  class Simulation;
21 
22 /**
23  * Namespace for MPC Fluid classes.
24  */
25 namespace MPCFluid
26 {
27 
28  /**
29  * Base class for MPC fluids.
30  *
31  * The MPC fluid consists of a certain number of <em>logical entities</em>,
32  * each of which consists of one or more <em>MPC particles</em>.
33  *
34  * Examples of logical entities include linear polymers, ring polymers, and
35  * dendrimers. The MPC particles (i.e. the fundamental objects that MPC is
36  * concerned with) that would make up those logical entities would typically
37  * couple to each other via interaction potentials that depend on the kind
38  * of MPC fluid at hand.
39  */
40  class Base
41  {
42  friend class CUDA::Simulation;
43 
44  protected:
45  /**
46  * The constructor.
47  * This constructor neither initializes the fluid state on Host or Device,
48  * nor does it synchronize Host and Device.
49  * @param[in] sim The simulation instance.
50  * @param[in] count The number of fluid particles.
51  * @param[in] streamingTimestep_ The timestep for a streaming step.
52  * @param[in] rng_ A random number generator to seed this instance's RNG with.
53  * @param[in] devMemMgr The Device memory manager.
54  */
55  Base(
56  const CUDA::Simulation* const sim, const unsigned int count, const FP streamingTimestep_,
57  RNG& rng_, DeviceMemoryManager* const devMemMgr);
58 
59  private:
60  Base(const Base&); ///< The copy constructor.
61 
62  public:
63  /**
64  * The destructor.
65  */
66  virtual ~Base();
67 
68  public:
69  /**
70  * Returns the number of MPC fluid particles.
71  */
72  unsigned int getParticleCount() const
73  {
74  return numberOfParticles;
75  }
76 
77  /**
78  * Returns the MPC fluid particle mass.
79  */
80  static unsigned int getParticleMass()
81  {
82  return mpcParticleMass;
83  }
84 
85  /**
86  * Returns the number of logical entities in the fluid.
87  */
88  virtual unsigned int getNumberOfLogicalEntities() const = 0;
89 
90  /**
91  * Returns whether all logical entities consist of the same number
92  * of MPC particles.
93  *
94  * The value of this function will remain unchanged for the lifetime
95  * of the instance.
96  */
97  virtual
99 
100  /**
101  * Returns the number of MPC particles per logical entity.
102  *
103  * @throw OpenMPCD::InvalidCallException
104  * Throws if
105  * `!numberOfParticlesPerLogicalEntityIsConstant()`.
106  */
107  virtual unsigned int getNumberOfParticlesPerLogicalEntity() const;
108 
109  /**
110  * Copies the MPC fluid particles from the CUDA Device to the Host.
111  */
112  void fetchFromDevice() const;
113 
114  /**
115  * Returns a MPC fluid particle's position vector.
116  * @warning
117  * This function only returns the position that was current the last time
118  * fetchFromDevice was called.
119  * @throw OutOfBoundsException If OPENMPCD_DEBUG is defined,
120  * throws if particleID >= getMPCParticleCount()
121  * @param[in] particleID The particle ID.
122  */
124  getPosition(const unsigned int particleID) const;
125 
126  /**
127  * Returns a MPC fluid particle's velocity vector.
128  * @warning
129  * This function only returns the velocity that was current the last time
130  * fetchFromDevice was called.
131  * @throw OutOfBoundsException If OPENMPCD_DEBUG is defined,
132  * throws if particleID >= getMPCParticleCount()
133  * @param[in] particleID The particle ID.
134  */
136  getVelocity(const unsigned int particleID) const;
137 
138  /**
139  * Sets the positions and velocities of the particles on the Device.
140  *
141  * @throw OpenMPCD::NULLPointerException
142  * If `OPENMPCD_DEBUG` is defined, throws if
143  * `positions == nullptr` or `velocities == nullptr`.
144  *
145  * @param[in] positions An array holding `3 * getParticleCount()`
146  * values on the Host; first, the `x`, `y`,
147  * and `z` coordinates of the first atom, then
148  * those of the second, etc.
149  * @param[in] velocities An array holding `3 * getParticleCount()`
150  * values on the Host; first, the `x`, `y`,
151  * and `z` velocities of the first atom, then
152  * those of the second, etc.
153  */
155  const MPCParticlePositionType* const positions,
156  const MPCParticleVelocityType* const velocities);
157 
158  /**
159  * Returns a const pointer to the MPC fluid positions on the Device.
160  */
162  {
163  return d_mpcParticlePositions;
164  }
165 
166  /**
167  * Returns a pointer to the MPC fluid positions on the Device.
168  */
170  {
171  return d_mpcParticlePositions;
172  }
173 
174  /**
175  * Returns a pointer to the MPC fluid velocities on the Device.
176  */
178  {
180  }
181 
182  /**
183  * Returns a const pointer to the MPC fluid velocities on the Device.
184  */
186  {
188  }
189 
190  /**
191  * Returns a pointer to the MPC fluid positions on the Host.
192  */
194  {
195  return mpcParticlePositions;
196  }
197 
198  /**
199  * Returns a pointer to the MPC fluid velocities on the Host.
200  */
202  {
203  return mpcParticleVelocities;
204  }
205 
206  /**
207  * Returns the fluid instrumentation.
208  * @throw NULLPointerException If OPENMPCD_DEBUG is defined, throws if instrumentation is a NULL pointer.
209  */
211  {
212  #ifdef OPENMPCD_DEBUG
213  if(instrumentation==NULL)
214  OPENMPCD_THROW(NULLPointerException, "instrumentation");
215  #endif
216 
217  return *const_cast<Instrumentation::Base*>(instrumentation);
218  }
219 
220  /**
221  * Writes the particle positions and velocities to the given
222  * snapshot file.
223  *
224  * This function will call `fetchFromDevice`, and write the current
225  * Device data to the given snapshot file.
226  *
227  * @throw OpenMPCD::NULLPointerException
228  * Throws if `snapshot == nullptr`.
229  * @throw OpenMPCD::InvalidArgumentException
230  * Throws if the number of atoms declared in the snapshot
231  * does not match the number of particles in this instance.
232  * @throw OpenMPCD::InvalidArgumentException
233  * Throws if the given snapshot is not in write mode.
234  *
235  * @param[in,out] snapshot The snapshot file.
236  */
237  void writeToSnapshot(VTFSnapshotFile* const snapshot) const;
238 
239  /**
240  * Computes, on the Host, which MPC particles match the criterion represented by `func`.
241  * @warning
242  * This function operates only on the data that were current the last time `fetchFromDevice` was called.
243  * @param[in] func Iff this function returns true, the MPC particles are considered to satisfy
244  * the criterion. The first argument is the particle's position, the second
245  * its velocity.
246  * @param[out] matches Will hold the IDs of the particles that satisfied the criterion; may be `nullptr` if not needed.
247  * @param[out] matchCount Will hold the number of particles that satisfied the criterion; may be `nullptr` if not needed.
248  */
251  std::vector<unsigned int>* const matches,
252  unsigned int* const matchCount
253  ) const;
254 
255 
256  /**
257  * Computes the center of mass for each logical entity, and saves
258  * their coordinates in the given Device buffer.
259  *
260  * The computation is performed with the data as it currently exists
261  * on the Device.
262  *
263  * @throw OpenMPCD::NULLPointerException
264  * Throws if `buffer == nullptr`. *
265  *
266  * @param[out] buffer The device buffer to save the coordinates to.
267  * It must be able to hold at least
268  * `3 * getNumberOfLogicalEntities()` elements.
269  * The first element in the buffer will be
270  * the `x` coordinate of the center of mass of
271  * the first logical entity, followed by the `y`
272  * and `z` coordinates. After that, the second
273  * entity's coordinates follow, and so on.
274  */
276  MPCParticlePositionType* const buffer) const;
277 
278  protected:
279  /**
280  * Performs a streaming step.
281  */
282  virtual void stream() = 0;
283 
284  /**
285  * Copies the MPC fluid particles from the Host to the CUDA Device.
286  */
287  void pushToDevice();
288 
289  /**
290  * Initializes the fluid particles velocities in Host memory.
291  */
292  void initializeVelocitiesOnHost() const;
293 
294  /**
295  * Scales all MPC fluid particle velocities so that the global temperature matches the given target.
296  * @param[in] kT The target temperature, multiplied by Boltzmann's constant.
297  * @param[in,out] velocities The MPC fluid particle velocities on the Host.
298  * @param[in] particleCount The number of MPC fluid particles.
299  */
300  static void globalUnbiasedThermostat(const FP kT, MPCParticleVelocityType* const velocities,
301  const unsigned int particleCount);
302 
303  /**
304  * Returns the total momentum of the given MPC fluid particles.
305  * @param[in] velocities The velocities of the MPC fluid particles on the Host.
306  * @param[in] particleCount The number of MPC fluid particles.
307  */
309  getTotalMomentum(const MPCParticleVelocityType* const velocities,
310  const unsigned int particleCount);
311 
312  /**
313  * Returns the mean momentum of the given MPC fluid particles.
314  * @param[in] velocities The velocities of the MPC fluid particles on the Host.
315  * @param[in] particleCount The number of MPC fluid particles.
316  */
318  getMeanMomentum(const MPCParticleVelocityType* const velocities,
319  const unsigned int particleCount);
320 
321  /**
322  * Returns the total kinetic energy of the MPC fluid.
323  * @param[in] velocities The velocities of the MPC fluid particles on the Host.
324  * @param[in] particleCount The number of MPC fluid particles.
325  */
326  static FP getKineticEnergy(const MPCParticleVelocityType* const velocities,
327  const unsigned int particleCount);
328 
329  /**
330  * Returns the product of Boltzmann's constant with the MPC fluid temperature,
331  * as measured by the total kinetic energy of the MPC fluid.
332  * @param[in] velocities The velocities of the MPC fluid particles on the Host.
333  * @param[in] particleCount The number of MPC fluid particles.
334  */
335  static FP getkTViaKineticEnergy(const MPCParticleVelocityType* const velocities,
336  const unsigned int particleCount)
337  {
338  return 2.0/3.0*getKineticEnergy(velocities, particleCount)/particleCount;
339  }
340 
341  private:
342  /**
343  * Reads the configuration.
344  */
345  void readConfiguration();
346 
347  private:
348  const Base& operator=(const Base&); ///< The assignment operator.
349 
350  protected:
351  static const unsigned int mpcParticleMass = 1; ///< The mass of each MPC particle.
352 
353  const CUDA::Simulation* const simulation; ///< The simulation instance.
354  DeviceMemoryManager* const deviceMemoryManager; ///< The Device memory manager.
355 
356  Instrumentation::Base* instrumentation; ///< The fluid instrumentation.
357 
358  mutable RNG rng; ///< The random number generator.
359 
360  mutable MPCParticlePositionType* mpcParticlePositions; ///< Host pointer for the MPC particle positions.
361  mutable MPCParticleVelocityType* mpcParticleVelocities; ///< Host pointer for the MPC particle positions.
362 
363  MPCParticlePositionType* d_mpcParticlePositions; ///< Device pointer for the MPC particle positions.
364  MPCParticleVelocityType* d_mpcParticleVelocities; ///< Device pointer for the MPC particle positions.
365 
366  const unsigned int numberOfParticles; ///< The number of fluid particles.
367 
368  const FP streamingTimestep; ///< The timestep for a streaming step.
369  };
370 
371 } //namespace MPCFluid
372 } //namespace CUDA
373 } //namespace OpenMPCD
374 
375 #endif
OpenMPCD::CUDA::MPCFluid::Base::d_mpcParticleVelocities
MPCParticleVelocityType * d_mpcParticleVelocities
Device pointer for the MPC particle positions.
Definition: CUDA/MPCFluid/Base.hpp:364
OpenMPCD::CUDA::MPCFluid::Base::getKineticEnergy
static FP getKineticEnergy(const MPCParticleVelocityType *const velocities, const unsigned int particleCount)
Returns the total kinetic energy of the MPC fluid.
OpenMPCD::CUDA::MPCFluid::Base
Base class for MPC fluids.
Definition: CUDA/MPCFluid/Base.hpp:40
OpenMPCD::CUDA::MPCFluid::Base::deviceMemoryManager
DeviceMemoryManager *const deviceMemoryManager
The Device memory manager.
Definition: CUDA/MPCFluid/Base.hpp:354
OpenMPCD::RemotelyStoredVector
Represents a vector whose data is stored elsewhere.
Definition: RemotelyStoredVector.hpp:26
OpenMPCD::CUDA::MPCFluid::Base::streamingTimestep
const FP streamingTimestep
The timestep for a streaming step.
Definition: CUDA/MPCFluid/Base.hpp:368
Exceptions.hpp
OpenMPCD::CUDA::MPCFluid::Base::getParticleMass
static unsigned int getParticleMass()
Returns the MPC fluid particle mass.
Definition: CUDA/MPCFluid/Base.hpp:80
OPENMPCD_THROW
#define OPENMPCD_THROW(ExceptionType, message)
Throws the given ExceptionType, passing the given message along with file and line number information...
Definition: Exceptions.hpp:22
OpenMPCD::CUDA::MPCFluid::Base::getTotalMomentum
static const Vector3D< MPCParticleVelocityType > getTotalMomentum(const MPCParticleVelocityType *const velocities, const unsigned int particleCount)
Returns the total momentum of the given MPC fluid particles.
OpenMPCD::CUDA::MPCFluid::Base::globalUnbiasedThermostat
static void globalUnbiasedThermostat(const FP kT, MPCParticleVelocityType *const velocities, const unsigned int particleCount)
Scales all MPC fluid particle velocities so that the global temperature matches the given target.
OpenMPCD::CUDA::MPCFluid::Base::stream
virtual void stream()=0
Performs a streaming step.
OpenMPCD::CUDA::MPCFluid::Base::setPositionsAndVelocities
void setPositionsAndVelocities(const MPCParticlePositionType *const positions, const MPCParticleVelocityType *const velocities)
Sets the positions and velocities of the particles on the Device.
OpenMPCD::CUDA::DeviceMemoryManager
Class for managing memory on the CUDA Device.
Definition: DeviceMemoryManager.hpp:21
OpenMPCD::Vector3D
3-dimensional vector.
Definition: Vector3D.hpp:38
OpenMPCD::CUDA::MPCFluid::Base::mpcParticlePositions
MPCParticlePositionType * mpcParticlePositions
Host pointer for the MPC particle positions.
Definition: CUDA/MPCFluid/Base.hpp:360
RemotelyStoredVector.hpp
OpenMPCD::CUDA::MPCFluid::Base::writeToSnapshot
void writeToSnapshot(VTFSnapshotFile *const snapshot) const
Writes the particle positions and velocities to the given snapshot file.
OpenMPCD::CUDA::MPCFluid::Base::saveLogicalEntityCentersOfMassToDeviceMemory
void saveLogicalEntityCentersOfMassToDeviceMemory(MPCParticlePositionType *const buffer) const
Computes the center of mass for each logical entity, and saves their coordinates in the given Device ...
OpenMPCD::CUDA::MPCFluid::Base::pushToDevice
void pushToDevice()
Copies the MPC fluid particles from the Host to the CUDA Device.
OpenMPCD::CUDA::MPCFluid::Base::getMeanMomentum
static const Vector3D< MPCParticleVelocityType > getMeanMomentum(const MPCParticleVelocityType *const velocities, const unsigned int particleCount)
Returns the mean momentum of the given MPC fluid particles.
OpenMPCD::CUDA::MPCFluid::Base::getDevicePositions
const MPCParticlePositionType * getDevicePositions() const
Returns a const pointer to the MPC fluid positions on the Device.
Definition: CUDA/MPCFluid/Base.hpp:161
OpenMPCD::MPCParticlePositionType
FP MPCParticlePositionType
The data type for the positions of MPC particles.
Definition: Types.hpp:15
OpenMPCD::CUDA::MPCFluid::Base::Base
Base(const CUDA::Simulation *const sim, const unsigned int count, const FP streamingTimestep_, RNG &rng_, DeviceMemoryManager *const devMemMgr)
The constructor.
VTFSnapshotFile.hpp
OpenMPCD::CUDA::MPCFluid::Base::getHostVelocities
MPCParticleVelocityType * getHostVelocities()
Returns a pointer to the MPC fluid velocities on the Host.
Definition: CUDA/MPCFluid/Base.hpp:201
OpenMPCD::CUDA::MPCFluid::Base::findMatchingParticlesOnHost
void findMatchingParticlesOnHost(bool(*func)(const RemotelyStoredVector< const MPCParticlePositionType > &, const RemotelyStoredVector< const MPCParticleVelocityType > &), std::vector< unsigned int > *const matches, unsigned int *const matchCount) const
Computes, on the Host, which MPC particles match the criterion represented by func.
OpenMPCD::CUDA::MPCFluid::Base::initializeVelocitiesOnHost
void initializeVelocitiesOnHost() const
Initializes the fluid particles velocities in Host memory.
OpenMPCD::CUDA::MPCFluid::Base::mpcParticleMass
static const unsigned int mpcParticleMass
The mass of each MPC particle.
Definition: CUDA/MPCFluid/Base.hpp:351
OpenMPCD::CUDA::MPCFluid::Base::rng
RNG rng
The random number generator.
Definition: CUDA/MPCFluid/Base.hpp:358
OpenMPCD::CUDA::MPCFluid::Base::getInstrumentation
Instrumentation::Base & getInstrumentation() const
Returns the fluid instrumentation.
Definition: CUDA/MPCFluid/Base.hpp:210
OpenMPCD::CUDA::MPCFluid::Base::numberOfParticles
const unsigned int numberOfParticles
The number of fluid particles.
Definition: CUDA/MPCFluid/Base.hpp:366
OpenMPCD::CUDA::MPCFluid::Base::fetchFromDevice
void fetchFromDevice() const
Copies the MPC fluid particles from the CUDA Device to the Host.
OpenMPCD::CUDA::Simulation
MPCD simulation with Molecular Dynamics on CUDA-capable GPUs.
Definition: CUDA/Simulation.hpp:48
DeviceMemoryManager.hpp
OpenMPCD::RNG
boost::mt11213b RNG
The random number generator type.
Definition: Types.hpp:18
Vector3D.hpp
OpenMPCD::CUDA::MPCFluid::Base::getDeviceVelocities
const MPCParticleVelocityType * getDeviceVelocities() const
Returns a const pointer to the MPC fluid velocities on the Device.
Definition: CUDA/MPCFluid/Base.hpp:185
OpenMPCD::CUDA::MPCFluid::Base::getVelocity
const RemotelyStoredVector< const MPCParticleVelocityType > getVelocity(const unsigned int particleID) const
Returns a MPC fluid particle's velocity vector.
OpenMPCD::CUDA::MPCFluid::Base::getParticleCount
unsigned int getParticleCount() const
Returns the number of MPC fluid particles.
Definition: CUDA/MPCFluid/Base.hpp:72
OpenMPCD::FP
double FP
Default floating point type.
Definition: Types.hpp:13
OpenMPCD::CUDA::MPCFluid::Instrumentation::Base
Base class for MPC fluids instrumentation.
Definition: CUDA/MPCFluid/Instrumentation/Base.hpp:34
OpenMPCD::MPCParticleVelocityType
FP MPCParticleVelocityType
The data type for the velocities of MPC particles.
Definition: Types.hpp:16
OpenMPCD::CUDA::MPCFluid::Base::getDevicePositions
MPCParticlePositionType * getDevicePositions()
Returns a pointer to the MPC fluid positions on the Device.
Definition: CUDA/MPCFluid/Base.hpp:169
OpenMPCD::CUDA::MPCFluid::Base::d_mpcParticlePositions
MPCParticlePositionType * d_mpcParticlePositions
Device pointer for the MPC particle positions.
Definition: CUDA/MPCFluid/Base.hpp:363
OpenMPCD::CUDA::MPCFluid::Base::simulation
const CUDA::Simulation *const simulation
The simulation instance.
Definition: CUDA/MPCFluid/Base.hpp:353
OpenMPCD::CUDA::MPCFluid::Base::getNumberOfParticlesPerLogicalEntity
virtual unsigned int getNumberOfParticlesPerLogicalEntity() const
Returns the number of MPC particles per logical entity.
OpenMPCD::CUDA::MPCFluid::Base::getkTViaKineticEnergy
static FP getkTViaKineticEnergy(const MPCParticleVelocityType *const velocities, const unsigned int particleCount)
Returns the product of Boltzmann's constant with the MPC fluid temperature, as measured by the total ...
Definition: CUDA/MPCFluid/Base.hpp:335
OpenMPCD::VTFSnapshotFile
Representation of a simulation snapshot file in the VTF format.
Definition: VTFSnapshotFile.hpp:49
OpenMPCD::CUDA::MPCFluid::Base::~Base
virtual ~Base()
The destructor.
OpenMPCD::CUDA::MPCFluid::Base::mpcParticleVelocities
MPCParticleVelocityType * mpcParticleVelocities
Host pointer for the MPC particle positions.
Definition: CUDA/MPCFluid/Base.hpp:361
OpenMPCD::CUDA::MPCFluid::Base::getHostPositions
MPCParticlePositionType * getHostPositions()
Returns a pointer to the MPC fluid positions on the Host.
Definition: CUDA/MPCFluid/Base.hpp:193
OpenMPCD::CUDA::MPCFluid::Base::instrumentation
Instrumentation::Base * instrumentation
The fluid instrumentation.
Definition: CUDA/MPCFluid/Base.hpp:356
OpenMPCD::CUDA::MPCFluid::Base::getDeviceVelocities
MPCParticleVelocityType * getDeviceVelocities()
Returns a pointer to the MPC fluid velocities on the Device.
Definition: CUDA/MPCFluid/Base.hpp:177
OpenMPCD::CUDA::MPCFluid::Base::getPosition
const RemotelyStoredVector< const MPCParticlePositionType > getPosition(const unsigned int particleID) const
Returns a MPC fluid particle's position vector.
OpenMPCD::CUDA::MPCFluid::Base::getNumberOfLogicalEntities
virtual unsigned int getNumberOfLogicalEntities() const =0
Returns the number of logical entities in the fluid.
OpenMPCD::NULLPointerException
NULL-pointer exception.
Definition: Exceptions.hpp:96
Base.hpp
OpenMPCD::CUDA::MPCFluid::Base::numberOfParticlesPerLogicalEntityIsConstant
virtual bool numberOfParticlesPerLogicalEntityIsConstant() const =0
Returns whether all logical entities consist of the same number of MPC particles.