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 <fstream>
#include <memory>
#include "BaseLib/FileTools.h"
#include "BaseLib/MPI.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 28 of file generateMatPropsFromMatID.cpp.

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

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