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