Molecular Dynamics Simulation  1.0
Thermostat.cpp
Go to the documentation of this file.
1 #include "Thermostat.h"
2 
3 #include "io/logger/Logger.h"
4 #include "utils/ArrayUtils.h"
6 
7 Thermostat::Thermostat(double target_temperature, double max_temperature_change, ThirdDimension third_dimension)
8  : target_temperature(target_temperature), max_temperature_change(max_temperature_change), third_dimension(third_dimension) {
9  if (target_temperature < 0) {
10  Logger::logger->error("Target temperature must be positive");
11  throw std::runtime_error("Target temperature must be positive");
12  }
13 
14  if (max_temperature_change < 0) {
15  Logger::logger->error("Max temperature change must be an absolute value (positive)");
16  throw std::runtime_error("Max temperature change must be an absolute value (positive)");
17  }
18 }
19 
20 void Thermostat::setParticleTemperature(double new_temperature, Particle& particle, ThirdDimension third_dimension) {
21  size_t dimension_count = third_dimension == ThirdDimension::ENABLED ? 3 : 2;
22 
23  particle.setV(particle.getV() + maxwellBoltzmannDistributedVelocity(std::sqrt(new_temperature / particle.getM()), dimension_count));
24 }
25 
26 void Thermostat::setTemperature(double new_temperature, const std::unique_ptr<ParticleContainer>& particle_container) {
27  size_t dimension_count = third_dimension == ThirdDimension::ENABLED ? 3 : 2;
28 
29  for (auto& particle : *particle_container) {
30  particle.setV(maxwellBoltzmannDistributedVelocity(std::sqrt(new_temperature / particle.getM()), dimension_count));
31  }
32 }
33 
35 
37 
ThirdDimension
Enum class to define the dimension count of the simulation (2D or 3D). Affects primarily the dimensio...
Definition: Enums.h:7
std::array< double, 3 > maxwellBoltzmannDistributedVelocity(double averageVelocity, size_t dimensions)
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.
Definition: Logger.h:35
Class to represent a particle.
Definition: Particle.h:26
const std::array< double, 3 > & getV() const
Gets the velocity of the particle.
Definition: Particle.h:162
double getM() const
Gets the mass of the particle.
Definition: Particle.h:177
void setV(const std::array< double, 3 > &v)
Sets the velocity of the particle.
Definition: Particle.h:116
Thermostat(double target_temperature, double max_temperature_change, ThirdDimension third_dimension)
Construct a new Thermostat object.
Definition: Thermostat.cpp:7
const double max_temperature_change
The maximum temperature change allowed per thermostat application.
Definition: Thermostat.h:94
ThirdDimension getThirdDimension() const
Get if the third dimension is enabled on this thermostat or not.
Definition: Thermostat.cpp:38
const double target_temperature
The target temperature for thermostat applications.
Definition: Thermostat.h:89
void setTemperature(double new_temperature, const std::unique_ptr< ParticleContainer > &particle_container)
Set the initial temperature of a particle container. This function sets the velocity of all particles...
Definition: Thermostat.cpp:26
double getMaxTemperatureChange() const
Get the maximum temperature change of the thermostat.
Definition: Thermostat.cpp:36
const ThirdDimension third_dimension
Defines whether the third dimension is enabled.
Definition: Thermostat.h:99
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
double getTargetTemperature() const
Get the target temperature of the thermostat.
Definition: Thermostat.cpp:34