Skip to content

Commit 8bc6ac3

Browse files
casellamaltelenz
andauthored
Fix Modelica.Media.R134a.R134a_ph.setState_pTX (#4626)
* Fixed R134a dofpT() to always converge in all cases * Added test of fixed R134a to ModelicaTest. The test is based on asserts, no need to generate reference results. * Remove trailing whitespace. * Add empty comparisonSignals.txt for test intended to be tested with asserts only. --------- Co-authored-by: Malte Lenz <maltel@wolfram.com>
1 parent 67fbb95 commit 8bc6ac3

3 files changed

Lines changed: 68 additions & 7 deletions

File tree

Modelica/Media/R134a.mo

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,6 +2473,12 @@ This function integrates the derivative of temperature w.r.t. time in order to a
24732473
output SI.Density d "Density";
24742474

24752475
protected
2476+
constant Real p_breaks[:]=R134aData.pbreaks
2477+
"Grid points of reduced pressure";
2478+
constant Real dl_coef[:, 4]=R134aData.dlcoef
2479+
"Coefficients of cubic spline for rho_liq(p)";
2480+
constant Real dv_coef[:, 4]=R134aData.dvcoef
2481+
"Coefficients of cubic spline for rho_vap(p)";
24762482
constant Real T_breaks[:]=R134aData.Tbreaks
24772483
"Grid points of reduced temperature";
24782484
constant Real dlt_coef[:, 4]=R134aData.dltcoef
@@ -2481,8 +2487,10 @@ This function integrates the derivative of temperature w.r.t. time in order to a
24812487
"Coefficients of cubic spline for rho_vap(T)";
24822488

24832489
Boolean liquid "Is liquid";
2484-
Boolean supercritical "Is supercritcal";
2490+
Boolean supercritical "Is supercritical (p > pcrit)";
2491+
Boolean highT "= true for high temperature";
24852492
Integer int "Interval number";
2493+
Real pred "Reduced pressure";
24862494
Real Tred "Reduced temperature";
24872495
Real localx "Abscissa of local spline";
24882496
Integer i "Newton iteration number";
@@ -2500,9 +2508,18 @@ This function integrates the derivative of temperature w.r.t. time in order to a
25002508
i := 0;
25012509
error := 0;
25022510
found := false;
2511+
pred := p/R134aData.data.FPCRIT;
25032512
Tred := T/R134aData.data.FTCRIT;
2504-
(int,error) := Common.FindInterval(Tred, T_breaks);
2505-
localx := Tred - T_breaks[int];
2513+
highT := (Tred > 0.95);
2514+
// Guess values for density computed as function of p for high temperature, as function of T for low Temperature
2515+
// this improves convergence properties (see discussion in #3695)
2516+
if highT then
2517+
(int,error) := Common.FindInterval(pred, p_breaks);
2518+
localx := pred - p_breaks[int];
2519+
else
2520+
(int,error) := Common.FindInterval(Tred, T_breaks);
2521+
localx := Tred - T_breaks[int];
2522+
end if;
25062523
// set decent initial guesses for d and T
25072524
supercritical := p > R134aData.data.FPCRIT;
25082525
if supercritical then
@@ -2511,11 +2528,11 @@ This function integrates the derivative of temperature w.r.t. time in order to a
25112528
else
25122529
liquid := T <= Modelica.Media.R134a.R134a_ph.saturationTemperature(p);
25132530
if liquid then
2514-
d := R134aData.data.FDCRIT*Common.CubicSplineEval(localx, dlt_coef[int,
2515-
1:4])*1.02;
2531+
d := if highT then R134aData.data.FDCRIT*Common.CubicSplineEval(localx, dl_coef[int,1:4])*1.02
2532+
else R134aData.data.FDCRIT*Common.CubicSplineEval(localx, dlt_coef[int,1:4])*1.02;
25162533
else
2517-
d := R134aData.data.FDCRIT*Common.CubicSplineEval(localx, dvt_coef[int,
2518-
1:4])*0.95;
2534+
d := if highT then R134aData.data.FDCRIT*Common.CubicSplineEval(localx, dv_coef[int,1:4])*0.95
2535+
else R134aData.data.FDCRIT*Common.CubicSplineEval(localx, dvt_coef[int,1:4])*0.95;
25192536
end if;
25202537
end if;
25212538

ModelicaTest/Media.mo

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,49 @@ is given to compare the approximation.
971971
annotation (experiment(StopTime=1));
972972
end R134a_setState_phX;
973973

974+
model R134a_pTX_phX_all "Tests the R134a_ph.setState_pTX and setState_phX functions for consistency over the entire valid range of p and T"
975+
extends Modelica.Icons.Example;
976+
package Medium = Modelica.Media.R134a.R134a_ph;
977+
parameter Medium.AbsolutePressure pmin = 0.0042e5;
978+
parameter Medium.AbsolutePressure pmax = 700e5;
979+
parameter Modelica.Units.SI.PerUnit p_increase = 2;
980+
parameter Medium.Temperature Tmin = 190;
981+
parameter Medium.Temperature Tmax = 450;
982+
983+
Medium.ThermodynamicState state = Medium.setState_pTX(p,T);
984+
Medium.Temperature Tsat = Medium.saturationTemperature(p);
985+
Medium.AbsolutePressure p;
986+
Medium.Temperature T;
987+
Medium.Density d = Medium.density(state);
988+
Medium.SpecificEnthalpy h = Medium.specificEnthalpy(state);
989+
Medium.ThermalConductivity k = Medium.thermalConductivity(state);
990+
991+
Medium.ThermodynamicState state_check = Medium.setState_phX(p,h);
992+
Medium.Temperature T_check = Medium.temperature(state_check);
993+
Medium.Density d_check = Medium.density(state_check);
994+
995+
Modelica.Units.SI.PerUnit dT;
996+
initial equation
997+
p = pmin;
998+
T = Tmin;
999+
dT = 100*45;
1000+
equation
1001+
der(T) = dT;
1002+
when abs(T - Tsat) < 2 then
1003+
// skip region close to Tsat
1004+
reinit(T, Tsat+4*sign(dT));
1005+
end when;
1006+
when {T > Tmax, T < Tmin} then
1007+
dT = -pre(dT);
1008+
p = pre(p)*p_increase;
1009+
end when;
1010+
assert(0.7e5 < h and h < 6e5, "Specific enthalpy h out of bounds");
1011+
assert(d > 0 and d < 1600, "Density out of bounds");
1012+
assert(abs(T - T_check)/T < 1e-3 and abs(d - d_check)/d < 1e-2, "Convergence error");
1013+
annotation(
1014+
experiment(StartTime = 0, StopTime = 1, Tolerance = 1e-06, Interval = 0.0001));
1015+
end R134a_pTX_phX_all;
1016+
9741017
model WaterIF97_dewEnthalpy "Test dewEnthalpy of WaterIF97"
9751018
extends Modelica.Icons.Example;
9761019
replaceable package Medium = Modelica.Media.Water.StandardWater "Medium model";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
time

0 commit comments

Comments
 (0)