OGS
ExtractMaterials.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
4#include <tclap/CmdLine.h>
5
6#include <fstream>
7#include <range/v3/algorithm/minmax.hpp>
8#include <range/v3/range/conversion.hpp>
9#include <range/v3/view/filter.hpp>
10#include <range/v3/view/iota.hpp>
11
12#include "BaseLib/FileTools.h"
13#include "BaseLib/Logging.h"
14#include "BaseLib/MPI.h"
16#include "InfoLib/GitInfo.h"
19#include "MeshLib/Mesh.h"
21
22MeshLib::Mesh* extractMatGroup(MeshLib::Mesh const& mesh, int const mat_id)
23{
24 auto const mat_ids =
25 *mesh.getProperties().getPropertyVector<int>("MaterialIDs");
26
27 auto const elem_list =
28 ranges::views::iota(std::size_t{0}, mat_ids.size()) |
29 ranges::views::filter([&](std::size_t i)
30 { return mat_ids[i] != mat_id; }) |
31 ranges::to<std::vector>;
32
33 return MeshToolsLib::removeElements(mesh, elem_list, "matgroup");
34}
35
36int main(int argc, char* argv[])
37{
38 TCLAP::CmdLine cmd(
39 "Takes a mesh with multiple MaterialIDs and writes elements of a given "
40 "ID into a new mesh. If no ID is specified, meshes for each existing "
41 "MaterialID are created.\n\n"
42 "OpenGeoSys-6 software, version " +
44 ".\n"
45 "Copyright (c) 2012-2026, OpenGeoSys Community "
46 "(http://www.opengeosys.org)",
48 TCLAP::ValueArg<std::size_t> arg_mat_id(
49 "m", "material-id",
50 "The MaterialID for which elements should be extracted into a new "
51 "mesh.",
52 false, 0, "MATERIAL_ID");
53 cmd.add(arg_mat_id);
54 TCLAP::ValueArg<std::string> output_arg(
55 "o", "output", "Output (.vtu). Name of the output mesh", true, "",
56 "OUTPUT_FILE");
57 cmd.add(output_arg);
58 TCLAP::ValueArg<std::string> input_arg(
59 "i", "input", "Input (.vtu). Name of the input mesh", true, "",
60 "INPUT_FILE");
61 cmd.add(input_arg);
62 auto log_level_arg = BaseLib::makeLogLevelArg();
63 cmd.add(log_level_arg);
64 cmd.parse(argc, argv);
65
66 BaseLib::MPI::Setup mpi_setup(argc, argv);
67 BaseLib::initOGSLogger(log_level_arg.getValue());
68
69 std::string const input_name = input_arg.getValue();
70 std::string const output_name = output_arg.getValue();
71 std::string const base_name = BaseLib::dropFileExtension(output_name);
72 std::string const ext = BaseLib::getFileExtension(output_name);
73
74 std::unique_ptr<MeshLib::Mesh> const mesh(
76 if (mesh == nullptr)
77 {
78 ERR("Error reading input mesh. Aborting...");
79 return EXIT_FAILURE;
80 }
81
82 auto mat_ids = MeshLib::materialIDs(*mesh);
83 if (mat_ids == nullptr)
84 {
85 ERR("No material IDs found in mesh. Aborting...");
86 return EXIT_FAILURE;
87 }
88
89 auto const [min, max] = ranges::minmax(*mat_ids);
90 if (min == max)
91 {
92 ERR("Mesh only contains one material, no extraction required.");
93 return EXIT_FAILURE;
94 }
95 int min_id, max_id;
96 if (arg_mat_id.isSet())
97 {
98 min_id = static_cast<int>(arg_mat_id.getValue());
99 if (min_id < min || min_id > max)
100 {
101 ERR("Specified material ID does not exist.");
102 return EXIT_FAILURE;
103 }
104 max_id = min_id;
105 }
106 else
107 {
108 min_id = min;
109 max_id = max;
110 }
111
112 std::ofstream ostream;
113 if (min_id != max_id)
114 {
115 ostream.open(base_name + "_Layers.txt");
116 }
117
118 for (int i = min_id; i <= max_id; ++i)
119 {
120 INFO("Extracting material group {:d}...", i);
121 std::unique_ptr<MeshLib::Mesh> mat_group(extractMatGroup(*mesh, i));
122 if (mat_group == nullptr)
123 {
124 WARN("No elements with material group {:d} found.", i);
125 continue;
126 }
127 MeshLib::IO::VtuInterface vtu(mat_group.get());
128 std::string const file_name(base_name + "_Layer" + std::to_string(i) +
129 ext);
130 vtu.writeToFile(file_name);
131 if (ostream.is_open())
132 {
133 ostream << file_name << "\n";
134 }
135 }
136 if (ostream.is_open())
137 {
138 ostream.close();
139 }
140 return EXIT_SUCCESS;
141}
int main(int argc, char *argv[])
MeshLib::Mesh * extractMatGroup(MeshLib::Mesh const &mesh, int const mat_id)
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
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
bool writeToFile(std::filesystem::path const &file_path)
Properties & getProperties()
Definition Mesh.h:125
PropertyVector< T > const * getPropertyVector(std::string_view name) const
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
std::string getFileExtension(const std::string &path)
std::string dropFileExtension(std::string const &filename)
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition Mesh.cpp:258
MeshLib::Mesh * removeElements(const MeshLib::Mesh &mesh, const std::vector< std::size_t > &removed_element_ids, const std::string &new_mesh_name)