Finite Differerence Intro

Embed Size (px)

Citation preview

4D-Adamello Numerical Modelling shortcourseETH Zurich

Introduction to the Finite Difference Method Finite difference basicsThe basics of the nite difference method are best understood with an example. Consider the one-dimensional transient heat conduction equation c p T T = k t x x (1)

where is density, c p heat capacity, k thermal conductivity, T temperature, x distance and t time. If the thermal conductivity, density and heat capacity are constant over the model domain, the equation can be simplied to T 2 T = 2 t x (2)

k where = c p is the thermal diffusivity (a common value for rocks is = 106 m2 s1 ). We are interested in the temperature evolution versus time T (x,t) which satises equation 2, given an initial temperature distribution (Fig. 1A). An example would be the intrusion of a basaltic dike in cooler country rocks. How long does it take to cool the dike to a certain temperature? What is the maximum temperature that the country rock experiences? The rst step in the nite differences method is to construct a grid with points on which we are interested in solving the equation (this is called discretization, see Fig. 1B). The next step is to replace the continuous derivatives of equation 2 with their nite difference approximations. The derivative of temperature versus time T can be approximated with a forward nite difference approximation as t

T n+1 Tin T n+1 Tin T new Ticurrent T in+1 n = i = i t t t t t

(3)

here n represents the temperature at the current timestep whereas n + 1 represents the new (future) temperature. The subscript i refers to the location (Fig. 1B). Both n and i are integers; n varies from 1 to nt (total number of time steps) and i varies from 1 to nx (total number of grid points in x-direction). The spatial derivative of equation 2 is replaced by a central nite difference approximation, i.e., 2 T = 2 x x T x n Ti+1 Tin x

x

n Tin Ti1 x

=

n n Ti+1 2Tin + Ti1 x2

(4)

Substituting equation 4 and 3 into equation 2 givesn T n 2Tin + Ti1 Tin+1 Tin = i+1 t x2

(5)

The third and last step is a rearrangement of the discretized equation, so that all known quantities (i.e. temperature at time n) are on the right hand side and the unknown quantities on the left-hand side (properties at n + 1). This results in: n n Ti+1 2Tin + Ti1 Tin+1 = Tin + t (6) 2 x Because the temperature at the current timestep (n) is known, we can use equation 6 to compute the new temperature. The last step is to specify the initial and the boundary conditions. If for example the country rock has a temperature of 300 C and the dike a width of 2 meters, with a magma temperature of 1200 C, we can write as initial conditions: T (x < 1, x > 1,t = 0) = 300 T (1 x 1,t = 0) = 1200 (7) (8)

A

country rock

dike

B

boundary nodes

i,n+1

i-1,n

i,n

i+1,n

L

time

T(x,0)

i,n-1

Dt Dx

x

L

space

Figure 1: A) Setup of the model considered here. A hot basaltic dike intrudes cooler country rocks. Only variations in x-direction are considered; properties in the other directions are assumed to be constant. The initial temperature distribution T (x, 0) has a step-like perturbation. B) Finite difference discretization of the 1D heat equation. The nite difference method approximates the temperature at given grid points, with spacing x. The time-evolution is also computed at given times with timestep t. In addition we assume that the temperature far away from the dike center (at |L/2|) remains at a constant temperature. The boundary conditions are thus T (x = L/2,t) = 300 T (x = L/2,t) = 300 (9) (10)

So now you have a feeling about nite differences. The attached MATLAB code shows an example in which the grid is initialized, and a time loop is performed. In the exercise, you will ll in the questionmarks and obtain a working code that solves equation 2.

Exercises1. Open MATLAB and an editor and type the Matlab script in an empty le. Save the le under the name heat1Dexplicit.m. Fill in the question marks and run the le by typing heat1Dexplicit in the MATLAB command window (make sure youre in the correct directory). 2. Vary the parameters (e.g. use more gridpoints, a larger timestep). Note that if the timestep is increased beyond a certain value (what does this value depend on?), the numerical method becomes unstable. This is a major drawback of explicit nite difference codes such as the one presented here. In the next lesson we will learn methods that do not have these limitations. 3. Go through the rest of the handout and see how one derives nite difference approximations. 4. Record and plot the temperature evolution versus time at a distance of 5 meter from the dike/country rock contact. What is the maximum temperature the country rock experiences at this location and when is it reached? Assume that the country rock was composed of shales, and that those shales were transformed to hornfels above a temperature of 600 C. What is the width of the metamorphic aureole? 5. Bonus question: Derive a nite-difference approximation for variable k and variable x.

2

%heat1Dexplicit.m % % Solves the 1D heat equation with an explicit finite difference scheme clear %Physical parameters L = 100; Tmagma = 1200; Trock = 300; kappa = 1e-6; W = 5; day = 3600*24; dt = 1*day; % Numerical nx = nt = dx = x =

% % % % % % %

Length of modeled domain Temperature of magma Temperature of country rock Thermal diffusivity of rock Width of dike # seconds per day Timestep

[m] [C] [C] [m2/s] [m] [s]

parameters 201; % 500; % L/(nx-1); % -L/2:dx:L/2;%

Number of gridpoints in x-direction Number of timesteps to compute Spacing of grid Grid

% Setup initial temperature profile T = ones(size(x))*Trock; T(find(abs(x)