Skip to content

Instantly share code, notes, and snippets.

@dpastoor
Last active March 16, 2017 23:31
Show Gist options
  • Save dpastoor/db9c49507c2126b833d971f9cec91f7f to your computer and use it in GitHub Desktop.
Save dpastoor/db9c49507c2126b833d971f9cec91f7f to your computer and use it in GitHub Desktop.
ode tests in julia
using DifferentialEquations
using ParameterizedFunctions
using DataFrames
type OneCmtOral{T} <: DEDataArray{T}
x::Array{T,1}
KA::T
CL::T
V::T
end
function onecmtoral(t, u, du)
du[1] = -u.KA*u[1]
du[2] = u.KA*u[1] - u[2]*(u.CL/u.V)
end
# try changing the overall drug clearance to 3x
function affect!(integrator)
for c in user_cache(integrator)
c.CL = c.CL*3.0
end
end
const tstop1 = [10.]
function condition(t,u,integrator) # Event when event_f(t,u) == 0
t in tstop1
end
cb = DiscreteCallback(condition, affect!, (true, true))
u0 = OneCmtOral([200.0;0.0], 1.0, 1.3, 12.3)
tspan = (0.0,20.0)
prob = ODEProblem(onecmtoral,u0,tspan)
function sim(prob, cb, tstops)
# saveat does not seem to be working?
#solve(prob, callback = cb, tstops = [10.], saveat=0.25)
#solve(prob, callback = cb, tstops = [10.], saveat=collect([0.:0.25:12.]))
# stop where the discrete condition occurs to make sure the changes happen
solve(prob, callback = cb, tstops = tstops)
end
function get_params(sol)
CL = zeros(length(sol))
V = zeros(length(sol))
pred = zeros(length(sol))
for s in 1:length(sol)
CL[s] = sol[s].CL
V[s] = sol[s].V
pred[s] = sol[s][2]
end
DataFrame(CL = CL, V = V, TIME = sol.t, pred = pred )
end
sol = sim(prob, cb, tstop1)
@time res = get_params(sim(prob, cb, tstop1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment