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