Skip to content

Help with running simple example #71

@sdwfrost

Description

@sdwfrost

I'm trying to use Causal.jl to re-implement one of my examples using DifferentialEquations here, modularizing it into two systems. It's a simple epidemiological model, with infection followed by recovery. According to the docs, this should be a static system, not a dynamic one, but although Causal.jl breaks the algebraic loop, it results in an error. Code below, noting that I have deliberately kept the code unnecessarily verbose. I'm also not sure how to feed the initial state (which should be [990.0,10.0]) as an input in a static system - do I need to use a memory component?

using Causal

@inline function rate_to_proportion(r::Float64,t::Float64)
    1-exp(-r*t)
end

function infection(t,u,p)
    (S,I) = u
    (β,γ,δt) = p
    ifrac = rate_to_proportion*I,δt)
    inf = ifrac*S
    return [S-inf,I+inf]
end

function recovery(t,u,p)
    (S,I) = u
    (β,γ,δt) = p
    rfrac = rate_to_proportion(γ,δt)
    rec = rfrac*I
    return [S,I-rec]
end

@def_static_system struct InfectionSystem{IP, OP, RO} <: AbstractStaticSystem
      β::Float64 = 0.5/1000
      γ::Float64 = 0.25
      δt::Float64 = 0.1
      p = (β,γ,δt)
      input::IP = Inport(2)
      output::OP = Outport(2)
      readout::RO = (t,u) -> infection(t,u,p)
end

@def_static_system struct RecoverySystem{IP, OP, RO} <: AbstractStaticSystem
      β::Float64 = 0.5/1000
      γ::Float64 = 0.25
      δt::Float64 = 0.1
      p = (β,γ,δt)
      input::IP = Inport(2)
      output::OP = Outport(2)
      readout::RO = (t,u) -> recovery(t,u,p)
end

@defmodel model begin
    @nodes begin
        infsys = InfectionSystem()
        recsys = RecoverySystem()
        writer = Writer(input=Inport(2))
    end
    @branches begin
        infsys[1:2] => recsys[1:2]
        recsys[1:2] => infsys[1:2]
        infsys[1:2] => writer[1:2]
    end
end

simulate!(model, 0.0, 0.1, 0.1) # run for single step

The output is:

[ Info: 2021-04-16T14:42:40.211 Started simulation...
[ Info: 2021-04-16T14:42:40.211 Inspecting model...
┌ Info:     The model has algrebraic loops:[[1, 2]]
└       Trying to break these loops...
[ Info:     Loop [1, 2] is broken
[ Info: 2021-04-16T14:42:41.039 Done.
[ Info: 2021-04-16T14:42:41.039 Initializing the model...
[ Info: 2021-04-16T14:42:41.081 Done...
[ Info: 2021-04-16T14:42:44.434 Running the simulation...
┌ Error: Failed for Causal.LoopBreaker{Outport{Outpin{Float64}}, Causal.var"#926#929"{Causal.var"#951#952"{Vector{Function}, Int64}}, Inpin{Float64}, Outpin{Bool}, Nothing, Base.UUID}(nothing, Outport(numpins:2, eltype:Outpin{Float64}), Causal.var"#926#929"{Causal.var"#951#952"{Vector{Function}, Int64}}(Core.Box(2), Causal.var"#951#952"{Vector{Function}, Int64}(Function[Causal.var"#func#938"{BitVector, InfectionSystem{Inport{Inpin{Float64}}, Outport{Outpin{Float64}}, var"#8#13"{Tuple{Float64, Float64, Float64}}, Inpin{Float64}, Outpin{Bool}, Nothing, Base.UUID}}(Bool[1, 1], InfectionSystem{Inport{Inpin{Float64}}, Outport{Outpin{Float64}}, var"#8#13"{Tuple{Float64, Float64, Float64}}, Inpin{Float64}, Outpin{Bool}, Nothing, Base.UUID}(0.0005, 0.25, 0.1, (0.0005, 0.25, 0.1), Inport(numpins:2, eltype:Inpin{Float64}), Outport(numpins:2, eltype:Outpin{Float64}), var"#8#13"{Tuple{Float64, Float64, Float64}}((0.0005, 0.25, 0.1)), Inpin(eltype:Float64, isbound:true), Outpin(eltype:Bool, isbound:true), nothing, Symbol(""), UUID("fa11882b-390d-4141-a53d-3059090e747b"))), Causal.var"#func#937"{RecoverySystem{Inport{Inpin{Float64}}, Outport{Outpin{Float64}}, var"#18#23"{Tuple{Float64, Float64, Float64}}, Inpin{Float64}, Outpin{Bool}, Nothing, Base.UUID}}(RecoverySystem{Inport{Inpin{Float64}}, Outport{Outpin{Float64}}, var"#18#23"{Tuple{Float64, Float64, Float64}}, Inpin{Float64}, Outpin{Bool}, Nothing, Base.UUID}(0.0005, 0.25, 0.1, (0.0005, 0.25, 0.1), Inport(numpins:2, eltype:Inpin{Float64}), Outport(numpins:2, eltype:Outpin{Float64}), var"#18#23"{Tuple{Float64, Float64, Float64}}((0.0005, 0.25, 0.1)), Inpin(eltype:Float64, isbound:true), Outpin(eltype:Bool, isbound:true), nothing, Symbol(""), UUID("d78e5f28-2174-45da-af02-c054edb68f44")))], 2)), Inpin(eltype:Float64, isbound:true), Outpin(eltype:Bool, isbound:true), nothing, Symbol(""), UUID("2a9ba428-100f-4702-96f1-687044c07531"))
└ @ Causal ~/.julia/packages/Causal/vsleZ/src/models/taskmanager.jl:39

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions