Supplying an inverse to a function

This post looks at how the inverse of a function can be provided by using the inverse () annotation. Also, an example is provided where the inverse function is used.

Why supply an inverse to a function?

During translation, symbolic manipulation will reorder the equations into a form that can be solved in a procedural way (i.e. top down). In this process the Modelica equations are manipulated and this can result in the inverse of a function having to be solved. If no inverse function is supplied then this can result in a nonlinear equation being required to solve the inverse of the function. Nonlinear equations typically make models run slower and less robustly.

How is an inverse function supplied to a function?

The inverse of a function is added using the inverse () annotation as described in the Modelica Specification 3.4. An example of the inverse function being used is in Figure 1.

Figure 1. The guardedDivision function makes use of the inverse annotation to supply the inverse functions to calculate b = a/c and a = b*c
Figure 1. The guardedDivision function makes use of the inverse annotation to supply the inverse functions to calculate b = a/c and a = b*c

In Figure 1, the inverse annotation provides the inverse function(s) for solving b=a/c and a=b*c. There can only be one inverse annotation and this can contain all the inverse functions of that function. Note that the LateInline=true annotation is used; if this is not used then the function may be automatically inlined and the inverse annotation information will be lost (see the inline blog post). The smoothOrder annotation requests that the derivatives of the function are automatically calculated (see smoothOrder related blog post).

An example

In Figure 2, the guardedDivision function of Figure 1 is used in such a way that the inverse functions are utilised.

Figure 2.  An example where the inverse of a function is calculated
Figure 2. An example where the inverse of a function is calculated

In Figure 2 the equations c = guardedDivision(a, d) and
c = guardedDivision(e, b) are present in which the unknowns to be solved are d and e respectively. To solve these, both inverse functions in the guardedDivision function are used. Figure 3 is the translation of the example.

Figure 3.  Translation log of example in Figure 2 where the inverse functions are provided resulting in  Nonlinear systems being set to 0.
Figure 3. Translation log of example in Figure 2 where the inverse functions are provided resulting in Nonlinear systems being set to 0.

In Figure 3 the nonlinear system of equations is initially {1, 1} this is because the inverse of the guardedDivision function is required. During translation the inverse functions provided in the guardedDivision function are used and so the nonlinear equations are replaced with these inverse functions.

Conclusion

Providing inverse functions can reduce the amount of nonlinear equations in models.

Written by: Garron Fish – Chief Engineer

Please get in touch if you have any questions or have got a topic in mind that you would like us to write about. You can submit your questions / topics via: Tech Blog Questions / Topic Suggestion

CONTACT US

Got a question? Just fill in this form and send it to us and we'll get back to you shortly.

Sending

© Copyright 2010-2025 Claytex Services Ltd All Rights Reserved

Log in with your credentials

Forgot your details?