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

#include <ProgressBarInterceptor.h>

Inheritance diagram for ProgressBarInterceptor:
Inheritance graph
Collaboration diagram for ProgressBarInterceptor:
Collaboration graph

Public Member Functions

void onSimulationStart (Simulation &simulation) override
 This function initalized the start time of the simulation and the previous time point. More...
 
void operator() (size_t iteration, Simulation &simulation) override
 This function is called on every nth iteration. It prints a progress bar to the console and updates the previous time point. More...
 
void onSimulationEnd (size_t iteration, Simulation &simulation) override
 This function is empty as the progress bar 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 progress bar. 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

size_t expected_iterations
 
std::chrono::high_resolution_clock::time_point t_start
 
std::chrono::high_resolution_clock::time_point t_end
 
std::chrono::high_resolution_clock::time_point t_prev
 
size_t last_particle_count = 0
 
size_t last_iteration = 0
 
double last_remaining_seconds = 0
 
double last_particle_updates_per_second = 0
 

Additional Inherited Members

- Protected Attributes inherited from SimulationInterceptor
size_t every_nth_iteration = 1
 

Detailed Description

Definition at line 7 of file ProgressBarInterceptor.h.

Member Function Documentation

◆ logSummary()

void ProgressBarInterceptor::logSummary ( int  depth) const
overridevirtual

Logs the summary of the progress bar.

Implements SimulationInterceptor.

Definition at line 117 of file ProgressBarInterceptor.cpp.

117  {
118  std::string indent = std::string(depth * 2, ' ');
119 
120  Logger::logger->info("{}╟┤{}ProgressBar: {}", indent, ansi_orange_bold, ansi_end);
121  Logger::logger->info("{}║ ├Enabled", indent);
122 }
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 ProgressBarInterceptor::onSimulationEnd ( size_t  iteration,
Simulation simulation 
)
overridevirtual

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

Parameters
iterationThe current iteration
simulationThe simulation object

Implements SimulationInterceptor.

Definition at line 113 of file ProgressBarInterceptor.cpp.

113  {
115 }
void printProgress(const std::filesystem::path &input_file_path, size_t percentage, size_t iteration, size_t expected_iterations, int estimated_remaining_seconds=-1, double particle_updates_per_second=-1, bool finished=false)
std::filesystem::path input_file_path
Path to the input file of the simulation.
const SimulationParams & params
Reference to the simulation parameters object.
Definition: Simulation.h:45

◆ onSimulationStart()

void ProgressBarInterceptor::onSimulationStart ( Simulation simulation)
overridevirtual

This function initalized the start time of the simulation and the previous time point.

Parameters
simulationThe simulation object

Implements SimulationInterceptor.

Definition at line 56 of file ProgressBarInterceptor.cpp.

56  {
57  t_start = std::chrono::high_resolution_clock::now();
58  t_prev = t_start;
59 
60  expected_iterations = static_cast<size_t>(std::ceil(simulation.params.end_time / simulation.params.delta_t));
61 
62  SimulationInterceptor::every_nth_iteration = std::max(1, static_cast<int>(expected_iterations / 1000.0));
63 
64  const size_t percentage = std::min(100ul, static_cast<size_t>(100.0 * static_cast<double>(simulation.params.start_iteration) /
65  static_cast<double>(expected_iterations)));
66 
67  last_particle_count = simulation.particle_container->size();
69 
72 
73  Logger::logger->flush();
74  printProgress(simulation.params.input_file_path, percentage, simulation.params.start_iteration, expected_iterations, -1, -1);
75 }
std::chrono::high_resolution_clock::time_point t_start
std::chrono::high_resolution_clock::time_point t_prev
double delta_t
Time step of a single simulation iteration.
size_t start_iteration
Start iteration of the simulation.
double end_time
End time of the simulation.
std::unique_ptr< ParticleContainer > particle_container
Reference to the ParticleContainer on whose content the simulation is performed.
Definition: Simulation.h:50

◆ operator std::string()

ProgressBarInterceptor::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 124 of file ProgressBarInterceptor.cpp.

124 { return ""; }

◆ operator()()

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

This function is called on every nth iteration. It prints a progress bar to the console and updates the previous time point.

Parameters
iterationThe current iteration
simulationThe simulation object

Implements SimulationInterceptor.

Definition at line 77 of file ProgressBarInterceptor.cpp.

77  {
78  // calculate time since last write
79  auto t_now = std::chrono::high_resolution_clock::now();
80  const double seconds_since_last_write = std::chrono::duration<double>(t_now - t_prev).count();
81  t_prev = t_now;
82 
83  // calculate estimated remaining time
84  const int estimated_remaining_seconds = std::floor(seconds_since_last_write * static_cast<double>(expected_iterations - iteration) /
85  static_cast<double>(every_nth_iteration));
86 
87  // calculate percentage
88  const size_t percentage =
89  std::min(100ul, static_cast<size_t>(100.0 * static_cast<double>(iteration) / static_cast<double>(expected_iterations)));
90 
91  // calculate particle updates per second
92  size_t current_particle_count = simulation.particle_container->size();
93  size_t particle_updates = (current_particle_count + last_particle_count) / 2 * (iteration - last_iteration);
94 
95  double particle_updates_per_second = static_cast<double>(particle_updates) / seconds_since_last_write;
96  last_particle_count = current_particle_count;
97  last_iteration = iteration;
98 
99  // Smooth out the estimated remaining time and the updates per second
100  if (last_remaining_seconds < 0) last_remaining_seconds = estimated_remaining_seconds;
101  if (last_particle_updates_per_second < 0) last_particle_updates_per_second = particle_updates_per_second;
102 
103  double smoothed_remaining_seconds = (estimated_remaining_seconds + last_remaining_seconds) / 2;
104  double smoothed_particle_updates_per_second = (particle_updates_per_second + last_particle_updates_per_second) / 2;
105 
106  printProgress(simulation.params.input_file_path, percentage, iteration, expected_iterations, smoothed_remaining_seconds,
107  smoothed_particle_updates_per_second);
108 
109  last_remaining_seconds = smoothed_remaining_seconds;
110  last_particle_updates_per_second = smoothed_particle_updates_per_second;
111 }

Member Data Documentation

◆ expected_iterations

size_t ProgressBarInterceptor::expected_iterations
private

Definition at line 51 of file ProgressBarInterceptor.h.

◆ last_iteration

size_t ProgressBarInterceptor::last_iteration = 0
private

Definition at line 56 of file ProgressBarInterceptor.h.

◆ last_particle_count

size_t ProgressBarInterceptor::last_particle_count = 0
private

Definition at line 55 of file ProgressBarInterceptor.h.

◆ last_particle_updates_per_second

double ProgressBarInterceptor::last_particle_updates_per_second = 0
private

Definition at line 58 of file ProgressBarInterceptor.h.

◆ last_remaining_seconds

double ProgressBarInterceptor::last_remaining_seconds = 0
private

Definition at line 57 of file ProgressBarInterceptor.h.

◆ t_end

std::chrono::high_resolution_clock::time_point ProgressBarInterceptor::t_end
private

Definition at line 53 of file ProgressBarInterceptor.h.

◆ t_prev

std::chrono::high_resolution_clock::time_point ProgressBarInterceptor::t_prev
private

Definition at line 54 of file ProgressBarInterceptor.h.

◆ t_start

std::chrono::high_resolution_clock::time_point ProgressBarInterceptor::t_start
private

Definition at line 52 of file ProgressBarInterceptor.h.


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