Molecular Dynamics Simulation  1.0
Public Member Functions | Static Public Member Functions | List of all members
ChkptPointFileReader Class Reference

Class to read particle and simulation data from a '.xml' file. More...

#include <ChkptPointFileReader.h>

Collaboration diagram for ChkptPointFileReader:
Collaboration graph

Public Member Functions

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. More...
 

Static Public Member Functions

static size_t calculateHash (const std::filesystem::path &filepath)
 Calculates the hash for the given input file. More...
 
static bool detectSourceFileChanges (const std::string &filepath)
 Checks if the given file contains a valid hash generated from the original input file. More...
 

Detailed Description

Class to read particle and simulation data from a '.xml' file.

Definition at line 10 of file ChkptPointFileReader.h.

Member Function Documentation

◆ calculateHash()

size_t ChkptPointFileReader::calculateHash ( const std::filesystem::path &  filepath)
static

Calculates the hash for the given input file.

Parameters
filepathPath to the input file to calculate the hash for

Each CheckPoint saves the hash of the original it was created from. This function calculates the hash of the given file.

Definition at line 43 of file ChkptPointFileReader.cpp.

43  {
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 }
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

◆ detectSourceFileChanges()

bool ChkptPointFileReader::detectSourceFileChanges ( const std::string &  filepath)
static

Checks if the given file contains a valid hash generated from the original input file.

Parameters
filepathPath of the checkpoint file to check

Each CheckPoint saves the hash of the original it was created from. This function checks if the original file still hashes to the same value. If not, the CheckPoint is invalid because it refers to a input file which has been changed since the CheckPoint was created.

Definition at line 64 of file ChkptPointFileReader.cpp.

64  {
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 }

◆ readFile()

std::tuple< std::vector< Particle >, int > ChkptPointFileReader::readFile ( const std::filesystem::path &  filepath) const

Reads particle data from a '.xml' file and returns a vector of particles.

Parameters
filepathPath to the file to read
Returns
A tuple containing a vector of particles and the iteration number

Definition at line 19 of file ChkptPointFileReader.cpp.

19  {
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 }
void summarizeMetadata(MetaDataDataType m)
static Particle convertToParticle(const ParticleType &particle)
Converts a particle type from the XSD format to the internal format.

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