-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from SciML/sparsedifftools_support
Setup with SparseDiffTools
- Loading branch information
Showing
8 changed files
with
222 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,144 @@ | ||
mutable struct JacobianWrapper{fType, pType} | ||
struct JacobianWrapper{fType, pType} | ||
f::fType | ||
p::pType | ||
end | ||
|
||
(uf::JacobianWrapper)(u) = uf.f(u, uf.p) | ||
(uf::JacobianWrapper)(res, u) = uf.f(res, u, uf.p) | ||
|
||
struct ImmutableJacobianWrapper{fType, pType} | ||
f::fType | ||
p::pType | ||
end | ||
struct NonlinearSolveTag end | ||
|
||
(uf::ImmutableJacobianWrapper)(u) = uf.f(u, uf.p) | ||
|
||
function calc_J(solver, cache) | ||
@unpack u, f, p, alg = solver | ||
@unpack uf = cache | ||
uf.f = f | ||
uf.p = p | ||
J = jacobian(uf, u, solver) | ||
return J | ||
function sparsity_colorvec(f, x) | ||
sparsity = f.sparsity | ||
colorvec = DiffEqBase.has_colorvec(f) ? f.colorvec : | ||
(isnothing(sparsity) ? (1:length(x)) : matrix_colors(sparsity)) | ||
sparsity, colorvec | ||
end | ||
|
||
function calc_J(solver, uf::ImmutableJacobianWrapper) | ||
@unpack u, f, p, alg = solver | ||
J = jacobian(uf, u, solver) | ||
return J | ||
function jacobian_finitediff_forward!(J, f, x, jac_config, forwardcache, cache) | ||
(FiniteDiff.finite_difference_jacobian!(J, f, x, jac_config, forwardcache); | ||
maximum(jac_config.colorvec)) | ||
end | ||
function jacobian_finitediff!(J, f, x, jac_config, cache) | ||
(FiniteDiff.finite_difference_jacobian!(J, f, x, jac_config); | ||
2 * maximum(jac_config.colorvec)) | ||
end | ||
|
||
function jacobian(f, x::Number, solver) | ||
if alg_autodiff(solver.alg) | ||
J = ForwardDiff.derivative(f, x) | ||
function jacobian!(J::AbstractMatrix{<:Number}, cache) | ||
f = cache.f | ||
uf = cache.uf | ||
x = cache.u | ||
fx = cache.fu | ||
jac_config = cache.jac_config | ||
alg = cache.alg | ||
|
||
if alg_autodiff(alg) | ||
forwarddiff_color_jacobian!(J, uf, x, jac_config) | ||
#cache.destats.nf += 1 | ||
else | ||
J = FiniteDiff.finite_difference_derivative(f, x, alg_difftype(solver.alg), | ||
eltype(x)) | ||
isforward = alg_difftype(alg) === Val{:forward} | ||
if isforward | ||
uf(fx, x) | ||
#cache.destats.nf += 1 | ||
tmp = jacobian_finitediff_forward!(J, uf, x, jac_config, fx, | ||
cache) | ||
else # not forward difference | ||
tmp = jacobian_finitediff!(J, uf, x, jac_config, cache) | ||
end | ||
#cache.destats.nf += tmp | ||
end | ||
return J | ||
nothing | ||
end | ||
|
||
function jacobian(f, x, solver) | ||
if alg_autodiff(solver.alg) | ||
J = ForwardDiff.jacobian(f, x) | ||
function build_jac_config(alg, f::F1, uf::F2, du1, u, tmp, du2) where {F1, F2} | ||
haslinsolve = hasfield(typeof(alg), :linsolve) | ||
|
||
if !SciMLBase.has_jac(f) && # No Jacobian if has analytical solution | ||
((concrete_jac(alg) === nothing && (!haslinsolve || (haslinsolve && # No Jacobian if linsolve doesn't want it | ||
(alg.linsolve === nothing || LinearSolve.needs_concrete_A(alg.linsolve))))) || | ||
(concrete_jac(alg) !== nothing && concrete_jac(alg))) # Jacobian if explicitly asked for | ||
jac_prototype = f.jac_prototype | ||
sparsity, colorvec = sparsity_colorvec(f, u) | ||
|
||
if alg_autodiff(alg) | ||
_chunksize = get_chunksize(alg) === Val(0) ? nothing : get_chunksize(alg) # SparseDiffEq uses different convection... | ||
|
||
T = if standardtag(alg) | ||
typeof(ForwardDiff.Tag(NonlinearSolveTag(), eltype(u))) | ||
else | ||
typeof(ForwardDiff.Tag(uf, eltype(u))) | ||
end | ||
jac_config = ForwardColorJacCache(uf, u, _chunksize; colorvec = colorvec, | ||
sparsity = sparsity, tag = T) | ||
else | ||
if alg_difftype(alg) !== Val{:complex} | ||
jac_config = FiniteDiff.JacobianCache(tmp, du1, du2, alg_difftype(alg), | ||
colorvec = colorvec, | ||
sparsity = sparsity) | ||
else | ||
jac_config = FiniteDiff.JacobianCache(Complex{eltype(tmp)}.(tmp), | ||
Complex{eltype(du1)}.(du1), nothing, | ||
alg_difftype(alg), eltype(u), | ||
colorvec = colorvec, | ||
sparsity = sparsity) | ||
end | ||
end | ||
else | ||
J = FiniteDiff.finite_difference_jacobian(f, x, alg_difftype(solver.alg), eltype(x)) | ||
jac_config = nothing | ||
end | ||
return J | ||
jac_config | ||
end | ||
|
||
function calc_J!(J, solver, cache) | ||
@unpack f, u, p, alg = solver | ||
@unpack du1, uf, jac_config = cache | ||
function get_chunksize(jac_config::ForwardDiff.JacobianConfig{T, V, N, D}) where {T, V, N, D | ||
} | ||
Val(N) | ||
end # don't degrade compile time information to runtime information | ||
|
||
uf.f = f | ||
uf.p = p | ||
|
||
jacobian!(J, uf, u, du1, solver, jac_config) | ||
function jacobian_finitediff(f, x, ::Type{diff_type}, dir, colorvec, sparsity, | ||
jac_prototype) where {diff_type} | ||
(FiniteDiff.finite_difference_derivative(f, x, diff_type, eltype(x), dir = dir), 2) | ||
end | ||
function jacobian_finitediff(f, x::AbstractArray, ::Type{diff_type}, dir, colorvec, | ||
sparsity, jac_prototype) where {diff_type} | ||
f_in = diff_type === Val{:forward} ? f(x) : similar(x) | ||
ret_eltype = eltype(f_in) | ||
J = FiniteDiff.finite_difference_jacobian(f, x, diff_type, ret_eltype, f_in, | ||
dir = dir, colorvec = colorvec, | ||
sparsity = sparsity, | ||
jac_prototype = jac_prototype) | ||
return J, _nfcount(maximum(colorvec), diff_type) | ||
end | ||
function jacobian(cache, f::F) where {F} | ||
x = cache.u | ||
alg = cache.alg | ||
uf = cache.uf | ||
local tmp | ||
|
||
function jacobian!(J, f, x, fx, solver, jac_config) | ||
alg = solver.alg | ||
if alg_autodiff(alg) | ||
ForwardDiff.jacobian!(J, f, fx, x, jac_config) | ||
if DiffEqBase.has_jac(cache.f) | ||
J = f.jac(cache.u, cache.p) | ||
elseif alg_autodiff(alg) | ||
J, tmp = jacobian_autodiff(uf, x, cache.f, alg) | ||
else | ||
FiniteDiff.finite_difference_jacobian!(J, f, x, jac_config) | ||
jac_prototype = cache.f.jac_prototype | ||
sparsity, colorvec = sparsity_colorvec(cache.f, x) | ||
dir = true | ||
J, tmp = jacobian_finitediff(uf, x, alg_difftype(alg), dir, colorvec, sparsity, | ||
jac_prototype) | ||
end | ||
nothing | ||
J | ||
end | ||
|
||
jacobian_autodiff(f, x, nonlinfun, alg) = (ForwardDiff.derivative(f, x), 1, alg) | ||
function jacobian_autodiff(f, x::AbstractArray, nonlinfun, alg) | ||
jac_prototype = nonlinfun.jac_prototype | ||
sparsity, colorvec = sparsity_colorvec(nonlinfun, x) | ||
maxcolor = maximum(colorvec) | ||
chunk_size = get_chunksize(alg) === Val(0) ? nothing : get_chunksize(alg) | ||
num_of_chunks = chunk_size === nothing ? | ||
Int(ceil(maxcolor / | ||
SparseDiffTools.getsize(ForwardDiff.pickchunksize(maxcolor)))) : | ||
Int(ceil(maxcolor / _unwrap_val(chunk_size))) | ||
(forwarddiff_color_jacobian(f, x, colorvec = colorvec, sparsity = sparsity, | ||
jac_prototype = jac_prototype, chunksize = chunk_size), | ||
num_of_chunks) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using NonlinearSolve, LinearAlgebra, SparseArrays, Symbolics | ||
|
||
const N = 32 | ||
const xyd_brusselator = range(0, stop = 1, length = N) | ||
brusselator_f(x, y) = (((x - 0.3)^2 + (y - 0.6)^2) <= 0.1^2) * 5.0 | ||
limit(a, N) = a == N + 1 ? 1 : a == 0 ? N : a | ||
function brusselator_2d_loop(du, u, p) | ||
A, B, alpha, dx = p | ||
alpha = alpha / dx^2 | ||
@inbounds for I in CartesianIndices((N, N)) | ||
i, j = Tuple(I) | ||
x, y = xyd_brusselator[I[1]], xyd_brusselator[I[2]] | ||
ip1, im1, jp1, jm1 = limit(i + 1, N), limit(i - 1, N), limit(j + 1, N), | ||
limit(j - 1, N) | ||
du[i, j, 1] = alpha * (u[im1, j, 1] + u[ip1, j, 1] + u[i, jp1, 1] + u[i, jm1, 1] - | ||
4u[i, j, 1]) + | ||
B + u[i, j, 1]^2 * u[i, j, 2] - (A + 1) * u[i, j, 1] + | ||
brusselator_f(x, y) | ||
du[i, j, 2] = alpha * (u[im1, j, 2] + u[ip1, j, 2] + u[i, jp1, 2] + u[i, jm1, 2] - | ||
4u[i, j, 2]) + | ||
A * u[i, j, 1] - u[i, j, 1]^2 * u[i, j, 2] | ||
end | ||
end | ||
p = (3.4, 1.0, 10.0, step(xyd_brusselator)) | ||
|
||
function init_brusselator_2d(xyd) | ||
N = length(xyd) | ||
u = zeros(N, N, 2) | ||
for I in CartesianIndices((N, N)) | ||
x = xyd[I[1]] | ||
y = xyd[I[2]] | ||
u[I, 1] = 22 * (y * (1 - y))^(3 / 2) | ||
u[I, 2] = 27 * (x * (1 - x))^(3 / 2) | ||
end | ||
u | ||
end | ||
u0 = init_brusselator_2d(xyd_brusselator) | ||
prob_brusselator_2d = NonlinearProblem(brusselator_2d_loop, u0, p) | ||
sol = solve(prob_brusselator_2d, NewtonRaphson()) | ||
|
||
du0 = copy(u0) | ||
jac_sparsity = Symbolics.jacobian_sparsity((du, u) -> brusselator_2d_loop(du, u, p), du0, | ||
u0) | ||
|
||
ff = NonlinearFunction(brusselator_2d_loop; jac_prototype = float.(jac_sparsity)) | ||
prob_brusselator_2d = NonlinearProblem(ff, u0, p) | ||
sol = solve(prob_brusselator_2d, NewtonRaphson()) | ||
@test norm(sol.resid) < 1e-8 | ||
|
||
sol = solve(prob_brusselator_2d, NewtonRaphson(autodiff = false)) | ||
@test norm(sol.resid) < 1e-6 | ||
|
||
cache = init(prob_brusselator_2d, NewtonRaphson()) | ||
@test maximum(cache.jac_config.colorvec) == 12 |