Molecular Dynamics Simulation  1.0
Public Member Functions | Private Member Functions | Private Attributes | List of all members
DiffusionFunctionInterceptor Class Reference

#include <DiffusionFunctionInterceptor.h>

Inheritance diagram for DiffusionFunctionInterceptor:
Inheritance graph
Collaboration diagram for DiffusionFunctionInterceptor:
Collaboration graph

Public Member Functions

 DiffusionFunctionInterceptor (double sample_every_x_percent)
 Construct a new Thermostat Interceptor object. More...
 
void onSimulationStart (Simulation &simulation) override
 This function is sets the particle_updates to 0 and initializes the start time of the simulation. More...
 
void operator() (size_t iteration, Simulation &simulation) override
 This function is called on every nth iteration. It counts the number of particle updates which have been performed. More...
 
void onSimulationEnd (size_t iteration, Simulation &simulation) override
 This function is empty as the thermostat doesnt need to do anything at the end of the simulation. More...
 
 operator std::string () const override
 The string representation of this interceptor. More...
 
void logSummary (int depth) const override
 Logs the summary of the radial distribution function. More...
 
- Public Member Functions inherited from SimulationInterceptor
virtual ~SimulationInterceptor ()=default
 Destroy the Simulation Interceptor object. More...
 
void notify (size_t iteration, Simulation &simulation)
 This function is called by the simulation loop on every iteration. Whenever the iteration is a multiple of every_nth_iteration, the operator() function is called. More...
 

Private Member Functions

void saveCurrentParticlePositions (const Simulation &simulation)
 
double calculateCurrentDiffusion (const Simulation &simulation) const
 

Private Attributes

double sample_every_x_percent
 
std::unique_ptr< CSVWritercsv_writer
 
std::vector< std::array< double, 3 > > old_particle_positions
 
size_t last_sampled_iteration = 0
 

Additional Inherited Members

- Protected Attributes inherited from SimulationInterceptor
size_t every_nth_iteration = 1
 

Detailed Description

Definition at line 9 of file DiffusionFunctionInterceptor.h.

Constructor & Destructor Documentation

◆ DiffusionFunctionInterceptor()

DiffusionFunctionInterceptor::DiffusionFunctionInterceptor ( double  sample_every_x_percent)
inlineexplicit

Construct a new Thermostat Interceptor object.

Definition at line 14 of file DiffusionFunctionInterceptor.h.

Member Function Documentation

◆ calculateCurrentDiffusion()

double DiffusionFunctionInterceptor::calculateCurrentDiffusion ( const Simulation simulation) const
private

Definition at line 63 of file DiffusionFunctionInterceptor.cpp.

63  {
64  double total_displacement = 0;
65 
66  for (size_t i = 0; i < simulation.particle_container->size(); i++) {
67  auto& particle = simulation.particle_container->getParticles()[i];
68  auto& old_particle_position = old_particle_positions[i];
69 
70  std::array<double, 3> displacement = particle.getX() - old_particle_position;
71 
72  // detect if particle has moved over the periodic boundary
73  if (std::holds_alternative<SimulationParams::LinkedCellsType>(simulation.params.container_type)) {
74  auto& container = std::get<SimulationParams::LinkedCellsType>(simulation.params.container_type);
75  auto& box_size = container.domain_size;
76 
77  for (size_t i = 0; i < displacement.size(); i++) {
78  if (std::abs(displacement[i]) > box_size[i] / 2) {
79  displacement[i] = box_size[i] - std::abs(displacement[i]);
80  }
81  }
82  }
83 
84  double distance = std::pow(ArrayUtils::L2Norm(displacement), 2);
85 
86  total_displacement += distance;
87  }
88 
89  double var_t = total_displacement / simulation.particle_container->size();
90 
91  return var_t;
92 }
std::vector< std::array< double, 3 > > old_particle_positions
std::variant< DirectSumType, LinkedCellsType > container_type
Type of the particle container.
std::unique_ptr< ParticleContainer > particle_container
Reference to the ParticleContainer on whose content the simulation is performed.
Definition: Simulation.h:50
const SimulationParams & params
Reference to the simulation parameters object.
Definition: Simulation.h:45
auto L2Norm(const Container &c)
Definition: ArrayUtils.h:174

◆ logSummary()

void DiffusionFunctionInterceptor::logSummary ( int  depth) const
overridevirtual

Logs the summary of the radial distribution function.

Implements SimulationInterceptor.

Definition at line 47 of file DiffusionFunctionInterceptor.cpp.

