Skip to content

Instantly share code, notes, and snippets.

@ronisbr
Created January 24, 2017 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronisbr/99cc40134d9a5551738e22622b47c3f4 to your computer and use it in GitHub Desktop.
Save ronisbr/99cc40134d9a5551738e22622b47c3f4 to your computer and use it in GitHub Desktop.
importall Base
import RecursiveArrayTools.recursivecopy!
using DiffEqBase
using OrdinaryDiffEq
type SimType{T} <: AbstractArray{T,1}
x::Array{T,1}
f1::T
end
function SimType{T}(::Type{T}, length::Int64)
return SimType{T}(zeros(length), T(0))
end
function SimType{T}(S::SimType{T})
return SimType(T, length(S.x))
end
function SimType{T}(S::SimType{T}, dims::Dims)
return SimType(T, prod(dims))
end
similar{T}(A::SimType{T}) = begin
R = SimType(A)
R.f1 = A.f1
R
end
similar{T}(A::SimType{T}, ::Type{T}, dims::Dims) = begin
R = SimType(A, dims)
R.f1 = A.f1
R
end
done(A::SimType, i::Int64) = done(A.x,i)
eachindex(A::SimType) = eachindex(A.x)
next(A::SimType, i::Int64) = next(A.x,i)
start(A::SimType) = start(A.x)
length(A::SimType) = length(A.x)
ndims(A::SimType) = ndims(A.x)
size(A::SimType) = size(A.x)
recursivecopy!(B::SimType, A::SimType) = begin
recursivecopy!(B.x, A.x)
B.f1 = A.f1
end
getindex( A::SimType, i::Int...) = (A.x[i...])
setindex!(A::SimType, x, i::Int...) = (A.x[i...] = x)
function f(t,u,du)
display(t)
du[1] = -0.5*u[1] + u.f1
du[2] = -0.5*u[2]
end
const tstop1 = [5.]
const tstop2 = [8.]
const tstop = [5.;8.]
function condition(t,u,integrator)
t in tstop1
end
function affect!(integrator)
integrator.u.f1 = 1.5
integrator.cache.tmp.f1 = 1.5
end
save_positions = (true,true)
cb = DiscreteCallback(condition, affect!, save_positions)
function condition2(t,u,integrator)
t in tstop2
end
function affect2!(integrator)
integrator.u.f1 = -1.5
integrator.cache.tmp.f1 = -1.5
end
save_positions = (false,true)
cb2 = DiscreteCallback(condition2, affect2!, save_positions)
cbs = CallbackSet(cb,cb2)
u0 = SimType{Float64}([10;10], 0.0)
prob = ODEProblem(f,u0,(0.0,10.0))
sol = solve(prob,Tsit5(),callback = cbs, tstops=tstop, dtmin=0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment