Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

29
Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier

Transcript of Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Page 1: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical methods to solve diffusion equations

A tutorial for biologists

E. Grenier

Page 2: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Introduction

Page 3: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Introduction: what is a diffusion equation ?

A diffusion equation, or heat equation models for instance

• The evolution of the temperature in a room

• The evolution of the concentration of chemical components in a solution

• The repartition of a population of animals, each animal walking at random

• The evolution of the concentration of ionic species in extracellular space

It models the general behavior of particules or indiviuals which move at random

• Ions move at random in extracellular space through thermal agitation

• Animals move at random (and interact of course, but this interaction is not included in diffusion equation)

Page 4: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Introduction: what equation ?

To fix the ideas, let us denote by u(t,x) the concentration of ionic species in extracellular space, t being the time variable and x the space variable.

Diffusion of u is described by the following heat / diffusion equation

∂t u – νΔ u = 0

where

∂t u denotes the time derivative of u

Δ u is the Laplace operator applied on u:

in one space dimension: Δ u is the second derivative of u with respect to u: Δ u = ∂xx u

in two space dimensions: Δ u = ∂xx u + ∂yy u

in three space dimensions: Δ u = ∂xx u + ∂yy u + ∂zz

ν is the diffusivity coefficient.

Page 5: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Introduction: boundary conditions

This equation must be completed with an initial data (value of u at t = 0).

It must also be completed with boundary data, which describe what happens on the boundary. Two main classes of conditions

• Dirichlet condition: u = 0 on the boundary

the value of u is imposed on the boundary and equal 0. For instance:

- the temperature of the walls of the room is fixed and equals 0

- the ionic concentration vanishes on the boundary

• Neuman condition: ∂n u = 0 on the boundary where u is the derivative in the direction normal to the boundary. The flux of u vanishes on the boundary. For instance

- no heat enters the domain

- the boundary are impermeable to ions (no entry, no exit)

Page 6: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Introduction: boundary conditions

Other boundary conditions

• Non homogeneous Dirichlet condition: u = g(t,x) on the boundary, where g is a given function: the value of u is imposed on the boundary and equal 0. For instance:

- the temperature of the walls of the room is fixed and equals g(t,x)

- the ionic concentration equals g(t,x) on the boundary

• Non homogeneous Neuman condition: ∂n u = g(t,x) on the boundary where u is the derivative in the direction normal to the boundary. The flux of u is given and equals g on the boundary. For instance

- a heat flux g(t,x) enters the domain

- ions enter or exit the domain, with a given flux g(t,x)

Page 7: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Dimension 1

Page 8: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in one space dimension

Simulation in one space dimension is fast and easy. Let us describe how to do in details

1.1 Discretization

The first step is to discretize u:• Time discretization

we compute the solution u only at particular times t_n, multiples of the time step, denoted by Δt:

t_n = n Δt• Space discretization

we compute the solution u only at particular points x_n. For simplicity we will only describe the case where these points x_n are equidistributed. Let Δx be the spacial discretization parameter. To discretize a interval [a b] with N points, we take

x_j = a + j Δx where

Δx = (b – a) / NWe denote by u_j^n the approximation of the value of u at (t_n,x_j)

Page 9: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in one space dimension

1.2 Dirichlet conditions

(u_j^(n+1) – u_j^n) / Δt=

ν (u_(j+1)^(n+1) + u (j – 1)^(n+1) – 2 u_j^(n+1)) / Δ x^2for

0 < j < N,

completed with the discretized boundary conditions

u_0^n = 0u_N^n = 0.

It is a crucial point to put an « n+1 » on the third line and not an « n », which would lead to a completly unstable and impossible to use scheme.

Not that these three lines define the u_j^(n+1) as the solution of a system that must be solved. Let us now detail how to solve it and how to get the « n+1 » step as a function of the « n » step.

Page 10: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in one space dimension:Dirichlet boundary condition: resolution

The numerical scheme may be written as

A (u_j^(n+1)) = (u_j^n)

where A is a N-1 x N-1 array constructed as follows

A = Id - ν (Δt / Δx^2) B

where B is the array-2 1 0 0 …… 0 0 01 -2 1 0 …… 0 0 00 1 -2 1 …… 0 0 00 0 1 -2 …… 0 0 0 ……………………….0 0 0 0 …… 1 -2 10 0 0 0 …… 0 1 -2

Page 11: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in one space dimension:Dirichlet boundary condition: algorithm

The algorithm is then

• Initiate u_j^0 by u_0(x_j)

• Define u_j^n by recurrence

(u_j^(n+1)) = A^(-1) (u_j^n)

The inversion of the array A is very rapid since it has a lot of zeros.

The algorithm gives good approximate solutions provided

