Вы находитесь на странице: 1из 11

M1 International - Arts et M etiers PariTech Programing and Numerical Methods Fortran Project

deadline : November 23rd 2012

The second Fortran project is an homework due November 23rd 2012. It represents 25% of the total evaluation. You can send a pdf or doc le including your comments, results, gures, listings of routines to xavier.gloerfelt@paris.ensam.fr. To be sure that your email is well delivered, please verify that you have a conrmation by e-mail return specifying that your work is received.

The unsteady heat equation


In the rst Fortran project, we have considered 2-D heat transfer problems in which the tem-

peratures are independent of time. In many applications, however the temperatures are varying with time, and we require the understanding of the complete time history of the temperature variation. For example, in metallurgy, the heat treating process can be controlled to directly aect the characteristics of the processed materials. Annealing (slow cool) can soften metals and improve ductility. On the other hand, quenching (rapid cool) can harden the strain boundary and increase strength. In order to characterize this transient behavior, the unsteady equation is needed. The onedimensional version is 2 = 2 , 0 x L, t 0 (1) t x where = (x, t) is the dependent variable, and is a constant coecient. Equation (1) is a model of transient heat conduction in a slab of material with thickness L. The domain of solution is a semi-innite strip of width L that continues indenitely in time. The material property is the thermal diusivity. In a practical computation, the solution is obtained only for a nite time tmax . Solution to Equation (1) requires specication of boundary conditions at x = 0 and x = L, and initital conditions at t = 0. Simple boundary and initital conditions are : (0, t) = 0 , (L, t) = L , (x, 0) = f0 (x) (2)

Discretization
A nite-dierence method is used for obtaining numerical solutions to Equation (1). Applying

nite dierences, a discrete dierence equation is obtained from the original PDE for (x, t). Then

the solution method, coded in Fortran, will provide an approximation m j to (x, t). For the codes you have to develop, the discrete x are uniformly spaced in the interval 0 x L such that : xj = (j 1)x , j = 1, 2, ...N

where N is the total number of spatial nodes, including those on the boundary. Given L and N , the spacing between the xj is computed with x = L N 1

Similarly, the discrete time t are uniformly spaced in 0 t tmax : tm = (m 1)t , m = 1, 2, ...M

where M is the total number oftime steps and t is the size of a time step t = tmax M 1

Both the time and space derivatives are replaced by nite dierences. For the second-order spatial operator, you will use a second-order central dierence (as in the rst Fortran project). Two dierent time integration will be used : Forward Euler scheme Crank-Nicolson scheme This is respectively Adams-Bashforth of order 1 and Adams-Moulton of order 2. The rst one is an explicit scheme, and the time step is limited by Fourier criterion. The second method is implicit and will require the inversion of a system (with a tridiagonal matrix).

Test problem
The nite dierence codes are tested by solving the heat equation with boundary condition

(0, t) = (L, t) = 0, and initial condition f0 = sin(x/L). The exact solution for this problem is derived in the Appendix.

Fortran program
The proposed structure of the program could be a module part, where specication of variables which are used in several subroutines are dened : specif.f90 2

a main program main.f90, where the following operations are realized : initialization of the variables, calls to solving algorithm (Forward Euler or Crank-Nicolson), computation of the nal error, and output in a le of the solution and error to be plotted with Matlab. a subroutine for the Forward Euler/Centered dierences method, forward_euler.f90. a subroutine for the Crank-Nicolson/Centered dierences method, crank_nicolson.f90. a subroutine to solve a tridiagonal system tridiag.f90 where the Thomas algorithm is used to obtain directly the LU decomposition. a subroutine solve_LU.f90 including the forward and backward substitution steps for solving a system given its LU decomposition (here provided by tridiag.f90). A sample makele is provided using these names for the f90-les. You are free to change routine names or the number or scope of the subroutines. You then have to change consequently the makele. Example makele PROG = prog

