Create a derivative function using Dymola

Sometimes it is useful to be able to determine the derivative of a function, for example when you require to use the derivative of the function directly. This post looks at how you can use Dymola to determine this for you while going through an example.

Why calculate the derivative of a function?

Being able to calculate the derivative of a function can be useful, as this derivative is going to be used within another function or because for some reason the derivative cannot be calculated automatically. When the analytic Jacobian in a model is required, often the second order derivatives of functions are needed, and many times these are not present. So these derivative functions may have to be created.

Example of calculating the derivative of the intersection of a line with a static plane

The Claytex TerrainServer library allows users to simulate vehicles driving on a road generated by rFpro, using the rFpro TerrainServer. However the derivative of the contact point is not provided by rFpro, only the contact point is. Fortunately it is possible to calculate the contact point derivative by using the projection direction used to calculate the contact point, and the static plane that the contact point is on. This derivative calculation is just the derivative of the Claytex intersectionOfLineOnPlane function.

Calculating the derivative of a function

This can be done by duplicating the function, making it a model as described in Figure 1, translating the model to generate the flat Modelica code (i.e. dsmodel.mof) and extracting the algorithm for calculating the derivative from this.

In this example the derivative of the Claytex function intersectionOfLineOnPlane is calculated, where the plane is static.

Figure 1. The function of the left is rewritten as a model so that the derivative can be calculated.

Figure 1. The function of the left is rewritten as a model so that the derivative can be calculated. (Further descriptions below)

In Figure 1, the numbers 1-7 represent the steps used to modify a function to a model from which the derivative algorithm can be calculated; these steps are described below:

  1. Change function to model.
  2. Change inputs that are static to parameters, remember to set the Evaluate flag to false.
  3. Rename the inputs that have derivatives, in this case “In” is added to these inputs.
  4. Add the derivative signals for the input signals that contain derivatives.
  5. Remove output from the output of the original function and add a new output which is the derivative of the old output.
  6. Use the Claytex scalarOneDerivative function to create variables that can be differentiated.
  7. Change algorithm to equation.
  8. Set the following Dymola flags in the Dymola command line:
    1. Evaluate = false;
    2. Advanced.OutputModelicaCode = true;
  9. Translate the function and look Output Section in the dsmodel.mof file that is generated.

The Output Section of the dsmodel.mof file is the algorithm to calculate the derivative of the function.

Converting the dsmodel.mof Output Section to a function

When the model is translated the dsmodel.mof file is generated that contains the derivative function in the Output Section as in Figure 2.

Figure 2. The derivative of the intersectionOfLineOnPlane function

Figure 2. The derivative of the intersectionOfLineOnPlane function

The Output Section contains the calculation of b_der which is the derivative of the intersectionOfLineOnPlane function. Now the derivative function can be created that makes use of this b_der calculation.

Code optimization

The derivative calculation in dsmodel.mof can often be simplified. In this case the q2In – q1In and q2_der – q1_derIn calculations appear in the algorithm in multiple places. By creating variables equal to these calculations and substituting these in the algorithm these calculations are calculated only once. In some cases this substitution method can make the functions significantly faster.

Check the derivative

Once the derivative function is created it must be checked; to do this follow the method described in this blog post:

Conclusion

Using the procedure described in this blog post, the derivative of a function can be calculated by using Dymola. A similar process can often be used to calculate the partial derivative functions by utilising the method described in the Modelica specification to calculate the partial derivative function.

Some care is required as some functions contain an algorithm section that cannot be converted into equation section by just replacing the algorithm with equation. However, these algorithms can often be made into an equation.

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-2024 Claytex Services Ltd All Rights Reserved

Log in with your credentials

Forgot your details?