This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
/
DifferentialEquations.jl
161 lines (140 loc) · 3.99 KB
/
DifferentialEquations.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
import DiffEqBase
export DiffEqJLSolver, DiffEqJLIMEXSolver
abstract type AbstractDiffEqJLSolver <: AbstractODESolver end
"""
DiffEqJLSolver(f, RKA, RKB, RKC, Q; dt, t0 = 0)
This is a time stepping object for explicitly time stepping the differential
equation given by the right-hand-side function `f` with the state `Q`, i.e.,
```math
\\dot{Q} = f(Q, t)
```
via a DifferentialEquations.jl DEAlgorithm, which includes support
for OrdinaryDiffEq.jl, Sundials.jl, and more.
"""
mutable struct DiffEqJLSolver{I} <: AbstractDiffEqJLSolver
integ::I
steps::Int
function DiffEqJLSolver(
rhs!,
alg,
Q,
args...;
t0 = 0,
p = nothing,
kwargs...,
)
prob = DiffEqBase.ODEProblem(
(du, u, p, t) -> rhs!(du, u, p, t; increment = false),
Q,
(float(t0), typemax(typeof(float(t0)))),
p,
)
integ = DiffEqBase.init(
prob,
alg,
args...;
adaptive = false,
save_everystep = false,
save_start = false,
save_end = false,
kwargs...,
)
new{typeof(integ)}(integ, 0)
end
end
"""
DiffEqJLSolver(f, RKA, RKB, RKC, Q; dt, t0 = 0)
This is a time stepping object for explicitly time stepping the differential
equation given by the right-hand-side function `f` with the state `Q`, i.e.,
```math
\\dot{Q} = f_I(Q, t) + f_E(Q, t)
```
via a DifferentialEquations.jl DEAlgorithm, which includes support
for OrdinaryDiffEq.jl, Sundials.jl, and more.
"""
mutable struct DiffEqJLIMEXSolver{I} <: AbstractDiffEqJLSolver
integ::I
steps::Int
function DiffEqJLIMEXSolver(
rhs!,
rhs_implicit!,
alg,
Q,
args...;
t0 = 0,
p = nothing,
kwargs...,
)
prob = DiffEqBase.SplitODEProblem(
(du, u, p, t) -> rhs_implicit!(du, u, p, t; increment = false),
(du, u, p, t) -> rhs!(du, u, p, t; increment = false),
Q,
(float(t0), typemax(typeof(float(t0)))),
p,
)
integ = DiffEqBase.init(
prob,
alg,
args...;
adaptive = false,
save_everystep = false,
save_start = false,
save_end = false,
kwargs...,
)
new{typeof(integ)}(integ, 0)
end
end
gettime(solver::AbstractDiffEqJLSolver) = solver.integ.t
getdt(solver::AbstractDiffEqJLSolver) = solver.integ.dt
updatedt!(solver::AbstractDiffEqJLSolver, dt) =
DiffEqBase.set_proposed_dt!(solver.integ, dt)
updatetime!(solver::AbstractDiffEqJLSolver, t) =
DiffEqBase.set_t!(solver.integ, t)
isadjustable(solver::AbstractDiffEqJLSolver) = true # Is this isadaptive? Or something different?
"""
ODESolvers.general_dostep!(Q, solver::AbstractODESolver, p,
timeend::Real, adjustfinalstep::Bool)
Use the solver to step `Q` forward in time from the current time, to the time
`timeend`. If `adjustfinalstep == true` then `dt` is adjusted so that the step
does not take the solution beyond the `timeend`.
"""
function general_dostep!(
Q,
solver::AbstractDiffEqJLSolver,
p,
timeend::Real;
adjustfinalstep::Bool,
)
integ = solver.integ
if first(integ.opts.tstops) !== timeend
DiffEqBase.add_tstop!(integ, timeend)
end
dostep!(Q, solver, p, time)
solver.integ.t
end
function dostep!(
Q,
solver::AbstractDiffEqJLSolver,
p,
time,
slow_δ = nothing,
slow_rv_dQ = nothing,
in_slow_scaling = nothing,
)
integ = solver.integ
integ.p = p # Can this change?
rv_Q = realview(Q)
if integ.u != Q
integ.u .= Q
DiffEqBase.u_modified!(integ, true)
# Will time always be correct?
end
DiffEqBase.step!(integ)
rv_Q .= solver.integ.u
end
function DiffEqJLConstructor(alg)
constructor =
(F, Q; dt = 0, t0 = 0) -> DiffEqJLSolver(F, alg, Q; t0 = t0, dt = dt)
return constructor
end