Skip to content

Commit

Permalink
Merged IBPSA, issue1531_der_spliceFunction (#2681)
Browse files Browse the repository at this point in the history
This avoids a potential overflow in the analytical derivative of the splice function
  • Loading branch information
mwetter authored Oct 16, 2021
1 parent 85dbc3e commit c779fa7
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 78 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
simulateModel("Buildings.Utilities.Math.Functions.Examples.SpliceFunctionDerivativeCheck", startTime=-1, method="CVode", tolerance=1e-6, stopTime=1.0, resultFile="SpliceFunctionDerivativeCheck");
removePlots();
createPlot(id = 5,
position = {45, 33, 639, 463},
y = {"x", "y"},
range = {(-1.0), 1, 2.5, 0},
autoscale = true,
autoerase = true,
autoreplot = true,
grid = true,
color = true,
filename = "SpliceFunctionDerivativeCheck.mat",
leftTitleType = 1,
bottomTitleType = 1);
createPlot(id = 5,
position = {45, 33, 639, 226},
y = {"der(x)", "der(y)"},
range = {(-1.0), 1, 2, (-2.0)},
autoscale = true,
autoerase = true,
autoreplot = true,
grid = true,
color = true,
subPlot = 2,
leftTitleType = 1,
bottomTitleType = 1);
createPlot(id=5, position={61, 52, 878, 851}, y={"x", "y"}, range={-1.0, 1.0, -20.0, 10.0}, grid=true, colors={{28,108,200}, {238,46,47}}, timeUnit="s", displayUnits={"s3", ""});
createPlot(id=5, position={61, 52, 878, 851}, y={"der(x)", "der(y)"}, range={-1.0, 1.0, -50.0, 100.0}, grid=true, subPlot=102, colors={{28,108,200}, {238,46,47}}, timeUnit="s", displayUnits={"s2", ""});
createPlot(id=5, position={61, 52, 878, 851}, y={"d2y"}, range={-1.0, 1.0, -500.0, 500.0}, grid=true, subPlot=103, colors={{28,108,200}}, timeUnit="s");
createPlot(id=5, position={61, 52, 878, 851}, y={"d3y"}, range={-1.0, 1.0, -10000.0, 5000.0}, grid=true, subPlot=104, colors={{28,108,200}}, timeUnit="s");
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ compareVars :=
"x",
"y",
"der(x)",
"der(y)"
"der(y)",
"d2y",
"d3y"
};
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
within Buildings.Utilities.Math.Functions.BaseClasses;
function der_spliceFunction "Derivative of splice function"
extends Modelica.Icons.Function;
input Real pos;
input Real neg;
input Real x;
input Real deltax=1;
input Real dpos;
input Real dneg;
input Real dx;
input Real ddeltax=0;
output Real out;
input Real pos;
input Real neg;
input Real x;
input Real deltax=1;
input Real dpos;
input Real dneg;
input Real dx;
input Real ddeltax=0;
output Real out;
protected
Real scaledX;
Real scaledX1;
Real dscaledX1;
Real y;
constant Real asin1 = Modelica.Math.asin(1);
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;
if scaledX1 <= -0.99999999999 then
out := dneg;
elseif scaledX1 >= 0.9999999999 then
out := dpos;
else
scaledX := scaledX1*asin1;
dscaledX1 := (dx - scaledX1*ddeltax)/deltax;
y := (Modelica.Math.tanh(Modelica.Math.tan(scaledX)) + 1)/2;
out := dpos*y + (1 - y)*dneg;
out := out + (pos - neg)*dscaledX1*asin1/2/(
Modelica.Math.cosh(Modelica.Math.tan(scaledX))*Modelica.Math.cos(
scaledX))^2;
end if;
scaledX1 := x/deltax;
if scaledX1 <= -0.9999999999 then
y := 0.0;
elseif scaledX1 >= 0.9999999999 then
y := 1.0;
else
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
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;

