OpenMPCD
CUDA/Simulation.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Defines the OpenMPCD::CUDA::Simulation class.
4  */
5 
6 #ifndef OPENMPCD_CUDA_SIMULATION_HPP
7 #define OPENMPCD_CUDA_SIMULATION_HPP
8 
14 #include <OpenMPCD/CUDA/Types.hpp>
17 #include <OpenMPCD/Types.hpp>
18 #include <OpenMPCD/Vector3D.hpp>
19 #include <string>
20 
21 namespace OpenMPCDTest
22 {
23 namespace CUDA
24 {
25 class SimulationTest;
26 } //namespace CUDA
27 } //namespace OpenMPCDTest
28 
29 
30 namespace OpenMPCD
31 {
32 
33 /**
34  * Namespace for simulations using CUDA.
35  */
36 namespace CUDA
37 {
38  /**
39  * MPCD simulation with Molecular Dynamics on CUDA-capable GPUs.
40  *
41  * For a description of the Maxwell-Boltzmann-Scaling Thermostat, see
42  * "Cell-level canonical sampling by velocity scaling for multiparticle
43  * collision dynamics simulations"
44  * by C. C. Huang, A. Chatterji, G. Sutmann, G. Gompper, and R. G. Winkler.
45  * Journal of Computational Physics 229 (2010) 168-177.
46  * DOI:10.1016/j.jcp.2009.09.024
47  */
48  class Simulation
49  {
50  friend class OpenMPCDTest::CUDA::SimulationTest;
51 
52  public:
53  /**
54  * The constructor.
55  * @param[in] configurationFilename The path to the simulation configuration.
56  * @param[in] rngSeed The seed for the random number generator.
57  * @param[in] dir The directory where simulation runs will be saved.
58  */
59  Simulation(const std::string& configurationFilename, const unsigned int rngSeed, const std::string& dir);
60 
61  /**
62  * The constructor.
63  * @param[in] configuration The configuration instance.
64  * @param[in] rngSeed The seed for the random number generator.
65  */
66  Simulation(const Configuration& configuration, const unsigned int rngSeed);
67 
68  private:
69  Simulation(const Simulation&); ///< The copy constructor.
70 
71  public:
72  /**
73  * The destructor.
74  */
75  ~Simulation();
76 
77  public:
78  /**
79  * Returns the configuration.
80  */
82  {
83  return config;
84  }
85 
86  /**
87  * Performs the warmup step.
88  */
89  void warmup();
90 
91  /**
92  * Performs a sweep.
93  */
94  void sweep();
95 
96  /**
97  * Returns the number of times `sweep` has completed, not counting
98  * warmup sweeps as performed by `warmup`.
99  */
100  unsigned int getNumberOfCompletedSweeps() const;
101 
102  /**
103  * Returns the size of the primary simulation box along the x direction.
104  */
105  unsigned int getSimulationBoxSizeX() const
106  {
107  return mpcSimulationBoxSizeX;
108  }
109 
110  /**
111  * Returns the size of the primary simulation box along the y direction.
112  */
113  unsigned int getSimulationBoxSizeY() const
114  {
115  return mpcSimulationBoxSizeY;
116  }
117 
118  /**
119  * Returns the size of the primary simulation box along the z direction.
120  */
121  unsigned int getSimulationBoxSizeZ() const
122  {
123  return mpcSimulationBoxSizeZ;
124  }
125 
126  /**
127  * Returns the number of collision cells.
128  */
129  unsigned int getCollisionCellCount() const
130  {
131  return
135  }
136 
137 
138  /**
139  * Returns whether an MPC fluid has been configured.
140  */
141  bool hasMPCFluid() const
142  {
143  return mpcFluid != NULL;
144  }
145 
146  /**
147  * Returns the MPC fluid.
148  *
149  * @throw OpenMPCD::InvalidCallException
150  * If `OPENMPCD_DEBUG` is defined, throws if `!hasMPCFluid()`.
151  */
153  {
156 
157  return *mpcFluid;
158  }
159 
160  /**
161  * Returns the MPC solute
162  *
163  * @throw OpenMPCD::InvalidCallException
164  * If `OPENMPCD_DEBUG` is defined, throws if no solute has
165  * been configured.
166  */
167  const MPCSolute::Base<
169  getMPCSolute() const
170  {
171  #ifdef OPENMPCD_DEBUG
172  if(!mpcSolute)
173  OPENMPCD_THROW(InvalidCallException, "getMPCSolute");
174  #endif
175 
176  return *mpcSolute;
177  }
178 
179  /**
180  * Returns the boundary conditions.
181  */
183  {
184  return boundaryCondition;
185  }
186 
187  /**
188  * Returns true if Simulation has an instance of
189  * MPCSolute::Base
190  */
191  bool hasSolute() const
192  {
193  if (mpcSolute) return true;
194  return false;
195  }
196 
197  /**
198  * Returns the MPC time that has passed since the start of the simulation.
199  */
200  FP getMPCTime() const
201  {
202  return mpcTime;
203  }
204 
205  /**
206  * Returns the MPC timestep.
207  */
209  {
210  return mpcTimestep;
211  }
212 
213  /**
214  * Returns the directory containing the Simulation run data.
215  */
216  std::string getRundir() const
217  {
218  return rundir;
219  }
220 
221  /**
222  * Returns the Device memory manager.
223  */
225  {
226  return &deviceMemoryManager;
227  }
228 
229  private:
230  /**
231  * Reads the configuration.
232  * @throw std::runtime_error Throws if the configuration is invalid.
233  */
234  void readConfiguration();
235 
236  /**
237  * Initializes the simulation.
238  */
239  void initialize();
240 
241  /**
242  * Runs the simulation for the given number of steps.
243  * @param[in] stepCount The number of time steps to simulate.
244  */
245  void run(const unsigned int stepCount);
246 
247  /**
248  * Performs a streaming step.
249  */
250  void stream();
251 
252  /**
253  * Performs a SRD collision step.
254  */
255  void collide();
256 
257  /**
258  * Generates a new grid shift vector and uploads it to the CUDA device.
259  */
261 
262  /**
263  * Generates new rotation axes for the collision cells and uploads them to the CUDA device.
264  */
266 
267  /**
268  * Generates Maxwell-Boltzmann-Scaling factors for the collision
269  * cells and uploads them to the CUDA device.
270  *
271  * If the Maxwell-Boltzmann-Scaling thermostat is not configured,
272  * this function does nothing.
273  */
275 
276  private:
277  const Simulation& operator=(const Simulation&); ///< The assignment operator.
278 
279  private:
280  Configuration config; ///< The configuration for this simulation.
281 
282  unsigned int mpcSimulationBoxSizeX; /**< The length of the MPC simulation box in the x direction,
283  in units of the collision cell size.*/
284  unsigned int mpcSimulationBoxSizeY; /**< The length of the MPC simulation box in the y direction,
285  in units of the collision cell size.*/
286  unsigned int mpcSimulationBoxSizeZ; /**< The length of the MPC simulation box in the z direction,
287  in units of the collision cell size.*/
288 
289  FP mpcTimestep; ///< The timestep for the MPC simulation.
290  FP srdCollisionAngle; ///< The collision angle for the SRD collision step.
291  FP gridShiftScale; ///< The factor the random grid shift vector is scaled with.
292 
293  FP bulkThermostatTargetkT;
294  /**< The product of Boltzmann's constant and the bulk
295  thermostat's target temperature, or `0` if no bulk
296  thermostat is configured.*/
297 
298  unsigned int mpcSweepSize; ///< The size of each MPC sweep.
299  unsigned int numberOfCompletedSweeps;
300  ///< The number of sweeps that have been completed.
301 
302 
303  MPCFluid::Base* mpcFluid; ///< The MPC fluid.
304 
306  mpcSolute; ///< The MPC solute.
307 
308  BoundaryCondition::Base* boundaryCondition;
309  ///< The boundary condition instance.
310 
311  const std::string rundir; ///< The directory where Simulation runs will be saved.
312 
313  mutable RNG rng; ///< The random number generator.
314  CUDA::GPURNG* gpurngs; ///< Points to Device memory containing RNGs.
315 
316 
317  FP mpcTime; ///< The MPC time that has passed since the start of the simulation.
318 
319  DeviceMemoryManager deviceMemoryManager; ///< The Device memory manager.
320 
321  MPCParticlePositionType* d_gridShift; ///< The grid shift vector on the Device.
322  MPCParticleVelocityType* d_leesEdwardsVelocityShift; ///< Temporary Device variable to store velocity corrections due to Lees-Edwards boundary conditions.
323  unsigned int* d_fluidCollisionCellIndices; ///< The collision cell indices for the fluid particles.
324  unsigned int* d_collisionCellParticleCounts; ///< The number of particles in the collision cells.
325  MPCParticleVelocityType* d_collisionCellMomenta; ///< The momenta in the collision cells.
326  MPCParticlePositionType* collisionCellRotationAxes; ///< The rotation axes in the collision cells on the Host.
327  MPCParticlePositionType* d_collisionCellRotationAxes; ///< The rotation axes in the collision cells on the Device.
328  FP* collisionCellRelativeVelocityScalings; ///< The relative velocity scaling factors in the collision cells on the Host.
329  FP* d_collisionCellRelativeVelocityScalings; ///< The relative velocity scaling factors in the collision cells on the Device.
330  FP* d_collisionCellFrameInternalKineticEnergies;
331  /**< Stores, for each collision cell, the sum of the kinetic
332  energies of the particles in that collision cell, as
333  measured in that collision cell's center-of-mass frame.*/
334  FP* d_collisionCellMasses; ///< The masses of the collision cells.
335 
336  MPCParticleVelocityType* d_leesEdwardsVelocityShift_solute;
337  /**< Temporary Device buffer to store Lees-Edwards velocity
338  shifts for solute particles.*/
339  unsigned int* d_collisionCellIndices_solute;
340  ///< The collision cell indices for the solute particles.
341  };
342 } //namespace OpenMPCD::CUDA
343 } //namespace OpenMPCD
344 
345 #endif
OpenMPCD::CUDA::Simulation::getNumberOfCompletedSweeps
unsigned int getNumberOfCompletedSweeps() const
Returns the number of times sweep has completed, not counting warmup sweeps as performed by warmup.
OpenMPCD::CUDA::MPCFluid::Base
Base class for MPC fluids.
Definition: CUDA/MPCFluid/Base.hpp:40
OpenMPCD::CUDA::Simulation::getSimulationBoxSizeZ
unsigned int getSimulationBoxSizeZ() const
Returns the size of the primary simulation box along the z direction.
Definition: CUDA/Simulation.hpp:121
OpenMPCD::CUDA::Simulation::hasSolute
bool hasSolute() const
Returns true if Simulation has an instance of MPCSolute::Base.
Definition: CUDA/Simulation.hpp:191
OpenMPCD::InvalidCallException
Exception for a forbidden function call.
Definition: Exceptions.hpp:144
OpenMPCD::CUDA::Simulation::getMPCFluid
const MPCFluid::Base & getMPCFluid() const
Returns the MPC fluid.
Definition: CUDA/Simulation.hpp:152
OpenMPCD::CUDA::Simulation::getSimulationBoxSizeY
unsigned int getSimulationBoxSizeY() const
Returns the size of the primary simulation box along the y direction.
Definition: CUDA/Simulation.hpp:113
OpenMPCD::Configuration
Represents the configuration of the simulation.
Definition: Configuration.hpp:28
OpenMPCD::CUDA::Simulation::Simulation
Simulation(const std::string &configurationFilename, const unsigned int rngSeed, const std::string &dir)
The constructor.
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::DeviceCode::mpcSimulationBoxSizeY
__constant__ unsigned int mpcSimulationBoxSizeY
The size of the primary simulation box along the y direction.
OpenMPCD::CUDA::DeviceCode::generateGridShiftVector
__global__ void generateGridShiftVector(MPCParticlePositionType *const gridShift, const FP gridShiftScale, GPURNG *const rngs)
Generates a new random grid shift vector.
OpenMPCD::CUDA::DeviceCode::generateCollisionCellRotationAxes
__global__ void generateCollisionCellRotationAxes(const unsigned int workUnitOffset, const unsigned int collisionCellCount, FP *const collisionCellRotationAxes, GPURNG *const rngs)
Generates new rotation axes for the collision cells.
OpenMPCD::CUDA::Simulation::~Simulation
~Simulation()
The destructor.
OpenMPCD::CUDA::Simulation::getBoundaryConditions
const BoundaryCondition::Base * getBoundaryConditions() const
Returns the boundary conditions.
Definition: CUDA/Simulation.hpp:182
OpenMPCD::CUDA::DeviceMemoryManager
Class for managing memory on the CUDA Device.
Definition: DeviceMemoryManager.hpp:21
OpenMPCD::CUDA::Simulation::getRundir
std::string getRundir() const
Returns the directory containing the Simulation run data.
Definition: CUDA/Simulation.hpp:216
Base.hpp
OpenMPCD::CUDA::DeviceCode::srdCollisionAngle
__constant__ FP srdCollisionAngle
The collision angle for SRD collisions.
Base.hpp
OpenMPCD::CUDA::Simulation::hasMPCFluid
bool hasMPCFluid() const
Returns whether an MPC fluid has been configured.
Definition: CUDA/Simulation.hpp:141
RemotelyStoredVector.hpp
OpenMPCD::CUDA::Simulation::getSimulationBoxSizeX
unsigned int getSimulationBoxSizeX() const
Returns the size of the primary simulation box along the x direction.
Definition: CUDA/Simulation.hpp:105
Types.hpp
OpenMPCD::CUDA::DeviceCode::mpcSimulationBoxSizeX
__constant__ unsigned int mpcSimulationBoxSizeX
The size of the primary simulation box along the x direction.
OpenMPCD::CUDA::Simulation::getMPCSolute
const MPCSolute::Base< MPCParticlePositionType, MPCParticleVelocityType > & getMPCSolute() const
Returns the MPC solute.
Definition: CUDA/Simulation.hpp:169
OpenMPCD::MPCParticlePositionType
FP MPCParticlePositionType
The data type for the positions of MPC particles.
Definition: Types.hpp:15
OpenMPCD::CUDA::Simulation::getCollisionCellCount
unsigned int getCollisionCellCount() const
Returns the number of collision cells.
Definition: CUDA/Simulation.hpp:129
OpenMPCD::CUDA::MPCSolute::Base
Base class for MPC solutes.
Definition: CUDA/MPCSolute/Base.hpp:35
OpenMPCD::CUDA::Simulation::getConfiguration
const Configuration & getConfiguration() const
Returns the configuration.
Definition: CUDA/Simulation.hpp:81
OpenMPCD::CUDA::Simulation::getMPCTime
FP getMPCTime() const
Returns the MPC time that has passed since the start of the simulation.
Definition: CUDA/Simulation.hpp:200
Base.hpp
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
OpenMPCD::CUDA::DeviceCode::mpcSimulationBoxSizeZ
__constant__ unsigned int mpcSimulationBoxSizeZ
The size of the primary simulation box along the z direction.
Vector3D.hpp
OPENMPCD_DEBUG_ASSERT.hpp
OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE
#define OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE(assertion, ExceptionType)
Definition: OPENMPCD_DEBUG_ASSERT.hpp:76
OpenMPCD::FP
double FP
Default floating point type.
Definition: Types.hpp:13
OpenMPCD::CUDA::Simulation::getDeviceMemoryManager
DeviceMemoryManager * getDeviceMemoryManager()
Returns the Device memory manager.
Definition: CUDA/Simulation.hpp:224
Types.hpp
OpenMPCD::MPCParticleVelocityType
FP MPCParticleVelocityType
The data type for the velocities of MPC particles.
Definition: Types.hpp:16
OpenMPCD::CUDA::DeviceCode::generateCollisionCellMBSFactors
__global__ void generateCollisionCellMBSFactors(const unsigned int workUnitOffset, const unsigned int collisionCellCount, const FP *const collisionCellFrameInternalKineticEnergies, unsigned int *const collisionCellParticleCounts, FP *const collisionCellRelativeVelocityScalings, const FP bulkThermostatTargetkT, GPURNG *const rngs)
Generates Maxwell-Boltzmann-Scaling factors for the collision cells.
OpenMPCD::CUDA::Simulation::getMPCTimestep
FP getMPCTimestep() const
Returns the MPC timestep.
Definition: CUDA/Simulation.hpp:208
Configuration.hpp
OpenMPCD::CUDA::Simulation::warmup
void warmup()
Performs the warmup step.
OpenMPCD::CUDA::Simulation::sweep
void sweep()
Performs a sweep.
OpenMPCD::CUDA::BoundaryCondition::Base
Base class for boundary condition.
Definition: CUDA/BoundaryCondition/Base.hpp:26
OpenMPCD::CUDA::Random::Generators::Philox4x32_10
Philox4x32-10 counter-bases PRNG.
Definition: Philox4x32-10.hpp:30