Skip to content

Commit

Permalink
Reformulatd state selection, introduced _u
Browse files Browse the repository at this point in the history
For #1412
  • Loading branch information
mwetter committed Nov 12, 2020
1 parent db0c992 commit 3f779ac
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions IBPSA/Fluid/Interfaces/ConservationEquation.mo
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,21 @@ model ConservationEquation "Lumped volume with mass and energy balance"
preferredMediumStates = energyDynamics <> Modelica.Fluid.Types.Dynamics.SteadyState,
p(start=p_start),
h(start=hStart),
u(stateSelect=if ((not computeCSen) and energyDynamics <> Modelica.Fluid.Types.Dynamics.SteadyState) then
StateSelect.prefer else StateSelect.never),
T(start=T_start),
Xi(start=X_start[1:Medium.nXi]),
X(start=X_start),
d(start=rho_start)) "Medium properties";

Modelica.SIunits.Energy U(start=fluidVolume*rho_start*
Medium.specificInternalEnergy(Medium.setState_pTX(
T=T_start,
p=p_start,
X=X_start[1:Medium.nXi])) +
(T_start - Medium.reference_T)*CSen) "Internal energy of fluid";
Modelica.SIunits.Energy U(
stateSelect=StateSelect.never,
start=fluidVolume*rho_start*
Medium.specificInternalEnergy(Medium.setState_pTX(
T=T_start,
p=p_start,
X=X_start[1:Medium.nXi]))
+ (T_start - Medium.reference_T)*CSen) "Internal energy of fluid";

Modelica.SIunits.Mass m(
min=Modelica.Constants.eps,
Expand Down Expand Up @@ -140,6 +144,8 @@ protected
// Density at medium default values, used to compute the size of control volumes
final parameter Modelica.SIunits.Density rho_default=Medium.density(
state=state_default) "Density, used to compute fluid mass";
final parameter Real mInv(final unit="1/kg") = 1/(fluidVolume*rho_default)
"Inverse of mass, used to scale internal state variable";
// Parameter that is used to construct the vector mXi_flow
final parameter Real s[Medium.nXi] = {if Modelica.Utilities.Strings.isEqual(
string1=Medium.substanceNames[i],
Expand All @@ -156,6 +162,22 @@ protected
constant Boolean _simplify_mWat_flow = simplify_mWat_flow and Medium.nX > 1
"If true, then port_a.m_flow + port_b.m_flow = 0 even if mWat_flow is non-zero, and equations are simplified";

// The variable _u is used as a state. In the previous implementation, U
// was a state. However, setting the nominal attribute of U
// requires using the fluidVolume, which is a non-literal expression in some
// models. In this case, Dymola would simply set nominal=1, and the state
// is then not scaled properly, which can cause problems for the integrator.
// Therefore, we introduce _u = U/(fluidVolume*rho_default), for which
// we can set the nominal attribute.
// Note that this is different from using medium.u if the expression
// U = m*medium.u + CSen*(medium.T-Medium.reference_T)
// is used. Therefore, the new variable _u has been introduced.
Medium.SpecificEnergy _u(
stateSelect = if (computeCSen and energyDynamics <> Modelica.Fluid.Types.Dynamics.SteadyState)
then StateSelect.prefer else StateSelect.default,
nominal=1E4)
"Surrogate for specific energy, used as a state variable, and set to _u = U/(fluidVolume*rho_default)";

// Quantities exchanged through the fluid ports
Medium.EnthalpyFlowRate ports_H_flow[nPorts]
"Enthalpy flow rates at the ports";
Expand Down Expand Up @@ -257,8 +279,11 @@ equation
mXi = m*medium.Xi;
if computeCSen then
U = m*medium.u + CSen*(medium.T-Medium.reference_T);
// Internal state variable
_u = U * mInv;
else
U = m*medium.u;
_u = medium.u;
end if;
mC = m*C;

Expand Down Expand Up @@ -427,11 +452,13 @@ IBPSA.Fluid.MixingVolumes.MixingVolume</a>.
</html>", revisions="<html>
<ul>
<li>
November 6, 2020, by Michael Wetter and Filip Jorissen:<br/>
November 12, 2020, by Michael Wetter and Filip Jorissen:<br/>
Changed model to use density instead of mass as a prefered state.
Using mass would require to setting <code>m(nominal=fluidVolume...</code>
but <code>fluidVolume</code> is in some models not a literal value.
Also, set nominal attribute of <code>medium.u</code>.<br/>
Changed model to use either <code>medium.u</code> or a new variable <code>_u</code>
as the state rather than <code>U</code> to avoid using <code>fluidVolume</code>
in the expression for the nominal value.<br/>
This is for
<a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1412\">1412</a>.
</li>
Expand Down

0 comments on commit 3f779ac

Please sign in to comment.