From 55f66bd765d1fabdb4af9280087ad52a2e1b09a3 Mon Sep 17 00:00:00 2001 From: TobiasEppacher Date: Fri, 15 Dec 2023 10:01:33 +0100 Subject: [PATCH] Remove all custom file formats (deprecated), remove the corresponding tests, convert some old example input files to xml --- README.md | 4 +- docs/InputFileFormats.md | 71 +------------ .../body_collision_assignment2.cub | 17 ---- .../body_collision_assignment2.xml | 72 +++++++++++++ input/periodic_system/eingabe-figure-8.ps | Bin 195 -> 0 bytes input/periodic_system/eingabe-figure-8.xml | 77 ++++++++++++++ src/io/input/FileInputHandler.cpp | 6 +- src/io/input/FileInputHandler.h | 2 - src/io/input/InputFormats.cpp | 2 +- .../input/custom_formats/FileLineReader.cpp | 31 ------ src/io/input/custom_formats/FileLineReader.h | 96 ------------------ .../custom_formats/cub/CubFileReader.cpp | 92 ----------------- .../input/custom_formats/cub/CubFileReader.h | 27 ----- .../input/custom_formats/ps/PsFileReader.cpp | 54 ---------- src/io/input/custom_formats/ps/PsFileReader.h | 17 ---- tests/data/input/CubExample.cub | 17 ---- tests/io/filereaders/TestCubFileReader.cpp | 25 ----- 17 files changed, 156 insertions(+), 454 deletions(-) delete mode 100644 input/body_collision/body_collision_assignment2.cub create mode 100644 input/body_collision/body_collision_assignment2.xml delete mode 100644 input/periodic_system/eingabe-figure-8.ps create mode 100644 input/periodic_system/eingabe-figure-8.xml delete mode 100644 src/io/input/custom_formats/FileLineReader.cpp delete mode 100644 src/io/input/custom_formats/FileLineReader.h delete mode 100644 src/io/input/custom_formats/cub/CubFileReader.cpp delete mode 100644 src/io/input/custom_formats/cub/CubFileReader.h delete mode 100644 src/io/input/custom_formats/ps/PsFileReader.cpp delete mode 100644 src/io/input/custom_formats/ps/PsFileReader.h delete mode 100644 tests/data/input/CubExample.cub delete mode 100644 tests/io/filereaders/TestCubFileReader.cpp diff --git a/README.md b/README.md index d670b8604..d12262762 100644 --- a/README.md +++ b/README.md @@ -86,9 +86,9 @@ In this section we describe how to build the project. You can use the following - An example run could look like this: `./MolSim ../../body_collision.cub -d 0.0002 -e 5` - - Further information about the ossible input file formats can be found in the `/docs` directory. + - Further information about the possible input file formats can be found in the `/docs` directory. - - **Note:** Some input file formats can provide own simulation parameters. In the case that the user provides additional parameters via the command line, both sources of parameters are merged. If a conflict occurs, the command line parameters take precedence, since it was explicitly requested by the user. + - **Note:** Input files can, and for some cases have to provide own simulation parameters. In case the user provides additional parameters via the command line, both sources of parameters are merged. If a conflict occurs, the command line parameters take precedence, since it was explicitly requested by the user. To avoid mixups, it is recommended to define all parameters in the input file and only use command line for small, temporary adjustments to avoid changing the input file. ### Run the tests diff --git a/docs/InputFileFormats.md b/docs/InputFileFormats.md index 053861a33..2ad3a2730 100644 --- a/docs/InputFileFormats.md +++ b/docs/InputFileFormats.md @@ -7,77 +7,12 @@ Our program supports multiple file formats as input files, which are interpreted differently. The file format is automatically determined by the file extension. The following file extensions are supported: -- `.ps`: Contains Raw Particle Data -- `.cub`: Contains arguments for the internal cuboid generator - `.xml`: Contains data for particle generation and allows to specify all the input parameters for the simulation as well In the following sections, the file formats are explained in detail. ## File Formats Explained -### .ps - -The `.ps` file format contains raw particle data. It is used to directly specify the initial state of the simulation. - -- Starts with an arbitrary amount of comment lines, which start with `#` and are only allowed at the beginning of the file -- The first non-comment line contains a single integer, the number of particles in the simulation -- Now follow an amount of lines equal to the number of particles, each containing the following arguments (each seperated by spaces): - - The particle's position in 3D space (Three floating point numbers, seperated by spaces) - - The particle's initial velocity (Three floating point numbers, seperated by spaces) - - The particle's mass (A single floating point number the particle's mass) - -Note: Floating point numbers can be written in scientific notation, e.g. `1.0e-3` for `0.001`. - -An example file could look like this: - -```text -# This is a comment -# xyz-coord velocity mass -4 -0.0 0.0 0.0 0.0 0.0 0.0 1.0 -0.0 1.0 0.0 -1.0 0.0 0.0 3.0e-6 -0.0 5.36 0.0 -0.425 0.0 0.0 9.55e-4 -34.75 0.0 0.0 0.0 0.0296 0.0 1.0e-14 -``` - -### .cub - -The `.cub` file format contains arguments for the internal cuboid generator. The program uses these arguments to generate one or multiple cuboids of particles given the specified parameters. - -- Consists of one or more definitions of cuboids with the following arguments (each on a seperate line), where the values on each line are seperated by spaces: - - The coordinate of the lower left front-side corner: '`double` `double` `double`' - - Grid Dimensions: '`int` `int` `int`' - - Distance of the particles (mesh width): '`double`' - - Mass of a single particle: '`double`' - - Initial velocity of the particles: '`double` `double` `double`' - - Initial temperature of the particles (for temperature dependent wobbling, excludes temperature created by full cuboid velocity) '`double`' - - Type of the cuboid: '`int` used for labeling particles in the cuboid' -- Outside the cuboid definitions an arbitrary amount of comment lines, which start with `#` and empty lines are allowed, furthermore comments are allowed at the end of a line after the last argument - -Note: Floating point numbers can be written in scientific notation, e.g. `1.0e-3` for `0.001`. - -An example file could look like this: - -```text -# Definition of Cuboid 1 -0.0 0.0 0.0 # Lower left front-side corner -10 10 10 # Grid Dimensions -0.1 # Distance of the particles (mesh width) -1.0 # Mass of a single particle -0.4 0.0 0.0 # Initial velocity of the particles -0.0 # Initial temperature -0 # Type of the cuboid - -# Definition of Cuboid 2 -10.0 10.0 10.0 # Lower left front-side corner -10 10 10 # Grid Dimensions -0.1 # Distance of the particles (mesh width) -1.0 # Mass of a single particle -0.0 0.4 0.0 # Initial velocity of the particles -0.0 # Initial temperature -1 # Type of the cuboid -``` - ### .xml The `.xml` file format can be used to specify all the input parameters for the simulation at once. Therefore, it is preferred over the other file formats. @@ -92,9 +27,9 @@ Its definition is based on the [simulation_schema.xsd](simulation_schema.xsd) fi - `` Which particle container implementation should be used - Particle Data: - - ``: Input data for a cuboid spawner, which generates particles in a cuboid shape - - ``: Input data for a sphere spawner, which generates particles in a spherical shape - - ``: Input data for a single particle, which is placed at the specified position + - ``: Input data for a cuboid spawner, which generates particles in a cuboid shape (2 or 3 dimensional) + - ``: Input data for a sphere spawner, which generates particles in a spherical shape (2 or 3 dimensional) + - ``: Input data for a single particle, which is placed at the specified position An example file could look like this: diff --git a/input/body_collision/body_collision_assignment2.cub b/input/body_collision/body_collision_assignment2.cub deleted file mode 100644 index 81d173f20..000000000 --- a/input/body_collision/body_collision_assignment2.cub +++ /dev/null @@ -1,17 +0,0 @@ -# Cube 1 -0.0 0.0 0.0 # lower left -40 8 1 # grid dimensions -1.1225 # grid spacing -1.0 # mass -0.0 0.0 0.0 # velocity -0.1 # initial temperature (using brownian motion) -0 # type - -# Cube 2 -15.0 15.0 0.0 # lower left -8 8 1 # grid dimensions -1.1225 # grid spacing -1.0 # mass -0.0 -10.0 0.0 # velocity -0.1 # initial temperature (using brownian motion) -1 # type \ No newline at end of file diff --git a/input/body_collision/body_collision_assignment2.xml b/input/body_collision/body_collision_assignment2.xml new file mode 100644 index 000000000..9bd89442a --- /dev/null +++ b/input/body_collision/body_collision_assignment2.xml @@ -0,0 +1,72 @@ + + + + + + 24 + 30 + 0.0002 + 5.0 + false + + + + + + + + + + + + + 0.0 + 0.0 + 0.0 + + + 40 + 8 + 1 + + 1.1225 + 0.1 + 1 + + 0.0 + 0.0 + 0.0 + + 0 + 5.0 + 1.0 + + + + + 15.0 + 15.0 + 0.0 + + + 8 + 8 + 1 + + 1.1225 + 0.1 + 1 + + 0.0 + -10.0 + 0.0 + + 1 + 5.0 + 1.0 + + + + + \ No newline at end of file diff --git a/input/periodic_system/eingabe-figure-8.ps b/input/periodic_system/eingabe-figure-8.ps deleted file mode 100644 index b070031e1ebc4865a5640bf0d0fed038f6a4cf0c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 195 zcmY#ZsHm*cP0r6RN>KoVvecaX4X35xjuOR6ND diff --git a/input/periodic_system/eingabe-figure-8.xml b/input/periodic_system/eingabe-figure-8.xml new file mode 100644 index 000000000..ad12a232a --- /dev/null +++ b/input/periodic_system/eingabe-figure-8.xml @@ -0,0 +1,77 @@ + + + + + + 24 + 30 + 0.0005 + 20.0 + false + + + + + + + + + + + + + -1.0 + 0.0 + 0.0 + + 0 + 1 + + 0.347113 + 0.532727 + 0.0 + + 0 + 1.0 + 1.2 + + + + + 1.0 + 0.0 + 0.0 + + 0 + 1 + + 0.347113 + 0.532727 + 0.0 + + 1 + 1.0 + 1.2 + + + + + 0.0 + 0.0 + 0.0 + + 0 + 1 + + -0.694226 + -1.065454 + 0.0 + + 2 + 1.0 + 1.2 + + + + \ No newline at end of file diff --git a/src/io/input/FileInputHandler.cpp b/src/io/input/FileInputHandler.cpp index 76484db88..29b5e1d2a 100644 --- a/src/io/input/FileInputHandler.cpp +++ b/src/io/input/FileInputHandler.cpp @@ -28,11 +28,7 @@ std::tuple, std::optional> FileInputHand std::unique_ptr file_reader; - if (file_extension == ".ps") { - file_reader = std::make_unique(); - } else if (file_extension == ".cub") { - file_reader = std::make_unique(); - } else if (file_extension == ".xml") { + if (file_extension == ".xml") { file_reader = std::make_unique(fresh, allow_recursion); } else if (file_extension == ".chkpt") { file_reader = std::make_unique(); diff --git a/src/io/input/FileInputHandler.h b/src/io/input/FileInputHandler.h index a638ac690..cdf00a581 100644 --- a/src/io/input/FileInputHandler.h +++ b/src/io/input/FileInputHandler.h @@ -5,8 +5,6 @@ #include "io/input/InputFormats.h" #include "io/input/chkpt/ChkptPointFileReader.h" -#include "io/input/custom_formats/cub/CubFileReader.h" -#include "io/input/custom_formats/ps/PsFileReader.h" #include "io/input/xml/XMLFileReader.h" #include "simulation/SimulationParams.h" diff --git a/src/io/input/InputFormats.cpp b/src/io/input/InputFormats.cpp index 3c1f32914..812adbf21 100644 --- a/src/io/input/InputFormats.cpp +++ b/src/io/input/InputFormats.cpp @@ -1,3 +1,3 @@ #include "InputFormats.h" -const std::set get_supported_input_file_extensions() { return {".ps", ".cub", ".xml", ".chkpt"}; } \ No newline at end of file +const std::set get_supported_input_file_extensions() { return {".xml", ".chkpt"}; } \ No newline at end of file diff --git a/src/io/input/custom_formats/FileLineReader.cpp b/src/io/input/custom_formats/FileLineReader.cpp deleted file mode 100644 index 1cd6fcfa9..000000000 --- a/src/io/input/custom_formats/FileLineReader.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "FileLineReader.h" - -#include "io/logger/Logger.h" - -FileLineReader::FileLineReader(const std::string& filepath) : filepath(filepath), file_stream(filepath) {} - -void FileLineReader::skipLine() { - std::string buffer; - std::getline(file_stream, buffer); - current_line_number++; -} - -void FileLineReader::nextLine() { - std::getline(file_stream, curr_line); - current_line_number++; - curr_line_stream = std::stringstream(curr_line); -} - -const std::string& FileLineReader::getLine() const { return curr_line; } - -std::stringstream& FileLineReader::getLineStream() { return curr_line_stream; } - -bool FileLineReader::is_open() const { return file_stream.is_open(); } - -bool FileLineReader::eof() const { return file_stream.eof(); } - -int FileLineReader::peek() { return file_stream.peek(); } - -int FileLineReader::getLineNumber() const { return current_line_number; } - -const std::string& FileLineReader::getFilePath() const { return filepath; } \ No newline at end of file diff --git a/src/io/input/custom_formats/FileLineReader.h b/src/io/input/custom_formats/FileLineReader.h deleted file mode 100644 index d47e5bfdb..000000000 --- a/src/io/input/custom_formats/FileLineReader.h +++ /dev/null @@ -1,96 +0,0 @@ -#pragma once - -#include -#include -#include - -/** - * @brief Class to provide a convenient way to read a file line by line - * - * This class provides a convenient way to read a file line by line. It is the key component of the custom file readers. - */ -class FileLineReader { - /** - * @brief Path of the file being read - */ - const std::string filepath; - - /** - * @brief The internal file stream used to read the file - */ - std::ifstream file_stream; - - /** - * @brief The content of the last line read from the file - */ - std::string curr_line; - - /** - * @brief The remaining unprocessed part of the last line read from the file - */ - std::stringstream curr_line_stream; - - /** - * @brief The line number of the last line read from the file - */ - int current_line_number = 0; - - public: - /** - * @brief Construct a new FileLineReader object - * @param filepath Name of the file to read - */ - explicit FileLineReader(const std::string& filepath); - - /** - * @brief Skip the next line in the file, does and update the value of `getLineNumber()`, the line content however is not stored - */ - void skipLine(); - - /** - * @brief Read the next line from the file and update the values of `getLine()` getLineStream()` and `getLineNumber()` - */ - void nextLine(); - - /** - * @brief Return the content of the last line read from the file - * @return The content of the last line read from the file - */ - const std::string& getLine() const; - - /** - * @brief Reference to the remaining unprocessed part of the last line read from the file - * @return The remaining unprocessed part of the last line read from the file - */ - std::stringstream& getLineStream(); - - /** - * @brief Check if the file is open, wrapper for `std::ifstream::is_open()` - * @return True if the file is open, false otherwise - */ - bool is_open() const; - - /** - * @brief Check if the end of the file has been reached, wrapper for `std::ifstream::eof()` - * @return True if the end of the file has been reached, false otherwise - */ - bool eof() const; - - /** - * @brief Get the next character from the file without extracting it, wrapper for `std::ifstream::peek()` - * @return The next character from the file - */ - int peek(); - - /** - * @brief Get the line number of the last line returned by `getline()` - * @return The line number of the last line returned by `getline()` - */ - int getLineNumber() const; - - /** - * @brief Get the path of the file - * @return The path of the file - */ - const std::string& getFilePath() const; -}; \ No newline at end of file diff --git a/src/io/input/custom_formats/cub/CubFileReader.cpp b/src/io/input/custom_formats/cub/CubFileReader.cpp deleted file mode 100644 index 428ab3c45..000000000 --- a/src/io/input/custom_formats/cub/CubFileReader.cpp +++ /dev/null @@ -1,92 +0,0 @@ -#include "CubFileReader.h" - -#include - -#include "particles/spawners/cuboid/CuboidSpawner.h" - -std::tuple, std::optional> CubFileReader::readFile(const std::string& filepath) const { - FileLineReader input_file(filepath); - - if (!input_file.is_open()) { - throw FileFormatException(fmt::format("Error: could not open file '{}'.", filepath)); - } - - std::vector particles; - - while (!input_file.eof()) { - while (input_file.peek() == '#' || input_file.peek() == '\n') { - input_file.skipLine(); - } - - input_file.nextLine(); - double x, y, z; - input_file.getLineStream() >> x >> y >> z >> std::ws; - checkAndReportInvalidEntry(input_file, " "); - std::array lower_left_front_corner{x, y, z}; - - input_file.nextLine(); - int nx, ny, nz; - input_file.getLineStream() >> nx >> ny >> nz >> std::ws; - checkAndReportInvalidEntry(input_file, " "); - std::array grid_dimensions{nx, ny, nz}; - - input_file.nextLine(); - double grid_spacing; - input_file.getLineStream() >> grid_spacing >> std::ws; - checkAndReportInvalidEntry(input_file, ""); - - input_file.nextLine(); - double mass; - input_file.getLineStream() >> mass >> std::ws; - checkAndReportInvalidEntry(input_file, ""); - - input_file.nextLine(); - double vx, vy, vz; - input_file.getLineStream() >> vx >> vy >> vz >> std::ws; - checkAndReportInvalidEntry(input_file, " "); - std::array initial_velocity{vx, vy, vz}; - - input_file.nextLine(); - double mean_wobbling_velocity; - input_file.getLineStream() >> mean_wobbling_velocity >> std::ws; - checkAndReportInvalidEntry(input_file, ""); - - input_file.nextLine(); - int type; - input_file.getLineStream() >> type >> std::ws; - checkAndReportInvalidEntry(input_file, ""); - - auto spawner = CuboidSpawner(lower_left_front_corner, grid_dimensions, grid_spacing, mass, initial_velocity, type); - - spawner.spawnParticles(particles); - } - - return std::make_tuple(particles, std::nullopt); -} - -void CubFileReader::checkAndReportInvalidEntry(FileLineReader& input_file, const std::string& expected_format) { - if (input_file.getLineStream().fail()) { - auto error_msg = fmt::format( - "Invalid entry in file '{}' on line {}.\n" - "\t Expected format: '{}'\n" - "\t Got: '{}'", - input_file.getFilePath(), input_file.getLineNumber(), expected_format, input_file.getLine()); - - throw FileFormatException(error_msg); - } - - if (input_file.getLineStream().peek() != '#' && input_file.getLineStream().peek() != EOF) { - auto error_msg = fmt::format( - "Invalid entry in file '{}' on line {}.\n" - "\t Comments must start with: '#', but got: '{}'\n" - "\t Content of line: '{}'", - input_file.getFilePath(), input_file.getLineNumber(), static_cast(input_file.getLineStream().peek()), - input_file.getLine()); - - if (input_file.getLine().find('#') != std::string::npos) { - error_msg += "\n\t Make sure that comments start after the arguments in the line."; - } - - throw FileFormatException(error_msg); - } -} \ No newline at end of file diff --git a/src/io/input/custom_formats/cub/CubFileReader.h b/src/io/input/custom_formats/cub/CubFileReader.h deleted file mode 100644 index ba3189b00..000000000 --- a/src/io/input/custom_formats/cub/CubFileReader.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include "io/input/FileReader.h" -#include "io/input/custom_formats/FileLineReader.h" - -/** - * @brief Class to read particle data from a '.cub' file - */ -class CubFileReader : public FileReader { - public: - /** - * @brief Reads the '.cub' file with the given path and returns a vector of particles - * (see \ref InputFileFormats "Input File Formats" for details on the .cub file format) - * - * @param filepath Path to the file to be read - */ - [[nodiscard]] std::tuple, std::optional> readFile(const std::string& filepath) const override; - - private: - /** - * @brief Reports an invalid entry in the given file and terminates the full program - * - * @param input_file FileLineReader which read the entry - * @param expected_format Expected format of the entry - */ - static void checkAndReportInvalidEntry(FileLineReader& input_file, const std::string& expected_format); -}; \ No newline at end of file diff --git a/src/io/input/custom_formats/ps/PsFileReader.cpp b/src/io/input/custom_formats/ps/PsFileReader.cpp deleted file mode 100644 index 8219ef108..000000000 --- a/src/io/input/custom_formats/ps/PsFileReader.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "PsFileReader.h" - -#include - -#include -#include - -std::tuple, std::optional> PsFileReader::readFile(const std::string& filepath) const { - std::array x{}; - std::array v{}; - double m; - int num_particles = 0; - - std::ifstream input_file(filepath); - std::string curr_line; - - if (!input_file.is_open()) { - throw FileFormatException(fmt::format("Error: could not open file '{}'.", filepath)); - } - - getline(input_file, curr_line); - - while (curr_line.empty() or curr_line[0] == '#') { - getline(input_file, curr_line); - } - - // Initialize particle container - std::vector particles; - - std::istringstream numstream(curr_line); - numstream >> num_particles; - getline(input_file, curr_line); - - for (int i = 0; i < num_particles; i++) { - std::istringstream datastream(curr_line); - - for (auto& xj : x) { - datastream >> xj; - } - for (auto& vj : v) { - datastream >> vj; - } - if (datastream.eof()) { - throw FileFormatException(fmt::format("Error reading file: eof reached unexpectedly reading from line {}.", i)); - } - datastream >> m; - - particles.emplace_back(x, v, m, i); - - getline(input_file, curr_line); - } - - return std::make_tuple(particles, std::nullopt); -} diff --git a/src/io/input/custom_formats/ps/PsFileReader.h b/src/io/input/custom_formats/ps/PsFileReader.h deleted file mode 100644 index f64f83d20..000000000 --- a/src/io/input/custom_formats/ps/PsFileReader.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "io/input/FileReader.h" - -/** - * @brief Class to read particle data from a '.ps' file - */ -class PsFileReader : public FileReader { - public: - /** - * @brief Reads the file with the given path and returns a vector of particles - * (see \ref InputFileFormats "Input File Formats" for details on the .ps file format). - * - * @param filepath Path to the file to be read - */ - [[nodiscard]] std::tuple, std::optional> readFile(const std::string& filepath) const override; -}; diff --git a/tests/data/input/CubExample.cub b/tests/data/input/CubExample.cub deleted file mode 100644 index 73e088e2c..000000000 --- a/tests/data/input/CubExample.cub +++ /dev/null @@ -1,17 +0,0 @@ -# Cube 1 -1.0 2.0 3.0 # lower left -4 7 3 # grid dimensions -2.0 # grid spacing -3.14 # mass --1.0 -2.0 -3.0 # velocity -0.69 # mean value brownian -42 # type - -# Cube 2 -50.0 60.0 70.0 # lower left -2 2 1 # grid dimensions -1.345 # grid spacing -6.0 # mass --11.0 -10.0 6.0 # velocity -0.1 # mean value brownian -112 # type \ No newline at end of file diff --git a/tests/io/filereaders/TestCubFileReader.cpp b/tests/io/filereaders/TestCubFileReader.cpp deleted file mode 100644 index ece0f34bc..000000000 --- a/tests/io/filereaders/TestCubFileReader.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include - -#include "data/FileLoader.h" -#include "io/input/custom_formats/cub/CubFileReader.h" - -#define EXPECT_CONTAINS_POS_NEAR(list, point, tol) \ - EXPECT_TRUE(std::find_if((list).begin(), (list).end(), [&](auto& x) { return ArrayUtils::L2Norm(x - (point)) < (tol); }) != \ - (list).end()); - -/* - * Test if a CubFileReader reads the correct data out of the files. - */ -TEST(CubFileReader, CorrectReadingOfParticles) { - CubFileReader cub_file_reader; - - auto [particles, params] = cub_file_reader.readFile(FileLoader::get_input_file_path("CubExample.cub")); - - EXPECT_EQ(particles.size(), 4 * 7 * 3 + 2 * 2 * 1); - - EXPECT_NEAR((particles)[0].getM(), 3.14, 1e-13); - EXPECT_EQ((particles)[0].getType(), 42); - - EXPECT_NEAR((particles)[particles.size() - 1].getM(), 6.0, 1e-13); - EXPECT_EQ((particles)[particles.size() - 1].getType(), 112); -} \ No newline at end of file