Molecular Dynamics Simulation  1.0
Public Member Functions | List of all members
RelativeThermostat Class Reference

A relative thermostat that can be used to control the temperature of a particle container. Allows for gradual scaling of the temperature towards a set target. More...

#include <RelativeThermostat.h>

Inheritance diagram for RelativeThermostat:
Inheritance graph
Collaboration diagram for RelativeThermostat:
Collaboration graph

Public Member Functions

 RelativeThermostat (double target_temperature, double max_temperature_change=std::numeric_limits< double >::max(), ThirdDimension third_dimension=ThirdDimension::ENABLED)
 Construct a new Thermostat object. More...
 
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 temperature change. More...
 
double getContainerKineticEnergy (const std::unique_ptr< ParticleContainer > &particle_container, std::array< double, 3 > average_velocity) const
 Get the kinetic energy of a particle. More...
 
double getCurrentContainerTemperature (const std::unique_ptr< ParticleContainer > &particle_container, std::array< double, 3 > average_velocity) const
 Get the current temperature of a particle. More...
 
double getCurrentContainerTemperature (const std::unique_ptr< ParticleContainer > &particle_container) const override
 Get the current temperature of a particle container. More...
 
- Public Member Functions inherited from Thermostat
 Thermostat (double target_temperature, double max_temperature_change, ThirdDimension third_dimension)
 Construct a new Thermostat object. More...
 
virtual ~Thermostat ()=default
 Destroy the Thermostat object. More...
 
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 in the container to a random value according to the Maxwell-Boltzmann distribution (all previous velocities are discarded). Use this function for systems with no initial velocity. More...
 
double getTargetTemperature () const
 Get the target temperature of the thermostat. More...
 
double getMaxTemperatureChange () const
 Get the maximum temperature change of the thermostat. More...
 
ThirdDimension getThirdDimension () const
 Get if the third dimension is enabled on this thermostat or not. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Thermostat
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 the Maxwell-Boltzmann distribution. This function can be used for particle generators since previous velocities are not discarded. More...
 
- Protected Attributes inherited from Thermostat
const double target_temperature
 The target temperature for thermostat applications. More...
 
const double max_temperature_change
 The maximum temperature change allowed per thermostat application. More...
 
const ThirdDimension third_dimension
 Defines whether the third dimension is enabled. More...
 

Detailed Description

A relative thermostat that can be used to control the temperature of a particle container. Allows for gradual scaling of the temperature towards a set target.

This thermostat is a basic implementation of the Thermostat interface.

This Thermostats is "relative" in the sense that it takes the actual "average" velocity of the particles into account when scaling the temperature. This means that the particles jiggle gets reduced, but in total the particles will still move if given an initial velocity.

Definition at line 19 of file RelativeThermostat.h.

Constructor & Destructor Documentation

◆ RelativeThermostat()

RelativeThermostat::RelativeThermostat ( double  target_temperature,
double  max_temperature_change = std::numeric_limits<double>::max(),
ThirdDimension  third_dimension = ThirdDimension::ENABLED 
)

Construct a new Thermostat object.

Parameters
target_temperatureThe target temperature for thermostat applications.
max_temperature_changeThe maximum temperature change allowed per thermostat application.
third_dimensionWether the thermostat applies to a 3-dimensional domain or 2 dimensions.

Definition at line 7 of file RelativeThermostat.cpp.

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
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

Member Function Documentation

◆ getContainerKineticEnergy()

double RelativeThermostat::getContainerKineticEnergy ( const std::unique_ptr< ParticleContainer > &  particle_container,
std::array< double, 3 >  average_velocity 
) const

Get the kinetic energy of a particle.

Parameters
particle_containerThe particle container to get the kinetic energy of.
average_velocityThe average velocity of all particles in the container.
Returns
double The kinetic energy of the particle.

Definition at line 36 of file RelativeThermostat.cpp.

37  {
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 }

◆ getCurrentContainerTemperature() [1/2]

double RelativeThermostat::getCurrentContainerTemperature ( const std::unique_ptr< ParticleContainer > &  particle_container) const
overridevirtual

Get the current temperature of a particle container.

Parameters
particle_containerThe particle container to get the temperature of.
Returns
double The current temperature of the particle container.

Implements Thermostat.

Definition at line 53 of file RelativeThermostat.cpp.

53  {
54  return getCurrentContainerTemperature(particle_container, averageVelocity(particle_container));
55 }
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.

◆ getCurrentContainerTemperature() [2/2]

double RelativeThermostat::getCurrentContainerTemperature ( const std::unique_ptr< ParticleContainer > &  particle_container,
std::array< double, 3 >  average_velocity 
) const

Get the current temperature of a particle.

Parameters
particle_containerThe particle container to get the temperature of.
average_velocityThe average velocity of all particles in the container.

Definition at line 47 of file RelativeThermostat.cpp.

48  {
49  double dimension_count = third_dimension == ThirdDimension::ENABLED ? 3 : 2;
50  return 2 * getContainerKineticEnergy(particle_container, average_velocity) / (dimension_count * particle_container->size());
51 }
double getContainerKineticEnergy(const std::unique_ptr< ParticleContainer > &particle_container, std::array< double, 3 > average_velocity) const
Get the kinetic energy of a particle.

◆ scaleTemperature()

void RelativeThermostat::scaleTemperature ( const std::unique_ptr< ParticleContainer > &  particle_container) const
overridevirtual

Scale the temperature of a particle container towards the target temperature. Capped by the maximum temperature change.

Parameters
particle_containerThe particle container to scale the temperature of.

Implements Thermostat.

Definition at line 21 of file RelativeThermostat.cpp.

21  {
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 }

The documentation for this class was generated from the following files: