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

Wrapper class to abstract the writing of output files. More...

#include <FileOutputHandler.h>

Collaboration diagram for FileOutputHandler:
Collaboration graph

Public Member Functions

 FileOutputHandler (const OutputFormat output_format, const SimulationParams &params)
 Construct a new FileOutputHandler object. More...
 
std::optional< const std::filesystem::path > writeFile (size_t iteration, const std::unique_ptr< ParticleContainer > &particle_container) const
 Writes the given ParticleContainers particle data to a file. More...
 
std::optional< const std::filesystem::path > writeFile (size_t iteration, const std::vector< Particle > &particles) const
 Writes the given ParticleContainers particle data to a file. More...
 

Private Attributes

const SimulationParamsparams
 SimulationParams object which provides the output directory path. More...
 
std::unique_ptr< FileWriterfile_writer
 Pointer to the FileWriter object to use. More...
 

Detailed Description

Wrapper class to abstract the writing of output files.

Currently there are two supported output formats: VTU and XYZ. Additionally a 'NONE' format is available, which does not write any output.

Definition at line 16 of file FileOutputHandler.h.

Constructor & Destructor Documentation

◆ FileOutputHandler()

FileOutputHandler::FileOutputHandler ( const OutputFormat  output_format,
const SimulationParams params 
)
explicit

Construct a new FileOutputHandler object.

Parameters
output_formatThe output format to use
paramsSimulationParams object which provides the output directory path

Definition at line 10 of file FileOutputHandler.cpp.

10  : params(params) {
11  switch (output_format) {
12  case OutputFormat::VTU:
13  file_writer = std::make_unique<VTUWriter>();
14  break;
15  case OutputFormat::XYZ:
16  file_writer = std::make_unique<XYZWriter>();
17  break;
19  file_writer = std::make_unique<CheckPointWriter>();
20  break;
21  default:
22  Logger::logger->error("Output format not implemented.");
23  exit(1);
24  }
25 
26  if (std::filesystem::exists(params.output_dir_path)) {
27  auto supported = get_supported_output_formats();
28 
29  auto file_extension = std::find_if(supported.begin(), supported.end(), [&params, &output_format](const auto& pair) {
30  return pair.second == output_format;
31  })->first;
32 
33  auto count = 0;
34  for (const auto& entry : std::filesystem::directory_iterator(params.output_dir_path)) {
35  if (entry.path().extension() == "." + file_extension) {
36  std::string filename = entry.path().filename().string();
37  std::string iteration_str =
38  filename.substr(filename.find_last_of("_") + 1, filename.find_last_of(".") - filename.find_last_of("_") - 1);
39 
40  size_t iteration = std::stoul(iteration_str);
41 
42  if (params.start_iteration == 0 || iteration > params.start_iteration) {
43  std::filesystem::remove(entry.path());
44  count++;
45  }
46  }
47  }
48  if (count > 0) {
49  Logger::logger->warn("Removed {} {} files from output directory '{}'.", count, file_extension, params.output_dir_path.string());
50  }
51  } else {
52  Logger::logger->info("Creating output directory '{}'.", params.output_dir_path.string());
53  std::filesystem::create_directories(params.output_dir_path.string());
54  }
55 }
const std::map< std::string, OutputFormat > get_supported_output_formats()
Returns a mappping of supported output formats.
std::unique_ptr< FileWriter > file_writer
Pointer to the FileWriter object to use.
const SimulationParams & params
SimulationParams object which provides the output directory path.
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.
Definition: Logger.h:35
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.

Member Function Documentation

◆ writeFile() [1/2]

std::optional< const std::filesystem::path > FileOutputHandler::writeFile ( size_t  iteration,
const std::unique_ptr< ParticleContainer > &  particle_container 
) const

Writes the given ParticleContainers particle data to a file.

Parameters
iterationThe current iteration number of the simulation
particle_containerThe ParticleContainer to write to the file
Returns
The path to the written file

Definition at line 65 of file FileOutputHandler.cpp.

66  {
67  return writeFile(iteration, particle_container->getParticles());
68 }
std::optional< const std::filesystem::path > writeFile(size_t iteration, const std::unique_ptr< ParticleContainer > &particle_container) const
Writes the given ParticleContainers particle data to a file.

◆ writeFile() [2/2]

std::optional< const std::filesystem::path > FileOutputHandler::writeFile ( size_t  iteration,
const std::vector< Particle > &  particles 
) const

Writes the given ParticleContainers particle data to a file.

Parameters
iterationThe current iteration number of the simulation
particlesA vector of particles to write to the file
Returns
The path to the written file

Definition at line 57 of file FileOutputHandler.cpp.

57  {
58  if (!file_writer) {
59  Logger::logger->warn("No file writer set. Not writing file.");
60  throw std::runtime_error("No file writer set. Not writing file.");
61  }
62  return file_writer->writeFile(params, iteration, particles);
63 }

Member Data Documentation

◆ file_writer

std::unique_ptr<FileWriter> FileOutputHandler::file_writer
private

Pointer to the FileWriter object to use.

Definition at line 26 of file FileOutputHandler.h.

◆ params

const SimulationParams& FileOutputHandler::params
private

SimulationParams object which provides the output directory path.

Definition at line 21 of file FileOutputHandler.h.


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