Hi there,
I am trying to modelling the circulatory system as an electrical circuit, based on the following image:
My code is as follows, with resistors, capacitors and inductors based on those in the RC model example.
function elastance(t, E_s, E_d, T, T_s1, T_s2)
"""
Inputs:
t Time relative to t=0
E_s Maximum elastance of chamber during systole
E_d Minimum elastance of chamber during diastole
T Period of cardiac cycle
T_s1 Time of systolic phase peak relative to start of cycle
T_s2 Time of end of systolic phase relative to start of cycle
"""
# Find current time relative to start of current cycle
t_pulse = rem(t,T);
# Elastance activation function
e = (t_pulse < T_s1) * cos((t_pulse*pi)/T_s1) +
(t_pulse >= T_s1) * (t_pulse < T_s1) * cos(((t_pulse+T_s2-T_s1)*pi)/T_s1) +
(t_pulse >= T_s2) * (t_pulse < T) * 0
# Elastance
E = E_d + (E_s-E_d)*e/2
return E
end
@mtkmodel Ventricle begin
@components begin
in = Pin()
out = Pin()
end
@variables begin
p(t)
V(t)
end
@parameters begin
E_s = 2
E_d = 1
T = 60/72
T_s1 = 0.25
T_s2 = 0.5
V0 = 5
p0 = 1
E = elastance(t, E_s, E_d, T, T_s1, T_s2)
end
@equations begin
0 ~ in.p - out.p
p ~ in.p
p ~ (V-V0)*E + p0
D(V) ~ in.q + out.q
end
end
@mtkmodel systemic_circulation_model begin
@components begin
source = Ventricle()
ground = Ground()
# Aorta
resistor_ao = Resistor(R = R_ao)
capacitor_ao = Capacitor(C = C_ao)
inductor_ao = Inductor(L = L_ao)
# Arteries
resistor_ar = Resistor(R = R_ar)
capacitor_ar = Capacitor(C = C_ar)
inductor_ar = Inductor(L = L_ar)
# Artioles
resistor_at = Resistor(R = R_at)
# Capilliaries
resistor_cp = Resistor(R = R_cp)
# Veins
resistor_vn = Resistor(R = R_vn)
capacitor_vn = Capacitor(C = C_vn)
end
@equations begin
connect(source.out, inductor_ao.in, capacitor_ao.in)
connect(capacitor_ao.out, ground.g)
connect(inductor_ao.out, resistor_ao.in)
connect(resistor_ao.out, inductor_ar.in, capacitor_ar.in)
connect(capacitor_ar.out, ground.g)
connect(inductor_ar.out, resistor_ar.in)
connect(resistor_ar.out, resistor_at.in)
connect(resistor_at.out, resistor_cp.in)
connect(resistor_cp.out, resistor_vn.in, capacitor_vn.in)
connect(resistor_vn.out, capacitor_vn.out, ground.g)
connect(source.in, ground.g)
end
end
@mtkbuild systemic_circulation = systemic_circulation_model()
@nonamespace u0 = [
systemic_circulation.inductor_ao.q => 50,
systemic_circulation.capacitor_ar.Δp => -50,
systemic_circulation.inductor_ar.q => 50,
systemic_circulation.capacitor_vn.Δp => -20
]
prob = ODEProblem(systemic_circulation, u0, (0, 50.0))
sol= solve(prob, RK4(), reltol=1e-6)
However I am getting the following error:
ArgumentError: SymbolicUtils.BasicSymbolic{Real}[inductor_ao₊q(t), capacitor_ar₊Δp(t), inductor_ar₊q(t), capacitor_vn₊Δp(t)] are missing from the variable map.
Stacktrace:
[1] throw_missingvars(vars::Vector{SymbolicUtils.BasicSymbolic{Real}})
@ ModelingToolkit C:\Users\aoife\.julia\packages\ModelingToolkit\oIgbi\src\variables.jl:122
[2] _varmap_to_vars(varmap::Dict{Num, Int64}, varlist::Vector{SymbolicUtils.BasicSymbolic{Real}}; defaults::Dict{Any, Any}, check::Bool, toterm::typeof(ModelingToolkit.default_toterm))
@ ModelingToolkit C:\Users\aoife\.julia\packages\ModelingToolkit\oIgbi\src\variables.jl:116
[3] varmap_to_vars(varmap::Vector{Pair{Num, Int64}}, varlist::Vector{Any}; defaults::Dict{Any, Any}, check::Bool, toterm::Function, promotetoconcrete::Nothing, tofloat::Bool, use_union::Bool)
@ ModelingToolkit C:\Users\aoife\.julia\packages\ModelingToolkit\oIgbi\src\variables.jl:85
[4] get_u0_p(sys::ODESystem, u0map::Vector{Pair{Num, Int64}}, parammap::SciMLBase.NullParameters; use_union::Bool, tofloat::Bool, symbolic_u0::Bool)
@ ModelingToolkit C:\Users\aoife\.julia\packages\ModelingToolkit\oIgbi\src\systems\diffeqs\abstractodesystem.jl:786
[5] get_u0_p
@ C:\Users\aoife\.julia\packages\ModelingToolkit\oIgbi\src\systems\diffeqs\abstractodesystem.jl:768 [inlined]
[6] process_DEProblem(constructor::Type, sys::ODESystem, u0map::Vector{Pair{Num, Int64}}, parammap::SciMLBase.NullParameters; implicit_dae::Bool, du0map::Nothing, version::Nothing, tgrad::Bool, jac::Bool, checkbounds::Bool, sparse::Bool, simplify::Bool, linenumbers::Bool, parallel::Symbolics.SerialForm, eval_expression::Bool, use_union::Bool, tofloat::Bool, symbolic_u0::Bool, u0_constructor::typeof(identity), kwargs::Base.Pairs{Symbol, Integer, Tuple{Symbol, Symbol, Symbol}, NamedTuple{(:t, :has_difference, :check_length), Tuple{Int64, Bool, Bool}}})
@ ModelingToolkit C:\Users\aoife\.julia\packages\ModelingToolkit\oIgbi\src\systems\diffeqs\abstractodesystem.jl:811
[7] (ODEProblem{true, SciMLBase.AutoSpecialize})(sys::ODESystem, u0map::Vector{Pair{Num, Int64}}, tspan::Tuple{Int64, Float64}, parammap::SciMLBase.NullParameters; callback::Nothing, check_length::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ ModelingToolkit C:\Users\aoife\.julia\packages\ModelingToolkit\oIgbi\src\systems\diffeqs\abstractodesystem.jl:936
[8] ODEProblem
@ C:\Users\aoife\.julia\packages\ModelingToolkit\oIgbi\src\systems\diffeqs\abstractodesystem.jl:929 [inlined]
[9] (ODEProblem{true, SciMLBase.AutoSpecialize})(sys::ODESystem, u0map::Vector{Pair{Num, Int64}}, tspan::Tuple{Int64, Float64})
@ ModelingToolkit C:\Users\aoife\.julia\packages\ModelingToolkit\oIgbi\src\systems\diffeqs\abstractodesystem.jl:929
[10] (ODEProblem{true})(::ODESystem, ::Vector{Pair{Num, Int64}}, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ ModelingToolkit C:\Users\aoife\.julia\packages\ModelingToolkit\oIgbi\src\systems\diffeqs\abstractodesystem.jl:916
[11] (ODEProblem{true})(::ODESystem, ::Vector{Pair{Num, Int64}}, ::Vararg{Any})
@ ModelingToolkit C:\Users\aoife\.julia\packages\ModelingToolkit\oIgbi\src\systems\diffeqs\abstractodesystem.jl:915
...
@ ModelingToolkit C:\Users\aoife\.julia\packages\ModelingToolkit\oIgbi\src\systems\diffeqs\abstractodesystem.jl:912
[13] ODEProblem(::ODESystem, ::Vector{Pair{Num, Int64}}, ::Vararg{Any})
@ ModelingToolkit C:\Users\aoife\.julia\packages\ModelingToolkit\oIgbi\src\systems\diffeqs\abstractodesystem.jl:911
[14] top-level scope
@ c:\Users\aoife\Desktop\School\5 yr\Thesis\Julia\Thesis_code\heart_chambers.ipynb:7
I have done some quick searches and found this and this threads, but don’t quite understand the solutions they came to/they didn’t work in my case.
Any help is appreciated