15#include <QtXml/QDomDocument>
34 : XMLQtInterface(
"OpenGeoSysProject.xsd"), _filename(
""), _project(project)
40 if (XMLQtInterface::readFile(fileName) == 0)
45 QFileInfo fi(fileName);
47 (fi.path().length() > 3) ? QString(fi.path() +
"/") : fi.path();
49 QDomNode param_root = QDomNode();
50 QDomNode pvar_root = QDomNode();
51 QDomDocument doc(
"OGS-PROJECT-DOM");
53 QDomElement docElement = doc.documentElement();
54 if (docElement.nodeName().compare(
"OpenGeoSysProject"))
56 ERR(
"XmlPrjInterface::readFile(): Unexpected XML root.");
60 auto read_single_mesh = [&](QString
const& mesh_str)
62 std::unique_ptr<MeshLib::Mesh> mesh{
70 QDomNodeList fileList = docElement.childNodes();
72 for (
int i = 0; i < fileList.count(); i++)
74 QDomNode
const node(fileList.at(i));
75 QString
const node_name(node.nodeName());
76 QString
const file_name(node.toElement().text().trimmed());
77 if (file_name.isEmpty())
82 if (node_name ==
"geometry")
87 gml.
readFile(QString(path + file_name));
89 catch (std::runtime_error
const& err)
92 "Failed to read file `" + fileName +
"'");
95 else if (node_name ==
"stations")
98 stn.
readFile(QString(path + file_name));
100 else if (node_name ==
"mesh")
102 read_single_mesh(path + file_name);
104 else if (node_name ==
"meshes")
106 for (QDomNode meshes_node = node.firstChild();
107 meshes_node != QDomNode();
108 meshes_node = meshes_node.nextSibling())
110 if (!meshes_node.isElement())
112 ERR(
"Expected an XML element node.");
115 if (meshes_node.nodeName() !=
"mesh")
117 ERR(
"Expected an XML element node named 'mesh' got '{:s}'.",
118 meshes_node.nodeName().toStdString());
121 if (meshes_node.childNodes().count() != 1)
123 ERR(
"Expected an XML element node named 'mesh' to contain "
124 "exactly one child node but it has {:d} children.",
125 meshes_node.childNodes().count());
128 QDomNode node_text = meshes_node.firstChild();
129 if (!node_text.isText())
131 ERR(
"Expected an XML element node named 'mesh' to contain "
135 read_single_mesh(path + node_text.toText().data().trimmed());
139 else if (node_name ==
"parameters")
143 else if (node_name ==
"process_variables")
149 if (param_root != QDomNode() && pvar_root != QDomNode())
154 INFO(
"Skipping process variables");
160 QString
const& param_name)
const
162 QDomNode param = param_root.firstChild();
164 while (param != QDomNode())
166 QDomNodeList nodeList = param.childNodes();
167 for (
int i = 0; i < nodeList.count(); i++)
169 QDomNode node = nodeList.at(i);
172 if (node.nodeName() ==
"name" &&
173 node.toElement().text() == param_name)
178 param = param.nextSibling();
184 QDomNode
const& param_root)
186 QDomNode pvar = pvar_root.firstChild();
187 while (pvar != QDomNode())
190 QDomNodeList nodeList = pvar.childNodes();
191 for (
int i = 0; i < nodeList.count(); i++)
193 QDomNode
const node = nodeList.at(i);
194 QString
const node_name = node.nodeName();
195 if (node_name ==
"name")
197 process_var.
name = node.toElement().text().toStdString();
199 else if (node_name ==
"components")
201 process_var.
components = node.toElement().text().toInt();
203 else if (node_name ==
"order")
205 process_var.
order = node.toElement().text().toInt();
207 else if (node_name ==
"boundary_conditions")
211 else if (node_name ==
"source_terms")
216 pvar = pvar.nextSibling();
221 QDomNode
const& bc_root,
222 QDomNode
const& param_root,
225 QDomNode bc = bc_root.firstChild();
226 while (bc != QDomNode())
228 std::unique_ptr<DataHolderLib::BoundaryCondition> cond(
231 if (cond->getType() !=
237 bc = bc.nextSibling();
242 QDomNode
const& st_root,
243 QDomNode
const& param_root,
246 QDomNode st = st_root.firstChild();
247 while (st != QDomNode())
249 std::unique_ptr<DataHolderLib::SourceTerm> cond(
255 st = st.nextSibling();
261 QDomNode
const& node,
262 QDomNode
const& param_root,
267 typename T::ConditionType type = T::ConditionType::NONE;
268 std::string base_obj_name;
269 std::string obj_name;
270 std::string param_name;
271 QDomNode param_node = QDomNode();
272 QDomNodeList nodeList = node.childNodes();
273 for (
int i = 0; i < nodeList.count(); i++)
275 QString
const node_name = nodeList.at(i).nodeName();
276 QString
const content = nodeList.at(i).toElement().text().trimmed();
277 if (node_name ==
"geometrical_set" &&
280 base_obj_name = content.toStdString();
282 else if (node_name ==
"geometry" &&
285 obj_name = content.toStdString();
287 else if (node_name ==
"type")
289 type = T::convertStringToType(content.toStdString());
291 else if (node_name ==
"mesh")
294 base_obj_name = content.toStdString();
297 else if (node_name ==
"field_name")
299 param_name = content.toStdString();
301 else if (node_name ==
"parameter")
303 QDomNode val =
findParam(param_root, content);
304 if (val == QDomNode())
308 param_name = content.toStdString();
309 param_node = nodeList.at(i);
313 if (!param_name.empty())
315 T* cond =
new T(pvar, param_name, type);
318 cond->setMesh(base_obj_name);
322 cond->setGeoObject(base_obj_name, obj_name);
327 return new T({
"", 0, 0},
"", T::ConditionType::NONE);
339 QFileInfo fi(QString::fromStdString(
_filename));
340 std::string path((fi.absolutePath()).toStdString() +
"/");
342 out <<
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
344 out <<
"<?xml-stylesheet type=\"text/xsl\" "
345 "href=\"OpenGeoSysProject.xsl\"?>\n\n";
347 QDomDocument doc(
"OGS-PROJECT-DOM");
348 QDomElement root = doc.createElement(
"OpenGeoSysProject");
349 root.setAttribute(
"xmlns:ogs",
"http://www.opengeosys.org");
350 root.setAttribute(
"xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance");
352 doc.appendChild(root);
356 for (
auto const& mesh : mesh_vector)
362 QDomElement mesh_tag = doc.createElement(
"mesh");
363 root.appendChild(mesh_tag);
364 QDomText filename_text =
365 doc.createTextNode(QString::fromStdString(mesh->getName()));
366 mesh_tag.appendChild(filename_text);
376 std::string(path + name +
".gml")))
379 QDomElement geo_tag = doc.createElement(
"geometry");
380 root.appendChild(geo_tag);
381 QDomText filename_text =
382 doc.createTextNode(QString::fromStdString(name +
".gml"));
383 geo_tag.appendChild(filename_text);
386 ERR(
"XmlGmlInterface::writeFile(): Error writing gml-file '{:s}'.",
391 std::vector<std::string> stn_names;
393 for (std::string
const& name : stn_names)
400 path + name +
".stn"))
403 QDomElement stn_tag = doc.createElement(
"stations");
404 root.appendChild(stn_tag);
405 QDomText filename_text =
406 doc.createTextNode(QString::fromStdString(name +
".stn"));
407 stn_tag.appendChild(filename_text);
410 ERR(
"XmlStnInterface::writeFile(): Error writing stn-file '{:s}'.",
421 std::string xml = doc.toString().toStdString();
428 QString
const& node_name,
429 QString
const& content)
431 QDomElement tag = doc.createElement(node_name);
432 parent.appendChild(tag);
433 QDomText order_text = doc.createTextNode(content);
434 tag.appendChild(order_text);
438 std::vector<DataHolderLib::ProcessVariable>
const& p_vars)
440 return std::any_of(p_vars.begin(), p_vars.end(),
441 [&](
auto const& p_var) { return p_var.name == name; });
444std::vector<DataHolderLib::ProcessVariable>
447 std::vector<std::unique_ptr<DataHolderLib::BoundaryCondition>>
const&
449 std::vector<std::unique_ptr<DataHolderLib::SourceTerm>>
const&
452 std::vector<DataHolderLib::ProcessVariable> p_vars;
453 for (
auto& bc : boundary_conditions)
458 p_vars.push_back(pvar);
462 for (
auto& st : source_terms)
467 p_vars.push_back(pvar);
490 QString::fromStdString(T::convertTypeToString(
491 static_cast<T const&
>(cond).getType())));
500 QString::fromStdString(T::convertTypeToString(
501 static_cast<T const&
>(cond).getType())));
512 QDomElement& bc_list_tag,
513 std::string
const& name)
const
515 std::vector<std::unique_ptr<DataHolderLib::BoundaryCondition>>
const&
517 for (
auto& bc : boundary_conditions)
519 if (bc->getProcessVarName() != name)
523 QDomElement bc_tag = doc.createElement(
"boundary_condition");
524 bc_list_tag.appendChild(bc_tag);
530 QDomElement& st_list_tag,
531 std::string
const& name)
const
533 std::vector<std::unique_ptr<DataHolderLib::SourceTerm>>
const&
535 for (
auto& st : source_terms)
537 if (st->getProcessVarName() != name)
541 QDomElement st_tag = doc.createElement(
"source_term");
542 st_list_tag.appendChild(st_tag);
548 QDomElement& root)
const
550 std::vector<DataHolderLib::ProcessVariable>
const p_vars(
553 QDomElement param_list_tag = doc.createElement(
"parameters");
554 root.appendChild(param_list_tag);
556 QDomElement pvar_list_tag = doc.createElement(
"process_variables");
557 root.appendChild(pvar_list_tag);
561 QDomElement pvar_tag = doc.createElement(
"process_variable");
562 pvar_list_tag.appendChild(pvar_tag);
563 addTextNode(doc, pvar_tag,
"name", QString::fromStdString(p_var.name));
564 addTextNode(doc, pvar_tag,
"order", QString::number(p_var.order));
566 QString::number(p_var.components));
567 QDomElement bc_list_tag = doc.createElement(
"boundary_conditions");
568 pvar_tag.appendChild(bc_list_tag);
570 QDomElement st_list_tag = doc.createElement(
"source_terms");
571 pvar_tag.appendChild(st_list_tag);
Definition of the GEOObjects class.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition of the Mesh class.
Definition of the OGSError class.
Definition of the Writer class.
Definition of the XmlGmlInterface class.
Definition of the XmlStnInterface class.
std::ostringstream out
The stream to write to.
std::string writeToString()
Writes the object to a string.
QByteArray const & getContent() const
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 addBoundaryCondition(std::unique_ptr< BoundaryCondition > bc)
Adds a boundary condition to the project.
const std::vector< std::unique_ptr< MeshLib::Mesh > > & getMeshObjects() const
Returns all the meshes with their respective names.
void addMesh(std::unique_ptr< MeshLib::Mesh > mesh)
GeoLib::GEOObjects & getGEOObjects()
Returns the GEOObjects containing all points, polylines and surfaces.
std::vector< std::unique_ptr< SourceTerm > > const & getSourceTerms() const
Returns the vector of source terms.
void addSourceTerm(std::unique_ptr< SourceTerm > st)
Adds a source term to the project.
std::vector< std::unique_ptr< BoundaryCondition > > const & getBoundaryConditions() const
Returns the vector of boundary conditions.
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)
Definition of readMeshFromFile function.