Fluid libraries usually have balance volume components with one fluid connector port that fulfill the mass and energy balance and on a different grid components that fulfill the momentum balance. A balance volume component, called junction volume below, should be primarily implemented in the following way (see also the implementation in Modelica.Media.Examples.Utilities.PortVolume):
model JunctionVolume
import SI=Modelica.SIunits;
import Modelica.Media.Examples.Utilities.FluidPort_a;
parameter SI.Volume V = 1e-6 "Fixed size of junction volume";
replaceable package Medium = Modelica.Media.Interfaces.PartialMedium
"Medium model" annotation (choicesAllMatching = true);
FluidPort_a port(redeclare package Medium = Medium);
Medium.BaseProperties medium(preferredMediumStates = true);
SI.Energy U "Internal energy of junction volume";
SI.Mass M "Mass of junction volume";
SI.Mass MX[Medium.nXi] "Independent substance masses of junction volume";
equation
medium.p = port.p;
medium.h = port.h;
medium.Xi = port.Xi;
M = V*medium.d; // mass of JunctionVolume
MX = M*medium.Xi; // mass fractions in JunctionVolume
U = M*medium.u; // internal energy in JunctionVolume
der(M) = port.m_flow; // mass balance
der(MX) = port.mX_flow; // substance mass balance
der(U) = port.H_flow; // energy balance
end JunctionVolume;
Assume the Modelica.Media.Air.SimpleAir medium model is used with the JunctionVolume model above. This medium model uses pressure p and temperature T as independent variables. If the flag "preferredMediumStates" is set to true in the declaration of "medium", then the independent variables of this medium model get the attribute "stateSelect = StateSelect.prefer", i.e., the Modelica translator should use these variables as states, if this is possible. Basically, this means that constraints between the potential states p,T and the potential states U,M are present. A Modelica tool will therefore automatically differentiate medium equations and will use the following equations for code generation (note the equations related to X are removed, because SimpleAir consists of a single substance only):
M = V*medium.d;
U = M*medium.u;
// balance equations
der(M) = port.m_flow;
der(U) = port.H_flow;
// abbreviations introduced to get simpler terms
p = medium.p;
T = medium.T;
d = medium.d;
u = medium.u;
h = medium.h;
// medium equations
d = fd(p,T);
h = fh(p,T);
u = h - p/d;
// equations derived automatically by a Modelica tool due to index reduction
der(U) = der(M)*u + M*der(u);
der(M) = V*der(d);
der(u) = der(h) - der(p)/d - p/der(d);
der(d) = der(fd,p)*der(p) + der(fd,T)*der(T);
der(h) = der(fh,p)*der(p) + der(fd,T)*der(T);
Note, that "der(y,x)" is an operator that characterizes in the example above the partial derivative of y with respect to x (this operator will be included in one of the next Modelica language releases). All media models in this library are written in such a way that at least the partial derivatives of the medium variables with respect to the independent variables are provided, either because the equations are directly given (= symbolic differentiation is possible) or because the derivative of the corresponding function (such as fd above) is provided. A Modelica tool will transform the equations above in differential equations with p and T as states, i.e., will generate equations to compute der(p) and der(T) as function of p and T.
Note, when preferredMediumStates = false, no differentiation will take place and the Modelica translator will use the variables appearing differentiated as states, i.e., M and U. This has the disadvantage that for many media non-linear systems of equations are present to compute the intrinsic properties p, d, T, u, h from M and U.