API reference

The functions for users come first, the developer functions after them. Both are public (Julia's REPL help marks other names as internal). OMFrontend.USER_API and OMFrontend.DEVELOPER_API list them; the tests check that each name is in its section below. The flags have their own page, Flags, and the API for graphical editors its own, GUI API (experimental).

Public API

Translate Modelica to flat Modelica with named arguments, check a model, read a class. These functions print only warnings while they work; a model that cannot be translated raises a short ModelicaError with the model's messages. debug! is the developer mode with full stack traces.

OMFrontend.instantiate — Function
instantiate(model = ""; file = "", string = "", libraries = String[], modifiers = String[], scalarize = true)
    -> FlatModelica

Translate the Modelica class model to flat Modelica. The class is in file (a .mo file), in string (Modelica text), or with neither in one of the libraries. model can be left out when the file or string has one top-level class.

libraries: the libraries the model uses, each a .mo file, a package directory, the name of an installed library (installLibrary), or "Modelica" / "Modelica 3.2.3" (the bundled Modelica Standard Library). A model that uses the Modelica Standard Library gets version 4.1.0 without naming it. modifiers: modifications of the class, as in M(k = 3) (["k = 3", "x(start = 2)"]). scalarize = false keeps arrays.

The result shows as flat Modelica; a model that cannot be translated raises a ModelicaError with its messages.

Examples

fm = OMFrontend.instantiate(string = "model M Real x(start = 1); equation der(x) = -x; end M;")
fm = OMFrontend.instantiate("HelloWorld"; file = "HelloWorld.mo")
fm = OMFrontend.instantiate("Modelica.Blocks.Examples.PID_Controller")
fm = OMFrontend.instantiate("MyLib.Examples.Test"; libraries = ["MyLib/package.mo"])
fm = OMFrontend.instantiate("Modelica.Blocks.Examples.PID_Controller"; modifiers = ["driveAngle = 1.0"])
source
OMFrontend.flatModelica — Function
flatModelica(model = ""; file = "", string = "", libraries = String[], modifiers = String[], scalarize = true)
    -> String

The flat Modelica text of the class model: instantiate with the same arguments, as a string.

Example

print(OMFrontend.flatModelica("Modelica.Electrical.Analog.Examples.ChuaCircuit"))
source
OMFrontend.check — Function
check(model = ""; file = "", string = "", libraries = String[]) -> CheckResult

Check the class model as omc's checkModel does: translate it (arguments as for instantiate) and count its scalar equations and variables as omc does. A model the frontend cannot translate gives ok == false with the messages, no error; equal counts mean a balanced model.

Example

julia> OMFrontend.check(string = "model M Real x(start = 1); equation der(x) = -x; end M;")
Check of M completed successfully.
Class M has 1 equation(s) and 1 variable(s).
source
OMFrontend.classNames — Function
classNames(parent = ""; file = "", string = "", libraries = String[]) -> Vector{String}

The names of the classes in file or string (or with neither, in libraries and the loaded libraries), in declaration order: the top-level classes, or with parent the classes declared in that class. The Modelica Standard Library is found without naming it, as in instantiate.

Examples

OMFrontend.classNames(file = "Plant.mo")
OMFrontend.classNames("Modelica.Blocks.Examples")
source
OMFrontend.source — Function
source(class; file = "", string = "", libraries = String[]) -> String

The Modelica text of class as it is written in its file or string, e.g. the definition of a library class (source("Modelica.Blocks.Continuous.PID")). The class is found as in classNames.

source
OMFrontend.description — Function
description(class; file = "", string = "", libraries = String[]) -> String

The description string of class (model M "this text"), "" without one.

source
OMFrontend.documentation — Function
documentation(class; file = "", string = "", libraries = String[]) -> String

The info text (HTML) of the Documentation annotation of class, "" without one.

source
OMFrontend.variables — Function
variables(fm::FlatModelica) -> Vector{String}

The names of the variables of a flat model, in its order (parameters and constants included).

source
OMFrontend.parameters — Function
parameters(fm::FlatModelica) -> Vector{Pair{String, String}}

The parameters and constants of a flat model, each name => its binding as Modelica text ("" without one), in the model's order.

Example

julia> OMFrontend.parameters(OMFrontend.instantiate(string = "model M parameter Real k = 2; Real x = k * time; end M;"))
1-element Vector{Pair{String, String}}:
 "k" => "2.0"
source
OMFrontend.exportJSON — Function
exportJSON(FM; output_dir=".", base_name, hierarchy=true, flat=true,
           class_mapping=nothing)

Export an already-flattened model FM (a FLAT_MODEL or the (fm, funcs) tuple from flattenModel) as <base>_hierarchy.json and/or <base>_flat.json in output_dir. Structural-mode components are grouped into class templates with parameter overrides. See StructuralModeJSON.exportJSON.

source
exportJSON(fm::FlatModelica; output_dir = ".", base_name, hierarchy = true, flat = true, class_mapping = nothing)

Export a model translated by instantiate as JSON: <base>_hierarchy.json (components grouped into class templates with their parameter overrides) and <base>_flat.json in output_dir.

source
exportJSON(modelName, fileName, library = "";
           output_dir, base_name,
           hierarchy=true, flat=true, class_mapping=nothing, atd=false)

Parse fileName, instantiate modelName to a FLAT_MODEL, then export it as JSON. When library is non-empty, it is loaded via loadInstalledLibrary and combined with the model via flattenModelWithLibraries (e.g. library = "Modelica"). When library is empty, the model is flattened on its own with flattenModel. Pass atd=true to also write the <base>.atd schema.

source
OMFrontend.componentGraphics — Function
componentGraphics(fm, component) -> (; coordinateSystem, items, placements)

The icon of the class of component (its name in the flat model fm: "spring", "engine.damper") as Modelica text, to draw a replacement like it: coordinateSystem ("coordinateSystem(...)", or empty), items (the graphic items, its base classes' first, as tools draw them) and placements (each of the component's connectors => its "Placement(...)" annotation, declared in the class or a base class). An error if fm has no component of that name.

source
componentGraphics(fm::FlatModelica, component) -> (; coordinateSystem, items, placements)

The icon of the class of component in a model translated by instantiate, as Modelica text (see componentGraphics(::Frontend.FLAT_MODEL, ::String)).

source
OMFrontend.installLibrary — Function
installLibrary(name; version, url = nothing, tag = nothing, subdir = nothing, dest = nothing) -> String

Download and install the Modelica library name into OM.jl's library directory (omjlLibraryDir(): ~/OMJL/libraries, on Windows %APPDATA%\OMJL\libraries; or dest) as <dest>/<name> <version>/, from its git repository at a release tag. After installation libraries() discovers it and loadInstalledLibrary loads it. git runs without prompts: a repository that asks for credentials (GitHub's answer for one that does not exist) fails with git's message.

  • version is required.
  • url: the git repository (a GitHub URL, or any URL or path git clone takes). Without it, a registered library (["Buildings"]) comes from its registered repository.
  • tag: the release tag; by default v<version>, then <version> (a registered library: its own prefix).
  • subdir: the package's folder in the repository; by default the folder name with a package.mo, else the repository's root when its package.mo is the package name.

