08_io
Important
Visit https://aerosand.cc for the latest updates.
0. Preface
Through the eight discussions in the first phase, I believe readers have gained a certain understanding of OpenFOAM’s project architecture and compilation principles. Next, we will further discuss some fundamentals necessary for solver projects.
Tip
It is worth reiterating personal recommendations for C++ learning for beginners in OpenFOAM at this stage.
- What to do: First, learn the fundamentals of object-oriented C++, then continuously study C++ and gradually deepen your knowledge, accumulating C++ experience through ongoing practice.
- What not to do: There is no need to master advanced C++ features at this stage, nor to learn complex algorithms; do not wait to finish learning C++ before starting OpenFOAM.
This section primarily discusses:
- Understanding information input and output in OpenFOAM
- Implementing information input and output using C++
- Compiling and running an IO project
1. Reference OF Input/Output
We know that C++ can implement information input and output through I/O streams. Let us examine the data format of input and output in OpenFOAM.
Run the following command in the terminal to navigate to the run folder path:
runIf the terminal indicates that this folder is missing, run the following commands to create the run folder and navigate to it:
mkdir -p $WM_PROJECT_USER_DIR/run
runRun the following command to copy the native test case cavity to the run folder path:
cp -r $FOAM_TUTORIALS/incompressible/icoFoam/cavity/cavity .Run the following command to open the test case with VS Code and view its files:
code cavity1.1. Input Files
Run the following command in the VS Code terminal to view the file structure of the test case and its initial files:
treeThe terminal output is as follows:
.
├── 0
│ ├── p
│ └── U
├── constant
│ └── transportProperties
└── system
├── blockMeshDict
├── controlDict
├── decomposeParDict
├── fvSchemes
├── fvSolution
└── PDRblockMeshDictFile explanations are as follows:
- The folder
0/contains the initial field files for the test case (i.e., the time step at time 0)- For example, it includes the pressure field p and velocity field U for this test case
- Both include corresponding internal fields and boundary conditions
- The folder
constant/contains dictionaries related to geometry, physics, etc.- “Dictionary” here refers to OpenFOAM’s input file format, which can be simply understood as OpenFOAM’s input files
- For example, it includes the
transportPropertiesfile, which typically specifies physical parameters such as fluid viscosity and diffusion coefficients - Mesh information files generated later will be saved in this folder
- The folder
system/contains dictionaries for controlling the computation- For example, it includes the
blockMeshDictfile, which specifies parameters for mesh generation - For example, it includes the
controlDictfile, which specifies parameters such as time step, start and end times - For example, it includes the
fvSchemesfile, which specifies spatial and temporal discretization schemes - For example, it includes the
fvSolutionfile, which specifies solver selection, relaxation factors, etc. - Other dictionary files will not be explored at this stage
- For example, it includes the
Use VS Code to view the transportProperties dictionary:
| |
Use VS Code to view the controlDict dictionary:
| |
In simple terms, all files in the native test case (including mesh information generated later) are input files, and their information will be transferred into the OpenFOAM application.
1.2. Output Files
Let us quickly compute this test case.
Run the following commands in the terminal to generate the mesh and compute:
blockMesh
icoFoamRun the following command to view the file structure after computation:
tree -L 2
.
├── 0
│ ├── p
│ └── U
├── 0.1
│ ├── p
│ ├── phi
│ ├── U
│ └── uniform
...
├── 0.5
│ ├── p
│ ├── phi
│ ├── U
│ └── uniform
├── constant
│ ├── polyMesh
│ └── transportProperties
└── system
├── blockMeshDict
├── controlDict
├── decomposeParDict
├── fvSchemes
├── fvSolution
└── PDRblockMeshDictIt can be seen that after computation, results are stored as specified in the dictionary: 0.005 * 20 = 0.1 s, every 0.1 seconds until stopping at 0.5 seconds. The stored content is also determined by the solver (e.g., phi, etc.), which we will not delve into at this stage. Additionally, we can see the mesh information folder polyMesh saved after mesh generation.
Let us examine the data format of the velocity field results in the 0.5/ folder:
| |
Tip
The entire process is:
- Writing information from external files into the project
- Project execution (computation)
- Writing information from the project to external files
2. Implementing Input/Output Using C++
In the early stages of learning, we learned that C++ provides the iostream standard library, which includes cin and cout methods for reading information streams from standard input and writing information streams to standard output. Additionally, C++ provides the fstream standard library for interaction between external files and information streams.
ofstream: Output file stream, used to create files and write information to themifstream: Input file stream, used to read information from filesfstream: General file stream, capable of both input and output operations
We will use C++ to implement input and output.
2.1. Project Preparation
Run the following command in the terminal to create the project for this section:
ofspOpen this project using VS Code’s C/C++ Project Generator.
Run the following command in the terminal to test the project template:
make runThe project runs successfully, indicating that the initial project template is functioning correctly.
2.2. Main Source Code
Through this project, we simulate how a solver reads parameters from case dictionary files.
The main source code in ofsp_08_io/src/main.cpp is as follows:
| |
2.3. Documentation File
Since the current project is relatively simple, readers can prepare documentation files according to their own needs. This will not be elaborated further.
2.4. Compilation and Execution
Run the following command in the terminal to check the compilation:
makeThe terminal output is as follows:
g++ -std=c++17 -Wall -Wextra -g -Iinclude -c -MMD src/main.cpp -o src/main.o
src/main.cpp: In function ‘int main(int, char**)’:
src/main.cpp:6:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
6 | int main(int argc, char *argv[])
| ~~~~^~~~
src/main.cpp:6:26: warning: unused parameter ‘argv’ [-Wunused-parameter]
6 | int main(int argc, char *argv[])
| ~~~~~~^~~~~~
g++ -std=c++17 -Wall -Wextra -g -Iinclude -o output/main src/main.o -Llib
Executing all complete!The terminal output shows a warning warning: unused parameter ‘argc’. Since we indeed did not use argc, this can be ignored. The final message Executing all complete! indicates successful compilation with no issues.
Create a file similar to OpenFOAM dictionaries in the root directory of the project. The ofspProperties file content is as follows (located at /ofsp/ofsp_08_io/ofspProperties):
nu 0.01
application laplacianFoam
deltaT 0.005
writeInterval 20
purgeWrite 0Run the following command in the terminal to execute this project:
./output/mainAlternatively, clean and recompile, then run:
make clean
make runThe terminal output is as follows:
nu 0.01
application laplacianFoam
deltaT 0.005
writeInterval 20
purgeWrite 0We can see that the parameters following each keyword in the external file have been read into the program, meaning these parameters can participate in the project’s computation, and the project ultimately writes out the results.
The written file U is also located in the project root directory, with the following content:
| |
The format is similar to OpenFOAM’s velocity field output format.
3. Summary
Through this project, we can gain a simple understanding of how OpenFOAM solvers obtain parameter values specified by dictionary files. These parameters participate in the project’s computation. Subsequently, the project writes the computation results to external files. The written files essentially conform to OpenFOAM’s format requirements and meet the format requirements of post-processing software, serving as data files awaiting further analysis.
This section has completed the following discussions:
- Understanding information input and output in OpenFOAM
- Implementing information input and output using C++
- Compiling and running an IO 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
