31
31
32
32
# # Krylov.jl
33
33
34
- struct KrylovJL{F,Tl,Tr,T,I,A,K} <: SciMLLinearSolveAlgorithm
34
+ struct KrylovJL{F,Tl,Tr,T,I,A,K} <: AbstractKrylovSubspaceMethod
35
35
KrylovAlg:: F
36
36
Pl:: Tl
37
37
Pr:: Tr
38
38
abstol:: T
39
39
reltol:: T
40
40
maxiter:: I
41
+ ifverbose:: Bool
41
42
restart:: I
42
43
window:: I
43
44
args:: A
44
45
kwargs:: K
45
46
end
46
47
47
48
function KrylovJL (args... ; KrylovAlg = Krylov. gmres!, Pl= I, Pr= I,
48
- abstol= 0.0 , reltol= 0.0 , maxiter= 0 , # for solver call
49
+ abstol= 0.0 , reltol= 0.0 , maxiter= 0 , ifverbose = false ,
49
50
restart= 20 , window= 0 , # for building solver
50
51
kwargs... )
51
52
52
- return KrylovJL (KrylovAlg, Pl, Pr, abstol, reltol, maxiter,
53
+ return KrylovJL (KrylovAlg, Pl, Pr, abstol, reltol, maxiter, ifverbose,
53
54
restart, window,
54
55
args, kwargs)
55
56
end
56
57
57
58
KrylovJL_CG (args... ;kwargs... ) =
58
- KrylovJL (Krylov . cg!, args... ; kwargs... )
59
+ KrylovJL (args... ; KrylovAlg = Krylov . cg!, kwargs... )
59
60
KrylovJL_GMRES (args... ;kwargs... ) =
60
- KrylovJL (Krylov . gmres!, args... ; kwargs... )
61
+ KrylovJL (args... ; KrylovAlg = Krylov . gmres!, kwargs... )
61
62
KrylovJL_BICGSTAB (args... ;kwargs... ) =
62
- KrylovJL (Krylov . bicgstab!, args... ; kwargs... )
63
+ KrylovJL (args... ; KrylovAlg = Krylov . bicgstab!, kwargs... )
63
64
KrylovJL_MINRES (args... ;kwargs... ) =
64
- KrylovJL (Krylov . minres!, args... ; kwargs... )
65
+ KrylovJL (args... ; KrylovAlg = Krylov . minres!, kwargs... )
65
66
66
67
const KrylovJL_solvers = Dict (
67
68
(Krylov. lsmr! => Krylov. LsmrSolver ),
@@ -105,7 +106,7 @@ function init_cacheval(alg::KrylovJL, A, b, u)
105
106
KS === Krylov. DqgmresSolver ||
106
107
KS === Krylov. DiomSolver ||
107
108
KS === Krylov. GmresSolver ||
108
- KS === Krylov. FormSovler
109
+ KS === Krylov. FomSolver
109
110
)
110
111
KS (A, b, alg. restart)
111
112
elseif (KS === Krylov. MinresSolver ||
@@ -133,36 +134,46 @@ function SciMLBase.solve(cache::LinearCache, alg::KrylovJL; kwargs...)
133
134
abstol = (alg. abstol == 0 ) ? √ eps (eltype (cache. b)) : alg. abstol
134
135
reltol = (alg. reltol == 0 ) ? √ eps (eltype (cache. b)) : alg. reltol
135
136
maxiter = (alg. maxiter == 0 ) ? length (cache. b) : alg. maxiter
137
+ verbose = alg. ifverbose ? 1 : 0
136
138
137
139
138
140
args = (cache. cacheval, cache. A, cache. b)
139
- kwargs = (atol= abstol, rtol= reltol, itmax= maxiter, alg. kwargs... )
141
+ kwargs = (atol= abstol, rtol= reltol, itmax= maxiter, verbose= verbose,
142
+ alg. kwargs... )
140
143
141
- if cache. cacheval == Krylov. GmresSolver
144
+ if cache. cacheval isa Krylov. CgSolver
145
+ alg. Pr != LinearAlgebra. I &&
146
+ @warn " $(alg. KrylovAlg) doesn't support right preconditioning."
147
+ Krylov. solve! (args... ; M= alg. Pl,
148
+ kwargs... )
149
+ elseif cache. cacheval isa Krylov. GmresSolver
142
150
Krylov. solve! (args... ; M= alg. Pl, N= alg. Pr,
143
151
kwargs... )
144
- elseif cache. cacheval == Krylov. CgSolver
152
+ elseif cache. cacheval isa Krylov. BicgstabSolver
153
+ Krylov. solve! (args... ; M= alg. Pl, N= alg. Pr,
154
+ kwargs... )
155
+ elseif cache. cacheval isa Krylov. MinresSolver
145
156
alg. Pr != LinearAlgebra. I &&
146
157
@warn " $(alg. KrylovAlg) doesn't support right preconditioning."
147
158
Krylov. solve! (args... ; M= alg. Pl,
148
159
kwargs... )
149
160
else
150
- Krylov. solve! (args... ; M= alg. Pl, N= alg. Pr,
151
- kwargs... )
161
+ Krylov. solve! (args... ; kwargs... )
152
162
end
153
163
154
164
return cache. u
155
165
end
156
166
157
167
# # IterativeSolvers.jl
158
168
159
- struct IterativeSolversJL{F,Tl,Tr,T,I,A,K} <: SciMLLinearSolveAlgorithm
169
+ struct IterativeSolversJL{F,Tl,Tr,T,I,A,K} <: AbstractKrylovSubspaceMethod
160
170
generate_iterator:: F
161
171
Pl:: Tl
162
172
Pr:: Tr
163
173
abstol:: T
164
174
reltol:: T
165
175
maxiter:: I
176
+ ifverbose:: Bool
166
177
restart:: I
167
178
args:: A
168
179
kwargs:: K
@@ -172,21 +183,21 @@ function IterativeSolversJL(args...;
172
183
generate_iterator = IterativeSolvers. gmres_iterable!,
173
184
Pl= IterativeSolvers. Identity (),
174
185
Pr= IterativeSolvers. Identity (),
175
- abstol= 0.0 , reltol= 0.0 , maxiter= 0 , restart = 0 ,
176
- kwargs... )
186
+ abstol= 0.0 , reltol= 0.0 , maxiter= 0 , ifverbose = true ,
187
+ restart = 0 , kwargs... )
177
188
return IterativeSolversJL (generate_iterator, Pl, Pr,
178
- abstol, reltol, maxiter, restart ,
179
- args, kwargs)
189
+ abstol, reltol, maxiter, ifverbose ,
190
+ restart, args, kwargs)
180
191
end
181
192
182
193
IterativeSolversJL_CG (args... ; kwargs... ) =
183
- IterativeSolversJL (IterativeSolvers . cg_iterator!, args... ; kwargs... )
194
+ IterativeSolversJL (args... ; generate_iterator = IterativeSolvers . cg_iterator!, kwargs... )
184
195
IterativeSolversJL_GMRES (args... ;kwargs... ) =
185
- IterativeSolversJL (IterativeSolvers . gmres_iterable!, args... ; kwargs... )
196
+ IterativeSolversJL (args... ; generate_iterator = IterativeSolvers . gmres_iterable!, kwargs... )
186
197
IterativeSolversJL_BICGSTAB (args... ;kwargs... ) =
187
- IterativeSolversJL (IterativeSolvers. bicgstabl_iterator!, args ... ; kwargs... )
198
+ IterativeSolversJL (args ... ; generate_iterator = IterativeSolvers. bicgstabl_iterator!, kwargs... )
188
199
IterativeSolversJL_MINRES (args... ;kwargs... ) =
189
- IterativeSolversJL (IterativeSolvers. minres_iterable!, args ... ; kwargs... )
200
+ IterativeSolversJL (args ... ; generate_iterator = IterativeSolvers. minres_iterable!, kwargs... )
190
201
191
202
function init_cacheval (alg:: IterativeSolversJL , A, b, u)
192
203
Pl = (alg. Pl == LinearAlgebra. I) ? IterativeSolvers. Identity () : alg. Pl
@@ -228,10 +239,12 @@ function SciMLBase.solve(cache::LinearCache, alg::IterativeSolversJL; kwargs...)
228
239
cache = set_cacheval (cache, solver)
229
240
end
230
241
231
- for resi in cache. cacheval
232
- # allow for verbose, log
233
- # inject specific code into KSP solve func!(cache.cacheval)
242
+ alg. ifverbose && println (" Using IterativeSolvers.$(alg. generate_iterator) " )
243
+ for iter in enumerate (cache. cacheval)
244
+ alg. ifverbose && println (" Iter: $(iter[1 ]) , residual: $(iter[2 ]) " )
245
+ # inject callbacks KSP into solve cb!(cache.cacheval)
234
246
end
247
+ alg. ifverbose && println ()
235
248
236
249
return cache. u
237
250
end
0 commit comments