Modia.jl

TravisAppVoyercodecov.ioThe MIT License

Introduction

Modia is a domain specific extension of Julia for modeling and simulation of physical systems.

Papers and presentations about Modia:

Modia is designed to model and simulate physical systems (electrical, mechanical, thermo-dynamical, etc.) described by differential and algebraic equations. A user defines a model on a high level with model components (like a mechanical body, an electrical resistance, or a pipe) that are physically connected together. A model component is constructed by "expression = expression" equations. The defined model is symbolically processed (for example, equations might be analytically differentiated), JIT compiled and simulated with Sundials IDA solver with the KLU sparse matrix package. By this approach it's possible and convenient to build models with hundred thousands of equations describing the dynamics of a car, an airplane, a power plant, etc. and simulate them. The authors used previous experience from the design of the modeling language Modelica. Modia will also be used to design and evaluate features for future Modelica versions.

Component models are defined by @model macros. Such models contain definition of variables with various attributes such as start values, min, max, SI unit, etc. An @equations macro is used to define the equations of such a component. Coupling between components is expressed using a connect statement involving groups of variables. The semantics is either to constrain connected variables to be equal or to constrain a set of variables to sum to zero, for example to model Kirchhoff's current law.

Installation

Modia is registered in METADATA.jl and can be installed with Pkg.add. Version (0.2.3) is the last one with support for Julia >= 0.6. Trunk and later versions support Julia >=1.0.

# Julia >= 0.6:
julia> Pkg.add("Modia")
julia> Pkg.add("ModiaMath")

# Julia >= 0.7:
julia> ]add Modia ModiaMath

Modia uses PyPlot for plotting. If PyPlot is not available in your current Julia environment an information message is printed and all plot(..) calls are ignored.

In order that plot windows are displayed, you need to add PyPlot to your current environment via ]add PyPlot. Often this automatic installation fails and it is recommended to follow the instructions Installing PyPlot in a robust way.

Use

To define a model:

  using Modia
  @model FirstOrder begin
     x = Variable(start=1)   # start means x(0)
     T = Parameter(0.5)      # Time constant
     u = 2.0                 # Same as Parameter(2.0)
  @equations begin
     T*der(x) + x = u        # der() means time derivative
     end
  end;

To simulate a model:

  result = simulate(FirstOrder, 2);
  @show result["x"][end];
  ModiaMath.plot(result, "x")

Examples

The schematics below are screenshots of Modelica models. These models have been converted to Modia and the examples below execute these models. Note, in Modia there is not (yet) a graphical definition of models.

Current Controller

Current Controller

examples/CurrentController.jl

Cauer Low Pass Filter

Cauer Low Pass Filter

examples/CauerLowPassFilter.jl

To run examples:

  using Modia
  include("$(Modia.ModiaDir)/examples/runexamples.jl")

To run tests:

  using Modia
  include("$(Modia.ModiaDir)/test/runtests.jl")

Examples (Jupyter Notebooks)

Status

The version released now is partial since certain prototype functionalities needs to be generalized and refactored. See below. Such prototype features are enabled by flags in the simulate command. See examples.

Available functionalities

  • Model instantiation (including handling of modifiers and extends)
  • Model flattening
  • Redeclarations
  • Handling of T, size, start, state and flow attributes
  • Size deduction of variables and equations
  • Array equations
  • Index reduction
  • BLT
  • Symbolic transformations of equations (solving, differentiating)
  • Algebraic loop handling
  • Logging of transformation steps
  • Simulation with ModiaMath

To Do

Enhance and refactor prototype codes for:

  • Alias handling
  • Handle overdetermined equations
  • Introduction of partial and block attribute to models
  • Automatic state selection
  • Arrays of components
  • Complex data type
  • Event handling
  • Synchronous equations
  • Sparse Jacobian handling
  • Impulse handling
  • API to dynamically change model topology
  • More models converted from Modelica Standard Library

To implement

  • Tearing of systems of equations
  • Improved code generation for really large models
  • Allowing change of parameters without recompilation
  • Taking min and max attributes into account
  • Handling of rotation matrices involved in algebraic loops
  • Linearization
  • ...