31{
32 TCLAP::CmdLine cmd(
33 "Partition a mesh for parallel computing."
34 "The tasks of this tool are in twofold:\n"
35 "1. Convert mesh file to the input file of the partitioning tool,\n"
36 "2. Partition a mesh using the partitioning tool,\n"
37 "\tcreate the mesh data of each partition,\n"
38 "\trenumber the node indices of each partition,\n"
39 "\tand output the results for parallel computing.\n"
40 "Note: If this tool is installed as a system command,\n"
41 "\tthe command must be run with its full path.\n\n"
42 "OpenGeoSys-6 software, version " +
44 ".\n"
45 "Copyright (c) 2012-2025, OpenGeoSys Community "
46 "(http://www.opengeosys.org)",
48 TCLAP::ValueArg<std::string> mesh_input(
49 "i", "mesh-input-file",
50 "Input (.vtu). The name of the file containing the input mesh", true,
51 "", "INPUT_FILE");
52 cmd.add(mesh_input);
53
54 TCLAP::ValueArg<std::string> metis_mesh_input(
55 "x", "metis-mesh-input-file",
56 "Input (.mesh). Base name (without .mesh extension) of the file "
57 "containing the metis input mesh",
58 false, "", "BASE_FILENAME_INPUT");
59 cmd.add(metis_mesh_input);
60
61 TCLAP::ValueArg<std::string> output_directory_arg(
62 "o", "output", "Output. Directory name for the output files", false, "",
63 "OUTPUT_PATH");
64 cmd.add(output_directory_arg);
65
66 TCLAP::ValueArg<int> nparts("n", "np",
67 "the number of partitions, "
68 "(min = 0)",
69 false, 2, "N_PARTS");
70 cmd.add(nparts);
71
72 TCLAP::SwitchArg ogs2metis_flag(
73 "s", "ogs2metis",
74 "Indicator to convert the ogs mesh file to METIS input file", cmd,
75 false);
76
77 TCLAP::SwitchArg exe_metis_flag(
78 "m", "exe_metis", "Call mpmetis inside the programme via system().",
79 false);
80 cmd.add(exe_metis_flag);
81
83 cmd.add(log_level_arg);
84
85
86
87 TCLAP::UnlabeledMultiArg<std::string> other_meshes_filenames_arg(
88 "other_meshes_filenames", "mesh file names.", false, "file");
89 cmd.add(other_meshes_filenames_arg);
90
91 cmd.parse(argc, argv);
92
95
96 const auto output_directory = output_directory_arg.getValue();
98
103
104 const std::string input_file_name_wo_extension =
106 std::unique_ptr<MeshLib::Mesh> mesh_ptr(
108 INFO(
"Mesh '{:s}' read: {:d} nodes, {:d} elements.",
109 mesh_ptr->getName(),
110 mesh_ptr->getNumberOfNodes(),
111 mesh_ptr->getNumberOfElements());
112
114 output_directory,
116
117 if (ogs2metis_flag.getValue())
118 {
119 INFO(
"Write the mesh into METIS input file.");
121 output_file_name_wo_extension + ".mesh");
122 INFO(
"Total runtime: {:g} s.", run_timer.
elapsed());
123 INFO(
"Total CPU time: {:g} s.", CPU_timer.
elapsed());
124
125 return EXIT_SUCCESS;
126 }
127
129 nparts.getValue(), std::move(mesh_ptr));
130
131 const int num_partitions = nparts.getValue();
132
133 if (num_partitions < 1)
134 {
135 OGS_FATAL(
"Number of partitions must be positive.");
136 }
137
138 if (num_partitions == 1)
139 {
141 "Partitioning the mesh into one domain is unnecessary because OGS "
142 "reads vtu mesh data directly when called with 'mpirun bin/ogs "
143 "-np=1'.");
144 }
145
146 auto metis_mesh = output_file_name_wo_extension;
147 if (metis_mesh_input.getValue() != "")
148 {
149 metis_mesh = metis_mesh_input.getValue();
150 }
151
152
153 if (exe_metis_flag.getValue())
154 {
155 INFO(
"METIS is running ...");
156 const std::string exe_name = argv[0];
158 INFO(
"Path to mpmetis is: \n\t{:s}", exe_path);
159
160 const std::string mpmetis_com =
162 metis_mesh + ".mesh" + "\" " + std::to_string(nparts.getValue());
163
164 INFO(
"Running: {:s}", mpmetis_com);
165 const int status = system(mpmetis_com.c_str());
166 if (status != 0)
167 {
168 INFO(
"Failed in system calling.");
169 INFO(
"Return value of system call {:d} ", status);
170 return EXIT_FAILURE;
171 }
172 }
173 mesh_partitioner.resetPartitionIdsForNodes(
175 mesh_partitioner.mesh().getNumberOfNodes()));
176
177
178 if (exe_metis_flag.getValue())
179 {
181 }
182
183 INFO(
"Partitioning the mesh in the node wise way ...");
184 mesh_partitioner.partitionByMETIS();
185
186 INFO(
"Partitioning other meshes according to the main mesh partitions.");
187 for (
auto const& filename : other_meshes_filenames_arg.
getValue())
188 {
189 std::unique_ptr<MeshLib::Mesh> mesh(
191 INFO(
"Mesh '{:s}' from file '{:s}' read: {:d} nodes, {:d} elements.",
192 mesh->getName(), filename, mesh->getNumberOfNodes(),
193 mesh->getNumberOfElements());
194
195 std::string const other_mesh_output_file_name_wo_extension =
197 output_directory,
199 auto partitions = mesh_partitioner.partitionOtherMesh(*mesh);
200
202 mesh_partitioner.renumberBulkIdsProperty(partitions,
203 partitioned_properties);
204
205 mesh_partitioner.writeOtherMesh(
206 other_mesh_output_file_name_wo_extension, partitions,
207 partitioned_properties);
208 }
209
211 io_run_timer.
start();
212 mesh_partitioner.write(output_file_name_wo_extension);
213 INFO(
"Writing the partitions data into binary files took {:g} s",
215
216 INFO(
"Total runtime: {:g} s.", run_timer.
elapsed());
217 INFO(
"Total CPU time: {:g} s.", CPU_timer.
elapsed());
218
219 return EXIT_SUCCESS;
220}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
void start()
Start the timer.
double elapsed() const
Get the elapsed time after started.
double elapsed() const
Get the elapsed time in seconds.
void start()
Start the timer.
void writeMETIS(std::vector< MeshLib::Element * > const &elements, const std::string &file_name)
MeshLib::Properties partitionProperties(std::unique_ptr< MeshLib::Mesh > const &mesh, std::vector< Partition > &partitions)
Partition existing properties and add vtkGhostType cell data array property.
void removeMetisPartitioningFiles(std::string const &file_name_base, long const number_of_partitions)
std::vector< std::size_t > readMetisData(const std::string &file_name_base, long const number_of_partitions, std::size_t const number_of_nodes)
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
std::string extractPath(std::string const &pathname)
std::string extractBaseNameWithoutExtension(std::string const &pathname)
std::string dropFileExtension(std::string const &filename)
std::string joinPaths(std::string const &pathA, std::string const &pathB)
bool createOutputDirectory(std::string const &dir)
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)