05_vector
Important
Visit https://aerosand.cc for the latest updates.
0. Preface
In the previous section, we briefly reviewed several common basic classes. This section attempts to discuss some details of the vector class to help readers become familiar with the development process, tool usage, and compilation principles.
This section primarily discusses:
- Practicing source code navigation
- Discussing parts of the implementation of the
vectorclass - Understanding the use of native libraries
- Compiling and running a
vectorproject
1. Vector Class
API page: https://api.openfoam.com/2506/classFoam_1_1Vector.html
Run the following command in the terminal to search locally:
find $FOAM_SRC -iname vectorRun the following command to open the class folder:
code $FOAM_SRC/OpenFOAM/primitives/VectorThe file structure of this class is as follows:
tree -L 1
.
├── bools/
├── complex/
├── floats/
├── ints/
├── lists/
├── Vector.H
└── VectorI.HReading the code description in Vector.H, we learn that this class is a template class for 3D Vectors. It inherits from the VectorSpace class and adds encapsulation for 3-component construction, component element interfaces, dot product, cross product, and other methods.
Specifically for the code in Vector.H, simple methods are implemented directly in the code, while complex methods extensively use inline functions to improve code performance. OpenFOAM specifically provides the VectorI.H file for implementing inline functions.
Enter the Vector/int folder and examine the code in Vector/ints/labelVector.H:
...
typedef Vector<label> labelVector;
...Enter the Vector/floats folder and examine the code in Vector/floats/vector.H:
| |
It can be seen that the folders representing different data types (/bools, /complex, /floats, /ints, /lists) are all typedef aliases (type aliases) of the Vector template class for different basic data types.
Warning
Do not delve into all the details of the code at this stage.
2. Source Code Discussion
2.1. Declaration
Let us examine the code in Vector/Vector.H section by section.
API page: https://api.openfoam.com/2506/Vector_8H_source.html
Tip
On the API code page, you can click to jump to different header files, classes, functions, etc.
| |
2.2. Inline
Many method definitions are in the inline file.
API page: https://api.openfoam.com/2506/VectorI_8H_source.html
Examine the code in Vector/VectorI.H:
| |
Warning
The above code discussion is primarily intended to help readers become familiar with the use of C++ in OpenFOAM, overcome any strangeness or fear of the C++ language, and facilitate understanding of the code in subsequent practical sections. There is no need to spend additional time reading more OpenFOAM source code at this stage, nor to delve into code details. This will be covered in the ofsc series later.
3. Project Setup
Run the following commands in the terminal to create the project for this section:
ofsp
mkdir ofsp_05_vector
code ofsp_05_vectorContinue using terminal commands or the VS Code interface to create additional files. The final file structure is as follows:
tree
.
├── Aerosand
│ ├── Aerosand.C
│ ├── Aerosand.H
│ └── Make
│ ├── files
│ └── options
├── Make
│ ├── files
│ └── options
└── ofsp_05_vector.C4. Development Library
4.1. Library Source Code
The code in Aerosand.H is:
| |
The code in Aerosand.C is:
| |
4.2. Library Make
The library Make/files is:
Aerosand.C
LIB = $(FOAM_USER_LIBBIN)/libAerosandThe development library has no other dependencies; the library Make/options is left empty.
4.3. Library Compilation
Run the following commands in the terminal to compile the library:
wclean Aerosand
wmake Aerosand5. Main Project
5.1. Main Source Code
The code in ofsp_05_vector.C is:
| |
5.2. Project Make
The project Make/files is:
ofsp_05_vector.C
EXE = $(FOAM_USER_APPBIN)/ofsp_05_vectorBecause we included vector.H, whose path is $FOAM_SRC/OpenFOAM/primitives/Vector/\frac{floats}{vector.H, we can determine from the Make file location that this file belongs to the OpenFOAM library at $FOAM_SRC/OpenFOAM. The affiliation is as follows:
$FOAM_SRC/OpenFOAM
├── lnInclude/
├── ...
├── Make
│ ├── files
│ └── options
└── primitives
├── ...
└── Vector
├── ...
├── floats
│ ├── ...
│ └── vector.H
├── Vector.H
└── VectorI.HIn principle, we should include the OpenFOAM library in the project Make/options. In practice, OpenFOAM’s build system (wmake) automatically handles the dependencies of essential built-in libraries. We only need to add third-party libraries, self-developed libraries, and libraries for certain optional modules.
Important
The $FOAM_SRC/OpenFOAM library is automatically included as a dependency; any classes used from it do not require the user to link again.
The project Make/options is:
EXE_INC = \
-IAerosand/lnInclude
EXE_LIBS = \
-L$(FOAM_USER_LIBBIN) \
-lAerosand6. Compilation and Execution
Run the following commands in the terminal to compile and run the project:
wclean
wmake
ofsp_05_vectorThe output is as follows:
3.14 * (1 2 3) = (3.14 6.28 9.42)
distance: 1.73205
normalise: (0.267261 0.534522 0.801784)
inner product: 5.34522
inner product: 5.34522
cross product: (-0.267261 0.534522 -0.267261)
cross product: (-0.267261 0.534522 -0.267261)
Hi, OpenFOAM! Here we are.
1 + 3.14159 = 4.14159
1 * 3.14159 = 3.14159
Current time step is : 0.27. Summary
This section has completed the following discussions:
- Practicing source code navigation
- Discussing parts of the implementation of the
vectorclass - Understanding the use of native libraries
- Compiling and running a
vectorproject
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
