OGS
identifySubdomains.cpp
Go to the documentation of this file.
1
11#include <tclap/CmdLine.h>
12
13#include "BaseLib/Logging.h"
14#include "BaseLib/MPI.h"
15#include "BaseLib/RunTime.h"
17#include "InfoLib/GitInfo.h"
23#include "MeshLib/Mesh.h"
24
25std::vector<std::unique_ptr<MeshLib::Mesh>> readMeshes(
26 std::vector<std::string> const& filenames)
27{
28 std::vector<std::unique_ptr<MeshLib::Mesh>> meshes;
29 meshes.reserve(filenames.size());
30
31 for (auto const& filename : filenames)
32 {
33 auto mesh = MeshLib::IO::readMeshFromFile(filename);
34 if (mesh == nullptr)
35 {
36 OGS_FATAL("Could not read mesh from '{:s}' file.", filename);
37 }
38 meshes.emplace_back(mesh);
39 }
40 if (meshes.empty())
41 {
42 OGS_FATAL("No subdomain meshes were read.");
43 }
44 return meshes;
45}
46
47int main(int argc, char* argv[])
48{
49 BaseLib::RunTime run_time;
50 run_time.start();
51
52 TCLAP::CmdLine cmd(
53 "Checks if the subdomain meshes are part of the bulk mesh and writes "
54 "the 'bulk_node_ids' and the 'bulk_element_ids' in each of them. The "
55 "documentation is available at "
56 "https://www.opengeosys.org/docs/tools/meshing-submeshes/"
57 "identifysubdomains/.\n\n"
58 "OpenGeoSys-6 software, version " +
60 ".\n"
61 "Copyright (c) 2012-2025, OpenGeoSys Community "
62 "(http://www.opengeosys.org)",
64
65 TCLAP::SwitchArg force_overwrite_arg(
66 "f", "force", "Overwriting existing subdomain meshes.");
67 cmd.add(force_overwrite_arg);
68
69 TCLAP::ValueArg<std::string> output_prefix_arg(
70 "o",
71 "output_prefix",
72 "Output. Prefix the subdomain meshes' filenames with the output "
73 "prefix/path.",
74 false,
75 "",
76 "BASE_FILENAME_OUTPUT");
77 cmd.add(output_prefix_arg);
78
79 TCLAP::ValueArg<double> search_length_arg(
80 "s",
81 "searchlength",
82 "search length determining radius for the node search algorithm. "
83 "Non-negative floating point number (min = 0) ",
84 false,
85 1e-16,
86 "SEARCH_LENGTH");
87 cmd.add(search_length_arg);
88
89 TCLAP::ValueArg<std::string> bulk_mesh_arg(
90 "m", "mesh", "Input (.vtu). The file name of the bulk mesh", true, "",
91 "INPUT_FILE");
92 cmd.add(bulk_mesh_arg);
93
94 // All the remaining arguments are used as file names for boundary/subdomain
95 // meshes.
96 TCLAP::UnlabeledMultiArg<std::string> subdomain_meshes_filenames_arg(
97 "subdomain_meshes_filenames", "mesh file names.", true,
98 "SUBDOMAIN_NAME");
99 cmd.add(subdomain_meshes_filenames_arg);
100 auto log_level_arg = BaseLib::makeLogLevelArg();
101 cmd.add(log_level_arg);
102 cmd.parse(argc, argv);
103
104 BaseLib::MPI::Setup mpi_setup(argc, argv);
105 BaseLib::initOGSLogger(log_level_arg.getValue());
106
107 //
108 // The bulk mesh.
109 //
110 BaseLib::RunTime mesh_reading_time;
111 mesh_reading_time.start();
112 std::unique_ptr<MeshLib::Mesh> bulk_mesh{
113 MeshLib::IO::readMeshFromFile(bulk_mesh_arg.getValue())};
114 if (bulk_mesh == nullptr)
115 {
116 OGS_FATAL("Could not read bulk mesh from '{:s}'",
117 bulk_mesh_arg.getValue());
118 }
119
120 //
121 // Read the subdomain meshes.
122 //
123 auto const subdomain_meshes =
124 readMeshes(subdomain_meshes_filenames_arg.getValue());
125 INFO("Mesh reading time: {:g} s", mesh_reading_time.elapsed());
126
127 //
128 // Bulk mesh node searcher.
129 //
130 BaseLib::RunTime mesh_node_searcher_construction_time;
131 mesh_node_searcher_construction_time.start();
132 auto const& mesh_node_searcher =
134 *bulk_mesh,
135 std::make_unique<MeshGeoToolsLib::SearchLength>(
136 search_length_arg.getValue()));
137 INFO("MeshNodeSearcher construction time: {:g} s",
138 mesh_node_searcher_construction_time.elapsed());
139
140 //
141 // Identify the subdomains in the bulk mesh.
142 //
143 BaseLib::RunTime identify_subdomain_time;
144 identify_subdomain_time.start();
145 for (auto& mesh_ptr : subdomain_meshes)
146 {
147 // If force overwrite is set or the output is to different mesh than
148 // the input mesh.
149 bool const overwrite_property_vectors =
150 force_overwrite_arg.getValue() ||
151 !output_prefix_arg.getValue().empty();
152 identifySubdomainMesh(*mesh_ptr, *bulk_mesh, mesh_node_searcher,
153 overwrite_property_vectors);
154 }
155 INFO("identifySubdomains time: {:g} s", identify_subdomain_time.elapsed());
156
157 //
158 // Output after the successful subdomain mesh identification.
159 //
160 BaseLib::RunTime writing_time;
161 writing_time.start();
162 for (auto const& mesh_ptr : subdomain_meshes)
163 {
165 *mesh_ptr,
166 output_prefix_arg.getValue() + mesh_ptr->getName() + ".vtu");
167 }
168 INFO("writing time: {:g} s", writing_time.elapsed());
169
170 INFO("Entire run time: {:g} s", run_time.elapsed());
171 return EXIT_SUCCESS;
172}
#define OGS_FATAL(...)
Definition Error.h:26
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:36
Definition of the Mesh class.
Definition of the RunTime class.
Base class for different search length strategies.
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
static OGS_NO_DANGLING MeshNodeSearcher const & getMeshNodeSearcher(MeshLib::Mesh const &mesh, std::unique_ptr< MeshGeoToolsLib::SearchLength > &&search_length_algorithm)
int main(int argc, char *argv[])
std::vector< std::unique_ptr< MeshLib::Mesh > > readMeshes(std::vector< std::string > const &filenames)
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)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > variable_output_names)
Definition of readMeshFromFile function.