Dune::Copasi
Loading...
Searching...
No Matches
cell_data_parser.hh
Go to the documentation of this file.
1#ifndef DUNE_COPASI_GRID_CELL_DATA_PARSER_HH
2#define DUNE_COPASI_GRID_CELL_DATA_PARSER_HH
3
7
8#include <dune/grid/common/rangegenerators.hh>
9
10#include <spdlog/spdlog.h>
11
12#include <dune/common/exceptions.hh>
13#include <dune/common/parametertree.hh>
14
15#include <vector>
16#include <fstream>
17
18namespace Dune::Copasi {
19
20namespace Impl {
21
22 // Read a datafile for the scalar griddata type & add it to cell_data
23 template<Dune::Concept::GridView 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);
26
27 // read the data file:
28 std::ifstream gridDataFile;
30
31 if(!gridDataFile.is_open()){
32 throw format_exception(IOError{}, "Could not open datafile");
33 }
34
35 std::string line;
36 int numDataLines = 0;
37 bool foundNumDataLines = false;
38
39 // Find first non comment line and format it to an integer which should
40 // provide the number of data lines in the file.
41 while (std::getline(gridDataFile, line)) {
42 if (line.empty() || line[0] == '#') {
43 continue; // Ignore comment lines
44 }
45 // Check if this line contains the number of data lines
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!");
49 }
50 foundNumDataLines = true;
51 break; // Found the number of data lines, exit the loop
52 }
53
54 if (!foundNumDataLines) {
55 throw format_exception(IOError{}, "IO format error -> number of data lines not found!");
56 }
57
58 std::map<std::size_t, T> data;
59
60 // Read the data lines (integer followed by double)
61 int intValue;
62 double doubleValue;
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");
66 }
67
68 // Note that this follows 0-base index.
69 // If user wants gmsh compatible indices, (s)he needs to subtract 1.
71 }
72 gridDataFile.close();
73 // add the data to the cell data manager
74 cell_data.addData(key, data);
75 }
76}
77
93template<Dune::Concept::GridView GV, class T>
95
96 auto& cell_data_config = grid_config.sub("cell_data");
97
98 // reserve the data needed to store the griddata
99 // --> Every element can contain cell data for every key
100 // --> Fixed look up table addresses providing (potentially) highest performance
101 // --> Cell data may already contain data (e.g. gmsh_id)
102 cell_data.reserve(cell_data.size() + cell_data_config.getSubKeys().size());
103
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");
114 } else {
115 throw format_exception(IOError{}, "Unknown type {}", type);
116 }
117 }
118}
119
120} // namespace Dune::Copasi
121
122#endif // DUNE_COPASI_GRID_CELL_DATA_PARSER_HH
Container for cell data of a grid view.
Definition cell_data.hh:25
Definition axis_names.hh:7
auto format_exception(Exception &&e, fmt::format_string< Args... > format, Args &&... args)
Definition exceptions.hh:23
constexpr bool is_bitflags_v
Alias for Bitflag indicator.
Definition bit_flags.hh:24
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