09_keyword
Important
Visit https://aerosand.cc for the latest updates.
0. Preface
Let us pause and carefully consider the project from the previous section. It is difficult to claim that it reads parameters by keyword because the code merely implements mechanical reading. If we change the order of keywords in the input file, it will lead to inconsistent parameter assignment (readers can try changing the order of keywords in the ofspProperties file, re-run the code, and observe the results).
Let us consider how to truly implement reading by keyword.
This section primarily discusses:
- Understanding how OpenFOAM reads parameters
- Implementing keyword-based parameter reading using C++
- Compiling and running a keyword project
1. OpenFOAM Parameter Reading
Tip
OpenFOAM’s parameter reading approach has undergone significant changes with architecture updates. We will use code from an earlier version as an example to facilitate reader understanding.
Let us examine what the statements for reading simple types of parameters look like in OpenFOAM.
In the laplacianFoam/createFields.H file, we can see the following statements:
| |
Although the syntax of lookup() is somewhat outdated in OpenFOAM, it is relatively intuitive.
To summarize briefly:
IOdictionaryis a class related to file readingIOdictionaryconstructs the objecttransportPropertiesbased on the external filetransportProperties- The object
transportPropertieshas methods of theIOdictionaryclass, such aslookup() - That is, the object
transportPropertiescan use thelookup()method
We will attempt to mimic this ourselves and implement OpenFOAM’s reading functionality.
2. Implementing Keyword-Based Parameter Reading
2.1. Project Preparation
Run the following commands in the terminal to create this project:
ofsp
foamNewApp ofsp_09_keyword
code ofsp_09_keywordCreate new files for this project. The final project structure is as follows:
.
├── IOdictionary
│ ├── IOdictionary.C
│ ├── IOdictionary.H
│ └── Make
│ ├── files
│ └── options
├── Make
│ ├── files
│ └── options
├── ofsp_09_keyword.C
└── ofspProperties2.2. Self-Developed IOdictionary
We envision that our self-developed IOdictionary class can also have a syntax similar to OpenFOAM. A simplified form is as follows:
// Desired syntax
IOdictionary ofspProperties // Construct an instance of the class
(
"ofspProperties" // Name of the external file, root directory path
);
double viscosity = ofspProperties.lookup("nu"); // The class instance can use methods
Here, the object ofspProperties is an instantiation of the class IOdictionary based on the file name (path) ofspProperties. The object ofspProperties can use the method lookup() defined in the class IOdictionary.
To implement the functionality for the self-developed class IOdictionary to read external files, we will still use the C++ native fstream class.
The class declaration in IOdictionary.H is as follows:
| |
Tip
The code style is intentionally aligned with that of OpenFOAM.
The class definition in IOdictionary.C is as follows:
| |
We also need to define the library Make.
The content of IOdictionary/Make/files is as follows:
IOdictionary.C
LIB = $(FOAM_USER_LIBBIN)/libIOdictionarySince no other libraries are used, the file IOdictionary/Make/options can be left empty.
Run the following commands in the terminal to compile and generate the dynamic library:
wclean IOdictionary
wmake IOdictionary2.3. Main Project
The main source code in ofsp_09_keyword.C is as follows:
| |
We also need to define the project Make.
The content of ofsp_09_keyword/Make/files is as follows:
ofsp_01_IO_keyword.C
EXE = $(FOAM_USER_APPBIN)/ofsp_01_IO_keywordThe content of ofsp_09_keyword/Make/options is as follows:
EXE_INC = \
-IIOdictionary/lnInclude
EXE_LIBS = \
-L$(FOAM_USER_LIBBIN) \
-lIOdictionary2.5. Compilation and Execution
Provide a file similar to an OpenFOAM dictionary in the root directory of the project. The content of ofspProperties is as follows (located at /ofsp/ofsp_09_keyword/ofspProperties):
nu 0.01
application laplacianFoam
deltaT 0.005
writeInterval 20
purgeWrite 0Run the following commands in the terminal to compile and run the project:
wclean
wmake
ofsp_09_keywordThe terminal output is as follows:
nu 0.01
application laplacianFoam
deltaT 0.005
writeInterval 20
purgeWrite 0Even if we adjust the order of parameters in the external dictionary file, for example:
nu 0.01
application laplacianFoam
writeInterval 20
purgeWrite 0
deltaT 0.005Without recompiling the project, we can run it directly:
Run the following command in the terminal:
ofsp_09_keywordThe result remains the same.
3. Summary
From the above discussion, it can be seen that the approach to reconstructing the project is essentially correct. However, the parameters obtained are uniformly of type string, which still does not meet our final requirements; we need to further develop functional features.
This section has completed the following discussions:
- Understanding how OpenFOAM reads parameters
- Implementing keyword-based parameter reading using C++
- Compiling and running a keyword 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
