Hi there!
I am trying to implement an AC voltage component, similar to Voltage
from the standard library. When I try to @mtkbuild
the component on its own, I get an ExtraVariablesSystemException
. However, when used in the RC Circuit example it just works…
I am wondering, if I can ignore the error and if not, what to change.
What am I missing?
Thanks for your help and any advice!
Thomas
Here is my code:
using ModelingToolkit, OrdinaryDiffEq, Plots
using ModelingToolkitStandardLibrary.Electrical
using ModelingToolkitStandardLibrary.Blocks: Constant, RealInput
using ModelingToolkit: t_nounits as t
@mtkmodel ACVoltage begin
@extend v, i = oneport = OnePort()
@parameters begin
φ = 0, [description = "Phase offset in rad"]
f = 50, [description = "Frequency in Hz"]
end
@components begin
V = RealInput()
end
@equations begin
v ~ sin(2π*f*t+φ) * V.u
end
end
@mtkbuild ac_test = ACVoltage() # Gives the mentioned error
@mtkbuild dc_test = Voltage() # Works as expected
@mtkmodel RC begin
@parameters begin
R = 1.0
C = 1.0
V = 1.0
end
@components begin
resistor = Resistor(R = R)
capacitor = Capacitor(C = C, v = 0.0)
source = ACVoltage(f=1)
constant = Constant(k = V)
ground = Ground()
end
@equations begin
connect(constant.output, source.V)
connect(source.p, resistor.p)
connect(resistor.n, capacitor.p)
connect(capacitor.n, source.n, ground.g)
end
end
@mtkbuild sys = RC()
prob = ODEProblem(sys, Pair[], (0, 10.0)) # Works just fine with ACVoltage comp!
sol = solve(prob)
plot(sol, idxs = [sys.capacitor.v, sys.resistor.i],
title = "RC Circuit Demonstration",
labels = ["Capacitor Voltage" "Resistor Current"])