Created
December 15, 2016 16:49
-
-
Save ronisbr/a89021585266af326df7c77d1f0e9943 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
importall Base | |
import RecursiveArrayTools.recursivecopy! | |
using DifferentialEquations | |
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) | |
du[1] = -0.5*u[1] + u.f1 | |
du[2] = -0.5*u[2] | |
end | |
const tstop1 = [3.] | |
const tstop2 = [6.] | |
const tstop = [3.;6.] | |
function event_f(t,u) | |
!(t in tstop1) | |
end | |
function apply_event!(u,cache) | |
display("EVENT 1") | |
for c in cache | |
c.f1 = 1.5 | |
end | |
end | |
function event_f2(t,u) | |
!(t in tstop2) | |
end | |
function apply_event2!(u,cache) | |
display("EVENT 2") | |
for c in cache | |
c.f1 = -2.5 | |
end | |
end | |
const rootfind_event_loc = false | |
const interp_points = 0 | |
callback = @ode_callback begin | |
@ode_event event_f apply_event! rootfind_event_loc interp_points | |
event_occured = false | |
@ode_event event_f2 apply_event2! rootfind_event_loc interp_points | |
end | |
u0 = SimType{Float64}([10;10], 0.0) | |
prob = ODEProblem(f,u0,(0.0,10.0)) | |
sol = solve(prob,callback = callback, tstops=tstop) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment