# Component-based model for N reactors connected to a tank

**URL:** <https://discourse.julialang.org/t/component-based-model-for-n-reactors-connected-to-a-tank/119536>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [September 18, 2024, 6:48am UTC](https://discourse.julialang.org/t/component-based-model-for-n-reactors-connected-to-a-tank/119536 "2024-09-18T06:48:26Z")\
**Posts on this page:** 10\
**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:** [September 18, 2024, 6:48am UTC](https://discourse.julialang.org/t/component-based-model-for-n-reactors-connected-to-a-tank/119536/1 "2024-09-18T06:48:26Z")

</div>

Say I’ve got 10 chemical reactors, which all flow into a single tank. The reactors all have different production rates for some species S. I’m interested in the concentration of S in the reactors and in the tank. I guess I should have a Reactor component (then I can have an array of 10 reactors), and a Tank component. How should they be connected? What should be my through/across variables?

I’m a novice here (and not an engineer), but starting from the RC-circuit analogy, my thought is that concentration neither sums like I nor is it shared like V. Mass flow of S and mass flow of H2O do sum, so they feel alright for through variables, but then I guess I’d need two corresponding across variables…? [In the modelica book, they use pressure](https://mbe.modelica.university/components/connectors/fluid_connectors/), but there’s only one of those. Maybe I should use [two MassFlow components](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/API/hydraulic/#ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible.MassFlow) for my two chemical species (S and water), then add an equation that their pressures are equal…? Does that even make sense?

I’m lost! 🙂 An example would be very appreciated.

---

<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:** [September 20, 2024, 8:20am UTC](https://discourse.julialang.org/t/component-based-model-for-n-reactors-connected-to-a-tank/119536/2 "2024-09-20T08:20:16Z")

</div>

From preliminary experiments on my side, it looks like A) one can have a flow-only connector and it works just fine, B) [it can cause a (spurrious?) error message](https://github.com/SciML/ModelingToolkit.jl/issues/3064), C) it can be fixed by adding a dummy across variable.

From this, simply modeling mass-flow-of-H2O and mass-flow-of-S should work.

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [September 20, 2024, 12:30pm UTC](https://discourse.julialang.org/t/component-based-model-for-n-reactors-connected-to-a-tank/119536/3 "2024-09-20T12:30:21Z")

</div>

You have referred to some Modelica resource for inspiration, hence I will continue: in the language specification in the Chapter 15 on [Stream connectors](https://specification.modelica.org/master/stream-connectors.html) they write

> The two basic variable types in a connector – _potential_ (or _across_ ) variable and _flow_ (or _through_ ) variable – are not sufficient to describe in a numerically sound way the bi-directional flow of matter with convective transport of specific quantities, such as specific enthalpy and chemical composition […] This is not possible with across and through variables though. This fundamental problem is addressed in Modelica by introducing a third type of connector variable, called _stream variable_ […]

I guess in ModelingToolkit (assuming that’s the package that you are considering) pretty much the same fundamental limitation holds. Whether a solution is already implemented or not, I do not know, but apparently the coupling between components must be based on three variables and not just two.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [September 20, 2024, 12:36pm UTC](https://discourse.julialang.org/t/component-based-model-for-n-reactors-connected-to-a-tank/119536/4 "2024-09-20T12:36:23Z")

</div>

ModelingToolkit also has `@variables k(t) [connect = Stream]`, but the difficulties referred to in the cited modelica documentation might not come into play in this example? IIRC it only becomes relevant with flow that can change direction, which does not appear to be possible with the reactors and the tank described in the OP?

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [September 20, 2024, 12:53pm UTC](https://discourse.julialang.org/t/component-based-model-for-n-reactors-connected-to-a-tank/119536/5 "2024-09-20T12:53:50Z")

</div>

Honestly, I do not know. Sorry if my comment led to confusion. I am not fluent in these fluid things beyond simple mechanics. I only remember that often two variables like volumetric flow and pressure (in analogy with current and voltage), whose product is power, are not enough if thermal content or mass fractions need to be considered in addition to the fluid doing some (mechanical) work, which triggered my comment. I see, here probably the mechanics of the problem is irrelevant.

---

<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:** [September 21, 2024, 5:43am UTC](https://discourse.julialang.org/t/component-based-model-for-n-reactors-connected-to-a-tank/119536/6 "2024-09-21T05:43:43Z")

</div>

I just talked with @Vinicius_Viena about the state choice for this kind of system for the development of:

> **[GitHub - SciML/ProcessSimulator.jl](https://github.com/SciML/ProcessSimulator.jl)**
>
> Contribute to SciML/ProcessSimulator.jl development by creating an account on GitHub.

Specifically, this change is going on in:

> <https://github.com/SciML/ProcessSimulator.jl/pull/14>
>
> \## Checklist
> 
> \- \[\] Appropriate tests were added
> \- \[\] Any code changes were …done in a way that does not break public API
> \- \[\] All documentation related to code changes were updated
> \- \[\] The new code follows the
> \[contributor guidelines\](https://github.com/SciML/.github/blob/master/CONTRIBUTING.md), in particular the \[SciML Style Guide\](https://github.com/SciML/SciMLStyle) and
> \[COLPRAC\](https://github.com/SciML/COLPRAC).
> \- \[\] Any new documentation only uses public API
>   
> \## Additional context
> 
> Add any other context about the problem here.

I think it could be interesting to gather a few folks into a call because it seems there is some parallel development efforts in this. DM me if interested.

---

<div class="post-metadata">

**Author:** ![Vinicius\_Viena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinicius_viena/32/36579_2.png) [@Vinicius\_Viena](https://discourse.julialang.org/u/Vinicius_Viena)\
**Post date:** [September 21, 2024, 7:55pm UTC](https://discourse.julialang.org/t/component-based-model-for-n-reactors-connected-to-a-tank/119536/7 "2024-09-21T19:55:49Z")

</div>

That seems to be a similar problem for process pressure-driven simulation where flow direction are pressure drop equations are coupled. It might be fruitful to have discussion on it. Totally up for it.

I have been looking into some references (this is a good one) [Pressure-driven dynamic process simulation using a new generic stream object - ScienceDirect](https://www.sciencedirect.com/science/article/pii/S0009250919306621?via=ihub). Also this one from modelica ([https://doc.modelica.org/Modelica%204.0.0/Resources/Documentation/Fluid/Stream-Connectors-Overview-Rationale.pdf](https://doc.modelica.org/Modelica%204.0.0/Resources/Documentation/Fluid/Stream-Connectors-Overview-Rationale.pdf)). Yet, it’s not clear for me how to implement such things in the context of chemical process simulation where hydraulics and other more complicated phenomena (reaction, phase change) are coupled.

---

<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:** [October 23, 2024, 10:15pm UTC](https://discourse.julialang.org/t/component-based-model-for-n-reactors-connected-to-a-tank/119536/8 "2024-10-23T22:15:11Z")

</div>

We have a (unidirectional) flow loop of tank → N reactors → tank (with the reactors in parallel), and it really highlights some of the challenges. In the `tank -> reactor` flow direction, then the reactor inlets (aka ports) all share the same concentration (the concentration of the tank). However, in the `reactor -> tank` flow direction, the concentration of the reactor outlets have to be put in a weighted average to compute the tank inlet’s concentration.

But the `connect` pattern is identical in the two cases! Thus it would seem that we cannot use the same through/across connectors in the tank → reactor direction as we do in the reactor → tank direction. Unless I missed something again.

Thus, what should we do?

- Use different connectors for the two scenarios.
- Use stream connectors?
- Connect the N reactors to N separate ports on the tank. Then we can implement the inflow / outflow logic there. I feel like I’ve seen an example of this in the documentation somewhere?
- Use equations outside the components (i.e. “give up” on components)

Thoughts?

---

<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:** [October 24, 2024, 2:03am UTC](https://discourse.julialang.org/t/component-based-model-for-n-reactors-connected-to-a-tank/119536/9 "2024-10-24T02:03:50Z")

</div>

> [@baggepinnen](#):
>
> IIRC it only becomes relevant with flow that can change direction, which does not appear to be possible with the reactors and the tank described in the OP?

Yes, that is what the [documentation](https://specification.modelica.org/master/stream-connectors.html) suggests:

> The two basic variable types in a connector – _potential_ (or _across_ ) variable and _flow_ (or _through_ ) variable – are not sufficient to describe in a numerically sound way the bi-directional flow of matter with convective transport of specific quantities, such as specific enthalpy and chemical composition.

but I haven’t figured out a way to do this otherwise.

---

<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:** [October 24, 2024, 8:29am UTC](https://discourse.julialang.org/t/component-based-model-for-n-reactors-connected-to-a-tank/119536/10 "2024-10-24T08:29:25Z")

</div>

I asked ChatGPT about doing it without stream connectors and it suggested the `Connect the N reactors to N separate ports on the tank` solution, so I would assume that it’s fairly standard.

> Also this one from modelica ([https://doc.modelica.org/Modelica%204.0.0/Resources/Documentation/Fluid/Stream-Connectors-Overview-Rationale.pdf](https://doc.modelica.org/Modelica%204.0.0/Resources/Documentation/Fluid/Stream-Connectors-Overview-Rationale.pdf)). Yet, it’s not clear for me how to implement such things in the context of chemical process simulation where hydraulics and other more complicated phenomena (reaction, phase change) are coupled.

Yes, I share that feeling. For unidirectional flow, the many-port solution gives complete flexibility about how to combine the quantities. It’s a shame that it’s uglier…