Checked: the folder holds the package name (else nothing is installed), and its version annotation is version (a warning when not). The libraries its uses annotation names are listed, each as available or missing.

Example

OMFrontend.installLibrary("Buildings"; version = "13.0.0")
OMFrontend.installLibrary("ExternData"; version = "3.0.4", url = "https://github.com/modelica-3rdparty/ExternData.mo")
key = OMFrontend.loadInstalledLibrary("Buildings")
source
OMFrontend.libraries — Function
libraries(; installDir = nothing) -> Dict{String, Vector{NamedTuple}}

Discover Modelica libraries available for loading. Searches, in order:

  1. The directories of installed libraries (libraryDirectories): OM.jl's, ~/OMJL/libraries (installLibrary installs there), then OpenModelica's, ~/.openmodelica/libraries (on Windows both under %APPDATA%). Only installDir when it is given.
  2. The bundled lib/Modelica/ directory inside this package, excluding the pre-packaged MSL_* single-file variants.

Returns a Dict mapping library name to a Vector of (version, path, source) named tuples, where source is :installed or :bundled.

Example

avail = OMFrontend.libraries()
# avail["Modelica"] => [(version="4.1.0", path="...", source=:installed), ...]
source
OMFrontend.setFlags — Function
setFlags(; flags...) -> Nothing

Set flags of the frontend by name: a debug flag to true or false, an option to a value of its type (an integer, a number, a string, a vector of strings). The flags hold in the Julia session until they are set again. The page Flags lists them with their defaults. An unknown name or a value of the wrong type raises an ArgumentError, and then no flag changes.

