AC voltage component: system is unbalanced

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"])

Can you show what you got?

Oh I see what you mean.

Yes, this is an incomplete model, in the sense that without defining its connections it’s not simultatable. @mtkbuild is meant to be building final models, and so it’s running that check and mentioning that ac_test is not runnable. You don’t end up using ac_test in your ODEProblem though so it doesn’t end up mattering. It is okay to have partial models which are then connected to build a complete model, in fact every resistor, capacitor, etc. are components which do not make sense in isolation and require being connected to be completed, so it’s not odd or rare to have this kind of component.

Ok, that makes sense. Thanks for the quick answer!

However, if the RealInput is changed to some constant value the ac_test model will be fine. But that makes no sense further down the line for my particular problem :slight_smile:

Cheers
Thomas