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