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

#include <FrameWriterInterceptor.h>

Inheritance diagram for FrameWriterInterceptor:
Inheritance graph
Collaboration diagram for FrameWriterInterceptor:
Collaboration graph

Public Member Functions

 FrameWriterInterceptor (OutputFormat output_format, int fps, int video_length)
 Construct a new Save File Interceptor object. More...
 
void onSimulationStart (Simulation &simulation) override
 This function saves the initial state of the simulation. More...
 
void operator() (size_t iteration, Simulation &simulation) override
 This function is called on every nth iteration. It writes the current state of the simulation to a file. More...
 
void onSimulationEnd (size_t iteration, Simulation &simulation) override
 This function saves the final state 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 thermostat. 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 file_counter = 0
 
std::unique_ptr< FileOutputHandlerfile_output_handler
 
OutputFormat output_format
 
size_t fps
 
size_t video_length
 

Additional Inherited Members

- Protected Attributes inherited from SimulationInterceptor
size_t every_nth_iteration = 1
 

Detailed Description

Definition at line 8 of file FrameWriterInterceptor.h.

Constructor & Destructor Documentation

◆ FrameWriterInterceptor()

FrameWriterInterceptor::FrameWriterInterceptor ( OutputFormat  output_format,
int  fps,
int  video_length 
)

Construct a new Save File Interceptor object.

Definition at line 9 of file FrameWriterInterceptor.cpp.

11  if (fps < 0) {
12  Logger::logger->error("FPS must be positive");
13  throw std::runtime_error("FPS must be positive");
14  }
15  if (video_length < 0) {
16  Logger::logger->error("Video length must be positive");
17  throw std::runtime_error("Video length must be positive");
18  }
19 }
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.
Definition: Logger.h:35

Member Function Documentation

◆ logSummary()

void FrameWriterInterceptor::logSummary ( int  depth) const
overridevirtual

Logs the summary of the thermostat.

Implements SimulationInterceptor.

Definition at line 44 of file FrameWriterInterceptor.cpp.

44  {
45  std::string indent = std::string(depth * 2, ' ');
46 
47  auto supported_output_formats = get_supported_output_formats();
48 
49  std::string output_format_s =
50  std::find_if(supported_output_formats.begin(), supported_output_formats.end(), [this](const auto& format) {
51  return format.second == output_format;
52  })->first;
53 
54  Logger::logger->info("{}╟┤{}Frame Writer: {}", indent, ansi_orange_bold, ansi_end);
55  Logger::logger->info("{}║ ┌Output format: {}", indent, output_format_s);
56  Logger::logger->info("{}║ ├Frames per second: {}", indent, fps);
57  Logger::logger->info("{}║ └Video length: {}", indent, video_length);
58 }
const std::string ansi_end
Definition: Logger.h:13
const std::string ansi_orange_bold
Definition: Logger.h:10
const std::map< std::string, OutputFormat > get_supported_output_formats()
Returns a mappping of supported output formats.

◆ onSimulationEnd()

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

This function saves the final state of the simulation.

Parameters
iterationThe current iteration
simulationThe simulation object

Implements SimulationInterceptor.

Definition at line 39 of file FrameWriterInterceptor.cpp.

39  {
40  file_output_handler->writeFile(static_cast<int>(iteration), simulation.particle_container);
41  file_counter++;
42 }
std::unique_ptr< FileOutputHandler > file_output_handler
std::unique_ptr< ParticleContainer > particle_container
Reference to the ParticleContainer on whose content the simulation is performed.
Definition: Simulation.h:50

◆ onSimulationStart()

void FrameWriterInterceptor::onSimulationStart ( Simulation simulation)
overridevirtual

This function saves the initial state of the simulation.

Parameters
simulationThe simulation object

Implements SimulationInterceptor.

Definition at line 21 of file FrameWriterInterceptor.cpp.

21  {
22  auto expected_iterations = static_cast<size_t>(std::ceil(simulation.params.end_time / simulation.params.delta_t) + 1);
23 
24  FrameWriterInterceptor::every_nth_iteration = std::max(expected_iterations / (fps * video_length), 1ul);
25 
26  file_output_handler = std::make_unique<FileOutputHandler>(output_format, simulation.params);
27 
28  if (simulation.params.start_iteration == 0) {
29  file_output_handler->writeFile(simulation.params.start_iteration, simulation.particle_container);
30  file_counter++;
31  }
32 }
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.
const SimulationParams & params
Reference to the simulation parameters object.
Definition: Simulation.h:45

◆ operator std::string()

FrameWriterInterceptor::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 60 of file FrameWriterInterceptor.cpp.

60 { return "Frame Writer: " + std::to_string(file_counter) + " files saved"; }
std::string to_string(const Container &container, const std::string &delimiter=", ", const std::array< std::string, 2 > &surround={"[", "]"})
Definition: ArrayUtils.h:97

◆ operator()()

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

This function is called on every nth iteration. It writes the current state of the simulation to a file.

Parameters
iterationThe current iteration
simulationThe simulation object

Implements SimulationInterceptor.

Definition at line 34 of file FrameWriterInterceptor.cpp.

34  {
35  file_output_handler->writeFile(static_cast<int>(iteration), simulation.particle_container);
36  file_counter++;
37 }

Member Data Documentation

◆ file_counter

size_t FrameWriterInterceptor::file_counter = 0
private

Definition at line 54 of file FrameWriterInterceptor.h.

◆ file_output_handler

std::unique_ptr<FileOutputHandler> FrameWriterInterceptor::file_output_handler
private

Definition at line 55 of file FrameWriterInterceptor.h.

◆ fps

size_t FrameWriterInterceptor::fps
private

Definition at line 57 of file FrameWriterInterceptor.h.

◆ output_format

OutputFormat FrameWriterInterceptor::output_format
private

Definition at line 56 of file FrameWriterInterceptor.h.

◆ video_length

size_t FrameWriterInterceptor::video_length
private

Definition at line 58 of file FrameWriterInterceptor.h.


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