Molecular Dynamics Simulation  1.0
FileOutputHandler.cpp
Go to the documentation of this file.
1 #include "FileOutputHandler.h"
2 
3 #include <filesystem>
4 
5 #include "io/logger/Logger.h"
9 
10 FileOutputHandler::FileOutputHandler(const OutputFormat output_format, const SimulationParams& params) : 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 }
56 
57 std::optional<const std::filesystem::path> FileOutputHandler::writeFile(size_t iteration, const std::vector<Particle>& particles) const {
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 }
64 
65 std::optional<const std::filesystem::path> FileOutputHandler::writeFile(
66  size_t iteration, const std::unique_ptr<ParticleContainer>& particle_container) const {
67  return writeFile(iteration, particle_container->getParticles());
68 }
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
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.
std::unique_ptr< FileWriter > file_writer
Pointer to the FileWriter object to use.
const SimulationParams & params
SimulationParams object which provides the output directory path.
FileOutputHandler(const OutputFormat output_format, const SimulationParams &params)
Construct a new FileOutputHandler object.
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.
Definition: Logger.h:35
Contains all parameters needed to run a simulation.
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.