Hi there, I am trying to model a 3 element windkessel circuit (see below) using modelingtoolkit
By code is as following:
using ModelingToolkit
using Plots
using DifferentialEquations
@variables t
D = Differential(t)
@connector function Pin(;name)
sts = @variables v(t)=1.0 i(t)=1.0 [connect = Flow]
ODESystem(Equation[], t, sts, []; name=name)
end
function Ground(;name)
@named g = Pin()
eqs = [g.v ~ 0]
compose(ODESystem(eqs, t, [], []; name=name), g)
end
function OnePort(;name)
@named p = Pin()
@named n = Pin()
sts = @variables v(t)=1.0 i(t)=1.0
eqs = [
v ~ p.v - n.v
0 ~ p.i + n.i
i ~ p.i ]
compose(ODESystem(eqs, t, sts, []; name=name), p, n)
end
function Resistor(;name, R = 1.0)
@named oneport = OnePort()
@unpack v, i = oneport
ps = @parameters R=R
eqs = [ v ~ i * R ]
extend(ODESystem(eqs, t, [], ps; name=name), oneport)
end
function Capacitor(;name, C = 1.0)
@named oneport = OnePort()
@unpack v, i = oneport
ps = @parameters C=C
eqs = [ D(v) ~ i / C ]
extend(ODESystem(eqs, t, [], ps; name=name), oneport)
end
@component function DrivenCurrent(; name, I=1.0, fun)
@named oneport = OnePort()
@unpack i = oneport
ps = @parameters I = I
eqs = [
i ~ I * fun(t)
]
extend(ODESystem(eqs, t, [], ps; name=name), oneport)
end
@component function WK3(; name, Rp=1.0, Rc = 1.0, C=1.0)
@named in = Pin()
@named out = Pin()
sts = @variables Δv(t) = 0.0 i(t) = 0.0
# Parameters are inherited from subcomponents
ps = []
# These are the components the subsystem is made of:
@named Rp = Resistor(R=Rp)
@named Rc = Resistor(R=Rp)
@named C = Capacitor(C=C)
eqs = [
Δv ~ out.v - in.v
0 ~ in.i + out.i
i ~ in.i
connect(in, Rc.p)
connect(Rc.n, Rp.p, C.p)
connect(Rp.n, C.n, out)
]
# and finaly compose the system
compose(ODESystem(eqs, t, sts, ps; name=name), in, out, Rp, Rc, C)
end
@mtkmodel WK3model begin
@components begin
wk3 = WK3(Rp = 0.9, Rc = 0.01, C = 1.1)
source = DrivenCurrent(I = 1.0,fun=sin)
ground = Ground()
end
@equations begin
connect(source.n, wk3.in)
connect(source.p, wk3.out, ground.g)
end
end
@mtkbuild wk3model = WK3model()
u0 = [
wk3model.wk3.in.v => 0.0
]
prob = ODEProblem(wk3model, u0, (0, 10.0))
sol_3_elem = solve(prob, RK4())
and I am getting the following error:
MethodError: no method matching *(::ODESystem, ::Float64)
Closest candidates are:
*(::Any, ::Any, !Matched::Any, !Matched::Any...)
@ Base operators.jl:578
*(!Matched::T, ::T) where T<:Union{Float16, Float32, Float64}
@ Base float.jl:410
*(!Matched::StridedArray{P}, ::Real) where P<:Dates.Period
@ Dates C:\Users\aoife\AppData\Local\Programs\Julia-1.9.3\share\julia\stdlib\v1.9\Dates\src\deprecated.jl:44
...
Stacktrace:
[1] macro expansion
@ C:\Users\aoife\.julia\packages\SymbolicUtils\ssQsQ\src\code.jl:375 [inlined]
[2] macro expansion
@ C:\Users\aoife\.julia\packages\RuntimeGeneratedFunctions\Yo8zx\src\RuntimeGeneratedFunctions.jl:163 [inlined]
[3] macro expansion
@ .\none:0 [inlined]
[4] generated_callfunc
@ .\none:0 [inlined]
[5] (::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :ˍ₋arg3, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xe7136e0c, 0x89ddc800, 0x9f1bc9d7, 0x15f355d5, 0xe306f24a), Nothing})(::Vector{Float64}, ::Vector{Float64}, ::Vector{Float64}, ::Vector{ODESystem}, ::Float64)
@ RuntimeGeneratedFunctions C:\Users\aoife\.julia\packages\RuntimeGeneratedFunctions\Yo8zx\src\RuntimeGeneratedFunctions.jl:150
[6] (::ModelingToolkit.var"#g#544"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :ˍ₋arg3, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xc1b5139c, 0x0030c261, 0x78e5ec20, 0x348b67d2, 0x725601c9), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :ˍ₋arg3, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xe7136e0c, 0x89ddc800, 0x9f1bc9d7, 0x15f355d5, 0xe306f24a), Nothing}})(du::Vector{Float64}, u::Vector{Float64}, p::Tuple{Vector{Float64}, Vector{ODESystem}}, t::Float64)
@ ModelingToolkit C:\Users\aoife\.julia\packages\ModelingToolkit\oIgbi\src\systems\diffeqs\abstractodesystem.jl:367
...
@ C:\Users\aoife\.julia\packages\DiffEqBase\xSmHR\src\solve.jl:943 [inlined]
[25] solve(prob::ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Tuple{Vector{Float64}, Vector{ODESystem}}, ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#g#544"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :ˍ₋arg3, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xc1b5139c, 0x0030c261, 0x78e5ec20, 0x348b67d2, 0x725601c9), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :ˍ₋arg3, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xe7136e0c, 0x89ddc800, 0x9f1bc9d7, 0x15f355d5, 0xe306f24a), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Vector{Symbol}, Symbol, Tuple{Symbol, Symbol}, ModelingToolkit.var"#637#generated_observed#555"{Bool, ODESystem, Dict{Any, Any}, Tuple{Vector{SymbolicUtils.BasicSymbolic{Real}}, Vector{SymbolicUtils.BasicSymbolic{Real}}}}, Nothing, ODESystem}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, args::RK4{typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False})
@ DiffEqBase C:\Users\aoife\.julia\packages\DiffEqBase\xSmHR\src\solve.jl:933
[26] top-level scope
@ c:\Users\aoife\Desktop\School\5 yr\Thesis\Julia\electric_circuit.ipynb:6
I am new to Julia (and modelingtoolkit) so any help/guidance is appreciated!