18_fieldCal
Important
Visit https://aerosand.cc for the latest updates.
0. Preface
Based on the previous discussions, we will practice a simple case of field computation.
This section primarily discusses:
- Adding simple physical fields for computation
- Managing physical fields in the OpenFOAM way
- Practicing development library usage
- Compiling and running a fieldCal project
1. Project Preparation
Run the following commands in the terminal to create the project:
ofsp
foamNewApp ofsp_18_fieldCal
cd ofsp_18_fieldCal
cp -r $FOAM_TUTORIALS/incompressible/icoFoam/cavity/cavity debug_case
code .Test the initial solver, and provide scripts (refer to previous scripts) and documentation.
2. Adding Physical Fields
The copied test case does not have a temperature field. We will add an initial temperature field to the case.
2.1. Temperature Field
Temperature, like pressure, is a scalar. We can obtain the temperature field file by copying the pressure field file and modifying it.
Run the following command in the terminal to prepare the temperature field file:
cp -r debug_case/0/p debug_case/0/T
code debug_case/0/TThe modified temperature field file is as follows:
| |
2.2. Main Source Code
Modify the main source code as follows:
| |
2.3. Compilation and Execution
Compile and run this project (ensure that the temperature field file is not cleared).
The terminal output is as follows:
Create time
Create mesh for time = 0
Reading transportProperties
Reading field p
Reading field T
Reading field U
Reading/calculating face flux field phi
Starting time loop
Time = 0.005
Time = 0.01
Time = 0.015
Time = 0.02
...
Time = 0.49
Time = 0.495
Time = 0.5
Calculation done.
ExecutionTime = 0.02 s ClockTime = 0 s
EndRun the following command in the terminal to visualize the results:
paraFoam -case debug_caseNote that new time step field files appear in the debug_case/ folder. Taking debug_case/0.3/ as an example, the computed result files contained are as follows:
debug_case/0.3
├── gradT
├── p
├── phi
├── T
├── U
├── uniform
│ ├── functionObjects
│ │ └── functionObjectProperties
│ └── time
├── zeroScalarField
└── zeroVectorFieldYou can view the computed numerical values in each field file.
Warning
When opening 0.3/phi, you can see that the values have not been updated, and other files besides p and U have not been updated either. This is because the computation formulas given during field creation only initialize the fields; they do not automatically update as time advances. Therefore, the required fields need to be computed in the time loop of the main source code.
3. Project Organization
In practical OpenFOAM applications, generally:
- Field inclusion should be placed in a separate file
createFields.H - OpenFOAM itself provides
createPhi.H - Custom methods should also be placed in separate files, such as
calculatePressure.H
Therefore, the file structure of this application after reorganization is:
tree -L 1
.
├── calculatePressure.H
├── caseclean
├── caserun
├── createFields.H
├── debug_case
├── Make
└── ofsp_18_fieldCal.C3.1. createFields.H
The field inclusion file createFields.H is as follows:
| |
3.2. calculatePressure.H
The custom method calculatePressure.H is as follows:
| |
3.3. Main Source Code
The main source code is reorganized as follows:
| |
3.4. Compilation and Execution
Compile and run. The results will be the same.
After reorganization:
- The main source code has a clear functional division
- Each function is easier to maintain
4. Development Library
When the implementation of a certain type of method can be retained as a specific functionality for future use, it can be fixed as a development library for use in various projects.
Refer to the previous discussion: https://aerosand.cc/docs/ofs/ofsp/03_wmake/
4.1. Development Library Code
We will create a development library calculateVelocityPressure and provide the corresponding files.
Run the following command in the terminal to create the development library:
foamNewApp calculateVelocityPressureAfter creating the development library, the project file structure is as follows:
.
├── calculateVelocityPressure
│ ├── calculateVelocityPressure.C
│ ├── calculateVelocityPressure.H
│ └── Make
│ ├── files
│ └── options
├── caseclean
├── caserun
├── createFields.H
├── debug_case
│ ├── 0.org
│ │ ├── p
│ │ ├── T
│ │ └── U
│ ├── constant/
│ └── system/
├── Make
│ ├── files
│ └── options
└── ofsp_18_fieldCal.CThe development library declaration calculateVelocityPressure.H is as follows:
| |
The development library definition calculateVelocityPressure.C is as follows:
| |
4.2. Development Library Make
The development library Make/files is as follows:
calculateVelocityPressure.C
LIB = $(FOAM_USER_LIBBIN)/libcalculateVelocityPressureThe development library Make/options is as follows:
EXE_INC = \
-I$(LIB_SRC)/finiteVolume/lnInclude \
-I$(LIB_SRC)/meshTools/lnInclude
EXE_LIBS = \
-lfiniteVolume \
-lmeshToolsWhen compiling this library, it does not require additional libraries beyond these basic ones, so the above options are sufficient.
4.3. Development Library Compilation
Run the following command in the terminal to compile the development library:
wmake calculateVelocityPressureThe terminal indicates that the development library has been successfully compiled and is ready for use.
4.4. Field Inclusion
Keep the createFields.H code unchanged.
4.5. Main Source Code
Modify the main source code ofsp_18_fieldCal.C as follows:
| |
4.6. Project Make
The project Make/files is as follows:
ofsp_18_fieldCal.C
EXE = $(FOAM_USER_APPBIN)/ofsp_18_fieldCalThe project Make/options is as follows:
EXE_INC = \
-I$(LIB_SRC)/finiteVolume/lnInclude \
-I$(LIB_SRC)/meshTools/lnInclude \
-IcalculateVelocityPressure/lnInclude
EXE_LIBS = \
-lfiniteVolume \
-lmeshTools \
-L$(FOAM_USER_LIBBIN) -lcalculateVelocityPressureNote that when using a development library, both “inclusion” and “linking” are required. Both the “path” and the “name” must be specified.
4.7. Compilation and Execution
Compile and run the project.
The terminal output is as follows:
Create time
Create mesh for time = 0
Reading transportProperties
Reading field p
Reading field T
Reading field U
Reading/calculating face flux field phi
This is a test
Starting time loop
Time = 0.005
Time = 0.01
Time = 0.015
Time = 0.02
...
Time = 0.49
Time = 0.495
Time = 0.5
ExecutionTime = 0.02 s ClockTime = 0 s
End4.8. Post-Processing
We can also visualize the computed results using ParaView.
Run the following command in the terminal to visualize the results:
paraFoam -case debug_case5. Summary
Reflecting on the previous discussions, we can now appreciate the core elements of numerical computation: time, mesh, and physical fields. With these tools, we can construct mathematical physical models into mathematical equations, thereby forming systems of linear algebraic equations for numerical solution. Do not rush; we are getting closer to solver programming.
This section has completed the following discussions:
- Adding simple physical fields for computation
- Managing physical fields in the OpenFOAM way
- Practicing development library usage
- Compiling and running a fieldCal 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
