[ANN] Modia.jl - Modeling and Simulation (version 0.5, major redesign)

Modia.jl version 0.5 (major redesign)

Modia is an environment in the form of a Julia package 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 (such as a mechanical body, an electrical resistance, or a pipe) that are physically connected together. A model component is constructed by equations (expression = expression) or by Julia structs/functions, such as the pre-defined Modia 3D-mechanical components. The defined model is symbolically processed (for example, equations are sorted and solved and might be analytically differentiated). A Julia function is generated from the transformed model which is used to simulate the model with integrators from DifferentialEquations.jl. The basic type of the floating point variables is usually Float64, but can for example be set to Float32, DoubleFloat, Measurement{Float64}, StaticParticles{Float64,100}.

Modia includes the multibody package Modia3D including 3D shapes for visualization and collision handling. It is, for example, possible to model the 3D mechanical part of a robot with Modia multibody components and the electrical motors and gearboxes that are driving the joints with equation-based Modia components. Collision handling with elastic response calculation is performed for shapes that are defined with a contact material and have a convex geometry or are approximated by the convex hull of a concave geometry.

Modia version 0.5 is a complete redesign and extension compared to earlier versions regarding scalability and how models are defined and manipulated and includes the coupling between 3d-mechanical models and equation-based models.

A brief overview of Modia is given in the JuliaCon 2021 lightning talk: Modia – Modeling Multidomain Engineering Systems with Julia, 2021-07-30, 20:10–20:20 UTC (Modia Lightning talk)

The Modia Tutorial provides an introduction to Modia. The Modia3D Tutorial provides an introduction to the use of 3D components in Modia.

Examples

Modia3D

Animation of 3D mechanics is supported (see video link below):

YouBotsGripping

A recursively defined Modia model is used as a benchmark (see video link below). It has about 900 solids, 380 joints and 380 equation-based damper models (a corresponding Modelica model has about 135000 scalar equations/unknowns):

Mobile8

Example videos:

Equation-based Model

The following equations describe a damped pendulum:

PendulumEquations

where phi is the rotation angle, omega the angular velocity, m the mass, L the rod length, d a damping constant, g the gravity constant and r the vector from the origin of the world system to the tip of the pendulum. These equations can be defined with:


using Modia

@usingModiaPlot 

Pendulum = Model(

   L = 0.8u"m",

   m = 1.0u"kg",

   d = 0.5u"N*m*s/rad",

   g = 9.81u"m/s^2",

   phi = Var(init = 1.57*u"rad"),

   w   = Var(init = 0u"rad/s"),

   equations = :[

          w = der(phi)

        0.0 = m*L^2*der(w) + d*w + m*g*L*sin(phi)

          r = [L*cos(phi), -L*sin(phi)]

   ]

)

Simulation and plotting of the pendulum with normally distributed uncertainty added to some parameters is performed in the following way:


using Measurements

PendulumWithUncertainties = Pendulum | Map(L = (0.8 ± 0.2)u"m",

                                           m = (1.0 ± 0.2)u"kg",

                                           d = (0.5 ± 0.2)u"N*m*s/rad")

pendulum2 =  @instantiateModel(PendulumWithUncertainties, FloatType = Measurement{Float64})

simulate!(pendulum2, Tsit5(), stopTime = 10.0u"s")

plot(pendulum2, [("phi", "w"); "r"], figure = 2)

resulting in the following plot where mean values are shown with thick lines and standard deviations as area around the mean values.

Hilding Elmqvist, Martin Otter, Andrea Neumayr, Gerhard Hippman

29 Likes

What are the differences between Modia.jl and something like ModellingToolkit.jl? You might talk about it on the talk but I dunno.

1 Like

@ChrisRackauckas 's take on the topic: ModelingToolkit, Modelica, and Modia: The Composable Modeling Future in Julia - Stochastic Lifestyle.

3 Likes

Modia is focused on making it easy to build large models by reusing well-tested models (hierarchically coupling components and setting parameters) and making all transformations automatic. It is based on long experience of Modelica. It has:

  • simple and unified syntax and semantics
  • built-in libraries of components (currently quite small, but we are working on translation of Modelica model components to Modia)
  • built-in support for fast 3D multibody modeling and simulation including collision handling
  • support for 3D animation
  • possible to combine Modia equation based modeling and specialized modeling by Julia functions
  • transformations, such as index reduction of DAEs are automatic
  • emphasis on scalability since easy to build large models
  • possible to use DifferentialEquations.jl
8 Likes

For comparison with ModelingToolkit.jl and others see Comparison of ModelingToolkit vs Equation-Based and Block Modeling Languages · ModelingToolkit.jl

2 Likes

That needed an update because Modia.jl v0.5 is a pretty big major redesign :sweat_smile:. This is really nice to see, great work @HildingElmqvist ! It seems that there has been a bit of convergent evolution really: Modia v0.5 incorporates a lot of TinyModia.jl’s ideas, drops most of the macros and moves towards Julia expressions in a way that’s almost ModelingToolkit.jl-like.

I think that the main difference at this point is that ModelingToolkit.jl supports many non-ODE/DAE models (SDEs, PDEs, etc). The other main difference is ModelingToolkit.jl is built on Symbolics.jl so that all of the expressions that the user writes can be manipulated with the full functionality of a computer algebra system (CAS). In this sense it’s made to be an open transformation system where people can easily do custom model transformations using the substitute, derivative, etc. functionality, where structural_simplify is similar to the Modelica standard transformation (on ODEs) but is just one transform a user may or may not choose to perform. On the other hand, Modia offers a more focused and Modelica-like experience. One place where you can see this is in the Var constructor constructs: Modia.jl allows for all of the standard Modelica flags (flow, potential, etc.) while ModelingToolkit allows for tagging any metadata onto the array and then requires that the user defines dispatches of connect to describe how this kind of variable should act (with the standard Modelica-like flags being included by default). That specific example should highlight the design difference.

5 Likes

Spoilt for choice! I have been a long time user of OpenModelica and now my company has bought me a Dymola license, I have even better functionality. I intend to use the Dymola/Modelica models to create Modia.jl models (probably by hand). I have written a lot of other functionality in Julia and I also hope to use the awesome ModelingToolkit.jl. I need speed for real-time control purposes. The results will be published (not directly as a Modia/ModelingToolkit comparison) but both will be referenced and obviously DiffEq stuff etc!

You guys are amazing! Thank you for saving me many years of work! :heart:

2 Likes