0% found this document useful (0 votes)
19 views

highLevelProgramming-5

Uploaded by

Himani Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

highLevelProgramming-5

Uploaded by

Himani Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Solving PDEs with OpenFOAM

• The PDEs we wish to solve involve derivatives of tensor fields with


respect to time and space
• The PDEs must be discretized in time and space before we solve
them

• We will start by having a look at algebra of tensors in OpenFOAM


at a single point
• We will then have a look at how to generate tensor fields from
tensors
• Finally we will see how to discretize PDEs and how to set boundary
conditions using high-level coding in OpenFOAM

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 112


Basic tensor classes in OpenFOAM
• Pre-defined classes for tensors of rank 0-3, but may be extended indefinitely

Rank Common name Basic name Access function


0 Scalar scalar
1 Vector vector x(), y(), z()
2 Tensor tensor xx(), xy(), xz(), ...

Example:
 
11 12 13
A tensor T =  21 22 23  is defined line-by-line:
31 32 33
tensor T( 11, 12, 13, 21, 22, 23, 31, 32, 33);

Info << "Txz = " << T.xz() << endl;


Outputs to the screen:
Txz = 13

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 113


Algebraic tensor operations in OpenFOAM
• Tensor operations operate on the entire tensor entity instead of a
series of operations on its components
• The OpenFOAM syntax closely mimics the syntax used in written mathematics,
using descriptive functions or symbolic operators

Examples:

Operation Comment Mathematical Description


description in OpenFOAM
Addition a+b a+b
Outer product Rank a, b ≥ 1 ab a*b
Inner product Rank a, b ≥ 1 a · b a&b
Cross product Rank a, b = 1 a × b aˆb
Operations exclusive to tensors of rank 2
Transpose TT T.T()
Determinant detT det(T)
Operations exclusive to scalars
Positive (boolean) s≥0 pos(s)
Hyperbolic arc sine asinh s asinh(s)

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 114


Examples of the use of some tensor classes
• In $FOAM_APP/test we can find examples of the use of some classes.
• Tensor class examples:
run
cp -r $FOAM_APP/test .
cd test/tensor
wmake
tensorTest >& log
• Have a look inside tensorTest.C to see the high-level code.
• See also vector, symmTensorField, sphericalTensorField
and many other examples.

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 115


Dimensional units in OpenFOAM
• OpenFOAM checks the dimensional consistency
Declaration of a tensor with dimensions:
dimensionedTensor sigma
(
"sigma",
dimensionSet( 1, -1, -2, 0, 0, 0, 0),
tensor( 1e6, 0, 0, 0, 1e6, 0, 0, 0, 1e6)
);
The values of dimensionSet correspond to the powers of each SI unit:
No. Property Unit Symbol
1 Mass kilogram kg
2 Length metre m
3 Time second s
4 Temperature Kelvin K
5 Quantity moles mol
6 Current ampere A
7 Luminous intensity candela cd
 2

sigma then has the dimension kg/ms

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 116


Dimensional units in OpenFOAM
• Add the following to tensorTest.C:
Before main():
#include "dimensionedTensor.H"
Before return(0):

dimensionedTensor sigma
(
"sigma",
dimensionSet( 1, -1, -2, 0, 0, 0, 0),
tensor( 1e6, 0, 0, 0, 1e6, 0, 0, 0, 1e6)
);
Info<< "Sigma: " << sigma << endl;

• Compile, run again, and you will get:

Sigma: sigma [1 -1 -2 0 0 0 0] (1e+06 0 0 0 1e+06 0 0 0 1e+06)

You see that the object sigma that belongs to the dimensionedTensor class
contains both the name, the dimensions and values.

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 117


Dimensional units in OpenFOAM
• Try some member functions of the dimensionedTensor class:

Info<< "Sigma name: " << sigma.name() << endl;


Info<< "Sigma dimensions: " << sigma.dimensions() << endl;
Info<< "Sigma value: " << sigma.value() << endl;

• You now also get:

Sigma name: sigma


Sigma dimensions: [1 -1 -2 0 0 0 0]
Sigma value: (1e+06 0 0 0 1e+06 0 0 0 1e+06)

• Extract one of the values:


Info<< "Sigma yy value: " << sigma.value().yy() << endl;
Note here that the value() member function first converts the expression to a
tensor, which has a yy() member function. The dimensionedTensor class
does not have a yy() member function, so it is not possible to do sigma.yy().

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 118


Construction of a tensor field in OpenFOAM
• A tensor field is a list of tensors
• The use of typedef in OpenFOAM yields readable type definitions:
scalarField, vectorField, tensorField, symmTensorField, ...
• Algebraic operations can be performed between different fields,
and between a field and a single tensor, e.g. Field U, scalar 2.0:
U = 2.0 * U;
• Add the following to tensorTest:
Before main():
#include "tensorField.H"
Before return(0):

