# Appendix C Derivation of Stream Equations

This appendix contains a derivation of the equation for stream connectors from chapter 15.

# C.1 Mixing Enthalpy

Consider a connection set with $n$ connectors, and denote the mass flow rates m_flow by $\tilde{m}$. The mixing enthalpy is defined by the mass balance (the general mass-balance for a component has $\dot{m}=\sum\tilde{m}$ which simplifies for the mixing enthalpy where $m=0$ and thus $\dot{m}=0$)

 $0=\sum_{j=1}^{n}\tilde{m}_{j}$

and similarly the energy balance

 $0=\sum_{j=1}^{n}\tilde{H}_{j}$

with

 $\tilde{H}_{j}=\tilde{m}_{j}\begin{cases}h_{\mathrm{mix}}&\text{if \tilde{m}_{% j}>0}\\ h_{\mathrm{outflow},j}&\text{if \tilde{m}_{j}<=0}\end{cases}$

Herein, mass flow rates are positive when entering models (exiting the connection set). The specific enthalpy represents the specific enthalpy inside the component, close to the connector, for the case of outflow. Expressed with variables used in the balance equations we arrive at:

 $h_{\mathrm{outflow},j}=\begin{cases}\frac{\tilde{H}_{j}}{\tilde{m}_{j}}&\text{% if \tilde{m}_{j}<0}\\ \textrm{arbitrary}&\text{if \tilde{m}_{j}\geq 0}\end{cases}$

While these equations are suitable for device-oriented modeling, the straightforward usage of this definition leads to models with discontinuous residual equations, which violates the prerequisites of several solvers for nonlinear equation systems. This is the reason why the actual mixing enthalpy is not modelled directly in the model equations. The stream connectors provide a suitable alternative.

# C.2 Rationale for inStream

For simplicity, the derivation of inStream is shown at hand of 3 model components that are connected together. The case for $N$ connections follows correspondingly.

The energy and mass balance equations for the connection set for 3 components are (see above):

 $\begin{split}0=&\tilde{m}_{1}\cdot\begin{cases}h_{\mathrm{mix}}&\text{if % \tilde{m}_{1}>0}\\ h_{\mathrm{outflow},1}&\text{if \tilde{m}_{1}\leq 0}\end{cases}\\ +&\tilde{m}_{2}\cdot\begin{cases}h_{\mathrm{mix}}&\text{if \tilde{m}_{2}>0}% \\ h_{\mathrm{outflow},2}&\text{if \tilde{m}_{2}\leq 0}\end{cases}\\ +&\tilde{m}_{3}\cdot\begin{cases}h_{\mathrm{mix}}&\text{if \tilde{m}_{3}>0}% \\ h_{\mathrm{outflow},3}&\text{if \tilde{m}_{3}\leq}\end{cases}\end{split}$ (C.1a) $0=\tilde{m}_{1}+\tilde{m}_{2}+\tilde{m}_{3}$ (C.1b)

The balance equations are implemented using a $\operatorname{max}$ operator in place of the piecewise expressions, taking care of the different flow directions:

 $\begin{split}0=&\operatorname{max}(\tilde{m}_{1},0)h_{\mathrm{mix}}-% \operatorname{max}(-\tilde{m}_{1},0)h_{\mathrm{outflow},1}\\ +&\operatorname{max}(\tilde{m}_{2},0)h_{\mathrm{mix}}-\operatorname{max}(-% \tilde{m}_{2},0)h_{\mathrm{outflow},2}\\ +&\operatorname{max}(\tilde{m}_{3},0)h_{\mathrm{mix}}-\operatorname{max}(-% \tilde{m}_{3},0)h_{\mathrm{outflow},3}\end{split}$ (C.2a)
 $\begin{split}0=&\operatorname{max}(\tilde{m}_{1},0)-\operatorname{max}(-\tilde% {m}_{1},0)\\ +&\operatorname{max}(\tilde{m}_{2},0)-\operatorname{max}(-\tilde{m}_{2},0)\\ +&\operatorname{max}(\tilde{m}_{3},0)-\operatorname{max}(-\tilde{m}_{3},0)\end% {split}$ (C.2b)

