Skip to content

Commit

Permalink
Merge branch 'ma'
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisRackauckas committed Mar 20, 2018
2 parents c86fd9c + 3d95ed6 commit cd8c2be
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
22 changes: 12 additions & 10 deletions src/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ function find_first_continuous_callback(integrator, callback::AbstractContinuous
find_first_continuous_callback(integrator,find_callback_time(integrator,callback)...,1,1,args...)
end

function find_first_continuous_callback(integrator,tmin::Number,upcrossing::Float64,idx::Int,counter::Int,callback2)
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 = find_callback_time(integrator,callback2)
tmin2,upcrossing2,event_occurred2 = find_callback_time(integrator,callback2)

if (tmin2 < tmin && tmin2 != zero(typeof(tmin))) || tmin == zero(typeof(tmin))
return tmin2,upcrossing2,counter,counter
if event_occurred2 && (tmin2 < tmin || !event_occured)
return tmin2,upcrossing2,true,counter,counter
else
return tmin,upcrossing,idx,counter
return tmin,upcrossing,event_occured,idx,counter
end
end

function find_first_continuous_callback(integrator,tmin::Number,upcrossing::Float64,idx::Int,counter::Int,callback2,args...)
find_first_continuous_callback(integrator,find_first_continuous_callback(integrator,tmin,upcrossing,idx,counter,callback2)...,args...)
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)
Expand All @@ -38,7 +40,7 @@ end
else
previous_condition = callback.condition(@view(integrator.uprev[callback.idxs]),integrator.tprev,integrator)
end
if isapprox(previous_condition,0,rtol=callback.reltol,atol=callback.abstol)
if integrator.event_last_time
prev_sign = 0.0
else
prev_sign = sign(previous_condition)
Expand All @@ -64,7 +66,7 @@ end
end
new_sign = callback.condition(_tmp,integrator.tprev+dt*Θs[i],integrator)
if prev_sign == 0
prev_sign = new_sign
prev_sign = sign(new_sign)
prev_sign_index = i
end
if ((prev_sign<0 && !(typeof(callback.affect!)<:Void)) || (prev_sign>0 && !(typeof(callback.affect_neg!)<:Void))) && prev_sign*new_sign<0
Expand Down Expand Up @@ -122,7 +124,7 @@ function find_callback_time(integrator,callback)
else
new_t = zero(typeof(integrator.t))
end
new_t,prev_sign
new_t,prev_sign,event_occurred
end

function apply_callback!(integrator,callback::ContinuousCallback,cb_time,prev_sign)
Expand Down
1 change: 1 addition & 0 deletions src/integrator_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mutable struct ODEInterfaceIntegrator{uType,uPrevType,oType,SType,solType} <: Ab
sizeu::SType
sol::solType
eval_sol_fcn::InterpFunction
event_last_time::Bool
end

(integrator::ODEInterfaceIntegrator)(t) = integrator.eval_sol_fcn(t)
8 changes: 6 additions & 2 deletions src/integrator_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ function handle_callbacks!(integrator,eval_sol_fcn)
discrete_modified = false
saved_in_cb = false
if !(typeof(continuous_callbacks)<:Tuple{})
time,upcrossing,idx,counter = find_first_continuous_callback(integrator,continuous_callbacks...)
if time != zero(typeof(integrator.t)) && upcrossing != 0 # if not, then no events
time,upcrossing,event_occured,idx,counter =
find_first_continuous_callback(integrator,continuous_callbacks...)
if event_occured
integrator.event_last_time = true
continuous_modified,saved_in_cb = apply_callback!(integrator,continuous_callbacks[idx],time,upcrossing)
else
integrator.event_last_time = false
end
end
if !(typeof(discrete_callbacks)<:Tuple{})
Expand Down
3 changes: 2 additions & 1 deletion src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ function solve{uType,tType,isinplace,AlgType<:ODEInterfaceAlgorithm}(

opts = DEOptions(saveat_internal,save_everystep,callbacks_internal)
integrator = ODEInterfaceIntegrator(u,uprev,tspan[1],tspan[1],opts,
false,tdir,sizeu,sol,InterpFunction((t)->[t]))
false,tdir,sizeu,sol,
InterpFunction((t)->[t]),false)

outputfcn = OutputFunction(integrator)
o[:OUTPUTFCN] = outputfcn
Expand Down

0 comments on commit cd8c2be

Please sign in to comment.