Molecular Dynamics Simulation  1.0
ChkptPointFileReader.cpp
Go to the documentation of this file.
1 #include "ChkptPointFileReader.h"
2 
3 #include <fstream>
4 #include <sstream>
5 
6 #include "io/input/FileReader.h"
7 #include "io/logger/Logger.h"
9 
10 void summarizeMetadata(MetaDataDataType m) {
11  Logger::logger->info("Loaded checkpoint file with following metadata:");
12  Logger::logger->info(" - Original file: {}", m.input_file());
13  Logger::logger->info(" - Original file hash: {}", m.input_file_hash());
14  Logger::logger->info(" - Original End time: {}", m.end_time());
15  Logger::logger->info(" - Original Delta t: {}", m.delta_t());
16  Logger::logger->info(" - Iteration: {}", m.current_iteration());
17 }
18 
19 std::tuple<std::vector<Particle>, int> ChkptPointFileReader::readFile(const std::filesystem::path& filepath) const {
20  try {
21  auto checkpoint = CheckPoint(filepath, xml_schema::flags::dont_validate);
22 
23  auto particle_data = checkpoint->ParticleData();
24  auto meta_data = checkpoint->MetaData();
25 
26  summarizeMetadata(meta_data);
27 
28  std::vector<Particle> particles;
29 
30  for (auto xsd_particle : particle_data.particle()) {
31  auto particle = XSDToInternalTypeAdapter::convertToParticle(xsd_particle);
32  particles.push_back(std::move(particle));
33  }
34 
35  return std::make_tuple(particles, meta_data.current_iteration());
36  } catch (const xml_schema::exception& e) {
37  std::stringstream error_message;
38  error_message << "Error: could not parse file '" << filepath << "'.\n";
39  error_message << e << std::endl;
40  throw FileReader::FileFormatException(error_message.str());
41  }
42 }
43 size_t ChkptPointFileReader::calculateHash(const std::filesystem::path& filepath) {
44  if (!std::filesystem::exists(filepath)) {
45  Logger::logger->warn("File '{}' does not exist. Using hash 0.", filepath.string());
46  return 0;
47  }
48 
49  std::ifstream input_file(filepath);
50 
51  if (!input_file.is_open()) {
52  Logger::logger->error("Error: could not open file '{}'.", filepath.string());
53  throw FileReader::FileFormatException("Could not open file");
54  }
55 
56  auto buffer = std::stringstream();
57  buffer << input_file.rdbuf();
58 
59  std::hash<std::string> hasher;
60  auto hash = hasher(buffer.str());
61  return hash;
62 }
63 
64 bool ChkptPointFileReader::detectSourceFileChanges(const std::string& filepath) {
65  auto checkpoint = CheckPoint(filepath, xml_schema::flags::dont_validate);
66  auto meta_data = checkpoint->MetaData();
67 
68  std::ifstream input_file(meta_data.input_file());
69 
70  auto buffer = std::stringstream();
71  buffer << input_file.rdbuf();
72 
73  std::hash<std::string> hasher;
74  auto curr_hash = hasher(buffer.str());
75 
76  bool hash_valid = curr_hash == meta_data.input_file_hash();
77 
78  if (!hash_valid) {
79  Logger::logger->warn("Source file '{}' has changed since last checkpoint. Original hash: {}, current hash: {}",
80  meta_data.input_file(), meta_data.input_file_hash(), curr_hash);
81  }
82 
83  return hash_valid;
84 }
void summarizeMetadata(MetaDataDataType m)
static bool detectSourceFileChanges(const std::string &filepath)
Checks if the given file contains a valid hash generated from the original input file.
std::tuple< std::vector< Particle >, int > readFile(const std::filesystem::path &filepath) const
Reads particle data from a '.xml' file and returns a vector of particles.
static size_t calculateHash(const std::filesystem::path &filepath)
Calculates the hash for the given input file.
Exception to be thrown when the file format is invalid.
Definition: FileReader.h:31
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.
Definition: Logger.h:35
static Particle convertToParticle(const ParticleType &particle)
Converts a particle type from the XSD format to the internal format.