OGS
GMSH2OGS.cpp File Reference

Detailed Description

Implementation of the GMSH2OGS converter.

Author
Thomas Fischer
Date
2011-12-13

Definition in file GMSH2OGS.cpp.

Include dependency graph for GMSH2OGS.cpp:

Go to the source code of this file.

Functions

static std::unique_ptr< MeshLib::MeshcreateMeshFromElements (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)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ createMeshFromElements()

static std::unique_ptr< MeshLib::Mesh > createMeshFromElements ( MeshLib::Mesh const & mesh,
std::vector< MeshLib::Element * > const & selected_elements,
std::string mesh_name )
static

Definition at line 45 of file GMSH2OGS.cpp.

49{
50 // Create boundary mesh.
51 auto nodes = copyNodeVector(mesh.getNodes());
52 auto elements = copyElementVector(selected_elements, nodes);
53
54 // Cleanup unused nodes
56 MeshToolsLib::markUnusedNodes(elements, nodes), nodes);
57
58 return std::make_unique<MeshLib::Mesh>(std::move(mesh_name), nodes,
59 elements);
60}
std::vector< Node * > copyNodeVector(const std::vector< Node * > &nodes)
Creates a deep copy of a Node vector.
std::vector< Element * > copyElementVector(std::vector< Element * > const &elements, std::vector< Node * > const &new_nodes, std::vector< std::size_t > const *const node_id_map)
std::vector< bool > markUnusedNodes(std::vector< MeshLib::Element * > const &elements, std::vector< MeshLib::Node * > const &nodes)
Marks nodes not used by any of the elements.
void removeMarkedNodes(std::vector< bool > const &nodes_to_delete, std::vector< MeshLib::Node * > &nodes)
Deallocates and removes nodes marked true.

References MeshLib::Mesh::getNodes(), MeshToolsLib::markUnusedNodes(), and MeshToolsLib::removeMarkedNodes().

Referenced by extractBoundaryMeshes().

◆ extractBoundaryMeshes()

static std::vector< std::unique_ptr< MeshLib::Mesh > > extractBoundaryMeshes ( MeshLib::Mesh const & mesh,
std::vector< std::size_t > selected_element_ids )
static

Definition at line 62 of file GMSH2OGS.cpp.

64{
65 auto const material_ids = materialIDs(mesh);
66 if (material_ids == nullptr)
67 {
69 "GMSH2OGS: Expected material ids to be present in the mesh to "
70 "extract boundary meshes.");
71 }
72
73 std::vector<std::unique_ptr<MeshLib::Mesh>> boundary_meshes;
74
75 auto const& elements = mesh.getElements();
76
77 while (!selected_element_ids.empty())
78 {
79 // Partition in two blocks, with elements for the material id at
80 // the end, s.t. one can erase them easily.
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; });
86
87 // Add elements with same material id to the mesh.
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]; });
94
95 // Remove already extracted elements.
96 selected_element_ids.erase(split, end(selected_element_ids));
97
98 // Create boundary mesh and identify the nodes/elements.
99 boundary_meshes.push_back(createMeshFromElements(
100 mesh, single_material_elements, std::to_string(material_id)));
101 }
102 return boundary_meshes;
103}
#define OGS_FATAL(...)
Definition Error.h:26
static std::unique_ptr< MeshLib::Mesh > createMeshFromElements(MeshLib::Mesh const &mesh, std::vector< MeshLib::Element * > const &selected_elements, std::string mesh_name)
Definition GMSH2OGS.cpp:45
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition Mesh.cpp:268

References createMeshFromElements(), MeshLib::Mesh::getElements(), and OGS_FATAL.

Referenced by main().

◆ identifyAndWriteBoundaryMeshes()

static void identifyAndWriteBoundaryMeshes ( MeshLib::Mesh const & mesh,
std::string const & file_name,
std::vector< std::unique_ptr< MeshLib::Mesh > > & boundary_meshes )
static

Definition at line 105 of file GMSH2OGS.cpp.

