-
Notifications
You must be signed in to change notification settings - Fork 68
/
linear_simple.f90
298 lines (220 loc) · 8.43 KB
/
linear_simple.f90
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
!!
!! Copyright (C) 2009-2013 Johns Hopkins University
!!
!! This file is part of lesgo.
!!
!! lesgo is free software: you can redistribute it and/or modify
!! it under the terms of the GNU General Public License as published by
!! the Free Software Foundation, either version 3 of the License, or
!! (at your option) any later version.
!!
!! lesgo is distributed in the hope that it will be useful,
!! but WITHOUT ANY WARRANTY; without even the implied warranty of
!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
!! GNU General Public License for more details.
!!
!! You should have received a copy of the GNU General Public License
!! along with lesgo. If not, see <http://www.gnu.org/licenses/>.
!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! to solve small, nonsingular linear systems
! reference numerical recipes (fortran 90/95)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
module linear_simple
!use precision
use types, only : rp => rprec
use messages
implicit none
private
public :: solve_linear
character (len=*), parameter :: mod_name = 'linear_simple'
interface assert_eq
module procedure assert_eq2, assert_eq3, assert_eq4
end interface
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
contains
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! solves Ax = b for x
! does NOT destroy A or b
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine solve_linear (A, b, x)
implicit none
real (rp), intent (in) :: A(:, :), b(:)
real (rp), intent (out) :: x(size (b))
character (len=*), parameter :: sub_name = mod_name // '.solve_linear'
integer :: indx(size (A, 1))
real (rp) :: A_tmp(size (A, 1), size (A, 2))
real (rp) :: d
!----------------------------------------------------------------------
A_tmp = A
call ludcmp (A_tmp, indx, d)
x = b
call lubksb (A_tmp, indx, x)
end subroutine solve_linear
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! index of maxloc on an array
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!FUNCTION imaxloc (arr)
!INTEGER :: imaxloc
!
!REAL (RP), DIMENSION(:), INTENT(IN) :: arr
!
!!----------------------------------------------------------------------
!
!imaxloc = maxloc (arr(:), dim = 1)
!
!END FUNCTION imaxloc
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Report and die if integers not all equal (used for size checking)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
FUNCTION assert_eq2(n1,n2,string)
CHARACTER(LEN=*), INTENT(IN) :: string
INTEGER, INTENT(IN) :: n1,n2
INTEGER :: assert_eq2
CHARACTER(LEN=1024) :: msg
character (len=*), parameter :: sub_name = mod_name // '.assert_eq2'
!----------------------------------------------------------------------
if (n1 == n2) then
assert_eq2=n1
else
write (msg, *) 'failed assert with tag:', n_l, string
call error (sub_name, msg)
end if
END FUNCTION assert_eq2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
FUNCTION assert_eq3(n1,n2,n3,string)
CHARACTER(LEN=*), INTENT(IN) :: string
INTEGER, INTENT(IN) :: n1,n2,n3
INTEGER :: assert_eq3
CHARACTER(LEN=1024) :: msg
character (len=*), parameter :: sub_name = mod_name // '.assert_eq3'
!----------------------------------------------------------------------
if (n1 == n2 .and. n2 == n3) then
assert_eq3=n1
else
write (msg, *) 'failed assert with tag:', n_l, string
call error (sub_name, msg)
end if
END FUNCTION assert_eq3
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
FUNCTION assert_eq4(n1,n2,n3,n4,string)
CHARACTER(LEN=*), INTENT(IN) :: string
INTEGER, INTENT(IN) :: n1,n2,n3,n4
INTEGER :: assert_eq4
CHARACTER(LEN=1024) :: msg
character (len=*), parameter :: sub_name = mod_name // '.assert_eq4'
!----------------------------------------------------------------------
if (n1 == n2 .and. n2 == n3 .and. n3 == n4) then
assert_eq4=n1
else
write (msg, *) 'failed assert with tag:', n_l, string
call error (sub_name, msg)
end if
END FUNCTION assert_eq4
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Given an N × N input matrix a, this routine replaces it by the LU
! decomposition of a rowwise permutation of itself. On output, a is
! arranged as in equation (2.3.14); indx is an output vector of
! length N that records the row permutation effected by the partial
! pivoting: d is output as ±1 depending on whether the number of row
! interchanges was even or odd, respectively. This routine is used in
! combination with lubksb to solve linear equations or invert a matrix.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine ludcmp (a, indx, d)
implicit none
real (rp), intent (inout) :: a(:, :)
integer, intent (out) :: indx(:)
real (rp), intent (out) :: d
character (len=*), parameter :: sub_name = mod_name // '.ludcmp'
real (rp), parameter :: TINY = 1.0e-20_rp ! a small number
integer :: j,n,imax
real (rp) :: vv(size (a, 1))
!--stores the implicit scaling of each row
!----------------------------------------------------------------------
n = assert_eq (size (a, 1), size(a, 2), size(indx), 'ludcmp')
d = 1.0_rp ! no row interchanges yet
vv = maxval (abs (a), dim=2) ! loop over rows to get implicit scaling info
if (any (vv == 0.0_rp)) call error (sub_name, 'singular matrix')
!--There is a row of zeros
vv = 1.0_rp / vv ! save the scaling
do j = 1, n
! find pivot row
imax = (j - 1) + maxloc ( vv(j:n) * abs (a(j:n, j)), dim = 1 )
! do we need to interchange rows?
if (j /= imax) then ! yes
! interchange row, change d, and interchange scale factor
call swap (a(imax, :), a(j, :))
d = -d
vv(imax) = vv(j)
end if
indx(j)=imax
! If the pivot element is zero the matrix is singular (at least
! to the precision of the algorithm). For some applications on
! singular matrices, it is desirable to substitute TINY for zero.
if (a(j, j) == 0.0_rp) a(j, j) = TINY
! divide by the pivot element
a(j+1:n, j) = a(j+1:n, j) / a(j, j)
! reduce remaining submatrix
a(j+1:n, j+1:n) = a(j+1:n, j+1:n) - &
outerprod (a(j+1:n, j), a(j, j+1:n))
end do
end subroutine ludcmp
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Solves the set of N linear equations AX = B. Here the N × N matrix a
! is input, not as the original matrix A, but rather as its LU
! decomposition, determined by the routine ludcmp.
! indx is input as the permutation vector of length N returned by ludcmp
! b is input as the right-hand-side vector B, also of length N, and
! returns with the solution vector X.
! a and indx are not modified by this routine and can be left in place
! for successive calls with di
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SUBROUTINE lubksb(a,indx,b)
IMPLICIT NONE
REAL(RP), DIMENSION(:,:), INTENT(IN) :: a
INTEGER, DIMENSION(:), INTENT(IN) :: indx
REAL(RP), DIMENSION(:), INTENT(INOUT) :: b
INTEGER :: i,n,ii,ll
REAL(RP) :: summ
!----------------------------------------------------------------------
n=assert_eq(size(a,1),size(a,2),size(indx),'lubksb')
ii=0
! When ii is set to a positive value, it will become the index of the
! first nonvanishing element of b. We now do the forward substitution,
! equation (2.3.6). The only new wrinkle is to unscramble the
! permutation as we go.
do i=1,n
ll=indx(i)
summ=b(ll)
b(ll)=b(i)
if (ii /= 0) then
summ = summ - dot_product(a(i,ii:i-1),b(ii:i-1))
else if (summ /= 0.0) then
ii=i
! a nonzero element was encountered, so from now on we will have
! to do the dot product above
end if
b(i)=summ
end do
do i=n,1,-1
! now we do the backsubstitution, equation (2.3.7)
b(i) = (b(i) - dot_product(a(i,i+1:n),b(i+1:n))) / a(i,i)
end do
END SUBROUTINE lubksb
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
FUNCTION outerprod(a,b)
REAL(RP), DIMENSION(:), INTENT(IN) :: a,b
REAL(RP), DIMENSION(size(a),size(b)) :: outerprod
outerprod = spread(a,dim=2,ncopies=size(b)) * &
spread(b,dim=1,ncopies=size(a))
END FUNCTION outerprod
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SUBROUTINE swap(a,b)
REAL(RP), DIMENSION(:), INTENT(INOUT) :: a,b
REAL(RP), DIMENSION(SIZE(a)) :: dum
dum=a
a=b
b=dum
END SUBROUTINE swap
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
end module linear_simple