#SRCS = specif.f90 main.f90 forward_euler.f90 #OBJS = specif.o main.o forward_euler.o SRCS = specif.f90 main.f90 forward_euler.f90 crank_nicolson.f90 tridiag.f90 solve_LU.f90 OBJS = specif.o main.o forward_euler.o crank_nicolson.o tridiag.o solve_LU.o LIBS = # for CYGWIN with gfortran F90 = gfortran F90FLAGS = -fdefault-real-8 # for LINUX PC with intel fortran #F90 = ifort #F90FLAGS = -O3 -r8 -fpe0 LDFLAGS = all: $(PROG) $(PROG): $(OBJS) $(F90) -o $@ $(OBJS) $(LIBS) $(LDFLAGS) clean: rm -f $(PROG) $(OBJS) *.mod *.out

debug: $(F90) -g $(SRCS) $(LIBS) dxladebug a.out .SUFFIXES: $(SUFFIXES) .f90 .f90.o: $(F90) $(F90FLAGS) -c $< main.o: specif.o forward_euler.o: specif.o crank_nicolson.o: specif.o Example specif.f90 !============================================================================ ! Common variables !============================================================================ module variables implicit none ! unknown variable (first dimension nx for space and second nt for time) real, dimension(:,:), allocatable :: u ! spatial boundary conditions at x=0 and x=L real :: u0,uL ! space discretization integer :: nx ! number of mesh points in x direction real :: L ! length of the domain real :: dx ! mesh size real, dimension(:), allocatable :: x ! location of finite difference nodes ! time discretization integer :: nt ! number of steps real :: tmax ! maximum time for the simulation real :: dt ! time step ! constants real :: alpha ! diffusivity real :: pi ! constant pi end module variables 4

