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/MPI.h"
24#include "BaseLib/RunTime.h"
25#include "BaseLib/StringTools.h"
26#include "InfoLib/GitInfo.h"
31#include "MeshLib/Mesh.h"
32#include "MeshLib/Node.h"
33
34int main(int argc, char* argv[])
35{
36 TCLAP::CmdLine cmd(
37 "Tool merges one mesh to a bulk mesh.\n\n"
38 "OpenGeoSys-6 software, version " +
40 ".\n"
41 "Copyright (c) 2012-2025, OpenGeoSys Community "
42 "(http://www.opengeosys.org)",
44 TCLAP::ValueArg<std::string> bulk_mesh_in(
45 "b", "bulk-mesh-input-file",
46 "the name of the file containing the input bulk mesh", true, "",
47 "file name of the input bulk mesh");
48 cmd.add(bulk_mesh_in);
49 TCLAP::ValueArg<std::string> mesh_in(
50 "i", "mesh-input-file",
51 "the name of the file containing the input mesh to be merged", true, "",
52 "file name of the input mesh to be merged");
53 cmd.add(mesh_in);
54 TCLAP::ValueArg<std::string> mesh_out(
55 "o", "mesh-output-file",
56 "the name of the file the merged mesh should be written to", true, "",
57 "file name of the merged mesh for output");
58 cmd.add(mesh_out);
59
60 TCLAP::ValueArg<double> p("", "pressure",
61 "initial pressure value in the mesh to be merged",
62 false, 0.0, "floating point value");
63 cmd.add(p);
64
65 TCLAP::ValueArg<double> pg(
66 "", "gas_pressure",
67 "initial gas pressure value in the mesh to be merged", false, 0.0,
68 "floating point value");
69 cmd.add(pg);
70
71 TCLAP::ValueArg<double> pc(
72 "", "capillary_pressure",
73 "initial capillary pressure value in the mesh to be merged", false, 0.0,
74 "floating point value");
75 cmd.add(pc);
76
77 TCLAP::ValueArg<double> T(
78 "", "temperature", "initial temperature value in the mesh to be merged",
79 false, 290.0, "floating point value");
80 cmd.add(T);
81
82 TCLAP::ValueArg<double> sxx(
83 "", "sigma_xx", "initial stress xx value in the mesh to be merged",
84 false, 0.0, "floating point value");
85 cmd.add(sxx);
86 TCLAP::ValueArg<double> syy(
87 "", "sigma_yy", "initial stress yy value in the mesh to be merged",
88 false, 0.0, "floating point value");
89 cmd.add(syy);
90 TCLAP::ValueArg<double> szz(
91 "", "sigma_zz", "initial stress zz value in the mesh to be merged",
92 false, 0.0, "floating point value");
93 cmd.add(szz);
94
95 TCLAP::ValueArg<int> mat_id("", "material_id",
96 "Material ID of the mesh to be merged", false,
97 0.0, "integer cell value");
98 cmd.add(mat_id);
99
100 cmd.parse(argc, argv);
101
102 BaseLib::MPI::Setup mpi_setup(argc, argv);
103
104 BaseLib::RunTime timer;
105 timer.start();
106
107 std::unordered_map<std::string, double> initial_value_dict;
108 initial_value_dict.insert({"p", p.getValue()});
109 initial_value_dict.insert({"pg", pg.getValue()});
110 initial_value_dict.insert({"pc", pc.getValue()});
111 initial_value_dict.insert({"T", T.getValue()});
112 initial_value_dict.insert({"sxx", sxx.getValue()});
113 initial_value_dict.insert({"syy", syy.getValue()});
114 initial_value_dict.insert({"szz", szz.getValue()});
115 initial_value_dict.insert({"mat_id", mat_id.getValue()});
116
117 auto read_mesh = [](std::string const& mesh_file_name)
118 {
119 std::unique_ptr<MeshLib::Mesh> mesh(MeshLib::IO::readMeshFromFile(
120 mesh_file_name, true /* compute_element_neighbors */));
121
122 if (!mesh)
123 {
124 OGS_FATAL("Could not read the mesh {:s}", mesh_file_name);
125 }
126
127 INFO("Read {:s}: {:d} nodes, {:d} elements.", mesh_file_name,
128 mesh->getNumberOfNodes(), mesh->getNumberOfElements());
129 return mesh;
130 };
131
132 auto bulk_mesh = read_mesh(bulk_mesh_in.getValue());
133 auto const mesh_to_be_merged = read_mesh(mesh_in.getValue());
134
135 auto merged_mesh = MeshToolsLib::mergeMeshToBulkMesh(
136 *bulk_mesh, *mesh_to_be_merged, initial_value_dict);
137
138 MeshLib::IO::VtuInterface writer(merged_mesh.get());
139
140 auto const result = writer.writeToFile(mesh_out.getValue());
141 if (!result)
142 {
143 ERR("Could not write mesh to '{:s}'.", mesh_out.getValue());
144 return EXIT_FAILURE;
145 }
146 INFO("It took {} s", timer.elapsed());
147
148 // The merged nodes are moved to the new mesh, and the duplicated nodes on
149 // the mesh interface have already been deleted in
150 // MeshToolsLib::mergeMeshToBulkMesh. The elements are also moved to the
151 // merged mesh. The merged mesh object handles the release of the memory
152 // allocated for nodes and elements. Therefore, shallowClean is called.
153 mesh_to_be_merged->shallowClean();
154 bulk_mesh->shallowClean();
155
156 return EXIT_SUCCESS;
157}
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:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
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)
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.