15_mesh
Important
Visit https://aerosand.cc for the latest updates.
0. Preface
In addition to setRootCase.H and createTime.H, there is another essential header file that inevitably appears: createMesh.H.
This section primarily discusses:
- Understanding mesh-related classes
- Understanding the
createMesh.Hheader file - Practicing mesh-related methods
- Compiling and running a mesh project
1. Project Preparation
Run the following commands in the terminal to create the project:
ofsp
foamNewApp ofsp_15_mesh
cd ofsp_15_mesh
cp -r $FOAM_TUTORIALS/incompressible/icoFoam/cavity/cavity debug_case
code .Test the initial solver, and provide scripts and documentation.
2. mesh
Let us first not look at the source code of createMesh.H, but start from the needs of the application. That is, we need a mesh for computation based on a specific case. This mesh naturally has its own data (including points, faces, etc.) and its own methods (including returning point lists, returning face centers, etc.).
2.1. primitiveMesh
API page: https://api.openfoam.com/2506/classFoam_1_1primitiveMesh.html
OpenFOAM provides the primitiveMesh class based on geometric elements.
Run the following command in the terminal to view the declaration of the primitiveMesh class:
find $FOAM_SRC -iname primitiveMesh.HIt can be seen that the primitiveMesh class does not inherit from other classes.
Let us briefly explore the primitiveMesh class by looking at a few parts of the code.
| |
From this constructor, it can be seen that the primitiveMesh class constructs an object from the basic geometric elements: points, faces, and cells.
The geometric elements—points, faces, and cells—are generated by blockMesh or by third-party mesh software and then converted to OpenFOAM format. In any case, the points, faces, and cells are generated in the case/constant/polyMesh/ folder. The geometric data here are just data and do not have any methods.
Note that this class is a pure virtual class (abstract class) and cannot be instantiated directly.
2.2. polyMesh
Based on the pure virtual class primitiveMesh, OpenFOAM provides the derived class polyMesh. The polyMesh class is essentially still geometric and topological, but additionally provides some geometric and topological methods.
API page: https://api.openfoam.com/2506/classFoam_1_1polyMesh.html
Run the following command in the terminal to view the declaration of polyMesh:
find $FOAM_SRC -iname polyMesh.HLet us briefly explore the polyMesh class by looking at a few parts of the code.
| |
Using the polyMesh class, we can construct a mesh object and perform some mesh operations.
Geometric elements can be encapsulated for subsequent use.
OpenFOAM provides the IOobject class to encapsulate access to these data.
API page: https://api.openfoam.com/2506/classFoam_1_1IOobject.html
Run the following command in the terminal to find IOobject.H:
find $FOAM_SRC -iname IOobject.HLet us briefly explore the IOobject class by looking at a few parts of the code.
| |
Modify the main source code as follows:
| |
Some readers may notice that the construction of IOobject is based on runTime, rather than directly on constant/polyMesh. Recalling the search mechanism of the Time class discussed in the previous section, it can be understood that the Time class has a complex search mechanism.
Tip
This involves implementation details of the source code. At the current stage, it is not necessary or recommended to delve too deeply into it.
Compile and run.
The terminal output is as follows:
Create time
Max cell centre: (0.0975 0.0975 0.005)
Max cell volumes: 2.5e-07
Max cell face cetres: (0.1 0.1 0.01)
ExecutionTime = 0 s ClockTime = 0 s
End2.3. fvMesh
Based on the polyMesh class, OpenFOAM adds finite volume methods to derive the fvMesh class.
API page: https://api.openfoam.com/2506/classFoam_1_1fvMesh.html
Run the following command in the terminal to view the declaration of the fvMesh class:
find $FOAM_SRC -iname fvMesh.HLet us briefly explore the fvMesh class by looking at a few parts of the code.
| |
Using the fvMesh class, we can construct a mesh object and perform various operations, including mesh operations.
Modify the main source code as follows:
| |
Compile and run.
The terminal output is as follows:
Create time
Max cell centre: max(C) [0 1 0 0 0 0 0] (0.1 0.1 0.005)
Max cell volumes: max(V) [0 3 0 0 0 0 0] 2.5e-07
Max cell face cetres: max(Cf) [0 1 0 0 0 0 0] (0.1 0.1 0.005)
ExecutionTime = 0 s ClockTime = 0 s
EndReaders can also remove the max function and recompile to view the results. Based on the discussion of these three subclasses and base classes, we can imagine what the main code statements in createMesh.H are.
3. createMesh.H
API page: https://api.openfoam.com/2506/createMesh_8H.html
Run the following command in the terminal to view this header file:
find $FOAM_SRC -iname createMesh.HFor ease of understanding, let us examine the code from the OpenFOAM 2.0x version.
The GitHub repository file link is: https://github.com/OpenFOAM/OpenFOAM-2.0.x/blob/master/src/OpenFOAM/include/createMesh.H
The code content is as follows:
| |
It can be seen that this is the same as the code we wrote earlier.
The code in the modern version is as follows:
| |
Comparing the two versions, we can roughly understand the main content of this file and what mechanisms have been added in the modern version.
4. Mesh Information
We can integrate the above discussion and use mesh information in the project.
Modify the main source code as follows:
| |
To facilitate result display, modify the mesh resolution settings in the test case file debug_case/system/blockMeshDict:
| |
Compile and run.
The terminal output is as follows:
Create time
Create mesh for time = 0
Mesh directory: "polyMesh"
Number of points: 18
( 0.000000e+00, 0.000000e+00, 0.000000e+00)
( 5.000000e-02, 0.000000e+00, 0.000000e+00)
( 1.000000e-01, 0.000000e+00, 0.000000e+00)
Number of faces: 20
( 1 4 13 10)
( 3 12 13 4)
( 4 13 14 5)
( 4 7 16 13)
( 6 15 16 7)
( 7 16 17 8)
( 0 9 12 3)
( 3 12 15 6)
( 2 5 14 11)
( 5 8 17 14)
( 0 1 10 9)
( 1 2 11 10)
( 0 3 4 1)
( 3 6 7 4)
( 1 4 5 2)
( 4 7 8 5)
( 9 10 13 12)
( 12 13 16 15)
( 10 11 14 13)
( 13 14 17 16)
Number of face owner: 20
0
0
1
2
2
3
0
2
1
3
0
1
0
2
1
3
0
2
1
3
Number of face neighbour: 4
1
2
3
3
Number of boundary mesh: 3
Boundary name: movingWall Boundary type: wall
Boundary name: fixedWalls Boundary type: wall
Boundary name: frontAndBack Boundary type: empty
Bounding box: (0.000000e+00 0.000000e+00 0.000000e+00) (1.000000e-01 1.000000e-01 1.000000e-02)
Mesh volume: 1.000000e-04
ExecutionTime = 0.000000e+00 s ClockTime = 0.000000e+00 s
End5. Mesh Methods
Let us practice using more mesh methods (member methods).
Modify the main source code as follows:
| |
Compile and run.
The terminal output is as follows:
Create time
Create mesh for time = 0
Time : 0
Number of mesh cells : 4
Number of internal faces: 4
Cell 0 with center at (0.025 0.025 0.005)
Internal face 0 with center at (0.05 0.025 0.005) with owner cell 0 and neighbour cell 1
Patch 0 is movingWall with 2 faces. Start from face 4
Patch 1 is fixedWalls with 6 faces. Start from face 6
Patch 2 is frontAndBack with 0 faces. Start from face 12
Patch 0
its face 0 adjacent to cell 2
its normal vector (0 0.0005 0)
its surface area 0.0005
Patch 1
its face 0 adjacent to cell 0
its normal vector (-0.0005 0 0)
its surface area 0.0005
Patch 2
its face 0 adjacent to cell 0
its normal vector (0 0 -0.0025)
its surface area 0.0025
Internal face 0 with centre at (0.05 0.025 0.005) has 4 vertices: (0.05 0 0) (0.05 0.05 0) (0.05 0.05 0.01) (0.05 0 0.01)
Patch 2: frontAndBack is empty.
Retrived patch movingWall at index 0 using its name only.
ExecutionTime = 0.01 s ClockTime = 0 s
End6. Summary
Through this discussion, we have gained a brief understanding of mesh-related classes and what createMesh.H is.
At this point, the essential header files—setRootCase.H, createTime.H, and createMesh.H—have all been discussed.
This section has completed the following discussions:
- Understanding mesh-related classes
- Understanding the
createMesh.Hheader file - Practicing mesh-related methods
- Compiling and running a mesh project
Support us
Tip
Hopefully, the sharing here can be helpful to you.
If you find this content helpful, your comments or donations would be greatly appreciated. Your support helps ensure the ongoing updates, corrections, refinements, and improvements to this and future series, ultimately benefiting new readers as well.
The information and message provided during donation will be displayed as an acknowledgment of your support.
Copyright @ 2026 Aerosand
- Course (text, images, etc.):CC BY-NC-SA 4.0
- Code derived from OpenFOAM:GPL v3
- Other code:MIT License
