06_tensor
Important
Visit https://aerosand.cc for the latest updates.
0. Preface
The previous section discussed some code details based on the Vector class. This section discusses the Tensor class.
This section primarily discusses:
- Discussing parts of the implementation of the
Tensorclass - Understanding the compilation and linking of multi-class complex libraries
- Compiling and running a
tensorproject
1. Tensor Class
API page: https://api.openfoam.com/2506/classFoam_1_1Tensor.html
Run the following command in the terminal to search locally:
find $FOAM_SRC -iname tensorRun the following command to open the class folder:
code $FOAM_SRC/OpenFOAM/primitives/TensorThe file structure of this class is as follows:
tree -L 1
.
├── floats
├── ints
├── lists
├── Tensor.H
└── TensorI.HExamine Tensor/Tensor.H to see the implementation details of this class. We will not go through it line by line here.
We can also use the API or the terminal to find and read related classes:
dimensionedTensortensorField
Warning
Do not delve into code details at this stage; a general understanding of the usage of member functions is sufficient.
2. OFextension Plugin
It is highly recommended to install the community plugin OFextension in VS Code.
2.1. Configuring the Plugin
- Click the gear icon in the lower-left corner of VS Code to open
Settings. - Search for
ofextensionin the search bar. - Set the correct OpenFOAM path in
Ofextension: OFpath. - Open the user’s development application with VS Code, press
F1, enterofInitto initialize the configuration.
2.2. Using the Plugin
During project development, for example, in the main source code of this section, when entering relevant objects, VS Code will automatically pop up available methods (member functions).
Moreover, in the main source code, you can select header files, classes, etc., right-click, and use Go to Definition or Go to Declaration to directly jump to the source code.
This plugin is highly recommended for its convenience. Be careful not to initialize it in the OpenFOAM source folder.
Warning
Sometimes the jumped source code may not point to the correct location; pay attention to distinguish.
3. Project Setup
Run the following commands in the terminal to create the project for this section:
ofsp
mkdir ofsp_06_tensor
code ofsp_06_tensorContinue using terminal commands or the VS Code interface to create additional files. The final file structure is as follows:
tree
.
├── Aerosand
│ ├── class1
│ │ ├── class1.C
│ │ └── class1.H
│ ├── class2
│ │ ├── class2.C
│ │ └── class2.H
│ └── Make
│ ├── files
│ └── options
├── Make
│ ├── files
│ └── options
└── ofsp_06_tensor.CNote that the file structure of the development library is slightly different from the previous section. We have already observed that under an OpenFOAM library, there are generally multiple sub-libraries/classes. A user’s development library may also consist of several classes, and the development library has its own Make file to manage multiple classes. For example, here the Aerosand library has three classes: class1, class2, and class3.
4. Development Library
4.1. class1
For the first class, we still use the previous code.
The code in class1.H is:
| |
The code in class1.C is:
| |
4.2. class2
For the second class, we attempt to create a new class through inheritance.
The code in class2.H is:
| |
The code in class2.C is:
| |
Tip
Note that scalar and vector used in the declaration and definition belong to the Foam namespace, so the namespace must be used.
4.3. class3
For the third class, we write some simple content.
The code in class3.H is:
| |
The code in class3.C is:
| |
4.4. Library Make
The library Make/files is:
class1/class1.C
class2/class2.C
class3/class3.C
LIB = $(FOAM_USER_LIBBIN)/libAerosandThis development library has no other dependencies; the library Make/options can be left empty.
4.5. 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_06_tensor.C is:
| |
5.2. Project Make
The project Make/files is:
ofsp_06_tensor.C
EXE = $(FOAM_USER_APPBIN)/ofsp_06_tensorThe project Make/options is:
EXE_INC = \
-IAerosand/lnInclude
EXE_LIBS = \
-L$(FOAM_USER_LIBBIN) \
-lAerosandSimilarly, the $FOAM_SRC/OpenFOAM library is automatically included as a dependency; any classes used from it do not require the user to link again.
6. Compilation and Execution
Run the following commands in the terminal to compile and run the project:
wclean
wmake
ofsp_06_tensorThe output is as follows:
3.14 * (1 2 3) = (3.14 6.28 9.42)
pos(s): 1
asinh(s): 1.86181
T: (11 12 13 21 22 23 31 32 33)
Txy: 12
T3: (2 4 6 5 7 9 8 10 12)
T4': (1.33333 1.33333 -0.333333 1.33333 1.83333 -0.333333 -0.333333 -0.333333 0.333333)
T4' * T4: (1 0 0 1.66533e-16 1 0 -5.55112e-17 0 1)
T4.x(): (3 -2 1)
T4.y(): (-2 2 0)
T4.z(): (1 0 4)
T4^T: (3 -2 1 -2 2 0 1 0 4)
det(T4): 6
sigma: sigma [1 -2 -2 0 0 0 0] (1e+06 0 0 0 1e+06 0 0 0 1e+06)
sigma name: sigma
sigma dimension: [1 -2 -2 0 0 0 0]
sigma value: (1e+06 0 0 0 1e+06 0 0 0 1e+06)
sigma yy value: 1e+06
tf: 2{(1 1 1 1 1 1 1 1 1)}
tf: 2((1 2 3 4 5 6 7 8 9) (1 2 3 1 2 3 1 2 3))
2.0 * tf2((2 4 6 8 10 12 14 16 18) (2 4 6 2 4 6 2 4 6))
Hi, OpenFOAM! Here we are.
1 + 3.14159 = 4.14159
1 * 3.14159 = 3.14159
Current time step is : 0.2
Sum of vector components: 12
This is class37. Summary
This section has completed the following discussions:
- Discussing parts of the implementation of the
Tensorclass - Understanding the compilation and linking of multi-class complex libraries
- Compiling and running a
tensorproject
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