• Δt and Δx are small: the smaller Δt and Δx, the more precise the results, but the slower the computations !

• Δt / Δx must be sufficiently small, else oscillations appear (see examples below).

Page 12: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in one space dimension:Dirichlet boundary condition: Matlab program

Matlab is well adapted to program this algorithm: diffusion.m

%% Simulation of diffusion equation on [0 1]%

N = 400; % Number of pointsdt = 0.01; % Time stepTmax = 5; % Maximum time for simulationdx = 1/N; % Spacial stepnu = 0.1; % diffusivity

B = -2*diag(ones(N-1,1)) + diag(ones(N-2,1),1) + diag(ones(N-2,1),-1);A = eye(N-1) - nu*dt*B/(dx^2);

u = zeros(N-1,1); % Initialization

for J = N/4:N/2, % Initial data u(J) = 1;end

for t=dt:dt:Tmax, u = A\u; % Evaluation of inv(A) u plot([1:N-1]*dx + i*u'); % plot of u axis([0 1 0 1]); % Axis drawnow;end

Page 13: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in one space dimension:Dirichlet boundary condition: Matlab program

Same program, but with a movie as output: diffusionmovie.m

%% Simulation of diffusion equation on [0 1]%

N = 400; % Number of pointsdt = 0.01; % Time stepTmax = 2.5; % Maximum time for simulationdx = 1/N; % Spacial stepnu = 0.1; % diffusivityindex1 = 0; % index for the movie

B = -2*diag(ones(N-1,1)) + diag(ones(N-2,1),1) + diag(ones(N-2,1),-1);A = eye(N-1) - nu*dt*B/(dx^2);

u = zeros(N-1,1); % Initialization

for J = N/4:N/2, % Initial data u(J) = 1;end

for t=dt:dt:Tmax, index1 = index1 + 1; u = A\u; % Evaluation of inv(A) u plot([1:N-1]*dx + i*u'); % plot of u axis([0 1 0 1]); % Axis drawnow; MOVI(index1) = getframe; % creation of the movieendmovie2avi(MOVI,'diffusionmovie1.avi'); % creation of AVI file

Page 14: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in one space dimension:Dirichlet boundary condition: movie

Click to launch the movie

Page 15: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in one space dimension:Dirichlet boundary condition: time and space steps

Let us discute the importance of Δt and Δx.

• If Δt and Δx are too large, the u_j^n is a very poor approximation of the true solution.

• If they are too small, the gain on the approximation is marginal, whereas the computationnal cost and time increase and becomes much too large.

• Note that is some cases, large oscillations may appear. This means that Δt is too large

with respect to Δx.

Page 16: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in one space dimension: Neumann conditions

1.2 Neumann conditions

(u_j^(n+1) – u_j^n) / Δt

=

ν (u_(j+1)^(n+1) + u (j – 1)^(n+1) – 2 u_j^(n+1)) / Δ x^2

for

0 < j < N,

completed with the discretized boundary conditions

u_0^n = u_1^n

u_N^n = u_(N – 1)^n.

Page 17: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in one space dimension:Neumann boundary condition: resolution

The numerical scheme may be written as in Dirichlet condition

A (u_j^(n+1)) = (u_j^n)

where A is a N-1 x N-1 array constructed as follows

A = Id - ν (Δt / Δx^2) B

where B is now the array-1 1 0 0 …… 0 0 01 -2 1 0 …… 0 0 00 1 -2 1 …… 0 0 00 0 1 -2 …… 0 0 0 ……………………….0 0 0 0 …… 1 -2 10 0 0 0 …… 0 1 -1

Note that only two coefficients differ between Neumann and Dirichlet boundary conditions.

Page 18: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in one space dimension:Neumann boundary condition: Matlab program

Matlab is well adapted to program this algorithm: diffusionneuman.m

%% Simulation of diffusion equation on [0 1]%

N = 400; % Number of pointsdt = 0.01; % Time stepTmax = 5; % Maximum time for simulationdx = 1/N; % Spacial stepnu = 0.1; % diffusivity

B = -2*diag(ones(N-1,1)) + diag(ones(N-2,1),1) + diag(ones(N-2,1),-1);B(1,1) = -1;B(N-1,N-1) = -1;A = eye(N-1) - nu*dt*B/(dx^2);

u = zeros(N-1,1); % Initialization

for J = N/4:N/2, % Initial data u(J) = 1;end

for t=dt:dt:Tmax, u = A\u; % Evaluation of inv(A) u plot([1:N-1]*dx + i*u'); % plot of u axis([0 1 0 1]); % Axis drawnow;end

Page 19: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in one space dimension:Neumann boundary condition: Matlab program

Same program, but with a movie as output: diffusionneumanmovie.m

%% Simulation of diffusion equation on [0 1]%

N = 400; % Number of pointsdt = 0.01; % Time stepTmax = 2.5; % Maximum time for simulationdx = 1/N; % Spacial stepnu = 0.1; % diffusivityindex1 = 0; % index for the movie

B = -2*diag(ones(N-1,1)) + diag(ones(N-2,1),1) + diag(ones(N-2,1),-1);B(1,1) = -1: B(N-1,N-1) = -1;A = eye(N-1) - nu*dt*B/(dx^2);

u = zeros(N-1,1); % Initialization

for J = N/4:N/2, % Initial data u(J) = 1;end

for t=dt:dt:Tmax, index1 = index1 + 1; u = A\u; % Evaluation of inv(A) u plot([1:N-1]*dx + i*u'); % plot of u axis([0 1 0 1]); % Axis drawnow; MOVI(index1) = getframe; % creation of the movieendmovie2avi(MOVI,'diffusionmovie1.avi'); % creation of AVI file

Page 20: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in one space dimension:Neumann boundary condition: movie

Click to launch the movie

Page 21: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Dimension 2

Page 22: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in two space dimensions

Simulation in two dimensions is more technical and requires the use of specific programs like Matlab « pdetoolbox ».

1 Mesh

The discretization is more delicate in two dimensions that in one dimension, since the domain may have a complex geometry. The usual method is to split the computationnal domain into small triangles. This may be done automatically by programs which are able to mesh any given domain very quickly.

u is then approximated by its value on each triangle. The algorithm is very similar to the one dimensionnal one, except that the evolution of the value on a given triangle depends on the value of u on the neighbouring triangles (in one space dimension the evolution of the value at a point only depends on the value on the left and on the right).

This leads problems of numerotations of triangles, to problems of building of arrays (like the building of B) … which requires the use of specific programs like Matlab « pde » toolbox.

Page 23: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in two space dimensions

2 Splitting the domain in triangles

Let us explain how to define a mesh using Matlab pdetoolbox.

• Launch « pdetool »

• Define the boundary of the domain using drawing commands

• Mesh it

• Refine the mesh if necessary

The two figures show a coarse and a refined mesh.

Page 24: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in two space dimensions: building the mesh

More precisely

• Launch « matlab »

• Type « pdetool ». Caution: the pde toolbox is not in the basic version of Matlab and must be bought separatly.

• A new window appear. Part of the buttons deal with drawing the boundary .

• To mesh the domain, push the « triangle » button

• To refine the mesh push the triangle just on the right

The points then lie in the « p » variables, the edges in « e » and the triangles in « t ». You can save them using the « mesh » menu.

The next step is to specify the boundary condition: this is done in the « boundary » menu.

Then save the boundary parameters « g » and « b » in the same menu.

You are then ready to solve diffusion equation.

We refer to « Notes sur la toolbox EDP de matlab » for more precisions.

Page 25: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in two space dimensions: Dirichlet routine

3. Dirichlet boundary condition

U = 0*p(1,:); % U is a vector a length the number of points, initiated to 0U(100) = 1; % The initial data equals 1 on the triangle number 100, 0 elsewhere

pdemesh(p,e,t,U); % plot the initial datadrawnow; % draw and pausepause;

nu = 0.01; % diffusivitydt = 0.01; % time step

for I=1:100, F = ''; % clear F F = pdeintrp(p,t,U'); % technical: Interpolate from node data to triangle midpoint

data U = assempde(b,p,e,t, nu*dt,1,F)'; % solve the iterative algorithm pdemesh(p,e,t,U); % plot U drawnow; % drawend

Note that a function may defines on the nodes or on the middes of the triangles. Transfer of the data from nodes to middle of triangles is done by pdeintrp and pdeprtni.

Page 26: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in two space dimensions: Dirichlet routine

Click on the image to run

(vertical scale changes with time)

Page 27: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in two space dimensions: Dirichlet routine

Click on the image to run

(vertical scale changes with time)

4. Neumann boundary condition

Simply change the boundary type condition in the « boundary » menu.

Page 28: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Dimension 3

Page 29: Numerical methods to solve diffusion equations A tutorial for biologists E. Grenier.

Numerical simulation in three space dimensions

Simulation in two dimensions is very technical and requires … the help of specialists !

Why ?

Mainly because the domain must be split in tetrahedrs, and this can not be done automatically. The design of the splitting of the domain in elementary tetrahedrs may be very technical and lengthly, specially in complex anatomic cases like brain.

Moreover the computationnal cost is very high, and ofter requires precise algorithms / powerful computers.

It is therefore better to be help by specialists of numerical computations !