LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
lf::io::GmshReader Class Reference

Reads a Gmsh *.msh file into a mesh::MeshFactory and provides a link between mesh::Entity objects and the gmsh's physical entities. More...

#include <lf/io/gmsh_reader.h>

Public Types

using size_type = mesh::Mesh::size_type
 
using dim_t = base::RefEl::dim_t
 
using GmshFileVariant = std::variant<GMshFileV2, GMshFileV4>
 

Public Member Functions

std::shared_ptr< mesh::Meshmesh ()
 Get the mesh that was read by this reader.
 
std::shared_ptr< const mesh::Meshmesh () const
 Get the mesh that was read by this reader.
 
size_type PhysicalEntityName2Nr (const std::string &name, dim_t codim=-1) const
 maps the name of a physical entity to the physical entity number of gmsh.
 
std::string PhysicalEntityNr2Name (size_type number, dim_t codim=-1) const
 Gives the name of a physical entity (inverse of PhysicalEntityName2Nr())
 
std::vector< std::pair< size_type, std::string > > PhysicalEntities (dim_t codim) const
 Retrieve a list of all (Gmsh) physical entities of the given codim.
 
std::vector< size_typePhysicalEntityNr (const mesh::Entity &e) const
 Return the Physical Entity Numbers associated with this mesh entity, sorted ascending.
 
bool IsPhysicalEntity (const mesh::Entity &e, size_type physical_entity_nr) const
 Test whether the given entity belongs to a Gmsh physical entity.
 
 GmshReader (std::unique_ptr< mesh::MeshFactory > factory, const GmshFileVariant &msh_file)
 Create a new GmshReader from the given MshFile (advanced usage)
 
 GmshReader (std::unique_ptr< mesh::MeshFactory > factory, const std::string &filename)
 Create a new GmshReader by reading from the specified file.
 

Private Member Functions

void InitGmshFile (const GMshFileV2 &msh_file)
 
void InitGmshFile (const GMshFileV4 &msh_file)
 

Private Attributes

std::shared_ptr< mesh::Meshmesh_
 The underlying grid created by the grid factory.
 
std::unique_ptr< mesh::MeshFactorymesh_factory_
 
std::shared_ptr< mesh::utils::AllCodimMeshDataSet< std::vector< size_type > > > physical_nrs_
 The PhysicalEntityNr of every entity (0 if not set):
 
std::multimap< std::string, std::pair< size_type, dim_t > > name_2_nr_
 Map from physicalEntity name -> nr, codim.
 
std::multimap< size_type, std::pair< std::string, dim_t > > nr_2_name_
 Map from physicalEntity nr -> name, codim.
 

Detailed Description

Reads a Gmsh *.msh file into a mesh::MeshFactory and provides a link between mesh::Entity objects and the gmsh's physical entities.

In order to import the *.msh file successfully make sure that:

The import of meshes from Gmsh-generated mesh files is also discussed in Lecture Document Example 2.7.1.11.

Sample usage:

// Read *.msh_file
auto mesh_factory = std::make_unique<mesh::hybrid2d::MeshFactory>(2);
GmshReader reader(std::move(mesh_factory), "my_mesh.msh");
// print all physical entities:
std::cout << "Physical Entities in Gmsh File " << std::endl;
std::cout
<< "---------------------------------------------------------------\n";
for (lf::base::dim_t codim = 0; codim <= 2; ++codim) {
for (auto& pair : reader.PhysicalEntities(codim)) {
std::cout << "codim = " << static_cast<int>(codim) << ": " << pair.first
<< " <=> " << pair.second << std::endl;
}
}
std::cout << std::endl << std::endl;
// Get Physical Entity Number from it's name (as specified in Gmsh):
auto air_nr = reader.PhysicalEntityName2Nr("air");
// Get all codim=0 entities that belong to the "air" physical entity:
for (auto& e : reader.mesh()->Entities(0)) {
if (reader.IsPhysicalEntity(*e, air_nr)) {
// This entity belongs to the "air" physical entity.
}
}

Note about mesh completeness:

In principle, Gmsh can export meshes that are not "complete", i.e. meshes containing entities with codim>0 that are not sub-entities of cell-entities. Also, if Gmsh exports a higher-order mesh, it has to introduce "auxilliary nodes" in order to define the higher-order mesh elements (these auxilliary nodes are normally placed on edges, faces or in the interior of cells).

Since incomplete meshes are usually not useful in a FEM context, GmshReader doesn't insert into the mesh auxilliary nodes or nodes not belonging to at least one cell. Auxilliary nodes are however used in the construction of higher order geometry objects.

Definition at line 70 of file gmsh_reader.h.

Member Typedef Documentation

◆ dim_t

Definition at line 73 of file gmsh_reader.h.

◆ GmshFileVariant

Definition at line 74 of file gmsh_reader.h.

◆ size_type

Definition at line 72 of file gmsh_reader.h.

Constructor & Destructor Documentation

◆ GmshReader() [1/2]

lf::io::GmshReader::GmshReader ( std::unique_ptr< mesh::MeshFactory > factory,
const GmshFileVariant & msh_file )

Create a new GmshReader from the given MshFile (advanced usage)

Parameters
factoryThe mesh::MeshFactory that is used to construct the mesh.
msh_fileA LehrFEM++ specific representation of a GmshFile.
See also
MshFile

Definition at line 24 of file gmsh_reader.cc.

References InitGmshFile().

◆ GmshReader() [2/2]

lf::io::GmshReader::GmshReader ( std::unique_ptr< mesh::MeshFactory > factory,
const std::string & filename )

Create a new GmshReader by reading from the specified file.

