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 cmd.parse(argc, argv);
171
172#ifdef USE_PETSC
173 MPI_Init(&argc, &argv);
174#endif
175
176 // *** read mesh
177 INFO("Reading {:s}.", gmsh_mesh_arg.getValue());
178#ifndef WIN32
179 BaseLib::MemWatch mem_watch;
180 unsigned long mem_without_mesh(mem_watch.getVirtMemUsage());
181#endif
182 BaseLib::RunTime run_time;
183 run_time.start();
184 MeshLib::Mesh* mesh(FileIO::GMSH::readGMSHMesh(gmsh_mesh_arg.getValue()));
185
186 if (mesh == nullptr)
187 {
188 INFO("Could not read mesh from {:s}.", gmsh_mesh_arg.getValue());
189#ifdef USE_PETSC
190 MPI_Finalize();
191#endif
192 return -1;
193 }
194#ifndef WIN32
195 INFO("Mem for mesh: {} MiB",
196 (mem_watch.getVirtMemUsage() - mem_without_mesh) / (1024 * 1024));
197#endif
198
199 INFO("Time for reading: {:f} seconds.", run_time.elapsed());
200 INFO("Read {:d} nodes and {:d} elements.", mesh->getNumberOfNodes(),
201 mesh->getNumberOfElements());
202
203 // Optionally remove line elements or create boundary meshes.
204 if (exclude_lines_arg.getValue() || create_boundary_meshes_arg.getValue())
205 {
206 auto ex = MeshLib::ElementSearch(*mesh);
207 ex.searchByElementType(MeshLib::MeshElemType::LINE);
208 auto const& selected_element_ids = ex.getSearchedElementIDs();
209
210 // First we extract the boundary meshes, then optionally remove the line
211 // elements, and only then run the node/element identification and write
212 // the meshes.
213
214 std::vector<std::unique_ptr<MeshLib::Mesh>> boundary_meshes;
215 if (create_boundary_meshes_arg.getValue())
216 {
217 boundary_meshes =
218 extractBoundaryMeshes(*mesh, selected_element_ids);
219 }
220
221 if (exclude_lines_arg.getValue())
222 {
224 *mesh, selected_element_ids, mesh->getName() + "-withoutLines");
225 if (m != nullptr)
226 {
227 INFO("Removed {:d} lines.",
228 mesh->getNumberOfElements() - m->getNumberOfElements());
229 std::swap(m, mesh);
230 delete m;
231 }
232 else
233 {
234 INFO("Mesh does not contain any lines.");
235 }
236 }
237
238 if (create_boundary_meshes_arg.getValue())
239 {
240 identifyAndWriteBoundaryMeshes(*mesh, ogs_mesh_arg.getValue(),
241 boundary_meshes);
242 }
243 }
244 // *** print meshinformation
245
246 INFO("Please check your mesh carefully!");
247 INFO(
248 "Degenerated or redundant mesh elements can cause OGS to stop or "
249 "misbehave.");
250 INFO("Use the -e option to delete redundant line elements.");
251
252 // Geometric information
253 const GeoLib::AABB aabb =
255 INFO("Axis aligned bounding box: {}", aabb);
256
257 auto const [min, max] = minMaxEdgeLength(mesh->getElements());
258 INFO("Edge length: [{:g}, {:g}]", min, max);
259
260 // Element information
262
264
265 if (valid_arg.isSet())
266 {
268 }
269
270 // *** write mesh in new format
271 MeshLib::IO::writeMeshToFile(*mesh, ogs_mesh_arg.getValue());
272
273 delete mesh;
274#ifdef USE_PETSC
275 MPI_Finalize();
276#endif
277 return EXIT_SUCCESS;
278}
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)
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().