1#ifndef DUNE_COPASI_GRID_CELL_DATA_PARSER_HH
2#define DUNE_COPASI_GRID_CELL_DATA_PARSER_HH
8#include <dune/grid/common/rangegenerators.hh>
10#include <spdlog/spdlog.h>
12#include <dune/common/exceptions.hh>
13#include <dune/common/parametertree.hh>
23 template<Dune::Concept::Gr
idView GV,
class T>
24 void cell_data_scalar_parser(CellData<GV,T>& cell_data, std::string datafile_path, std::string key) {
25 spdlog::info(
"Reading grid cell data '{}'", datafile_path);
28 std::ifstream gridDataFile;
29 gridDataFile.open(datafile_path);
31 if(!gridDataFile.is_open()){
37 bool foundNumDataLines =
false;
41 while (std::getline(gridDataFile, line)) {
42 if (line.empty() || line[0] ==
'#') {
46 std::istringstream iss(line);
47 if (!(iss >> numDataLines)) {
48 throw format_exception(IOError{},
"IO format error -> format of number of data lines not correct!");
50 foundNumDataLines =
true;
54 if (!foundNumDataLines) {
55 throw format_exception(IOError{},
"IO format error -> number of data lines not found!");
58 std::map<std::size_t, T> data;
63 for (
int i = 0; i < numDataLines; ++i) {
64 if (!(gridDataFile >> intValue >> doubleValue)) {
65 throw format_exception(IOError{},
"IO format error -> error reading the dataformat");
70 data[intValue] = doubleValue;
74 cell_data.addData(key, data);
93template<Dune::Concept::Gr
idView GV,
class T>
96 auto& cell_data_config = grid_config.sub(
"cell_data");
102 cell_data.
reserve(cell_data.
size() + cell_data_config.getSubKeys().size());
104 for (
const auto& sub : cell_data_config.getSubKeys()) {
105 const auto& sub_config = cell_data_config.sub(sub,
true);
106 const std::string datafile_path = sub_config[
"path"];
107 const std::string type = sub_config[
"type"];
108 if (type ==
"scalar") {
109 Impl::cell_data_scalar_parser(cell_data, datafile_path, sub);
110 }
else if (type ==
"vector") {
111 throw format_exception(NotImplemented{},
"vector griddata is not implemented");
112 }
else if (type ==
"tensor") {
113 throw format_exception(NotImplemented{},
"tensor griddata is not implemented");
Container for cell data of a grid view.
Definition: cell_data.hh:25
std::size_t size() const noexcept
Number of elements per entity in the container.
Definition: cell_data.hh:92
void reserve(std::size_t new_cap)
Reservers new_cap elements per entity in the container.
Definition: cell_data.hh:106
Definition: axis_names.hh:7
auto format_exception(Exception &&e, fmt::format_string< Args... > format, Args &&... args)
Definition: exceptions.hh:23
void cell_data_parser(const ParameterTree &grid_config, CellData< GV, T > &cell_data)
Parse data file with cell data and add its values to cell data.
Definition: cell_data_parser.hh:94