Equation (C.2a) is solved for $h_{\mathrm{mix}}$

 $h_{\mathrm{mix}}=\frac{\operatorname{max}(-\tilde{m}_{1},0)h_{\mathrm{outflow}% ,1}+\operatorname{max}(-\tilde{m}_{2},0)h_{\mathrm{outflow},2}+\operatorname{% max}(-\tilde{m}_{3},0)h_{\mathrm{outflow},3}}{\operatorname{max}(\tilde{m}_{1}% ,0)+\operatorname{max}(\tilde{m}_{2},0)+\operatorname{max}(\tilde{m}_{3},0)}$

Using (C.2b), the denominator can be changed to:

 $h_{\mathrm{mix}}=\frac{\operatorname{max}(-\tilde{m}_{1},0)h_{\mathrm{outflow}% ,1}+\operatorname{max}(-\tilde{m}_{2},0)h_{\mathrm{outflow},2}+\operatorname{% max}(-\tilde{m}_{3},0)h_{\mathrm{outflow},3}}{\operatorname{max}(-\tilde{m}_{1% },0)+\operatorname{max}(-\tilde{m}_{2},0)+\operatorname{max}(-\tilde{m}_{3},0)}$

Above it was shown that an equation of this type does not yield properly formulated model equations. In the streams concept we therefore decide to split the energy balance, which consists of different branches depending on the mass flow direction. Consequently, separate energy balances are the result; each valid for specific flow directions.

In a model governing equations have to establish the specific enthalpy of fluid leaving the model based on the specific enthalpy of fluid flowing into it. Whenever the mixing enthalpy is used in a model it is therefore the mixing enthalpy under the assumption of fluid flowing into said model.

We establish this quantity using a dedicated operator ${\text{\lstinline{{\lst@@@set@language\lst@@@set@numbers\lst@@@set@frame% \lst@@@set@rulecolor\lst@@@set@language\lst@@@set@language\lst@@@set@frame% \normalsize{\@listingGroup{ltx_lst_keywords3}{\color[rgb]{0.33,0.24,0.03}inStr% eam}}}}}}(h_{\mathrm{outflow},i})=h_{\mathrm{mix}}$ assuming that $\tilde{m}_{i}\geq 0$. This leads to three different incarnations of ($n$ in the general case). This is illustrated in the figure below. For the present example of three components in a connection set, this means the following.

 ${\displaystyle\text{\lstinline{{\lst@@@set@language\lst@@@set@numbers% \lst@@@set@frame\lst@@@set@rulecolor\lst@@@set@language\lst@@@set@language% \lst@@@set@frame\normalsize{\@listingGroup{ltx_lst_keywords3}{\color[rgb]{% 0.33,0.24,0.03}inStream}}}}}}(h_{\mathrm{outflow},1})$ $\displaystyle=\frac{\operatorname{max}(-\tilde{m}_{2},0)h_{\mathrm{outflow},2}% +\operatorname{max}(-\tilde{m}_{3},0)h_{\mathrm{outflow},3}}{\operatorname{max% }(-\tilde{m}_{2},0)+\operatorname{max}(-\tilde{m}_{3},0)}$ ${\displaystyle\text{\lstinline{{\lst@@@set@language\lst@@@set@numbers% \lst@@@set@frame\lst@@@set@rulecolor\lst@@@set@language\lst@@@set@language% \lst@@@set@frame\normalsize{\@listingGroup{ltx_lst_keywords3}{\color[rgb]{% 0.33,0.24,0.03}inStream}}}}}}(h_{\mathrm{outflow},2})$ $\displaystyle=\frac{\operatorname{max}(-\tilde{m}_{1},0)h_{\mathrm{outflow},1}% +\operatorname{max}(-\tilde{m}_{3},0)h_{\mathrm{outflow},3}}{\operatorname{max% }(-\tilde{m}_{1},0)+\operatorname{max}(-\tilde{m}_{3},0)}$ ${\displaystyle\text{\lstinline{{\lst@@@set@language\lst@@@set@numbers% \lst@@@set@frame\lst@@@set@rulecolor\lst@@@set@language\lst@@@set@language% \lst@@@set@frame\normalsize{\@listingGroup{ltx_lst_keywords3}{\color[rgb]{% 0.33,0.24,0.03}inStream}}}}}}(h_{\mathrm{outflow},3})$ $\displaystyle=\frac{\operatorname{max}(-\tilde{m}_{1},0)h_{\mathrm{outflow},1}% +\operatorname{max}(-\tilde{m}_{2},0)h_{\mathrm{outflow},2}}{\operatorname{max% }(-\tilde{m}_{1},0)+\operatorname{max}(-\tilde{m}_{2},0)}$

In the general case of a connection set with n components, similar considerations lead to the following.

 ${\text{\lstinline{{\lst@@@set@language\lst@@@set@numbers\lst@@@set@frame% \lst@@@set@rulecolor\lst@@@set@language\lst@@@set@language\lst@@@set@frame% \normalsize{\@listingGroup{ltx_lst_keywords3}{\color[rgb]{0.33,0.24,0.03}inStr% eam}}}}}}(h_{\mathrm{outflow},i})=\frac{\sum_{j=1,\dotsc,n;j\neq i}% \operatorname{max}(-\tilde{m}_{j},0)h_{\mathrm{outflow},j}}{\sum_{j=1,\dotsc,n% ;j\neq i}\operatorname{max}(-\tilde{m}_{j},0)}$

# C.3 Special Cases Covered by inStream Definition

## C.3.1 Unconnected Stream Connector – 1 Stream Connector

For this case, the return value of inStream is arbitrary. Therefore, it is set to the outflow value.

## C.3.2 One to One Connections – Connection of 2 Stream Connectors

 ${\displaystyle\text{\lstinline{{\lst@@@set@language\lst@@@set@numbers% \lst@@@set@frame\lst@@@set@rulecolor\lst@@@set@language\lst@@@set@language% \lst@@@set@frame\normalsize{\@listingGroup{ltx_lst_keywords3}{\color[rgb]{% 0.33,0.24,0.03}inStream}}}}}}(h_{\mathrm{outflow},1})$ $\displaystyle=\frac{\operatorname{max}(-\tilde{m}_{2},0)h_{\mathrm{outflow},2}% }{\operatorname{max}(-\tilde{m}_{2},0)}=h_{\mathrm{outflow},2}$ ${\displaystyle\text{\lstinline{{\lst@@@set@language\lst@@@set@numbers% \lst@@@set@frame\lst@@@set@rulecolor\lst@@@set@language\lst@@@set@language% \lst@@@set@frame\normalsize{\@listingGroup{ltx_lst_keywords3}{\color[rgb]{% 0.33,0.24,0.03}inStream}}}}}}(h_{\mathrm{outflow},2})$ $\displaystyle=\frac{\operatorname{max}(-\tilde{m}_{1},0)h_{\mathrm{outflow},1}% }{\operatorname{max}(-\tilde{m}_{1},0)}=h_{\mathrm{outflow},1}$

In this case, inStream is continuous (contrary to $h_{\mathrm{mix}}$) and does not depend on flow rates. The latter result means that this transformation may remove nonlinear systems of equations, which requires that either simplifications of the form $a*b/a=b$ must be provided, or that this case is treated directly.

## C.3.3 Zero Mass Flow Rate – Connection of 3 Stream Connectors

The case where $N=3$ and $\tilde{m}_{3}=0$ occurs when a one-port sensor (like a temperature sensor) is connected to two connected components. For the sensor, the min attribute of the mass flow rate should be set to zero (no fluid exiting the component via this connector). This simplification (and similar ones) can also be used if a tool determines that a mass flow rate is zero or non-negative. It is also possible to generalize this to the case where more than one sensor is connected. The suggested implementation results in the following equations, and as indicated the last formula can be simplified further by using $\tilde{m}_{3}=0$:

 ${\displaystyle\text{\lstinline{{\lst@@@set@language\lst@@@set@numbers% \lst@@@set@frame\lst@@@set@rulecolor\lst@@@set@language\lst@@@set@language% \lst@@@set@frame\normalsize{\@listingGroup{ltx_lst_keywords3}{\color[rgb]{% 0.33,0.24,0.03}inStream}}}}}}(h_{\mathrm{outflow},1})$ $\displaystyle=h_{\mathrm{outflow},2}$ ${\displaystyle\text{\lstinline{{\lst@@@set@language\lst@@@set@numbers% \lst@@@set@frame\lst@@@set@rulecolor\lst@@@set@language\lst@@@set@language% \lst@@@set@frame\normalsize{\@listingGroup{ltx_lst_keywords3}{\color[rgb]{% 0.33,0.24,0.03}inStream}}}}}}(h_{\mathrm{outflow},2})$ $\displaystyle=h_{\mathrm{outflow},1}$ ${\displaystyle\text{\lstinline{{\lst@@@set@language\lst@@@set@numbers% \lst@@@set@frame\lst@@@set@rulecolor\lst@@@set@language\lst@@@set@language% \lst@@@set@frame\normalsize{\@listingGroup{ltx_lst_keywords3}{\color[rgb]{% 0.33,0.24,0.03}inStream}}}}}}(h_{\mathrm{outflow},3})$ $\displaystyle=\frac{\operatorname{max}(-\tilde{m}_{1},0)h_{\mathrm{outflow},1}% +\operatorname{max}(-\tilde{m}_{2},0)h_{\mathrm{outflow},2}}{\operatorname{max% }(-\tilde{m}_{1},0)+\operatorname{max}(-\tilde{m}_{2},0)}$ \displaystyle=\begin{cases}\hidden@noalign{}\textstyle h_{\mathrm{outflow},2}&% \text{if \tilde{m}_{1}\geq 0}\\ \hidden@noalign{}\textstyle h_{\mathrm{outflow},1}&\text{if \tilde{m}_{1}<0 % and \tilde{m}_{3}=0}\end{cases}

For the two components with finite mass flow rates (not the sensor), the properties discussed for two connected components still hold. The connection set equations reflect that the sensor does not any influence by discarding the flow rate of the latter. In several cases a non-linear equation system is removed by this transformation. However, inStream results in a discontinuous equation for the sensor, which is consistent with modeling the convective phenomena only. The discontinuous equation is uncritical, if the sensor variable is not used in a feedback loop with direct feedthrough, since the discontinuous equation is then not part of an algebraic loop. Otherwise, it is advisable to regularize or filter the sensor signal.

## C.3.4 Ideal Splitting Junction for Uni-Directional Flow - Connection of 3 Stream Connectors where Two Mass Flow Rates are Positive

If uni-directional flow is present and an ideal splitter is modelled, the required flow direction should be defined in the connector instance with the min attribute (the max attribute could be also defined, however it does not lead to simplifications):

model m2
Fluidport c(m_flow(min=0));
$\ldots$
end m2;

Consider the case of and all other mass flow rates positive (with the min attribute set accordingly). Connecting m1.c with m2.c and m3.c, such that

m2.c.m_flow.min = 0; // max(-m2.c.m_flow,0) = 0
m3.c.m_flow.min = 0; // max(-m3.c.m_flow,0) = 0

