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 41 of file GMSH2OGS.cpp.

45{
46 // Create boundary mesh.
47 auto nodes = copyNodeVector(mesh.getNodes());
48 auto elements = copyElementVector(selected_elements, nodes);
49
50 // Cleanup unused nodes
52 MeshToolsLib::markUnusedNodes(elements, nodes), nodes);
53
54 return std::make_unique<MeshLib::Mesh>(std::move(mesh_name), nodes,
55 elements);
56}
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 58 of file GMSH2OGS.cpp.

60{
61 auto const material_ids = materialIDs(mesh);
62 if (material_ids == nullptr)
63 {
65 "GMSH2OGS: Expected material ids to be present in the mesh to "
66 "extract boundary meshes.");
67 }
68
69 std::vector<std::unique_ptr<MeshLib::Mesh>> boundary_meshes;
70
71 auto const& elements = mesh.getElements();
72
73 while (!selected_element_ids.empty())
74 {
75 // Partition in two blocks, with elements for the material id at
76 // the end, s.t. one can erase them easily.
77 int const material_id = (*material_ids)[selected_element_ids.back()];
78 auto split = std::partition(
79 begin(selected_element_ids), end(selected_element_ids),
80 [&material_id, &material_ids](int const id)
81 { return (*material_ids)[id] != material_id; });
82
83 // Add elements with same material id to the mesh.
84 std::vector<MeshLib::Element*> single_material_elements;
85 single_material_elements.reserve(
86 std::distance(split, end(selected_element_ids)));
87 std::transform(split, end(selected_element_ids),
88 std::back_inserter(single_material_elements),
89 [&](int const id) { return elements[id]; });
90
91 // Remove already extracted elements.
92 selected_element_ids.erase(split, end(selected_element_ids));
93
94 // Create boundary mesh and identify the nodes/elements.
95 boundary_meshes.push_back(createMeshFromElements(
96 mesh, single_material_elements, std::to_string(material_id)));
97 }
98 return boundary_meshes;
99}
#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:41
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition Mesh.cpp:269

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 101 of file GMSH2OGS.cpp.

105{
106 // Bulk mesh node searcher usef for boundary mesh identification.
107 auto const& mesh_node_searcher =
109 mesh,
110 std::make_unique<MeshGeoToolsLib::SearchLength>(
111 0)); // Exact match of nodes.
112
113 for (auto& boundary_mesh : boundary_meshes)
114 {
115 identifySubdomainMesh(*boundary_mesh, mesh, mesh_node_searcher);
116
117 // Save the boundary mesh.
118 auto boundary_mesh_file_name = BaseLib::dropFileExtension(file_name) +
119 '_' + boundary_mesh->getName() +
120 BaseLib::getFileExtension(file_name);
121
122 MeshLib::IO::writeMeshToFile(*boundary_mesh, boundary_mesh_file_name);
123 }
124}
static OGS_NO_DANGLING 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 126 of file GMSH2OGS.cpp.

127{
128 TCLAP::CmdLine cmd(
129 "Converting meshes in gmsh file format (ASCII, version 2.2) to a vtk "
130 "unstructured grid file (new OGS file format) or to the old OGS file "
131 "format - see options.\n\n"
132 "OpenGeoSys-6 software, version " +
134 ".\n"
135 "Copyright (c) 2012-2025, OpenGeoSys Community "
136 "(http://www.opengeosys.org)",
138
139 TCLAP::ValueArg<std::string> ogs_mesh_arg(
140 "o",
141 "out",
142 "Output (.msh | .vtk). Filename for output mesh (if extension is .msh, "
143 "old OGS-5 file format "
144 "is written, if extension is .vtu, a vtk unstructure grid file is "
145 "written (OGS-6 mesh format))",
146 true,
147 "",
148 "OUTPUT_FILE");
149 cmd.add(ogs_mesh_arg);
150
151 TCLAP::ValueArg<std::string> gmsh_mesh_arg("i", "in", "Input (.msh) file",
152 true, "", "INPUT_FILE");
153 cmd.add(gmsh_mesh_arg);
154
155 TCLAP::SwitchArg valid_arg("v", "validation", "validate the mesh");
156 cmd.add(valid_arg);
157
158 TCLAP::SwitchArg create_boundary_meshes_arg(
159 "b", "boundaries", "if set, boundary meshes will be generated");
160 cmd.add(create_boundary_meshes_arg);
161
162 TCLAP::SwitchArg exclude_lines_arg(
163 "e", "exclude-lines",
164 "if set, lines will not be written to the ogs mesh");
165 cmd.add(exclude_lines_arg);
166
167 auto log_level_arg = BaseLib::makeLogLevelArg();
168 cmd.add(log_level_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 BaseLib::MPI::Setup mpi_setup(argc, argv);
180 BaseLib::initOGSLogger(log_level_arg.getValue());
181
182 // *** read mesh
183 INFO("Reading {:s}.", gmsh_mesh_arg.getValue());
184#ifndef WIN32
185 BaseLib::MemWatch mem_watch;
186 unsigned long mem_without_mesh(mem_watch.getVirtMemUsage());
187#endif
188 BaseLib::RunTime run_time;
189 run_time.start();
190 MeshLib::Mesh* mesh(FileIO::GMSH::readGMSHMesh(gmsh_mesh_arg.getValue(),
191 gmsh2_arg.getValue()));
192
193 if (mesh == nullptr)
194 {
195 INFO("Could not read mesh from {:s}.", gmsh_mesh_arg.getValue());
196 return -1;
197 }
198#ifndef WIN32
199 INFO("Mem for mesh: {} MiB",
200 (mem_watch.getVirtMemUsage() - mem_without_mesh) / (1024 * 1024));
201#endif
202
203 INFO("Time for reading: {:f} seconds.", run_time.elapsed());
204 INFO("Read {:d} nodes and {:d} elements.", mesh->getNumberOfNodes(),
205 mesh->getNumberOfElements());
206
207 // Optionally remove line elements or create boundary meshes.
208 if (exclude_lines_arg.getValue() || create_boundary_meshes_arg.getValue())
209 {
210 auto ex = MeshLib::ElementSearch(*mesh);
211 ex.searchByElementType(MeshLib::MeshElemType::LINE);
212 auto const& selected_element_ids = ex.getSearchedElementIDs();
213
214 // First we extract the boundary meshes, then optionally remove the line
215 // elements, and only then run the node/element identification and write
216 // the meshes.
217
218 std::vector<std::unique_ptr<MeshLib::Mesh>> boundary_meshes;
219 if (create_boundary_meshes_arg.getValue())
220 {
221 boundary_meshes =
222 extractBoundaryMeshes(*mesh, selected_element_ids);
223 }
224
225 if (exclude_lines_arg.getValue())
226 {
228 *mesh, selected_element_ids, mesh->getName() + "-withoutLines");
229 if (m != nullptr)
230 {
231 INFO("Removed {:d} lines.",
232 mesh->getNumberOfElements() - m->getNumberOfElements());
233 std::swap(m, mesh);
234 delete m;
235 }
236 else
237 {
238 INFO("Mesh does not contain any lines.");
239 }
240 }
241
242 if (create_boundary_meshes_arg.getValue())
243 {
244 identifyAndWriteBoundaryMeshes(*mesh, ogs_mesh_arg.getValue(),
245 boundary_meshes);
246 }
247 }
248 // *** print meshinformation
249
250 INFO("Please check your mesh carefully!");
251 INFO(
252 "Degenerated or redundant mesh elements can cause OGS to stop or "
253 "misbehave.");
254 INFO("Use the -e option to delete redundant line elements.");
255
256 // Geometric information
257 const GeoLib::AABB aabb =
259 INFO("Axis aligned bounding box: {}", aabb);
260
261 auto const [min, max] = minMaxEdgeLength(mesh->getElements());
262 INFO("Edge length: [{:g}, {:g}]", min, max);
263
264 // Element information
266
268
269 if (valid_arg.isSet())
270 {
272 }
273
274 // *** write mesh in new format
275 MeshLib::IO::writeMeshToFile(*mesh, ogs_mesh_arg.getValue());
276
277 delete mesh;
278 return EXIT_SUCCESS;
279}
static std::vector< std::unique_ptr< MeshLib::Mesh > > extractBoundaryMeshes(MeshLib::Mesh const &mesh, std::vector< std::size_t > selected_element_ids)
Definition GMSH2OGS.cpp:58
static void identifyAndWriteBoundaryMeshes(MeshLib::Mesh const &mesh, std::string const &file_name, std::vector< std::unique_ptr< MeshLib::Mesh > > &boundary_meshes)
Definition GMSH2OGS.cpp:101
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:36
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
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:64
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:190
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(), BaseLib::initOGSLogger(), MeshLib::LINE, BaseLib::makeLogLevelArg(), 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().