OGS
MergeMeshToBulkMesh.cpp
Go to the documentation of this file.
1
13
14#include <tclap/CmdLine.h>
15
16#include <algorithm>
17#include <memory>
18#include <string>
19#include <unordered_map>
20#include <vector>
21
22#include "BaseLib/FileTools.h"
23#include "BaseLib/Logging.h"
24#include "BaseLib/MPI.h"
25#include "BaseLib/RunTime.h"
26#include "BaseLib/StringTools.h"
28#include "InfoLib/GitInfo.h"
33#include "MeshLib/Mesh.h"
34#include "MeshLib/Node.h"
35
36int main(int argc, char* argv[])
37{
38 TCLAP::CmdLine cmd(
39 "Tool merges one mesh to a bulk mesh.\n\n"
40 "OpenGeoSys-6 software, version " +
42 ".\n"
43 "Copyright (c) 2012-2025, OpenGeoSys Community "
44 "(http://www.opengeosys.org)",
46 TCLAP::ValueArg<std::string> bulk_mesh_in(
47 "b", "bulk-mesh-input-file",
48 "Input (.vtk | .msh). The name of the file containing the input bulk "
49 "mesh",
50 true, "", "INPUT_FILE");
51 cmd.add(bulk_mesh_in);
52 TCLAP::ValueArg<std::string> mesh_in(
53 "i", "mesh-input-file",
54 "Input (.vtk | .msh). The name of the file containing the input mesh "
55 "to be merged",
56 true, "", "INPUT_FILE");
57 cmd.add(mesh_in);
58 TCLAP::ValueArg<std::string> mesh_out(
59 "o", "mesh-output-file",
60 "Output (.vtk | .msh). The name of the file the merged mesh should be "
61 "written to",
62 true, "", "OUTPUT_FILE");
63 cmd.add(mesh_out);
64
65 TCLAP::ValueArg<double> p("", "pressure",
66 "initial pressure value in the mesh to be merged",
67 false, 0.0, "PRESSURE");
68 cmd.add(p);
69
70 TCLAP::ValueArg<double> pg(
71 "", "gas_pressure",
72 "initial gas pressure value in the mesh to be merged", false, 0.0,
73 "GAS_PRESSURE");
74 cmd.add(pg);
75
76 TCLAP::ValueArg<double> pc(
77 "", "capillary_pressure",
78 "initial capillary pressure value in the mesh to be merged", false, 0.0,
79 "CAPILLARY_PRESSURE");
80 cmd.add(pc);
81
82 TCLAP::ValueArg<double> T(
83 "", "temperature", "initial temperature value in the mesh to be merged",
84 false, 290.0, "TEMPERATURE");
85 cmd.add(T);
86
87 TCLAP::ValueArg<double> sxx(
88 "", "sigma_xx", "initial stress xx value in the mesh to be merged",
89 false, 0.0, "SIGMA_XX");
90 cmd.add(sxx);
91 TCLAP::ValueArg<double> syy(
92 "", "sigma_yy", "initial stress yy value in the mesh to be merged",
93 false, 0.0, "SIGMA_YY");
94 cmd.add(syy);
95 TCLAP::ValueArg<double> szz(
96 "", "sigma_zz", "initial stress zz value in the mesh to be merged",
97 false, 0.0, "SIGMA_ZZ");
98 cmd.add(szz);
99
100 TCLAP::ValueArg<int> mat_id(
101 "", "material_id", "Material ID of the mesh to be merged, (min = 0)",
102 false, 0.0, "MATERIAL_ID");
103 cmd.add(mat_id);
104 auto log_level_arg = BaseLib::makeLogLevelArg();
105 cmd.add(log_level_arg);
106
107 cmd.parse(argc, argv);
108
109 BaseLib::MPI::Setup mpi_setup(argc, argv);
110 BaseLib::initOGSLogger(log_level_arg.getValue());
111
112 BaseLib::RunTime timer;
113 timer.start();
114
115 std::unordered_map<std::string, double> initial_value_dict;
116 initial_value_dict.insert({"p", p.getValue()});
117 initial_value_dict.insert({"pg", pg.getValue()});
118 initial_value_dict.insert({"pc", pc.getValue()});
119 initial_value_dict.insert({"T", T.getValue()});
120 initial_value_dict.insert({"sxx", sxx.getValue()});
121 initial_value_dict.insert({"syy", syy.getValue()});
122 initial_value_dict.insert({"szz", szz.getValue()});
123 initial_value_dict.insert({"mat_id", mat_id.getValue()});
124
125 auto read_mesh = [](std::string const& mesh_file_name)
126 {
127 std::unique_ptr<MeshLib::Mesh> mesh(MeshLib::IO::readMeshFromFile(
128 mesh_file_name, true /* compute_element_neighbors */));
129
130 if (!mesh)
131 {
132 OGS_FATAL("Could not read the mesh {:s}", mesh_file_name);
133 }
134
135 INFO("Read {:s}: {:d} nodes, {:d} elements.", mesh_file_name,
136 mesh->getNumberOfNodes(), mesh->getNumberOfElements());
137 return mesh;
138 };
139
140 auto bulk_mesh = read_mesh(bulk_mesh_in.getValue());
141 auto const mesh_to_be_merged = read_mesh(mesh_in.getValue());
142
143 auto merged_mesh = MeshToolsLib::mergeMeshToBulkMesh(
144 *bulk_mesh, *mesh_to_be_merged, initial_value_dict);
145
146 MeshLib::IO::VtuInterface writer(merged_mesh.get());
147
148 auto const result = writer.writeToFile(mesh_out.getValue());
149 if (!result)
150 {
151 ERR("Could not write mesh to '{:s}'.", mesh_out.getValue());
152 return EXIT_FAILURE;
153 }
154 INFO("It took {} s", timer.elapsed());
155
156 // The merged nodes are moved to the new mesh, and the duplicated nodes on
157 // the mesh interface have already been deleted in
158 // MeshToolsLib::mergeMeshToBulkMesh. The elements are also moved to the
159 // merged mesh. The merged mesh object handles the release of the memory
160 // allocated for nodes and elements. Therefore, shallowClean is called.
161 mesh_to_be_merged->shallowClean();
162 bulk_mesh->shallowClean();
163
164 return EXIT_SUCCESS;
165}
int main(int argc, char *argv[])
Definition of the Element class.
#define OGS_FATAL(...)
Definition Error.h:26
Filename manipulation routines.
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:36
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:48
Definition of the Mesh class.
Definition of the Node class.
Definition of the RunTime class.
Definition of string helper functions.
Implementation of the VtuInterface class.
Count the running time.
Definition RunTime.h:29
double elapsed() const
Get the elapsed time in seconds.
Definition RunTime.h:42
void start()
Start the timer.
Definition RunTime.h:32
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
bool writeToFile(std::filesystem::path const &file_path)
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:64
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
std::unique_ptr< MeshLib::Mesh > mergeMeshToBulkMesh(MeshLib::Mesh const &bulk_mesh, MeshLib::Mesh const &other_mesh, std::unordered_map< std::string, double > &initial_value_dict)
Definition of readMeshFromFile function.