-
Notifications
You must be signed in to change notification settings - Fork 1
/
gmres.f90
267 lines (194 loc) · 5.65 KB
/
gmres.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
module solvergmres
implicit none
contains
!===================================================
subroutine findnorm(norm,v1,n)
integer,intent(in) :: n
real*8,intent(in) :: v1(n)
real*8, intent(out) :: norm
integer :: i
norm=0.d0
do i=1,n
norm=norm+v1(i)*v1(i)
enddo
norm=sqrt(norm)
end subroutine findnorm
!===================================================
subroutine innerproduct(v1dotv2,v1,v2,n)
integer, intent(in) :: n
real*8, intent(in) :: v1(n),v2(n)
real*8, intent(out) :: v1dotv2
integer :: i
v1dotv2=0.d0
do i=1,n
v1dotv2=v1dotv2+v1(i)*v2(i)
enddo
end subroutine innerproduct
!===================================================
subroutine arnoldialgorithm(v1,m,n,timederivfactor,vel,dcoeff,reac,dirc_bc_flags,&
flux_bc_flags,dircvals,fluxvals,dx,dt,&
Hmat,kspvecs,findAX,precond,lucky)
integer,intent(in) :: m,n
real*8,intent(in) :: v1(n)
real*8, intent(inout) :: Hmat(m+1,m)
real*8, intent(inout) :: kspvecs(n,m+1)
logical,intent(inout) :: lucky
real*8, intent(in) :: vel(n),dcoeff(n),reac(n)
real*8, intent(in) :: dx,dt
logical,intent(in) :: dirc_bc_flags(2),flux_bc_flags(2)
real*8, intent(in) :: dircvals(2),fluxvals(2)
real*8, intent(in) :: timederivfactor
external :: findAX
external :: precond
real*8 :: Avj(n)
real*8 :: MinvAvj(n)
real*8 :: wj(n)
real*8 :: vi(n),vj(n)
integer :: i,j
!initialize variables
Avj = 0.d0
MinvAvj = 0.d0
wj = 0.d0
vi = 0.d0
vj = 0.d0
kspvecs(:,1)=v1
lucky = .false.
do j=1,m
vj = kspvecs(:,j)
call findAX(Avj,vj,timederivfactor,vel,dcoeff,reac,dirc_bc_flags,&
flux_bc_flags,dircvals,fluxvals,dx,dt,n)
call precond(MinvAvj,Avj,timederivfactor,vel,dcoeff,reac,&
dirc_bc_flags,&
flux_bc_flags,dircvals,fluxvals,&
dx,dt,n)
do i=1,j
vi = kspvecs(:,i)
call innerproduct(Hmat(i,j),MinvAvj,vi,n)
enddo
wj=MinvAvj
do i=1,j
vi = kspvecs(:,i)
wj=wj-Hmat(i,j)*vi
enddo
call findnorm(Hmat(j+1,j),wj,n)
if(Hmat(j+1,j) > 0.d0) then
kspvecs(:,j+1)=wj(:)/Hmat(j+1,j)
else
lucky=.true.
endif
enddo
end subroutine arnoldialgorithm
!===================================================
subroutine leastsquaresminimize(y,Hmat,m,beta)
integer,intent(in) :: m
real*8,intent(inout) :: Hmat(m+1,m)
real*8,intent(inout) :: y(m)
real*8,intent(in) :: beta
real*8 :: c,s,h_up,h_down,dtr
real*8 :: val1,val2
real*8 :: beta_e1(m+1)
integer :: i,j
beta_e1 = 0.d0
beta_e1(1) = beta;
do i=1,m
h_up = Hmat(i,i)
h_down = Hmat(i+1,i)
dtr = sqrt(h_up*h_up+h_down*h_down)
c = h_up/dtr
s = h_down/dtr
do j=1,m
h_up = Hmat(i,j)
h_down = Hmat(i+1,j)
Hmat(i,j) = c*h_up + s*h_down
Hmat(i+1,j) = -s*h_up + c*h_down
enddo
val1 = c*beta_e1(i) + s*beta_e1(i+1);
val2 = -s*beta_e1(i) + c*beta_e1(i+1);
beta_e1(i) = val1
beta_e1(i+1) = val2
enddo
y(m) = beta_e1(m)/Hmat(m,m)
do i=m-1,1,-1
y(i)=beta_e1(i)
do j=i+1,m
y(i)=y(i)-Hmat(i,j)*y(j)
enddo
y(i) = y(i)/Hmat(i,i)
enddo
end subroutine leastsquaresminimize
!===================================================
subroutine performgmres(b,x0,x,timederivfactor,vel,dcoeff,reac,&
dirc_bc_flags,flux_bc_flags,&
dircvals,fluxvals,dx,dt,m,n,nrestarts,findAX,precond)
integer,intent(in) :: m,n,nrestarts
real*8, intent(in) :: b(n),x0(n)
real*8, intent(inout) :: x(n)
external :: findAX, precond
real*8, intent(in) :: vel(n),dcoeff(n),reac(n)
real*8, intent(in) :: dx,dt
logical,intent(in) :: dirc_bc_flags(2),flux_bc_flags(2)
real*8, intent(in) :: dircvals(2),fluxvals(2)
real*8, intent(in) :: timederivfactor
integer :: i,j
real*8 :: r0(n)
real*8 :: Minvr0(n)
real*8 :: Ax0(n),Ax(n)
real*8 :: r(n),v1(n)
real*8 :: beta
real*8 :: y(m)
real*8 :: tol
real*8 :: kspvectors(n,m+1)
real*8 :: Hmat(m+1,m)
logical :: lucky
!initialize everything to 0
r0 = 0.d0
Minvr0 = 0.d0
Ax0 = 0.d0
Ax = 0.d0
r = 0.d0
v1 = 0.d0
beta = 0.d0
y = 0.d0
tol = 0.d0
call findAX(Ax0,x0,timederivfactor,vel,dcoeff,reac,&
dirc_bc_flags,flux_bc_flags,&
dircvals,fluxvals,dx,dt,n)
r0 = b-Ax0
call precond(Minvr0,r0,timederivfactor,vel,dcoeff,reac,dirc_bc_flags,&
flux_bc_flags,dircvals,fluxvals,dx,dt,n)
r=Minvr0
x=x0
Hmat = 0.d0
kspvectors = 0.d0
lucky = .false.
tol = 1d-10
do i=1,nrestarts
call findnorm(beta,r,n)
print *,"restart iteration:",i,"residual norm:",beta
if(beta < tol) then
exit
endif
v1=r/beta
call arnoldialgorithm(v1,m,n,timederivfactor,vel,dcoeff,&
reac,dirc_bc_flags,flux_bc_flags,&
dircvals,fluxvals,dx,dt,Hmat,kspvectors,findAX,precond,lucky)
if(lucky .eqv. .true.) then
print *,"lucky condition"
exit
endif
call leastsquaresminimize(y,Hmat,m,beta)
do j=1,m
x=x+y(j)*kspvectors(:,j)
enddo
call findAX(Ax,x,timederivfactor,vel,dcoeff,reac,&
dirc_bc_flags,flux_bc_flags,&
dircvals,fluxvals,dx,dt,n)
r=b-Ax
call precond(Minvr0,r,timederivfactor,vel,dcoeff,reac,&
dirc_bc_flags,&
flux_bc_flags,dircvals,fluxvals,dx,dt,n)
r=Minvr0
enddo
end subroutine performgmres
!===================================================
end module solvergmres