Sie sind auf Seite 1von 84

Disclaimer

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.
Introductory OpenFOAM Course
University of Genoa, DICCA
Dipartimento di Ingegneria Civile, Chimica e Ambientale
From 17
th
to 21
th
February, 2014
Your Lecturer
Joel GUERRERO

joel.guerrero@unige.it





guerrero@wolfdynamics.com

Todays lecture
1. How to implement a new boundary condition
in OpenFOAM
2. How to implement my own application in
OpenFOAM
3. How to add temperature to icoFoam

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.
Todays lecture
1. How to implement a new boundary condition
in OpenFOAM
2. How to implement my own application in
OpenFOAM
3. How to add temperature to icoFoam

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.
How to implement a new boundary condition in OpenFOAM
Remember, all components in OpenFOAM are implemented in library
form for easy re-use. So, in order to implement a new boundary
condition, we should follow these basic steps:

The implementations of the boundary conditions are located in the
directory $FOAM_SRC/finiteVolume/fields/fvPatchFields/
To add a new boundary condition, start by finding one that does
almost what you want to do. Then, copy that boundary condition
implementation to $WM_PROJECT_USER_DIR and modify it according
to your needs.
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.
How to implement a new boundary condition in OpenFOAM
Remember, all components in OpenFOAM are implemented in library
form for easy re-use. So, in order to implement a new boundary
condition, we should follow these basic steps:

Rename all the copied .C and .H files, for instance you can change
their names to myFvPatchField.*. In the new files, search for all
text occurrences with originalFvPatchField (the original name of
the .C and .H files) and replace them with myFvPatchField.
Modify the boundary condition to suit your needs. After you finish
modifying it, compile it and use it as a dynamic library.
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.
Parabolic inlet boundary condition
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.
How to implement a new boundary condition in OpenFOAM
Parabolic inlet boundary condition
We will add a new parabolic inlet profile boundary condition.

In the directory $ptofc/programming/src/finiteVolume/fields/
fvPatchFields/derived/parabolicVelocity you will find the new
boundary condition. Notice that we kept the same directory structure as in
$FOAM_SRC.
From the terminal type:

cd $ptofc/programming/src/finiteVolume/fields/
fvPatchFields/derived/parabolicVelocity
wmake libso
Starting from OpenFOAM version 2.2.1, the argument libso is not
required anymore.
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.
How to implement a new boundary condition in OpenFOAM
Parabolic inlet boundary condition

If you did not get any error, the new boundary condition is ready to use. wmake
will compile the new boundary condition and will place the new library in the
directory $WM_PROJECT_USER_DIR /platforms/linux64GccDPOpt/lib
(environment variable $FOAM_USER_LIBBIN).

Remember to modify the files located in the Make directory, so they
reflect the location of the new library and its name.

By the way, take a look at the files.
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.
How to implement a new boundary condition in OpenFOAM
Parabolic inlet boundary condition
Let us now setup a new case using the new boundary condition. From the
terminal:
cd $ptofc/mycases1/elbow2d_1/elbow_parabolic_inlet_0

Before running the solver we need to:
Add the new boundary condition parabolicVelocity in 0/U.
velocity-inlet-5
{
type parabolicVelocity;
maxValue 2.0;
n (1 0 0);
y (0 1 0);
value (0 0 0); //dummy value
}
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.
How to implement a new boundary condition in OpenFOAM
Parabolic inlet boundary condition
Let us now setup a new case using the new boundary condition.
Before running the solver we need to:

Then add the line:
libs (parabolicvelocityBC.so)

to the case controlDict dictionary in the system directory.
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.
How to implement a new boundary condition in OpenFOAM
Parabolic inlet boundary condition

Remember to always add the newly created boundary condition to your control
dictionary controlDict. If you do not do this OpenFOAM will complain.

Now we are ready to run the case. From the terminal type:

fluentMeshToFoam ../mesh/ascii.msh
(The mesh is in the folder ../mesh)
checkMesh
icoFoam
paraFoam
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.
How to implement a new boundary condition in OpenFOAM
Parabolic inlet boundary condition
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Mesh
Parabolic inlet boundary condition
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Parabolic inlet profile Uniform inlet profile
Parabolic inlet boundary condition
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Inlet profiles
A closer look to the parabolic inlet boundary condition
The parabolicVelocityFvPatchVectorField boundary condition
consists of two files:
parabolicVelocityFvPatchVectorField.C
parabolicVelocityFvPatchVectorField.H
The .H file is the header file, and it is included in the header of the .C file.
We can see that in the .H file we create a sub class to the
fixedValueFvPatchVectorField:
class parabolicVelocityFvPatchVectorField: public fixedValueFvPatchVectorField
i.e. this is for Dirichlet boundary conditions for a vector field.
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.
How to implement a new boundary condition in OpenFOAM
A closer look to the parabolic inlet boundary condition
The class has private data and takes in the following data
//- Peak velocity magnitude
scalar maxValue_;
//- Flow direction
vector n_;
//- Direction of the y-coordinate
vector y_;

The TypeName("parabolicVelocity"), used when specifying the
boundary condition, is defined.
There are some public constructors and member functions that are defined
in detail in the .C file.
We used the third constructor when we tested the boundary condition, i.e.
we read the member data from a dictionary.
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.
How to implement a new boundary condition in OpenFOAM
A closer look to the parabolic inlet boundary condition
The actual implementation of the boundary condition can be found in the
updateCoeffs() member function:
boundBox bb(patch().patch().localPoints(), true);
vector ctr = 0.5*(bb.max() + bb.min());
const vectorField& c = patch().Cf();
scalarField coord = 2*((c - ctr) & y_)/((bb.max() - bb.min()) & y_);
vectorField::operator=(n_*maxValue_*(1.0 - sqr(coord)));



The member function write defines how to write out the boundary values in
the time directory. The final line, writeEntry("value", os); writes out
all the values, which is only needed for post-processing.
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.
How to implement a new boundary condition in OpenFOAM
A closer look to the parabolic inlet boundary condition
Find out more about all the variables by including the following lines at the end
of the updateCoeffs member function:


Info << c << c << endl;
Info << ctr << ctr << endl;
Info << y_ << y_ << endl;
Info << bb.max() << bb.max() << endl;
Info << bb.min() << bb.min() << endl;
Info << (c - ctr) << c - ctr << endl;
Info << ((c - ctr) & y_) << ((c - ctr) & y_) << endl;
Info << ((bb.max() - bb.min()) & y_) << ((bb.max() - bb.min()) & y_) << end;
Info << coord << coord << endl;
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.
How to implement a new boundary condition in OpenFOAM
Wait, what if I do not want to program my boundary
condition?
You can save the pain of programming the boundary conditions in
OpenFOAM if you use swak4foam.
swak4foam offers the possibility of implementing new boundary conditions
without the need of programming.
If you go to the directory $ptofc/mycases1/elbow2d_1/
elbow_parabolic_inlet_1, you will find a case were we use
swak4foam to implement a parabolic profile inlet.
Do not worry, I will show how to use swak4foam and how to run this case.
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.
How to implement a new boundary condition in OpenFOAM
Wait, what if I do not want to program my boundary
condition?
I highly advise you to spend some time and try to learn how to use
swak4foam as it will save you a lot time.
I have been able to program really complex boundary conditions using
swak4foam. It works most of the times.
For more information about swak4foam you can visit the following site
http://openfoamwiki.net/index.php/Contrib/swak4Foam.
Let us go to the directory $ptofc/mycases1/elbow2d_1/
elbow_parabolic_inlet_1, from now on follow me.
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.
How to implement a new boundary condition in OpenFOAM
Paraboloid inlet boundary condition
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.
How to implement a new boundary condition in OpenFOAM
Paraboloid inlet boundary condition
Starting from the parabolic inlet profile boundary condition, let us now create a
new boundary condition to model a paraboloid inlet profile.

