Crank-Nicolson Scheme in 1-d heat equation for Fortran programming?

dimanche 2 février 2014

The one-dimensional PDE for heat equation u_t= u_xx.

Initial condition : u(x,0)= 1-|x|

Boundary values: u(+-1,t)=0

Write a Fortran programming,Using Crank-Nicolsan Scheme to simulate heat flow in a dimensional region |x|<1 for t>0 and uniform mesh size of x=0.1 with initial and boundary temperatures specified on input.



=> Module come_function

Implicit None

contains

function d(u, m, JI) !The main process is the thomas algorithm

Implicit None

integer :: J,JI

real, dimension(JI) :: u

real, dimension(JI) :: a, b, c,d

real :: m



Do j = 1, JI

if (j==1) then

a(j) = (1-2.0*m)*u(j) + m * u(j+1)

elseif (j == JI) then

a(j) = (1-2.0*m)*u(j) + m * u(j-1)

else

a(j) = (1-2.0*m)*u(j) + m * u(j-1) + m * u(j+1)

EndIf

End Do

b(1) = -m/(1.0 + 2.0*m)

c(1) = a(1)/(1.0 + 2.0*m)

Do j = 2, JI-1

b(j) = -m/(1+2*m + m * b(j-1))

c(j) = (a(j) + m * c(j-1))/(1+2*m + m * b(j-1))

End Do

c(JI) = (a(JI) + m * c(JI-1))/(1+2*m + m * b(JI-1))

d(JI) = c(JI) !The Array algorithm stores solution vector after solving, and return to main function

Do j = JI-1, 1, -1

d(j) = c(j) - b(j)*d(j+1)

End Do

return

End function

End Module come_function



Program crank_nicolson

use come_function

Real, allocatable :: x(:),u(:)

Real:: m,dx,dt,Tmax

Integer:: j,NI,JI

Print*, 'Enter the total number of time steps'

read*, NI

Print*, 'Enter the final time'

read*, Tmax



dt=Tmax/NI !The size of timestep

Print*, 'This gives stepsize of time dt=',dt

dx= 0.1 !delta x =0.1

allocate (u(0:JI))

m = dt/(2*dx**2)

JI= int(2/dx)-1

open(10,file='crank_nicolsan.m')



!Initial Condition

do j=1,JI-1

x(j)= -1+j*dx

u(j)=(1+x(j))*(1-x(j))**2 !x=-1+j*dx

end do





!Print out the Approximate solution in matlab file

write(10,*) 'ApproximateSolution =[',u(0)

do j =1, JI

u(j) = initialfun(j,dx)

end do

write(10,*)u(JI),']'





end Program crank_nicolson



I have some errors and warning in the code.

Run time errors: undefined variables arrays element of functions on allocate (u(0:JI))

warning 242 - Variable M has been given a value but never used





0 commentaires:

Enregistrer un commentaire