# Having a component whose equations refer another component

**URL:** <https://discourse.julialang.org/t/having-a-component-whose-equations-refer-another-component/128952>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [May 13, 2025, 9:05am UTC](https://discourse.julialang.org/t/having-a-component-whose-equations-refer-another-component/128952 "2025-05-13T09:05:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [May 13, 2025, 9:05am UTC](https://discourse.julialang.org/t/having-a-component-whose-equations-refer-another-component/128952/1 "2025-05-13T09:05:30Z")

</div>

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).

```julia
@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`.

---

<div class="post-metadata">

**Author:** ![Matthew1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matthew1/32/215134_2.png) [@Matthew1](https://discourse.julialang.org/u/Matthew1)\
**Post date:** [May 13, 2025, 9:22am UTC](https://discourse.julialang.org/t/having-a-component-whose-equations-refer-another-component/128952/2 "2025-05-13T09:22:58Z")

</div>

You can:

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

example:

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

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

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [May 16, 2025, 8:41am UTC](https://discourse.julialang.org/t/having-a-component-whose-equations-refer-another-component/128952/3 "2025-05-16T08:41:33Z")

</div>

After some investigation, it feels like the official solution should be along the lines of [RelativeTemperatureSensor](https://github.com/jbatteh/ModelingToolkitStandardLibrary.jl/blob/8a69c4d4db7c414f1c5ec385ba75f2f6c819dd2d/src/Thermal/HeatTransfer/sensors.jl#L26-L51). Then as long as both `Comp2()` and `GooeyComp2()` both have a port to get the information, they can use the values passed from `Comp1` (or not)
