OGS
AddLayer.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include <tclap/CmdLine.h>
5
6#include <memory>
7
8#include "BaseLib/FileTools.h"
9#include "BaseLib/Logging.h"
10#include "BaseLib/MPI.h"
12#include "InfoLib/GitInfo.h"
15#include "MeshLib/Mesh.h"
17
18int main(int argc, char* argv[])
19{
20 TCLAP::CmdLine cmd(
21 "Adds a layer to an existing mesh."
22 "The documentation is available at "
23 "https://www.opengeosys.org/docs/tools/meshing/addlayer."
24 "\n"
25 "OpenGeoSys-6 software, version " +
27 ".\n"
28 "Copyright (c) 2012-2026, OpenGeoSys Community "
29 "(https://www.opengeosys.org)",
31 TCLAP::ValueArg<std::string> mesh_arg("i", "input-mesh-file",
32 "Input (.vtu). The name of the file "
33 "containing the mesh",
34 true, "", "INPUT_FILE");
35 cmd.add(mesh_arg);
36
37 TCLAP::ValueArg<std::string> mesh_out_arg(
38 "o", "output-mesh-file",
39 "Output (.vtu). The name of the file the mesh should be written to",
40 true, "", "OUTPUT_FILE");
41 cmd.add(mesh_out_arg);
42
43 TCLAP::ValueArg<double> layer_thickness_arg(
44 "t", "layer-tickness",
45 "the thickness of the new layer, "
46 "(min = 0)",
47 false, 10, "LAYER_THICKNESS");
48 cmd.add(layer_thickness_arg);
49
50 TCLAP::SwitchArg layer_position_arg(
51 "", "add-layer-on-bottom",
52 "Per default the layer is add on the top, if this argument is set the "
53 "layer is add on the bottom.",
54 true);
55 cmd.add(layer_position_arg);
56
57 TCLAP::SwitchArg copy_material_ids_arg(
58 "", "copy-material-ids",
59 "Copy the existing material distribution of the layer which is to be "
60 "extended. If the switch isn't given a new material id will be "
61 "created.",
62 false);
63 cmd.add(copy_material_ids_arg);
64
65 TCLAP::ValueArg<int> set_material_arg(
66 "", "set-material-id", "the material id of the new layer, (min = 0)",
67 false, 0, "MATERIAL_ID");
68 cmd.add(set_material_arg);
69 auto log_level_arg = BaseLib::makeLogLevelArg();
70 cmd.add(log_level_arg);
71 cmd.parse(argc, argv);
72
73 BaseLib::MPI::Setup mpi_setup(argc, argv);
74 BaseLib::initOGSLogger(log_level_arg.getValue());
75
76 if (set_material_arg.isSet() && copy_material_ids_arg.isSet())
77 {
78 ERR("It is not possible to set both options '--copy-material-ids' and "
79 "'--set-material-id'.");
80 return EXIT_FAILURE;
81 }
82
83 INFO("Reading mesh '{:s}' ... ", mesh_arg.getValue());
84 auto subsfc_mesh = std::unique_ptr<MeshLib::Mesh>(
85 MeshLib::IO::readMeshFromFile(mesh_arg.getValue(), true));
86 if (!subsfc_mesh)
87 {
88 ERR("Error reading mesh '{:s}'.", mesh_arg.getValue());
89 return EXIT_FAILURE;
90 }
91 INFO("done.");
92
93 std::optional<int> layer_id;
94 if (set_material_arg.isSet())
95 {
96 layer_id = set_material_arg.getValue();
97 }
98
99 std::unique_ptr<MeshLib::Mesh> result(MeshToolsLib::addLayerToMesh(
100 *subsfc_mesh, layer_thickness_arg.getValue(), mesh_out_arg.getValue(),
101 layer_position_arg.getValue(), copy_material_ids_arg.getValue(),
102 layer_id));
103 if (!result)
104 {
105 ERR("Failure while adding layer.");
106 return EXIT_FAILURE;
107 }
108
109 INFO("Writing mesh '{:s}' ... ", mesh_out_arg.getValue());
110 MeshLib::IO::writeMeshToFile(*result, mesh_out_arg.getValue());
111 INFO("done.");
112
113 return EXIT_SUCCESS;
114}
int main(int argc, char *argv[])
Definition AddLayer.cpp:18
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
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)
MeshLib::Mesh * addLayerToMesh(MeshLib::Mesh const &mesh, double const thickness, std::string const &name, bool const on_top, bool const copy_material_ids, std::optional< int > const layer_id)