Skip to content

Commit

Permalink
Refactor spliceFunction (modelica#3885)
Browse files Browse the repository at this point in the history
* Improve readability slightly
* Remove cosh, because cosh(800+) might return NaN
* As reported by ibpsa/modelica-ibpsa#1531
  • Loading branch information
thorade committed Feb 1, 2022
1 parent fb880e2 commit c15f9d3
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions Modelica/Media/Air/MoistAir.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1429,20 +1429,23 @@ Specific entropy of moist air is computed from pressure, temperature and composi
input Real deltax=1 "Region around x with spline interpolation";
output Real out;
protected
Real scaledX;
Real scaledX1;
Real scaledX1 "x scaled to -1 ... 1 interval";
Real scaledXp "x scaled to -pi/2 ... pi/2 interval";
Real scaledXt "x scaled to -inf ... inf interval";
Real y;
algorithm
scaledX1 := x/deltax;
scaledX := scaledX1*Modelica.Math.asin(1);
if scaledX1 <= -0.999999999 then
y := 0;
y := 0.0;
elseif scaledX1 >= 0.999999999 then
y := 1;
y := 1.0;
else
y := (Modelica.Math.tanh(Modelica.Math.tan(scaledX)) + 1)/2;
scaledXp := scaledX1*0.5*Modelica.Constants.pi;
scaledXt := Modelica.Math.tan(scaledXp);
y := 0.5*Modelica.Math.tanh(scaledXt) + 0.5;
end if;
out := pos*y + (1 - y)*neg;
out := pos*y + (1.0 - y)*neg;

annotation (derivative=spliceFunction_der);
end spliceFunction;

Expand All @@ -1458,26 +1461,27 @@ Specific entropy of moist air is computed from pressure, temperature and composi
input Real ddeltax=0;
output Real out;
protected
Real scaledX;
Real scaledX1;
Real scaledX1 "x scaled to -1 ... 1 interval";
Real scaledXp "x scaled to -pi/2 ... pi/2 interval";
Real scaledXt "x scaled to -inf ... inf interval";
Real dscaledX1;
Real y;
algorithm
scaledX1 := x/deltax;
scaledX := scaledX1*Modelica.Math.asin(1);
dscaledX1 := (dx - scaledX1*ddeltax)/deltax;
if scaledX1 <= -0.99999999999 then
y := 0;
if scaledX1 <= -0.9999999999 then
y := 0.0;
elseif scaledX1 >= 0.9999999999 then
y := 1;
y := 1.0;
else
y := (Modelica.Math.tanh(Modelica.Math.tan(scaledX)) + 1)/2;
scaledXp := scaledX1*0.5*Modelica.Constants.pi;
scaledXt := Modelica.Math.tan(scaledXp);
y := 0.5*Modelica.Math.tanh(scaledXt) + 0.5;
end if;
out := dpos*y + (1 - y)*dneg;

if (abs(scaledX1) < 1) then
out := out + (pos - neg)*dscaledX1*Modelica.Math.asin(1)/2/(
Modelica.Math.cosh(Modelica.Math.tan(scaledX))*Modelica.Math.cos(
scaledX))^2;
dscaledX1 := (dx - scaledX1*ddeltax)/deltax;
out := out + (pos - neg)*dscaledX1*0.25*Modelica.Constants.pi*(1 - Modelica.Math.tanh(scaledXt)^2)*(scaledXt^2 + 1);
end if;
end spliceFunction_der;

Expand Down

0 comments on commit c15f9d3

Please sign in to comment.