47  {
48  std::string indent = std::string(depth * 2, ' ');
49 
50  Logger::logger->info("{}╟┤{}DiffusionFunctionInterceptor: {}", indent, ansi_orange_bold, ansi_end);
51  Logger::logger->info("{}║ ├Sample every x percent: {}", indent, sample_every_x_percent);
52 }
const std::string ansi_end
Definition: Logger.h:13
const std::string ansi_orange_bold
Definition: Logger.h:10
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.
Definition: Logger.h:35

◆ onSimulationEnd()

void DiffusionFunctionInterceptor::onSimulationEnd ( size_t  iteration,
Simulation simulation 
)
overridevirtual

This function is empty as the thermostat doesnt need to do anything at the end of the simulation.

Parameters
iterationThe current iteration
simulationThe simulation object

Implements SimulationInterceptor.

Definition at line 40 of file DiffusionFunctionInterceptor.cpp.

40  {
41  if (iteration != last_sampled_iteration) {
42  double var_t = calculateCurrentDiffusion(simulation);
43  csv_writer->writeRow({iteration, var_t});
44  }
45 }
std::unique_ptr< CSVWriter > csv_writer
double calculateCurrentDiffusion(const Simulation &simulation) const

◆ onSimulationStart()

void DiffusionFunctionInterceptor::onSimulationStart ( Simulation simulation)
overridevirtual

This function is sets the particle_updates to 0 and initializes the start time of the simulation.

Parameters
simulationThe simulation object

Implements SimulationInterceptor.

Definition at line 10 of file DiffusionFunctionInterceptor.cpp.

10  {
11  bool append = simulation.params.start_iteration != 0;
12 
13  csv_writer = std::make_unique<CSVWriter>(simulation.params.output_dir_path / "statistics" / "diffusion_function.csv", append);
14 
15  csv_writer->initialize({"iteration", "var(t)"});
16 
17  auto expected_iterations = static_cast<size_t>(std::ceil(simulation.params.end_time / simulation.params.delta_t) + 1);
18  SimulationInterceptor::every_nth_iteration = std::max(1, static_cast<int>(sample_every_x_percent * expected_iterations / 100));
19 
20  if (simulation.params.start_iteration == 0) {
21  saveCurrentParticlePositions(simulation);
22  }
25 }
void saveCurrentParticlePositions(const Simulation &simulation)
double delta_t
Time step of a single simulation iteration.
std::filesystem::path output_dir_path
Path to the directory in which to save the simulation output.
size_t start_iteration
Start iteration of the simulation.
double end_time
End time of the simulation.

◆ operator std::string()

DiffusionFunctionInterceptor::operator std::string ( ) const
explicitoverridevirtual

The string representation of this interceptor.

Returns
std::string

This is used to write the final summary of the Interceptors to the console.

Implements SimulationInterceptor.

Definition at line 54 of file DiffusionFunctionInterceptor.cpp.

54 { return ""; }

◆ operator()()

void DiffusionFunctionInterceptor::operator() ( size_t  iteration,
Simulation simulation 
)
overridevirtual

This function is called on every nth iteration. It counts the number of particle updates which have been performed.

Parameters
iterationThe current iteration
simulationThe simulation object

Implements SimulationInterceptor.

Definition at line 27 of file DiffusionFunctionInterceptor.cpp.

27  {
28  if (simulation.particle_container->size() != old_particle_positions.size()) {
29  throw std::runtime_error(
30  "Particle container size changed during simulation. This is not supported by the diffusion function interceptor.");
31  }
32 
33  double var_t = calculateCurrentDiffusion(simulation);
34  csv_writer->writeRow({iteration, var_t});
35 
36  saveCurrentParticlePositions(simulation);
37  last_sampled_iteration = iteration;
38 }

◆ saveCurrentParticlePositions()

void DiffusionFunctionInterceptor::saveCurrentParticlePositions ( const Simulation simulation)
private

Definition at line 56 of file DiffusionFunctionInterceptor.cpp.

56  {
57  old_particle_positions.clear();
58  for (auto& particle : *simulation.particle_container) {
59  old_particle_positions.push_back(particle.getX());
60  }
61 }

Member Data Documentation

◆ csv_writer

std::unique_ptr<CSVWriter> DiffusionFunctionInterceptor::csv_writer
private

Definition at line 59 of file DiffusionFunctionInterceptor.h.

◆ last_sampled_iteration

size_t DiffusionFunctionInterceptor::last_sampled_iteration = 0
private

Definition at line 61 of file DiffusionFunctionInterceptor.h.

◆ old_particle_positions

std::vector<std::array<double, 3> > DiffusionFunctionInterceptor::old_particle_positions
private

Definition at line 60 of file DiffusionFunctionInterceptor.h.

◆ sample_every_x_percent

double DiffusionFunctionInterceptor::sample_every_x_percent
private

Definition at line 58 of file DiffusionFunctionInterceptor.h.


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