In the directory $ptofc/programming/src/finiteVolume/fields/
fvPatchFields/derived/p3d you will find the new boundary condition.
Notice that we kept the same directory structure as in $FOAM_SRC.
Notice that we also kept the same naming convention for the files and
classes as for the parabolic inlet profile boundary condition, that is, we simply
cloned the directory. This is to show you how the code can be reused.
Starting from this point, we can modify the files to fit our needs.
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.
How to implement a new boundary condition in OpenFOAM
Paraboloid inlet boundary condition
Starting from the parabolic inlet profile boundary condition, let us now create a
new boundary condition to model a paraboloid inlet profile.

As we simply cloned the directory parabolicVelocity, the name of the
paraboloid inlet boundary condition is the same as the one used in the
parabolicVelocity boundary condition and the implementation consists
of two files:
parabolicVelocityFvPatchVectorField.C
parabolicVelocityFvPatchVectorField.H

By the way, take a look at the files and identify all the modifications.
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.
How to implement a new boundary condition in OpenFOAM
Paraboloid inlet boundary condition
Starting from the parabolic inlet profile boundary condition, let us now create a
new boundary condition to model a paraboloid inlet profile.

While this approach will work, I highly recommend you to change the names
of the files and classes. For instance, if you use the library
parabolicvelocityBC.so and p3d.so in the same case, you will have
a conflict between the libraries as both of them use the same symbols.
In the directory $ptofc/programming/src/finiteVolume/fields/
fvPatchFields/derived/paraboloid you will find the paraboloid
inlet boundary condition. For your convenience, in this implementation we
already replaced all instances of parabolicVelocity by paraboloid
(names of files and classes).
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.
How to implement a new boundary condition in OpenFOAM
Paraboloid inlet boundary condition
Starting from the parabolic inlet profile boundary condition, let us now create a
new boundary condition to model a paraboloid inlet profile.

By the way, take a look at the files in the directory $ptofc/programming/
src/finiteVolume/fields/fvPatchFields/derived/paraboloid
and identify all the modifications (in naming convention and implementation).

To compile:
cd $ptofc/programming/src/finiteVolume/fields/
fvPatchFields/derived/paraboloid
wmake libso
Starting from OpenFOAM version 2.2.1, the argument libso is not
required anymore
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.
How to implement a new boundary condition in OpenFOAM
Paraboloid inlet boundary condition

If you did not get any error, the new boundary condition is ready to use. wmake
will compile the new boundary condition and will place the new library in the
directory $WM_PROJECT_USER_DIR /platforms/linux64GccDPOpt/lib
(environment variable $FOAM_USER_LIBBIN).

Remember to modify the files located in the Make directory, so they
reflect the location of the new library and its name.

By the way, take a look at the files.
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.
How to implement a new boundary condition in OpenFOAM
Paraboloid inlet boundary condition
Let us now setup a new case using the new boundary condition. From the
terminal:
cd $ptofc/mycases1/elbow3d/elbow_paraboloid
Before running the solver we need to:
Add the new boundary condition parabolicVelocity in 0/U.
inlet1
{
type p3d;
maxValue 0.4;
n (1 0 0);
y (0 1 0);
y1 (0 0 1);
radius 0.25;
value uniform (0 0 0); //dummy value
}
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.
How to implement a new boundary condition in OpenFOAM
Paraboloid inlet boundary condition
Let us now setup a new case using the new boundary condition.
Before running the solver we need to:

Then add the line:
libs (paraboloid.so)

to the case controlDict dictionary in the system directory.
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.
How to implement a new boundary condition in OpenFOAM
Paraboloid inlet boundary condition

Remember to always add the newly created boundary condition to your control
dictionary controlDict. If you do not do this OpenFOAM will complain.

