OGS
ExtractMaterials.cpp File Reference
#include <tclap/CmdLine.h>
#include <fstream>
#include <range/v3/algorithm/minmax.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/filter.hpp>
#include <range/v3/view/iota.hpp>
#include "BaseLib/FileTools.h"
#include "BaseLib/Logging.h"
#include "BaseLib/MPI.h"
#include "BaseLib/TCLAPArguments.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/IO/VtkIO/VtuInterface.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/Mesh.h"
#include "MeshToolsLib/MeshEditing/RemoveMeshComponents.h"
Include dependency graph for ExtractMaterials.cpp:

Go to the source code of this file.

Functions

MeshLib::MeshextractMatGroup (MeshLib::Mesh const &mesh, int const mat_id)
int main (int argc, char *argv[])

Function Documentation

◆ extractMatGroup()

MeshLib::Mesh * extractMatGroup ( MeshLib::Mesh const & mesh,
int const mat_id )

Definition at line 22 of file ExtractMaterials.cpp.

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}
MeshLib::Mesh * removeElements(const MeshLib::Mesh &mesh, const std::vector< std::size_t > &removed_element_ids, const std::string &new_mesh_name)

References MeshLib::Mesh::getProperties(), MeshLib::Properties::getPropertyVector(), and MeshToolsLib::removeElements().

Referenced by main().

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 36 of file ExtractMaterials.cpp.

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}
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....
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

References BaseLib::dropFileExtension(), ERR(), extractMatGroup(), BaseLib::getFileExtension(), INFO(), BaseLib::initOGSLogger(), BaseLib::makeLogLevelArg(), MeshLib::materialIDs(), GitInfoLib::GitInfo::ogs_version, MeshLib::IO::readMeshFromFile(), WARN(), and MeshLib::IO::VtuInterface::writeToFile().