Skip to content

Commit

Permalink
Merge branch 'dynamic'
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisRackauckas committed Apr 14, 2018
2 parents bb2036c + 24d0f58 commit 9d45d98
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/SteadyStateDiffEq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module SteadyStateDiffEq

using Reexport
@reexport using DiffEqBase
using NLsolve

using NLsolve, DiffEqCallbacks

using Compat

Expand All @@ -14,6 +14,6 @@ import DiffEqBase: solve
include("algorithms.jl")
include("solve.jl")

export SSRootfind
export SSRootfind, DynamicSS

end # module
7 changes: 7 additions & 0 deletions src/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ struct SSRootfind{F} <: SteadyStateDiffEqAlgorithm
nlsolve::F
end
SSRootfind(;nlsolve=(f,u0,abstol) -> (res=NLsolve.nlsolve(f,u0,ftol = abstol);res.zero)) = SSRootfind(nlsolve)

struct DynamicSS{A,AT,RT} <: SteadyStateDiffEqAlgorithm
alg::A
abstol::AT
reltol::RT
end
DynamicSS(alg;abstol = 1e-8, reltol = 1e-6) = DynamicSS(alg,abstol,reltol)
7 changes: 7 additions & 0 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ function solve(prob::AbstractSteadyStateProblem,alg::SteadyStateDiffEqAlgorithm,
error("Algorithm not recognized")
end
end

function solve(prob::AbstractSteadyStateProblem,alg::DynamicSS,args...;kwargs...)
_prob = ODEProblem(prob.f,prob.u0,(0.0,Inf),prob.p,
mass_matrix=prob.mass_matrix,
jac_prototype=prob.jac_prototype)
solve(_prob,alg.alg,args...;kwargs...,callback=TerminateSteadyState(alg.abstol,alg.reltol))
end
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ u0 = zeros(2)
prob = SteadyStateProblem(f,u0)
abstol = 1e-8
sol = solve(prob,SSRootfind())
p = nothing

du = zeros(2)
f(du,sol.u,nothing,0)
Expand All @@ -25,3 +26,14 @@ f(du,sol.u,nothing,0)
sol = solve(prob,SSRootfind(nlsolve = (f,u0,abstol) -> (res=Sundials.kinsol(f,u0)) ))
f(du,sol.u,nothing,0)
@test du == [0,0]

using OrdinaryDiffEq
sol = solve(prob,DynamicSS(Rodas5()))

f(du,sol.u[end],p,0)
@test du [0,0] atol = 1e-7

sol = solve(prob,DynamicSS(CVODE_BDF()),dt=1.0)

f(du,sol.u[end],p,0)
@test du [0,0] atol = 1e-6

0 comments on commit 9d45d98

Please sign in to comment.