CFD,  Education,  Free software,  OpenFOAM

P. 1. Dimensional units in OpenFOAM

One of the first OpenFOAM® features users encounter when they start learning how to set simulations up is that the code verifies the dimensional consistency of the variables involved in operations.

While this verification can be disabled, and is performed only when using dimensioned data types, it is a very convenient feature and helps preventing incorrect operations when programming new features.

Probably the simplest example of dimensioned type is the dimensionedScalar. The origin of the name is straightforward:

  • a scalar type in OpenFOAM is either a C++ float or a double, depending on the options with which the OpenFOAM source code has been compiled.
  • dimensioned indicates a dimensionSet is associated to the scalar

A dimensionSet is what defines the dimensions of the data type it is associated to, by specifying the exponents of the fundamental units of the unit system of choice.

Such fundamental units are:

  1. Mass
  2. Length
  3. Time
  4. Temperature
  5. Amount of substance 
  6. Electric current 
  7. Luminous intensity

Consequently, a dimensionedScalar used to store a velocity magnitude measured in m s-1 is represented as:

[0 1 -1 0 0 0 0]

Note that square brackets are used to represent a dimensionSet in input files. Consequently, a density ? will be specified as:

rho    rho [1 -3 0 0 0 0 0] 1000;

and a kinematic viscosity as:

nu    nu  [0 2 -1 0 0 0 0]  0.1;

as it can be seen in many of the tutorials for incompressible flows.

Using dimension sets adds some caveats when implementing new features. First, in the code a dimensionSet object is constructed simply specifying the tuple of exponents:

dimensionSet(0, 2, -1, 0, 0, 0, 0)

This syntax becomes particularly useful when defining dimensioned constants, which will be created as follow:

dimensionedScalar nu0("nu0", dimensionSet(0, 2, -1, 0, 0, 0, 0), 0.1);

Similarly, dimensionSet can be used to construct vectors, tensors and fields in the code. 

Finally, one aspect to be considered concerns working with fields. When operating with elements of a volScalarField, volVectorField and volTensorField directly by accessing the list element, verification of dimensional units is not performed. Consequently, if in the code the programmer works on a volScalarField as follows:

a[i] = ...;

the assignment will only involve the value of the element of index i, and no dimensional verification will be performed. If a dimensioned type appears on the right-hand side of the assignment operator, the .value() method will need to be invoked:

dimensionedScalar nu0("nu0", dimensionSet(0, 2, -1, 0, 0, 0, 0), 0.1);

a[i] = nu0.value()*...;

To conclude, the dimensionSet of a dimensioned type a can be retrieved using the .dimensions() method:

dimensionSet myDimensionSet(a.dimensions());

For more technical details about the dimensionSet object, see the official documentation here.

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM® and OpenCFD® trade marks. Alberto Passalacqua is not associated to OpenCFD Ltd.