8#include <QtXml/QDomDocument>
33 if (XMLQtInterface::readFile(fileName) == 0)
38 QFileInfo fi(fileName);
40 (fi.path().length() > 3) ? QString(fi.path() +
"/") : fi.path();
42 QDomNode param_root = QDomNode();
43 QDomNode pvar_root = QDomNode();
44 QDomDocument doc(
"OGS-PROJECT-DOM");
46 QDomElement docElement = doc.documentElement();
47 if (docElement.nodeName().compare(
"OpenGeoSysProject"))
49 ERR(
"XmlPrjInterface::readFile(): Unexpected XML root.");
53 auto read_single_mesh = [&](QString
const& mesh_str)
55 std::unique_ptr<MeshLib::Mesh> mesh{
63 QDomNodeList fileList = docElement.childNodes();
65 for (
int i = 0; i < fileList.count(); i++)
67 QDomNode
const node(fileList.at(i));
68 QString
const node_name(node.nodeName());
69 QString
const file_name(node.toElement().text().trimmed());
70 if (file_name.isEmpty())
75 if (node_name ==
"geometry")
80 gml.
readFile(QString(path + file_name));
82 catch (std::runtime_error
const& err)
85 "Failed to read file `" + fileName +
"'");
88 else if (node_name ==
"stations")
91 stn.
readFile(QString(path + file_name));
93 else if (node_name ==
"mesh")
95 read_single_mesh(path + file_name);
97 else if (node_name ==
"meshes")
99 for (QDomNode meshes_node = node.firstChild();
100 meshes_node != QDomNode();
101 meshes_node = meshes_node.nextSibling())
103 if (!meshes_node.isElement())
105 ERR(
"Expected an XML element node.");
108 if (meshes_node.nodeName() !=
"mesh")
110 ERR(
"Expected an XML element node named 'mesh' got '{:s}'.",
111 meshes_node.nodeName().toStdString());
114 if (meshes_node.childNodes().count() != 1)
116 ERR(
"Expected an XML element node named 'mesh' to contain "
117 "exactly one child node but it has {:d} children.",
118 meshes_node.childNodes().count());
121 QDomNode node_text = meshes_node.firstChild();
122 if (!node_text.isText())
124 ERR(
"Expected an XML element node named 'mesh' to contain "
128 read_single_mesh(path + node_text.toText().data().trimmed());
132 else if (node_name ==
"parameters")
136 else if (node_name ==
"process_variables")
142 if (param_root != QDomNode() && pvar_root != QDomNode())
147 INFO(
"Skipping process variables");
153 QString
const& param_name)
const
155 QDomNode param = param_root.firstChild();
157 while (param != QDomNode())
159 QDomNodeList nodeList = param.childNodes();
160 for (
int i = 0; i < nodeList.count(); i++)
162 QDomNode node = nodeList.at(i);
165 if (node.nodeName() ==
"name" &&
166 node.toElement().text() == param_name)
171 param = param.nextSibling();
177 QDomNode
const& param_root)
179 QDomNode pvar = pvar_root.firstChild();
180 while (pvar != QDomNode())
183 QDomNodeList nodeList = pvar.childNodes();
184 for (
int i = 0; i < nodeList.count(); i++)
186 QDomNode
const node = nodeList.at(i);
187 QString
const node_name = node.nodeName();
188 if (node_name ==
"name")
190 process_var.
name = node.toElement().text().toStdString();
192 else if (node_name ==
"components")
194 process_var.
components = node.toElement().text().toInt();
196 else if (node_name ==
"order")
198 process_var.
order = node.toElement().text().toInt();
200 else if (node_name ==
"boundary_conditions")
204 else if (node_name ==
"source_terms")
209 pvar = pvar.nextSibling();
214 QDomNode
const& bc_root,
215 QDomNode
const& param_root,
218 QDomNode bc = bc_root.firstChild();
219 while (bc != QDomNode())
221 std::unique_ptr<DataHolderLib::BoundaryCondition> cond(
224 if (cond->getType() !=
227 _project.addBoundaryCondition(std::move(cond));
230 bc = bc.nextSibling();
235 QDomNode
const& st_root,
236 QDomNode
const& param_root,
239 QDomNode st = st_root.firstChild();
240 while (st != QDomNode())
242 std::unique_ptr<DataHolderLib::SourceTerm> cond(
246 _project.addSourceTerm(std::move(cond));
248 st = st.nextSibling();
254 QDomNode
const& node,
255 QDomNode
const& param_root,
260 typename T::ConditionType type = T::ConditionType::NONE;
261 std::string base_obj_name;
262 std::string obj_name;
263 std::string param_name;
264 QDomNode param_node = QDomNode();
265 QDomNodeList nodeList = node.childNodes();
266 for (
int i = 0; i < nodeList.count(); i++)
268 QString
const node_name = nodeList.at(i).nodeName();
269 QString
const content = nodeList.at(i).toElement().text().trimmed();
270 if (node_name ==
"geometrical_set" &&
273 base_obj_name = content.toStdString();
275 else if (node_name ==
"geometry" &&
278 obj_name = content.toStdString();
280 else if (node_name ==
"type")
282 type = T::convertStringToType(content.toStdString());
284 else if (node_name ==
"mesh")
287 base_obj_name = content.toStdString();
290 else if (node_name ==
"field_name")
292 param_name = content.toStdString();
294 else if (node_name ==
"parameter")
296 QDomNode val =
findParam(param_root, content);
297 if (val == QDomNode())
301 param_name = content.toStdString();
302 param_node = nodeList.at(i);
306 if (!param_name.empty())
308 T* cond =
new T(pvar, param_name, type);
311 cond->setMesh(base_obj_name);
315 cond->setGeoObject(base_obj_name, obj_name);
320 return new T({
"", 0, 0},
"", T::ConditionType::NONE);
332 QFileInfo fi(QString::fromStdString(
_filename));
333 std::string path((fi.absolutePath()).toStdString() +
"/");
335 out <<
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
337 out <<
"<?xml-stylesheet type=\"text/xsl\" "
338 "href=\"OpenGeoSysProject.xsl\"?>\n\n";
340 QDomDocument doc(
"OGS-PROJECT-DOM");
341 QDomElement root = doc.createElement(
"OpenGeoSysProject");
342 root.setAttribute(
"xmlns:ogs",
"http://www.opengeosys.org");
343 root.setAttribute(
"xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance");
345 doc.appendChild(root);
348 auto const& mesh_vector =
_project.getMeshObjects();
349 for (
auto const& mesh : mesh_vector)
355 QDomElement mesh_tag = doc.createElement(
"mesh");
356 root.appendChild(mesh_tag);
357 QDomText filename_text =
358 doc.createTextNode(QString::fromStdString(mesh->getName()));
359 mesh_tag.appendChild(filename_text);
369 std::string(path + name +
".gml")))
372 QDomElement geo_tag = doc.createElement(
"geometry");
373 root.appendChild(geo_tag);
374 QDomText filename_text =
375 doc.createTextNode(QString::fromStdString(name +
".gml"));
376 geo_tag.appendChild(filename_text);
379 ERR(
"XmlGmlInterface::writeFile(): Error writing gml-file '{:s}'.",
384 std::vector<std::string> stn_names;
386 for (std::string
const& name : stn_names)
393 path + name +
".stn"))
396 QDomElement stn_tag = doc.createElement(
"stations");
397 root.appendChild(stn_tag);
398 QDomText filename_text =
399 doc.createTextNode(QString::fromStdString(name +
".stn"));
400 stn_tag.appendChild(filename_text);
403 ERR(
"XmlStnInterface::writeFile(): Error writing stn-file '{:s}'.",
407 if (!
_project.getBoundaryConditions().empty() ||
414 std::string xml = doc.toString().toStdString();
421 QString
const& node_name,
422 QString
const& content)
424 QDomElement tag = doc.createElement(node_name);
425 parent.appendChild(tag);
426 QDomText order_text = doc.createTextNode(content);
427 tag.appendChild(order_text);
431 std::vector<DataHolderLib::ProcessVariable>
const& p_vars)
433 return std::any_of(p_vars.begin(), p_vars.end(),
434 [&](
auto const& p_var) { return p_var.name == name; });
437std::vector<DataHolderLib::ProcessVariable>
440 std::vector<std::unique_ptr<DataHolderLib::BoundaryCondition>>
const&
441 boundary_conditions =
_project.getBoundaryConditions();
442 std::vector<std::unique_ptr<DataHolderLib::SourceTerm>>
const&
443 source_terms =
_project.getSourceTerms();
445 std::vector<DataHolderLib::ProcessVariable> p_vars;
446 for (
auto& bc : boundary_conditions)
451 p_vars.push_back(pvar);
455 for (
auto& st : source_terms)
460 p_vars.push_back(pvar);
483 QString::fromStdString(T::convertTypeToString(
484 static_cast<T const&
>(cond).getType())));
493 QString::fromStdString(T::convertTypeToString(
494 static_cast<T const&
>(cond).getType())));
505 QDomElement& bc_list_tag,
506 std::string
const& name)
const
508 std::vector<std::unique_ptr<DataHolderLib::BoundaryCondition>>
const&
509 boundary_conditions =
_project.getBoundaryConditions();
510 for (
auto& bc : boundary_conditions)
512 if (bc->getProcessVarName() != name)
516 QDomElement bc_tag = doc.createElement(
"boundary_condition");
517 bc_list_tag.appendChild(bc_tag);
523 QDomElement& st_list_tag,
524 std::string
const& name)
const
526 std::vector<std::unique_ptr<DataHolderLib::SourceTerm>>
const&
527 source_terms =
_project.getSourceTerms();
528 for (
auto& st : source_terms)
530 if (st->getProcessVarName() != name)
534 QDomElement st_tag = doc.createElement(
"source_term");
535 st_list_tag.appendChild(st_tag);
541 QDomElement& root)
const
543 std::vector<DataHolderLib::ProcessVariable>
const p_vars(
546 QDomElement param_list_tag = doc.createElement(
"parameters");
547 root.appendChild(param_list_tag);
549 QDomElement pvar_list_tag = doc.createElement(
"process_variables");
550 root.appendChild(pvar_list_tag);
554 QDomElement pvar_tag = doc.createElement(
"process_variable");
555 pvar_list_tag.appendChild(pvar_tag);
556 addTextNode(doc, pvar_tag,
"name", QString::fromStdString(p_var.name));
557 addTextNode(doc, pvar_tag,
"order", QString::number(p_var.order));
559 QString::number(p_var.components));
560 QDomElement bc_list_tag = doc.createElement(
"boundary_conditions");
561 pvar_tag.appendChild(bc_list_tag);
563 QDomElement st_list_tag = doc.createElement(
"source_terms");
564 pvar_tag.appendChild(st_list_tag);
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
std::ostringstream out
The stream to write to.
std::string writeToString()
Writes the object to a string.
QByteArray const & getContent() const
XMLQtInterface(QString schemaFile="")
Base class for boundary conditions, initial conditions and source terms.
std::string getBaseObjName() const
Returns the name of the base object (i.e. geometry or mesh)
BaseObjType getBaseObjType() const
Specifies if the condition is set a geometry or on a mesh.
std::string const getObjName() const
Returns the name of the geometric object.
std::string const getParamName() const
Returns the name of the parameter associated with the condition.
void readBoundaryConditions(QDomNode const &bc_root, QDomNode const ¶m_root, DataHolderLib::ProcessVariable const &pvar)
Reading all boundary conditions.
T * parseCondition(QDomNode const &node, QDomNode const ¶m_root, DataHolderLib::ProcessVariable const &pvar) const
Parsing one specific condition.
int writeToFile(const std::string &filename)
Writes a project to the specified file.
std::vector< DataHolderLib::ProcessVariable > getPrimaryVariableVec() const
Compiles a vector of all existing primary variables for writing purposes.
bool write() override
Writes the object to the internal stream. This method must be implemented by a subclass....
void writeSourceTerms(QDomDocument &doc, QDomElement &st_list_tag, std::string const &name) const
Writes a list of source terms.
void readSourceTerms(QDomNode const &st_root, QDomNode const ¶m_root, DataHolderLib::ProcessVariable const &pvar)
Reading all source terms.
int readFile(const QString &fileName) override
Reads an xml-file containing a project.
void writeProcessVariables(QDomDocument &doc, QDomElement &root) const
Writes information on process variables.
DataHolderLib::Project & _project
void writeCondition(QDomDocument &doc, QDomElement &tag, DataHolderLib::FemCondition const &cond) const
Writes one specific condition.
void writeBoundaryConditions(QDomDocument &doc, QDomElement &bc_list_tag, std::string const &name) const
Writes a list of boundary conditions.
XmlPrjInterface(DataHolderLib::Project &project)
void readConditions(QDomNode const &pvar_root, QDomNode const ¶m_root)
Manages reading all kinds of conditions.
QDomNode findParam(QDomNode const ¶m_root, QString const ¶m_name) const
Tests if a given parameter exists within the file.
Container class for geometric objects.
std::vector< std::string > getGeometryNames() const
Returns the names of all geometry vectors.
void getStationVectorNames(std::vector< std::string > &names) const
Returns the names of all station vectors.
Reads and writes GeoObjects to and from XML files.
int readFile(const QString &fileName) override
Reads an xml-file containing geometric object definitions into the GEOObjects used in the constructor...
Reads and writes Observation Sites to and from XML files.
int readFile(const QString &fileName) override
Reads an xml-file containing station object definitions into the GEOObjects used in the constructor (...
static void box(const QString &e)
int writeStringToFile(std::string_view content, std::filesystem::path const &file_path)
void addTextNode(QDomDocument &doc, QDomElement &parent, QString const &node_name, QString const &content)
bool PVarExists(std::string const &name, std::vector< DataHolderLib::ProcessVariable > const &p_vars)
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > variable_output_names)