wiki:WritingCompliantLibraries

Version 8 (modified by Adeel Asghar, 9 years ago) ( diff )

--

Writing libraries compliant to the Modelica specification

Writing libraries compliant to the Modelica specification is not easy. Converting libraries that were not written with the (current) specification in mind is also hard as most of the steps need to be performed manually. Here are some hints to help you on the way.

Graphical annotations

OMEdit was written after Modelica 3 was introduced. Because of this, it has no support for Modelica 1 or 2-style graphical annotations. If you want OMEdit to be able to browse your library, you will need to use a tool that knows how to convert these annotations; for example the Dymola demo version (which understands Modelica 1, but not Modelica 2 annotations). Import the library and export it choosing Modelica 3 graphical annotations.

File encoding

Make sure that the library is encoded in UTF-8 or 7-bit ASCII. It is not UTF-8 encoded if OpenModelica says:

Error: The file was not encoded in UTF-8

To solve this, convert the files to UTF-8, e.g., opening it in OMEdit and clicking on the button to convert to UTF-8, or manually modify the sources. To manually change the encoding of a file, open it in a text editor (such as pluma or Notepad++) and make sure each character looks the way it should, then save as UTF-8 text. To automatically convert all sources if you know the current encoding you can use the following script (Unix only):

find $(SOURCE_DIRS) -regextype posix-egrep -regex '.*\.(cpp|c|h|mo|tpl)$$' -exec bash -c "iconv -f UTF-8 -t UTF-8 '{}' -o /dev/null 2>tmp || (rm -f tmp && cp '{}' tmp && iconv -f ISO-8859-1 -t UTF-8 tmp -o '{}' && echo Converted {} to UTF-8)" ';'

To manually convert to 7-bit ASCII (as used by the MSL), use HTML entities. For example: "°C" becomes "<html>&deg;C</html>" and <html>Linköping</html> becomes <html>Link&ouml;ping</html>.

Subscripting modifiers is not allowed

Code like M m(arr[:]={1.0,2.0}); is not allowed in the Modelica grammar, but this can easily be re-written to: M m(arr={1.0,2.0});.

Wrong package.order

When in doubt, remove all package.order files. If they are present, they must follow a certain structure. OpenModelica will give nice warnings about what is missing or superfluous. Sometimes, these errors are due to Modelica being case-sensitive.

Modelica is case-sensitive

Error: Expected the package to have name HVAC, but got Hvac.

Means the file was named HVAC but the contained class did not have the same name. Usually solved by renaming the file.

Within clauses

Error: Expected the package to have within ATplus; but got within ;

In Modelica 2 and beyond, packages are required to have a within-clause saying where in the hierarchy it is. within ; is the empty clause, so this message tells you to add the missing clauses. To automatically add within-clauses to a library, there exists a utility in our git repository: addMissingWithin.sh.

Incorrect floating point numbers

Warning: Treating .5 as 0.5. This is not standard Modelica and only done for compatibility with old code. Support for this feature may be removed in the future.

Add the missing 0 or your library might not work if the Modelica grammar needs to be updated. To do this in Notepad++ (or other regex replacing tool such as sed):

([*=,;\s-])(\.\d) to \10\2

Missing Integer to enumeration conversions

Some tools used to allow comparisons like E.one == 0 (enumeration type == Integer), which was never allowed in the specification. Change all integer literals to enumeration literals instead. Note that in some tools, the function call Integer(E.one) will generate an error-message. In this case, it is useful to add a function EnumToInteger to convert the enumeration to integer.

Connecting Integer with Enumeration

Some tools allows connecting an enumeration component in a connector (or expandable connector) with an integer variable. Such connection is not valid Modelica and either the Integer variable should be an enumeration or the enumeration should be made an Integer in the connector and the Integer function used to convert the enumeration to an Integer variable that should be connected.

Redeclare instead of redeclare replaceable or replaceable in modifiers

Some tools support redeclare (interpreted as redeclare replaceable) in modifiers instead of the correct Modelica: replaceable. Because of this, in some libraries where this is used OpenModelica will give an error that an element is not declared as replaceable (because the modifier is just redeclare without replaceable). Change 'redeclare' to 'redeclare replaceable' or 'replaceable' from top to leafs.

Missing inner declarations

Some tools add missing inner declarations automatically if you forgot them. OpenModelica does not, so add them to your example models. A related error is World world1;, which is sometimes added by accident. The inner World objects needs to be called world or the MultiBody model will not work properly.

Old standards

Use omc +std=2.x or add uses(Modelica(version="2.2.2")) to the package. This might make it work without further conversion. You are recommended to convert the library to MSL 3.2.1 and ModelicaSpec 3.2r2 or above since in particular the standard library is now conforming to the specification.

Large Integer values

If you have large Integer values (on 32 bit OpenModelica support just 31 bit integers) you should change them to Real. Example:

parameter SI.AbsolutePressure P_constant=10000000;

should be:

parameter SI.AbsolutePressure P_constant=10000000.0;

If you don't do this the compiler will do it for you but it will generate a warning.

Note: See TracWiki for help on using the wiki.