Molecular Dynamics Simulation  1.0
RelativeThermostat.cpp
Go to the documentation of this file.
1 #include "RelativeThermostat.h"
2 
3 #include <cmath>
4 
5 #include "utils/ArrayUtils.h"
6 
7 RelativeThermostat::RelativeThermostat(double target_temperature, double max_temperature_change, ThirdDimension third_dimension)
8  : Thermostat(target_temperature, max_temperature_change, third_dimension) {}
9 
10 std::array<double, 3> averageVelocity(const std::unique_ptr<ParticleContainer>& particle_container) {
11  std::array<double, 3> average_velocity = {0, 0, 0};
12  size_t count = 0;
13  for (auto& particle : *particle_container) {
14  if (particle.isLocked()) continue;
15  average_velocity = average_velocity + particle.getV();
16  count++;
17  }
18  average_velocity = (1.0 / count) * average_velocity;
19  return average_velocity;
20 }
21 void RelativeThermostat::scaleTemperature(const std::unique_ptr<ParticleContainer>& particle_container) const {
22  const std::array<double, 3> average_velocity = averageVelocity(particle_container);
23 
24  const double current_temperature = getCurrentContainerTemperature(particle_container, average_velocity);
25  const double temperature_change = std::min(std::abs(target_temperature - current_temperature), max_temperature_change);
26  const double new_temperature = current_temperature + temperature_change * (target_temperature > current_temperature ? 1 : -1);
27 
28  const double scaling_factor = std::sqrt(new_temperature / current_temperature);
29 
30  for (auto& particle : *particle_container) {
31  if (particle.isLocked()) continue;
32  particle.setV(average_velocity + scaling_factor * (particle.getV() - average_velocity));
33  }
34 }
35 
36 double RelativeThermostat::getContainerKineticEnergy(const std::unique_ptr<ParticleContainer>& particle_container,
37  std::array<double, 3> average_velocity) const {
38  double total_kinetic_energy = 0;
39  for (auto& particle : *particle_container) {
40  if (particle.isLocked()) continue;
41  std::array<double, 3> v = particle.getV() - average_velocity;
42  total_kinetic_energy += particle.getM() * (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]) * 0.5;
43  }
44  return total_kinetic_energy;
45 }
46 
47 double RelativeThermostat::getCurrentContainerTemperature(const std::unique_ptr<ParticleContainer>& particle_container,
48  std::array<double, 3> average_velocity) const {
49  double dimension_count = third_dimension == ThirdDimension::ENABLED ? 3 : 2;
50  return 2 * getContainerKineticEnergy(particle_container, average_velocity) / (dimension_count * particle_container->size());
51 }
52 
53 double RelativeThermostat::getCurrentContainerTemperature(const std::unique_ptr<ParticleContainer>& particle_container) const {
54  return getCurrentContainerTemperature(particle_container, averageVelocity(particle_container));
55 }
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 > averageVelocity(const std::unique_ptr< ParticleContainer > &particle_container)
double getCurrentContainerTemperature(const std::unique_ptr< ParticleContainer > &particle_container, std::array< double, 3 > average_velocity) const
Get the current temperature of a particle.
double getContainerKineticEnergy(const std::unique_ptr< ParticleContainer > &particle_container, std::array< double, 3 > average_velocity) const
Get the kinetic energy of a particle.
RelativeThermostat(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