OpenLB 1.8.1
Loading...
Searching...
No Matches
tupleParser.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2024 Dennis Teutscher
4 *
5 * E-mail contact: info@openlb.net
6 * The most recent release of OpenLB can be downloaded at
7 * <http://www.openlb.net/>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25#include "core/superLattice.h"
26
27#include <tinyxml2.h> // Include TinyXML2 header
28#include <iostream>
29#include <sstream>
30#include <vector>
31#include <string>
32#include <map>
33
34namespace olb {
35
36template<typename T, typename DESCRIPTOR>
38private:
39 std::string filename;
40 std::vector<std::vector<std::string>> momentaLists;
41 std::vector<std::string> equilibriaList;
42 std::vector<std::string> collisionList;
43 std::vector<int> indicators;
44
45public:
46 DynamicsTupleParser(const std::string& filename) : filename(filename) {
48 }
49
51
52 std::vector<std::string> generateDynamicsStrings() {
53 std::vector<std::string> dynamicsList;
54 for (size_t i = 0; i < momentaLists.size(); i++) {
55 std::string dynamicsString = "dynamics::" + momentaLists[i][0] + momentaLists[i][1] + "," +
56 momentaLists[i][2] + "," + momentaLists[i][3] + "," +
57 momentaLists[i][4] + ">," + equilibriaList[i] + "," +
58 collisionList[i] + ",Default>";
59 dynamicsList.push_back(dynamicsString);
60 }
61 return dynamicsList;
62 }
63
64 std::vector<std::string> readTupleFromXML() {
65 OstreamManager clout("Tupleparser");
66 tinyxml2::XMLDocument doc;
67 if (doc.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS) {
68 std::cerr << "Failed to load file: " << filename << std::endl;
69 return {};
70 }
71
72 tinyxml2::XMLElement* paramElement = doc.FirstChildElement("Param");
73 if (!paramElement) {
74 std::cerr << "No 'Param' element found in XML. Exiting.\n";
75 return {};
76 }
77
78 tinyxml2::XMLElement* tuplesElement = paramElement->FirstChildElement("Methods");
79 if (!tuplesElement) {
80 std::cerr << "No 'Methods' element found in 'Param'. Exiting.\n";
81 return {};
82 }
83
84 std::vector<std::string> dynamics;
85
86 // Iterate through all Map elements
87 for (tinyxml2::XMLElement* mapElement = tuplesElement->FirstChildElement("Map");
88 mapElement; mapElement = mapElement->NextSiblingElement("Map")) {
89 for (tinyxml2::XMLElement* dynamicElement = mapElement->FirstChildElement("Dynamic");
90 dynamicElement; dynamicElement = dynamicElement->NextSiblingElement("Dynamic")) {
91 std::string dynamicString;
92 createDynamicString(dynamicElement, dynamicString);
93 clout << dynamicString << std::endl;
94 dynamics.push_back("dynamics::Tuple<" + dynamicString + ",Default>");
95 }
96 }
97 return dynamics;
98 }
99
100 void processElement(tinyxml2::XMLElement* element, std::string& dynamicString) {
101 if (!element) {
102 return;
103 }
104
105 tinyxml2::XMLElement* childElement = element->FirstChildElement();
106 if (childElement) {
107 bool hasTextContent = false;
108 while (childElement) {
109 if (hasTextContent) {
110 dynamicString += ",";
111 }
112 if (childElement->FirstChildElement()) {
113 dynamicString += childElement->Value();
114 dynamicString += "<";
115 processElement(childElement, dynamicString);
116 dynamicString += ">";
117 } else {
118 dynamicString += childElement->Value();
119 hasTextContent = true;
120 }
121 childElement = childElement->NextSiblingElement();
122 }
123 }
124 }
125
126 void createDynamicString(tinyxml2::XMLElement* dynamicElement, std::string& dynamicString) {
127 if (!dynamicElement) {
128 return;
129 }
130
131 tinyxml2::XMLElement* momentaElement = dynamicElement->FirstChildElement("Momenta");
132 tinyxml2::XMLElement* equilibriaElement = dynamicElement->FirstChildElement("Equilibria");
133 tinyxml2::XMLElement* collisionElement = dynamicElement->FirstChildElement("Collision");
134
135 if (momentaElement) {
136 dynamicString += "Momenta<";
137 processElement(momentaElement, dynamicString);
138 dynamicString += ">,";
139 }
140
141 if (equilibriaElement) {
142 dynamicString += equilibriaElement->FirstChild()->Value();
143 dynamicString += ",";
144 }
145
146 if (collisionElement) {
147 processElement(collisionElement, dynamicString);
148 }
149
150 if (!dynamicString.empty() && dynamicString.back() == ',') {
151 dynamicString.pop_back();
152 }
153 }
154
155 std::vector<int> readIndicatorFromXML() {
156 tinyxml2::XMLDocument doc;
157 if (doc.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS) {
158 std::cerr << "Failed to load file: " << filename << std::endl;
159 return {};
160 }
161
162 tinyxml2::XMLElement* paramElement = doc.FirstChildElement("Param");
163 if (!paramElement) {
164 std::cerr << "No 'Param' element found in XML. Exiting.\n";
165 return {};
166 }
167
168 tinyxml2::XMLElement* tuplesElement = paramElement->FirstChildElement("Methods");
169 if (!tuplesElement) {
170 std::cerr << "No 'Methods' element found in 'Param'. Exiting.\n";
171 return {};
172 }
173
174 // Iterate through all Map elements
175 for (tinyxml2::XMLElement* mapElement = tuplesElement->FirstChildElement("Map");
176 mapElement; mapElement = mapElement->NextSiblingElement("Map")) {
177 tinyxml2::XMLElement* indicatorElement = mapElement->FirstChildElement("Indicator");
178 if (indicatorElement) {
179 const char* indicatorText = indicatorElement->GetText();
180 if (indicatorText) {
181 indicators.push_back(std::stoi(indicatorText));
182 }
183 }
184 }
185 return indicators;
186 }
187
188 std::map<std::string, float> readParameterFromXML() {
189 tinyxml2::XMLDocument doc;
190 if (doc.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS) {
191 std::cerr << "Failed to load file: " << filename << std::endl;
192 return {};
193 }
194
195 tinyxml2::XMLElement* paramElement = doc.FirstChildElement("Param");
196 if (!paramElement) {
197 std::cerr << "No 'Param' element found in XML. Exiting.\n";
198 return {};
199 }
200
201 tinyxml2::XMLElement* methodsElement = paramElement->FirstChildElement("Methods");
202 if (!methodsElement) {
203 std::cerr << "No 'Methods' element found in 'Param'. Exiting.\n";
204 return {};
205 }
206
207 std::map<std::string, float> parameterMap;
208
209 // Iterate through all Map elements
210 for (tinyxml2::XMLElement* mapElement = methodsElement->FirstChildElement("Map");
211 mapElement; mapElement = mapElement->NextSiblingElement("Map")) {
212 tinyxml2::XMLElement* dynamicElement = mapElement->FirstChildElement("Dynamic");
213 if (!dynamicElement) {
214 std::cerr << "No 'Dynamic' element found in 'Map'. Skipping.\n";
215 continue;
216 }
217
218 tinyxml2::XMLElement* parameterElement = dynamicElement->FirstChildElement("Parameter");
219 if (parameterElement) {
220 // Iterate through all children of the Parameter element
221 for (tinyxml2::XMLElement* childElement = parameterElement->FirstChildElement();
222 childElement; childElement = childElement->NextSiblingElement()) {
223 std::string tagName = childElement->Value();
224 std::string tagValue = childElement->GetText() ? childElement->GetText() : "";
225 try {
226 float value = std::stof(tagValue);
227 parameterMap[tagName] = value;
228 } catch (const std::invalid_argument& e) {
229 std::cerr << "Invalid value for tag " << tagName << ": " << tagValue << std::endl;
230 } catch (const std::out_of_range& e) {
231 std::cerr << "Value out of range for tag " << tagName << ": " << tagValue << std::endl;
232 }
233 }
234 }
235 }
236 return parameterMap;
237 }
238};
239
240} // namespace olb
void processElement(tinyxml2::XMLElement *element, std::string &dynamicString)
std::vector< int > readIndicatorFromXML()
void createDynamicString(tinyxml2::XMLElement *dynamicElement, std::string &dynamicString)
std::vector< std::string > readTupleFromXML()
Definition tupleParser.h:64
std::vector< std::string > generateDynamicsStrings()
Definition tupleParser.h:52
std::map< std::string, float > readParameterFromXML()
DynamicsTupleParser(const std::string &filename)
Definition tupleParser.h:46
class for marking output with some text
Top level namespace for all of OpenLB.