Molecular Dynamics Simulation  1.0
RadialDistributionFunctionInterceptor.cpp
Go to the documentation of this file.
2 
3 #include <chrono>
4 
6 #include "utils/ArrayUtils.h"
7 
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 }
22 
24  saveCurrentRadialDistribution(iteration, simulation);
25 }
26 
28  saveCurrentRadialDistribution(iteration, simulation);
29 }
30 
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 }
38 
39 RadialDistributionFunctionInterceptor::operator std::string() const {
40  return fmt::format("RadialDistributionFunction: bin_width={}, sample_every_x_percent={}", bin_width, sample_every_x_percent);
41 }
42 
43 double RadialDistributionFunctionInterceptor::calculateLocalDensity(size_t N, size_t bin_index) const {
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 }
51 
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 }
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
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 b...
void logSummary(int depth) const override
Logs the summary of the radial distribution function.
void onSimulationStart(Simulation &simulation) override
This function is sets the particle_updates to 0 and initializes the start time of the simulation.
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.
double calculateLocalDensity(size_t N, size_t bin_index) const
void saveCurrentRadialDistribution(size_t iteration, 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.
Class to run a simulation.
Definition: Simulation.h:20
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
std::string to_string(const Container &container, const std::string &delimiter=", ", const std::array< std::string, 2 > &surround={"[", "]"})
Definition: ArrayUtils.h:97
auto L2Norm(const Container &c)
Definition: ArrayUtils.h:174