/
callbacks.jl
247 lines (220 loc) · 9.4 KB
/
callbacks.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
# Use Recursion to find the first callback for type-stability
# Base Case: Only one callback
function find_first_continuous_callback(integrator, callback::DiffEqBase.AbstractContinuousCallback)
(find_callback_time(integrator,callback,1)...,1,1)
end
# Starting Case: Compute on the first callback
function find_first_continuous_callback(integrator, callback::DiffEqBase.AbstractContinuousCallback, args...)
find_first_continuous_callback(integrator,find_callback_time(integrator,callback,1)...,1,1,args...)
end
function find_first_continuous_callback(integrator,tmin::Number,upcrossing::Float64,
event_occured::Bool,idx::Int,counter::Int,
callback2)
counter += 1 # counter is idx for callback2.
tmin2,upcrossing2,event_occurred2 = find_callback_time(integrator,callback2,counter)
if event_occurred2 && (tmin2 < tmin || !event_occured)
return tmin2,upcrossing2,true,counter,counter
else
return tmin,upcrossing,event_occured,idx,counter
end
end
function find_first_continuous_callback(integrator,tmin::Number,upcrossing::Float64,event_occured::Bool,idx::Int,counter::Int,callback2,args...)
find_first_continuous_callback(integrator,find_first_continuous_callback(integrator,tmin,upcrossing,event_occured,idx,counter,callback2)...,args...)
end
@inline function determine_event_occurance(integrator,callback,counter)
event_occurred = false
Θs = range(typeof(integrator.t)(0), stop=typeof(integrator.t)(1), length=callback.interp_points)
interp_index = 0
dt = integrator.t - integrator.tprev
# Check if the event occured
if typeof(callback.idxs) <: Nothing
previous_condition = callback.condition(integrator.uprev,integrator.tprev,integrator)
elseif typeof(callback.idxs) <: Number
previous_condition = callback.condition(integrator.uprev[callback.idxs],integrator.tprev,integrator)
else
previous_condition = callback.condition(@view(integrator.uprev[callback.idxs]),integrator.tprev,integrator)
end
if integrator.event_last_time == counter && abs(previous_condition) < 100callback.abstol
# abs(previous_condition) < callback.abstol is for multiple events: only
# condition this on the correct event
# If there was a previous event, utilize the derivative at the start to
# chose the previous sign. If the derivative is positive at tprev, then
# we treat the value as positive, and derivative is negative then we
# treat the value as negative, reguardless of the postiivity/negativity
# of the true value due to it being =0 sans floating point issues.
tmp = integrator.tmp
if !(typeof(callback.idxs) <: Number)
integrator(tmp,integrator.tprev+100eps(integrator.tprev))
callback.idxs == nothing ? _tmp = tmp : _tmp = @view tmp[callback.idxs]
else
_tmp = integrator(integrator.tprev+100eps(integrator.tprev))[callback.idxs]
end
tmp_condition = callback.condition(_tmp,integrator.tprev +
100eps(integrator.tprev),
integrator)
prev_sign = sign((tmp_condition-previous_condition)/integrator.tdir)
else
prev_sign = sign(previous_condition)
end
prev_sign_index = 1
if typeof(callback.idxs) <: Nothing
next_sign = sign(callback.condition(integrator.u,integrator.t,integrator))
elseif typeof(callback.idxs) <: Number
next_sign = sign(callback.condition(integrator.u[callback.idxs],integrator.t,integrator))
else
next_sign = sign(callback.condition(@view(integrator.u[callback.idxs]),integrator.t,integrator))
end
if ((prev_sign<0 && !(typeof(callback.affect!)<:Nothing)) || (prev_sign>0 && !(typeof(callback.affect_neg!)<:Nothing))) && prev_sign*next_sign<=0
event_occurred = true
interp_index = callback.interp_points
elseif callback.interp_points!=0 # Use the interpolants for safety checking
tmp = integrator.tmp
for i in 2:length(Θs)
if !(typeof(callback.idxs) <: Number)
integrator(tmp,integrator.tprev+dt*Θs[i])
callback.idxs == nothing ? _tmp = tmp : _tmp = @view tmp[callback.idxs]
else
_tmp = integrator(integrator.tprev+dt*Θs[i])[callback.idxs]
end
new_sign = callback.condition(_tmp,integrator.tprev+dt*Θs[i],integrator)
if prev_sign == 0
prev_sign = sign(new_sign)
prev_sign_index = i
end
if ((prev_sign<0 && !(typeof(callback.affect!)<:Nothing)) || (prev_sign>0 && !(typeof(callback.affect_neg!)<:Nothing))) && prev_sign*new_sign<0
event_occurred = true
interp_index = i
break
end
end
end
event_occurred,interp_index,Θs,prev_sign,prev_sign_index
end
function find_callback_time(integrator,callback,counter)
event_occurred,interp_index,Θs,prev_sign,prev_sign_index = determine_event_occurance(integrator,callback,counter)
dt = integrator.t - integrator.tprev
if event_occurred
if typeof(callback.condition) <: Nothing
new_t = zero(typeof(integrator.t))
else
if callback.interp_points!=0
top_Θ = Θs[interp_index] # Top at the smallest
bottom_θ = Θs[prev_sign_index]
else
top_Θ = typeof(integrator.t)(1)
bottom_θ = typeof(integrator.t)(0)
end
if callback.rootfind
tmp = integrator.tmp
zero_func = (Θ) -> begin
if !(typeof(callback.idxs) <: Number)
integrator(tmp,integrator.tprev+Θ*dt)
callback.idxs == nothing ? _tmp = tmp : _tmp = @view tmp[callback.idxs]
else
_tmp = integrator(integrator.tprev+Θ*dt)[callback.idxs]
end
out = callback.condition(tmp,integrator.tprev+Θ*dt,integrator)
out
end
if zero_func(top_Θ) == 0
Θ = top_Θ
else
if integrator.event_last_time == counter &&
abs(zero_func(bottom_θ)) < 100callback.abstol &&
prev_sign_index == 1
# Determined that there is an event by derivative
# But floating point error may make the end point negative
sign_top = sign(zero_func(top_Θ))
bottom_θ += 2eps(typeof(bottom_θ))
iter = 1
while sign(zero_func(bottom_θ)) == sign_top && iter < 12
bottom_θ *= 5
iter += 1
end
iter == 12 && error("Double callback crossing floating pointer reducer errored. Report this issue.")
end
Θ = prevfloat(find_zero(zero_func,(bottom_θ,top_Θ),Roots.AlefeldPotraShi(),atol = callback.abstol/100))
end
#Θ = prevfloat(...)
# prevfloat guerentees that the new time is either 1 floating point
# numbers just before the event or directly at zero, but not after.
# If there's a barrier which is never supposed to be crossed,
# then this will ensure that
# The item never leaves the domain. Otherwise Roots.jl can return
# a float which is slightly after, making it out of the domain, causing
# havoc.
new_t = dt*Θ
elseif interp_index != callback.interp_points
new_t = dt*Θs[interp_index]
else
# If no solve and no interpolants, just use endpoint
new_t = dt
end
end
else
new_t = zero(typeof(integrator.t))
end
new_t,prev_sign,event_occurred
end
function apply_callback!(integrator,callback::ContinuousCallback,cb_time,prev_sign)
if cb_time != zero(typeof(integrator.t))
integrator.t = integrator.tprev+cb_time
integrator(integrator.u,integrator.t)
end
saved_in_cb = false
@inbounds if callback.save_positions[1]
savevalues!(integrator,true)
saved_in_cb = true
end
integrator.u_modified = true
if prev_sign < 0
if typeof(callback.affect!) <: Nothing
integrator.u_modified = false
else
callback.affect!(integrator)
end
elseif prev_sign > 0
if typeof(callback.affect_neg!) <: Nothing
integrator.u_modified = false
else
callback.affect_neg!(integrator)
end
end
if integrator.u_modified
@inbounds if callback.save_positions[2]
savevalues!(integrator,true)
saved_in_cb = true
end
return true,saved_in_cb
end
false,saved_in_cb
end
#Base Case: Just one
@inline function apply_discrete_callback!(integrator,callback::DiscreteCallback)
saved_in_cb = false
if callback.condition(integrator.u,integrator.t,integrator)
@inbounds if callback.save_positions[1]
savevalues!(integrator,true)
saved_in_cb = true
end
integrator.u_modified = true
callback.affect!(integrator)
@inbounds if callback.save_positions[2]
savevalues!(integrator,true)
saved_in_cb = true
end
end
integrator.u_modified,saved_in_cb
end
#Starting: Get bool from first and do next
@inline function apply_discrete_callback!(integrator,callback::DiscreteCallback,args...)
apply_discrete_callback!(integrator,apply_discrete_callback!(integrator,callback)...,args...)
end
@inline function apply_discrete_callback!(integrator,discrete_modified::Bool,saved_in_cb::Bool,callback::DiscreteCallback,args...)
bool,saved_in_cb2 = apply_discrete_callback!(integrator,apply_discrete_callback!(integrator,callback)...,args...)
discrete_modified || bool, saved_in_cb || saved_in_cb2
end
@inline function apply_discrete_callback!(integrator,discrete_modified::Bool,saved_in_cb::Bool,callback::DiscreteCallback)
bool,saved_in_cb2 = apply_discrete_callback!(integrator,callback)
discrete_modified || bool, saved_in_cb || saved_in_cb2
end