-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathh1dcn.f95
98 lines (75 loc) · 1.7 KB
/
h1dcn.f95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
! 2D Heat Equation Using Crank-Nicholson
! s.t.
! u(1,t) = 0 for t=>0
! u(0,t) = 0 for t=>0
!
! Dale Roberts <dale.o.roberts@gmail.com
!
program h1dcn
implicit none
! External Functions
external sgtsv
! X Space Parameters
integer, parameter :: nJ = 40
real, parameter :: mX = 0
real, parameter :: pX = 1
! T Space Parameters
integer, parameter :: nT = 10
real, parameter :: mT = 0
real, parameter :: pT = 0.1
! Variables
integer :: t, s, info, fd
real :: U(nJ+1), b(nJ+1)
real :: dl(nJ), dc(nJ+1), du(nJ)
real :: x, dx, dt, mu
dx = (pX-mX) / real(nJ)
dt = (pT-mT) / real(nT)
mu = dt / (dx**2)
! Set Initial Data
do s = 1, (nJ+1)
x = mX + (s-1)*dx
if (x <= 0.5) then
U(s) = 2 * x
else
U(s) = 2 - 2 * x
end if
end do
! Output Data in Gnuplot format
open(fd, FILE='data.0', STATUS='NEW')
do s = 1, (nJ+1)
x = mX + (s-1)*dx
write (fd, *) x, U(s)
end do
close(fd)
! Construct Tri-Diagonals
do s = 1, nJ
du(s) = -mu * 0.5
dc(s) = 1 + mu
dl(s-1) = -mu * 0.5
end do
dc(1) = 1
du(1) = 0
dl(1) = 0
du(nJ) = 0
dc(nJ+1) = 1
dl(nJ) = 0
do t = 1, nT
! Construct RHS for system
b(1) = 0
do s = 2, nJ
b(s) = 0.5 * mu * U(s-1) + (1-mu)*U(s) + 0.5*mu*U(s+1)
end do
b(nJ+1) = 0
! Solve Tri-Diagonal System
call sgtsv(nJ+1, 1, dl, dc, du, b, nJ+1, info)
print '(10F8.2)/', b
U = b
! Output Data in Gnuplot format
open(fd, FILE='data.' // char(96 + t), STATUS='NEW')
do s = 1, (nJ+1)
x = mX + (s-1)*dx
write (fd, *) x, U(s)
end do
close(fd)
end do
end program h1dcn