OGS
generateMatPropsFromMatID.cpp File Reference

Detailed Description

Implementation of the generateMatPropsFromMatID tool.

Author
Karsten Rink
Date
2011-12-19

Definition in file generateMatPropsFromMatID.cpp.

#include <tclap/CmdLine.h>
#include <memory>
#include "BaseLib/FileTools.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/Elements/Element.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/IO/writeMeshToFile.h"
#include "MeshLib/Mesh.h"
Include dependency graph for generateMatPropsFromMatID.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 26 of file generateMatPropsFromMatID.cpp.

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 }
#define OGS_FATAL(...)
Definition: Error.h:26
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:32
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
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

References ERR(), BaseLib::extractBaseNameWithoutExtension(), INFO(), MeshLib::materialIDs(), MaterialPropertyLib::name, OGS_FATAL, GitInfoLib::GitInfo::ogs_version, MeshLib::IO::readMeshFromFile(), and MeshLib::IO::writeMeshToFile().