annotation (
smoothOrder=2,
Documentation(
info="<html>
<p>
Expand All @@ -44,6 +47,12 @@ Buildings.Utilities.Math.Functions.spliceFunction</a>.
revisions="<html>
<ul>
<li>
October 13, 2021, by Michael Wetter:<br/>
Changed implementation to not use <code>cosh</code> which overflows around <i>800</i>.<br/>
This is for
<a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1531\">IBPSA, issue 1531</a>.
</li>
<li>
May 10, 2013, by Michael Wetter:<br/>
Reformulated implementation to avoid unrequired computations.
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ model SpliceFunctionDerivativeCheck

Real err "Error";

Real d1y "First derivative";
Real d2y "Second derivative";
Real d3y "Third derivative";

initial equation
y=y_comp;
equation
Expand All @@ -28,7 +32,14 @@ equation
err = y-y_comp;
assert(abs(err) < 1E-2, "Model has an error");

annotation(experiment(StartTime=-1, Tolerance=1e-6, StopTime=1.0),
d1y = der(y);
d2y = der(d1y);
d3y = der(d2y);

annotation(experiment(
StartTime=-1,
StopTime=1,
Tolerance=1e-06),
__Dymola_Commands(file="modelica://Buildings/Resources/Scripts/Dymola/Utilities/Math/Functions/Examples/SpliceFunctionDerivativeCheck.mos"
"Simulate and plot"),
Documentation(info="<html>
Expand All @@ -40,6 +51,12 @@ is not correct, the model will stop with an assert statement.
</html>", revisions="<html>
<ul>
<li>
October 13, 2021, by Michael Wetter:<br/>
Added new output for higher order derivatives.<br/>
This is for
<a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1531\">IBPSA, issue 1531</a>.
</li>
<li>
August 17, 2015 by Michael Wetter:<br/>
Updated regression test to have slope that is different from one.
This is for
Expand Down
2 changes: 1 addition & 1 deletion Buildings/Utilities/Math/Functions/spliceFunction.mo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ algorithm
end if;

annotation (
smoothOrder=1,
smoothOrder=3,
derivative=BaseClasses.der_spliceFunction,
Documentation(info="<html>
<p>
Expand Down
33 changes: 21 additions & 12 deletions Buildings/package.mo
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,13 @@ have been <b style=\"color:blue\">improved</b> in a
This new version also outputs this altitude and the latitude of the location on the weather data bus.<br/>
This is for <a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1477\">IBPSA, issue #1477</a>.
</td>
<tr><td colspan=\"2\"><b>Buildings.Fluid.Actuators</b>
<tr><td colspan=\"2\"><b>Buildings.Controls.OBC</b>
</td>
</tr>
<tr><td valign=\"top\">Buildings.Fluid.Movers.FlowControlled_dp<br/>
Buildings.Fluid.Movers.FlowControlled_m_flow<br/>
Buildings.Fluid.Movers.SpeedControlled_Nrpm<br/>
Buildings.Fluid.Movers.SpeedControlled_y
<tr><td valign=\"top\">Buildings.Controls.OBC.ASHRAE.G36_PR1.Generic.SetPoints.GroupStatus
</td>
<td valign=\"top\">Changed implementation of the filter.
The new implementation uses a simpler model.<br/>
This is for <a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1498\">IBPSA #1498</a>.
<td valign=\"top\">Added filters to select which zones are used in group.<br/>
This is for <a href=\"https://github.com/lbl-srg/modelica-buildings/issues/2544\">issue 2544</a>.
</td>
</tr>
<tr><td colspan=\"2\"><b>Buildings.Examples</b>
Expand Down Expand Up @@ -360,6 +356,19 @@ have been <b style=\"color:blue\">improved</b> in a
This is for <a href=\"https://github.com/lbl-srg/modelica-buildings/issues/2624\">issue #2624</a>.
</td>
</tr>
<tr><td colspan=\"2\"><b>Buildings.Fluid.Actuators</b>
</td>
</tr>
<tr><td valign=\"top\">Buildings.Fluid.Movers.FlowControlled_dp<br/>
Buildings.Fluid.Movers.FlowControlled_m_flow<br/>
Buildings.Fluid.Movers.SpeedControlled_Nrpm<br/>
Buildings.Fluid.Movers.SpeedControlled_y
</td>
<td valign=\"top\">Changed implementation of the filter.
The new implementation uses a simpler model.<br/>
This is for <a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1498\">IBPSA #1498</a>.
</td>
</tr>
<tr><td colspan=\"2\"><b>Buildings.Fluid.FMI</b>
</td>
</tr>
Expand Down Expand Up @@ -391,13 +400,13 @@ have been <b style=\"color:blue\">improved</b> in a
This is for <a href=\"https://github.com/ibpsa/modelica-ibpsa/pull/1516\">IBPSA #1516</a>.
</td>
</tr>
<tr><td colspan=\"2\"><b>Buildings.Controls.OBC</b>
<tr><td colspan=\"2\"><b>Buildings.Utilities.Math</b>
</td>
</tr>
<tr><td valign=\"top\">Buildings.Controls.OBC.ASHRAE.G36_PR1.Generic.SetPoints.GroupStatus
<tr><td valign=\"top\">Buildings.Utilities.Math.Functions.BaseClasses.der_spliceFunction
</td>
<td valign=\"top\">Add filters to select which zones are used in group.<br/>
This is for <a href=\"https://github.com/lbl-srg/modelica-buildings/issues/2544\">issue 2544</a>.
<td valign=\"top\">Reimplemented function to avoid a potential overflow caused by the <code>cosh</code> function.<br/>
This is for <a href=\"https://github.com/ibpsa/modelica-ibpsa/pull/1531\">IBPSA #1531</a>.
</td>
</tr>
</table>
Expand Down

0 comments on commit c779fa7

Please sign in to comment.