From a008c5bf14856c4f2bccac8ba2009cb933803741 Mon Sep 17 00:00:00 2001 From: Lisa Julia Nebel <lisa_julia.nebel@tu-dresden.de> Date: Sun, 17 Jan 2021 15:10:36 +0100 Subject: [PATCH] Move the function reading input data to a filereader.hh --- dune/gfe/filereader.hh | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 dune/gfe/filereader.hh diff --git a/dune/gfe/filereader.hh b/dune/gfe/filereader.hh new file mode 100644 index 00000000..e0958b9d --- /dev/null +++ b/dune/gfe/filereader.hh @@ -0,0 +1,43 @@ +#ifndef DUNE_GFE_FILEREADER_HH +#define DUNE_GFE_FILEREADER_HH + +#include <iostream> +#include <fstream> + +#include <dune/common/fvector.hh> +// This is a custom file format parser which reads in a grid deformation file +// The file must contain lines of the format <grid vertex>:<displacement or rotation> +// !!! THIS IS A TEMPORARY SOLUTION AND WILL BE REPLACED BY THE Dune::VtkReader in dune-vtk/dune/vtk/vtkreader.hh !!! +namespace Dune { + namespace GFE { + // Convert the pairs {grid vertex, vector of dimension d} in the given file to a map + template <int d> + static std::unordered_map<std::string, FieldVector<double,d>> transformFileToMap(std::string pathToFile) { + + std::unordered_map<std::string, FieldVector<double,d>> map; + + std::string line, displacement, entry; + + std::ifstream file(pathToFile, std::ios::in); + + if (file.is_open()) { + while (std::getline(file, line)) { + size_t j = 0; + size_t pos = line.find(":"); + displacement = line.substr(pos + 1); + line.erase(pos); + std::stringstream entries(displacement); + FieldVector<double,d> vector(0); + while(entries >> entry) + vector[j++] = std::stod(entry); + map.insert({line,vector}); + } + file.close(); + } else { + DUNE_THROW(Exception, "Error: Could not open the file " + pathToFile + " !"); + } + return map; + } + } +} +#endif \ No newline at end of file -- GitLab