109{
110 // Bulk mesh node searcher usef for boundary mesh identification.
111 auto const& mesh_node_searcher =
113 mesh,
114 std::make_unique<MeshGeoToolsLib::SearchLength>(
115 0)); // Exact match of nodes.
116
117 for (auto& boundary_mesh : boundary_meshes)
118 {
119 identifySubdomainMesh(*boundary_mesh, mesh, mesh_node_searcher);
120
121 // Save the boundary mesh.
122 auto boundary_mesh_file_name = BaseLib::dropFileExtension(file_name) +
123 '_' + boundary_mesh->getName() +
124 BaseLib::getFileExtension(file_name);
125
126 MeshLib::IO::writeMeshToFile(*boundary_mesh, boundary_mesh_file_name);
127 }
128}
static MeshNodeSearcher const & getMeshNodeSearcher(MeshLib::Mesh const &mesh, std::unique_ptr< MeshGeoToolsLib::SearchLength > &&search_length_algorithm)
std::string getFileExtension(const std::string &path)
std::string dropFileExtension(std::string const &filename)
void identifySubdomainMesh(MeshLib::Mesh &subdomain_mesh, MeshLib::Mesh const &bulk_mesh, MeshNodeSearcher const &mesh_node_searcher, bool const force_overwrite=false)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > variable_output_names)

References BaseLib::dropFileExtension(), BaseLib::getFileExtension(), MeshGeoToolsLib::MeshNodeSearcher::getMeshNodeSearcher(), and MeshLib::IO::writeMeshToFile().

Referenced by main().

◆ main()

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

Definition at line 130 of file GMSH2OGS.cpp.

