Further problems… I figured out how to make the acausal example in the old documentation work under the new style.
So I tried to use this working style and make my own “library” of components – I want to understand how things work. When doing mtkcompile, there is no error message, and the latexify of the model works and is correct. Then I try to create an ODEProblem, and get an error message:
A completed system is required. Call `complete` or `mtkcompile` on the system before creating a `ODEProblem`.
But I have done mtkcompile!! And if I do complete of the named system before I do mtkcompile, I get another error message:
Encountered operator `Differential(t, 1)` which has different independent variable than the one used in the system `nothing`.
Context:
c₊C*Differential(t, 1)(c₊u(t))
Any ideas?
Note: when I created my own “components”, I did not use the OnePort “container” with extension. Below is my entire code for this case. Note: I used u for voltage instead of v…; u is more common in Europe, and to me v is velocity… Also, I use slightly different names for various things, e.g., a and b for the pins on the one port.
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using OrdinaryDiffEq
@connector function ElectricPort(; name)
vars = @variables begin
u(t), [guess = 0.0]
i(t), [guess = 0.0, connect = Flow]
end
System(Equation[], t, vars, []; name = name)
end
@component function Ground(; name)
@named a = ElectricPort()
eqs = [a.u ~ 0]
compose(System(eqs, t, [], []; name = name), a)
end
@component function Resistor(; name, R = 1.0)
@named a = ElectricPort()
@named b = ElectricPort()
pars = @parameters begin
R = R, [description = "Resistance, Ohm"]
end
vars = @variables begin
u(t), [guess = 0.0]
i(t), [guess = 0.0]
end
eqs = [
u ~ a.u - b.u
0 ~ a.i + b.i
i ~ a.i
u ~ R * i
]
compose(System(eqs, t, vars, pars; name = name), a, b)
end
@component function Capacitor(; name, C = 1.0)
@named a = ElectricPort()
@named b = ElectricPort()
pars = @parameters begin
C = C, [description = "Capacitance, Farad"]
end
vars = @variables begin
u(t), [guess = 0.0]
i(t), [guess = 0.0]
end
eqs = [
u ~ a.u - b.u
0 ~ a.i + b.i
i ~ a.i
i ~ C * D(u)
]
compose(System(eqs, t, vars, pars; name = name), a, b)
end
@component function Inductor(; name, L = 1.0)
@named a = ElectricPort()
@named b = ElectricPort()
pars = @parameters begin
L = L, [description = "Inductance, Henry"]
end
vars = @variables begin
u(t), [guess = 0.0]
i(t), [guess = 0.0]
end
eqs = [
u ~ a.u - b.u
0 ~ a.i + b.i
i ~ a.i
u ~ L * D(i)
]
compose(System(eqs, t, vars, pars; name = name), a, b)
end
@component function VoltageSourceAC(; name, U = 1.0, f = 50.0)
@named a = ElectricPort()
@named b = ElectricPort()
pars = @parameters begin
U = U, [description = "Amplitude, Volt"]
f = f, [description = "Frequency, Hz"]
end
vars = @variables begin
u(t), [guess = 0.0]
end
eqs = [
u ~ U * sin(2 * pi * f * t)
u ~ a.u - b.u
]
compose(System(eqs, t, vars, pars; name = name), a, b)
end
# Parameters
#
R = 20 #, [description = "RCL resistance, ohm"]
R_ell = 40 #, [description = "RCL load resistance, ohm"]
C = 10e-6 #, [description = "RCL capacitance, F"]
L = 10e-3 #, [description = "RCL inductance, H"]
U = 10 #, [description = "Voltage source amplitude, V"]
f = 500 #, [description = "Voltage source frequency, Hz"]
#
#
@named r = Resistor(; R=R)
@named c = Capacitor(; C=C)
@named l = Inductor(; L=L)
@named r_ell = Resistor(; R=R_ell)
@named source = VoltageSourceAC(; U=U, f=f)
@named ground = Ground()
rcl2_eqs = [
connect(source.a, r.a)
connect(r.b, c.a)
connect(r.b, l.a)
connect(l.b, r_ell.a)
connect(c.b, ground.a)
connect(r_ell.b, ground.a)
connect(ground.a, source.b)
]
@named _rclsys2 = System(rcl2_eqs, t)
@named rclsys2 = compose(_rclsys2, [r, c, l, r_ell, source, ground])
#rclsys2 = complete(rclsys2)
sys = mtkcompile(rclsys2)
tspan = (0,5e-3)
prob = ODEProblem(rclsys2, [], tspan)
This gives the indicated error messages.