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