diff --git a/IBPSA/Airflow/Multizone/BaseClasses/Examples/package.order b/IBPSA/Airflow/Multizone/BaseClasses/Examples/package.order index e841c46bf2..d11030019d 100644 --- a/IBPSA/Airflow/Multizone/BaseClasses/Examples/package.order +++ b/IBPSA/Airflow/Multizone/BaseClasses/Examples/package.order @@ -1,4 +1,3 @@ -Interpolate PowerLaw PowerLawFixedM WindPressureLowRise diff --git a/IBPSA/Airflow/Multizone/BaseClasses/package.order b/IBPSA/Airflow/Multizone/BaseClasses/package.order index 698bba52a4..ab4a1d1fef 100644 --- a/IBPSA/Airflow/Multizone/BaseClasses/package.order +++ b/IBPSA/Airflow/Multizone/BaseClasses/package.order @@ -6,7 +6,6 @@ PowerLawResistanceParameters TwoWayFlowElement TwoWayFlowElementBuoyancy ZonalFlow -interpolate powerLaw powerLawFixedM windPressureLowRise diff --git a/IBPSA/Airflow/Multizone/Table_m_flow.mo b/IBPSA/Airflow/Multizone/Table_m_flow.mo index f736baf4b8..f17dd0c252 100644 --- a/IBPSA/Airflow/Multizone/Table_m_flow.mo +++ b/IBPSA/Airflow/Multizone/Table_m_flow.mo @@ -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, diff --git a/IBPSA/Resources/ReferenceResults/Dymola/IBPSA_Utilities_Math_Examples_Interpolate.txt b/IBPSA/Resources/ReferenceResults/Dymola/IBPSA_Utilities_Math_Examples_Interpolate.txt new file mode 100644 index 0000000000..b7c14e9f57 --- /dev/null +++ b/IBPSA/Resources/ReferenceResults/Dymola/IBPSA_Utilities_Math_Examples_Interpolate.txt @@ -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] diff --git a/IBPSA/Resources/ReferenceResults/Dymola/IBPSA_Airflow_Multizone_BaseClasses_Examples_Interpolate.txt b/IBPSA/Resources/ReferenceResults/Dymola/IBPSA_Utilities_Math_Functions_Examples_Interpolate.txt similarity index 100% rename from IBPSA/Resources/ReferenceResults/Dymola/IBPSA_Airflow_Multizone_BaseClasses_Examples_Interpolate.txt rename to IBPSA/Resources/ReferenceResults/Dymola/IBPSA_Utilities_Math_Functions_Examples_Interpolate.txt diff --git a/IBPSA/Resources/Scripts/Dymola/Utilities/Math/Examples/Interpolate.mos b/IBPSA/Resources/Scripts/Dymola/Utilities/Math/Examples/Interpolate.mos new file mode 100644 index 0000000000..43a18535fa --- /dev/null +++ b/IBPSA/Resources/Scripts/Dymola/Utilities/Math/Examples/Interpolate.mos @@ -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"}); diff --git a/IBPSA/Resources/Scripts/Dymola/Airflow/Multizone/BaseClasses/Examples/Interpolate.mos b/IBPSA/Resources/Scripts/Dymola/Utilities/Math/Functions/Examples/Interpolate.mos similarity index 62% rename from IBPSA/Resources/Scripts/Dymola/Airflow/Multizone/BaseClasses/Examples/Interpolate.mos rename to IBPSA/Resources/Scripts/Dymola/Utilities/Math/Functions/Examples/Interpolate.mos index 8d1f874fd5..a684903a75 100644 --- a/IBPSA/Resources/Scripts/Dymola/Airflow/Multizone/BaseClasses/Examples/Interpolate.mos +++ b/IBPSA/Resources/Scripts/Dymola/Utilities/Math/Functions/Examples/Interpolate.mos @@ -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"}); diff --git a/IBPSA/Utilities/Math/Examples/Interpolate.mo b/IBPSA/Utilities/Math/Examples/Interpolate.mo new file mode 100644 index 0000000000..0c63bd0737 --- /dev/null +++ b/IBPSA/Utilities/Math/Examples/Interpolate.mo @@ -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=" +
+This example is the same as + +IBPSA.Utilities.Math.Functions.Examples.Interpolate +except that the block is used in place of the function. +
+", revisions=" +This example demonstrates the function - -IBPSA.Airflow.Multizone.BaseClasses.interpolate. + +IBPSA.Utilities.Math.Functions.interpolate.
", revisions="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 + +IBPSA.Utilities.Math.Functions.splineDerivatives. +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.
@@ -60,6 +64,17 @@ National Institute of Standards and Technology, NIST TN 1887, Sep. 2015. doi: ", revisions="
Note that if xSup
and ySup
only depend on parameters
-or constants, then
-
-IBPSA.Utilities.Math.Functions.cubicHermiteLinearExtrapolation
-will be more efficient.
+or constants, and therefore will not change during the simulation,
+it is more efficient to first call
+
+IBPSA.Utilities.Math.Functions.splineDerivatives
+to find the derivatives, and then call
+
+IBPSA.Utilities.Math.Functions.interpolate 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
+
+IBPSA.Utilities.Math.Functions.Examples.Interpolate.
+
In contrast to the function Modelica.Math.Vectors.interpolate diff --git a/IBPSA/Utilities/Math/Functions/splineDerivatives.mo b/IBPSA/Utilities/Math/Functions/splineDerivatives.mo index 05216017aa..ac460a8d9d 100644 --- a/IBPSA/Utilities/Math/Functions/splineDerivatives.mo +++ b/IBPSA/Utilities/Math/Functions/splineDerivatives.mo @@ -83,13 +83,13 @@ The algorithm to ensure monotonicity is based on the method described in Fritsch
This function is typically used with - -Buildings.Utilities.Math.Functions.cubicHermiteLinearExtrapolation + +IBPSA.Utilities.Math.Functions.interpolate 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.
diff --git a/IBPSA/Utilities/Math/Interpolate.mo b/IBPSA/Utilities/Math/Interpolate.mo new file mode 100644 index 0000000000..604338a82e --- /dev/null +++ b/IBPSA/Utilities/Math/Interpolate.mo @@ -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=" +
+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 + +IBPSA.Utilities.Math.Functions.interpolate. +
+", revisions=" +