Molecular Dynamics Simulation  1.0
FrameWriterInterceptor.cpp
Go to the documentation of this file.
2 
3 #include <cmath>
4 #include <string>
5 
6 #include "io/logger/Logger.h"
8 
9 FrameWriterInterceptor::FrameWriterInterceptor(OutputFormat output_format, int fps, int video_length)
10  : output_format(output_format), fps(fps), video_length(video_length) {
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 }
20 
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 }
33 
34 void FrameWriterInterceptor::operator()(size_t iteration, Simulation& simulation) {
35  file_output_handler->writeFile(static_cast<int>(iteration), simulation.particle_container);
36  file_counter++;
37 }
38 
39 void FrameWriterInterceptor::onSimulationEnd(size_t iteration, Simulation& simulation) {
40  file_output_handler->writeFile(static_cast<int>(iteration), simulation.particle_container);
41  file_counter++;
42 }
43 
44 void FrameWriterInterceptor::logSummary(int depth) const {
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 }
59 
60 FrameWriterInterceptor::operator std::string() const { return "Frame Writer: " + std::to_string(file_counter) + " files saved"; }
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.
OutputFormat
Enum class to specify the output format.
Definition: OutputFormats.h:9
FrameWriterInterceptor(OutputFormat output_format, int fps, int video_length)
Construct a new Save File Interceptor object.
void onSimulationStart(Simulation &simulation) override
This function saves the initial state of the simulation.
void onSimulationEnd(size_t iteration, Simulation &simulation) override
This function saves the final state of the simulation.
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 fi...
std::unique_ptr< FileOutputHandler > file_output_handler
void logSummary(int depth) const override
Logs the summary of the thermostat.
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.
Definition: Logger.h:35
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.
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