13_commandLine
Important
Visit https://aerosand.cc for the latest updates.
0. Preface
Newcomers may find it hard to ignore that solvers always include a few fixed header files, such as setRootCase.H, createTime.H, and createMesh.H. Code explanations found online often provide only a brief comment about their functionality, which may not fully resolve the confusion. This series will discuss them separately.
The previous section discussed command-line arguments in C++. This section will transition to discussing command-line arguments in OpenFOAM. Before that, let us first examine the setRootCase.H file.
This section primarily discusses:
- Understanding the
setRootCase.Hheader file - Understanding the
argListclass - Developing user-defined command-line arguments
- Compiling and running a commandLine project
1. Project Preparation
Run the following commands in the terminal to create the project:
ofsp
foamNewApp ofsp_13_commandLine
cd ofsp_13_commandLine
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_13_commandLine -case debug_caseThe initial solver runs without issues and can be further developed. This step will not be repeated in the following sections.
Run the following commands in the terminal to create scripts and documentation, and grant permissions to the scripts:
code caserun caseclean READEME.md
chmod +x caserun casecleanRefer to previous discussions for the scripts and documentation. Unless there are special circumstances, details of the scripts and documentation will not be repeated.
Run the following command to view the file structure:
tree
.
├── caseclean
├── caserun
├── debug_case
│ ├── 0
│ │ ├── p
│ │ └── U
│ ├── constant
│ │ └── transportProperties
│ └── system
│ ├── blockMeshDict
│ ├── controlDict
│ ├── decomposeParDict
│ ├── fvSchemes
│ ├── fvSolution
│ └── PDRblockMeshDict
├── Make
│ ├── files
│ └── options
├── ofsp_13_commandLine.C
└── README.md
6 directories, 15 filesUnless there are special circumstances, simple file structures will not be elaborated further.
2. setRootCase.H
API page: https://api.openfoam.com/2506/setRootCase_8H.html
We can locate this file via the terminal:
find $FOAM_SRC -iname setRootCase.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.
For 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/setRootCase.H
The code is as follows:
| |
The code in the modern version is as follows:
| |
Many solvers also use the enhanced setRootCaseLists.H, whose code is as follows:
// This is setRootCase, but with additional solver-related listing
#include "setRootCaseListOptions.H"
// Add a series of command-line options for querying and listing to the solver
#include "setRootCase.H" // Original version
#include "setRootCaseListOutput.H"
// When the program starts, check whether the user has used these options and perform the corresponding output operations (e.g., list information and exit the program)
This header file not only maintains the simplicity of the basic functionality but also provides flexible extensibility for solvers (such as post-processing tools) that need to use these advanced features.
For example, a solver with added command-line options can be used in the terminal as follows:
simpleFoam -listSwitchesThe terminal will display all debugging/optimization switches.
Readers are encouraged to explore the command-line options provided in the code on their own.
3. argList Class
The Foam::argList class is a fundamental class in OpenFOAM, with many member data and member methods. Let us briefly explore the argList class by looking at a few parts of the code.
API page: https://api.openfoam.com/2506/argList_8H.html
We can locate this file via the terminal:
find $FOAM_SRC -iname argList.HOpen the declaration of this class in argList.H, which contains the following:
| |
To see the implementation of the member function checkRootCase(), we need to look at the class definition in the same directory, i.e., the argList.C file. A portion of its content is as follows:
| |
At this point, readers should understand what directories setRootCase.H actually checks.
Tip
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.
More in-depth code discussions can be found in the ofsc (OpenFOAM Sharing Coding) series.
4. Help Information
We can use command-line arguments to implement certain functionalities.
Modify the main function as follows:
| |
Run the following commands in the terminal to compile and view the help information:
wmake
ofsp_13_commandLine -helpThe terminal output is as follows:
Usage: ofsp_13_commandLine [OPTIONS]
Options:
-case <dir> Case directory (instead of current directory)
-decomposeParDict <file>
Alternative decomposePar dictionary file
-parallel Run in parallel
-doc Display documentation in browser
-help Display short help and exit
-help-full Display full help and exit
Program for command line @ Aerosand
Solver options:
Calculation Accuracy - o1, o2, o3
Calculation Acceleration - 1, 2, 3
Using: OpenFOAM-2406 (2406) - visit www.openfoam.com
Build: _9bfe8264-20241212 (patch=241212)
Arch: LSB;label=32;scalar=64Custom help information can be seen here.
5. Mandatory Arguments
Mandatory arguments must be provided when running the project; otherwise, an error will be triggered.
Modify the main function as follows:
| |
Recompile this project.
Run the following command in the terminal to run the project:
ofsp_13_commandLine -case debug_case/ o2 2The terminal output is as follows:
Create time
Solver setup:
Accuracy : o2
Acceleration : 2
ExecutionTime = 0 s ClockTime = 0 s
EndAs can be imagined, these command-line arguments can be used to participate in the project’s computation.
6. Application Options
Application options can be provided or omitted when running the project.
Modify the main function as follows:
| |
Recompile this project.
Provide a custom dictionary for this project. Instead of using the default dictionary path set in the code, create a dictionary at /debug_case/constant/myDict.
The custom dictionary is as follows:
| |
Run the following command in the terminal to run the project:
ofsp_13_commandLine -case debug_case/ o2 2 -dict ./debug_case/constant/myDict -nPrecision 16 -debugThe terminal output is as follows:
Create time
Create mesh for time = 0
Solver setup:
Accuracy : o2
Acceleration : 2
Using custom dictionary: "./debug_case/constant/myDict"
Reading myDict
Dictionary from "./debug_case/constant/myDict"
mathLib : Eigen
tolerance : 1e-06
Setting nPrecision to 16
Debug mode enabled
ExecutionTime = 0 s ClockTime = 0 s
End7. Other Command-Line Features
The postProcess.H header file is also commonly found in solver header files. This header file is primarily used to integrate post-processing functionality into solvers or utility programs. Its core purpose is to allow users to compute and output additional derived fields (such as vorticity, stream function, gradients, etc.) during or after simulation execution, without the need to run separate post-processing tools.
For example, after the computation is complete, the following command can be entered in the terminal:
postProcess -func vorticityThis calculates the vorticity field based on the saved time steps without modifying the code.
Alternatively, the functions module can be added in controlDict, such as:
...
functions
{
vorticity
{
type vorticity;
libs (fieldFunctionObjects);
}
}This allows the solver to compute and output the vorticity field at every write time step.
Additionally, the addCheckCaseOptions.H header file is also commonly found in solver header files, with the following code:
Foam::argList::addDryRunOption
(
"Check case set-up only using a single time step"
);
Foam::argList::addBoolOption
(
"dry-run-write",
"Check case set-up and write only using a single time step"
);This header file adds additional standard command-line options to the solver, primarily used to check the completeness of case setup without running the full simulation.
For example, the following command can be entered in the terminal:
simpleFoam -case debug_case -dry-run
// Check case setup only, do not run the solver
simpleFoam -case debug_case -dry-run-write
// Check case setup and write the initial fieldsThese commands allow users to quickly identify setup errors, avoiding the situation where problems are only discovered halfway through a full run.
8. Summary
This section has step by step discussed the possible functionalities of command-line arguments in OpenFOAM. Although the project presented here is only a simple demonstration, I believe readers can, based on this discussion, understand command-line arguments in OpenFOAM and gain ideas for their own future development.
This section primarily discussed:
- Understanding the
setRootCase.Hheader file - Understanding the
argListclass - Developing user-defined command-line arguments
- Compiling and running a commandLine 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
