OGS
ExtractMaterials.cpp File Reference

Detailed Description

Definition in file ExtractMaterials.cpp.

#include <fstream>
#include <tclap/CmdLine.h>
#include <mpi.h>
#include "BaseLib/FileTools.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 std::vector<std::size_t> elem_list;
29 std::vector<int> const mat_ids =
30 *mesh.getProperties().getPropertyVector<int>("MaterialIDs");
31 std::size_t const n_elems = mat_ids.size();
32 for (std::size_t i = 0; i < n_elems; ++i)
33 {
34 if (mat_ids[i] != mat_id)
35 {
36 elem_list.push_back(i);
37 }
38 }
39
40 if (elem_list.empty())
41 {
42 return nullptr;
43 }
44
45 return MeshToolsLib::removeElements(mesh, elem_list, "matgroup");
46}
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 48 of file ExtractMaterials.cpp.

49{
50 TCLAP::CmdLine cmd(
51 "Takes a mesh with multiple MaterialIDs and writes elements of a given "
52 "ID into a new mesh. If no ID is specified, meshes for each existing "
53 "MaterialID are created.\n\n"
54 "OpenGeoSys-6 software, version " +
56 ".\n"
57 "Copyright (c) 2012-2024, OpenGeoSys Community "
58 "(http://www.opengeosys.org)",
60 TCLAP::ValueArg<std::size_t> arg_mat_id(
61 "m", "material-id",
62 "The MaterialID for which elements should be extracted into a new "
63 "mesh.",
64 false, 0, "Number specifying the MaterialID");
65 cmd.add(arg_mat_id);
66 TCLAP::ValueArg<std::string> output_arg("o", "output",
67 "Name of the output mesh (*.vtu)",
68 true, "", "output file name");
69 cmd.add(output_arg);
70 TCLAP::ValueArg<std::string> input_arg("i", "input",
71 "Name of the input mesh (*.vtu)",
72 true, "", "input file name");
73 cmd.add(input_arg);
74 cmd.parse(argc, argv);
75
76#ifdef USE_PETSC
77 MPI_Init(&argc, &argv);
78#endif
79
80 std::string const input_name = input_arg.getValue();
81 std::string const output_name = output_arg.getValue();
82 std::string const base_name = BaseLib::dropFileExtension(output_name);
83 std::string const ext = BaseLib::getFileExtension(output_name);
84
85 std::unique_ptr<MeshLib::Mesh> const mesh(
87 if (mesh == nullptr)
88 {
89 ERR("Error reading input mesh. Aborting...");
90#ifdef USE_PETSC
91 MPI_Finalize();
92#endif
93 return EXIT_FAILURE;
94 }
95
96 auto mat_ids = MeshLib::materialIDs(*mesh);
97 if (mat_ids == nullptr)
98 {
99 ERR("No material IDs found in mesh. Aborting...");
100#ifdef USE_PETSC
101 MPI_Finalize();
102#endif
103 return EXIT_FAILURE;
104 }
105
106 auto id_range = std::minmax_element(mat_ids->cbegin(), mat_ids->cend());
107 if (id_range.first == id_range.second)
108 {
109 ERR("Mesh only contains one material, no extraction required.");
110#ifdef USE_PETSC
111 MPI_Finalize();
112#endif
113 return EXIT_FAILURE;
114 }
115 int min_id, max_id;
116 if (arg_mat_id.isSet())
117 {
118 min_id = static_cast<int>(arg_mat_id.getValue());
119 if (min_id < *id_range.first || min_id > *id_range.second)
120 {
121 ERR("Specified material ID does not exist.");
122#ifdef USE_PETSC
123 MPI_Finalize();
124#endif
125 return EXIT_FAILURE;
126 }
127 max_id = min_id;
128 }
129 else
130 {
131 min_id = *id_range.first;
132 max_id = *id_range.second;
133 }
134
135 std::ofstream ostream;
136 if (min_id != max_id)
137 {
138 ostream.open(base_name + "_Layers.txt");
139 }
140
141 for (int i = min_id; i <= max_id; ++i)
142 {
143 INFO("Extracting material group {:d}...", i);
144 std::unique_ptr<MeshLib::Mesh> mat_group(extractMatGroup(*mesh, i));
145 if (mat_group == nullptr)
146 {
147 WARN("No elements with material group {:d} found.", i);
148 continue;
149 }
150 MeshLib::IO::VtuInterface vtu(mat_group.get());
151 std::string const file_name(base_name + "_Layer" + std::to_string(i) +
152 ext);
153 vtu.writeToFile(file_name);
154 if (ostream.is_open())
155 {
156 ostream << file_name << "\n";
157 }
158 }
159 if (ostream.is_open())
160 {
161 ostream.close();
162 }
163#ifdef USE_PETSC
164 MPI_Finalize();
165#endif
166 return EXIT_SUCCESS;
167}
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().