OGS
ExtractMaterials.cpp File Reference

Detailed Description

Definition in file ExtractMaterials.cpp.

#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/MPI.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 26 of file ExtractMaterials.cpp.

27{
28 auto const mat_ids =
29 *mesh.getProperties().getPropertyVector<int>("MaterialIDs");
30
31 auto const elem_list =
32 ranges::views::iota(std::size_t{0}, mat_ids.size()) |
33 ranges::views::filter([&](std::size_t i)
34 { return mat_ids[i] != mat_id; }) |
35 ranges::to<std::vector>;
36
37 return MeshToolsLib::removeElements(mesh, elem_list, "matgroup");
38}
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 40 of file ExtractMaterials.cpp.

41{
42 TCLAP::CmdLine cmd(
43 "Takes a mesh with multiple MaterialIDs and writes elements of a given "
44 "ID into a new mesh. If no ID is specified, meshes for each existing "
45 "MaterialID are created.\n\n"
46 "OpenGeoSys-6 software, version " +
48 ".\n"
49 "Copyright (c) 2012-2025, OpenGeoSys Community "
50 "(http://www.opengeosys.org)",
52 TCLAP::ValueArg<std::size_t> arg_mat_id(
53 "m", "material-id",
54 "The MaterialID for which elements should be extracted into a new "
55 "mesh.",
56 false, 0, "Number specifying the MaterialID");
57 cmd.add(arg_mat_id);
58 TCLAP::ValueArg<std::string> output_arg("o", "output",
59 "Name of the output mesh (*.vtu)",
60 true, "", "output file name");
61 cmd.add(output_arg);
62 TCLAP::ValueArg<std::string> input_arg("i", "input",
63 "Name of the input mesh (*.vtu)",
64 true, "", "input file name");
65 cmd.add(input_arg);
66 cmd.parse(argc, argv);
67
68 BaseLib::MPI::Setup mpi_setup(argc, argv);
69
70 std::string const input_name = input_arg.getValue();
71 std::string const output_name = output_arg.getValue();
72 std::string const base_name = BaseLib::dropFileExtension(output_name);
73 std::string const ext = BaseLib::getFileExtension(output_name);
74
75 std::unique_ptr<MeshLib::Mesh> const mesh(
77 if (mesh == nullptr)
78 {
79 ERR("Error reading input mesh. Aborting...");
80 return EXIT_FAILURE;
81 }
82
83 auto mat_ids = MeshLib::materialIDs(*mesh);
84 if (mat_ids == nullptr)
85 {
86 ERR("No material IDs found in mesh. Aborting...");
87 return EXIT_FAILURE;
88 }
89
90 auto const [min, max] = ranges::minmax(*mat_ids);
91 if (min == max)
92 {
93 ERR("Mesh only contains one material, no extraction required.");
94 return EXIT_FAILURE;
95 }
96 int min_id, max_id;
97 if (arg_mat_id.isSet())
98 {
99 min_id = static_cast<int>(arg_mat_id.getValue());
100 if (min_id < min || min_id > max)
101 {
102 ERR("Specified material ID does not exist.");
103 return EXIT_FAILURE;
104 }
105 max_id = min_id;
106 }
107 else
108 {
109 min_id = min;
110 max_id = max;
111 }
112
113 std::ofstream ostream;
114 if (min_id != max_id)
115 {
116 ostream.open(base_name + "_Layers.txt");
117 }
118
119 for (int i = min_id; i <= max_id; ++i)
120 {
121 INFO("Extracting material group {:d}...", i);
122 std::unique_ptr<MeshLib::Mesh> mat_group(extractMatGroup(*mesh, i));
123 if (mat_group == nullptr)
124 {
125 WARN("No elements with material group {:d} found.", i);
126 continue;
127 }
128 MeshLib::IO::VtuInterface vtu(mat_group.get());
129 std::string const file_name(base_name + "_Layer" + std::to_string(i) +
130 ext);
131 vtu.writeToFile(file_name);
132 if (ostream.is_open())
133 {
134 ostream << file_name << "\n";
135 }
136 }
137 if (ostream.is_open())
138 {
139 ostream.close();
140 }
141 return EXIT_SUCCESS;
142}
MeshLib::Mesh * extractMatGroup(MeshLib::Mesh const &mesh, int const mat_id)
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
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:268

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