Skip to content

Instantly share code, notes, and snippets.

@ronisbr
Created December 14, 2016 14:52
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/2883ffd81abcc379a259f13faf2b92ea to your computer and use it in GitHub Desktop.
Save ronisbr/2883ffd81abcc379a259f13faf2b92ea to your computer and use it in GitHub Desktop.
Case 02 - Example of a system with custom type to be solved by DifferentialEquations.jl with right solution
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
callback = @ode_callback begin
if t > 5
for c in cache
c.f1 = 1.5
end
end
@ode_savevalues
end
u0 = SimType{Float64}([10;10], 0.0)
prob = ODEProblem(f,u0,(0.0,10.0))
sol = solve(prob,callback = callback, tstops = collect(0:0.5:10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment