CFD | Gmsh | meshing
Gmsh
Gmsh is an open source 3D finite element mesh generator with a built-in CAD engine and post-processor. Its design goal is to provide a fast, light and user-friendly meshing tool with parametric input and flexible visualization capabilities.
Gmsh is built around four modules
- geometry,
- mesh,
- solver and
- post-processing
Examples
2D cylinder
1 | |
Step by Step




2D case: cylinder by simpleFoam

Edit constant/polyMesh/boundary
- For 2D case,
1 | |
Run OpenFOAM
gmshToFoam
1 | |
then, copy momentumTransport and
transportProperties to constant/ folder.
execute solver
- No
blockMesh
1 | |
Older OpenFOAM --> Gmsh 2.x format
Older OpenFOAM gmshToFoam only supportsthe classic Gmsh 2.x format.
- Newer Gmsh files contain sections like
$Entities, binary data, or different element ordering that confuse the parser.
How to Fix It
If you are using Gmsh → OpenFOAM (most
frequent case):
- Open the mesh file in a text editor and look at the very beginning.
It should start with something like
$MeshFormat(forGmsh) or clearASCIIheaders. If it looks binary or has strange characters → wrong format.
1 | |
- In
Gmsh, go to File → Export and choose:- Format:
Gmsh 2.2(or "Gmsh 2 ASCII") — not the default "Gmsh" or "Gmsh Unstructured". - Uncheck "Save all elements" if you have physical groups.
- Format:
- If you are on a newer
OpenFOAMversion (v2312,v2406, etc.),gmshToFoamhas improved but still often fails with very recent Gmsh. Downgrading the export format is safest. - Using the Gmsh GUI (easiest)
- Finish meshing your geometry (Mesh → 2D / 3D as needed)
- OpenFOAM must be 3D even your case is 2D !!!
- Go to
File→Export. - In the
filedialog:- Choose file type:
Gmsh mesh (*.msh)(or just type a name ending with .msh). - Click the
Optionsbutton (or look at the bottom of the dialog). - In the
exportoptions:Select Version 2 ASCII(sometimes shown as “MSH 2 ASCII” or “Version 2.2”).- Uncheck “Save all elements” (important — otherwise gmshToFoam may complain about unhandled element types like points/lines).
- Uncheck “Save parametric coordinates” (recommended).
- Choose file type:
OpenFOAM (via gmshToFoam) only accepts 3D volume meshes
- OpenFOAM (via gmshToFoam) only accepts 3D volume meshes.
How to fix it (for Gmsh 4.15)
Method 1: Force 3D meshing in Gmsh GUI (recommended)
- Open your geometry in Gmsh.
- Make sure you have defined Physical Volumes (not only Physical Surfaces).
- Go to Geometry → Physical Groups → Add → Volume and select your volume(s).
- In the Mesh module:
- Click Mesh → 3D (this is crucial — do not click only 2D).
- After meshing finishes, go to File → Export.
- Choose Gmsh mesh (*.msh).
- Click Options and set:
- Version: 2 ASCII (or 2.2)
- Uncheck "Save all elements"
- Uncheck “Save parametric coordinates”
- Export as cylinder.msh.
Method 3: Add this at the end of your .geo file
Edit script
1 | |
then, Reload script.
gmshToFoam issue
1 | |
- FOAM Warning
- can be ignored. As gmshToFoam first adds all boundary faces to defaultFaces patch and then move faces to patches defined in MSH file. IIRC I've already posted explanation on the forum, or you can take a look at sources of gmshToFoam. link
- Not necessarily wrong. What this says is that OpenFOAM has found 824 faces external to your geometry that did not have a boundary condition associated with them and it has collected them together in a patch.If you think you have defined boundary types on the complete external surface of the model, there is a reason to worry; otherwise, just have a look at the default patch and see if it looks OK or not.link
- check
constant/polyMesh/boundary
solver: simpleFoam
Error
Attempt to cast type patch to type symmetryPlane
- This happens because one (or more) of your boundary patches is
defined as type
symmetryPlane; in0/Uor0/p(or other field files), but in the mesh fileconstant/polyMesh/boundaryits actual type is justpatch. - Root Cause (Very Common with gmshToFoam)
- In your earlier
gmshToFoamoutput you had patches: frontandback, top, inlet, bottom, outlet, wall. - You probably have frontandback (or similar) intended as symmetry planes (common for 3D cylinder or wedge simulations to reduce the domain).
- However,
gmshToFoamcreates all unnamed or leftover faces as typepatch;. - When you (or the tutorial) set type
symmetryPlane; in the boundary conditions, it mismatches the mesh definition.
- In your earlier
1 | |
- Please run these and share the output:
1 | |
1 | |
- Edit
constant/polyMesh/boundaryand change typepatch; to typesymmetryPlane; for the symmetry patches (usually frontandback).
1 | |
- Do not change
inlet,outlet,wall,top,bottom— those should stay aspatchorwall. - Make boundary conditions consistent in the
0/folder In0/Uand0/p, set the same patches tosymmetryPlane:
1 | |
then,
1 | |
Cannot find patchField entry for frontandback
frontandbackisemptyfor 2D case.
Patch type for patch wall must be wall
- You are using a wall function for nut (turbulent viscosity) on the patch named "wall" (very common with kEpsilon model), but in the mesh file constant/polyMesh/boundary, this patch is still defined as:
1 | |
OpenFOAM requires the patch type to be wallwhen you use any wall function(nutkWallFunction, nutUWallFunction,kqRWallFunction, etc.).
- Open this file:
constant/polyMesh/boundary
- Find the wall entry and change it from
patchtowall:
1 | |
- Do not change the other patches unless needed:
inlet,outlet,top,bottom→ keep aspatchfrontandback→ should besymmetryPlane(from previous fix)
- Make sure your boundary conditions are correct
- In
0/nut(this is usually where the error is triggered):- ``` wall { type nutkWallFunction; // or nutUWallFunction value
uniform 0; } #### low-Reynolds simulation
1
2
3
4
5
6
7- In `0/k` and `0/epsilon` on the wall patch you usually have:
- ```
wall
{
type kqRWallFunction; // or fixedValue + low Re
value uniform 0;
}
- ``` wall { type nutkWallFunction; // or nutUWallFunction value
uniform 0; }
- In
Alternative (if you prefer not to use wall functions) If you want a low-Reynolds simulation instead (no wall functions):
- Change wall type back to patch in boundary file, or
- In
0/nutuse:wall { type calculated; value uniform 0; }
- Use
lowResettings for k and epsilon (fixedValue with small values near wall).
- In
But for most industrial cylinder/pipe flows, wall functions(nutkWallFunction) + type wall; is the standard and recommendedway.