Having a component whose equations refer another component

I have two components in ModelingToolkit.jl, and I want to have an equation across components, such as comp1.x ~ comp2.y. But I’d like that equation to only be there when Comp2() is used (I have an alternative GooeyComp2() where the equation is unneeded).

@mtkmodel Comp1 begin
     @variables begin
         x(t)
     end
end

@mtkmodel Comp2 begin
     @variables begin
         y(t)
     end
end

@mtkmodel Room begin
    @components begin
        comp1 = Comp1()
        comp2 = Comp2()
    end
end

How should I do that?

  • The natural place to put it is in Room’s equation block, but then I have to remember to put that equation every time I write a Room with a Comp2 (and not when I use a GooeyComp2).
  • I’d like to write it inside of Comp2’s equation block, but I’m not sure how. Is that a use case for scopes? I tried ParentScope(comp1).x ~ y, and it did not work.
  • I guess I could use connectors? In my case, it doesn’t feel so appropriate, and then it’s basically like the first solution above: I’ll have to remember to write the connect equation when I use a Comp2.

You can:

  1. Have a boolean structural parameter
  2. Make use of ifelse in the equation including connect

example:

@mtkmodel Room begin
@structural_parameters begin
complicated::true
end

@equations begin
x ~ ifelse(complicated, Comp2.x, GoeyComp2.C)
end
end
1 Like