Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable naming in states vs. equations #971

Closed
tshort opened this issue Apr 15, 2021 · 3 comments
Closed

Variable naming in states vs. equations #971

tshort opened this issue Apr 15, 2021 · 3 comments

Comments

@tshort
Copy link

tshort commented Apr 15, 2021

I'm experimenting with different ways of composing models. In the process, I uncovered something that might be a bug.

using ModelingToolkit

struct NodeSet end

RefBranch(x, i) = nothing
function RefBranch(n::Num, i)
    meta = Symbolics.getmetadata(n.val, NodeSet, Num[])
    push!(meta, i)
    Symbolics.setmetadata(n.val, NodeSet, meta)
    nothing
end

function Branch(n1, n2, v, i)
    RefBranch(n1, i)
    RefBranch(n2, -i)
    v ~ n1 - n2
end

@parameters t
const D = Differential(t)

function VoltageSource(n1, n2, V; name) 
    @variables i(t)
    @variables v(t)
    eqs = [
        Branch(n1, n2, v, i)
        v ~ V
    ]
    ODESystem(eqs, t, [i, v], [], name=name)
end

function Resistor(n1, n2, R; name) 
    @variables i(t)
    @variables v(t)
    eqs = [
        Branch(n1, n2, v, i)
        v ~ R * i
    ]
    ODESystem(eqs, t, [i, v], [], name=name)
end

function Capacitor(n1, n2, C; name) 
    @variables i(t)
    @variables v(t)
    eqs = [
        Branch(n1, n2, v, i)
        D(v) ~ i / C
    ]
    ODESystem(eqs, t, [i, v], [], name=name)
end

function Subsystem(n1, n2; name)
    @variables vs(t)
    g = 0.0  # A ground has zero volts; it's not a variable.
    systems = [
        Resistor(n1, vs, 10.0, name = :r1)
        Capacitor(vs, g, 5.0e-3, name = :c1)
        Resistor(vs, n2, 10.0, name = :r2)
    ]
    ODESystem(Equation[], t, [vs], [], systems=systems, name=name)
end

function Circuit(; name)
    @variables v1(t) v2(t)
    g = 0.0  # A ground has zero volts; it's not a variable.
    systems = [
        VoltageSource(v1, g, sin(2pi * 60 * t), name = :vsrc)
        Subsystem(v1, v2, name = :ss)
        Resistor(v2, g, 5.0, name = :r)
    ]
    ODESystem(Equation[], t, [v1, v2], [], systems=systems, name=name)
end

@named ckt = Circuit()

After running, I see the following:

julia> states(M.ckt)
13-element Array{Term{Real,Nothing},1}:
 v1(t)
 v2(t)
 vsrc₊i(t)
 vsrc₊v(t)
 ss₊vs(t)
 ss₊r1₊i(t)
 ss₊r1₊v(t)
 ss₊c1₊i(t)
 ss₊c1₊v(t)
 ss₊r2₊i(t)
 ss₊r2₊v(t)
 r₊i(t)
 r₊v(t)

julia> equations(M.ckt)
10-element Array{Equation,1}:
 vsrc₊v(t) ~ vsrc₊v1(t)
 vsrc₊v(t) ~ sin(376.99111843077515t)
 ss₊r1₊v(t) ~ ss₊r1₊v1(t) - ss₊r1₊vs(t)
 ss₊r1₊v(t) ~ 10.0ss₊r1₊i(t)
 ss₊c1₊v(t) ~ ss₊c1₊vs(t)
 Differential(t)(ss₊c1₊v(t)) ~ 200.0ss₊c1₊i(t)
 ss₊r2₊v(t) ~ ss₊r2₊vs(t) - ss₊r2₊v2(t)
 ss₊r2₊v(t) ~ 10.0ss₊r2₊i(t)
 r₊v(t) ~ r₊v2(t)
 r₊v(t) ~ 5.0r₊i(t)

The states look like they have proper naming. In the equations, some of the variables have extra terms. For example, r₊v2(t) should just be v2(t), and ss₊r1₊v1(t) should be v1(t).

@YingboMa
Copy link
Member

This is expected. I think you intend to do inheritance, but the ODESystem(...; systems=systems) approach gives you composition, so you will get one extra level of namespace.

@ChrisRackauckas
Copy link
Member

If you want inheritance instead of composition, you'll want #807 (and hint, it's a good first issue!)

@tshort
Copy link
Author

tshort commented Apr 17, 2021

I don't want inheritance in the sense of Modelica's extends or the combine described in #807. I'm trying to do functional composition that ends up with a hierarchy of variables and equations.

I suspect my attempt at composition is just incompatible with how MTK does composition. I'll try to write up what I'm trying to do to see if that sparks any ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants