Skip to content

Unbalanced system error when using "compose" #2027

@ctessum

Description

@ctessum

Hello,

I am trying to compose two systems using the "compose" function. For example, a falling body:

using ModelingToolkit

function newton()
    @variables t
    @variables g(t)
    @variables x(t) v(t)
    D = Differential(t)
    eq = [
        D(v) ~ g,
        D(x) ~ v
    ]

    @named sys = ODESystem(eq)
end

function gravity()
    @variables t
    @variables g(t)
    eq = [g ~ -9.8]
    @named gravity = ODESystem(eq)
end

sys = newton()
grav = gravity()

@named connection = ODESystem([
    sys.g ~ grav.g
])

connected = compose(sys, grav, connection)

simplified_sys = structural_simplify(connected)

In the code above, the dynamics are specified in one system and the amount of gravity is specified in another system, and then I compose them together. The last line of code above gives the error: ERROR: ExtraVariablesSystemException: The system is unbalanced. There are 3 highest order derivative variables and 2 equations.

If I specify everything in one system, however, it works fine:

function newton_combined()
    @variables t
    @variables g(t)
    @variables x(t) v(t)
    D = Differential(t)
    eq = [
        D(v) ~ g,
        D(x) ~ v,
        g ~ -9.8
    ]

    @named sys = ODESystem(eq)
end

sys_combined = newton_combined()
structural_simplify(sys_combined)

I would like to be able to specify things in multiple systems and combine them. In the example above, hypothetically, I might want to use my falling object in different gravity fields or a gravity field that varies with time, and don't want to have to copy and paste the dynamics every time. (Obviously this is a stand-in for a much more complex use case.)

I've moved this over from discourse because based on discussion there it seems that this might be a software bug rather than user error.

Let me know any thoughts anyone might have. Thanks for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions