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
51 removeMarkedNodes(markUnusedNodes(elements, nodes), nodes);
52
53 return std::make_unique<MeshLib::Mesh>(std::move(mesh_name), nodes,
54 elements);
55}
std::vector< Node * > copyNodeVector(const std::vector< Node * > &nodes)
Creates a deep copy of a Node vector.
std::vector< bool > markUnusedNodes(std::vector< Element * > const &elements, std::vector< Node * > const &nodes)
Marks nodes not used by any of the elements.
void removeMarkedNodes(std::vector< bool > const &nodes_to_delete, std::vector< Node * > &nodes)
Deallocates and removes nodes marked true.
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)

References MeshLib::copyElementVector(), MeshLib::copyNodeVector(), MeshLib::Mesh::getNodes(), MeshLib::markUnusedNodes(), and MeshLib::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 57 of file GMSH2OGS.cpp.

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

References createMeshFromElements(), MeshLib::Mesh::getElements(), MeshLib::materialIDs(), 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 100 of file GMSH2OGS.cpp.

104{
105 // Bulk mesh node searcher usef for boundary mesh identification.
106 auto const& mesh_node_searcher =
108 mesh,
109 std::make_unique<MeshGeoToolsLib::SearchLength>(
110 0)); // Exact match of nodes.
111
112 for (auto& boundary_mesh : boundary_meshes)
113 {
114 identifySubdomainMesh(*boundary_mesh, mesh, mesh_node_searcher);
115
116 // Save the boundary mesh.
117 auto boundary_mesh_file_name = BaseLib::dropFileExtension(file_name) +
118 '_' + boundary_mesh->getName() +
119 BaseLib::getFileExtension(file_name);
120
121 MeshLib::IO::writeMeshToFile(*boundary_mesh, boundary_mesh_file_name);
122 }
123}
static MeshNodeSearcher const & getMeshNodeSearcher(MeshLib::Mesh const &mesh, std::unique_ptr< MeshGeoToolsLib::SearchLength > &&search_length_algorithm)
std::string getFileExtension(const std::string &path)
Definition: FileTools.cpp:186
std::string dropFileExtension(std::string const &filename)
Definition: FileTools.cpp:169
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(), MeshGeoToolsLib::identifySubdomainMesh(), and MeshLib::IO::writeMeshToFile().

Referenced by main().

◆ main()

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

Definition at line 125 of file GMSH2OGS.cpp.

