edit

dune-gmsh4

dune-gmsh4

Dune-Gmsh4

File reader for the Gmsh MSH file format version 4.

Summary

Provides a file reader to read MSH file format version 4. It supports various grid-creator policies. Higher-order parametrized grids can be read using a LagrangeGridCreator.

Installation Instructions

dune-gmsh4 requires the DUNE core modules, version 2.9 or later. Please see the general instructions for building DUNE modules for detailed instructions on how to build the module.

Usage

Either construct a grid directly

#include <dune/gmsh4/gmsh4reader.hh>
#include <dune/grid/uggrid.hh>

int main()
{
  using namespace Dune;
  using Grid = UGGrid<2>;
  std::unique_ptr grid = Gmsh4Reader<Grid>::createGridFromFile("filename.msh");
}

or explicitly use a grid-creator

#include <dune/foamgrid/foamgrid.hh>
#include <dune/gmsh4/gmsh4reader.hh>
#include <dune/gmsh4/gridcreators/lagrangegridcreator.hh>

int main()
{
  using namespace Dune;
  using Grid = FoamGrid<2,3>;
  GridFactory<Grid> factory;
  Gmsh4::LagrangeGridCreator creator{factory};
  Gmsh4Reader reader{creator};

  reader.read("filename.msh");
  auto grid = factory.createGrid();
}

Note, the grid-creator itself is a grid-function.

Physical groups and boundary segments

The ContinuousGridCreator and the LagrangeGridCreator store the physical tags of the elements and of the boundary segments, indexed by the insertion index of the grid factory. Boundary segments are inserted for the elements of dimension dim-1 in the file that lie on the domain boundary, i.e., on entities bounding exactly one entity of dimension dim. Interfaces between subdomains are not inserted as boundary segments.

GridFactory<Grid> factory;
Gmsh4::ContinuousGridCreator creator{factory};
Gmsh4Reader reader{creator};
reader.read("filename.msh");
auto grid = factory.createGrid();

auto const& elementTags = creator.elementTags();
auto const& boundaryTags = creator.boundaryTags();
for (auto const& element : elements(grid->leafGridView())) {
  std::vector<int> const& tags = elementTags[factory.insertionIndex(element)];
  for (auto const& intersection : intersections(grid->leafGridView(), element))
    if (intersection.boundary() && factory.wasInserted(intersection)) {
      std::vector<int> const& bTags = boundaryTags[factory.insertionIndex(intersection)];
      // ...
    }
}

// name of the physical group of dimension 1 with tag 10, as given in the $PhysicalNames section
std::string name = reader.physicalName(1, 10);

If physical groups are defined, Gmsh writes only the elements of physical groups by default. Boundary segments without elements in the file are created by the grid and are not inserted, so check factory.wasInserted(intersection) before accessing boundaryTags().

Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.131.0 (Nov 27, 23:23, 2026)