real, dimension(:), allocatable :: t ! values of time at which solution is obtained (time nod

Example main.f90 ! ================================= ! main program ! ================================= program unsteady_heat ! USE ZONE: common variables use variables ! put implicit none to avoid declaration problem implicit none ! Local variables integer:: i,m,ires real :: t_final,t_initial,t_cpu integer:: done real, dimension(:), allocatable :: ue ! analytical solution (for each spatial node nx) real :: err ! error at last time step nt ! Constant Pi pi=acos(-1.) ! Values of parameters nt = 10 nx = 20 alpha = 0.1 L = 1 tmax = 0.5 ires=1 !!$ !!$ !!$ !!$ !!$ !!$ !!$ !!$ !!$ !!$ 5 print*,Size of the problem? print*,nt read*,nt print*,tmax read*,tmax print*,nx read*,nx print*,L read*,L

!!$ !!$

print *,choice of the method ->1: FE/CD , 2: CN/CD read *,ires

call cpu_time(t_initial) ! Memory allocations for dynamic variables ! ======================================== allocate(x(nx)) allocate(t(nt)) allocate(u(nx,nt)) allocate(ue(nx)) ! Compute mesh spacing and time step ! ================================== ! Mesh size dx=1./(nx-1) ! uniform mesh x do i=1,nx x(i)=(i-1)*dx end do ! Time step dt=1./(nt-1) ! uniform mesh t do i=1,nt t(i)=(i-1)*dt end do ! Initialization stage ! ==================== u=0. ! u is initialized at initial time (f0) do i=1,nx u(i,1)=sin(pi*x(i)/L) end do ! Initialization of the boundary values ! needed to apply BC inside time step loop u0 = 0

uL = 0 ! Exact solution (used to compute the error) ! ============== ! at end of simulation ue = sin(pi*x/L)*exp(-t(nt)*alpha*(pi/L)**2) ! Choice of the method ! ==================== select case (ires) case (1) call forward_euler case (2) call crank_nicolson case default print*,the option is not defined stop end select ! Error computation ! ================= err=0. do i=1,nx err=err+abs(u(i,nt)-ue(i)) end do err=err/nx ! Opening of output files to store the errors ! =========================================== open(10,file=error.dat) rewind(10) write(10,*) err close(10) ! The final solution is written in a file

! ======================================= ! MATLAB file (particular BINARY file) open(40,file=solution.bin,form=unformatted,status=unknown) write(40) nx write(40) nt ! write the meshgrid and time nodes write(40) (x(i),i=1,nx) write(40) (t(m),m=1,nt) ! write the computed solution write(40) ((u(i,m),i=1,nx),m=1,nt) ! write the analytical solution write(40) (ue(i),i=1,nx) close(40) ! Note that no format is used for a binary file ! Thats why the ASCII file is also named FORMATTED file ! by default, the "format" attribute is "formatted" ! Some informations are written to the screen ! =========================================== print *,Mesh size: ,nx print *,Number of iterations: ,nt print *,Error L2 : ,err print *,Log(Error L2 norm) : ,log10(err) call cpu_time(t_final) t_cpu=t_final-t_initial print *,CPU time,t_cpu ! The dynamic arrays are deallocated ! ================================== deallocate(x,t,u,ue) end program unsteady_heat Hereafter an example of visualization le reading a binary le is proposed. You can of course improve the visualization and add other plot such as error. 8

Example Matlab le vizu.m clear all close all opengl neverselect precision = double; %%precision = float; disp(Start reading file solution.bin); % for little_endian binary f1= fopen(solution.bin,r); % for big_endian binary %f1= fopen(solution.bin,r,ieee-be); frewind(f1); arg = fread(f1,1,float); nx = fread(f1,1,int); arg = fread(f1,1,float); arg = fread(f1,1,float); nt = fread(f1,1,int); arg = fread(f1,1,float); arg = fread(f1,1,float); x = fread(f1,nx,precision); arg = fread(f1,1,float); arg = fread(f1,1,float); t = fread(f1,nt,precision); arg = fread(f1,1,float); u = zeros(nx,nt); ue = zeros(nx,1); arg = fread(f1,1,float); 9

u = fread(f1,[nx nt],precision); arg = fread(f1,1,float); arg = fread(f1,1,float); ue = fread(f1,nx,precision); arg = fread(f1,1,float); fclose(f1); disp(End reading file solution.bin); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% fig = figure(1); colordef(fig,none); whitebg(fig,w); orient portrait; set(fig,papertype,a4letter); set(fig,units,centimeters,paperunits,centimeters); set(fig,paperposition,[2. 1. 12. 12.]); set(fig,position,[2. 1. 12. 12.]); axes(units,centimeters,position,[2. 2. 8. 8.]); plot(x,u(:,1),k) hold on plot(x,u(:,4),r) plot(x,u(:,6),b) plot(x,u(:,8),g) plot(x,u(:,10),m) xlabel(x,Fontsize,13); ylabel(T(x,t),Fontsize,13);

Work
In summary, you are asked to write your own fortran les to solve the one-dimensional unsteady

heat equation using second-order central nite dierences for space and both forward Euler and Crank-Nicolson schemes for time integration. In the case of the implicit scheme, it is recommended to use Thomas algorithm to solve the resulting tridiagonal matrix system. 10

You can use the previous examples freely to develop your program. All the routines must be included in your report le (in pdf or doc format). It is recommended to use Matlab to visualize the results. You can use the default values (in the example program main.f90) to start your computation. In your report, you must compare the two integration schemes (accuracy, cost, stability ...). You can for instance change the resolution parameter (nx, nt or tmax) to illustrate stability issues. Your comments and plots will be included in the report.

Appendix : Exact solution


The general solution of Equation (1) with the boundary an d initial conditions (0, t) = (L, t) = 0 , (x, 0) = f0 (x) (3)

is (x, t) =

bn sin
n=1

nx n2 2 t exp L L2

(4)

where the bn are obtained from bn = Example : f0 = sin(x/L) Compute bn = but


0 L

2 L

f0 (x) sin
0

x dx L

(5)

2 L

sin
0

x x sin dx L L L/2 if n=1 0 otherwise

sin Therefore

x x sin dx = L L

b1 = 1 , bn = 0 , The exact solution to Equation (1) is thus (x, t) = sin 2 t x exp 2 L L n = 2, 3, ...

11

Вам также может понравиться