Now we are ready to run the case. From the terminal type:
blockMesh
surfaceFeatureExtract
snappyHexMesh -overwrite
checkMesh
icoFoam
paraFoam
Note: you will need to modify the boundary file in the directory constant/polyMesh. If you do not know how
to modify the file boundary, take a look at the README.FIRST file for instructions.
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.
How to implement a new boundary condition in OpenFOAM
Paraboloid inlet boundary condition
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Geometry
Paraboloid inlet boundary condition
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Mesh
Paraboloid inlet boundary condition
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Paraboloid profile
Paraboloid inlet boundary condition
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Velocity magnitude contours in a cut plane
Atmospheric boundary layer
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.
How to implement a new boundary condition in OpenFOAM
Atmospheric boundary layer

Starting from the parabolic inlet profile boundary condition, let us now created a new
boundary condition to model the atmospheric boundary layer.

In the directory $ptofc/programming/src/finiteVolume/fields/
fvPatchFields/derived/ablVelocity you will find the new boundary condition.
Take a look at the new boundary condition and identify the changes we have done.
Also, try to figure out what kind of law we used to model the atmospheric boundary layer
inlet profile. In the terminal type:

cd $ptofc/programming/src/finiteVolume/fields/fvPatchFields/
derived/ablVelocity
wmake libso
Starting from OpenFOAM version 2.2.1, the argument libso is not
required anymore
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.
How to implement a new boundary condition in OpenFOAM
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.
How to implement a new boundary condition in OpenFOAM
Atmospheric boundary layer

If you did not get any error, the new boundary condition is ready to use. wmake
will compile the new boundary condition and will place the new library in the
directory $WM_PROJECT_USER_DIR /platforms/linux64GccDPOpt/lib
(environment variable $FOAM_USER_LIBBIN).

Remember to modify the files located in the Make directory, so they
reflect the location of the new library and its name.

By the way, take a look at the files.
Atmospheric boundary layer
Let us now setup a new case using the new boundary condition. From the
terminal:
cd $ptofc/mycases3/building/c0

Before running the solver we need to:
Add the new boundary condition ablVelocity in 0/U.
inflow
{
type ablVelocity;
n (1 0 0);
y (0 1 0);
maxValue 12.3;
value (0 0 0); //dummy value
}
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.
How to implement a new boundary condition in OpenFOAM
Atmospheric boundary layer
Let us now setup a new case using the new boundary condition.
Before running the solver we need to:

Then add the line:
libs (ablvelocityBC)

to the case controlDict dictionary in the system directory.
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.
How to implement a new boundary condition in OpenFOAM
Atmospheric boundary layer

Remember to always add the newly created boundary condition to your control
dictionary controlDict. If you do not do this OpenFOAM will complain.

Now we are ready to run the case. From the terminal type:

fluent3DMeshToFoam ../mesh/ascii1.cas
(The mesh is in the directory ../mesh)
checkMesh
simpleFoam
paraFoam
By the way, this case is expensive so you better run in parallel.
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.
How to implement a new boundary condition in OpenFOAM
Atmospheric boundary layer

