Skip to content

Commit

Permalink
Merge pull request #1845 from ibpsa/issue1844_moveInterpolateToMath
Browse files Browse the repository at this point in the history
Issue1844 move interpolate to math
  • Loading branch information
mwetter authored Mar 25, 2024
2 parents f7c9e3b + 09bec6d commit 48254b3
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 26 deletions.
1 change: 0 additions & 1 deletion IBPSA/Airflow/Multizone/BaseClasses/Examples/package.order
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Interpolate
PowerLaw
PowerLawFixedM
WindPressureLowRise
Expand Down
1 change: 0 additions & 1 deletion IBPSA/Airflow/Multizone/BaseClasses/package.order
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ PowerLawResistanceParameters
TwoWayFlowElement
TwoWayFlowElementBuoyancy
ZonalFlow
interpolate
powerLaw
powerLawFixedM
windPressureLowRise
Expand Down
2 changes: 1 addition & 1 deletion IBPSA/Airflow/Multizone/Table_m_flow.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ within IBPSA.Airflow.Multizone;
model Table_m_flow
"Mass flow(y-axis) vs Pressure(x-axis) cubic spline fit model based from table data, with last two points linearly interpolated"
extends IBPSA.Airflow.Multizone.BaseClasses.PartialOneWayFlowElement(
m_flow = IBPSA.Airflow.Multizone.BaseClasses.interpolate(
m_flow = IBPSA.Utilities.Math.Functions.interpolate(
u=dp,
xd=dpMea_nominal,
yd=mMea_flow_nominal,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
last-generated=2024-02-29
statistics-simulation=
{
"linear": " ",
"nonlinear": " ",
"numerical Jacobians": "0"
}
time=[0e+00, 5e+02]
int.y=[-8.709000051021576e-02, -8.607897162437439e-02, -8.508513867855072e-02, -8.410613983869553e-02, -8.313964307308197e-02, -8.218330889940262e-02, -8.123478293418884e-02, -8.029171824455261e-02, -7.935179024934769e-02, -7.841263711452484e-02, -7.747191935777664e-02, -7.652729749679565e-02, -7.557642459869385e-02, -7.461696118116379e-02, -7.364656031131744e-02, -7.266288250684738e-02, -7.166357338428497e-02, -7.064630091190338e-02, -6.960871070623398e-02, -6.854847073554993e-02, -6.74632266163826e-02, -6.635063886642456e-02, -6.520836800336838e-02, -6.403406709432602e-02, -6.282539665699005e-02, -6.157999858260155e-02, -6.03080615401268e-02, -5.901877209544182e-02, -5.770833417773247e-02, -5.637295171618462e-02, -5.500881373882294e-02, -5.36121279001236e-02, -5.217909067869186e-02, -5.070589855313301e-02, -4.918875172734261e-02, -4.762385040521622e-02, -4.600739479064941e-02, -4.433558508753777e-02, -4.260461404919624e-02, -4.081068560481071e-02, -3.894999995827675e-02, -3.698130697011948e-02, -3.486191853880882e-02, -3.258588165044785e-02, -3.014722652733326e-02, -2.754000015556812e-02, -2.463850006461143e-02, -2.133000083267689e-02, -1.741999946534634e-02, -1.231999974697828e-02, 0e+00, 1.231999974697828e-02, 1.741999946534634e-02, 2.133000083267689e-02, 2.528040856122971e-02, 2.613032609224319e-02, 2.613095752894878e-02, 2.613155916333199e-02, 2.61321347206831e-02, 2.613268233835697e-02, 2.613320574164391e-02, 2.613370306789875e-02, 2.613417431712151e-02, 2.613462321460247e-02, 2.613504603505135e-02, 2.613544836640358e-02, 2.613582648336887e-02, 2.613618411123753e-02, 2.613652125000954e-02, 2.613683789968491e-02, 2.613713406026363e-02, 2.613740973174572e-02, 2.613766863942146e-02, 2.613791078329086e-02, 2.613813430070877e-02, 2.613834105432034e-02, 2.613853104412556e-02, 2.613870799541473e-02, 2.613886818289757e-02, 2.613901719450951e-02, 2.61391494423151e-02, 2.61392705142498e-02, 2.613938041031361e-02, 2.613947726786137e-02, 2.613956481218338e-02, 2.61396411806345e-02, 2.613970823585987e-02, 2.61397659778595e-02, 2.613981626927853e-02, 2.613985724747181e-02, 2.613989263772964e-02, 2.613992244005203e-02, 2.613994479179382e-02, 2.613996341824532e-02, 2.613997645676136e-02, 2.613998576998711e-02, 2.61399932205677e-02, 2.6139996945858e-02, 2.613999880850315e-02, 2.61400006711483e-02, 2.61400006711483e-02]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
simulateModel("IBPSA.Utilities.Math.Examples.Interpolate", stopTime=500, tolerance=1e-06, resultFile="InterpolateBlock");
createPlot(id=1, position={15, 15, 584, 361}, x="int.u", y={"int.y"}, range={-50.0, 50.0, -0.1, 0.04000000000000001}, grid=true, filename="InterpolateBlock.mat", colors={{28,108,200}}, timeUnit="Pa", displayUnits={"kg/s"});
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
simulateModel("IBPSA.Airflow.Multizone.BaseClasses.Examples.Interpolate", stopTime=500, tolerance=1e-06, resultFile="Interpolate");
simulateModel("IBPSA.Utilities.Math.Functions.Examples.Interpolate", stopTime=500, tolerance=1e-06, resultFile="Interpolate");
createPlot(id=1, position={15, 15, 584, 361}, x="dp", y={"m_flow"}, range={-50.0, 50.0, -0.1, 0.04000000000000001}, grid=true, filename="Interpolate.mat", colors={{28,108,200}}, timeUnit="Pa", displayUnits={"kg/s"});
54 changes: 54 additions & 0 deletions IBPSA/Utilities/Math/Examples/Interpolate.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
within IBPSA.Utilities.Math.Examples;
model Interpolate "Example model of the Interpolate block"
extends Modelica.Icons.Example;

parameter Real table[:,:]=[-50,-0.08709; -25,-0.06158; -10,-0.03895; -5,-0.02754;
-3,-0.02133; -2,-0.01742; -1,-0.01232; 0,0; 1,0.01232; 2,0.01742; 3,0.02133;
4.5,0.02613; 50,0.02614]
"Table of mass flow rate in kg/s (second column) as a function of pressure difference in Pa (first column)";
parameter Real[:] xd=table[:,1] "x-axis support points";
parameter Real[size(xd, 1)] yd=table[:,2] "y-axis support points";
parameter Real[size(xd, 1)] d =
IBPSA.Utilities.Math.Functions.splineDerivatives(
x=xd,
y=yd,
ensureMonotonicity=true) "Derivatives at the support points";

IBPSA.Utilities.Math.Interpolate int(
xd=xd,
yd=yd,
d=d) "Interpolate block"
annotation (Placement(transformation(extent={{-10,-10},{10,10}})));
Modelica.Blocks.Sources.Ramp ramp(
duration=500,
height=100,
offset=-50) "Ramp from -50Pa to +50Pa"
annotation (Placement(transformation(extent={{-60,-10},{-40,10}})));

equation
connect(ramp.y, int.u)
annotation (Line(points={{-39,0},{-12,0}}, color={0,0,127}));
annotation (
experiment(
StopTime=500,
Tolerance=1e-06),
__Dymola_Commands(file="modelica://IBPSA/Resources/Scripts/Dymola/Utilities/Math/Examples/Interpolate.mos"
"Simulate and plot"), Documentation(info="<html>
<p>
This example is the same as
<a href=\"modelica://IBPSA.Utilities.Math.Functions.Examples.Interpolate\">
IBPSA.Utilities.Math.Functions.Examples.Interpolate</a>
except that the block is used in place of the function.
</p>
</html>", revisions="<html>
<ul>
<li>
February 29, 2024, by Hongxiang Fu:<br/>
First implementation.<br/>
This is for
<a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1844\">IBPSA, #1844</a>.
</li>
</ul>
</html>
"));
end Interpolate;
1 change: 1 addition & 0 deletions IBPSA/Utilities/Math/Examples/package.order
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Factorial
FallingFactorial
IntegerReplicator
IntegratorWithReset
Interpolate
InverseXRegularized
Polynomial
PowerLinearized
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
within IBPSA.Airflow.Multizone.BaseClasses.Examples;
within IBPSA.Utilities.Math.Functions.Examples;
model Interpolate "Test model for the interpolation function"
extends Modelica.Icons.Example;

Expand All @@ -13,37 +13,48 @@ model Interpolate "Test model for the interpolation function"
"Mass flow rate";

protected
parameter Real[:] xd=table[:,1] "X-axis support points";
parameter Real[size(xd, 1)] yd=table[:,2] "Y-axis support points";
parameter Real[:] xd=table[:,1] "x-axis support points";
parameter Real[size(xd, 1)] yd=table[:,2] "y-axis support points";
parameter Real[size(xd, 1)] d(each fixed=false) "Derivatives at the support points";

Modelica.Blocks.Sources.Ramp ramp(
duration=500,
height=100,
offset=-50) "Ramp from -50Pa to +50Pa";
initial equation
d =IBPSA.Utilities.Math.Functions.splineDerivatives(
d = IBPSA.Utilities.Math.Functions.splineDerivatives(
x=xd,
y=yd,
ensureMonotonicity=true);
equation
dp=ramp.y;
m_flow =IBPSA.Airflow.Multizone.BaseClasses.interpolate(u=dp,xd=xd,yd=yd,d=d);
m_flow = IBPSA.Utilities.Math.Functions.interpolate(u=dp, xd=xd, yd=yd, d=d);

annotation (
experiment(
StopTime=500,
Tolerance=1e-06),
__Dymola_Commands(file="modelica://IBPSA/Resources/Scripts/Dymola/Airflow/Multizone/BaseClasses/Examples/Interpolate.mos"
__Dymola_Commands(file="modelica://IBPSA/Resources/Scripts/Dymola/Utilities/Math/Functions/Examples/Interpolate.mos"
"Simulate and plot"), Documentation(info="<html>
<p>
This example demonstrates the function
<a href=\"modelica://IBPSA.Airflow.Multizone.BaseClasses.interpolate\">
IBPSA.Airflow.Multizone.BaseClasses.interpolate</a>.
<a href=\"modelica://IBPSA.Utilities.Math.Functions.interpolate\">
IBPSA.Utilities.Math.Functions.interpolate</a>.
</p>
</html>", revisions="<html>
<ul>
<li>
February 29, 2024, by Hongxiang Fu:<br/>
Moved to
<a href=\"modelica://IBPSA.Utilities.Math.Functions.Examples\">
IBPSA.Utilities.Math.Functions</a>
from
<a href=\"modelica://IBPSA.Airflow.Multizone.BaseClasses.Examples\">
IBPSA.Airflow.Multizone.BaseClasses</a>.<br/>
This is for
<a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1844\">IBPSA, #1844</a>.
</li>
<li>
February 2, 2022, by Michael Wetter:<br/>
Revised implementation.<br/>
This is for
Expand Down
1 change: 1 addition & 0 deletions IBPSA/Utilities/Math/Functions/Examples/package.order
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CubicHermite
ExponentialIntegralE1
Factorial
FallingFactorial
Interpolate
InverseXDerivativeCheck
InverseXDerivative_2_Check
InverseXRegularized
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
within IBPSA.Airflow.Multizone.BaseClasses;
within IBPSA.Utilities.Math.Functions;
function interpolate
"Function for the interpolation of table data for airflow models"
"Function for cubic hermite spline interpolation of table data"
extends Modelica.Icons.Function;

input Real u "Independent variable";
input Real[:] xd "X-axis support points";
input Real[size(xd, 1)] yd "Y-axis support points";
input Real[size(xd, 1)] d(each fixed=false) "Derivatives at the support points";
input Real[:] xd "x-axis support points";
input Real[size(xd, 1)] yd "y-axis support points";
input Real[size(xd, 1)] d "Derivatives at the support points";

output Real z "Dependent variable with monotone interpolation";

Expand Down Expand Up @@ -34,7 +34,11 @@ algorithm
Documentation(info="<html>
<p>
This function returns the value on a cubic hermite spline through the given support points
and provided spline derivatives at these points with monotonically increasing values.
and provided spline derivatives at these points.
The spline derivatives at the support points can be computed using
<a href=\"modelica://IBPSA.Utilities.Math.Functions.splineDerivatives\">
IBPSA.Utilities.Math.Functions.splineDerivatives</a>.
The support points must be monotonically increasing.
Outside the provided support points, the function returns a linear extrapolation with
the same slope as the cubic spline has at the respective support point.
</p>
Expand All @@ -60,6 +64,17 @@ National Institute of Standards and Technology, NIST TN 1887, Sep. 2015. doi:
</html>", revisions="<html>
<ul>
<li>
February 29, 2024, by Hongxiang Fu:<br/>
Moved to
<a href=\"modelica://IBPSA.Utilities.Math.Functions\">
IBPSA.Utilities.Math.Functions</a>
from
<a href=\"modelica://IBPSA.Airflow.Multizone.BaseClasses\">
IBPSA.Airflow.Multizone.BaseClasses</a>.<br/>
This is for
<a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1844\">IBPSA, #1844</a>.
</li>
<li>
February 26, 2024, by Hongxiang Fu:<br/>
Correct implementation to make it smooth.<br/>
This is for
Expand Down
1 change: 1 addition & 0 deletions IBPSA/Utilities/Math/Functions/package.order
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exponentialIntegralE1
factorial
fallingFactorial
integerReplicator
interpolate
inverseXRegularized
isMonotonic
polynomial
Expand Down
18 changes: 14 additions & 4 deletions IBPSA/Utilities/Math/Functions/smoothInterpolation.mo
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,20 @@ function value <i>y<sup>1</sup></i> is returned.
</p>
<p>
Note that if <code>xSup</code> and <code>ySup</code> only depend on parameters
or constants, then
<a href=\"modelica://IBPSA.Utilities.Math.Functions.cubicHermiteLinearExtrapolation\">
IBPSA.Utilities.Math.Functions.cubicHermiteLinearExtrapolation</a>
will be more efficient.
or constants, and therefore will not change during the simulation,
it is more efficient to first call
<a href=\"modelica://IBPSA.Utilities.Math.Functions.splineDerivatives\">
IBPSA.Utilities.Math.Functions.splineDerivatives</a>
to find the derivatives, and then call
<a href=\"modelica://IBPSA.Utilities.Math.Functions.interpolate\">
IBPSA.Utilities.Math.Functions.interpolate</a> to perform the interpolation.
This way the derivatives only need to be computed once upon initialisation,
not at each step during the simulation.
See the example implemented in
<a href=\"modelica://IBPSA.Utilities.Math.Functions.Examples.Interpolate\">
IBPSA.Utilities.Math.Functions.Examples.Interpolate</a>.
</p>
<p>
In contrast to the function
<a href=\"modelica://Modelica.Math.Vectors.interpolate\">
Modelica.Math.Vectors.interpolate</a>
Expand Down
8 changes: 4 additions & 4 deletions IBPSA/Utilities/Math/Functions/splineDerivatives.mo
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ The algorithm to ensure monotonicity is based on the method described in Fritsch
</p>
<p>
This function is typically used with
<a href=\"modelica://Buildings.Utilities.Math.Functions.cubicHermiteLinearExtrapolation\">
Buildings.Utilities.Math.Functions.cubicHermiteLinearExtrapolation</a>
<a href=\"modelica://IBPSA.Utilities.Math.Functions.interpolate\">
IBPSA.Utilities.Math.Functions.interpolate</a>
which is used to evaluate the cubic spline.
Because in many applications, the shape of the spline depends on parameters
which will no longer change once the initialisation is complete,
this function computes and stores the derivatives only once instead of doing so
at each time step to save system resources.
this function computes and returns the derivatives so that they can be stored by the calling
model to avoid repetitive computations.
</p>
<h4>References</h4>
<p>
Expand Down
51 changes: 51 additions & 0 deletions IBPSA/Utilities/Math/Interpolate.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
within IBPSA.Utilities.Math;
block Interpolate
"Output the cubic hermite spline interpolation of the input signal on the given curve"
extends Modelica.Blocks.Interfaces.SISO;

parameter Real[:] xd "x-axis support points";
parameter Real[size(xd, 1)] yd "y-axis support points";
parameter Real[size(xd, 1)] d "Derivatives at the support points";
equation
y = IBPSA.Utilities.Math.Functions.interpolate(u=u, xd=xd, yd=yd, d=d);

annotation (
defaultComponentName="int",
Documentation(info="<html>
<p>
This block outputs the value on a cubic hermite spline through the given
support points and their spline derivatives at these points, using the function
<a href=\"modelica://IBPSA.Utilities.Math.Functions.interpolate\">
IBPSA.Utilities.Math.Functions.interpolate</a>.
</p>
</html>", revisions="<html>
<ul>
<li>
February 29, 2024, by Hongxiang Fu:<br/>
First implementation.<br/>
This is for
<a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1844\">IBPSA, #1844</a>.
</li>
</ul>
</html>"),
Icon(graphics={
Ellipse(
extent={{-58,-56},{-68,-46}},
lineColor={28,108,200},
fillColor={28,108,200},
fillPattern=FillPattern.Solid),
Line(
points={{-64,-52},{-36,6},{8,40},{78,26}},
color={28,108,200},
smooth=Smooth.Bezier),
Ellipse(
extent={{82,20},{72,30}},
lineColor={28,108,200},
fillColor={28,108,200},
fillPattern=FillPattern.Solid),
Ellipse(
extent={{-10,18},{-20,28}},
lineColor={28,108,200},
fillColor={28,108,200},
fillPattern=FillPattern.Solid)}));
end Interpolate;
1 change: 1 addition & 0 deletions IBPSA/Utilities/Math/package.order
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Factorial
FallingFactorial
IntegerReplicator
IntegratorWithReset
Interpolate
InverseXRegularized
Max
Min
Expand Down

0 comments on commit 48254b3

Please sign in to comment.