## StaticStateSelectionStatic State Selection |

This information is part of the Modelica Standard Library maintained by the Modelica Association.

Without pre-caution when implementing a medium model, it is very easy that non-linear algebraic systems of equations occur when using the medium model. In this section it is explained how to avoid non-linear systems of equations that result from unnecessary dynamic state selections.

A medium model should be implemented in such a way that a tool is able to select states of a medium in a balance volume statically (during translation). This is only possible if the medium equations are written in a specific way. Otherwise, a tool has to dynamically select states during simulation. Since medium equations are usually non-linear, this means that non-linear algebraic systems of equations would occur in every balance volume.

It is assumed that medium equations in a balance volume are defined in the following way:

packageMedium = Modelica.Media.Interfaces.PartialMedium; Medium.BaseProperties medium;equation// mass balanceder(M) = port_a.m_flow + port_b.m_flow;der(MX) = port_a_mX_flow + port_b_mX_flow; M = V*medium.d; MX = M*medium.X; // Energy balance U = M*medium.u;der(U) = port_a.H_flow+port_b.H_flow;

**Single Substance Media**

A medium consisting of a single substance
has to define two of "p,T,d,u,h" with
stateSelect=StateSelect.prefer if BaseProperties.preferredMediumstates = **true**
and has to provide the other three variables as function of these
states. This results in:

- static state selection (no dynamic choices).
- a linear system of equations in the two state derivatives.

**Example for a single substance medium**

p, T are preferred states (i.e., StateSelect.prefer is set) and there are three equations written in the form:

d = fd(p,T) u = fu(p,T) h = fh(p,T)

Index reduction leads to the equations:

der(M) = V*der(d)der(U) =der(M)*u + M*der(u)der(d) =der(fd,p)*der(p) +der(fd,T)*der(T)der(u) =der(fu,p)*der(p) +der(fu,T)*der(T)

Note, that **der**(y,x) is the partial derivative of y with respect to x
and that this operator will be introduced in a future version
of the Modelica language.
The above equations imply, that if p,T are provided from the
integrator as states, all functions, such as fd(p,T)
or **der**(fd,p) can be evaluated as function of the states.
The overall system results in a linear system
of equations in **der**(p) and **der**(T) after eliminating
**der**(M), **der**(U), **der**(d), **der**(u) via tearing.

**Counter Example for a single substance medium**

An ideal gas with one substance is written in the form

redeclare model extendsBaseProperties( T(stateSelect=if preferredMediumStates then StateSelect.prefer else StateSelect.default), p(stateSelect=if preferredMediumStates then StateSelect.prefer else StateSelect.default)equationh = h(T); u = h - R*T; p = d*R*T; ...endBaseProperties;

If p, T are preferred states, these equations are **not**
written in the recommended form, because d is not a
function of p and T. If p,T would be states, it would be
necessary to solve for the density:

d = p/(R*T)

If T or R are zero, this results in a division by zero.
A tool does not know that R or T cannot become zero.
Therefore, a tool must assume that p, T **cannot** always be
selected as states and has to either use another static
state selection or use dynamic state selection. The only
other choice for static state selection is d,T, because
h,u,p are given as functions of d,T.
However, as potential states only variables appearing differentiated and variables
declared with StateSelect.prefer or StateSelect.always
are used. Since "d" does not appear differentiated and has
StateSelect.default, it cannot be selected as a state.
As a result, the tool has to select states dynamically
during simulation. Since the equations above are non-linear
and they are utilized in the dynamic state
selection, a non-linear system of equations is present
in every balance volume.

To summarize, for single substance ideal gas media there are the following two possibilities to get static state selection and linear systems of equations:

- Use p,T as preferred states and write the equation for d in the form: d = p/(T*R)
- Use d,T as preferred states and write the equation for p in the form: p = d*T*R

All other settings (other/no preferred states etc.) lead to dynamic state selection and non-linear systems of equations for a balance volume.

**Multiple Substance Media**

A medium consisting of multiple substance
has to define two of "p,T,d,u,h" as well
as the mass fractions Xi with
stateSelect=StateSelect.prefer (if BaseProperties.preferredMediumStates = **true**)
and has to provide
the other three variables as functions of these
states. Only then, static selection is possible
for a tool.

**Example for a multiple substance medium:**

p, T and Xi are defined as preferred states and the equations are written in the form:

d = fp(p,T,Xi); u = fu(p,T,Xi); h = fh(p,T,Xi);

Since the balance equations are written in the form:

M = V*medium.d; MXi = M*medium.Xi;

The variables M and MXi appearing differentiated in the balance equations are provided as functions of d and Xi and since d is given as a function of p, T and Xi, it is possible to compute M and MXi directly from the desired states. This means that static state selection is possible.