# ModelingToolkit -- can I use \`min\` and \`max\` functions?

**URL:** <https://discourse.julialang.org/t/modelingtoolkit-can-i-use-min-and-max-functions/127934>\
**Category:** Modelling & Simulations\
**Created:** [April 10, 2025, 2:53pm UTC](https://discourse.julialang.org/t/modelingtoolkit-can-i-use-min-and-max-functions/127934 "2025-04-10T14:53:23Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [April 11, 2025, 8:14pm UTC](https://discourse.julialang.org/t/modelingtoolkit-can-i-use-min-and-max-functions/127934/19 "2025-04-11T20:14:45Z")

</div>

I agree with what you say. The closed-loop system is causal and better shows its issue is indeed with wind-up. I was suspicious of the open-loop example because it’s acausal, perhaps leading to the crash without clipping. I think open- and closed-loop crashed at different times and slightly different reasons. (Also I should have noticed you’re clamping in closed loop rather than back-calculating.)

[This previous thread](https://discourse.julialang.org/t/handling-of-saturated-integrator-with-anti-windeup/124819) discusses the differences with OpenModelica. They have nonlinear blocks that are saturation (limiter) aware:

> <https://github.com/modelica/ModelicaStandardLibrary/blob/daa10221dfd96caf4f4309190733715109ee5a3d/Modelica/Blocks/Nonlinear.mo#L33C2-L38C18>

This code allows several ways to saturate. It’s implemented explicitly with `if u > uMax...`. The `smooth(0,` is option to smooth with a given polynomial order, currently disabled with 0. The other branch with `homotopy` is for reinitializing integrator states after hitting a discrete change, difficult when algebraic equations contain `if`. They can iteratively solve starting with simplified knowledge of the rail.

I would expect MTK if-lifting to be great for identifying discrete changes. Not sure about initialization, but it’s possible to furnish MTK with guesses for solving algebraic equations, where the first guess is that system is stuck on rail.

---

_[View the full topic](https://discourse.julialang.org/t/modelingtoolkit-can-i-use-min-and-max-functions/127934)._
