Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DiffEqDiffTools API changes #224

Merged
merged 2 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/derivative_wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,23 @@ mutable struct UDerivativeWrapper{F,tType} <: Function
t::tType
end
(p::UDerivativeWrapper)(u) = p.f(p.t,u)

function derivative!(df::AbstractArray{<:Number}, f, x::Union{Number,AbstractArray{<:Number}}, fx::AbstractArray{<:Number}, integrator::DEIntegrator)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really clean way to handle all of this. Nice.

if alg_autodiff(integrator.alg)
ForwardDiff.derivative!(df, f, fx, x)
else
RealOrComplex = eltype(integrator.u) <: Complex ? Val{:Complex} : Val{:Real}
DiffEqDiffTools.finite_difference!(df, f, x, integrator.alg.diff_type, RealOrComplex, Val{:DiffEqDerivativeWrapper}, fx)
end
nothing
end

function jacobian!(J::AbstractMatrix{<:Number}, f, x::AbstractArray{<:Number}, fx::AbstractArray{<:Number}, integrator::DEIntegrator, jac_config)
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J, f, fx, x, jac_config)
else
RealOrComplex = eltype(integrator.u) <: Complex ? Val{:Complex} : Val{:Real}
DiffEqDiffTools.finite_difference_jacobian!(J, f, x, integrator.alg.diff_type, RealOrComplex, Val{:JacobianWrapper}, fx)
end
nothing
end
84 changes: 14 additions & 70 deletions src/integrators/rosenbrock_integrators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ end
else
tf.vf.sizeu = sizeu
tf.uprev = uprev
if alg_autodiff(integrator.alg)
ForwardDiff.derivative!(dT, tf, vec(du2), t)
else
DiffEqDiffTools.finite_difference!(dT, tf, t, integrator.alg.diff_type, vec(du2), Val{:DiffEqDerivativeWrapper})
end
derivative!(dT, tf, t, vec(du2), integrator)
end
end

Expand All @@ -52,11 +48,7 @@ end
else
uf.vfr.sizeu = sizeu
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J, uf, vec(du1), vec(uprev), jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), integrator.alg.diff_type, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
for j in 1:length(u), i in 1:length(u)
@inbounds W[i,j] = mass_matrix[i,j] - γ*J[i,j]
Expand Down Expand Up @@ -146,11 +138,7 @@ end
else
tf.vf.sizeu = sizeu
tf.uprev = uprev
if alg_autodiff(integrator.alg)
ForwardDiff.derivative!(dT, tf, vec(du2), t)
else
DiffEqDiffTools.finite_difference!(dT, tf, t, integrator.alg.diff_type, vec(du2), Val{:DiffEqDerivativeWrapper})
end
derivative!(dT, tf, t, vec(du2), integrator)
end
end

Expand All @@ -168,11 +156,7 @@ end
else
uf.vfr.sizeu = sizeu
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J, uf, vec(du1), vec(uprev), jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), integrator.alg.diff_type, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
for j in 1:length(u), i in 1:length(u)
@inbounds W[i,j] = mass_matrix[i,j] - γ*J[i,j]
Expand Down Expand Up @@ -453,11 +437,7 @@ end
else
tf.vf.sizeu = sizeu
tf.uprev = uprev
if alg_autodiff(integrator.alg)
ForwardDiff.derivative!(dT, tf, vec(du2), t)
else
DiffEqDiffTools.finite_difference!(dT, tf, t, integrator.alg.diff_type, vec(du2), Val{:DiffEqDerivativeWrapper})
end
derivative!(dT, tf, t, vec(du2), integrator)
end
end

Expand All @@ -476,11 +456,7 @@ end
else
uf.vfr.sizeu = sizeu
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J, uf, vec(du1), vec(uprev), jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), integrator.alg.diff_type, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
for j in 1:length(u), i in 1:length(u)
@inbounds W[i,j] = mass_matrix[i,j]/dtgamma - J[i,j]
Expand Down Expand Up @@ -661,11 +637,7 @@ end
else
tf.vf.sizeu = sizeu
tf.uprev = uprev
if alg_autodiff(integrator.alg)
ForwardDiff.derivative!(dT, tf, vec(du2), t)
else
DiffEqDiffTools.finite_difference!(dT, tf, t, integrator.alg.diff_type, vec(du2), Val{:DiffEqDerivativeWrapper})
end
derivative!(dT, tf, t, vec(du2), integrator)
end
end

Expand All @@ -683,11 +655,7 @@ end
else
uf.vfr.sizeu = sizeu
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J, uf, vec(du1), vec(uprev), jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), integrator.alg.diff_type, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
for j in 1:length(u), i in 1:length(u)
@inbounds W[i,j] = mass_matrix[i,j]/dtgamma - J[i,j]
Expand Down Expand Up @@ -893,11 +861,7 @@ end
else
tf.vf.sizeu = sizeu
tf.uprev = uprev
if alg_autodiff(integrator.alg)
ForwardDiff.derivative!(dT, tf, vec(du2), t)
else
DiffEqDiffTools.finite_difference!(dT, tf, t, integrator.alg.diff_type, vec(du2), Val{:DiffEqDerivativeWrapper})
end
derivative!(dT, tf, t, vec(du2), integrator)
end
end

