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

#include <RadialDistributionFunctionInterceptor.h>

Inheritance diagram for RadialDistributionFunctionInterceptor:
Inheritance graph
Collaboration diagram for RadialDistributionFunctionInterceptor:
Collaboration graph

Public Member Functions

 RadialDistributionFunctionInterceptor (double bin_width, 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...
 
double calculateLocalDensity (size_t N, size_t bin_index) const
 
- 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 saveCurrentRadialDistribution (size_t iteration, Simulation &simulation)
 

Private Attributes

double bin_width
 
double sample_every_x_percent
 
std::unique_ptr< CSVWritercsv_writer
 

Additional Inherited Members

- Protected Attributes inherited from SimulationInterceptor
size_t every_nth_iteration = 1
 

Detailed Description

Definition at line 8 of file RadialDistributionFunctionInterceptor.h.

Constructor & Destructor Documentation

◆ RadialDistributionFunctionInterceptor()

RadialDistributionFunctionInterceptor::RadialDistributionFunctionInterceptor ( double  bin_width,
double  sample_every_x_percent 
)
inline

Member Function Documentation

◆ calculateLocalDensity()

double RadialDistributionFunctionInterceptor::calculateLocalDensity ( size_t  N,
size_t  bin_index 
) const

Definition at line 43 of file RadialDistributionFunctionInterceptor.cpp.

43  {
44  double bin_start = bin_index * bin_width;
45  double bin_end = (bin_index + 1) * bin_width;
46 
47  double bin_volume = 4.0 / 3.0 * M_PI * (std::pow(bin_end, 3) - std::pow(bin_start, 3));
48 
49  return N / bin_volume;
50 }

◆ logSummary()

void RadialDistributionFunctionInterceptor::logSummary ( int  depth) const
overridevirtual

Logs the summary of the radial distribution function.

Implements SimulationInterceptor.

Definition at line 31 of file RadialDistributionFunctionInterceptor.cpp.

31  {
32  std::string indent = std::string(depth * 2, ' ');
33 
34  Logger::logger->info("{}╟┤{}RadialDistributionFunction: {}", indent, ansi_orange_bold, ansi_end);
35  Logger::logger->info("{}║ ┌Bin length: {}", indent, bin_width);
36  Logger::logger->info("{}║ └Sample every x percent: {}", indent, sample_every_x_percent);
37 }
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 RadialDistributionFunctionInterceptor::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 27 of file RadialDistributionFunctionInterceptor.cpp.

27  {
28  saveCurrentRadialDistribution(iteration, simulation);
29 }
void saveCurrentRadialDistribution(size_t iteration, Simulation &simulation)

◆ onSimulationStart()

void RadialDistributionFunctionInterceptor::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 8 of file RadialDistributionFunctionInterceptor.cpp.

8  {
9  bool append = simulation.params.start_iteration != 0;
10 
11  csv_writer = std::make_unique<CSVWriter>(simulation.params.output_dir_path / "statistics" / "radial_distribution_function.csv", append);
12 
13  csv_writer->initialize({"iteration", "bin_index (w= " + std::to_string(bin_width) + ")", "samples", "local_density"});
14 
15  auto expected_iterations = static_cast<size_t>(std::ceil(simulation.params.end_time / simulation.params.delta_t));
16  SimulationInterceptor::every_nth_iteration = std::max(1, static_cast<int>(sample_every_x_percent * expected_iterations / 100));
17 
18  if (simulation.params.start_iteration == 0) {
19  saveCurrentRadialDistribution(0, simulation);
20  }
21 }
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.
const SimulationParams & params
Reference to the simulation parameters object.
Definition: Simulation.h:45
std::string to_string(const Container &container, const std::string &delimiter=", ", const std::array< std::string, 2 > &surround={"[", "]"})
Definition: ArrayUtils.h:97

◆ operator std::string()

RadialDistributionFunctionInterceptor::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 39 of file RadialDistributionFunctionInterceptor.cpp.

39  {
40  return fmt::format("RadialDistributionFunction: bin_width={}, sample_every_x_percent={}", bin_width, sample_every_x_percent);
41 }

◆ operator()()

void RadialDistributionFunctionInterceptor::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 23 of file RadialDistributionFunctionInterceptor.cpp.

23  {
24  saveCurrentRadialDistribution(iteration, simulation);
25 }

◆ saveCurrentRadialDistribution()

void RadialDistributionFunctionInterceptor::saveCurrentRadialDistribution ( size_t  iteration,
Simulation simulation 
)
private

Definition at line 52 of file RadialDistributionFunctionInterceptor.cpp.

52  {
53  std::map<size_t, size_t> samples_per_bin_index;
54 
55  for (auto it1 = simulation.particle_container->begin(); it1 != simulation.particle_container->end(); it1++) {
56  auto& particle = *it1;
57  for (auto it2 = it1 + 1; it2 != simulation.particle_container->end(); it2++) {
58  auto& other_particle = *it2;
59 
60  if (particle == other_particle) continue;
61 
62  double distance = ArrayUtils::L2Norm(particle.getX() - other_particle.getX());
63 
64  size_t bin_index = std::floor(distance / bin_width);
65  samples_per_bin_index[bin_index]++;
66  }
67  }
68 
69  if (samples_per_bin_index.rbegin() != samples_per_bin_index.rend()) {
70  for (size_t i = 0; i <= samples_per_bin_index.rbegin()->first; i++) {
71  if (samples_per_bin_index.find(i) == samples_per_bin_index.end()) {
72  samples_per_bin_index[i] = 0;
73  }
74  }
75  }
76 
77  for (auto& [bin_index, samples] : samples_per_bin_index) {
78  csv_writer->writeRow({iteration, bin_index, samples, calculateLocalDensity(samples, bin_index)});
79  }
80 }
double calculateLocalDensity(size_t N, size_t bin_index) const
std::unique_ptr< ParticleContainer > particle_container
Reference to the ParticleContainer on whose content the simulation is performed.
Definition: Simulation.h:50
auto L2Norm(const Container &c)
Definition: ArrayUtils.h:174

Member Data Documentation

◆ bin_width

double RadialDistributionFunctionInterceptor::bin_width
private

Definition at line 63 of file RadialDistributionFunctionInterceptor.h.

◆ csv_writer

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

Definition at line 65 of file RadialDistributionFunctionInterceptor.h.

◆ sample_every_x_percent

double RadialDistributionFunctionInterceptor::sample_every_x_percent
private

Definition at line 64 of file RadialDistributionFunctionInterceptor.h.


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