epirecipes / sir-julia

Various implementations of the classical SIR model in Julia
MIT License
195 stars 39 forks source link

Add PyCall RHS #116

Closed sdwfrost closed 2 months ago

sdwfrost commented 2 months ago

In the spirit of diffeqpy, plug in a Python RHS for an ODE or discrete system rather than a Julia one.

sdwfrost commented 2 months ago

The following works:

using OrdinaryDiffEq
using ModelingToolkit
using PythonCall

@pyexec """
def sir_ode_py(u,p,t):
    S = u[0]
    I = u[1]
    R = u[2]
    N = S+I+R
    beta = p[0]
    c = p[1]
    gamma = p[2]
    dS = -beta*c*I/N*S
    dI = beta*c*I/N*S - gamma*I
    dR = gamma*I
    return [dS,dI,dR]
""" => sir_ode_py

sir_ode_jl(u,p,t) = pyconvert(Array, sir_ode_py(u, p, t))

δt = 0.1
tmax = 40.0
tspan = (0.0,tmax)
u0 = [990.0,10.0,0.0] # S,I,R
p = [0.05,10.0,0.25] # β,c,γ
prob = ODEProblem{false}(sir_ode_jl, u0, tspan, p)
@named sys = modelingtoolkitize(prob)
prob_mtk = ODEProblem(sys, u0, tspan, p)
sol_mtk = solve(prob_mtk, Tsit5(), dt = δt)

I need to add an in-place version too.

sdwfrost commented 2 months ago

Closed with 8045d5c020b47bbb8443622edff787f749cf1f92