14_time
Important
Visit https://aerosand.cc for the latest updates.
0. Preface
This section briefly introduces the Time class in OpenFOAM, and based on this, discusses the fundamental header file createTime.H.
This section primarily discusses:
- Testing new scripts
- Understanding the
createTime.Hheader file - Understanding the Time class
- Understanding time-related member methods
- Compiling and running a time project
1. Project Preparation
Run the following commands in the terminal to create the project:
ofsp
foamNewApp ofsp_14_time
cd ofsp_14_time
cp -r $FOAM_TUTORIALS/incompressible/icoFoam/cavity/cavity debug_case
code .Run the following command in the terminal to test the initial solver:
wmake
ofsp_14_time -case debug_caseThe initial solver runs without issues and can be further developed. This step will not be repeated in the following sections.
1.1. Scripts and Documentation
Readers may have noticed that the scripts used in previous projects are not generic and require modifications each time, which is quite tedious.
We will gradually introduce more convenient script writing methods.
Run the following command in the terminal to create new scripts:
code caserun casecleanThe caserun script is responsible for running the test case after the application is successfully compiled. Its content is as follows:
| |
Here, $0 returns the absolute path of the current script, %xxx removes the xxx characters, and /* represents / and all characters after it. || exit 1 means that if the cd command fails (returns a non-zero value), the script exits with error code 1.
Tip
Do not worry about the script language for now; it is sufficient to know that the above script can be used. Deeper script language knowledge will be introduced gradually as the discussion progresses.
The caseclean script is responsible for cleaning up the test case and restoring it to its initial state. Its content is as follows:
| |
These two scripts automatically obtain the application path and application name, making them suitable for use in other applications discussed later. Simply copy and use them.
Run the following command in the terminal to make the scripts executable:
chmod +x caserun caseclean2. createTime.H
API page: https://api.openfoam.com/2506/createTime_8H.html
We can locate this file via the terminal:
find $FOAM_SRC -iname createTime.HUsing VS Code, we can directly click on the file path output in the terminal to open the file.
In VS Code, we can also use the OFextension to directly jump to and open the file; this will not be repeated in the following sections.
The code is as follows:
| |
3. Time Class
The Time class is a fundamental class in OpenFOAM, with many member data and member methods. Let us briefly explore the Time class by looking at a few parts of the code.
API page: https://api.openfoam.com/2506/Time_8H.html
Run the following command in the terminal to view the declaration of the Time class:
find $FOAM_SRC -iname Time.HOpen Time.H, which contains the following:
| |
Tip
To reiterate, at this stage, it is not advisable for readers to delve too deeply into the code. It is sufficient to have a general understanding without feeling unfamiliar or resistant.
4. Time Information
We can use some methods of the Time class to output and utilize time information.
Modify the main function as follows:
| |
Run the following commands in the terminal to compile and run this project:
wclean
wmake
./caseclean
./caserunThe terminal output is as follows:
Create time
Case name : "debug_case"
Root path : "/home/aerosand/01_project/ofsp/ofsp_14_time"
Path : "/home/aerosand/01_project/ofsp/ofsp_14_time/debug_case"
Time path : "/home/aerosand/01_project/ofsp/ofsp_14_time/debug_case/0"
Constant path : "/home/aerosand/01_project/ofsp/ofsp_14_time/debug_case/constant"
Constant : constant
System path : "/home/aerosand/01_project/ofsp/ofsp_14_time/debug_case/system"
Controdict :
{
application icoFoam;
startFrom startTime;
startTime 0;
stopAt endTime;
endTime 0.5;
deltaT 0.005;
writeControl timeStep;
writeInterval 20;
purgeWrite 0;
writeFormat ascii;
writePrecision 6;
writeCompression off;
timeFormat general;
timePrecision 6;
runTimeModifiable true;
}
Format : ascii
Version : 2.0
Start time : startTime [0 0 1 0 0 0 0] 0
End time : endTime [0 0 1 0 0 0 0] 0.5
Time step : deltaT [0 0 1 0 0 0 0] 0.005
# set end time = 10
# set deltaT = 1
Start time : startTime [0 0 1 0 0 0 0] 0
End time : endTime [0 0 1 0 0 0 0] 10
Time step : deltaT [0 0 1 0 0 0 0] 1
Time: 1
Time: 2
Time: 3
Time: 4
Time: 5
Time: 6
Time: 7
Time: 8
Time: 9
Time: 10
Execution time: 0 s
Clock time: 0 s
EndThrough this project, it can be seen that the runTime object can not only access time directories but also access folders such as system/, constant/, etc.
5. Summary
This section briefly discussed the Time class and explained the roles of the member methods of the Time class.
So far, we have generally understood the common header files setRootCase.H and createTime.H in OpenFOAM projects. The next section will discuss another essential header file.
This section has completed the following discussions:
- Testing new scripts
- Understanding the
createTime.Hheader file - Understanding the Time class
- Understanding time-related member methods
- Compiling and running a time 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