results in the following equation:

 ${\text{\lstinline{{\lst@@@set@language\lst@@@set@numbers\lst@@@set@frame% \lst@@@set@rulecolor\lst@@@set@language\lst@@@set@language\lst@@@set@frame% \normalsize{\@listingGroup{ltx_lst_keywords3}{\color[rgb]{0.33,0.24,0.03}inStr% eam}}}}}}(h_{\mathrm{outflow},1})=\frac{\operatorname{max}(-\tilde{m}_{2},0)h_% {\mathrm{outflow},2}+\operatorname{max}(-\tilde{m}_{3},0)h_{\mathrm{outflow},3% }}{\operatorname{max}(-\tilde{m}_{2},0)+\operatorname{max}(-\tilde{m}_{3},0)}=% \frac{0}{0}$

inStream cannot be evaluated for a connector, on which the mass flow rate has to be negative by definition. The reason is that the value is arbitrary, which is why it is defined as follows.

 ${\text{\lstinline{{\lst@@@set@language\lst@@@set@numbers\lst@@@set@frame% \lst@@@set@rulecolor\lst@@@set@language\lst@@@set@language\lst@@@set@frame% \normalsize{\@listingGroup{ltx_lst_keywords3}{\color[rgb]{0.33,0.24,0.03}inStr% eam}}}}}}(h_{\mathrm{outflow},1}):=h_{\mathrm{outflow},1}$

For the remaining connectors, inStream reduces to a simple result.

 ${\displaystyle\text{\lstinline{{\lst@@@set@language\lst@@@set@numbers% \lst@@@set@frame\lst@@@set@rulecolor\lst@@@set@language\lst@@@set@language% \lst@@@set@frame\normalsize{\@listingGroup{ltx_lst_keywords3}{\color[rgb]{% 0.33,0.24,0.03}inStream}}}}}}(h_{\mathrm{outflow},2})$ $\displaystyle=\frac{\operatorname{max}(-\tilde{m}_{1},0)h_{\mathrm{outflow},1}% +\operatorname{max}(-\tilde{m}_{3},0)h_{\mathrm{outflow},3}}{\operatorname{max% }(-\tilde{m}_{1},0)+\operatorname{max}(-\tilde{m}_{3},0)}=h_{\mathrm{outflow},1}$ ${\displaystyle\text{\lstinline{{\lst@@@set@language\lst@@@set@numbers% \lst@@@set@frame\lst@@@set@rulecolor\lst@@@set@language\lst@@@set@language% \lst@@@set@frame\normalsize{\@listingGroup{ltx_lst_keywords3}{\color[rgb]{% 0.33,0.24,0.03}inStream}}}}}}(h_{\mathrm{outflow},3})$ $\displaystyle=\frac{\operatorname{max}(-\tilde{m}_{1},0)h_{\mathrm{outflow},1}% +\operatorname{max}(-\tilde{m}_{2},0)h_{\mathrm{outflow},2}}{\operatorname{max% }(-\tilde{m}_{1},0)+\operatorname{max}(-\tilde{m}_{2},0)}=h_{\mathrm{outflow},1}$

Again, the previous non-linear algebraic system of equations is removed. This means that utilizing the information about uni-directional flow is very important.

To summarize, if all mass flow rates are zero, the balance equations for stream variables (C.1b) and for flows (C.2b) are identically fulfilled. In such a case, any value of $h_{\mathrm{mix}}$ fulfills (C.1b), i.e., a unique mathematical solution does not exist. This specification only requires that a solution fulfills the balance equations. Additionally, a recommendation is given to compute all unknowns in a unique way, by providing an explicit formula for inStream. Due to the definition, that only flows where the corresponding min attribute is neither zero nor positive enter this formula, a meaningful physcial result is always obtained, even in case of zero mass flow rate. As a side effect, non-linear equation systems are automatically removed in special cases, like sensors or uni-directional flow, without any symbolic transformations (no equation must be analyzed; only the min attributes of the corresponding flow variables).