tensorField tf1(2, tensor::one);


Info<< "tf1: " << tf1 << endl;
tf1[0] = tensor(1, 2, 3, 4, 5, 6, 7, 8, 9);
Info<< "tf1: " << tf1 << endl;
Info<< "2.0*tf1: " << 2.0*tf1 << endl;

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 119


Discretization of a tensor field in OpenFOAM
• FVM (Finite Volume Method)
• No limitations on the number of faces bounding each cell
• No restriction on the alignment of each face
• The mesh class polyMesh can be used to construct a polyhedral
mesh using the minimum information required
• The fvMesh class extends the polyMesh class to include additional
data needed for the FV discretization (see test/mesh)
• The geometricField class relates a tensor field to an fvMesh (can
also be typedef volField, surfaceField, pointField)
• A geometricField inherits all the tensor algebra of its correspond-
ing field, has dimension checking, and can be subjected to specific
discretization procedures

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 120


Examine an fvMesh
• Let us examine an fvMesh:
run
rm -rf cavity
cp -r $FOAM_TUTORIALS/incompressible/icoFoam/cavity .
cd cavity
sed -i s/"20 20 1"/"2 2 1"/g constant/polyMesh/blockMeshDict
blockMesh
• Run the test/mesh/meshTest (first compile it!)
• C() gives the center of all cells and boundary faces.
V() gives the volume of all the cells.
Cf() gives the center of all the faces.
• Try also adding in meshTest.C, before return(0):
Info<< mesh.C().internalField()[1][1] << endl;
Info<< mesh.boundaryMesh()[0].name() << endl;

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 121


Examine a volScalarField
• Read a volScalarField that corresponds to the mesh. Add in meshTest.C,
before return(0):

volScalarField p
(
IOobject
(
"p",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);
Info<< p << endl;
Info<< p.boundaryField()[0] << endl;

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 122


Equation discretization in OpenFOAM
• Converts the PDEs into a set of linear algebraic equations, Ax=b, where x and b are volFields
(geometricFields). A is an fvMatrix, which is created by a discretization of a geometricField and
inherits the algebra of its corresponding field, and it supports many of the standard algebraic
matrix operations
• The fvm (Finite Volume Method) and fvc (Finite Volume Calculus) classes contain static func-
tions for the differential operators, and discretize any geometricField. fvm returns an fvMatrix,
and fvc returns a geometricField.
Examples:
Term description Mathematical expression fvm::/fvc:: functions
Laplacian ∇ · Γ∇φ laplacian(Gamma,phi)
Time derivative ∂φ/∂t ddt(phi)
∂ρφ/∂t ddt(rho, phi)
Convection ∇ · (ψ) div(psi, scheme)
∇ · (ψφ) div(psi, phi, word)
div(psi, phi)
Source ρφ Sp(rho, phi)
SuSp(rho, phi)
φ: vol<type>Field, ρ: scalar, volScalarField, ψ: surfaceScalarField

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 123


Example
The equation
~
∂ρU ~ − ∇ · µ∇U
+ ∇ · φU ~ = −∇p
∂t

has the OpenFOAM representation


solve
(
fvm::ddt(rho, U)
+ fvm::div(phi, U)
- fvm::laplacian(mu, U)
==
- fvc::grad(p)
)

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 124


Example: laplacianFoam, the source code
Solves ∂T /∂t − ∇ · k∇T = 0
#include "fvCFD.H" // Include the class definitions
int main(int argc, char *argv[])
{
# include "setRootCase.H" // Set the correct path
# include "createTime.H" // Create the time
# include "createMesh.H" // Create the mesh
# include "createFields.H" // Temperature field T and diffusivity DT
while (runTime.loop()) // Time loop
{
# include "readSIMPLEControls.H" // Read solution controls
for (int nonOrth=0; nonOrth$<$=nNonOrthCorr; nonOrth++)
{
solve( fvm::ddt(T) - fvm::laplacian(DT, T) ); // Solve eq.
}
# include "write.H" // Write out results at specified time instances}
}
return 0; // End with ’ok’ signal
}

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 125


Example: laplacianFoam, discretization and boundary conditions
Find this in $FOAM_TUTORIALS/basic/laplacianFoam/flange
Discretization:
dictionary fvSchemes, read from file:
ddtSchemes
{
default Euler;
}

laplacianSchemes
{
default none;
laplacian(DT,T) Gauss linear corrected;
}
Boundary conditions:
Part of class volScalarField object T, read from file:
boundaryField{
patch1{ type zeroGradient;}
patch2{ type fixedValue; value uniform 273;}}

Håkan Nilsson, Chalmers / Applied Mechanics / Fluid Dynamics 126

You might also like