126{
127 TCLAP::CmdLine cmd(
128 "Converting meshes in gmsh file format (ASCII, version 2.2) to a vtk "
129 "unstructured grid file (new OGS file format) or to the old OGS file "
130 "format - see options.\n\n"
131 "OpenGeoSys-6 software, version " +
133 ".\n"
134 "Copyright (c) 2012-2022, OpenGeoSys Community "
135 "(http://www.opengeosys.org)",
137
138 TCLAP::ValueArg<std::string> ogs_mesh_arg(
139 "o",
140 "out",
141 "filename for output mesh (if extension is .msh, old OGS-5 fileformat "
142 "is written, if extension is .vtu, a vtk unstructure grid file is "
143 "written (OGS-6 mesh format))",
144 true,
145 "",
146 "filename as string");
147 cmd.add(ogs_mesh_arg);
148
149 TCLAP::ValueArg<std::string> gmsh_mesh_arg("i", "in", "gmsh input file",
150 true, "", "filename as string");
151 cmd.add(gmsh_mesh_arg);
152
153 TCLAP::SwitchArg valid_arg("v", "validation", "validate the mesh");
154 cmd.add(valid_arg);
155
156 TCLAP::SwitchArg create_boundary_meshes_arg(
157 "b", "boundaries", "if set, boundary meshes will be generated");
158 cmd.add(create_boundary_meshes_arg);
159
160 TCLAP::SwitchArg exclude_lines_arg(
161 "e", "exclude-lines",
162 "if set, lines will not be written to the ogs mesh");
163 cmd.add(exclude_lines_arg);
164
165 cmd.parse(argc, argv);
166
167 // *** read mesh
168 INFO("Reading {:s}.", gmsh_mesh_arg.getValue());
169#ifndef WIN32
170 BaseLib::MemWatch mem_watch;
171 unsigned long mem_without_mesh(mem_watch.getVirtMemUsage());
172#endif
173 BaseLib::RunTime run_time;
174 run_time.start();
175 MeshLib::Mesh* mesh(FileIO::GMSH::readGMSHMesh(gmsh_mesh_arg.getValue()));
176
177 if (mesh == nullptr)
178 {
179 INFO("Could not read mesh from {:s}.", gmsh_mesh_arg.getValue());
180 return -1;
181 }
182#ifndef WIN32
183 INFO("Mem for mesh: {} MiB",
184 (mem_watch.getVirtMemUsage() - mem_without_mesh) / (1024 * 1024));
185#endif
186
187 INFO("Time for reading: {:f} seconds.", run_time.elapsed());
188 INFO("Read {:d} nodes and {:d} elements.", mesh->getNumberOfNodes(),
189 mesh->getNumberOfElements());
190
191 // Optionally remove line elements or create boundary meshes.
192 if (exclude_lines_arg.getValue() || create_boundary_meshes_arg.getValue())
193 {
194 auto ex = MeshLib::ElementSearch(*mesh);
195 ex.searchByElementType(MeshLib::MeshElemType::LINE);
196 auto const& selected_element_ids = ex.getSearchedElementIDs();
197
198 // First we extract the boundary meshes, then optionally remove the line
199 // elements, and only then run the node/element identification and write
200 // the meshes.
201
202 std::vector<std::unique_ptr<MeshLib::Mesh>> boundary_meshes;
203 if (create_boundary_meshes_arg.getValue())
204 {
205 boundary_meshes =
206 extractBoundaryMeshes(*mesh, selected_element_ids);
207 }
208
209 if (exclude_lines_arg.getValue())
210 {
211 auto m = MeshLib::removeElements(*mesh, selected_element_ids,
212 mesh->getName() + "-withoutLines");
213 if (m != nullptr)
214 {
215 INFO("Removed {:d} lines.",
216 mesh->getNumberOfElements() - m->getNumberOfElements());
217 std::swap(m, mesh);
218 delete m;
219 }
220 else
221 {
222 INFO("Mesh does not contain any lines.");
223 }
224 }
225
226 if (create_boundary_meshes_arg.getValue())
227 {
228 identifyAndWriteBoundaryMeshes(*mesh, ogs_mesh_arg.getValue(),
229 boundary_meshes);
230 }
231 }
232 // *** print meshinformation
233
234 INFO("Please check your mesh carefully!");
235 INFO(
236 "Degenerated or redundant mesh elements can cause OGS to stop or "
237 "misbehave.");
238 INFO("Use the -e option to delete redundant line elements.");
239
240 // Geometric information
242 auto const minPt(aabb.getMinPoint());
243 auto const maxPt(aabb.getMaxPoint());
244 INFO("Node coordinates:");
245 INFO("\tx [{:g}, {:g}] (extent {:g})", minPt[0], maxPt[0],
246 maxPt[0] - minPt[0]);
247 INFO("\ty [{:g}, {:g}] (extent {:g})", minPt[1], maxPt[1],
248 maxPt[1] - minPt[1]);
249 INFO("\tz [{:g}, {:g}] (extent {:g})", minPt[2], maxPt[2],
250 maxPt[2] - minPt[2]);
251
252 INFO("Edge length: [{:g}, {:g}]", mesh->getMinEdgeLength(),
253 mesh->getMaxEdgeLength());
254
255 // Element information
257
259
260 if (valid_arg.isSet())
261 {
263 }
264
265 // *** write mesh in new format
266 MeshLib::IO::writeMeshToFile(*mesh, ogs_mesh_arg.getValue());
267
268 delete mesh;
269}
static std::vector< std::unique_ptr< MeshLib::Mesh > > extractBoundaryMeshes(MeshLib::Mesh const &mesh, std::vector< std::size_t > selected_element_ids)
Definition: GMSH2OGS.cpp:57
static void identifyAndWriteBoundaryMeshes(MeshLib::Mesh const &mesh, std::string const &file_name, std::vector< std::unique_ptr< MeshLib::Mesh > > &boundary_meshes)
Definition: GMSH2OGS.cpp:100
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:34
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:49
Eigen::Vector3d const & getMaxPoint() const
Definition: AABB.h:179
Eigen::Vector3d const & getMinPoint() const
Definition: AABB.h:172
Element search class.
Definition: ElementSearch.h:28
static void writePropertyVectorInformation(const MeshLib::Mesh &mesh)
writes out property vector information
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
MeshLib::Mesh * readGMSHMesh(std::string const &fname)
Definition: GmshReader.cpp:270
GITINFOLIB_EXPORT const std::string ogs_version
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(), MeshLib::MeshInformation::getBoundingBox(), MeshLib::Mesh::getMaxEdgeLength(), GeoLib::AABB::getMaxPoint(), MeshLib::Mesh::getMinEdgeLength(), GeoLib::AABB::getMinPoint(), MeshLib::Mesh::getName(), MeshLib::Mesh::getNumberOfElements(), MeshLib::Mesh::getNumberOfNodes(), BaseLib::MemWatch::getVirtMemUsage(), identifyAndWriteBoundaryMeshes(), INFO(), MeshLib::LINE, GitInfoLib::GitInfo::ogs_version, FileIO::GMSH::readGMSHMesh(), MeshLib::removeElements(), BaseLib::RunTime::start(), MeshLib::MeshInformation::writeAllNumbersOfElementTypes(), MeshLib::IO::writeMeshToFile(), MeshLib::MeshInformation::writeMeshValidationResults(), and MeshLib::MeshInformation::writePropertyVectorInformation().