-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathvalidated_integ.jl
546 lines (463 loc) · 17.6 KB
/
validated_integ.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# This file contains methods for validated integration of ODEs using Taylor models,
# originally from TaylorModels.jl. See license below.
#=
The TaylorModels.jl package is licensed under the MIT "Expat" License:
> Copyright (c) 2018-2020: David Sanders and Luis Benet.
>
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
>
> of this software and associated documentation files (the "Software"), to deal
>
> in the Software without restriction, including without limitation the rights
>
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
>
> copies of the Software, and to permit persons to whom the Software is
>
> furnished to do so, subject to the following conditions:
>
>
>
> The above copyright notice and this permission notice shall be included in all
>
> copies or substantial portions of the Software.
>
>
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
>
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
>
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
>
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
>
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
>
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
>
> SOFTWARE.
>
>
=#
"""
remainder_taylorstep!(f!, t, x, dx, xI, dxI, δI, δt, params, adaptive)
Returns a remainder for the integration step for the dependent variables (`x`)
checking that the solution satisfies the criteria for existence and uniqueness.
"""
function remainder_taylorstep!(f!::Function, t::Taylor1{T},
x::Vector{Taylor1{TaylorN{T}}}, dx::Vector{Taylor1{TaylorN{T}}},
xI::Vector{Taylor1{IA.Interval{T}}}, dxI::Vector{Taylor1{IA.Interval{T}}},
δI::IntervalBox{N,T}, δt::IA.Interval{T}, params, adaptive::Bool) where {N,T}
orderT = get_order(dx[1])
aux = δt^(orderT+1)
Δx = IntervalBox([xI[i][orderT+1] for i in eachindex(xI)]) * aux
Δdx = IntervalBox([dxI[i][orderT+1] for i in eachindex(xI)]) * aux
Δ0 = IntervalBox([dx[i][orderT](δI) for i in eachindex(x)]) * aux / (orderT+1)
Δ = Δ0 + Δdx * δt
Δxold = Δx
# Checking existence and uniqueness
iscontractive(Δ, Δx) && return (true, Δx, t[0])
# If the check didn't work, compute new remainders. A new Δx is proposed,
# and the corresponding Δdx is computed
xxI = Array{Taylor1{TaylorN{IA.Interval{T}}}}(undef, N)
dxxI = Array{Taylor1{TaylorN{IA.Interval{T}}}}(undef, N)
vv = Array{IA.Interval{T}}(undef, N)
for its = 1:50
# Remainder of Picard iteration
Δ = picard_remainder!(f!, t, x, dx, xxI, dxxI, δI, δt, Δx, Δ0, params)
# Checking existence and uniqueness
iscontractive(Δ, Δx) && return (true, Δx, t[0])
# iscontractive(Δ, Δx) && return _contract_iteration!(f!, t, x, dx, xxI, dxxI, δI, δt, Δx, Δdx, Δ0, params)
# Expand Δx in the directions needed
Δxold = Δx
if Δ ⊆ Δx
@inbounds for ind in 1:N
# Widen the directions where ⊂ does not hold
vv[ind] = Δx[ind]
if Δ[ind] == Δx[ind]
vv[ind] = widen.(Δ[ind])
end
end
Δx = IntervalBox(vv)
continue
end
Δx = Δ
end
if adaptive
return (false, Δx, t[0])
else
@format full
error("""
Error: cannot prove existence and unicity of the solution:
t0 = $(t[0])
δt = $(δt)
Δ = $(Δ)
Δxo = $(Δxold)
Δx = $(Δx)
$(Δ .⊆ Δxold)
""")
end
end
"""
iscontractive(Δ, Δx)
Checks if `Δ .⊂ Δx` is satisfied. If ``Δ ⊆ Δx` is satisfied, it returns
`true` if all cases where `==` holds corresponds to the zero `IA.Interval`.
"""
function iscontractive(Δ::IntervalBox{N,T}, Δx::IntervalBox{N,T}) where{N,T}
zi = IA.Interval{T}(0, 0)
@inbounds for ind in 1:N
Δ[ind] ⊂ Δx[ind] && continue
Δ[ind] == Δx[ind] == zi && continue
return false
end
return true
end
"""
picard_remainder!(f!, t, x, dx, xxI, dxxI, δI, δt, Δx, Δ0, params)
Return the remainder of Picard operator
"""
function picard_remainder!(f!::Function, t::Taylor1{T},
x::Vector{Taylor1{TaylorN{T}}}, dx::Vector{Taylor1{TaylorN{T}}},
xxI::Vector{Taylor1{TaylorN{IA.Interval{T}}}},
dxxI::Vector{Taylor1{TaylorN{IA.Interval{T}}}},
δI::IntervalBox{N,T}, δt::IA.Interval{T},
Δx::IntervalBox{N,T}, Δ0::IntervalBox{N,T}, params) where {N,T}
# Extend `x` and `dx` to have IA.Interval coefficients
zI = zero(IA.Interval{T})
@inbounds for ind in eachindex(x)
xxI[ind] = x[ind] + Δx[ind]
dxxI[ind] = dx[ind] + zI
end
# Compute `dxxI` from the equations of motion
f!(dxxI, xxI, params, t)
# Picard iteration, considering only the bound of `f` and the last coeff of f
Δdx = IntervalBox( TM.evaluate.( (dxxI - dx)(δt), δI... ) )
Δ = Δ0 + Δdx * δt
return Δ
end
# Picard iterations to contract further Δx, once Δ ⊂ Δx holds
# **Currently not used**
function _contract_iteration!(f!::Function, t::Taylor1{T},
x::Vector{Taylor1{TaylorN{T}}}, dx::Vector{Taylor1{TaylorN{T}}},
xxI::Vector{Taylor1{TaylorN{IA.Interval{T}}}}, dxxI::Vector{Taylor1{TaylorN{IA.Interval{T}}}},
δI::IntervalBox{N,T}, δt::IA.Interval{T},
Δx::IntervalBox{N,T}, Δdx::IntervalBox{N,T}, Δ0::IntervalBox{N,T}, params) where {N,T}
# Some abbreviations
Δ = Δ0 + Δdx * δt
Δxold = Δx
# Picard contractions
for its = 1:10
# Remainder of Picard iteration
Δ = picard_remainder!(f!, t, x, dx, xxI, dxxI, δI, δt, Δx, Δ0, params)
# If contraction doesn't hold, return old bound
iscontractive(Δ, Δx) || return Δxold
# Contract estimate
Δxold = Δx
Δx = Δ
end
return Δxold
end
"""
absorb_remainder(a::TaylorModelN)
Returns a TaylorModelN, equivalent to `a`, such that the remainder
is mostly absorbed in the constant and linear coefficients. The linear shift assumes
that `a` is normalized to the `IntervalBox(-1..1, Val(N))`.
Ref: Xin Chen, Erika Abraham, and Sriram Sankaranarayanan,
"Taylor Model Flowpipe Construction for Non-linear Hybrid
Systems", in Real Time Systems Symposium (RTSS), pp. 183-192 (2012),
IEEE Press.
"""
function absorb_remainder(a::TaylorModelN{N,T,T}) where {N,T}
Δ = remainder(a)
orderQ = get_order(a)
δ = IntervalBox(IA.Interval{T}(-1,1), Val(N))
aux = diam(Δ)/(2N)
rem = IA.Interval{T}(0, 0)
# Linear shift
lin_shift = mid(Δ) + sum((aux*TaylorN(i, order=orderQ) for i in 1:N))
bpol = a.pol + lin_shift
# Compute the new remainder
aI = a(δ)
bI = bpol(δ)
if bI ⊆ aI
rem = IA.Interval(aI.lo-bI.lo, aI.hi-bI.hi)
elseif aI ⊆ bI
rem = IA.Interval(bI.lo-aI.lo, bI.hi-aI.hi)
else
r_lo = aI.lo-bI.lo
r_hi = aI.hi-bI.hi
if r_lo > 0
rem = IA.Interval(-r_lo, r_hi)
else
rem = IA.Interval( r_lo, -r_hi)
end
end
return TaylorModelN(bpol, rem, a.x0, a.dom)
end
# Postverify and define Taylor models to be returned
function scalepostverify_sw!(xTMN::Vector{TaylorModelN{N,T,T}},
X::Vector{TaylorN{T}}) where {N,T}
postverify = true
x0 = xTMN[1].x0
B = domain(xTMN[1])
zI = zero(B[1])
@inbounds for i in eachindex(xTMN)
pol = polynomial(xTMN[i])
ppol = fp_rpa(TaylorModelN(pol(X), zI, x0, B ))
postverify = postverify && (xTMN[i](B) ⊆ ppol(B))
xTMN[i] = copy(ppol)
end
@assert postverify """
Failed to post-verify shrink-wrapping:
X = $(linear_polynomial(X))
$(xTMN)
"""
return postverify
end
"""
shrink_wrapping!(xTMN::TaylorModelN)
Returns a modified inplace `xTMN`, which has absorbed the remainder
by the modified shrink-wrapping method of Florian Bünger.
The domain of `xTMN` is the normalized IA.Interval box `[-1,1]^N`.
Ref: Florian B\"unger, Shrink wrapping for Taylor models revisited,
Numer Algor 78:1001–1017 (2018), https://doi.org/10.1007/s11075-017-0410-1
"""
function shrink_wrapping!(xTMN::Vector{TaylorModelN{N,T,T}}) where {N,T}
# Original domain of TaylorModelN should be the symmetric normalized box
B = IntervalBox(IA.Interval{T}(-1,1), Val(N))
@assert all(domain.(xTMN) .== (B,))
zI = zero(IA.Interval{T})
x0 = xTMN[1].x0
# Vector of independent TaylorN variables
order = get_order(xTMN[1])
X = [TaylorN(T, i, order=order) for i in 1:N]
# Remainder of original TaylorModelN and componentwise mag
rem = remainder.(xTMN)
r = mag.(rem)
qB = r .* B
# Shift to remove constant term
xTN0 = constant_term.(xTMN)
xTNcent = polynomial.(xTMN) .- xTN0
# Jacobian (at zero) and its inverse
jac = TaylorSeries.jacobian(xTNcent)
# If conditional number is too large (inverse of jac is ill defined),
# don't change xTMN
if cond(jac) > 1.0e4
return ones(eltype(r), N)
end
# Inverse of the Jacobian
invjac = inv(jac)
# Componentwise bound
r̃ = mag.(invjac * qB) # qB <-- r .* B
qB´ = r̃ .* B
@assert invjac * qB ⊆ qB´
# Nonlinear part (linear part is close to identity)
g = invjac*xTNcent .- X
# g = invjac*(xTNcent .- linear_polynomial.(xTNcent))
# ... and its jacobian matrix (full dependence!)
jacmatrix_g = TaylorSeries.jacobian(g, X)
# Step 7 of Bünger algorithm: obtain an estimate of `q`
# Some constants/parameters
q_tol = 1.0e-12
q = 1.0 .+ r̃
ff = 65/64
q_max = ff .* q
zs = zero(q)
s = similar(q)
q_old = similar(q)
q_1 = similar(q)
jaq_q1 = jacmatrix_g * r̃
iter_max = 100
improve = true
iter = 0
while improve && iter < iter_max
qB .= q .* B
s .= zs
q_old .= q
q_1 .= q_old .- 1.0
mul!(jaq_q1, jacmatrix_g, q_1)
@inbounds for i in eachindex(xTMN)
s[i] = mag( jaq_q1[i](qB) )
q[i] = 1.0 + r̃[i] + s[i]
# If q is too large, return xTMN unchanged
q[i] > q_max[i] && return xTMN
end
improve = any( ((q .- q_old)./q) .> q_tol )
iter += 1
end
# (improve || q == one.(q)) && return xTMN
# Compute final q and rescale X
@. q = 1.0 + r̃ + ff * s
@. X = q * X
# Postverify and define Taylor models to be returned
scalepostverify_sw!(xTMN, X)
return q
end
"""
validated-step!
"""
function validated_step!(f!, t::Taylor1{T}, x::Vector{Taylor1{TaylorN{T}}},
dx::Vector{Taylor1{TaylorN{T}}}, xaux::Vector{Taylor1{TaylorN{T}}},
tI::Taylor1{T}, xI::Vector{Taylor1{IA.Interval{T}}},
dxI::Vector{Taylor1{IA.Interval{T}}}, xauxI::Vector{Taylor1{IA.Interval{T}}},
t0::T, tmax::T, sign_tstep::Int,
xTMN::Vector{TaylorModelN{N,T,T}}, xv::Vector{IntervalBox{N,T}},
rem::Vector{IA.Interval{T}}, zbox::IntervalBox{N,T}, symIbox::IntervalBox{N,T},
nsteps::Int, orderT::Int, abstol::T, params, parse_eqs::Bool,
check_property::Function=(t, x)->true, adaptive::Bool=true) where {N,T}
# One step integration (non-validated)
# TaylorIntegration.__jetcoeffs!(Val(parse_eqs), f!, t, x, dx, xaux, params)
# δt = TaylorIntegration.stepsize(x, abstol)
δt = TaylorIntegration.taylorstep!(f!, t, x, dx, xaux, abstol, params, parse_eqs)
f!(dx, x, params, t) # Update `dx[:][orderT]`
# One step integration for the initial box
# TaylorIntegration.__jetcoeffs!(Val(parse_eqs), f!, tI, xI, dxI, xauxI, params)
# δtI = TaylorIntegration.stepsize(xI, abstol)
δtI = TaylorIntegration.taylorstep!(f!, tI, xI, dxI, xauxI, abstol, params, parse_eqs)
f!(dxI, xI, params, tI) # Update `dxI[:][orderT+1]`
# Step size
δt = min(δt, sign_tstep*(tmax-t0))
δt = sign_tstep * δt
# Test if `check_property` is satisfied; if not, half the integration time.
# If after 25 checks `check_property` is not satisfied, throw an error.
nsteps += 1
issatisfied = false
rem_old = copy(rem)
local success, _t0
for nchecks = 1:25
# Validate the solution: remainder consistent with Schauder thm
δtI = sign_tstep * IA.Interval(0, sign_tstep*δt)
(success, Δ, _t0) = remainder_taylorstep!(f!, t, x, dx, xI, dxI, symIbox, δtI, params, adaptive)
if adaptive && !success
break
end
rem .= rem_old .+ Δ
# Create TaylorModelN to store remainders and evaluation
@inbounds begin
for i in eachindex(x)
xTMN[i] = fp_rpa( TaylorModelN(x[i](δtI), rem[i], zbox, symIbox) )
# If remainder is still too big, do it again
# TODO: check if this can be avoided.
j = 0
while (j < 10) && (mag(rem[i]) > 1.0e-10)
j += 1
xTMN[i] = absorb_remainder(xTMN[i])
rem[i] = remainder(xTMN[i])
end
end
xv[nsteps] = TM.evaluate(xTMN, symIbox) # IntervalBox
if !check_property(t0+δt, xv[nsteps])
δt = δt/2
continue
end
end # @inbounds
issatisfied = true
break
end
if !issatisfied && !adaptive # TEMP do not return the error msg if adaptive is true
error("""
`check_property` is not satisfied:
$t0 $nsteps $δt
$(xv[nsteps])
$(check_property(t0+δt, xv[nsteps]))""")
end
return (success, δt, _t0)
end
function validated_integ!(F, f!, qq0::AbstractArray{T,1}, δq0::IntervalBox{N,T},
t0::T, tmax::T, orderQ::Int, orderT::Int, abstol::T, max_steps::Int, time_shift, adaptive::Bool=true, params=nothing;
parse_eqs::Bool=true, check_property::Function=(t, x)->true) where {N, T<:Real}
# Set proper parameters for jet transport
@assert N == get_numvars()
dof = N
# Some variables
zI = zero(IA.Interval{T})
zbox = IntervalBox(zI, Val(N))
symIbox = IntervalBox(IA.Interval{T}(-1, 1), Val(N))
q0 = IntervalBox(qq0)
t = t0 + Taylor1(orderT)
tI = t0 + Taylor1(orderT+1)
# Allocation of vectors
# Output
tv = Array{T}(undef, max_steps+1)
xv = Array{IntervalBox{N,T}}(undef, max_steps+1)
xTM1v = Array{TaylorModel1{TaylorN{T},T}}(undef, dof, max_steps+1)
# Internals: jet transport integration
x = Array{Taylor1{TaylorN{T}}}(undef, dof)
dx = Array{Taylor1{TaylorN{T}}}(undef, dof)
xaux = Array{Taylor1{TaylorN{T}}}(undef, dof)
xTMN = Array{TaylorModelN{N,T,T}}(undef, dof)
# Internals: Taylor1{IA.Interval{T}} integration
xI = Array{Taylor1{IA.Interval{T}}}(undef, dof)
dxI = Array{Taylor1{IA.Interval{T}}}(undef, dof)
xauxI = Array{Taylor1{IA.Interval{T}}}(undef, dof)
# Set initial conditions
rem = Array{IA.Interval{T}}(undef, dof)
@inbounds for i in eachindex(x)
qaux = normalize_taylor(qq0[i] + TaylorN(i, order=orderQ), δq0, true)
x[i] = Taylor1( qaux, orderT)
dx[i] = x[i]
xTMN[i] = TaylorModelN(qaux, zI, zbox, symIbox)
#
xI[i] = Taylor1( q0[i]+δq0[i], orderT+1 )
dxI[i] = xI[i]
rem[i] = zI
#
xTM1v[i, 1] = TaylorModel1(deepcopy(x[i]), zI, zI, zI)
end
sign_tstep = copysign(1, tmax-t0)
# Output vectors
@inbounds tv[1] = t0
@inbounds xv[1] = TM.evaluate(xTMN, symIbox)
# Determine if specialized jetcoeffs! method exists (built by @taylorize)
parse_eqs = parse_eqs && (length(methods(TaylorIntegration.jetcoeffs!)) > 2)
if parse_eqs
try
TaylorIntegration.jetcoeffs!(Val(f!), t, x, dx, params)
catch
parse_eqs = false
end
end
local success # if true, the validation step succeeded
local _t0 # represents how much the integration could advance until validation failed
# Integration
nsteps = 1
while sign_tstep*t0 < sign_tstep*tmax
# Validated step of the integration
(success, δt, _t0) = validated_step!(f!, t, x, dx, xaux, tI, xI, dxI, xauxI,
t0, tmax, sign_tstep, xTMN, xv, rem, zbox, symIbox,
nsteps, orderT, abstol, params, parse_eqs, check_property, adaptive)
if adaptive && !success
break
end
# New initial conditions and time
nsteps += 1
t0 += δt
@inbounds t[0] = t0
@inbounds tI[0] = t0
@inbounds tv[nsteps] = t0
@inbounds for i in eachindex(x)
δtI = sign_tstep * IA.Interval{T}(0, sign_tstep*δt)
xTM1v[i, nsteps] = TaylorModel1(deepcopy(x[i]), rem[i], zI, δtI)
aux = x[i](δt)
x[i] = Taylor1( aux, orderT )
dx[i] = Taylor1( zero(aux), orderT )
auxI = xTMN[i](symIbox)
xI[i] = Taylor1( auxI, orderT+1 )
dxI[i] = xI[i]
end
# construct the taylor model reach-set
Ri = TaylorModelReachSet(xTM1v[:, nsteps], TimeInterval(t0-δt, t0) + time_shift)
# update output flowpipe
# note that F has 1 less element than xTM1v and xv (we don't store the initial set)
push!(F, Ri)
if nsteps > max_steps
@warn("""
Maximum number of integration steps reached; exiting. Try increasing `max_steps`.
""")
break
end
end
return F, view(tv,1:nsteps), view(xv,1:nsteps), view(xTM1v, :, 1:nsteps), success, _t0
end