-
Notifications
You must be signed in to change notification settings - Fork 98
/
ODESolutions.jl
164 lines (139 loc) · 3.6 KB
/
ODESolutions.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
162
163
164
###############
# ODESolution #
###############
"""
abstract type ODESolution <: GridapType end
Wrapper around an `ODEOperator` and `ODESolver` that represents the solution at
a set of time steps. It is an iterator that computes the solution at each time
step in a lazy fashion when accessing the solution.
# Mandatory
- [`iterate(odesltn)`](@ref)
- [`iterate(odesltn, state)`](@ref)
"""
abstract type ODESolution <: GridapType end
"""
Base.iterate(odesltn::ODESolution) -> ((Real, AbstractVector), StateType)
Allocate the operators and cache and perform one time step of the `ODEOperator`
with the `ODESolver` attached to the `ODESolution`.
"""
function Base.iterate(odesltn::ODESolution)
@abstractmethod
end
"""
Base.iterate(odesltn::ODESolution) -> ((Real, AbstractVector), StateType)
Perform one time step of the `ODEOperator` with the `ODESolver` attached to the
`ODESolution`.
"""
function Base.iterate(odesltn::ODESolution, state)
@abstractmethod
end
Base.IteratorSize(::Type{<:ODESolution}) = Base.SizeUnknown()
######################
# GenericODESolution #
######################
"""
struct GenericODESolution <: ODESolution end
Generic wrapper for the evolution of an `ODEOperator` with an `ODESolver`.
"""
struct GenericODESolution <: ODESolution
odeslvr::ODESolver
odeop::ODEOperator
t0::Real
tF::Real
us0::Tuple{Vararg{AbstractVector}}
end
function Base.iterate(odesltn::GenericODESolution)
odeslvr, odeop = odesltn.odeslvr, odesltn.odeop
t0, us0 = odesltn.t0, odesltn.us0
# Allocate cache
odecache = allocate_odecache(odeslvr, odeop, t0, us0)
# Starting procedure
state0, odecache = ode_start(
odeslvr, odeop,
t0, us0,
odecache
)
# Marching procedure
stateF = copy.(state0)
tF, stateF, odecache = ode_march!(
stateF,
odeslvr, odeop,
t0, state0,
odecache
)
# Finishing procedure
uF = copy(first(us0))
uF, odecache = ode_finish!(
uF,
odeslvr, odeop,
t0, tF, stateF,
odecache
)
# Update iterator
data = (tF, uF)
state = (tF, stateF, state0, uF, odecache)
(data, state)
end
function Base.iterate(odesltn::GenericODESolution, state)
odeslvr, odeop = odesltn.odeslvr, odesltn.odeop
t0, state0, stateF, uF, odecache = state
if t0 >= odesltn.tF - ε
return nothing
end
# Marching procedure
tF, stateF, odecache = ode_march!(
stateF,
odeslvr, odeop,
t0, state0,
odecache
)
# Finishing procedure
uF, odecache = ode_finish!(
uF,
odeslvr, odeop,
t0, tF, stateF,
odecache
)
# Update iterator
data = (tF, uF)
state = (tF, stateF, state0, uF, odecache)
(data, state)
end
##############################
# Default behaviour of solve #
##############################
"""
solve(
odeslvr::ODESolver, odeop::ODEOperator,
t0::Real, tF::Real, us0::Tuple{Vararg{AbstractVector}},
) -> ODESolution
Create an `ODESolution` wrapper around the `ODEOperator` and `ODESolver`,
starting with state `us0` at time `t0`, to be evolved until `tF`.
"""
function Algebra.solve(
odeslvr::ODESolver, odeop::ODEOperator,
t0::Real, tF::Real, us0::Tuple{Vararg{AbstractVector}},
)
GenericODESolution(odeslvr, odeop, t0, tF, us0)
end
function Algebra.solve(
odeslvr::ODESolver, odeop::ODEOperator,
t0::Real, tF::Real, u0::AbstractVector,
)
us0 = (u0,)
solve(odeslvr, odeop, t0, tF, us0)
end
########
# Test #
########
"""
test_ode_solution(odesltn::ODESolution) -> Bool
Test the interface of `ODESolution` specializations.
"""
function test_ode_solution(odesltn::ODESolution)
for (t_n, us_n) in odesltn
@test t_n isa Real
@test us_n isa AbstractVector
end
true
end