Package with mathematical functions
This package contains functions for commonly used mathematical operations. The functions are used in the blocks Buildings.Utilities.Math.
Extends from Modelica.Icons.VariantsPackage (Icon for package containing variants).
Name | Description |
---|---|
average | Average of a vector |
bicubic | Bicubic function |
biquadratic | Biquadratic function |
booleanReplicator | Replicates Boolean signals |
cubicHermiteLinearExtrapolation | Interpolate using a cubic Hermite spline with linear extrapolation |
integerReplicator | Replicates integer signals |
inverseXRegularized | Function that approximates 1/x by a twice continuously differentiable function |
isMonotonic | Returns true if the argument is a monotonic sequence |
polynomial | Polynomial function |
powerLinearized | Power function that is linearized below a user-defined threshold |
quadraticLinear | Function that is quadratic in first argument and linear in second argument |
regNonZeroPower | Power function, regularized near zero, but nonzero value for x=0 |
regStep | Approximation of a general step, such that the approximation is continuous and differentiable |
smoothExponential | Once continuously differentiable approximation to exp(-|x|) in interval |x| < delta |
smoothHeaviside | Once continuously differentiable approximation to the Heaviside function |
smoothInterpolation | Interpolate using a cubic Hermite spline with linear extrapolation for a vector xSup[], ySup[] and independent variable x |
smoothLimit | Once continuously differentiable approximation to the limit function |
smoothMax | Once continuously differentiable approximation to the maximum function |
smoothMin | Once continuously differentiable approximation to the minimum function |
spliceFunction | |
splineDerivatives | Function to compute the derivatives for cubic hermite spline interpolation |
trapezoidalIntegration | Integration using the trapezoidal rule |
Examples | Collection of models that illustrate model use and test models |
BaseClasses | Package with base classes for Buildings.Utilities.Math.Functions |
Average of a vector
This block outputs the average of the vector.
Type | Name | Default | Description |
---|---|---|---|
Integer | nin | Number of inputs | |
Real | u[nin] | Input vector |
Type | Name | Description |
---|---|---|
Real | y | Result |
Bicubic function
y = a1 + a2 x1 + a3 x12 + a4 x2 + a5 x22 + a6 x1 x2 + a7 x1^3 + a8 x2^3 + a9 x12 x2 + a10 x1 x22
Type | Name | Default | Description |
---|---|---|---|
Real | a[10] | Coefficients | |
Real | x1 | Independent variable | |
Real | x2 | Independent variable |
Type | Name | Description |
---|---|---|
Real | y | Result |
Biquadratic function
y = a1 + a2 x1 + a3 x12 + a4 x2 + a5 x22 + a6 x1 x2
Type | Name | Default | Description |
---|---|---|---|
Real | a[6] | Coefficients | |
Real | x1 | Independent variable | |
Real | x2 | Independent variable |
Type | Name | Description |
---|---|---|
Real | y | Result |
Replicates Boolean signals
This function replicates the boolean input signal to an array of nout
identical output signals.
Type | Name | Default | Description |
---|---|---|---|
Integer | nout | 1 | Number of outouts |
Boolean | u | Boolean input signal |
Type | Name | Description |
---|---|---|
Boolean | y[nout] | Boolean output signals |
Interpolate using a cubic Hermite spline with linear extrapolation
For x1 < x < x2, this function interpolates using cubic hermite spline. For x outside this interval, the function linearly extrapolates.
For how to use this function, see Buildings.Utilities.Math.Functions.Examples.CubicHermite.
Type | Name | Default | Description |
---|---|---|---|
Real | x | Abscissa value | |
Real | x1 | Lower abscissa value | |
Real | x2 | Upper abscissa value | |
Real | y1 | Lower ordinate value | |
Real | y2 | Upper ordinate value | |
Real | y1d | Lower gradient | |
Real | y2d | Upper gradient |
Type | Name | Description |
---|---|---|
Real | y | Interpolated ordinate value |
Replicates integer signals
This function replicates the integer input signal to an array of
nout
identical output signals.
Type | Name | Default | Description |
---|---|---|---|
Integer | nout | Number of outputs | |
Integer | u | Integer input signal |
Type | Name | Description |
---|---|---|
Integer | y[nout] | Integer output signals |
Function that approximates 1/x by a twice continuously differentiable function
Function that approximates y=1 ⁄ x inside the interval -δ ≤ x ≤ δ. The approximation is twice continuously differentiable with a bounded derivative on the whole real line.
See the plot of Buildings.Utilities.Math.Functions.Examples.InverseXRegularized for the graph.
For efficiency, the polynomial coefficients
a, b, c, d, e, f
and
the inverse of the smoothing parameter deltaInv
are exposed as arguments to this function.
Typically, these coefficients only depend on parameters and hence
can be computed once.
They must be equal to their default values, otherwise the function
is not twice continuously differentiable.
By exposing these coefficients as function arguments, models
that call this function can compute them as parameters, and
assign these parameter values in the function call.
This avoids that the coefficients are evaluated for each time step,
as they would otherwise be if they were to be computed inside the
body of the function. However, assigning the values is optional
as otherwise, at the expense of efficiency, the values will be
computed each time the function is invoked.
See
Buildings.Utilities.Math.Functions.Examples.InverseXRegularized
for how to efficiently call this function.
Type | Name | Default | Description |
---|---|---|---|
Real | x | Abscissa value | |
Real | delta | Abscissa value below which approximation occurs | |
Real | deltaInv | 1/delta | Inverse value of delta |
Real | a | -15*deltaInv | Polynomial coefficient |
Real | b | 119*deltaInv^2 | Polynomial coefficient |
Real | c | -361*deltaInv^3 | Polynomial coefficient |
Real | d | 534*deltaInv^4 | Polynomial coefficient |
Real | e | -380*deltaInv^5 | Polynomial coefficient |
Real | f | 104*deltaInv^6 | Polynomial coefficient |
Type | Name | Description |
---|---|---|
Real | y | Function value |
Returns true if the argument is a monotonic sequence
This function returns true
if its argument is
monotonic increasing or decreasing, and false
otherwise.
If strict=true
, then strict monotonicity is tested,
otherwise weak monotonicity is tested.
Type | Name | Default | Description |
---|---|---|---|
Real | x[:] | Sequence to be tested | |
Boolean | strict | false | Set to true to test for strict monotonicity |
Type | Name | Description |
---|---|---|
Boolean | monotonic | True if x is monotonic increasing or decreasing |
Polynomial function
y = a1 + a2 x + a3 x2 + ...
Type | Name | Default | Description |
---|---|---|---|
Real | x | Independent variable | |
Real | a[:] | Coefficients |
Type | Name | Description |
---|---|---|
Real | y | Result |
Power function that is linearized below a user-defined threshold
For x < x0, this function replaces y=xn by a linear function that is continuously differentiable everywhere.
A typical use of this function is to replace T = T4(1/4) in a radiation balance to ensure that the function is defined everywhere. This can help solving the initialization problem when a solver may be far from a solution and hence T4 < 0.
See the package Examples
for the graph.
Type | Name | Default | Description |
---|---|---|---|
Real | x | Abscissa value | |
Real | n | Exponent | |
Real | x0 | Abscissa value below which linearization occurs |
Type | Name | Description |
---|---|---|
Real | y | Function value |
Function that is quadratic in first argument and linear in second argument
y = a1 + a2 x1 + a3 x12 + (a4 + a5 x1 + a6 x12) x2
Type | Name | Default | Description |
---|---|---|---|
Real | a[6] | Coefficients | |
Real | x1 | Independent variable for quadratic part | |
Real | x2 | Independent variable for linear part |
Type | Name | Description |
---|---|---|
Real | y | Result |
Power function, regularized near zero, but nonzero value for x=0
This function replaces y=|x|n in the interval -δ...+δ by a 4-th order polynomial that has the same function value and the first and second derivative at x=± δ.
A typical use of this function is to replace the function for the convective heat transfer coefficient for forced or free convection that is of the form h=c |dT|n for some constant c and exponent 0 ≤ n ≤ 1. By using this function, the original function that has an infinite derivative near zero and that takes on zero at the origin is replaced by a function with a bounded derivative and a non-zero value at the origin. Physically, the region -δ...+δ may be interpreted as the region where heat conduction dominates convection in the boundary layer.
See the packageExamples
for the graph.
Type | Name | Default | Description |
---|---|---|---|
Real | x | Abscissa value | |
Real | n | Exponent | |
Real | delta | 0.01 | Abscissa value where transition occurs |
Type | Name | Description |
---|---|---|
Real | y | Function value |
Approximation of a general step, such that the approximation is continuous and differentiable
This function is used to approximate the equation
y = if x > 0 then y1 else y2;
by a smooth characteristic, so that the expression is continuous and differentiable:
y = smooth(1, if x > x_small then y1 else if x < -x_small then y2 else f(y1, y2));
In the region -x_small < x < x_small
a 2nd order polynomial is used
for a smooth transition from y1
to y2
.
Extends from Modelica.Icons.Function (Icon for functions).
Type | Name | Default | Description |
---|---|---|---|
Real | x | Abscissa value | |
Real | y1 | Ordinate value for x > 0 | |
Real | y2 | Ordinate value for x < 0 | |
Real | x_small | 1e-5 | Approximation of step for -x_small <= x <= x_small; x_small >= 0 required |
Type | Name | Description |
---|---|---|
Real | y | Ordinate value to approximate y = if x > 0 then y1 else y2 |
Once continuously differentiable approximation to exp(-|x|) in interval |x| < delta
Function to provide a once continuously differentiable approximation to exp(- |x| ) in the interval |x| < δ for some positive δ
Type | Name | Default | Description |
---|---|---|---|
Real | x | Input argument | |
Real | delta | Transition point where approximation occurs |
Type | Name | Description |
---|---|---|
Real | y | Output argument |
Once continuously differentiable approximation to the Heaviside function
Once Lipschitz continuously differentiable approximation to the
Heaviside(.,.)
function.
See Example
Buildings.Utilities.Math.Examples.SmoothHeaviside.
Type | Name | Default | Description |
---|---|---|---|
Real | x | Argument | |
Real | delta | Parameter used for scaling |
Type | Name | Description |
---|---|---|
Real | y | Result |
Interpolate using a cubic Hermite spline with linear extrapolation for a vector xSup[], ySup[] and independent variable x
For xSup1 ≤ x ≤ xSupn, where n is the size of the support points xSup, which must be strictly monotonically increasing, this function interpolates using cubic hermite spline. For x outside this interval, the function linearly extrapolates.
If n=2, linear interpolation is used an if n=1, the function value y1 is returned.
Note that if xSup
and ySup
only depend on parameters
or constants, then
Buildings.Utilities.Math.Functions.cubicHermiteLinearExtrapolation
will be more efficient.
In contrast to the function
Modelica.Math.Vectors.interpolate
which provides linear interpolation, this function does
not trigger events.
For how to use this function, see Buildings.Utilities.Math.Functions.Examples.SmoothInterpolation.
Type | Name | Default | Description |
---|---|---|---|
Real | x | Abscissa value | |
Real | xSup[:] | Support points (strictly increasing) | |
Real | ySup[size(xSup, 1)] | Function values at xSup | |
Boolean | ensureMonotonicity | isMonotonic(ySup, strict=fal... | Set to true to ensure monotonicity of the cubic hermite |
Type | Name | Description |
---|---|---|
Real | yInt | Interpolated ordinate value |
Once continuously differentiable approximation to the limit function
Once continuously differentiable approximation to the limit(.,.)
function.
The output is bounded to be in [l, u].
Note that the limit need not be respected, such as illustrated in Buildings.Utilities.Math.Examples.SmoothMin.
Type | Name | Default | Description |
---|---|---|---|
Real | x | Variable | |
Real | l | Low limit | |
Real | u | Upper limit | |
Real | deltaX | Width of transition interval |
Type | Name | Description |
---|---|---|
Real | y | Result |
Once continuously differentiable approximation to the maximum function
Once continuously differentiable approximation to the max(.,.)
function.
Note that the maximum need not be respected, such as illustrated in Buildings.Utilities.Math.Examples.SmoothMin.
Type | Name | Default | Description |
---|---|---|---|
Real | x1 | First argument | |
Real | x2 | Second argument | |
Real | deltaX | Width of transition interval |
Type | Name | Description |
---|---|---|
Real | y | Result |
Once continuously differentiable approximation to the minimum function
Once continuously differentiable approximation to the min(.,.)
function.
Note that the minimum need not be respected, such as illustrated in Buildings.Utilities.Math.Examples.SmoothMin.
Type | Name | Default | Description |
---|---|---|---|
Real | x1 | First argument | |
Real | x2 | Second argument | |
Real | deltaX | Width of transition interval |
Type | Name | Description |
---|---|---|
Real | y | Result |
Function to provide a once continuously differentiable transition between to arguments.
The function is adapted from Modelica.Media.Air.MoistAir.Utilities.spliceFunction and provided here for easier accessability to model developers.
Type | Name | Default | Description |
---|---|---|---|
Real | pos | Argument of x > 0 | |
Real | neg | Argument of x < 0 | |
Real | x | Independent value | |
Real | deltax | Half width of transition interval |
Type | Name | Description |
---|---|---|
Real | out | Smoothed value |
Function to compute the derivatives for cubic hermite spline interpolation
This function computes the derivatives at the support points xi that can be used as input for evaluating a cubic hermite spline.
If ensureMonotonicity=true
, then the support points yi
need to be monotone increasing (or increasing), and the computed derivatives
di are such that the cubic hermite is monotone increasing (or decreasing).
The algorithm to ensure monotonicity is based on the method described in Fritsch and Carlson (1980) for
ρ = ρ2.
This function is typically used with Buildings.Utilities.Math.Functions.cubicHermiteLinearExtrapolation which is used to evaluate the cubic spline. Because in many applications, the shape of the spline depends on parameters, this function has been implemented in such a way that all derivatives can be computed at once and then stored for use during the time stepping, in which the above function may be called.
F.N. Fritsch and R.E. Carlson, Monotone piecewise cubic interpolation. SIAM J. Numer. Anal., 17 (1980), pp. 238-246.
Type | Name | Default | Description |
---|---|---|---|
Real | x[:] | Support point, strict monotone increasing | |
Real | y[size(x, 1)] | Function values at x | |
Boolean | ensureMonotonicity | isMonotonic(y, strict=false) | Set to true to ensure monotonicity of the cubic hermite |
Type | Name | Description |
---|---|---|
Real | d[size(x, 1)] | Derivative at the support points |
Integration using the trapezoidal rule
This function computes a definite integral using the trapezoidal rule.
Type | Name | Default | Description |
---|---|---|---|
Integer | N | Number of integrand points | |
Real | f[:] | Integrands | |
Real | deltaX | Width of interval for Trapezoidal integration |
Type | Name | Description |
---|---|---|
Real | result | Result |