Download 1 The heat equation

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
M2R ASEP 2014-2015 / UE 33 - TP1 Numerical methods
Boris Dintrans (IRAP/CNRS, [email protected])
This practical session is under Python and a little Python reminder is given in the final
section 3. You will need a text editor, as for example the gedit one. A report must be given
at the end of the session and will be noted. To begin the tutorial:
1. Open a web browser (e.g. Firefox) to download the Python scripts from the following
website (section Enseignement):
http://www.ast.obs-mip.fr/dintrans
2. From a terminal, launch a text editor in the background to open each Python file:
gedit &
1
The heat equation
We want to compute the 1D solution of the heat equation on a closed domain x = [0, L]:

∂ 2T
∂T


=
χ
,

2

∂t
∂x



(1)
T (0, t) = T (L, t) = 0,






 T (x, 0) = sin πx ,
L
where T is the temperature and χ the radiative diffusion. The exact solution is known at any
time t and is given by:
πx χπ 2 t
exp − 2 .
T (x, t) = sin
(2)
L
L
1.1
An explicit scheme
a) Fill in the “?” in the Python script chaleur explicite.py that computes the solution of
system (1) by using the 2nd order FTCS scheme with a variable CFL given by:
CFL =
χ∆t
,
(∆x)2
(3)
named Courant-Friedrichs-Lewy’s condition (see lecture notes). To start the stability study
of this scheme, you will use a cautious CFL value, that is, CFL = 0.25.
We take L = 1, N = 100 gridpoints, a radiative diffusion χ = 1 and a final time step
tend = 0.1. Every p iterations, with e.g. p = 500, the numerical solution is plotted and finally
compared to the theoretical one at the final time tend.
Programming tips: Python is optimized to quickly perform operations on matrices and vectors.
It is strongly advised to make operations directly to vectors, not the elements of these vectors.
Thus, it is much faster (and clear) to perform
v+w,
to add the two vectors v et w, than doing an explicit for-loop:
1
for i in range(len(v)):
u[i]=v[i]+w[i]
Similarly, it is better to perform the operation:
u[2:10]=v[1:9]
than
for i in range(1,10):
u[i+1]=v[i]
for shifting a vector on the right. In particular, for the work requested in this tutorial, only
the loop on the variable time will be needed and we will use the Python function roll() to
shift the vectors of +1 or −1 on gridpoints xi .
b) Check numerically that the CFL condition:
CFL =
1
χ∆t
,
≤
(∆x)2
2
(4)
is necessary and sufficient to ensure the stability of the scheme.
1.2
An implicit scheme
The previous explicit scheme has the disadvantage of requiring the use of timesteps even
smaller than the space discretization is precise. In order to overcome this constraint, one
solution is to use an implicit centered scheme.
Put all the above questions with an implicit centered scheme of Crank-Nicolson’s type (Python
file chaleur implicite.py to be completed). In particular, check the stability of the scheme
even when the CFL condition of explicit scheme is not verified.
Programming Tips: the above remarks are still valid. In addition, the implicit system leads
to a tridiagonal matrix. This feature should definitely be exploited because it drastically
saves computation time and memory. For the construction of the matrix, use the ones()
function while the resolution of the linear system will be done with the tridag() function for
fixed boundary conditions (i.e. u = 0 on edges). This solver is already coded in the Python
tridag.py script.
2
The advection equation
We now want to solve numerically the advection equation, always in 1-D, but with periodic
boundary conditions:

∂u
∂u


+V
= 0,



∂t
∂x



(5)
u(0, t) = u(L, t),






2π

 u(x, 0) = cos( x).
L
We choose to work on the same domain as before, that is, L = 1 and N = 100 gridpoints.
What is the exact solution of this problem with advection?
2
2.1
Three explicit schemes (file advection explicite.py)
a) Implement the explicit FTCS centered scheme for the advection equation. Check that it
is unconditionally unstable whatever is the value of the CFL = V × ∆t/∆x.
b) Implement the explicit Lax-Friedrichs and upwind schemes. Study their stability and convergence. Which of them is the most diffusive?
2.2
An implicit scheme (file advection implicite.py)
a) Implement an implicit centered scheme of Crank-Nicolson’s type. This time, the matrix is
not strictly tridiagonal but cyclic due to the periodic boundary conditions. We then solve the
linear system using the subroutine cyclic() which is already encoded in the Python script
tridag.py. Make sure that this implicit method is freed again (but not entirely...) of the
CFL constraint of explicit schemes.
3
Python reminder
Python can be used either interactively by typing the command line in a shell ipython, or
by running the code directly via python ./code.py. To get help of a Python command, put
under ipython and type command name?. For example, to get help on the function max():
max?
3.1
Miscellaneous commands
python toto.py : run the script toto.py
run toto.py : run the script toto.py while you are in an ipython shell
n % 5 : The %(modulo) operator yields the remainder from the division of the first argument
by the second
for i in range(0,10): : declaration of a loop on i from 0 to 9 (10 iterations)
int() : returns the integer part
exit() : to exit Python when you are in a shell ipython
3.2
Operations on vectors and matrices
linspace(x1,x2,n) : creating a column vector with uniformly distributed values
: : submatrix extraction operator or subvector
Example: u[1:10]=v[5:15] copying the vector (v5 , · · · , v15 ) in the vector (u1 , · · · , u10 )
u=zeros(n,float) : creating a real array of size n
u=zeros like(v) : u is defined as the array v
ones(n,val) : creating a vector of size n having everywhere the value val
roll(u,-1) : shifting the vector u of an index to the left.
Example: if u = [0, 1, 2], then roll(u, −1) = [1, 2, 0]
diag([], 0 ou +1 ou -1) : creating a diagonal matrix
Example: diag([1, 2, 3], 0) creates a matrix of order 3 with 1,2,3 on its main diagonal
tridag(A,B,C,RHS) : solution of a tridiagonal linear system where A is the lower diagonal,
−−−→
B the main diagonal, C the upper diagonal and RHS the right-hand side (a vector)
3
−−→
~ = −
cyclic(A,B,C,alpha,beta,RHS) : solution of the linear system MX
RHS where the
matrix M is tridiagonal and cyclic (α and β coefficients in the corners)
3.3
Graphics commands
plot(x,f) : plots the curve f (x)
plot(x,g) : overprints the curve g(x)
show() : displays the result on the screen
4