I'm wondering if there is any advantage in Modelica in implementing a derivable function, with continuous derivates for the min(.) function ?
I tried the following for the min(.) function, for x⩾0:
1/ f1(x,s) := Smooth(0, noEvent(min(x,s)))
2/ f2(x,s) := Smooth(10, x*(1+(x/s)^60)^(-1/60) )
the implementation looks like the following:
function Limiter "Function Limiter"
input Real x;
input Real xc(final min=0);
output Real y;
protected
Real mx;
parameter Real k=3;
parameter Integer o(
final min=1,
max=30) = 30;
final parameter Integer twoO=2*o;
algorithm
mx := noEvent(min(x, k*xc));
if noEvent(abs(xc)) >= 1 then
y := mx*(1 + (mx/xc)^twoO)^(-1/twoO);
else
y := mx*xc*(xc^twoO + mx^twoO)^(-1/twoO);
end if;
annotation (derivative=DerLimiter, inverse(x=InverseLimiter(x=y, xc=xc), xc=InverseLimiter(x=y, xc=x)));
end Limiter;
Contrary to f1, I can provide modelica with a derivative function and inverse function of f2 for any x⩾0, which I understand helps the solver in speed.
Owerall, I'm wondering if the implementation of such helpers functions is advantageous in Modelica in terms of speed, or, do I waste my time in finding and implementing these ?
Any feedback and knowledge sharing on that topic is appreciated.
It depends on the application. The drawbacks of the smoothed min
-function are:
So I wouldn't use this smoothed min
-function until I see that it is actually needed in the specific application - and that is missing from the question.
For functions other than min
and max
that are non-smooth it may even be more efficient to keep them non-smooth since that allows the solver to quickly detect the discontinuity in derivatives and trigger an event, instead of spending time to track the rapid "continuous" change. (The smooth
operator is designed to allow this.)
The general advice is that functions that are smooth and invertible should be declared as such - not that every function should be modified to be smooth and invertible.