liveBook · Manning

Modelica

As explained in Digital Twins in Action

Modelica is an object-oriented, equation-based modeling language for representing and simulating complex physical systems.

Modelica is an object-oriented, equation-based language for modeling physical systems. Instead of prescribing a step-by-step calculation, a model states equations that must hold. This acausal approach is useful when a digital twin combines interacting electrical, mechanical, hydraulic, or thermal components. section 9.3

State the law, not the calculation order

For a draining tank, a procedural assignment such as `rate = -k * sqrt(h)` calculates flow rate from water height. To calculate height from a specified rate, that code would need rewriting. A Modelica equation such as `der(h) = -k * sqrt(h)` instead states how height changes. In OpenModelica, symbolic manipulation collects and rearranges the model’s equations before numerical integration. section 9.3.1

Models can also be assembled from reusable components rather than written entirely as raw equations. The Modelica Standard Library supplies an `OpenTank` component; its geometry and connections to a valve or pipe describe the physical arrangement. The model specifies parameters, states, and physics without importing an integration solver or defining time steps. section 9.3.2

Listing 9.2 Modelica tank model with 1 cm outlet hole
model WaterTankDrainage
  import Modelica.Constants.pi;

  inner Modelica.Fluid.System system  #1
    annotation (Placement(transformation(extent={{60,60},{80,80}})));

  Modelica.Fluid.Vessels.OpenTank tank(  #2
    redeclare package Medium = Modelica.Media.Water.
    ConstantPropertyLiquidWater,
    crossArea = pi * (1.5/2)^2,  // 1.767 m²
    height = 3.0,                // Max tank height
    level_start = 2.0,           // Initial water level
    nPorts = 1,                  // Number of ports
    portsData = {Modelica.Fluid.Vessels.BaseClasses.VesselPortsData(
                   diameter = 0.01, // Hole diameter
                   height = 0.0)})  // Port at bottom of tank
    annotation (Placement(transformation(extent={{-20,20},{20,60}})));

  Modelica.Fluid.Sources.Boundary_pT atmosphere(  #3
    redeclare package Medium = Modelica.Media.Water.
    ConstantPropertyLiquidWater,
    nPorts = 1,
    p = 101325,  // Atmospheric pressure (Pa)
    T = 293.15)  // Temperature (K)
    annotation (Placement(transformation(extent={{60,-20},{40,0}})));

equation
  connect(tank.ports[1], atmosphere.ports[1])  #4
    annotation (Line(points={{0,20},{0,-10},{50,-10}}, color={0,127,255}));

end WaterTankDrainage;

Inspect the tank model’s geometry, initial level, water medium, and 1 cm outlet. These declarations describe the system to simulate rather than a hand-written integration loop. section 9.3.2

In the OpenModelica GUI, run the tank simulation, change its initial water level, and plot the level over time. A run takes only seconds, allowing repeated predictions with different parameters. For a sensor-backed twin, the current measured level can be supplied to subsequent runs. Running the GUI requires a display server; the supplied Docker setup uses OMEdit and an X server. section 9.3.2

Figure 9.5 Output of a simulation run for the tank model, with an initial water height of 2 meters, showing the change in level over time
figure

The plotted six-hour run starts at a water height of 2 meters and shows the predicted level changing over time. section 9.3.2

How Digital Twins in Action covers it

How deep it goes
Explains acausal equalities and symbolic manipulation, then assembles and simulates a library-based tank model without detailing solver internals.
Examples
Uses a draining-tank equation, an OpenModelica tank model with a 1 cm outlet, and a plotted six-hour simulation.
What it assumes
Introduces acausal equations by contrasting them with procedural assignments; uses a simple physical drainage problem before library-based modeling.
What you can do after
Apply it via OpenModelica to assemble and simulate a tank model, vary its initial level, and inspect predicted changes.
How it connects
Connects acausal modeling, differential-algebraic equations, numerical integration, the Modelica Standard Library, and digital twins.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Digital Twins in Action. The section links point to the parts of the book it draws on.