OGS
generateMatPropsFromMatID.cpp
Go to the documentation of this file.
1 
15 #include <tclap/CmdLine.h>
16 
17 #include <memory>
18 
19 #include "BaseLib/FileTools.h"
20 #include "InfoLib/GitInfo.h"
24 #include "MeshLib/Mesh.h"
25 
26 int main(int argc, char* argv[])
27 {
28  TCLAP::CmdLine cmd(
29  "Creates a new file for material properties and sets the material ids "
30  "in the msh-file to 0\n\n"
31  "OpenGeoSys-6 software, version " +
33  ".\n"
34  "Copyright (c) 2012-2021, OpenGeoSys Community "
35  "(http://www.opengeosys.org)",
37 
38  TCLAP::ValueArg<std::string> mesh_arg("m",
39  "mesh",
40  "the mesh to open from a file",
41  false,
42  "",
43  "filename for mesh input");
44  cmd.add(mesh_arg);
45 
46  cmd.parse(argc, argv);
47 
48  // read mesh
49  std::unique_ptr<MeshLib::Mesh> mesh(
50  MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
51 
52  if (!mesh)
53  {
54  INFO("Could not read mesh from file '{:s}'.", mesh_arg.getValue());
55  return EXIT_FAILURE;
56  }
57 
58  auto const materialIds = materialIDs(*mesh);
59  if (!materialIds)
60  {
61  OGS_FATAL("Mesh contains no int-property vector named 'MaterialIDs'.");
62  }
63 
64  std::size_t const n_properties(materialIds->size());
65  assert(n_properties != mesh->getNumberOfElements());
66 
67  std::string const name =
68  BaseLib::extractBaseNameWithoutExtension(mesh_arg.getValue());
69  // create file
70  std::string const new_matname(name + "_prop");
71  std::ofstream out_prop(new_matname.c_str(), std::ios::out);
72  if (out_prop.is_open())
73  {
74  for (std::size_t i = 0; i < n_properties; ++i)
75  {
76  out_prop << i << "\t" << (*materialIds)[i] << "\n";
77  }
78  out_prop.close();
79  }
80  else
81  {
82  ERR("Could not create property '{:s}' file.", new_matname);
83  return EXIT_FAILURE;
84  }
85 
86  mesh->getProperties().removePropertyVector("MaterialIDs");
87 
88  std::string const new_mshname(name + "_new.vtu");
89  INFO("Writing mesh to file '{:s}'.", new_mshname);
90  MeshLib::IO::writeMeshToFile(*mesh, new_mshname);
91 
92  INFO("New files '{:s}' and '{:s}' written.", new_mshname, new_matname);
93 
94  return EXIT_SUCCESS;
95 }
Definition of the Element class.
#define OGS_FATAL(...)
Definition: Error.h:26
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
Definition of the Mesh class.
int main(int argc, char *argv[])
std::string extractBaseNameWithoutExtension(std::string const &pathname)
Definition: FileTools.cpp:180
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, [[maybe_unused]] std::set< std::string > variable_output_names)
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition: Mesh.cpp:258
Definition of readMeshFromFile function.