Example

OMFrontend.setFlags(execstat = true)    # prints the time and memory of each instantiation phase
OMFrontend.instantiate("Modelica.Blocks.Examples.PID_Controller")
OMFrontend.setFlags(execstat = false)
OMFrontend.setFlags(evaluateAllParameters = true, evalLoopLimit = 10^6)
source
OMFrontend.FlatModelica — Type
FlatModelica

A class translated to flat Modelica (instantiate). print(fm) and string(fm) give the flat Modelica text; the REPL shows it after the translation's warnings (as comments). Fields: name (the class), model (the frontend's FLAT_MODEL), functions (the functions the model calls, a function tree), warnings.

source
OMFrontend.CheckResult — Type
CheckResult

The result of check. Fields: name (the class), ok (it translates), equations and variables (omc's checkModel counts; zero when the check failed), messages (errors and warnings, file:line:column: Severity: text), text (the string argument). Shows as omc's checkModel text after the messages, which show as in a ModelicaError.

source
OMFrontend.ModelicaError — Type
ModelicaError <: Exception

A model the frontend cannot translate: a syntax error, a type error, an unknown class. messages holds the frontend's messages, one per entry, each file:line:column: Severity: text (as omc's, the severity Error, Warning or Notification). text is the Modelica text of the string argument, "" for a file. Shown, each message is followed by its source line with a ^ under the column. OMFrontend.debug!(true) shows the Julia stack trace of the failure instead.

source
OMFrontend.debug! — Function
OMFrontend.debug!(on::Bool = true) -> Bool

Developer mode for instantiate, flatModelica and classNames. Off (the default): they log only warnings, and a model that cannot be translated raises a short ModelicaError with the model's messages. On: library loading logs its progress, and a failure is rethrown with its Julia stack trace (MetaModelica failures with backtraces), after the model's messages on stderr. Returns the new setting.

source

Pipeline API (developers)

The steps instantiate runs, the library cache, and the older entry points.

OMFrontend.instantiateSCodeToFM — Function

Instantiates and translates to the flat model representation The element to instantiate should be provided in the following format: <component>.<component1>.<component2>...

source
OMFrontend.flattenModel — Function

flattenModel(modelName::String, fileName::String)

Returns the flat representation of a modelica model along with the functions used and define by the model.

source
OMFrontend.flattenModelWithMSL — Function

function flattenModelWithMSL(modelName::String, fileName::String; MSL_Version = "MSL:3.2.3", forceReload = false)

Returns the flat representation of a modelica model along with the functions used and define by the model. See the keyword argument for specifying MSL version. Valid versions are 3.2.3 and 4.1.0.

When forceReload is true, any cached MSL under this version key is dropped and the standard library is reparsed and re-translated from disk. This is useful when the on-disk MSL has changed during the session, or to force a clean benchmark of the MSL load path.

source

function flattenModelWithMSL(modelName::String; MSL_Version = "MSL:3.2.3", scalarize = true, forceReload = false)

Flatten an MSL model by name. When forceReload is true, the cached MSL under this version key is dropped and the standard library is reparsed and re-translated from disk before the model is instantiated.

source
OMFrontend.flattenModelWithLibraries — Function
flattenModelWithLibraries(modelName, fileName; libraries, MSL, MSL_Version, scalarize, forceReload)

Flatten a Modelica model combining it with one or more pre-loaded libraries. Libraries are looked up in LIBRARY_CACHE by their cache keys, followed by the libraries they use (libraryClosure). The libraries loaded into the session (useLibrary!, loadedLibraries) come along without being named. If MSL is requested, it is appended last (lowest priority for name resolution). fileName may be empty for a model of one of the libraries.

Ordering in the combined program (leftmost = highest priority):

  1. User model code
  2. User libraries (in order of libraries vector)
  3. The loaded libraries, but for the top-level classes the model's file has
  4. MSL (if MSL=true): a loaded library with the package Modelica, else the bundled MSL_Version (nothing: 3.2.3)

A library of libraries with a top-level class of a loaded library, and an MSL_Version of another version than a loaded Modelica's, are errors.

When forceReload=true and MSL=true, the cached MSL under the requested version key is dropped before instantiation. User libraries in libraries are not affected by this flag; reload them via loadLibrary if needed.

source
OMFrontend.loadMSL — Function

