Skip to content

Commit a1a52b0

Browse files
committed
basic implementation working
1 parent 7c83d1c commit a1a52b0

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

lib/ImplicitDiscreteSolve/src/ImplicitDiscreteSolve.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using SymbolicIndexingInterface
77
using LinearAlgebra
88
using ADTypes
99
using UnPack
10-
import OrdinaryDiffEqCore: OrdinaryDiffEqAlgorithm, alg_cache, OrdinaryDiffEqMutableCache, OrdinaryDiffEqConstantCache, get_fsalfirstlast, initialize!, perform_step!
10+
import OrdinaryDiffEqCore: OrdinaryDiffEqAlgorithm, alg_cache, OrdinaryDiffEqMutableCache, OrdinaryDiffEqConstantCache, get_fsalfirstlast, isfsal, initialize!, perform_step!, isdiscretecache, isdiscretealg, alg_order, beta2_default, beta1_default, dt_required
1111
import CommonSolve
1212
import DifferentiationInterface as DI
1313

@@ -25,6 +25,7 @@ end
2525

2626
include("cache.jl")
2727
include("solve.jl")
28+
include("alg_utils.jl")
2829

2930
export IDSolve
3031

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function SciMLBase.isautodifferentiable(alg::IDSolve)
2+
true
3+
end
4+
function SciMLBase.allows_arbitrary_number_types(alg::IDSolve)
5+
true
6+
end
7+
function SciMLBase.allowscomplex(alg::IDSolve)
8+
true
9+
end
10+
11+
SciMLBase.isdiscrete(alg::IDSolve) = true
12+
13+
isfsal(alg::IDSolve) = false
14+
alg_order(alg::IDSolve) = 0
15+
beta2_default(alg::IDSolve) = 0
16+
beta1_default(alg::IDSolve, beta2) = 0
17+
18+
dt_required(alg::IDSolve) = false
19+
isdiscretealg(alg::IDSolve) = true

lib/ImplicitDiscreteSolve/src/solve.jl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ function perform_step!(integrator, cache::IDSolveCache, repeat_step = false)
55
@unpack state, prob = cache
66
state.u .= uprev
77
state.t_next = t
8-
@show state
98
prob = remake(prob, p = state)
109

1110
u = solve(prob, nlsolve)
@@ -32,15 +31,3 @@ function initialize!(integrator, cache::IDSolveCache)
3231
end
3332
cache.prob = prob
3433
end
35-
36-
#### unnecessary
37-
#function DiffEqBase.__init(prob::ImplicitDiscreteProblem, alg)
38-
# f = prob.f
39-
# t_i = prob.tspan[1]
40-
# u0 = state_values(prob)
41-
# p = parameter_values(prob)
42-
#
43-
# _f(resid, u_next, p) = f(resid, u_next, p.u, p.p, p.t)
44-
# state = ImplicitDiscreteState(u0, p, t_i)
45-
# nlprob = NonlinearProblem(_f, u0, state)
46-
#end

lib/ImplicitDiscreteSolve/test/runtests.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ using SimpleNonlinearSolve
66

77
# Test implicit Euler using ImplicitDiscreteProblem
88
@testset "Implicit Discrete System" begin
9-
function lotkavolterra(u, p, t)
9+
function lotkavolterra(u, p, t)
1010
[1.5*u[1] - u[1]*u[2], -3.0*u[2] + u[1]*u[2]]
1111
end
1212

1313
function f!(resid, u_next, u, p, t)
14-
@. resid = u_next - 0.01*lotkavolterra(u_next, p, t)
14+
lv = lotkavolterra(u_next, p, t)
15+
resid[1] = u_next[1] - u[1] - 0.01*lv[1]
16+
resid[2] = u_next[2] - u[2] - 0.01*lv[2]
17+
nothing
1518
end
1619
u0 = [1., 1.]
17-
tspan = (0., 1.)
20+
tspan = (0., 0.5)
1821

1922
idprob = ImplicitDiscreteProblem(f!, u0, tspan, []; dt = 0.01)
2023
idsol = solve(idprob, IDSolve(SimpleNewtonRaphson()))
2124

2225
oprob = ODEProblem(lotkavolterra, u0, tspan)
2326
osol = solve(oprob, ImplicitEuler())
2427

25-
@test idsol[end] osol[end]
28+
@test isapprox(idsol[end], osol[end], atol = 0.01)
2629
end

0 commit comments

Comments
 (0)