-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtridiagonal.F90
More file actions
executable file
·191 lines (167 loc) · 4.57 KB
/
tridiagonal.F90
File metadata and controls
executable file
·191 lines (167 loc) · 4.57 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
!#include"cppdefs.h"
!-----------------------------------------------------------------------
!BOP
!
! !MODULE: mtridiagonal --- solving the system\label{sec:tridiagonal}
!
! !INTERFACE:
MODULE mtridiagonal
!
! !DESCRIPTION:
!
! Solves a linear system of equations with a tridiagonal matrix
! using Gaussian elimination.
!
use fabm_types, only: rk
! !PUBLIC MEMBER FUNCTIONS:
public init_tridiagonal, tridiagonal, clean_tridiagonal
!
! !PUBLIC DATA MEMBERS:
real(kind=rk), dimension(:), allocatable :: au,bu,cu,du
!
! !REVISION HISTORY:
! Original author(s): Hans Burchard & Karsten Bolding
!
! Phil Wallhead 24/02/2016: Commented out #include"cppdefs.h" statement on line 1
! Replaced real(kind=rk) declarations with real(kind=rk) and use mkind statement to avoid use of cpps
!
!EOP
!
! private data members
real(kind=rk), private, dimension(:), allocatable :: ru,qu
!
!-----------------------------------------------------------------------
contains
!-----------------------------------------------------------------------
!BOP
!
! !IROUTINE: Allocate memory
!
! !INTERFACE:
subroutine init_tridiagonal(N)
!
! !DESCRIPTION:
! This routines allocates memory necessary to perform the Gaussian
! elimination.
!
! !USES:
IMPLICIT NONE
!
! !INPUT PARAMETERS:
integer, intent(in) :: N
!
! !REVISION HISTORY:
! Original author(s): Hans Burchard & Karsten Bolding
!
!EOP
!
! !LOCAL VARIABLES:
integer :: rc
!
!-----------------------------------------------------------------------
!BOC
write(*,*) ' ', 'init_tridiagonal'
allocate(au(0:N),stat=rc)
if (rc /= 0) stop 'init_tridiagonal: Error allocating au)'
au = 0.0_rk
allocate(bu(0:N),stat=rc)
if (rc /= 0) stop 'init_tridiagonal: Error allocating bu)'
bu = 0.0_rk
allocate(cu(0:N),stat=rc)
if (rc /= 0) stop 'init_tridiagonal: Error allocating cu)'
cu = 0.0_rk
allocate(du(0:N),stat=rc)
if (rc /= 0) stop 'init_tridiagonal: Error allocating du)'
du = 0.0_rk
allocate(ru(0:N),stat=rc)
if (rc /= 0) stop 'init_tridiagonal: Error allocating ru)'
ru = 0.0_rk
allocate(qu(0:N),stat=rc)
if (rc /= 0) stop 'init_tridiagonal: Error allocating qu)'
qu = 0.0_rk
return
end subroutine init_tridiagonal
!EOC
!-----------------------------------------------------------------------
!BOP
!
! !IROUTINE: Simplified Gaussian elimination
!
! !INTERFACE:
subroutine tridiagonal(N,fi,lt,value)
!
! !DESCRIPTION:
! A linear equation with tridiagonal matrix structure is solved here. The main
! diagonal is stored on {\tt bu}, the upper diagonal on {\tt au}, and the
! lower diagonal on {\tt cu}, the right hand side is stored on {\tt du}.
! The method used here is the simplified Gauss elimination, also called
! \emph{Thomas algorithm}.
!
! !USES:
IMPLICIT NONE
!
! !INPUT PARAMETERS:
integer, intent(in) :: N,fi,lt
!
! !OUTPUT PARAMETERS:
real(kind=rk) :: value(0:N)
!
! !REVISION HISTORY:
! Original author(s): Hans Burchard & Karsten Bolding
!
!EOP
!
! !LOCAL VARIABLES:
integer :: i
!
!-----------------------------------------------------------------------
!BOC
ru(lt)=au(lt)/bu(lt)
qu(lt)=du(lt)/bu(lt)
do i=lt-1,fi+1,-1
ru(i)=au(i)/(bu(i)-cu(i)*ru(i+1))
qu(i)=(du(i)-cu(i)*qu(i+1))/(bu(i)-cu(i)*ru(i+1))
end do
qu(fi)=(du(fi)-cu(fi)*qu(fi+1))/(bu(fi)-cu(fi)*ru(fi+1))
value(fi)=qu(fi)
do i=fi+1,lt
value(i)=qu(i)-ru(i)*value(i-1)
end do
return
end subroutine tridiagonal
!EOC
!-----------------------------------------------------------------------
!BOP
!
! !IROUTINE: De-allocate memory
!
! !INTERFACE:
subroutine clean_tridiagonal()
!
! !DESCRIPTION:
! De-allocates memory allocated in init\_tridiagonal.
!
! !USES:
IMPLICIT NONE
!
! !REVISION HISTORY:
! Original author(s): Karsten Bolding
!
!EOP
!-----------------------------------------------------------------------
!BOC
write(*,*) ' ', 'clean_tridiagonal'
if (allocated(au)) deallocate(au)
if (allocated(bu)) deallocate(bu)
if (allocated(cu)) deallocate(cu)
if (allocated(du)) deallocate(du)
if (allocated(ru)) deallocate(ru)
if (allocated(qu)) deallocate(qu)
return
end subroutine clean_tridiagonal
!EOC
!-----------------------------------------------------------------------
end module mtridiagonal
!-----------------------------------------------------------------------
! Copyright by the GOTM-team under the GNU Public License - www.gnu.org
!-----------------------------------------------------------------------