Molecular Dynamics Simulation  1.0
SoftBodyCuboidSpawner.cpp
Go to the documentation of this file.
2 
3 #include <memory>
4 
5 #include "particles/Particle.h"
8 #include "utils/ArrayUtils.h"
9 
10 SoftBodyCuboidSpawner::SoftBodyCuboidSpawner(const std::array<double, 3>& lower_left_corner, const std::array<int, 3>& grid_dimensions,
11  double grid_spacing, double mass, const std::array<double, 3>& initial_velocity, int type,
12  double epsilon, double sigma, double spring_constant, ThirdDimension third_dimension,
13  double initial_temperature)
14  : lower_left_corner(lower_left_corner),
15  grid_dimensions(grid_dimensions),
16  grid_spacing(grid_spacing),
17  mass(mass),
18  type(type),
19  epsilon(epsilon),
20  sigma(sigma),
21  spring_constant(spring_constant),
22  initial_velocity(initial_velocity),
23  initial_temperature(initial_temperature),
24  third_dimension(third_dimension) {}
25 
26 int SoftBodyCuboidSpawner::spawnParticles(std::vector<Particle>& particles) const {
27  auto prev_size = particles.size();
28  particles.reserve(particles.size() + getEstimatedNumberOfParticles());
29  for (int i = 0; i < grid_dimensions[0]; i++) {
30  for (int j = 0; j < grid_dimensions[1]; j++) {
31  for (int k = 0; k < grid_dimensions[2]; k++) {
32  const auto grid_pos = std::array<double, 3>{static_cast<double>(i), static_cast<double>(j), static_cast<double>(k)};
33 
34  const auto x = lower_left_corner + grid_spacing * grid_pos;
35 
38  particles.push_back(std::move(particle));
39  }
40  }
41  }
42 
43  auto particle_pos_to_index = [this, &prev_size](const std::array<int, 3>& grid_pos) {
44  if (grid_pos[0] < 0 || grid_pos[0] >= grid_dimensions[0] || grid_pos[1] < 0 || grid_pos[1] >= grid_dimensions[1] ||
45  grid_pos[2] < 0 || grid_pos[2] >= grid_dimensions[2]) {
46  return -1;
47  }
48 
49  return static_cast<int>(prev_size) + grid_pos[2] + grid_dimensions[2] * grid_pos[1] +
50  grid_dimensions[2] * grid_dimensions[1] * grid_pos[0];
51  };
52 
53  // connect particles with harmonic springs
54  for (int i = 0; i < grid_dimensions[0]; i++) {
55  for (int j = 0; j < grid_dimensions[1]; j++) {
56  for (int k = 0; k < grid_dimensions[2]; k++) {
57  auto curr_index = particle_pos_to_index({i, j, k});
58  auto& curr_particle = particles[curr_index];
59 
60  for (int l = -1; l <= 1; l++) {
61  for (int m = -1; m <= 1; m++) {
62  for (int n = -1; n <= 1; n++) {
63  if (l == 0 && m == 0 && n == 0) {
64  continue;
65  }
66 
67  auto neighbor_index = particle_pos_to_index({i + l, j + m, k + n});
68 
69  if (neighbor_index == -1) {
70  continue;
71  }
72 
73  auto& neighbor_particle = particles[neighbor_index];
74 
75  auto initial_distance = ArrayUtils::L2Norm(curr_particle.getX() - neighbor_particle.getX());
76 
77  curr_particle.addConnectedParticle(&neighbor_particle - &curr_particle, initial_distance, spring_constant);
78  }
79  }
80  }
81  }
82  }
83  }
84 
86 }
87 
89  return static_cast<size_t>(grid_dimensions[0]) * grid_dimensions[1] * grid_dimensions[2];
90 }
ThirdDimension
Enum class to define the dimension count of the simulation (2D or 3D). Affects primarily the dimensio...
Definition: Enums.h:7
Class to represent a particle.
Definition: Particle.h:26
double sigma
Defines the Lennard-Jones sigma parameter of the particles in the cuboid.
const double initial_temperature
Defines the initial temperature of the particles in the cuboid.
const std::array< double, 3 > lower_left_corner
Defines the lower left corner where the cuboid will be spawned.
double epsilon
Defines the Lennard-Jones epsilon parameter of the particles in the cuboid.
size_t getEstimatedNumberOfParticles() const override
Estimate the number of particles to be spawned.
const int type
Defines the type of the particles in the cuboid.
const std::array< double, 3 > initial_velocity
Defines the initial velocity of the particles in the cuboid.
const ThirdDimension third_dimension
Defines whether the third dimension is enabled.
const double mass
Defines the mass of the particles in the cuboid.
const double grid_spacing
Defines the spacing between neighboring particles in the cuboid.
SoftBodyCuboidSpawner(const std::array< double, 3 > &lower_left_corner, const std::array< int, 3 > &grid_dimensions, double grid_spacing, double mass, const std::array< double, 3 > &initial_velocity, int type, double epsilon=1.0, double sigma=1.2, double spring_constant=300, ThirdDimension third_dimension=ThirdDimension::ENABLED, double initial_temperature=0.1)
Constructor.
int spawnParticles(std::vector< Particle > &particles) const override
Spawns particles in the given container.
const double spring_constant
Spring constant of the harmonic springs.
const std::array< int, 3 > grid_dimensions
Defines how big the cuboid will be. Each entry defines the number of particles in the respective dire...
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