20#include <tclap/CmdLine.h>
47 std::vector<MeshLib::Element*>
const& selected_elements,
48 std::string mesh_name)
51 auto nodes = copyNodeVector(mesh.
getNodes());
52 auto elements = copyElementVector(selected_elements, nodes);
58 return std::make_unique<MeshLib::Mesh>(std::move(mesh_name), nodes,
63 MeshLib::Mesh const& mesh, std::vector<std::size_t> selected_element_ids)
65 auto const material_ids = materialIDs(mesh);
66 if (material_ids ==
nullptr)
69 "GMSH2OGS: Expected material ids to be present in the mesh to "
70 "extract boundary meshes.");
73 std::vector<std::unique_ptr<MeshLib::Mesh>> boundary_meshes;
77 while (!selected_element_ids.empty())
81 int const material_id = (*material_ids)[selected_element_ids.back()];
82 auto split = std::partition(
83 begin(selected_element_ids), end(selected_element_ids),
84 [&material_id, &material_ids](
int const id)
85 {
return (*material_ids)[
id] != material_id; });
88 std::vector<MeshLib::Element*> single_material_elements;
89 single_material_elements.reserve(
90 std::distance(split, end(selected_element_ids)));
91 std::transform(split, end(selected_element_ids),
92 std::back_inserter(single_material_elements),
93 [&](
int const id) {
return elements[id]; });
96 selected_element_ids.erase(split, end(selected_element_ids));
100 mesh, single_material_elements, std::to_string(material_id)));
102 return boundary_meshes;
107 std::string
const& file_name,
108 std::vector<std::unique_ptr<MeshLib::Mesh>>& boundary_meshes)
111 auto const& mesh_node_searcher =
114 std::make_unique<MeshGeoToolsLib::SearchLength>(
117 for (
auto& boundary_mesh : boundary_meshes)
119 identifySubdomainMesh(*boundary_mesh, mesh, mesh_node_searcher);
123 '_' + boundary_mesh->getName() +
130int main(
int argc,
char* argv[])
133 "Converting meshes in gmsh file format (ASCII, version 2.2) to a vtk "
134 "unstructured grid file (new OGS file format) or to the old OGS file "
135 "format - see options.\n\n"
136 "OpenGeoSys-6 software, version " +
139 "Copyright (c) 2012-2024, OpenGeoSys Community "
140 "(http://www.opengeosys.org)",
143 TCLAP::ValueArg<std::string> ogs_mesh_arg(
146 "filename for output mesh (if extension is .msh, old OGS-5 fileformat "
147 "is written, if extension is .vtu, a vtk unstructure grid file is "
148 "written (OGS-6 mesh format))",
151 "filename as string");
152 cmd.add(ogs_mesh_arg);
154 TCLAP::ValueArg<std::string> gmsh_mesh_arg(
"i",
"in",
"gmsh input file",
155 true,
"",
"filename as string");
156 cmd.add(gmsh_mesh_arg);
158 TCLAP::SwitchArg valid_arg(
"v",
"validation",
"validate the mesh");
161 TCLAP::SwitchArg create_boundary_meshes_arg(
162 "b",
"boundaries",
"if set, boundary meshes will be generated");
163 cmd.add(create_boundary_meshes_arg);
165 TCLAP::SwitchArg exclude_lines_arg(
166 "e",
"exclude-lines",
167 "if set, lines will not be written to the ogs mesh");
168 cmd.add(exclude_lines_arg);
170 std::string
const gmsh2_opt_message =
171 "if set, the mesh is generated with Gmsh version 2 and it is saved"
172 " (or exported) as \"Version 2 ASCII\" format";
174 TCLAP::SwitchArg gmsh2_arg(
"",
"gmsh2_physical_id", gmsh2_opt_message);
177 cmd.parse(argc, argv);
180 MPI_Init(&argc, &argv);
184 INFO(
"Reading {:s}.", gmsh_mesh_arg.getValue());
192 gmsh2_arg.getValue()));
196 INFO(
"Could not read mesh from {:s}.", gmsh_mesh_arg.getValue());
203 INFO(
"Mem for mesh: {} MiB",
207 INFO(
"Time for reading: {:f} seconds.", run_time.
elapsed());
212 if (exclude_lines_arg.getValue() || create_boundary_meshes_arg.getValue())
216 auto const& selected_element_ids = ex.getSearchedElementIDs();
222 std::vector<std::unique_ptr<MeshLib::Mesh>> boundary_meshes;
223 if (create_boundary_meshes_arg.getValue())
229 if (exclude_lines_arg.getValue())
232 *mesh, selected_element_ids, mesh->
getName() +
"-withoutLines");
235 INFO(
"Removed {:d} lines.",
242 INFO(
"Mesh does not contain any lines.");
246 if (create_boundary_meshes_arg.getValue())
254 INFO(
"Please check your mesh carefully!");
256 "Degenerated or redundant mesh elements can cause OGS to stop or "
258 INFO(
"Use the -e option to delete redundant line elements.");
263 INFO(
"Axis aligned bounding box: {}", aabb);
265 auto const [min, max] = minMaxEdgeLength(mesh->
getElements());
266 INFO(
"Edge length: [{:g}, {:g}]", min, max);
273 if (valid_arg.isSet())
Definition of the AABB class.
Definition of Duplicate functions.
int main(int argc, char *argv[])
static std::unique_ptr< MeshLib::Mesh > createMeshFromElements(MeshLib::Mesh const &mesh, std::vector< MeshLib::Element * > const &selected_elements, std::string mesh_name)
static std::vector< std::unique_ptr< MeshLib::Mesh > > extractBoundaryMeshes(MeshLib::Mesh const &mesh, std::vector< std::size_t > selected_element_ids)
static void identifyAndWriteBoundaryMeshes(MeshLib::Mesh const &mesh, std::string const &file_name, std::vector< std::unique_ptr< MeshLib::Mesh > > &boundary_meshes)
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition of the MemWatch class.
Definition of the Mesh class.
Definition of the RunTime class.
unsigned long getVirtMemUsage()
double elapsed() const
Get the elapsed time in seconds.
void start()
Start the timer.
Class AABB is an axis aligned bounding box around a given set of geometric points of (template) type ...
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
const std::string getName() const
Get name of the mesh.
std::size_t getNumberOfNodes() const
Get the number of nodes.
std::size_t getNumberOfElements() const
Get the number of elements.
std::string getFileExtension(const std::string &path)
std::string dropFileExtension(std::string const &filename)
MeshLib::Mesh * readGMSHMesh(std::string const &fname, bool const is_created_with_gmsh2)
GITINFOLIB_EXPORT const std::string ogs_version
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > variable_output_names)