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