Parameters
factoryThe mesh::MeshFactory that is used to construct the mesh.
filenameThe filename of the .msh file that is read.
Note
If the factory.DimWorld() == 3, there must be at least one 3D mesh element in the *.msh file. Similarly, if factory.DimWorld() == 2 there should be only 2D mesh elements in the *.msh file!
GmshReader supports ASCII and Binary .msh files.

Definition at line 30 of file gmsh_reader.cc.

Member Function Documentation

◆ InitGmshFile() [1/2]

void lf::io::GmshReader::InitGmshFile ( const GMshFileV2 & msh_file)
private

◆ InitGmshFile() [2/2]

void lf::io::GmshReader::InitGmshFile ( const GMshFileV4 & msh_file)
private

◆ IsPhysicalEntity()

bool lf::io::GmshReader::IsPhysicalEntity ( const mesh::Entity & e,
size_type physical_entity_nr ) const

Test whether the given entity belongs to a Gmsh physical entity.

Parameters
eThe entity that should be tested.
physical_entity_nrThe number of the gmsh physical entity.
Returns
True if the entity e belongs to the physical entity.

This method is related to the method PhysicalEntityNr(): It returns true, if the vector returned by PhysicalEntityNr(e) contains physical_entity_nr

See also
PhysicalEntityNr() and the code snippet in the documentation of GmshReader.

Definition at line 17 of file gmsh_reader.cc.

References PhysicalEntityNr().

◆ mesh() [1/2]

std::shared_ptr< mesh::Mesh > lf::io::GmshReader::mesh ( )
inline

Get the mesh that was read by this reader.

Definition at line 79 of file gmsh_reader.h.

References mesh_.

◆ mesh() [2/2]

std::shared_ptr< const mesh::Mesh > lf::io::GmshReader::mesh ( ) const
inline

Get the mesh that was read by this reader.

Definition at line 84 of file gmsh_reader.h.

References mesh_.

◆ PhysicalEntities()

std::vector< std::pair< size_type, std::string > > lf::io::GmshReader::PhysicalEntities ( dim_t codim) const

Retrieve a list of all (Gmsh) physical entities of the given codim.

Parameters
codimThe codimension
Returns
A list of physical entities (number, name)

See the code snippet in the documentation of GmshReader.

Definition at line 102 of file gmsh_reader.cc.

References nr_2_name_.

◆ PhysicalEntityName2Nr()

size_type lf::io::GmshReader::PhysicalEntityName2Nr ( const std::string & name,
dim_t codim = -1 ) const

maps the name of a physical entity to the physical entity number of gmsh.

Parameters
nameName of the physical entity.
codimOptionally the codimension of the physical entity (w.r.t dimMesh), only needed if there are physical entities with the same name but different dimensions.
Returns
Number of the physical entity (as shown in Gmsh)
Note
If you have defined a physical entity in GMSH without giving it a name, you cannot use this function.
See also
PhysicalEntityNr2Name() and the code snippet in the documentation of GmshReader.

Definition at line 34 of file gmsh_reader.cc.

References name_2_nr_.

◆ PhysicalEntityNr()

std::vector< size_type > lf::io::GmshReader::PhysicalEntityNr ( const mesh::Entity & e) const

Return the Physical Entity Numbers associated with this mesh entity, sorted ascending.

Parameters
eThe entity of the grid.
Returns
The Physical Entity Number that was assigned in GMSH.

Definition at line 114 of file gmsh_reader.cc.

References physical_nrs_.

Referenced by IsPhysicalEntity().

◆ PhysicalEntityNr2Name()

std::string lf::io::GmshReader::PhysicalEntityNr2Name ( size_type number,
dim_t codim = -1 ) const

Gives the name of a physical entity (inverse of PhysicalEntityName2Nr())

Parameters
numberThe number of the physical entity (as obtained from PhysicalEntityName2Nr() )
codimSpecify additionally the codimension of the physical entity. If not specified and there is exactly one physical entity with the given number, that entity is returned. If there are multiple entities with the same number and codim=-1, an error is thrown.
Returns
The name of the physical entity with number number
See also
PhysicalEntityNr2Name

Definition at line 68 of file gmsh_reader.cc.

References nr_2_name_.

Member Data Documentation

◆ mesh_

std::shared_ptr<mesh::Mesh> lf::io::GmshReader::mesh_
private

The underlying grid created by the grid factory.

Definition at line 178 of file gmsh_reader.h.

Referenced by InitGmshFile(), InitGmshFile(), mesh(), and mesh().

◆ mesh_factory_

std::unique_ptr<mesh::MeshFactory> lf::io::GmshReader::mesh_factory_
private

Definition at line 180 of file gmsh_reader.h.

Referenced by InitGmshFile(), and InitGmshFile().

◆ name_2_nr_

std::multimap<std::string, std::pair<size_type, dim_t> > lf::io::GmshReader::name_2_nr_
private

Map from physicalEntity name -> nr, codim.

Definition at line 187 of file gmsh_reader.h.

Referenced by InitGmshFile(), InitGmshFile(), and PhysicalEntityName2Nr().

◆ nr_2_name_

std::multimap<size_type, std::pair<std::string, dim_t> > lf::io::GmshReader::nr_2_name_
private

Map from physicalEntity nr -> name, codim.

Definition at line 190 of file gmsh_reader.h.

Referenced by InitGmshFile(), InitGmshFile(), PhysicalEntities(), and PhysicalEntityNr2Name().

◆ physical_nrs_

std::shared_ptr<mesh::utils::AllCodimMeshDataSet<std::vector<size_type> > > lf::io::GmshReader::physical_nrs_
private

The PhysicalEntityNr of every entity (0 if not set):

Definition at line 184 of file gmsh_reader.h.

Referenced by InitGmshFile(), InitGmshFile(), and PhysicalEntityNr().


The documentation for this class was generated from the following files: