OGS
postLIE.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include <tclap/CmdLine.h>
5
6#include <boost/property_tree/ptree.hpp>
7#include <boost/property_tree/xml_parser.hpp>
8#include <map>
9#include <memory>
10#include <vector>
11
12#include "BaseLib/FileTools.h"
13#include "BaseLib/Logging.h"
14#include "BaseLib/MPI.h"
16#include "InfoLib/GitInfo.h"
19#include "MeshLib/Mesh.h"
23
24namespace
25{
26void postVTU(std::string const& int_vtu_filename,
27 std::string const& out_vtu_filename)
28{
29 // read VTU with simulation results
30 std::unique_ptr<MeshLib::Mesh const> mesh(
31 MeshLib::IO::readMeshFromFile(int_vtu_filename));
32 if (mesh->hasNonlinearElement())
33 {
34 mesh = MeshToolsLib::convertToLinearMesh(*mesh, mesh->getName());
35 }
36
37 // post-process
38 std::vector<MeshLib::Element*> vec_matrix_elements;
39 std::vector<int> vec_fracture_mat_IDs;
40 std::vector<std::vector<MeshLib::Element*>> vec_fracture_elements;
41 std::vector<std::vector<MeshLib::Element*>> vec_fracture_matrix_elements;
42 std::vector<std::vector<MeshLib::Node*>> vec_fracture_nodes;
43 std::vector<std::pair<std::size_t, std::vector<int>>>
44 vec_branch_nodeID_matIDs;
45 std::vector<std::pair<std::size_t, std::vector<int>>>
46 vec_junction_nodeID_matIDs;
48 *mesh, vec_matrix_elements, vec_fracture_mat_IDs, vec_fracture_elements,
49 vec_fracture_matrix_elements, vec_fracture_nodes,
50 vec_branch_nodeID_matIDs, vec_junction_nodeID_matIDs);
51
53 vec_fracture_mat_IDs,
54 vec_fracture_nodes,
55 vec_fracture_matrix_elements,
56 vec_branch_nodeID_matIDs,
57 vec_junction_nodeID_matIDs);
58
59 // create a new VTU file
60 INFO("create {:s}", out_vtu_filename);
61 if (MeshLib::IO::writeMeshToFile(post.getOutputMesh(), out_vtu_filename) !=
62 0)
63 {
64 return;
65 }
66}
67
68void postPVD(std::string const& in_pvd_filename,
69 std::string const& out_pvd_filename,
70 bool const allow_overwrite)
71{
72 auto const in_pvd_file_dir = BaseLib::extractPath(in_pvd_filename);
73 auto const out_pvd_file_dir = BaseLib::extractPath(out_pvd_filename);
74 INFO("start reading the PVD file {:s}", in_pvd_filename);
75 boost::property_tree::ptree pt;
76 read_xml(in_pvd_filename, pt,
77 boost::property_tree::xml_parser::trim_whitespace);
78
79 for (auto& dataset : pt.get_child("VTKFile.Collection"))
80 {
81 if (dataset.first != "DataSet")
82 {
83 continue;
84 }
85
86 // get VTU file name
87 auto const org_vtu_filename =
88 dataset.second.get<std::string>("<xmlattr>.file");
89 auto const org_vtu_filebasename =
90 BaseLib::extractBaseName(org_vtu_filename);
91 auto org_vtu_dir = BaseLib::extractPath(org_vtu_filename);
92 if (org_vtu_dir.empty())
93 {
94 org_vtu_dir = in_pvd_file_dir;
95 }
96 auto const org_vtu_filepath =
97 BaseLib::joinPaths(org_vtu_dir, org_vtu_filebasename);
98 INFO("processing {:s}...", org_vtu_filepath);
99
100 // post-process the VTU and save into the new file
101 auto const dest_vtu_filename = "post_" + org_vtu_filebasename;
102 auto const dest_vtu_filepath =
103 BaseLib::joinPaths(out_pvd_file_dir, dest_vtu_filename);
104 if (!allow_overwrite && BaseLib::IsFileExisting(dest_vtu_filepath))
105 {
106 INFO("The destination file already exists. Skip overwriting it.");
107 }
108 else
109 {
110 postVTU(org_vtu_filepath, dest_vtu_filepath);
111 }
112
113 // create a new VTU file and update XML
114 dataset.second.put("<xmlattr>.file", dest_vtu_filename);
115 }
116
117 // save into the new PVD file
118 INFO("save into the new PVD file {:s}", out_pvd_filename);
119 boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
120 write_xml(out_pvd_filename, pt, std::locale(), settings);
121}
122
123} // unnamed namespace
124
125int main(int argc, char* argv[])
126{
127 TCLAP::CmdLine cmd(
128 "Post-process results of the LIE approach.\n\n"
129 "OpenGeoSys-6 software, version " +
131 ".\n"
132 "Copyright (c) 2012-2026, OpenGeoSys Community "
133 "(http://www.opengeosys.org)",
135 TCLAP::ValueArg<std::string> arg_out_file(
136 "o", "output-file",
137 "Output (.vtu | .pvd). The name of the new PVD or VTU file", true, "",
138 "OUTPUT_PATH");
139 cmd.add(arg_out_file);
140 TCLAP::ValueArg<std::string> arg_in_file(
141 "i", "input-file",
142 "Input (.vtu | .pvd). The original PVD or VTU file name", true, "",
143 "INPUT_FILE");
144 cmd.add(arg_in_file);
145 TCLAP::SwitchArg nooverwrite_arg(
146 "",
147 "no-overwrite",
148 "don't overwrite existing post processed VTU files");
149 cmd.add(nooverwrite_arg);
150 auto log_level_arg = BaseLib::makeLogLevelArg();
151 cmd.add(log_level_arg);
152
153 cmd.parse(argc, argv);
154
155 BaseLib::MPI::Setup mpi_setup(argc, argv);
156 BaseLib::initOGSLogger(log_level_arg.getValue());
157
158 auto const in_file_ext = BaseLib::getFileExtension(arg_in_file.getValue());
159 if (in_file_ext == ".pvd")
160 {
161 postPVD(arg_in_file.getValue(), arg_out_file.getValue(),
162 !nooverwrite_arg.getValue());
163 }
164 else if (in_file_ext == ".vtu")
165 {
166 postVTU(arg_in_file.getValue(), arg_out_file.getValue());
167 }
168 else
169 {
170 OGS_FATAL("The given file type ({:s}) is not supported.", in_file_ext);
171 }
172
173 return EXIT_SUCCESS;
174}
#define OGS_FATAL(...)
Definition Error.h:19
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
MeshLib::Mesh const & getOutputMesh() const
Definition PostUtils.h:38
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
std::string getFileExtension(const std::string &path)
std::string extractPath(std::string const &pathname)
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists.
Definition FileTools.cpp:23
std::string joinPaths(std::string const &pathA, std::string const &pathB)
std::string extractBaseName(std::string const &pathname)
GITINFOLIB_EXPORT const std::string ogs_version
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 > output_variable_names, bool const use_compression, int const data_mode)
std::unique_ptr< MeshLib::Mesh > convertToLinearMesh(MeshLib::Mesh const &mesh, std::string const &new_mesh_name)
void getFractureMatrixDataInMesh(MeshLib::Mesh const &mesh, std::vector< MeshLib::Element * > &vec_matrix_elements, std::vector< int > &vec_fracture_mat_IDs, std::vector< std::vector< MeshLib::Element * > > &vec_fracture_elements, std::vector< std::vector< MeshLib::Element * > > &vec_fracture_matrix_elements, std::vector< std::vector< MeshLib::Node * > > &vec_fracture_nodes, std::vector< std::pair< std::size_t, std::vector< int > > > &vec_branch_nodeID_matIDs, std::vector< std::pair< std::size_t, std::vector< int > > > &vec_junction_nodeID_matIDs)
void postVTU(std::string const &int_vtu_filename, std::string const &out_vtu_filename)
Definition postLIE.cpp:26
void postPVD(std::string const &in_pvd_filename, std::string const &out_pvd_filename, bool const allow_overwrite)
Definition postLIE.cpp:68
int main(int argc, char *argv[])
Definition postLIE.cpp:125