OGS
postLIE.cpp
Go to the documentation of this file.
1
10#include <tclap/CmdLine.h>
11
12#ifdef USE_PETSC
13#include <mpi.h>
14#endif
15
16#include <boost/property_tree/ptree.hpp>
17#include <boost/property_tree/xml_parser.hpp>
18#include <map>
19#include <memory>
20#include <vector>
21
22#include "BaseLib/FileTools.h"
23#include "InfoLib/GitInfo.h"
26#include "MeshLib/Mesh.h"
30
31namespace
32{
33void postVTU(std::string const& int_vtu_filename,
34 std::string const& out_vtu_filename)
35{
36 // read VTU with simulation results
37 std::unique_ptr<MeshLib::Mesh const> mesh(
38 MeshLib::IO::readMeshFromFile(int_vtu_filename));
39 if (mesh->hasNonlinearElement())
40 {
41 mesh = MeshToolsLib::convertToLinearMesh(*mesh, mesh->getName());
42 }
43
44 // post-process
45 std::vector<MeshLib::Element*> vec_matrix_elements;
46 std::vector<int> vec_fracture_mat_IDs;
47 std::vector<std::vector<MeshLib::Element*>> vec_fracture_elements;
48 std::vector<std::vector<MeshLib::Element*>> vec_fracture_matrix_elements;
49 std::vector<std::vector<MeshLib::Node*>> vec_fracture_nodes;
50 std::vector<std::pair<std::size_t, std::vector<int>>>
51 vec_branch_nodeID_matIDs;
52 std::vector<std::pair<std::size_t, std::vector<int>>>
53 vec_junction_nodeID_matIDs;
55 *mesh, vec_matrix_elements, vec_fracture_mat_IDs, vec_fracture_elements,
56 vec_fracture_matrix_elements, vec_fracture_nodes,
57 vec_branch_nodeID_matIDs, vec_junction_nodeID_matIDs);
58
60 vec_fracture_mat_IDs,
61 vec_fracture_nodes,
62 vec_fracture_matrix_elements,
63 vec_branch_nodeID_matIDs,
64 vec_junction_nodeID_matIDs);
65
66 // create a new VTU file
67 INFO("create {:s}", out_vtu_filename);
68 MeshLib::IO::writeMeshToFile(post.getOutputMesh(), out_vtu_filename);
69}
70
71void postPVD(std::string const& in_pvd_filename,
72 std::string const& out_pvd_filename,
73 bool const allow_overwrite)
74{
75 auto const in_pvd_file_dir = BaseLib::extractPath(in_pvd_filename);
76 auto const out_pvd_file_dir = BaseLib::extractPath(out_pvd_filename);
77 INFO("start reading the PVD file {:s}", in_pvd_filename);
78 boost::property_tree::ptree pt;
79 read_xml(in_pvd_filename, pt,
80 boost::property_tree::xml_parser::trim_whitespace);
81
82 for (auto& dataset : pt.get_child("VTKFile.Collection"))
83 {
84 if (dataset.first != "DataSet")
85 {
86 continue;
87 }
88
89 // get VTU file name
90 auto const org_vtu_filename =
91 dataset.second.get<std::string>("<xmlattr>.file");
92 auto const org_vtu_filebasename =
93 BaseLib::extractBaseName(org_vtu_filename);
94 auto org_vtu_dir = BaseLib::extractPath(org_vtu_filename);
95 if (org_vtu_dir.empty())
96 {
97 org_vtu_dir = in_pvd_file_dir;
98 }
99 auto const org_vtu_filepath =
100 BaseLib::joinPaths(org_vtu_dir, org_vtu_filebasename);
101 INFO("processing {:s}...", org_vtu_filepath);
102
103 // post-process the VTU and save into the new file
104 auto const dest_vtu_filename = "post_" + org_vtu_filebasename;
105 auto const dest_vtu_filepath =
106 BaseLib::joinPaths(out_pvd_file_dir, dest_vtu_filename);
107 if (!allow_overwrite && BaseLib::IsFileExisting(dest_vtu_filepath))
108 {
109 INFO("The destination file already exists. Skip overwriting it.");
110 }
111 else
112 {
113 postVTU(org_vtu_filepath, dest_vtu_filepath);
114 }
115
116 // create a new VTU file and update XML
117 dataset.second.put("<xmlattr>.file", dest_vtu_filename);
118 }
119
120 // save into the new PVD file
121 INFO("save into the new PVD file {:s}", out_pvd_filename);
122 boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
123 write_xml(out_pvd_filename, pt, std::locale(), settings);
124}
125
126} // unnamed namespace
127
128int main(int argc, char* argv[])
129{
130 TCLAP::CmdLine cmd(
131 "Post-process results of the LIE approach.\n\n"
132 "OpenGeoSys-6 software, version " +
134 ".\n"
135 "Copyright (c) 2012-2024, OpenGeoSys Community "
136 "(http://www.opengeosys.org)",
138 TCLAP::ValueArg<std::string> arg_out_file(
139 "o", "output-file", "the name of the new PVD or VTU file", true, "",
140 "path");
141 cmd.add(arg_out_file);
142 TCLAP::ValueArg<std::string> arg_in_file(
143 "i", "input-file", "the original PVD or VTU file name", true, "",
144 "path");
145 cmd.add(arg_in_file);
146 TCLAP::SwitchArg nooverwrite_arg(
147 "",
148 "no-overwrite",
149 "don't overwrite existing post processed VTU files");
150 cmd.add(nooverwrite_arg);
151
152 cmd.parse(argc, argv);
153
154#ifdef USE_PETSC
155 MPI_Init(&argc, &argv);
156#endif
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#ifdef USE_PETSC
174 MPI_Finalize();
175#endif
176 return EXIT_SUCCESS;
177}
#define OGS_FATAL(...)
Definition Error.h:26
Filename manipulation routines.
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
Definition of the Mesh class.
MeshLib::Mesh const & getOutputMesh() const
Definition PostUtils.h:44
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:50
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 > variable_output_names)
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:33
void postPVD(std::string const &in_pvd_filename, std::string const &out_pvd_filename, bool const allow_overwrite)
Definition postLIE.cpp:71
int main(int argc, char *argv[])
Definition postLIE.cpp:128
Definition of readMeshFromFile function.