-
Notifications
You must be signed in to change notification settings - Fork 193
Simulation error when using MoistAir.T_psX #4771
Description
Problem
When using the function T_psX/temperature_psX from the MoistAir package to calculate the temperature based on given pressure, specific entropy and humidity the simulation fails to start with the following error:
Warning: The following error was detected at time: 0
Range subscripting: end out of range
Failed condition: (stop >=1)&&(stop <=a.dims[a_dim])
The stack of functions is:
Modelica.Media.Air.MoistAir.T_psX
Modelica.Media.Air.MoistAir.temperature_psX_Unique17(100000.0, s_out_MoistAir, {0})
The problem seems to be coming from the size of the mass fraction vector X provided to the function.
The equation T_out_MoistAir = medium_MoistAir.temperature_psX(p_out,s_out_MoistAir,{X_out}) leads to a model failure while T_out_MoistAir = medium_MoistAir.temperature_psX(p_out,s_out_MoistAir,{X_out,1-X_out}) runs perfectly.
However the equation T_out_MoistAir = medium_MoistAir.temperature_psX(p_out,s_out_MoistAir,{X_out}) runs perfectly fine when using ReferenceMoistAir or DryAirNasa.
Attached test model to reproduce the error: test_MoistAir_T_psX.zip
Solution
The vector X needs to also include the mass fraction of air since the function T_psX solves the non-linear equation s_pTX - s = 0 with the function s_pTX requiring X={X[Water],X[Air]}. However, the size of X is 1 when calling medium_MoistAir.temperature_psX(p_out,s_out_MoistAir,{X_out}).
The function T_psX can be changed as follow to solve the problem:
function T_psX
"Return temperature as a function of pressure p, specific entropy s and composition X"
extends Modelica.Icons.Function;
input AbsolutePressure p "Pressure";
input SpecificEntropy s "Specific entropy";
input MassFraction[:] X "Mass fractions of composition";
output Temperature T "Temperature";
protected
function f_nonlinear "Solve s_pTX(p,T,X) for T with given s"
extends Modelica.Math.Nonlinear.Interfaces.partialScalarFunction;
input AbsolutePressure p "Pressure";
input SpecificEntropy s "Specific entropy";
input MassFraction[:] X "Mass fractions of composition";
algorithm
y := s_pTX(p=p, T=u, X=X) - s;
end f_nonlinear;
algorithm
// === New implementation ===
if size(X, 1) == nX then
T := Modelica.Math.Nonlinear.solveOneNonlinearEquation(
function f_nonlinear(p=p, s=s, X=X[1:nX]), 190, 647);
else
T := Modelica.Math.Nonlinear.solveOneNonlinearEquation(
function f_nonlinear(p=p, s=s, X=cat(1,X,{1 - sum(X)})), 190, 647);
end if;
// === Initial implementation ===
// T := Modelica.Math.Nonlinear.solveOneNonlinearEquation(
// function f_nonlinear(p=p, s=s, X=X[1:nX]), 190, 647); //initial
end T_psX;
By doing this, if the vector X does not contain the mass fraction of Air then it is included when calling the non-linear solver.