16_meshRatio
Important
Visit https://aerosand.cc for the latest updates.
0. Preface
Based on our previous discussion of meshes, we will attempt to develop an application that computes mesh parameters, allowing readers to practice and deepen their understanding of meshes and the OpenFOAM project workflow.
The code for this project is adapted from Tom Smith’s lecture notes, Programming with OpenFOAM (Tutorial at The 3rd UCL OpenFOAM Workshop).
This section primarily discusses:
- Practicing the use of mesh class methods
- Considering code execution efficiency
- Reviewing the use of custom dictionary files
- Compiling and running a meshRatio project
1. Project Preparation
Run the following commands in the terminal to create the project:
ofsp
foamNewApp ofsp_16_meshRatio
cd ofsp_16_meshRatio
cp -r $FOAM_TUTORIALS/incompressible/simpleFoam/pitzDaily debug_case
code .Test the initial solver, and provide scripts and documentation.
2. Mesh Volume Ratio
Modify the main source code as follows:
| |
Execute the caserun script, or directly generate the mesh for the test case and run the solver:
wmake
blockMesh -case debug_case
ofsp_16_meshRatio -case debug_caseThe terminal output is as follows:
Create time
Create mesh for time = 0
Number of cells = 12225
Maximum volume ratio = 1.49908
ExecutionTime = 0.11 s ClockTime = 0 s
End3. Improving Volume Ratio Calculation
Although the append method of the List class is convenient, it is very inefficient because each time data is added to the List, a new List of size n+1 is created, the old data is copied over, and the new data is appended. When dealing with large amounts of data, this process of creating and copying each time new data is added is highly inefficient.
We can consider that for a given mesh, the total number of volume ratios to be computed is fixed. We can declare a List of fixed size, allocate memory for it, and then simply add data to it.
The improved main source code is as follows:
| |
Run the following commands in the terminal to compile and run:
The terminal output is as follows:
Create time
Create mesh for time = 0
Number of cells = 12225
Maximum volume ratio = 1.49908
ExecutionTime = 0.05 s ClockTime = 0 s
EndIt can be clearly seen that the execution time has decreased from 0.13 s to 0.06 s, significantly improving efficiency.
4. Maximum Volume Ratio
If we only need to obtain the maximum volume ratio, we do not actually need to store all volume ratios; storing only the maximum value is sufficient.
The main source code is as follows:
| |
Compile and run the project.
The terminal output is as follows:
Create time
Create mesh for time = 0
Number of cells = 12225
Maximum volume ratio = 1.49908
ExecutionTime = 0.05 s ClockTime = 0 s
End5. Volume Ratio Criterion
Simply obtaining the volume ratio is not sufficient; we may also want to know how many cells in the test case exceed a specified volume ratio criterion.
We will introduce the volume ratio criterion parameter via an OpenFOAM dictionary.
Provide a dictionary for the test case at /ofsp_16_meshRatio/debug_case/system/volumeRatioDict with the following content:
| |
Users can modify the parameters via the dictionary file without recompiling the project.
Tip
We adopt the following conventions:
Propertiesfiles are placed in the/userApp/constantfolderDictfiles are placed in the/userApp/systemfolder
The main source code is as follows:
| |
Compile and run the project.
The terminal output is as follows:
Create time
Create mesh for time = 0
Number of cells = 12225
Maximum volume ratio = 1.49908
Number of cell volume ratios exceeding 1.2 = 532
ExecutionTime = 0.04 s ClockTime = 0 s
End6. Summary
This section continued the discussion of mesh class methods and provided practical examples. Through repeated practice, readers can review previous knowledge points, overcome any unfamiliarity with OpenFOAM programming, and facilitate subsequent learning.
This section has completed the following discussions:
- Practicing the use of mesh class methods
- Considering code execution efficiency
- Reviewing the use of custom dictionary files
- Compiling and running a meshRatio 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
