Skip to content

Converting Gmsh mesh format to MDPA

marandra edited this page Nov 10, 2022 · 4 revisions

Converting Gmsh mesh format to MDPA

It is possible to convert Gmsh native mesh format (.msh) to MDPA, Kratos' native mesh format) using meshio's mdpa module.

Besides the geometry, MDPA format also includes data which is not available in the Gmsh mesh, particularly:

  1. MDPA distinguishes two types of cells: elements and conditions

meshio's mdpa module will categorize cells into elements or conditions (the cells on which the boundary conditions are applied) based on their dimension, i.e., 3D cells are considered elements and any other cell is considered a condition.

  1. Each element or condition in the MDPA file does not only represent the geometry of the cell, but also its physics.

After conversion, meshio will return cell types based only on their geometry. The user may need to edit the MDPA file and rename the elements and conditions accordingly to the name required by its application. This step can added to the conversion script.

List of possible cell names returned by meshio:

  • Line3D2
  • Triangle3D3
  • Quadrilateral3D4
  • Tetrahedra3D4
  • Hexahedra3D8
  • Prism3D6
  • Line3D3
  • Triangle3D6
  • Quadrilateral3D9
  • Tetrahedra3D10
  • Hexahedra3D27
  • Point3D
  • Quadrilateral3D8
  • Hexahedra3D20

Summary of the pipeline

  1. Create your .geo gmsh geometry, defining physical volumes and surfaces
  2. Generate its .msh mesh
  3. Use meshio to convert .msh to .mdpa
  4. Rename elements and conditions

Step by step tutorial

A small working example of the steps above, going from the initial definition of the geometry file until the final .mdpa file.

1. Install required programs and libraries

Gmsh

This step depends on your system.

meshio

The official repository of meshio does not have the last version of the MDPA module yet. For now, you can clone our forked repository. We'll update these instructions when/if the module makes it into the official repository.

Clone the repository, and make sure that the PYTHONPATH environment variable is properly set:

cd /path/to/
git clone git@github.com:marandra/meshio.git
export PYTHONPATH=/path/to/meshio/src:$PYTHONPATH

2. Definition of the geometry

Create a file cylinder.geo:

// geometry:
//          Lx
//     ____________
//    |            |
// cy -   O        | Ly
//    |___,________|
//        cx

Ly = 1;
Lx = 5;
Lz = 0.1;
cr = 0.1;
cy = 0.0;
cx = 1.25;

pCC = newp; Point(pCC) = {    cx,    cy, -Lz/2};
p00 = newp; Point(p00) = {     0, -Ly/2, -Lz/2, 0.4};
p01 = newp; Point(p01) = {    Lx, -Ly/2, -Lz/2, 0.4};
p03 = newp; Point(p03) = { cx+cr,     0, -Lz/2, 0.4};
p04 = newp; Point(p04) = {    cx,   -cr, -Lz/2, 0.4};
p05 = newp; Point(p05) = { cx-cr,     0, -Lz/2, 0.4};
p07 = newp; Point(p07) = {     0,  Ly/2, -Lz/2, 0.4};
p08 = newp; Point(p08) = {    Lx,  Ly/2, -Lz/2, 0.4};
p09 = newp; Point(p09) = {    cx,    cr, -Lz/2, 0.4};
l00 = newl;   Line(l00) = {p00, p01};
l01 = newl;   Line(l01) = {p01, p08};
l03 = newl; Circle(l03) = {p03, pCC, p04};
l04 = newl; Circle(l04) = {p04, pCC, p05};
l07 = newl; Circle(l07) = {p05, pCC, p09};
l08 = newl; Circle(l08) = {p09, pCC, p03};
l10 = newl;   Line(l10) = {p08, p07};
l11 = newl;   Line(l11) = {p07, p00};
Transfinite Line{l03} = 4;
Transfinite Line{l04} = 4;
Transfinite Line{l07} = 4;
Transfinite Line{l08} = 4;
ll0 = newll; Curve Loop(ll0) = {l00, l01, l10, l11};     
ll1 = newll; Curve Loop(ll1) = {l03, l04, l07, l08};     
wall = news; Plane Surface(wall) = {ll0, ll1};
BoundingBox;
box[] = Extrude{0, 0, Lz}{Surface{wall}; Layers{1};};

Physical Volume("Fluid") = {box[1]};
Physical Surface("Symm") = {wall, box[0]};
Physical Surface("Outlet") = {box[3]};
Physical Surface("Walls") = {box[2], box[4]};
Physical Surface("Inlet") = {box[5]};
Physical Surface("Cylinder") = {box[6], box[7], box[8], box[9]};

Note the block with the definitions of the physical parts, that define the regions of interest of the mesh, where boundary and initial conditions will be applied.

geometry_small

3. Generate the .msh mesh

gmsh -3 -o cylinder.msh cylinder.geo

That generates the following mesh: mesh

4. Convert the mesh

Create a simple python script msh2mdpa.py, for calling meshio:

import sys
import meshio

fni = sys.argv[1]
fno = fni.rsplit('.', 1)[0] + ".mdpa"
m = meshio.read(fni)
meshio.write(fno, m, "mdpa")

and execute it:

python msh2mdpa.py cylinder.msh

The output will be a cylinder.mdpa file, as shown below (edited):

...
Begin Nodes
 1 0.0000000000000000e+00 -5.0000000000000000e-01 -5.0000000000000003e-02
 ...
 204 1.1232090300445310e+00 -9.9985260714015506e-02 5.0000000000000003e-02
End Nodes

Begin Conditions Triangle3D3  // GUI group identifier: Symm
   1000001 0        5      35     135
   ...
   1000160 0      125     128     139
End Conditions

Begin Conditions Triangle3D3  // GUI group identifier: Walls
   2000001 0        9       1      17
   ...
   2000026 0       64       2      10
End Conditions

Begin Conditions Triangle3D3  // GUI group identifier: Outlet
   3000001 0       10       2      29
   ...
   3000006 0       66       7      11
End Conditions
...
Begin Elements Tetrahedra3D4  // GUI group identifier: Fluid
  11000001 0        5      35     135     193
  ...
  11000480 0      128     125     186     197
End Elements
...

The name of elements and conditions describe only their geometry. It is necessary to change their names to the actual elements and conditions required in the project. An example of the MDPA file after renaming:

...
Begin Nodes
 1 0.0000000000000000e+00 -5.0000000000000000e-01 -5.0000000000000003e-02
 ...
 204 1.1232090300445310e+00 -9.9985260714015506e-02 5.0000000000000003e-02
End Nodes

Begin Conditions Slip3D  // GUI group identifier: Symm
   1000001 0        5      35     135
   ...
   1000160 0      125     128     139
End Conditions

Begin Conditions MonolithicWallCondition3D3N  // GUI group identifier: Walls
   2000001 0        9       1      17
   ...
   2000026 0       64       2      10
End Conditions

Begin Conditions AutomaticInlet3D_Inlet  // GUI group identifier: Inlet
   3000001 0       10       2      29
   ...
   3000006 0       66       7      11
End Conditions
...
Begin Elements Element3D4N  // GUI group identifier: Fluid
  11000001 0        5      35     135     193
  ...
  11000480 0      128     125     186     197
End Elements
...

Finally, add the mesh to ProjectParameters.json:

   "solver_settings"  : {
      ...
      "model_import_settings"       : {
            "input_type"     : "mdpa",
            "input_filename" : "cylinder"
      },
   },
   ...   

Project information

Getting Started

Tutorials

Developers

Kratos structure

Conventions

Solvers

Debugging, profiling and testing

HOW TOs

Utilities

Kratos API

Kratos Structural Mechanics API

Clone this wiki locally