@author: johti17 Loads the Modelica Standard Library (MSL). Adds the Modelica standard library to the library cache. Currently 3.2.3 is the default version.

Available versions are: 4.1.0 3.2.3

source
OMFrontend.loadBundledMSL — Function
loadBundledMSL(; version = "3.2.3") -> String

Load the Modelica Standard Library from the bundled lib/Modelica/ files, independent of any OpenModelica installation. Returns the cache key.

version may be given as "3.2.3", "4.1.0", or the already-normalised form "MSL_3_2_3". The returned key can be passed to flattenModelWithLibraries or instantiateSCodeToFM.

This is the loader used by the test suite so results do not depend on the local OpenModelica installation.

Example

key = OMFrontend.loadBundledMSL()
(FM, _) = OMFrontend.flattenModelWithLibraries("My.Model", "model.mo"; libraries=[key])
source
OMFrontend.loadLibrary — Function
loadLibrary(libraryPath::String; name::Union{String, Nothing} = nothing)

Load an arbitrary Modelica library from a single .mo file into the library cache. Returns the cache key (a string) that can be passed to translate or simulate via the libraries keyword argument.

If name is not provided, the cache key is derived from the top-level class name in the parsed file.

Example

key = OMFrontend.loadLibrary("/path/to/MyLib.mo")
# key == "MyLib"
source
OMFrontend.loadPackageDirectory — Function
loadPackageDirectory(dirPath::String; name=nothing) -> String

Load a Modelica library organized as a directory tree with package.mo files. Each .mo file is parsed individually and merged into a single SCode program based on its within clause.

Returns the cache key (a string) for use in translate/simulate via the libraries keyword argument.

Directory structure

MyLibrary/
  package.mo          # top-level package declaration
  package.order       # optional: class ordering (one name per line)
  SomeModel.mo        # "within MyLibrary; model SomeModel ..."
  SubPackage/
    package.mo        # "within MyLibrary; package SubPackage ..."
    AnotherModel.mo   # "within MyLibrary.SubPackage; model AnotherModel ..."

Example

key = OMFrontend.loadPackageDirectory("/path/to/MyLibrary")
# key == "MyLibrary"
source
OMFrontend.loadInstalledLibrary — Function
loadInstalledLibrary(name; version = nothing, forceReload = false, autodeps = true, installDir = nothing) -> String

Load a Modelica library discovered via libraries. Installed OpenModelica libraries are searched first, then the bundled lib/Modelica/ directory.

If version is omitted the first available version is used. If given, an exact match is tried before a prefix match (e.g. "4.1" matches "4.1.0").

When autodeps = true (default), the uses(...) annotation of the loaded package.mo is parsed and any declared dependencies that are available via libraries() are loaded automatically before returning. Their keys are recorded in LIBRARY_DEPENDENCIES, so flattening with the returned key alone also uses them. installDir is the directory of installed libraries (see libraries).

Returns the cache key under which the library is stored in LIBRARY_CACHE.

Example

OMFrontend.loadInstalledLibrary("Modelica"; version = "4.1.0")
OMFrontend.loadInstalledLibrary("Buildings")   # auto-loads Modelica + ModelicaServices
source
OMFrontend.toFlatModelica — Function
toFlatModelica(fm, fLst; printBindingTypes = false)
toFlatModelica((fm, fLst); printBindingTypes = false)

Render a flat model and its function tree as textual flat Modelica.

source
OMFrontend.toString — Function

toString(model::FLAT_MODEL) Converts the flat model representation to a Julia String, the extra \n are replaced with

source
OMFrontend.enableDumpDebug — Function
enableDumpDebug()

Enable staged dumping of the flat model between different compiler phases. NOTE this will generate files on your local drive if enabled.

To disable see disableDumpDebug()

source
OMFrontend.removeQuotesFromFlatModelica — Function
removeQuotesFromFlatModelica(flatModelicaStr::String)

This function postprocesses a flat modelica model represented as a string. It does so by removing quoted variables and expressions where possible. This function should be used on models that has ascii characters only. This can be useful if you wish to remove redundant clutter from flat models.

NOTE: Not exhaustively tested for all models.

source
OMFrontend.julia_main — Function
julia_main() :: Cint

Entry point for an OMFrontend binary built with PackageCompiler.create_app. Reads command-line arguments from Base.ARGS and returns a process exit code (0 on success, 1 on uncaught error, 2 on usage error).

source

Index