Expand All @@ -916,11 +880,7 @@ end
else
uf.vfr.sizeu = sizeu
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J, uf, vec(du1), vec(uprev), jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), integrator.alg.diff_type, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
for j in 1:length(u), i in 1:length(u)
@inbounds W[i,j] = mass_matrix[i,j]/dtgamma - J[i,j]
Expand Down Expand Up @@ -1149,11 +1109,7 @@ end
else
tf.vf.sizeu = sizeu
tf.uprev = uprev
if alg_autodiff(integrator.alg)
ForwardDiff.derivative!(dT, tf, vec(du2), t)
else
DiffEqDiffTools.finite_difference!(dT, tf, t, integrator.alg.diff_type, vec(du2), Val{:DiffEqDerivativeWrapper})
end
derivative!(dT, tf, t, vec(du2), integrator)
end
end

Expand All @@ -1174,11 +1130,7 @@ end
else
uf.vfr.sizeu = sizeu
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J, uf, vec(du1), vec(uprev), jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), integrator.alg.diff_type, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
for j in 1:length(u), i in 1:length(u)
@inbounds W[i,j] = mass_matrix[i,j]/dtgamma - J[i,j]
Expand Down Expand Up @@ -1534,11 +1486,7 @@ end
else
tf.vf.sizeu = sizeu
tf.uprev = uprev
if alg_autodiff(integrator.alg)
ForwardDiff.derivative!(dT, tf, vec(du2), t)
else
DiffEqDiffTools.finite_difference!(dT, tf, t, integrator.alg.diff_type, vec(du2), Val{:DiffEqDerivativeWrapper})
end
derivative!(dT, tf, t, vec(du2), integrator)
end
end

Expand All @@ -1559,11 +1507,7 @@ end
else
uf.vfr.sizeu = sizeu
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J, uf, vec(du1), vec(uprev), jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), integrator.alg.diff_type, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
for j in 1:length(u), i in 1:length(u)
@inbounds W[i,j] = mass_matrix[i,j]/dtgamma - J[i,j]
Expand Down
72 changes: 12 additions & 60 deletions src/integrators/sdirk_integrators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,7 @@ end
f(Val{:jac},t,uprev,J)
else
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J,uf,vec(du1),vec(uprev),jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), Val{integrator.alg.diff_type}, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
end
# skip calculation of W if step is repeated
Expand Down Expand Up @@ -361,11 +357,7 @@ end
f(Val{:jac},t,uprev,J)
else
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J,uf,vec(du1),vec(uprev),jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), Val{integrator.alg.diff_type}, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
end
# skip calculation of W if step is repeated
Expand Down Expand Up @@ -908,11 +900,7 @@ end
f(Val{:jac},t,uprev,J)
else
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J,uf,vec(du1),vec(uprev),jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), Val{integrator.alg.diff_type}, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
end
# skip calculation of W if step is repeated
Expand Down Expand Up @@ -1231,11 +1219,7 @@ end
f(Val{:jac},t,uprev,J)
else
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J,uf,vec(du1),vec(uprev),jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), Val{integrator.alg.diff_type}, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
end
# skip calculation of W if step is repeated
Expand Down Expand Up @@ -1554,11 +1538,7 @@ end
f(Val{:jac},t,uprev,J)
else
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J,uf,vec(du1),vec(uprev),jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), Val{integrator.alg.diff_type}, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
end
# skip calculation of W if step is repeated
Expand Down Expand Up @@ -1904,11 +1884,7 @@ end
f(Val{:jac},t,uprev,J)
else
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J,uf,vec(du1),vec(uprev),jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), Val{integrator.alg.diff_type}, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
end
# skip calculation of W if step is repeated
Expand Down Expand Up @@ -2417,11 +2393,7 @@ end
f(Val{:jac},t,uprev,J)
else
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J,uf,vec(du1),vec(uprev),jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), Val{integrator.alg.diff_type}, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
end
# skip calculation of W if step is repeated
Expand Down Expand Up @@ -3025,11 +2997,7 @@ end
f(Val{:jac},t,uprev,J)
else
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J,uf,vec(du1),vec(uprev),jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), Val{integrator.alg.diff_type}, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
end
# skip calculation of W if step is repeated
Expand Down Expand Up @@ -3600,11 +3568,7 @@ end
f(Val{:jac},t,uprev,J)
else
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J,uf,vec(du1),vec(uprev),jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), Val{integrator.alg.diff_type}, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
end
# skip calculation of W if step is repeated
Expand Down Expand Up @@ -4157,11 +4121,7 @@ end
f(Val{:jac},t,uprev,J)
else
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J,uf,vec(du1),vec(uprev),jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), Val{integrator.alg.diff_type}, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
end
# skip calculation of W if step is repeated
Expand Down Expand Up @@ -4812,11 +4772,7 @@ end
f(Val{:jac},t,uprev,J)
else
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J,uf,vec(du1),vec(uprev),jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), Val{integrator.alg.diff_type}, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
end
# skip calculation of W if step is repeated
Expand Down Expand Up @@ -5565,11 +5521,7 @@ end
f(Val{:jac},t,uprev,J)
else
uf.t = t
if alg_autodiff(integrator.alg)
ForwardDiff.jacobian!(J,uf,vec(du1),vec(uprev),jac_config)
else
DiffEqDiffTools.finite_difference_jacobian!(J, uf, vec(uprev), Val{integrator.alg.diff_type}, vec(du1), Val{:JacobianWrapper})
end
jacobian!(J, uf, vec(uprev), vec(du1), integrator, jac_config)
end
end
# skip calculation of W if step is repeated
Expand Down