-
Notifications
You must be signed in to change notification settings - Fork 56
/
ManifoldsOrdinaryDiffEqDiffEqCallbacksExt.jl
279 lines (259 loc) · 8.22 KB
/
ManifoldsOrdinaryDiffEqDiffEqCallbacksExt.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
module ManifoldsOrdinaryDiffEqDiffEqCallbacksExt
if isdefined(Base, :get_extension)
using Manifolds
using Manifolds:
IntegratorTerminatorNearChartBoundary,
affine_connection,
get_chart_index,
transition_map!,
transition_map_diff!
import Manifolds: solve_chart_exp_ode, solve_chart_parallel_transport_ode
using ManifoldsBase
using DiffEqCallbacks
using OrdinaryDiffEq: OrdinaryDiffEq, SciMLBase, Rodas5, AutoVern9, ODEProblem, solve
else
# imports need to be relative for Requires.jl-based workflows:
# https://github.com/JuliaArrays/ArrayInterface.jl/pull/387
using ..Manifolds
using ..Manifolds:
IntegratorTerminatorNearChartBoundary,
affine_connection,
get_chart_index,
transition_map!,
transition_map_diff!
import ..Manifolds: solve_chart_exp_ode, solve_chart_parallel_transport_ode
using ..ManifoldsBase
using ..DiffEqCallbacks
using ..OrdinaryDiffEq: OrdinaryDiffEq, SciMLBase, Rodas5, AutoVern9, ODEProblem, solve
end
"""
(int_term::IntegratorTerminatorNearChartBoundary)(u, t, integrator)
Terminate integration when integrator goes too closely to chart boundary.
Closeness is determined by `ϵ` value of [`IntegratorTerminatorNearChartBoundary`](@ref)
`int_term`.
# Arguments:
- `int_term`: object containing keyword arguments for `check_chart_switch`, such as
the desired maximum distance to boundary,
- `u`: parameters of a point at which the integrator is solving a differential equation.
- `t`: time parameter of the integrator
- `integrator`: state of the integrator. Internal parameters are expected to contained
the manifold on which the equation is solved, the atlas and the current chart index.
"""
function (int_term::IntegratorTerminatorNearChartBoundary)(u, t, integrator)
(M, A, i) = integrator.p
if check_chart_switch(M, A, i, u.x[1]; int_term.check_chart_switch_kwargs...)
# switch charts
OrdinaryDiffEq.terminate!(integrator)
end
return u
end
"""
StitchedChartSolution{TM<:AbstractManifold,TA<:AbstractAtlas,TChart}
Solution of an ODE on a manifold `M` in charts of an [`AbstractAtlas`](@ref) `A`.
"""
struct StitchedChartSolution{Prob,TM<:AbstractManifold,TA<:AbstractAtlas,TChart}
M::TM
A::TA
sols::Vector{Tuple{SciMLBase.AbstractODESolution,TChart}}
end
function StitchedChartSolution(
M::AbstractManifold,
A::AbstractAtlas,
problem::Symbol,
TChart,
)
return StitchedChartSolution{problem,typeof(M),typeof(A),TChart}(M, A, [])
end
function (scs::StitchedChartSolution{:Exp})(t::Real)
if t < scs.sols[1][1].t[1]
throw(DomainError("Time $t is outside of the solution."))
end
for (sol, i) in scs.sols
if t <= sol.t[end]
B = induced_basis(scs.M, scs.A, i)
solt = sol(t)
p = get_point(scs.M, scs.A, i, solt.x[1])
X = get_vector(scs.M, p, solt.x[2], B)
return (p, X)
end
end
throw(
DomainError(
"Time $t is outside of the solution (solution time range is [$(scs.sols[1][1].t[1]), $(scs.sols[end][1].t[end])]).",
),
)
end
function (scs::StitchedChartSolution{:PT})(t::Real)
if t < scs.sols[1][1].t[1]
throw(DomainError("Time $t is outside of the solution."))
end
for (sol, i) in scs.sols
if t <= sol.t[end]
B = induced_basis(scs.M, scs.A, i)
solt = sol(t)
p = get_point(scs.M, scs.A, i, solt.x[1])
X = get_vector(scs.M, p, solt.x[2], B)
Y = get_vector(scs.M, p, solt.x[3], B)
return (p, X, Y)
end
end
throw(
DomainError(
"Time $t is outside of the solution (solution time range is [$(scs.sols[1][1].t[1]), $(scs.sols[end][1].t[end])]).",
),
)
end
function (scs::StitchedChartSolution)(t::AbstractArray)
return map(scs, t)
end
function chart_exp_problem(u, params, t)
M, A, i = params
a = u.x[1]
dx = u.x[2]
ddx = -affine_connection(M, A, i, a, dx, dx)
return ArrayPartition(dx, ddx)
end
"""
solve_chart_exp_ode(
M::AbstractManifold,
a,
Xc,
A::AbstractAtlas,
i0;
solver=AutoVern9(Rodas5()),
final_time=1.0,
check_chart_switch_kwargs=NamedTuple(),
kwargs...,
)
Solve geodesic ODE on a manifold `M` from point of coordinates `a` in chart `i0` from an
[`AbstractAtlas`](@ref) `A` in direction of coordinates `Xc` in the induced basis.
"""
function solve_chart_exp_ode(
M::AbstractManifold,
a,
Xc,
A::AbstractAtlas,
i0;
solver=AutoVern9(Rodas5()),
final_time=1.0,
check_chart_switch_kwargs=NamedTuple(),
kwargs...,
)
u0 = ArrayPartition(copy(a), copy(Xc))
cur_i = i0
# callback stops solver when we get too close to chart boundary
cb = FunctionCallingCallback(
IntegratorTerminatorNearChartBoundary(check_chart_switch_kwargs);
func_start=false,
)
retcode = SciMLBase.ReturnCode.Terminated
init_time = zero(final_time)
sols = StitchedChartSolution(M, A, :Exp, typeof(i0))
while retcode === SciMLBase.ReturnCode.Terminated && init_time < final_time
params = (M, A, cur_i)
prob =
ODEProblem(chart_exp_problem, u0, (init_time, final_time), params; callback=cb)
sol = solve(prob, solver; kwargs...)
retcode = sol.retcode
init_time = sol.t[end]::typeof(final_time)
push!(sols.sols, (sol, cur_i))
# here we switch charts
a_final = sol.u[end].x[1]::typeof(a)
new_i = get_chart_index(M, A, cur_i, a_final)
transition_map!(M, u0.x[1], A, cur_i, new_i, a_final)
transition_map_diff!(
M,
u0.x[2],
A,
cur_i,
a_final,
sol.u[end].x[2]::typeof(Xc),
new_i,
)
cur_i = new_i
end
return sols
end
function chart_pt_problem(u, params, t)
M, A, i = params
a = u.x[1]
dx = u.x[2]
dY = u.x[3]
ddx = -affine_connection(M, A, i, a, dx, dx)
ddY = -affine_connection(M, A, i, a, dx, dY)
return ArrayPartition(dx, ddx, ddY)
end
"""
solve_chart_parallel_transport_ode(
M::AbstractManifold,
a,
Xc,
A::AbstractAtlas,
i0,
Yc;
solver=AutoVern9(Rodas5()),
check_chart_switch_kwargs=NamedTuple(),
final_time=1.0,
kwargs...,
)
Parallel transport vector with coordinates `Yc` along geodesic on a manifold `M` from point of
coordinates `a` in a chart `i0` from an [`AbstractAtlas`](@ref) `A` in direction of
coordinates `Xc` in the induced basis.
"""
function solve_chart_parallel_transport_ode(
M::AbstractManifold,
a,
Xc,
A::AbstractAtlas,
i0,
Yc;
solver=AutoVern9(Rodas5()),
final_time=1.0,
check_chart_switch_kwargs=NamedTuple(),
kwargs...,
)
u0 = ArrayPartition(copy(a), copy(Xc), copy(Yc))
cur_i = i0
# callback stops solver when we get too close to chart boundary
cb = FunctionCallingCallback(
IntegratorTerminatorNearChartBoundary(check_chart_switch_kwargs);
func_start=false,
)
retcode = SciMLBase.ReturnCode.Terminated
init_time = zero(final_time)
sols = StitchedChartSolution(M, A, :PT, typeof(i0))
while retcode === SciMLBase.ReturnCode.Terminated && init_time < final_time
params = (M, A, cur_i)
prob =
ODEProblem(chart_pt_problem, u0, (init_time, final_time), params; callback=cb)
sol = solve(prob, solver; kwargs...)
retcode = sol.retcode
init_time = sol.t[end]::typeof(final_time)
push!(sols.sols, (sol, cur_i))
# here we switch charts
a_final = sol.u[end].x[1]::typeof(a)
new_i = get_chart_index(M, A, cur_i, a_final)
transition_map!(M, u0.x[1], A, cur_i, new_i, a_final)
transition_map_diff!(
M,
u0.x[2],
A,
cur_i,
a_final,
sol.u[end].x[2]::typeof(Xc),
new_i,
)
transition_map_diff!(
M,
u0.x[3],
A,
cur_i,
a_final,
sol.u[end].x[3]::typeof(Yc),
new_i,
)
cur_i = new_i
end
return sols
end
end