To run this case, you can use as initial conditions the solution obtained using
potentialFoam. From the terminal type:
cd $ptofc/mycases3/building/potential
rm ./0/*
cp 0org/U 0/U
cp 0org/p 0/p
fluent3DMeshToFoam ../mesh/ascii1.cas
(The mesh is in the directory ../mesh)
checkMesh
potentialFoam writep

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.
How to implement a new boundary condition in OpenFOAM
Atmospheric boundary layer

To run this case, you can use as initial conditions the solution obtained using
potentialFoam. From the terminal type:
cd $ptofc/mycases3/building/c0
rm ./0/*
cp ./0rg/* ./0
cp ../potential/0/U 0/
cp ../potential/0/p 0/
fluent3DMeshToFoam ../mesh/ascii1.cas
(The mesh is in the directory ../mesh)
checkMesh
simpleFoam

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.
How to implement a new boundary condition in OpenFOAM
Atmospheric boundary layer
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Mesh
Atmospheric boundary layer
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Pressure and velocity contours
Atmospheric boundary layer
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Pressure and velocity contours
Atmospheric boundary layer
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Pressure and velocity contours
Atmospheric boundary layer
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Velocity contours
Coarse mesh Fine mesh
Atmospheric boundary layer
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Inlet profile
Atmospheric boundary layer
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Unsteady solution in a fine mesh
Q-Criterion Velocity magnitude
Atmospheric boundary layer
By following the instructions, you should get something like this
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.
How to implement a new boundary condition in OpenFOAM
Energy spectrum
Todays lecture
1. How to implement a new boundary condition
in OpenFOAM
2. How to implement my own application in
OpenFOAM
3. How to add temperature to icoFoam

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.
How to implement my own application in OpenFOAM
Remember, all components in OpenFOAM are implemented in library
form for easy re-use. So, in order to implement a new application, we
should follow these basic steps:
The applications implementations are located in $WM_PROJECT_DIR/
applications
To add a new application, start by finding one that does almost what
you want to do. Then, copy that application implementation to
$WM_PROJECT_USER_DIR and modify it according to your needs.
You can also write a solver from scratch. Remember to keep the
prototype body of a solver or utility.
After you finish modifying or writing your own application, compile it
and use it as you normally do.
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.
Laplace equation solver

Let us write a solver for the Laplace equation:

In the directory $ptofc/programming/applications/solvers/
my_laplace/ you will find the new application.
From the terminal type:
cd $ptofc/programming/applications/solvers/my_laplace/
wmake

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.
How to implement my own application in OpenFOAM
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.
Laplace equation solver

If you did not get any error, the new boundary condition is ready to use. wmake
will compile the new boundary condition and will place the new library in the
directory $WM_PROJECT_USER_DIR /platforms/linux64GccDPOpt/bin
(environment variable $FOAM_USER_APPBIN).

Remember to modify the files located in the Make directory, so they
reflect the location of the new library and its name.

By the way, take a look at the files.
How to implement my own application in OpenFOAM
Laplace equation solver

So, what changes did we introduce in my_laplace directory:

We created volumeScalarField T in createFields.H
We created the dimensionedScalar DT in createFields.H
We added the Laplace equation in my_lapace.C
solve
(
fvm::ddt(T) - fvm::laplacian(DT, T)
);
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.
How to implement my own application in OpenFOAM
Laplace equation solver

So, what changes did we introduce in my_laplace directory:

Additionally we added in my_laplace.C the line:
#include "write.H

This line of code writes out volScalarField gradTx,
volScalarField gradTx and volScalarField gradTx (the T
gradient)
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.
How to implement my own application in OpenFOAM
Laplace equation solver

Let us now setup a new case using my_laplace. From the terminal:

cd $ptofc/mycases0/mylaplace_1
Before running the solver we need to:

Add the new field variable file T to 0 directory.
In fvSchemes, add the following line:
laplacian(DT,T) Gauss linearUpwind grad(T);
In fvSolution, add the solutions settings for T.

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.
How to implement my own application in OpenFOAM
Laplace equation solver

Remember to always add the new entries to the case dictionaries in the
directory system. Also, you will need to initialize all the fields you use in your
solver. If you do not do this OpenFOAM will complain.

Let us now setup a new case using my_laplace. From the terminal:

blockMesh
my_laplace
paraFoam

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.
How to implement my own application in OpenFOAM
Laplace equation solver
By following the instructions, you should get something like this
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.
Mesh (5 cells)
How to implement my own application in OpenFOAM
Laplace equation solver
By following the instructions, you should get something like this
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.
Comparison analytical solution versus numerical solution
How to implement my own application in OpenFOAM
100
120
140
160
180
200
220
240
260
0 0.005 0.01 0.015 0.02
Analytical solution
Numerical solution
100
120
140
160
180
200
220
240
260
0 0.005 0.01 0.015 0.02
Analytical solution
Numerical solution
5 cells
100 cells
Todays lecture
1. How to implement a new boundary condition
in OpenFOAM
2. How to implement my own application in
OpenFOAM
3. How to add temperature to icoFoam

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.
How to add temperature to icoFoam
Hereafter we will modify an existing solver (icoFoam).
The modification will consist in adding temperature to the solver.
We will need to copy the original solver into the directory
$WM_PROJECT_USER_DIR. Remember to use the exact directory structure.
After creating the new applications directory, various small changes are
necessary to introduce in the solver files.
Finally, the new field (temperature), needs to be added to the initial and
boundary conditions. Also, we need to tell to OpenFOAM how to discretize
the new terms, this is done in the fvSchemes dictionary. In the
fvSolution dictionary, we need to set the solver that we want to use to
solve the field temperature.
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.
In the folder $ptofc/programming/applications/solvers/
icoFoam/, you will find a copy of the original icoFoam solver. Let us copy
the content of this folder to the folder my_icoFoam. From the terminal:
cp -r $ptofc/programming/applications/solvers/
icoFoam/ $ptofc/programming/applications/solvers/
my_icoFoam/
cp -r $path_to_openfoamcourse/programming/applications/solvers/icoFoam/ $path_to_openfoamcourse/programming/applications/solvers/my_icoFoam/

Now rename the file icoFoam.C to my_icoFoam.C.
mv icoFoam.C my_icoFoam.C
Remember, the directory and the file must have the same name.
How to add temperature to icoFoam
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.
Now go to the Make sub-directory, and open the file files with your favorite
editor and add the following changes:
my_icoFoam.C
EXE = $(FOAM_USER_APPBIN)/my_icoFoam

No changes are necessary for the file options.
Now go back to the my_icoFoam directory and compile the application. In
the terminal,
cd ..

How to add temperature to icoFoam
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.
Before compiling the new application, make sure that you do not have old
binaries, old object files or old dependencies files in your application
directory. In the terminal:
wclean
This will clean the directory.

Now we are ready to compile the new application. In the terminal:
wmake
If everything went fine, your new solver binary should be in the
$FOAM_USER_APPBIN directory. Check this with:
ls $FOAM_USER_APPBIN
You should see the new solver my_icoFoam in this directory.
How to add temperature to icoFoam
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.
So far we only compiled the application with a new name.
Let us now add the necessary changes in order to solve the temperature
field.
How to add temperature to icoFoam
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.
First, we need to create the new temperature field. Open the
createFields.H file using your favorite editor.
The first thing we can notice in createFields.H is that OpenFOAM loads
the kinematic viscosity from the transportProperties dictionary file. Let
us add a new transport property related to the thermal diffusion which we will
denote as DT. Make the following changes:
dimensionedScalar nu
(
//Look for these lines
transportProperties.lookup("nu"));

//Starting from here add this line
dimensionedScalar DT
(
transportProperties.lookup("DT")
);
//Done adding new lines
How to add temperature to icoFoam
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.
If you keep reading the file, you will see that the scalar field p
(volScalarField) and the velocity field U (volVectorField) are
created. We need to add the new field for temperature T
(volScalarField). After the creation of the field U and before the line
#include createPhi.H, add the following lines:
Info<< "Reading field T\n" <<endl;
volScalarField T
(
IOobject
(
"T",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);
How to add temperature to icoFoam
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.
Save the modifications in createFields.H. At this point we have created
the new field T and the new transport property DT.
Let us now add the new equation to solve. Recalling that we want to add the
transport equation for the scalar T (temperature) to the current solver


Using OpenFOAM equation mimicking syntax, we can write this equation
as

fvScalarMatrix TEqn
(
fvm::ddt(T)
+ fvm::div(phi,T)
- fvm::laplacian(nu,T)
);
Teqn.solve( );
How to add temperature to icoFoam
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.
T
t
+ (T) (T) = 0
U -= rAU*fvc::grad(p);
U.correctBoundaryConditions( );
}

//Starting from here add these lines
fvScalarMatrix TEqn
(
fvm::ddt(T)
+ fvm::div(phi,T)
- fvm::laplacian(nu,T)
);
Teqn.solve( );
//End adding lines

runTime.write();
Now, we need to add the new equation to the file my_icoFoam.C. Using
your favorite editor add the following modifications.
Because the temperature transport depends on the velocity field, we will add
the equation after the momentum equation is solved (after the PISO loop),
but before the time step is written. Edit your file so it looks like this:
How to add temperature to icoFoam
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.
Save the modifications and compile the new solver. From the terminal
wclean
wmake
If everything went fine, your new solver binary should be in the
$FOAM_USER_APPBIN directory. Check this with:
ls $FOAM_USER_APPBIN
You should see the new solver my_icoFoam in this directory.

How to add temperature to icoFoam
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.
The next step is to test the new solver.

Go to the directory $ptofc/mycases1/elbow2d_1/
elbow_plus_temperature. In the terminal:
cd $ptofc/mycases1/elbow2d_1/elbow_plus_temperature
In this directory you will find the case setup files for a case using
my_icoFoam solver.

How to add temperature to icoFoam
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.
Notice that we need to create the field file for T in the 0 directory (initial and
boundary conditions).
We also need to add the new transport property to the
transportProperty dictionary in the constant directory.
We need to tell to OpenFOAM how to discretize the new equations. This
is done in the fvSchemes dictionary in the system directory.
Finally, you will need to tell to OpenFOAM how to solve the field T. This is
done in the fvSolution dictionary in the system directory.
Take a look at these files (T, transportProperties, fvSchemes and
fvSolution) and identify all the additions and changes made.
Always remember, OpenFOAM is fully dimensional so your
dimensions must be consistent.
How to add temperature to icoFoam
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.
We will now use the new solver. From now on follow me.
fluentMeshToFoam ../mesh/ascii.msh
(The mesh is in the folder ../mesh)
checkMesh
my_icoFoam
paraFoam
Remember to run the new solver in the directory
$ptofc/mycases1/elbow2d_1/elbow_plus_temperature

How to add temperature to icoFoam
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.
By following the instructions, you should get something like this
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.
Mesh
How to implement my own application in OpenFOAM
By following the instructions, you should get something like this
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.
Contours of velocity magnitude and temperature
How to implement my own application in OpenFOAM
By following the instructions, you should get something like this
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.
Contours of pressure
How to implement my own application in OpenFOAM
In the directory $ptofc/mycases1/elbow2d_1/, you will find various
setup for the same geometry. For instance you could use the solver
my_icoFoam with the parabolic inlet boundary condition we previously
created (elbow_parabolic_inlet_plus_temperature).
Try to run all cases and identify the changes in the dictionaries.
How to add temperature to icoFoam
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.

Remember, all components in OpenFOAM are implemented in library form for
easy re-use. So, in order to implement a new application, we should follow
these basic steps:

The applications implementations are located in $WM_PROJECT_DIR/
applications
To create a new application, start by finding one that does almost what you
want to do. Then, copy that application implementation to your user directory
$WM_PROJECT_USER_DIR and modify it according to your needs.
You can also write a solver from scratch. Remember to keep the prototype
body of a solver.
After you finish modifying or writing your application, compile it and use it as
you normally do.
How to add temperature to icoFoam
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.
At this point you should be able
to modify OpenFOAM solvers
and utilities, and create your own
applications and boundary
conditions.
How to add temperature to icoFoam
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.
Mesh generation using open source tools
Additional tutorials
In the folders $ptofc/programming/applications and
$ptofc/programming/src you will find many tutorials, try to go through each one to
understand how to program in OpenFOAM.

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.
Thank you for your attention
Hands-on session
In the courses directory ($ptofc) you will find many tutorials (which are different from
those that come with the OpenFOAM installation), let us try to go through each one to
understand and get functional using OpenFOAM.
If you have a case of your own, let me know and I will try to do my best to help you to
setup your case. But remember, the physics is yours.
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.

Das könnte Ihnen auch gefallen