-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKinsol.jl
271 lines (226 loc) · 8.98 KB
/
Kinsol.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
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
"""
Kinsol
Minimal Julia wrapper for the Sundials kinsol nonlinear system solver <https://computing.llnl.gov/projects/sundials/kinsol>
This closely follows the native C interface, as documented in the Kinsol manual, with conversion to-from native Julia types.
The main user-facing functions are [`Kinsol.kin_create`](@ref) and [`Kinsol.kin_solve`](@ref).
"""
module Kinsol
import Sundials
# import Infiltrator
###########################################################
# Internals: Julia <-> C wrapper functions
###########################################################
# wrapper with Julia user-supplied functions and data
# this is passed as an opaque pointer into the kinsol C code
mutable struct UserFunctionAndData{F1, F2, F3, F4}
func::F1
psetup::F2
psolve::F3
jv::F4
data::Any
end
# Julia adaptor function with C types, passed in to Kinsol C code as a callback
# wraps C types and forwards to the Julia user function
function kinsolfun(
y::Sundials.N_Vector,
fy::Sundials.N_Vector,
userfun::UserFunctionAndData
)
userfun.func(convert(Vector, fy), convert(Vector, y), userfun.data)
return Sundials.KIN_SUCCESS
end
function kinprecsetup(
u::Sundials.N_Vector,
uscale::Sundials.N_Vector,
fval::Sundials.N_Vector,
fscale::Sundials.N_Vector,
userfun::UserFunctionAndData
)
retval = userfun.psetup(
convert(Vector, u),
convert(Vector, uscale),
convert(Vector, fval),
convert(Vector, fscale),
userfun.data
)
return Cint(retval)
end
function kinprecsolve(
u::Sundials.N_Vector,
uscale::Sundials.N_Vector,
fval::Sundials.N_Vector,
fscale::Sundials.N_Vector,
v::Sundials.N_Vector,
userfun::UserFunctionAndData
)
retval = userfun.psolve(
convert(Vector, u),
convert(Vector, uscale),
convert(Vector, fval),
convert(Vector, fscale),
convert(Vector, v), userfun.data
)
return Cint(retval)
end
function kinjactimesvec(
v::Sundials.N_Vector,
Jv::Sundials.N_Vector,
u::Sundials.N_Vector,
new_u::Ptr{Cint},
userfun::UserFunctionAndData
)
retval = userfun.jv(
convert(Vector, v),
convert(Vector, Jv),
convert(Vector, u),
unsafe_wrap(Array, new_u, (1, )),
userfun.data
)
return Cint(retval)
end
"""
kin_create(f, y0 [; kwargs...]) -> kin
Create and return a kinsol solver context `kin`, which can then be passed to [`kin_solve`](@ref)
# Arguments
- `f`: Function of form f(fy::Vector{Float64}, y::Vector{Float64}, userdata)
- `y0::Vector` template Vector of initial values (used only to define problem dimension)
# Keywords
- `userdata`: optional user data, passed through to `f` etc.
- `linear_solver`: linear solver to use (only partially implemented, supports :Dense, :Band, :FGMRES)
- `psolvefun`: optional preconditioner solver function (for :FGMRES)
- `psetupfun`: optional preconditioner setup function
- `jvfun`: optional Jacobian*vector function (for :FGMRES)
"""
function kin_create(
f, y0::Vector{Float64};
userdata::Any = nothing,
linear_solver = :Dense,
jac_upper = 0,
jac_lower = 0,
krylov_dim = 0,
psetupfun = nothing,
psolvefun = nothing,
jvfun = nothing,
)
mem_ptr = Sundials.KINCreate()
(mem_ptr == C_NULL) && error("Failed to allocate KINSOL solver object")
kmem = Sundials.Handle(mem_ptr)
handles = []
# use the user_data field to pass a function
# see: https://github.com/JuliaLang/julia/issues/2554
userfun = UserFunctionAndData(f, psetupfun, psolvefun, jvfun, userdata)
# push!(handles, userfun) # TODO prevent userfun from being garbage collected ?
function getkinsolfun(userfun::T) where {T}
@cfunction(kinsolfun, Cint, (Sundials.N_Vector, Sundials.N_Vector, Ref{T}))
end
function getpsetupfun(userfun::T) where {T}
@cfunction(kinprecsetup, Cint, (Sundials.N_Vector, Sundials.N_Vector, Sundials.N_Vector, Sundials.N_Vector, Ref{T}))
end
function getpsolvefun(userfun::T) where {T}
@cfunction(kinprecsolve, Cint, (Sundials.N_Vector, Sundials.N_Vector, Sundials.N_Vector, Sundials.N_Vector, Sundials.N_Vector, Ref{T}))
end
function getkinjactimesvec(userfun::T) where {T}
@cfunction(kinjactimesvec, Cint, (Sundials.N_Vector, Sundials.N_Vector, Sundials.N_Vector, Ptr{Cint}, Ref{T}))
end
flag = Sundials.@checkflag Sundials.KINInit(kmem, getkinsolfun(userfun), Sundials.NVector(y0)) true
if linear_solver == :Dense
A = Sundials.SUNDenseMatrix(length(y0), length(y0))
push!(handles, Sundials.MatrixHandle(A, Sundials.DenseMatrix()))
LS = Sundials.SUNLinSol_Dense(y0, A)
push!(handles, Sundials.LinSolHandle(LS, Sundials.Dense()))
elseif linear_solver == :Band
A = Sundials.SUNBandMatrix(length(y0), jac_upper, jac_lower)
push!(handles, Sundials.MatrixHandle(A, Sundials.BandMatrix()))
LS = Sundials.SUNLinSol_Band(y0, A)
push!(handles, Sundials.LinSolHandle(LS, Sundials.Band()))
elseif linear_solver == :FGMRES
A = nothing
prec_side = isnothing(psolvefun) ? 0 : 2 # right preconditioning only
LS = Sundials.SUNLinSol_SPFGMR(y0, prec_side, krylov_dim)
push!(handles, Sundials.LinSolHandle(LS, Sundials.SPFGMR()))
end
flag = Sundials.@checkflag Sundials.KINSetUserData(kmem, userfun) true
flag = Sundials.@checkflag Sundials.KINSetLinearSolver(kmem, LS, A === nothing ? C_NULL : A) true
# flag = Sundials.@checkflag Sundials.KINDlsSetLinearSolver(kmem, LS, A === nothing ? C_NULL : A) true
if !isnothing(psolvefun)
flag = Sundials.@checkflag Sundials.KINSetPreconditioner(kmem,
psetupfun === nothing ? C_NULL : getpsetupfun(userfun),
getpsolvefun(userfun)) true
end
if !isnothing(jvfun)
flag = Sundials.@checkflag Sundials.KINSetJacTimesVecFn(kmem, getkinjactimesvec(userfun)) true
end
return (;kmem, handles)
end
"""
kin_solve(
kin, y0::Vector;
[strategy] [, fnormtol] [, mxiter] [, print_level] [,y_scale] [, f_scale] [, noInitSetup]
) -> (y, kin_stats)
Solve nonlinear system using kinsol solver context `kin` (created by [`kin_create`](@ref)) and initial conditions `y0`.
Returns solution `y` and solver statistics `kinstats`. `kinstats.returnflag` indicates success/failure.
"""
function kin_solve(
kin, y0::Vector{Float64};
strategy = Sundials.KIN_NONE,
fnormtol::Float64 = 0.0,
mxiter = 200,
print_level = 0,
y_scale = ones(length(y0)),
f_scale = ones(length(y0)),
noInitSetup = false,
)
y = copy(y0)
kmem = kin.kmem
flag = Sundials.@checkflag Sundials.KINSetPrintLevel(kmem, print_level) true
flag = Sundials.@checkflag Sundials.KINSetFuncNormTol(kmem, fnormtol) true
flag = Sundials.@checkflag Sundials.KINSetNumMaxIters(kmem, mxiter) true
flag = Sundials.@checkflag Sundials.KINSetNoInitSetup(kmem, noInitSetup) true
## Solve problem
returnflag = Sundials.KINSol(kmem, y, strategy, y_scale, f_scale)
## Get stats
nfevals = [0]
flag = Sundials.@checkflag Sundials.KINGetNumFuncEvals(kmem, nfevals)
nniters = [0]
flag = Sundials.@checkflag Sundials.KINGetNumNonlinSolvIters(kmem, nniters)
nbcfails = [0]
flag = Sundials.@checkflag Sundials.KINGetNumBetaCondFails(kmem, nbcfails)
nbacktr = [0]
flag = Sundials.@checkflag Sundials.KINGetNumBacktrackOps(kmem, nbacktr)
fnorm = [NaN]
flag = Sundials.@checkflag Sundials.KINGetFuncNorm(kmem, fnorm)
steplength = [NaN]
flag = Sundials.@checkflag Sundials.KINGetStepLength(kmem, steplength)
njevals = [0]
flag = Sundials.@checkflag Sundials.KINGetNumJacEvals(kmem, njevals)
nfevalsLS = [0]
flag = Sundials.@checkflag Sundials.KINGetNumLinFuncEvals(kmem, nfevalsLS)
nliters = [0]
flag = Sundials.@checkflag Sundials.KINGetNumLinIters(kmem, nliters)
nlcfails = [0]
flag = Sundials.@checkflag Sundials.KINGetNumLinConvFails(kmem, nlcfails)
npevals = [0]
flag = Sundials.@checkflag Sundials.KINGetNumPrecEvals(kmem, npevals)
npsolves = [0]
flag = Sundials.@checkflag Sundials.KINGetNumPrecSolves(kmem, npsolves)
njvevals = [0]
flag = Sundials.@checkflag Sundials.KINGetNumJtimesEvals(kmem, njvevals)
kinstats = (
ReturnFlag = returnflag,
NumFuncEvals = nfevals[],
NumNonlinSolvIters = nniters[],
NumBetaCondFails = nbcfails[],
NumBacktrackOps = nbacktr[],
FuncNorm = fnorm[],
StepLength = steplength[],
NumJacEvals = njevals[],
NumLinFuncEvals = nfevalsLS[],
NumLinIters = nliters[],
NumLinConvFails = nlcfails[],
NumPrecEvals = npevals[],
NumPrecSolves = npsolves[],
NumJtimesEvals = njvevals[],
)
return (y, kinstats)
end
end # module