Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
bc
Only a few trivial cases have known analytic solution
Then, how can we check our solutions ?
.. what could be wrong?
-the code itself (not to be expected in OpenFoam)
-the mesh and/or the time step (most likely)
-the model used (e.g, turbulence model)
Given b and the b.c., nd u
MMS inverts the process to generate arbitrarily complex
solutions of our problem:
Generate u and then obtain (by derivation) b
Exemple: Conduction heat transfer equation
fvm::ddt(T) - fvm::laplacian(D, T) == B
T
t
D
2
T
x
2
+
2
T
y
2
+
2
T
z
2
= B
T
t
D
2
T = B
D: thermal diffusivity
OpenFoam representation:
We choose our domain to be [0:1]^3
To keep things simple, we choose T to be
only a function of x, and thermal diffusivity
to be 1. Example:
T=-4*x*(x-1)*(x-3/4)*(x-1/4)
but in general, it can be (has to be) function of x,y,z
Then, by analitic derivation:
B=8*(x - 1)*(x - 1/4) + 8*(x - 1)*(x - 3/4) +
8*(x - 1/4)*(x - 3/4) + 8*x*(x - 1) + 8*x*(x - 1/4) +
8*x*(x - 3/4)
For lazy people.. use Matlab : (generaB)
clear all
close all
syms x y z t T B
DT=1 % difusividad termica (demasiado elevada, sin sentido sico)
T=-4*x*(x-1)*(x-3/4)*(x-1/4)
% ecuacion que vamos a resolver en OpenFOAM:
% fvm::ddt(T) - fvm::laplacian(DT, T) == B
% por tanto, este debe ser el termino fuente ..
B = diff(T,t) - DT*(diff(T,x,2)+diff(T,y,2)+diff(T,z,2) )
% Si queremos examinar los valores numericos de T o B, podemos hacerlo
% creando funciones a partir de los resultados simbolicos
fB=@(x,y,z,t) eval(char(B));
fT=@(x,y,z,t) eval(char(T));
fB(0,0.5,0.5,0.5)
fT(0,0.5,0.5,0.5)
Another example, again with thermal diffusivity equal to 1
T=-4*x*(x-1)*(x-3/4)*(x-1/4)*(x-7/10)*(x-8/10)*x^2
B = diff(T,t) - DT*(diff(T,x,2)+diff(T,y,2)+diff(T,z,2) )
B=simplify(B)
this yields:
B=
(x*(11200*x^5 - 29400*x^4 + 28485*x^3 - 12355*x^2 + 2271*x - 126))/50
B=
(x*(11200*x^5 - 29400*x^4 + 28485*x^3 - 12355*x^2 + 2271*x - 126))/50
T=-4*x*(x-1)*(x-3/4)*(x-1/4)*(x-7/10)*(x-8/10)*x^2
Now, we will use openFOAM to solve the problem:
T
t
D
2
T
x
2
+
2
T
y
2
+
2
T
z
2
= B
where
B= (x*(11200*x^5 - 29400*x^4 + 28485*x^3 - 12355*x^2 + 2271*x - 126))/50
the domain is a 1x1x1 cube and:
T=0 in the x boundaries
dT/dn=0 in the y,z boundaries (to make it 1d)
Laplace equation,
can be solved with the available laplacianFoam, but to
introduce a RHS, we need to write a new solver
2
T = 0
A new solver in this case is surprisingly easy, it will only be
about 50 lines of C++ code !
very easy (with the help of a friend) :)
Copy laplacianFoam source code in a separate folder
In createFields.h, add the following:
Info<< "Reading field B\n" << endl;
volScalarField B
(
IOobject
(
"B",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);
In laplacianFoam.C change the following:
1-Assign a value for B to each domain cell, as a function
of x,yz, and t
2-Change the PDE equation to be solved
in the original laplacianFoam, the equation is:
solve
(
fvm::ddt(T) - fvm::laplacian(DT, T)
);
This is C++ !
while (simple.loop())
{
Info<< "Time = " << runTime.timeName() << " peace & love " << runTime.value() << nl << endl;
forAll(B,celli) {
vector posicio=mesh.C()[celli];
double x,y,z,t; // get time & coordinates of our cell
x=posicio[0];
y=posicio[1];
z=posicio[2];
t=runTime.value();
// assign B value to our cell
B[celli]=(x*(11200.*x*x*x*x*x - 29400.*x*x*x*x + 28485.*x*x*x - 12355.*x*x + 2271.*x - 126.))/50.;
}
while (simple.correctNonOrthogonal())
{
solve
(
fvm::ddt(T) - fvm::laplacian(DT, T) == B // new equation to be solved
);
}
#include "write.H"
Info<< "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
<< " ClockTime = " << runTime.elapsedClockTime() << " s"
<< nl << endl;
}
Before each time step, we calculate get the coordinates of our cell and
compute B(x,y,z,t)
Then, we include the scalar eld B at the RHS of the equation to be solved,
note the elegance of the notation !
In the Make folder, we change the name of the executable:
laplacianFoam.C
EXE = newlaplacianFoam
The new solver is compiled typing wmake, and then it has to
be copied to a folder in our path. The easier way to do so is:
wmake
cp newlaplacianFoam /folder/with/our/case
Then, once in the case folder, we will launch it as
./newlaplacianFoam
le to be compiled
name of the executable
We also need to create a denition for B eld in 0 folder
This is a bit nonsense as B will be assigned later, but it is the easier
solution
FoamFile
{
version 2.0;
format ascii;
class volScalarField;
object T;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [0 0 -1 1 0 0 0];
internalField uniform 0;
boundaryField
{
adiabatic
{
type zeroGradient;
}
cold
{
type xedValue;
value uniform 0;
}
}
K/s
T
t
D
2
T
x
2
+
2
T
y
2
+
2
T
z
2
= B
K/s K/m^2 K/s m^2/s
In constant/transportProperties, we specify thermal diffusivity=1
DT DT [ 0 2 -1 0 0 0 0 ] 1.0;
m^2/s
In constant/polyMesh/blockMeshDict, we specify:
blocks
(
hex (0 1 2 3 4 5 6 7) (10 10 10) simpleGrading (1 1 1)
);
In system/controlDict, we specify:
application laplacianFoam;
startFrom startTime;
startTime 0;
stopAt endTime;
endTime 1e6;
deltaT 1e6;
Steps to run a simulation (once the solver is ready):
1-With paper&pencil (or Matlab if you are as lazy as me)
-Generate T
-Obtain B
2-Insert B in OpenFoam and solve for T
-Edit solver to change B (take care with integers!!)
-Compile (wmake) and copy (cp) the executable
-Change constant/polyMesh/blockMeshDict (to test
different meshes)
-Change system/controlDict (if needed)
-Change system/sampleDict (if needed)
-Generate the new mesh: blockMesh
-Run the solver: ./newlaplacianFoam
We can see the results in paraview, but it will be better to use
the openFoam sample utility
specications for sample are in system/sampleDict
// Fields to sample:
elds
(
T B
);
sets
(
lineX1
{
type uniform;
axis x;
start (0.0 0.5 0.5);
end (1.0 0.5 0.5);
nPoints 100;
}
);
x
i
=
x
2
3!
3
u
x
3
x
i
+. . . (11.22)
Hence, for an equally spaced grid the centered dierence approximation converges
quadratically as x 0:
u
x
x
i
=
u
i+1
u
i1
2x
+O(x
2
) (11.23)
Note that like the forward and backward Euler dierence formula, the centered dif-
ference uses information at only two points but delivers twice the order of the other
two methods. This property will hold in general whenever the grid spacing is con-
stant and the computational stencil, i.e. the set of points used in approximating
the derivative, is symmetric.
11.3.2 Higher order approximation
The Taylor expansion provides a very useful tool for the derivation of higher or-
der approximation to derivatives of any order. There are several approaches to
achieve this. We will rst look at an expendient one before elaborating on the
more systematic one. In most of the following we will assume the grid spacing to
be constant as is usually the case in most applications.
Equation (11.22) provides us with the simplest way to derive a fourth order ap-
proximation. An important property of this centered formula is that its truncation
error contains only odd derivative terms:
u
i+1
u
i1
2x
=
u
x
+
x
2
3!
3
u
x
3
+
x
4
5!
5
u
x
5
+
x
6
7!
7
u
x
7
+. . . +
x
2m
(2m+ 1)!
(2m+1)
u
x
(2m+1)
+. . .
(11.24)
The above formula can be applied with x replace by 2x, and 3x respectively
to get:
u
i+2
u
i2
4x
=
u
x
+
(2x)
2
3!
3
u
x
3
+
(2x)
4
5!
5
u
x
5
+
(2x)
6
7!
7
u
x
7
+O(x
8
) (11.25)
u
i+3
u
i3
6x
=
u
x
+
(3x)
2
3!
3
u
x
3
+
(3x)
4
5!
5
u
x
5
+
(3x)
6
7!
7
u
x
7
+O(x
8
) (11.26)
It is now clear how to combine the dierent estimates to obtain a fourth order
approximation to the rst derivative. Multiplying equation (11.24) by 2
2
and
78CHAPTER 11. FINITE DIFFERENCE APPROXIMATIONOF DERIVATIVES
x
i1
x
i
x
i+1
Figure 11.1: Computational grid and example of backward, forward, and central
approximation to the derivative at point x
i
. The dash-dot line shows the centered
parabolic interpolation, while the dashed line show the backward (blue), forward
(red) and centered (magenta) linear interpolation to the function.
Intuitively, the approximation will improve, i.e. the error will be smaller, as x is
made smaller. The above is not the only approximation possible, two equally valid
approximations are:
backward Euler:
u
0
(x
i
)
u(x
i
) u(x
i
x)
x
=
u
i
u
i1
x
(11.3)
Centered Dierence
u
0
(x
i
)
u(x
i
+x) u(x
i
x)
2x
=
u
i+1
u
i1
2x
(11.4)
All these denitions are equivalent in the continuum but lead to dierent approx-
imations in the discrete case. The question becomes which one is better, and is
80CHAPTER 11. FINITE DIFFERENCE APPROXIMATIONOF DERIVATIVES
If the (n+1)-th derivative of the function u has minimum m and maximum M
over the interval [x
i
x] then we can write:
Z
x
x
i
Z
x
x
i
m( ds)
n+1
R
n+1
Z
x
x
i
Z
x
x
i
M( ds)
n+1
(11.12)
m
(x x
i
)
n+1
(n + 1)!
R
n+1
M
(x x
i
)
n+1
(n + 1)!
(11.13)
which shows that the remainder is bounded by the values of the derivative and the
distance of the point x to the expansion point x
i
raised to the power (n + 1). If
we further assume that u
(n+1)
is continuous then it must take all values between
m and M that is
R
n+1
= u
(n+1)
()
(x x
i
)
n+1
(n + 1)!
(11.14)
for some in the interval [x
i
x].
11.3.1 Taylor series and nite dierences
Taylor series have been widely used to study the behavior of numerical approxi-
mation to dierential equations. Let us investigate the forward Euler with Taylor
series. To do so, we expand the function u at x
i+1
about the point x
i
:
u(x
i
+x
i
) = u(x
i
) +x
i
u
x
x
i
+
x
2
i
2!
2
u
x
2
x
i
+
x
3
i
3!
3
u
x
3
x
i
+. . . (11.15)
The Taylor series can be rearranged to read as follows:
u(x
i
+x
i
) u(x
i
)
x
i
u
x
x
i
=
x
i
2!
2
u
x
2
x
i
+
x
2
i
3!
3
u
x
3
x
i
+. . .
| {z }
Truncation Error
(11.16)
where it is now clear that the forward Euler formula (11.2) corresponds to truncat-
ing the Taylor series after the second term. The right hand side of equation (11.16)
is the error committed in terminating the series and is referred to as the trun-
cation error. The tuncation error can be dened as the dierence between the
partial derivative and its nite dierence representation. For suciently smooth
functions, i.e. ones that possess continuous higher order derivatives, and su-
ciently small x
i
, the rst term in the series can be used to characterize the order
of magnitude of the error. The rst term in the truncation error is the product
of the second derivative evaluated at x
i
and the grid spacing x
i
: the former is a
property of the function itself while the latter is a numerical parameter which can
be changed. Thus, for nite
2
u
x
2
, the numerical approximation depends lineraly on
the parameter x
i
. If we were to half x
i
we ought to expect a linear decrease
First order
approximation
Second order
approximation
http://www.rsmas.miami.edu/personal/miskandarani/Courses/MSC321/lectniteDifference.pdf
82CHAPTER 11. FINITE DIFFERENCE APPROXIMATIONOF DERIVATIVES
Second the truncation error is T.E. O(x
i1
x
i
) and is second order, that is
if the grid spacing is decreased by 1/2, the T.E. error decreases by factor of 2
2
.
Thirdly, the previous point can be made clearer by focussing on the important case
where the grid spacing is constant: x
i1
= x
i
= x, the expression simplies
to:
u
i+1
u
i1
2x
u
x
x
i
=
x
2
3!
3
u
x
3
x
i
+. . . (11.22)
Hence, for an equally spaced grid the centered dierence approximation converges
quadratically as x 0:
u
x
x
i
=
u
i+1
u
i1
2x
+O(x
2
) (11.23)
Note that like the forward and backward Euler dierence formula, the centered dif-
ference uses information at only two points but delivers twice the order of the other
two methods. This property will hold in general whenever the grid spacing is con-
stant and the computational stencil, i.e. the set of points used in approximating
the derivative, is symmetric.
11.3.2 Higher order approximation
The Taylor expansion provides a very useful tool for the derivation of higher or-
der approximation to derivatives of any order. There are several approaches to
achieve this. We will rst look at an expendient one before elaborating on the
more systematic one. In most of the following we will assume the grid spacing to
be constant as is usually the case in most applications.
Equation (11.22) provides us with the simplest way to derive a fourth order ap-
proximation. An important property of this centered formula is that its truncation
error contains only odd derivative terms:
u
i+1
u
i1
2x
=
u
x
+
x
2
3!
3
u
x
3
+
x
4
5!
5
u
x
5
+
x
6
7!
7
u
x
7
+. . . +
x
2m
(2m+ 1)!
(2m+1)
u
x
(2m+1)
+. . .
(11.24)
The above formula can be applied with x replace by 2x, and 3x respectively
to get:
u
i+2
u
i2
4x
=
u
x
+
(2x)
2
3!
3
u
x
3
+
(2x)
4
5!
5
u
x
5
+
(2x)
6
7!
7
u
x
7
+O(x
8
) (11.25)
u
i+3
u
i3
6x
=
u
x
+
(3x)
2
3!
3
u
x
3
+
(3x)
4
5!
5
u
x
5
+
(3x)
6
7!
7
u
x
7
+O(x
8
) (11.26)
It is now clear how to combine the dierent estimates to obtain a fourth order
approximation to the rst derivative. Multiplying equation (11.24) by 2
2
and
Second order approximation: error decreases with !x
2
IF is
-Small enough so that the factor !x
2
can vanish the other terms
-But no too small, as the rst derivative is evaluated as !u/!x and in the
limit this becomes 0/0
Some models (eg, LES) have an explicit dependande on the mesh size:
Lim LES = DNS
!x !0
V&V in the real world ?
Full verication of CFD results, not no mention validation,
is too expensive to be carried out in all outine simulations.
However, USE AT LEAST A COUPLE OF MESHES and compare the
results
In CFD it is almost compulsory to compare at least one simulation and
one experiment; if the results agree reasonably well, simulations can be
applied to similar situations.
Sometimes, even if there is a signicant disagreement (say, a 20% error
in the drag coefcient), the results can show correctly the trends (e.g,
the increse/decrease in drag when some parameter is increased/
decreased)
To conclude
DO
Begin with a similar problem that has a known solution, look in
literature for experimental results; reproduce it and understand the
resuts
Use AT LEAST two or (better) three different meshes of different
densities to evaluate how close to the mesh independent solution are
you
Be specially careful with turbulence models !!
NEVER DO
Never launch a simulation with a model (ie, multiphase, compressible,
LES..) if you dont understand the equations and the physics behind .
Never be satised with a single solution. If you can rene the mesh,
generate a coarser mesh.
Be impressed by the beauty of possibly wrong solutions
REMEMBER:
One day CFD will be reliable and wind tunnels will be closed; until then,
take care !