131{
132 TCLAP::CmdLine cmd(
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 " +
138 ".\n"
139 "Copyright (c) 2012-2024, OpenGeoSys Community "
140 "(http://www.opengeosys.org)",
142
143 TCLAP::ValueArg<std::string> ogs_mesh_arg(
144 "o",
145 "out",
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))",
149 true,
150 "",
151 "filename as string");
152 cmd.add(ogs_mesh_arg);
153
154 TCLAP::ValueArg<std::string> gmsh_mesh_arg("i", "in", "gmsh input file",
155 true, "", "filename as string");
156 cmd.add(gmsh_mesh_arg);
157
158 TCLAP::SwitchArg valid_arg("v", "validation", "validate the mesh");
159 cmd.add(valid_arg);
160
161 TCLAP::SwitchArg create_boundary_meshes_arg(
162 "b", "boundaries", "if set, boundary meshes will be generated");
163 cmd.add(create_boundary_meshes_arg);
164
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);
169
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";
173
174 TCLAP::SwitchArg gmsh2_arg("", "gmsh2_physical_id", gmsh2_opt_message);
175 cmd.add(gmsh2_arg);
176
177 cmd.parse(argc, argv);
178
179#ifdef USE_PETSC
180 MPI_Init(&argc, &argv);
181#endif
182
183 // *** read mesh
184 INFO("Reading {:s}.", gmsh_mesh_arg.getValue());
185#ifndef WIN32
186 BaseLib::MemWatch mem_watch;
187 unsigned long mem_without_mesh(mem_watch.getVirtMemUsage());
188#endif
189 BaseLib::RunTime run_time;
190 run_time.start();
191 MeshLib::Mesh* mesh(FileIO::GMSH::readGMSHMesh(gmsh_mesh_arg.getValue(),
192 gmsh2_arg.getValue()));
193
194 if (mesh == nullptr)
195 {
196 INFO("Could not read mesh from {:s}.", gmsh_mesh_arg.getValue());
197#ifdef USE_PETSC
198 MPI_Finalize();
199#endif
200 return -1;
201 }
202#ifndef WIN32
203 INFO("Mem for mesh: {} MiB",
204 (mem_watch.getVirtMemUsage() - mem_without_mesh) / (1024 * 1024));
205#endif
206
207 INFO("Time for reading: {:f} seconds.", run_time.elapsed());
208 INFO("Read {:d} nodes and {:d} elements.", mesh->getNumberOfNodes(),
209 mesh->getNumberOfElements());
210
211 // Optionally remove line elements or create boundary meshes.
212 if (exclude_lines_arg.getValue() || create_boundary_meshes_arg.getValue())
213 {
214 auto ex = MeshLib::ElementSearch(*mesh);
215 ex.searchByElementType(MeshLib::MeshElemType::LINE);
216 auto const& selected_element_ids = ex.getSearchedElementIDs();
217
218 // First we extract the boundary meshes, then optionally remove the line
219 // elements, and only then run the node/element identification and write
220 // the meshes.
221
222 std::vector<std::unique_ptr<MeshLib::Mesh>> boundary_meshes;
223 if (create_boundary_meshes_arg.getValue())
224 {
225 boundary_meshes =
226 extractBoundaryMeshes(*mesh, selected_element_ids);
227 }
228
229 if (exclude_lines_arg.getValue())
230 {
232 *mesh, selected_element_ids, mesh->getName() + "-withoutLines");
233 if (m != nullptr)
234 {
235 INFO("Removed {:d} lines.",
236 mesh->getNumberOfElements() - m->getNumberOfElements());
237 std::swap(m, mesh);
238 delete m;
239 }
240 else
241 {
242 INFO("Mesh does not contain any lines.");
243 }
244 }
245
246 if (create_boundary_meshes_arg.getValue())
247 {
248 identifyAndWriteBoundaryMeshes(*mesh, ogs_mesh_arg.getValue(),
249 boundary_meshes);
250 }
251 }
252 // *** print meshinformation
253
254 INFO("Please check your mesh carefully!");
255 INFO(
256 "Degenerated or redundant mesh elements can cause OGS to stop or "
257 "misbehave.");
258 INFO("Use the -e option to delete redundant line elements.");
259
260 // Geometric information
261 const GeoLib::AABB aabb =
263 INFO("Axis aligned bounding box: {}", aabb);
264
265 auto const [min, max] = minMaxEdgeLength(mesh->getElements());
266 INFO("Edge length: [{:g}, {:g}]", min, max);
267
268 // Element information
270
272
273 if (valid_arg.isSet())
274 {
276 }
277
278 // *** write mesh in new format
279 MeshLib::IO::writeMeshToFile(*mesh, ogs_mesh_arg.getValue());
280
281 delete mesh;
282#ifdef USE_PETSC
283 MPI_Finalize();
284#endif
285 return EXIT_SUCCESS;
286}
static std::vector< std::unique_ptr< MeshLib::Mesh > > extractBoundaryMeshes(MeshLib::Mesh const &mesh, std::vector< std::size_t > selected_element_ids)
Definition GMSH2OGS.cpp:62
static void identifyAndWriteBoundaryMeshes(MeshLib::Mesh const &mesh, std::string const &file_name, std::vector< std::unique_ptr< MeshLib::Mesh > > &boundary_meshes)
Definition GMSH2OGS.cpp:105
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
unsigned long getVirtMemUsage()
Definition MemWatch.cpp:59
Count the running time.
Definition RunTime.h:29
double elapsed() const
Get the elapsed time in seconds.
Definition RunTime.h:42
void start()
Start the timer.
Definition RunTime.h:32
Class AABB is an axis aligned bounding box around a given set of geometric points of (template) type ...
Definition AABB.h:56
Element search class.
static GeoLib::AABB getBoundingBox(const MeshLib::Mesh &mesh)
Returns the bounding box of the mesh.
static void writeMeshValidationResults(MeshLib::Mesh &mesh)
static void writeAllNumbersOfElementTypes(const MeshLib::Mesh &mesh)
writes all numbers of element types
static void writePropertyVectorInformation(const MeshLib::Mesh &mesh)
writes out property vector information
MeshLib::Mesh * readGMSHMesh(std::string const &fname, bool const is_created_with_gmsh2)
GITINFOLIB_EXPORT const std::string ogs_version
std::pair< double, double > minMaxEdgeLength(std::vector< Element * > const &elements)
Returns the minimum and maximum edge length for given elements.
Definition Mesh.cpp:189
MeshLib::Mesh * removeElements(const MeshLib::Mesh &mesh, const std::vector< std::size_t > &removed_element_ids, const std::string &new_mesh_name)

References BaseLib::RunTime::elapsed(), extractBoundaryMeshes(), MeshToolsLib::MeshInformation::getBoundingBox(), MeshLib::Mesh::getElements(), MeshLib::Mesh::getName(), MeshLib::Mesh::getNumberOfElements(), MeshLib::Mesh::getNumberOfNodes(), BaseLib::MemWatch::getVirtMemUsage(), identifyAndWriteBoundaryMeshes(), INFO(), MeshLib::LINE, GitInfoLib::GitInfo::ogs_version, FileIO::GMSH::readGMSHMesh(), MeshToolsLib::removeElements(), BaseLib::RunTime::start(), MeshToolsLib::MeshInformation::writeAllNumbersOfElementTypes(), MeshLib::IO::writeMeshToFile(), MeshToolsLib::MeshInformation::writeMeshValidationResults(), and MeshToolsLib::MeshInformation::writePropertyVectorInformation().