Molecular Dynamics Simulation  1.0
SphereSpawner.cpp
Go to the documentation of this file.
1 #include "SphereSpawner.h"
2 
3 #include "particles/Particle.h"
5 #include "utils/ArrayUtils.h"
6 
7 SphereSpawner::SphereSpawner(const std::array<double, 3>& center, const int sphere_radius, double grid_spacing, double mass,
8  const std::array<double, 3>& initial_velocity, int type, double epsilon, double sigma, LockState lock_state,
9  ThirdDimension third_dimension, double initial_temperature)
10  : center(center),
11  sphere_radius(sphere_radius),
12  grid_spacing(grid_spacing),
13  mass(mass),
14  type(type),
15  espilon(epsilon),
16  sigma(sigma),
17  lock_state(lock_state),
18  initial_velocity(initial_velocity),
19  initial_temperature(initial_temperature),
20  third_dimension(third_dimension) {}
21 
22 int SphereSpawner::spawnParticles(std::vector<Particle>& particles) const {
23  particles.reserve(particles.size() + getEstimatedNumberOfParticles());
24  int num_particles_spawned = 0;
25  for (int x = -sphere_radius; x <= sphere_radius; x++) {
26  for (int y = -sphere_radius; y <= sphere_radius; y++) {
27  for (int z = -sphere_radius; z <= sphere_radius; z++) {
28  if (third_dimension == ThirdDimension::DISABLED && z != 0) {
29  continue;
30  }
31 
32  const auto displacement = std::array<double, 3>{x * grid_spacing, y * grid_spacing, z * grid_spacing};
33  const double dist = ArrayUtils::L2Norm(displacement);
34 
35  if (dist <= sphere_radius * grid_spacing) {
36  const auto position = center + displacement;
37  Particle particle(position, initial_velocity, mass, type, espilon, sigma, lock_state);
39 
40  particles.push_back(std::move(particle));
41  num_particles_spawned++;
42  }
43  }
44  }
45  }
46  return num_particles_spawned;
47 }
48 
49 size_t SphereSpawner::getEstimatedNumberOfParticles() const { return static_cast<size_t>(4.0 / 3.0 * M_PI * std::pow(sphere_radius, 3)); }
ThirdDimension
Enum class to define the dimension count of the simulation (2D or 3D). Affects primarily the dimensio...
Definition: Enums.h:7
LockState
Enum class to define the lockstate of particles. Locked particles are not allowed to move.
Definition: Enums.h:12
Class to represent a particle.
Definition: Particle.h:26
const std::array< double, 3 > center
Defines the center of the sphere.
Definition: SphereSpawner.h:20
const double grid_spacing
Defines the spacing between neighboring particles in the sphere.
Definition: SphereSpawner.h:30
const double initial_temperature
Defines the initial temperature of the particles in the sphere.
Definition: SphereSpawner.h:65
const double espilon
Defines the Lennard-Jones epsilon parameter of the particles in the sphere.
Definition: SphereSpawner.h:45
const std::array< double, 3 > initial_velocity
Defines the initial velocity of the particles in the sphere.
Definition: SphereSpawner.h:60
SphereSpawner(const std::array< double, 3 > &center, int sphere_radius, double grid_spacing, double mass, const std::array< double, 3 > &initial_velocity, int type, double epsilon=1.0, double sigma=1.2, LockState lock_state=LockState::UNLOCKED, ThirdDimension third_dimension=ThirdDimension::ENABLED, double initial_temperature=0.1)
Constructor.
const int sphere_radius
Defines the radius of the sphere.
Definition: SphereSpawner.h:25
const LockState lock_state
Defines whether the particles in the sphere are locked.
Definition: SphereSpawner.h:55
const double mass
Defines the mass of the particles in the sphere.
Definition: SphereSpawner.h:35
const ThirdDimension third_dimension
Defines whether to use the third dimension.
Definition: SphereSpawner.h:70
const int type
Defines the type of the particles in the sphere.
Definition: SphereSpawner.h:40
const double sigma
Defines the Lennard-Jones sigma parameter of the particles in the sphere.
Definition: SphereSpawner.h:50
size_t getEstimatedNumberOfParticles() const override
Returns the number of particles to be spawned.
int spawnParticles(std::vector< Particle > &particles) const override
Spawns particles in the given container.
static void setParticleTemperature(double new_temperature, Particle &particle, ThirdDimension third_dimension)
Set the temperature of a particle. This method adds a random velocity to the particle according to th...
Definition: Thermostat.cpp:20
auto L2Norm(const Container &c)
Definition: ArrayUtils.h:174