Import your way to more lean Modelica code

When building up a library in Modelica it is often handy to use aliases to clean up your code.  Aliases to frequently used classes can help simplify the code and help its readability.  The Modelica import statement is how one creates aliases in Modelica.

One implementation that we often use here at Claytex is the following:

import SI = Modelica.SIunits;

In large classes where many variables are defined, it can be helpful to place this comment at the beginning of the class.  This allows the user to utilize the alias “SI” throughout the rest of the class rather than using the entire string Modelica.SIunits string.  Here is an example of the use of this import statement along with an easy-to-make mistake.

An example of the use of this import statement along with an easy-to-make mistake.

In packages (or libraries as large collections of packages are often referred to) where many variables with ‘SI Units’ are defined, a slightly more advanced implementation can be even more helpful.  The Modelica language allows this import statement to be placed within packages (in the package.mo file itself).  Implementation in this way makes the applied alias available throughout the entirety of the package (and all sub-packages).  Here is an example that matches how we generally implement this here at Claytex:

Here is an example that matches how we generally implement this here at Claytex

Closing remarks about using the import statement

Using the import statement to create aliases to classes can help simplify your Modelica code and help its readability.  Using import statements at the package level is the most effective implementation.  These package level aliases will help to minimize the number of lines of code in your library.  I hope this simple point will help you create more lean / easy-to-read Modelica libraries.

 

Written by: Nate Horn – Vice President

 

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-2018 Claytex Services Ltd All Rights Reserved. | Designed by CMA Marketing

Log in with your credentials

Forgot your details?