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

#include <VelocityProfileInterceptor.h>

Inheritance diagram for VelocityProfileInterceptor:
Inheritance graph
Collaboration diagram for VelocityProfileInterceptor:
Collaboration graph

Public Member Functions

 VelocityProfileInterceptor (std::pair< std::array< double, 3 >, std::array< double, 3 >> box, size_t num_bins, size_t 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 Attributes

std::pair< std::array< double, 3 >, std::array< double, 3 > > box
 
size_t num_bins
 
std::array< double, 3 > bin_width
 
size_t sample_every_x_percent
 
std::unique_ptr< CSVWritercsv_writer_x
 
std::unique_ptr< CSVWritercsv_writer_y
 
std::unique_ptr< CSVWritercsv_writer_z
 

Additional Inherited Members

- Protected Attributes inherited from SimulationInterceptor
size_t every_nth_iteration = 1
 

Detailed Description

Definition at line 8 of file VelocityProfileInterceptor.h.

Constructor & Destructor Documentation

◆ VelocityProfileInterceptor()

VelocityProfileInterceptor::VelocityProfileInterceptor ( std::pair< std::array< double, 3 >, std::array< double, 3 >>  box,
size_t  num_bins,
size_t  sample_every_x_percent 
)

Construct a new Thermostat Interceptor object.

Definition at line 8 of file VelocityProfileInterceptor.cpp.

Member Function Documentation

◆ logSummary()

void VelocityProfileInterceptor::logSummary ( int  depth) const
overridevirtual

Logs the summary of the radial distribution function.

Implements SimulationInterceptor.

Definition at line 95 of file VelocityProfileInterceptor.cpp.

95  {
96  std::string indent = std::string(depth * 2, ' ');
97 
98  Logger::logger->info("{}╟┤{}VelocityProfile: {}", indent, ansi_orange_bold, ansi_end);
99  Logger::logger->info("{}║ ┌Observed box: ({}, {}, {}) - ({}, {}, {})", indent, box.first[0], box.first[1], box.first[2],
100  box.second[0], box.second[1], box.second[2]);
101  Logger::logger->info("{}║ ├Number of bins per axis: {}", indent, num_bins);
102  Logger::logger->info("{}║ └Sample every x percent: {}", indent, sample_every_x_percent);
103 }
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 VelocityProfileInterceptor::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 93 of file VelocityProfileInterceptor.cpp.

93 {}

◆ onSimulationStart()

void VelocityProfileInterceptor::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 12 of file VelocityProfileInterceptor.cpp.

12  {
13  bool append = simulation.params.start_iteration != 0;
14 
15  csv_writer_x = std::make_unique<CSVWriter>(simulation.params.output_dir_path / "statistics" / "velocity_profile_x_axis.csv", append);
16  csv_writer_y = std::make_unique<CSVWriter>(simulation.params.output_dir_path / "statistics" / "velocity_profile_y_axis.csv", append);
17  csv_writer_z = std::make_unique<CSVWriter>(simulation.params.output_dir_path / "statistics" / "velocity_profile_z_axis.csv", append);
18 
19  bin_width[0] = (box.second[0] - box.first[0]) / num_bins;
20  bin_width[1] = (box.second[1] - box.first[1]) / num_bins;
21  bin_width[2] = (box.second[2] - box.first[2]) / num_bins;
22 
23  csv_writer_x->initialize(
24  {"iteration", "bin_index (w= " + std::to_string(bin_width[0]) + ")", "avg_velocity_x", "avg_velocity_y", "avg_velocity_z"});
25 
26  csv_writer_y->initialize(
27  {"iteration", "bin_index (w= " + std::to_string(bin_width[1]) + ")", "avg_velocity_x", "avg_velocity_y", "avg_velocity_z"});
28 
29  csv_writer_z->initialize(
30  {"iteration", "bin_index (w= " + std::to_string(bin_width[2]) + ")", "avg_velocity_x", "avg_velocity_y", "avg_velocity_z"});
31 
32  auto expected_iterations = static_cast<size_t>(std::ceil(simulation.params.end_time / simulation.params.delta_t));
33  SimulationInterceptor::every_nth_iteration = std::max(1, static_cast<int>(sample_every_x_percent * expected_iterations / 100));
34 
35  if (simulation.params.start_iteration == 0) {
36  (*this)(0, simulation);
37  }
38 }
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::unique_ptr< CSVWriter > csv_writer_x
std::unique_ptr< CSVWriter > csv_writer_y
std::unique_ptr< CSVWriter > csv_writer_z
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()

VelocityProfileInterceptor::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 105 of file VelocityProfileInterceptor.cpp.

105  {
106  return fmt::format("VelocityProfile: num_bins={}, sample_every_x_percent={}", num_bins, sample_every_x_percent);
107 }

◆ operator()()

void VelocityProfileInterceptor::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 40 of file VelocityProfileInterceptor.cpp.

40  {
41  std::map<size_t, std::array<double, 3>> avg_velocity_per_bin_index_x;
42  std::map<size_t, std::array<double, 3>> avg_velocity_per_bin_index_y;
43  std::map<size_t, std::array<double, 3>> avg_velocity_per_bin_index_z;
44 
45  std::map<size_t, size_t> samples_per_bin_index_x;
46  std::map<size_t, size_t> samples_per_bin_index_y;
47  std::map<size_t, size_t> samples_per_bin_index_z;
48 
49  for (auto& particle : *simulation.particle_container) {
50  auto& velocity = particle.getV();
51  auto& position = particle.getX();
52 
53  // check if particle is in box
54  if (position[0] < box.first[0] || position[0] > box.second[0] || position[1] < box.first[1] || position[1] > box.second[1] ||
55  position[2] < box.first[2] || position[2] > box.second[2]) {
56  continue;
57  }
58 
59  size_t bin_index_x = std::floor((position[0] - box.first[0]) / bin_width[0]);
60  size_t bin_index_y = std::floor((position[1] - box.first[1]) / bin_width[1]);
61  size_t bin_index_z = std::floor((position[2] - box.first[2]) / bin_width[2]);
62 
63  avg_velocity_per_bin_index_x[bin_index_x] =
64  (1.0 / (samples_per_bin_index_x[bin_index_x] + 1)) *
65  (samples_per_bin_index_x[bin_index_x] * avg_velocity_per_bin_index_x[bin_index_x] + velocity);
66 
67  avg_velocity_per_bin_index_y[bin_index_y] =
68  (1.0 / (samples_per_bin_index_y[bin_index_y] + 1)) *
69  (samples_per_bin_index_y[bin_index_y] * avg_velocity_per_bin_index_y[bin_index_y] + velocity);
70 
71  avg_velocity_per_bin_index_z[bin_index_z] =
72  (1.0 / (samples_per_bin_index_z[bin_index_z] + 1)) *
73  (samples_per_bin_index_z[bin_index_z] * avg_velocity_per_bin_index_z[bin_index_z] + velocity);
74 
75  samples_per_bin_index_x[bin_index_x]++;
76  samples_per_bin_index_y[bin_index_y]++;
77  samples_per_bin_index_z[bin_index_z]++;
78  }
79 
80  for (auto& [bin_index, avg_velocity] : avg_velocity_per_bin_index_x) {
81  csv_writer_x->writeRow({iteration, bin_index, avg_velocity[0], avg_velocity[1], avg_velocity[2]});
82  }
83 
84  for (auto& [bin_index, avg_velocity] : avg_velocity_per_bin_index_y) {
85  csv_writer_y->writeRow({iteration, bin_index, avg_velocity[0], avg_velocity[1], avg_velocity[2]});
86  }
87 
88  for (auto& [bin_index, avg_velocity] : avg_velocity_per_bin_index_z) {
89  csv_writer_z->writeRow({iteration, bin_index, avg_velocity[0], avg_velocity[1], avg_velocity[2]});
90  }
91 }
std::unique_ptr< ParticleContainer > particle_container
Reference to the ParticleContainer on whose content the simulation is performed.
Definition: Simulation.h:50

Member Data Documentation

◆ bin_width

std::array<double, 3> VelocityProfileInterceptor::bin_width
private

Definition at line 59 of file VelocityProfileInterceptor.h.

◆ box

std::pair<std::array<double, 3>, std::array<double, 3> > VelocityProfileInterceptor::box
private

Definition at line 57 of file VelocityProfileInterceptor.h.

◆ csv_writer_x

std::unique_ptr<CSVWriter> VelocityProfileInterceptor::csv_writer_x
private

Definition at line 61 of file VelocityProfileInterceptor.h.

◆ csv_writer_y

std::unique_ptr<CSVWriter> VelocityProfileInterceptor::csv_writer_y
private

Definition at line 62 of file VelocityProfileInterceptor.h.

◆ csv_writer_z

std::unique_ptr<CSVWriter> VelocityProfileInterceptor::csv_writer_z
private

Definition at line 63 of file VelocityProfileInterceptor.h.

◆ num_bins

size_t VelocityProfileInterceptor::num_bins
private

Definition at line 58 of file VelocityProfileInterceptor.h.

◆ sample_every_x_percent

size_t VelocityProfileInterceptor::sample_every_x_percent
private

Definition at line 60 of file VelocityProfileInterceptor.h.


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