Molecular Dynamics Simulation  1.0
AbsoluteThermostat.cpp
Go to the documentation of this file.
1 #include "AbsoluteThermostat.h"
2 
3 #include <cmath>
4 
5 #include "utils/ArrayUtils.h"
6 
7 AbsoluteThermostat::AbsoluteThermostat(double target_temperature, double max_temperature_change, ThirdDimension third_dimension)
8  : Thermostat(target_temperature, max_temperature_change, third_dimension) {}
9 
10 void AbsoluteThermostat::scaleTemperature(const std::unique_ptr<ParticleContainer>& particle_container) const {
11  const double current_temperature = getCurrentContainerTemperature(particle_container);
12  const double temperature_change = std::min(std::abs(target_temperature - current_temperature), max_temperature_change);
13  const double new_temperature = current_temperature + temperature_change * (target_temperature > current_temperature ? 1 : -1);
14 
15  const double scaling_factor = std::sqrt(new_temperature / current_temperature);
16 
17  for (auto& particle : *particle_container) {
18  if (particle.isLocked()) continue;
19  particle.setV(scaling_factor * particle.getV());
20  }
21 }
22 
23 double AbsoluteThermostat::getContainerKineticEnergy(const std::unique_ptr<ParticleContainer>& particle_container) const {
24  double total_kinetic_energy = 0;
25  for (auto& particle : *particle_container) {
26  if (particle.isLocked()) continue;
27  std::array<double, 3> v = particle.getV();
28  total_kinetic_energy += particle.getM() * (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]) * 0.5;
29  }
30  return total_kinetic_energy;
31 }
32 
33 double AbsoluteThermostat::getCurrentContainerTemperature(const std::unique_ptr<ParticleContainer>& particle_container) const {
34  double dimension_count = third_dimension == ThirdDimension::ENABLED ? 3 : 2;
35  return 2 * getContainerKineticEnergy(particle_container) / (dimension_count * particle_container->size());
36 }
ThirdDimension
Enum class to define the dimension count of the simulation (2D or 3D). Affects primarily the dimensio...
Definition: Enums.h:7
double getCurrentContainerTemperature(const std::unique_ptr< ParticleContainer > &particle_container) const override
Get the current temperature of a particle container.
double getContainerKineticEnergy(const std::unique_ptr< ParticleContainer > &particle_container) const
Get the kinetic energy of all particles in a particle container.
AbsoluteThermostat(double target_temperature, double max_temperature_change=std::numeric_limits< double >::max(), ThirdDimension third_dimension=ThirdDimension::ENABLED)
Construct a new Thermostat object.
void scaleTemperature(const std::unique_ptr< ParticleContainer > &particle_container) const override
Scale the temperature of a particle container towards the target temperature. Capped by the maximum t...
A thermostat that can be used to control the temperature of a particle container. Allows for gradual ...
Definition: Thermostat.h:13
const double max_temperature_change
The maximum temperature change allowed per thermostat application.
Definition: Thermostat.h:94
const double target_temperature
The target temperature for thermostat applications.
Definition: Thermostat.h:89
const ThirdDimension third_dimension
Defines whether the third dimension is enabled.
Definition: Thermostat.h:99