Are you ready to talk?

Import your way to more lean Modelica code

Table of contents

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

 

Need to talk to an expert?

Our engineering teams are on hand to provide tailored guidance and support with a deep knowledge of the full Dassault Systèmes portfolio.

Want to receive more content like this?

Sign up to receive a weekly roundup of Expert insights as they are published...

  • Related news & articles straight to your inbox
  • Hints, tips & how-tos
  • Thought leadership articles