# AC voltage component: system is unbalanced

**URL:** <https://discourse.julialang.org/t/ac-voltage-component-system-is-unbalanced/126904>\
**Category:** Modelling & Simulations\
**Created:** [March 13, 2025, 10:43am UTC](https://discourse.julialang.org/t/ac-voltage-component-system-is-unbalanced/126904 "2025-03-13T10:43:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sasrigais](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sasrigais/32/215762_2.png) [@sasrigais](https://discourse.julialang.org/u/sasrigais)\
**Post date:** [March 13, 2025, 10:43am UTC](https://discourse.julialang.org/t/ac-voltage-component-system-is-unbalanced/126904/1 "2025-03-13T10:43:11Z")

</div>

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:

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

```

 ![Plot](https://global.discourse-cdn.com/julialang/original/3X/6/9/691deac36fd5942fc4e9d6eb5c4fc71c95ac27cf.png)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 13, 2025, 4:26pm UTC](https://discourse.julialang.org/t/ac-voltage-component-system-is-unbalanced/126904/2 "2025-03-13T16:26:49Z")

</div>

> [@sasrigais](#):
>
> `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.

Can you show what you got?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 13, 2025, 4:29pm UTC](https://discourse.julialang.org/t/ac-voltage-component-system-is-unbalanced/126904/3 "2025-03-13T16:29:59Z")

</div>

Oh I see what you mean.

> [@sasrigais](#):
>
> `@mtkbuild ac_test = ACVoltage() # Gives the mentioned error`

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.

---

<div class="post-metadata">

**Author:** ![sasrigais](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sasrigais/32/215762_2.png) [@sasrigais](https://discourse.julialang.org/u/sasrigais)\
**Post date:** [March 13, 2025, 4:47pm UTC](https://discourse.julialang.org/t/ac-voltage-component-system-is-unbalanced/126904/4 "2025-03-13T16:47:07Z")

</div>

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 🙂

Cheers  
Thomas
