04_basicClass
Important
Visit https://aerosand.cc for the latest updates.
0. Preface
We have already explored native C++ implementations, Make-based implementations, and OpenFOAM’s wmake implementation. Before officially delving into OpenFOAM development, it is necessary to gain a basic understanding of common classes in OpenFOAM to facilitate their use.
We can refer to the official API at https://api.openfoam.com/2506/ for detailed information.
Tip
Changing the version number at the end of the URL allows access to the API for the corresponding version.
This section primarily discusses:
- Navigating the OpenFOAM API
- Understanding some common basic classes
1. Switch
You can search for the keyword Switch directly through the API website to access the Switch Class Reference page, which provides relevant information about the Switch class, including the corresponding Source files: Switch.H and Switch.C.
API page: https://api.openfoam.com/2506/Switch_8H.html
You can also locate and read the source code via the terminal.
Run the following command to find the source location:
find $FOAM_SRC -iname switch.HThe terminal output for the corresponding version is as follows:
/usr/lib/openfoam/openfoam2406/src/OpenFOAM/primitives/bools/Switch/Switch.H
/usr/lib/openfoam/openfoam2406/src/OpenFOAM/lnInclude/Switch.HBased on the discussion in the previous section, we can copy the correct path and read the source code locally using VS Code:
code /usr/lib/openfoam/openfoam2406/src/OpenFOAM/primitives/bools/Switch/Switch.HTip
Use Ctrl + Shift + C and Ctrl + Shift + V for copy-paste operations in the terminal.
In any case, part of the Switch.H source code is as follows (click the code block title to jump to the source code):
| |
According to the Description, Switch is a wrapper around the bool type, serving similar judgment functionality as bool.
We will not delve deeper into the detailed code definitions at this point.
2. label
Similarly, the source code for the label class can be found online or locally.
API page: https://api.openfoam.com/2506/label_8H.html
/*
...
Description
A label is an int32_t or int64_t as specified by the pre-processor macro
WM_LABEL_SIZE.
A readLabel function is defined so that label can be constructed from
Istream.
...
*/Based on the source code description, the label type is either int32_t or int64_t, depending on the definition of the preprocessor macro WM_LABEL_SIZE.
Simply put, label can be understood as a wrapper around the int type.
3. scalar
API page: https://api.openfoam.com/2506/scalar_8H.html
/*
...
Description
A floating-point number identical to float or double depending on
whether WM_SP, WM_SPDP or WM_DP is defined.
...
*/According to the source code description, the scalar type can be understood as a wrapper around floating-point types, equivalent to float or double depending on the usage context.
4. vector
API page: https://api.openfoam.com/2506/vector_8H.html
/*
...
Description
A Vector of values with scalar precision,
where scalar is float/double depending on the compilation flags.
...
*/Thus, the vector type is a vector composed of scalar values. This class also defines mathematical operations for vectors.
Question
What about vectors composed of other data types?
5. tensor
API page: https://api.openfoam.com/2506/tensor_8H.html
/*
...
Description
Tensor of scalars, i.e. Tensor<scalar>.
Analytical functions for the computation of complex eigenvalues and
complex eigenvectors from a given tensor.
...
*/Thus, the tensor type is a tensor composed of scalar values. This class also defines mathematical operations for tensors.
Question
What about tensors composed of other data types?
6. dimensionedScalar/Vector/Tensor
Taking dimensionedScalar as an example, the API page is: https://api.openfoam.com/2506/dimensionedScalar_8H_source.html
/*
...
Description
Dimensioned scalar obtained from generic dimensioned type.
...
*/These are scalars, vectors, and tensors that incorporate OpenFOAM’s unit system and corresponding computational methods.
7. scalar/vector/tensorField
Taking scalarField as an example, the API page is: https://api.openfoam.com/2506/scalarField_8H.html
These are essentially list values (i.e., field data storage) of scalar/vector/tensor types, with corresponding computational methods.
8. dimensionSet
API page: https://api.openfoam.com/2506/dimensionSet_8H.html
/*
...
Description
Dimension set for the base types, which can be used to implement
rigorous dimension checking for algebraic manipulation.
The dimensions are specified in the following order
(SI units for reference only):
\table
Property | SI Description | SI unit
MASS | kilogram | \c kg
LENGTH | metre | \c m
TIME | second | \c s
TEMPERATURE | Kelvin | \c K
MOLES | mole | \c mol
CURRENT | Ampere | \c A
LUMINOUS_INTENSITY | Candela | \c cd
\endtable
...
*/This class defines the unit system for basic types, enabling rigorous dimensional checking during algebraic computations.
Through the API page, you can see the files directly or indirectly included by this class. The source code provides many practical unit combinations:
| |
These predefined unit combinations will frequently appear in future programming practice.
9. tmp
API page: https://api.openfoam.com/2506/tmp_8H.html
/*
...
Description
A class for managing temporary objects.
This is a combination of std::shared_ptr (with intrusive ref-counting)
and a shared_ptr without ref-counting and null deleter.
This allows the tmp to double as a pointer management and an indirect
pointer to externally allocated objects.
In contrast to std::shared_ptr, only a limited number of tmp items
will ever share a pointer.
...
*/This class is a smart pointer template class implemented by OpenFOAM, commonly used for automatically managing the lifecycle of temporary objects. Its primary purpose is to optimize performance by avoiding unnecessary copying of large data (e.g., physical fields with massive data volumes).
10. IOobject
API page: https://api.openfoam.com/2506/IOobject_8H.html
In simple terms, this class provides a complete description and interface for objects that need to be read from or written to disk (as files) and memory (as objects).
In CFD simulations, thousands of variables (fields, boundary conditions, model parameters, etc.) need to be read from dictionary files or written to disk as results. This class addresses input/output issues by encapsulating all these miscellaneous tasks into a unified class.
We will continue to use this class in practice in the following sections.
11. Summary
Warning
For now, there is no need to delve deeply into the programming implementation details of these class definitions, as such details are not the focus of this series. Overly deep exploration can cause significant confusion for beginners and disrupt the learning pace.
This section has completed the following discussions:
- Navigating the OpenFOAM API
- Understanding some common basic classes
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
