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