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

Wrapper class to abstract the reading of input files. More...

#include <FileInputHandler.h>

Collaboration diagram for FileInputHandler:
Collaboration graph

Static Public Member Functions

static std::tuple< std::vector< Particle >, std::optional< SimulationParams > > readFile (const std::filesystem::path &input_file_path, bool fresh=false, bool allow_recursion=true)
 Reads the input file and stores the particles in the given ParticleContainer. Other simulation parameters are returned as SimulationParams object. More...
 

Detailed Description

Wrapper class to abstract the reading of input files.

This class abstracts the reading and writing of files, so that the Simulation class does not have to know about the concrete implementations. Automatically determines correct file format using the file extension.

Definition at line 17 of file FileInputHandler.h.

Member Function Documentation

◆ readFile()

std::tuple< std::vector< Particle >, std::optional< SimulationParams > > FileInputHandler::readFile ( const std::filesystem::path &  input_file_path,
bool  fresh = false,
bool  allow_recursion = true 
)
static

Reads the input file and stores the particles in the given ParticleContainer. Other simulation parameters are returned as SimulationParams object.

Parameters
input_file_pathThe path to the input file
freshWhether to start a fresh simulation or reuse cached data
allow_recursionWhether to allow subsimulations to be started
Returns
std::tuple<std::vector<Particle>, std::optional<SimulationParams>> Tuple containing the particles and the parameters of the file

Reads the input file, generates particles and returns them in a vector. Parameters are stored in a SimulationParams object and returned. For more information about the output file formats, see Input File Formats

Definition at line 8 of file FileInputHandler.cpp.

9  {
10  if (!std::filesystem::exists(input_file_path)) {
11  Logger::logger->error("Error: file '{}' does not exist.", input_file_path.string());
12  throw FileReader::FileFormatException("File does not exist");
13  }
14 
15  std::string file_extension = input_file_path.extension().string();
16 
17  if (get_supported_input_file_extensions().find(file_extension) == get_supported_input_file_extensions().end()) {
18  Logger::logger->error("Error: file extension '{}' is not supported.", file_extension);
19  throw FileReader::FileFormatException("File extension is not supported");
20  }
21 
22  std::unique_ptr<FileReader> file_reader;
23 
24  if (file_extension == ".xml") {
25  file_reader = std::make_unique<XMLFileReader>(fresh, allow_recursion);
26  } else {
27  Logger::logger->error("Error: file extension '{}' is not supported.", file_extension);
28  throw FileReader::FileFormatException("File extension is not supported");
29  }
30 
31  try {
32  auto [particles, config] = file_reader->readFile(input_file_path);
33  Logger::logger->info("Loaded {} particles from file {}", particles.size(), input_file_path.string());
34  return std::make_tuple(particles, config);
35  } catch (const FileReader::FileFormatException& e) {
36  Logger::logger->error("Error: file '{}' is not a valid {} file.", input_file_path.string(), file_extension);
37  Logger::logger->error("FileFormatException:\n{}", std::string(e.what()));
38  throw;
39  }
40 }
const std::set< std::string > get_supported_input_file_extensions()
Returns a list of supported input file extensions.
Definition: InputFormats.cpp:3
Exception to be thrown when the file format is invalid.
Definition: FileReader.h:31
const char * what() const noexcept override
Definition: FileReader.h:35
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.
Definition: Logger.h:35

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