-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkinsol_prec.jl
105 lines (75 loc) · 2.42 KB
/
kinsol_prec.jl
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
import Infiltrator
function f!(resid, x, userdata=nothing)
for i in eachindex(x)
resid[i] = sin(x[i]) + x[i]^3
end
end
x = ones(5)
kin = PALEOmodel.Kinsol.kin_create(
f!, x,
linear_solver = :FGMRES,
krylov_dim=20)
y, kinstats = PALEOmodel.Kinsol.kin_solve(
kin, x,
fnormtol=1e-8)
@test isapprox(y, zeros(5), atol=2e-8)
@info "FGMRES: kinstats = $kinstats"
function psetup(u, uscale,
fval, fscale,
data)
# println("psetup data=$data")
return 0
end
function psolve(u, uscale,
fval, fscale,
v, data)
# println("psolve data=$data v=$v")
v .= v
return 0
end
kin = PALEOmodel.Kinsol.kin_create(
f!, x,
linear_solver = :FGMRES,
krylov_dim=20,
psetupfun=psetup,
psolvefun=psolve,
userdata="hello")
y, kinstats = PALEOmodel.Kinsol.kin_solve(
kin, x,
fnormtol=1e-8)
@test isapprox(y, zeros(5), atol=2e-8)
@info "FGMRES identity prec: kinstats = $kinstats"
function jv(v, Jv,
u, new_u,
data)
# @Infiltrator.infiltrate
# println("jv data=$data v=$v")
r1 = similar(v)
r2 = similar(v)
sigma = 1e-6
f!(r2, u + sigma.*v, data)
f!(r1, u, data)
Jv .= (r2 .- r1)./sigma
# @Infiltrator.infiltrate
return 0 # Success
end
kin = PALEOmodel.Kinsol.kin_create(
f!, x,
linear_solver = :FGMRES,
krylov_dim=20,
# psetupfun=psetup,
psolvefun=psolve,
jvfun = jv,
userdata="hello")
y, kinstats = PALEOmodel.Kinsol.kin_solve(
kin, x,
fnormtol=1e-8)
@test isapprox(y, zeros(5), atol=2e-8)
# test reuse of kin instance
y, kinstats = PALEOmodel.Kinsol.kin_solve(
kin, x,
fnormtol=1e-8,
print_level = 1,
mxiter=10)
@test isapprox(y, zeros(5), atol=2e-8)
@info "FGMRES identity prec jv: kinstats = $kinstats"