440 Geophysics: Heat flow with finite differences Thorsten Becker, University of Southern California, 03/2005 Some physical problems, such as heat flow, can be tricky or impossible to solve analytically in any but the simplest situations. This is why we use numerical methods to model system behavior. All numerical methods suffer from general limitations, such as finite accuracy (resolution) and stability (robustness against blowing up ). This means that you will have to proceed carefully when using computers to get answers for geophysical problems, always being aware of the simplifications and approximations. However, with (nowadays) basic computer tools, you can solve relevant problems. For example, you may estimate the temperature distribution around a dike intrusion as a function of time and space, and this distribution will be reflected in the geological record. One method used to solve certain kinds of partial differential equations numerically is by finite differences (FD). Consider the heat flow equation in one dimension: T t = κ 2 T x 2 + H ρc p. (1) where T is temperature, κ thermal diffusivity, H volumetric heat production rate, ρ density, and c p heat capacity. T dependence on space, x, and time t is implied, but κ is assumed constant for eq. (1). Should the conductivity, k, be a function of x instead, we would have had to write 1 ρc p x (k(x) x T ) instead of κ 2 T for the heat flow term, since κ = k x 2 ρc p. In any numerical scheme, one has to discretize the spatial domain, and go from a continuous medium to individual points (or elements for the other main numerical scheme, finite elements). Let s subdivide the model region of extent 0...L in x direction into n nodes so that the i-th location is x i = x 0 + (i 1), (3) with (2) = L/(n 1), (4) which gives a range for x from x i=1 = 0 (for x 0 = 0) to x i=n = L. A larger number of nodes n therefore gives you finer spatial resolution,. What FD does then is approximate the / t and / x (as in eq. (1)) by finite differences between nodes, as opposed to infinitesimal partial derivatives in a continuum. Say, you need / x at x i, then you could approximate it from forward differences using the slope from the function value f (x i ) at x i to the next node at x i+1 = x i + like x (x i) f = f (x i+1) f (x i ). (5) This will be useful when you don t have a x i 1 node, as on the left domain boundary. Alternatively, you could use
backward differences where x (x i) b x = f (x i) f (x i 1 ), (6) for instance at the right domain boundary. However, it turns out that in general the best approximation to / x error-wise is by central differences. Those are obtained by averaging f x and b x such that x (x i) c x = x f + b x 2 = f (x i+1) f (x i 1 ). (7) 2 If we need second-order derivatives, those can be obtained from the finite difference between f and b x as 2 f x 2 (x i) c x 2 = x f b x = f (x i+1) 2 f (x i ) + f (x i 1 ) () 2. (8) (To be precise, the second order difference eq. (8) is properly derived by computing central differences of a forward difference at node x i+1/2 and a backward difference at node x i 1/2. This distinction can be important for k(x) type problems.) With those approximate finite differences at hand, we can now rewrite eq. (1). For simplicity, we will take κ constant and H = 0. The simplest way of rewriting the diffusion equation is by a Forward Time, Centered Space (FTCS) scheme f t T (x i,t j ) = κ c x 2 T (x i,t j ) (9) T (x i,t j+1 ) T (x i,t j ) κ ( = T (xi+1 t () 2,t j ) 2T (x i,t j ) + T (x i 1,t j ) ). (10) Time therefore needs to be discretized as well, and we have used i indices for space, and j for time, i.e. the temperature at time t j+1 = t j + t is T (x i,t j+1 ). We can march an initial solution at time t j forward by a time step t as T (x i,t j+1 ) = T (x i,t j ) + T = T (x i,t j ) + κ t () 2 ( T (xi+1,t j ) 2T (x i,t j ) + T (x i 1,t j ) ). (11) Because the incremental change of temperature from t j to t j+1, T, can be directly determined from the old temperature solution at t j, this method is called explicit. While FTCS is a really bad idea for advection problems (the method blows up), it is an OK (not the most efficient) approach for our diffusion problem, under certain conditions. Those stability conditions will be explored in class (see appendix). The general solution strategy for any heat flow, finite difference problem is thus to define an initial condition for T (x,t = 0), and boundary conditions (BCs) for the domain, i.e. at x = 0 and x = L. The latter are typically either constant temperature T = C (Dirichlet BC), or constant heat flow, T / x = C (Neumann BC). Then, discretize the spatial dimension, initialize the T i, j variables, and march the solution forward in time according to eq. (11) with some adequately chosen t. The following is a Matlab computer program (a script, actually) written by my post-doc Boris Kaus which illustrates these concepts. We will go over this in detail in class, but the script is included here for completeness. A percent sign,, is a comment, the rest are program instructions. 2
1D Diffusion, explicit finite difference scheme Equation solved: dt/dt = kappa*(dˆ2 T/dxˆ2) using Forward Time Centered Space (FTCS) explicit method i.e. T(t_{i+1}) = T(t_i) + kappa * dt * (T(x_{j+1})-2*T(x_j)+T(x_{j-1})/dxˆ2; Matlab script by Boris Kaus, March 2005 clear all; Input parameters L = 1000; length of model domain in m W_dike = 100; Width of intruding dike T_bg = 1000; Background temperature [C] T_dike = 1300; Temperature of intruding dike kappa = 1e-6; Thermal diffusivity [mˆ2/s] dt = 1; Timestep in years Numerical parameters nx = 101; Number of gridpoints in x-direction num_time= 100; Number of timesteps year = 3600*24*365.25; Seconds/year dt = dt*year; Initialization dx = L/(nx-1); Spacing in x-direction x = -L/2:dx:L/2; Numerical grid in x-direction T = ones(1,nx)*t_bg; ind = find(abs(x)<w_dike/2); spatial region where dike is found T(ind) = T_dike; Initial temperature 3
Time loop for itime=1:num_time save old temperature T_old = T; Compute finite difference scheme ind = 2:nx-1; index array T2nd = (T(ind+1)-2*T(ind)+T(ind-1))/dxˆ2; dt/dxˆ2 T(ind) = kappa*dt*t2nd + T_old(ind); Set BC (constant temperature) T(1) = T_old(1); T(nx) = T_old(nx); end Plotting figure(1), clf plot(x,t) xlabel( Distance [m] ) ylabel( Temperature [ˆo C] ) title([ Time =,num2str(itime*dt/year), years ]) drawnow pause(1/10) The Matlab software allows you to write computer programs quickly, and plot the results easily. You can read more about the Matlab language at http://www.mathworks.com/access/helpdesk/ help/techdoc/matlab.html/. One nifty, yet sometimes perplexing, feature of Matlab is that it works with vectors. For instance, the lines dx = L/(nx-1); Spacing in x-direction x = -L/2:dx:L/2; Numerical grid in x-direction tell the program to actually make an array x = {x 1, x 2,..., x n } with nx elements x 1 = L/2, x 2 = L/2+dx, x 3 = L/2 + 2dx,..., x nx = L/2. More about that in class. 4
Addendum: stability of FTCS and more As you noticed from the exercises in class, there exists a stability criterion for which the FTCS scheme works stably for the diffusion problem. The criterion says that you have to choose the time step t small enough for a given spatial resolution and diffusivity κ so that t ()2 2κ holds. This condition can be derived by means of von Neumann stability analysis, and makes physical sense: t is of order of the characteristic diffusion time for numerical noise as determined by the grid spacing (note factor two). As you might have also noted, one needs to march the solution forward by a large number of time steps so that the essential processes of heat conduction can be captured if t is limited by the above criterion. This makes FTCS as an explicity method a bad idea if efficiency is required. One would therefore rather use implicit schemes (such as Crank-Nicholson), but we don t have the time to discuss these in class. If you want to read more about solving the heat conduction (or any diffusion) equation, you might start with, e.g., Press et al. (1993), sec. 19.2. For advection problems, see sec. 19.1 in that same cookbook text. If you need more detail and explanations, a great resource is Spiegelman (2004). Marc teaches what has to be an awesome class (judging from his lecture notes) at Columbia University on numerical modeling. His chapter 6 (http://www.ldeo. columbia.edu/ mspieg/mmm/diffusion.pdf) deals with diffusion problems. References Press, W. H., Teukolsky, S. A., Vetterling, W. T., and Flannery, B. P. (1993). Numerical Recipes in C: The Art of Scientific Computing. Cambridge University Press, Cambridge, 2 edition. Spiegelman, M. (2004). Myths and Methods in Modeling. Columbia University Course Lecture Notes, http://www.ldeo.columbia.edu/ mspieg/mmm/. (12) 5