Skip to main content

modelica

pipeline status PyPI version Python versions License: MIT

A Modelica toolchain for Python: parse .mo source, flatten it to a DAE, simulate it, and draw it. Every stage up to simulation is an ordinary Python value you can read and change. For engineers and researchers who want Modelica semantics inside Python rather than behind a separate compiler.

Simulation runs on a precompiled engine: a virtual machine written in Rust that ships in the platform wheels and interprets the model as bytecode. Nothing is compiled per model. Where no engine wheel exists, the pure-Python wheel simulates with SciPy instead. See the engine.

Install name is modelica. The git repository keeps its older name, pymodelica, because PyPI's pymodelica holds an abandoned Modelon/JModelica.org upload. The import package and the distribution are both modelica.

Why modelica

Python has the two ends of a Modelica toolchain and little of the middle. FMPy and PyFMI run an FMU that some other tool compiled. The tools that compile Modelica (OpenModelica and its OMPython bridge, Dymola, JModelica) are large programs, and none of them is Python. If you want a mature, near-complete Modelica compiler for production, reach for one of those; this is not that yet.

So the gap is narrow and specific, and it is two things:

  • Nothing in Python takes Modelica source to a trajectory the way a hybrid model needs: adaptive stiff integration, index reduction, and events the solver stops at.
  • Nothing treats the model itself as an ordinary Python value (the flat equations, the incidence structure, the index) that you can read, inspect and change.

That gap is the project. Choose modelica when you want Modelica semantics as a library: to inspect the flat DAE, script the toolchain, or embed a solver in your own Python. Choose OpenModelica or Dymola when you need breadth of language coverage today.

Install

pip install modelica

It brings NumPy and SciPy. Python 3.12 or later.

Platform Simulation runs on
Linux x86_64, Linux aarch64, Windows x86_64, macOS arm64 the precompiled engine
any other SciPy

Result.backend says which one ran. The engine runs a System with method="Radau" (the default); other calls run on SciPy (the engine).

Status: 0.1. Parsing, flattening, causalization, simulation and diagrams are tested and give the same numbers on the engine and on SciPy. The API can still change. Experimental and not promised: modelica.fmi, modelica.ssp, and the modelica.sim modules not exported from modelica.sim (fv, fem, mesh, sensitivity, compare).

Quickstart

One call from a .mo file to a trajectory. Save this as Cooling.mo:

model NewtonCooling "A hot object cooling toward ambient"
  parameter Real T_amb = 25;
  parameter Real k = 0.5;
  Real T(start = 90, fixed = true);
equation
  der(T) = -k * (T - T_amb);
  annotation(experiment(StopTime = 10));
end NewtonCooling;
import modelica

result = modelica.run("Cooling.mo", parameters={"k": 1.0}, times=[0, 10])
print(result["T"])   # [90.         25.00295111]

The stop time comes from experiment(StopTime = 10); pass stop= to change it. The same run from the shell, as CSV (-o out.mat writes a Dymola .mat):

python -m modelica.sim Cooling.mo --points 3 --set k=1
time,T_amb,k,T,der_T
0,25,1,90,-65
5,25,1,25.43796545,-0.4379654537
10,25,1,25.00295111,-0.00295111351

Under run are four stages, and each is a value you can stop at and inspect:

from modelica.lang import parse
from modelica.sim import flatten, causalize, simulate

source = """
model NewtonCooling "A hot object cooling toward ambient"
  parameter Real T_amb = 25;
  parameter Real k = 0.5;
  Real T(start = 90, fixed = true);
equation
  der(T) = -k * (T - T_amb);
end NewtonCooling;
"""

flat = flatten([parse(source)], "NewtonCooling")   # AST -> flat DAE
system = causalize(flat)                            # matching, BLT sort, index reduction
result = simulate(system, stop=10.0)                # integrate (Radau by default)

print(system.states)   # ('T',)
print(result.time)     # the solver's own steps
print(result["T"])     # the trajectory, indexed by the name you wrote

Components from the Modelica Standard Library work under their real names. The bundled slice ships in the wheel, so nothing else needs to be passed:

source = """
model RC "A charged capacitor discharging through a resistor"
  Modelica.Electrical.Analog.Basic.Resistor r(R = 1000);
  Modelica.Electrical.Analog.Basic.Capacitor c(C = 1e-3, v(start = 1));
  Modelica.Electrical.Analog.Basic.Ground g;
equation
  connect(r.p, c.p);
  connect(r.n, c.n);
  connect(c.n, g.p);
end RC;
"""

result = simulate(causalize(flatten([parse(source)], "RC")), stop=1.0, times=[0.0, 1.0])
print(result["c.v"])   # [1.         0.36787944], exp(-t / RC) at t = 1

result.plot() returns an interactive chart that writes itself to a self-contained HTML page with no CDN behind it (result.plot().save("cooling.html")), and in a notebook it renders when it is the last line of a cell. result.write("cooling.mat") writes a Dymola-format .mat that ModelicaRes, DyMat and BuildingsPy read.

The layers compose from source to picture the same way. This draws a model to SVG with no other program:

python -m modelica.diagram examples/models/RLCCircuit.mo examples/library/Electrical.mo > rlc.svg
from modelica import diagram

picture = diagram.of("examples/models/RLCCircuit.mo", "examples/library/Electrical.mo")
picture.write("rlc.svg")          # an image, light- and dark-safe (it names no colour)
print(picture.to_mermaid())       # or text for a Markdown page
print(picture.to_dot())           # or source for Graphviz

Key features

  • Two front ends, one IR. A hand-written lexer and recursive-descent parser carry source positions, so an error says where; a Python builder API (modelica.build) produces the same AST values. The two are cross-checked against each other.
  • The flat model is a value. Flattening resolves extends, modifications and connect() into scalar variables and acausal equations; causalization does alias elimination, matching, Tarjan BLT sorting and Pantelides index reduction. States, candidate state sets, index and blocks are all readable off the System.
  • Hybrid simulation. Adaptive stiff integration (Radau on the engine; Radau, BDF and LSODA via SciPy), state and time events with reinit, a sparse Jacobian, and asserts checked along the trajectory.
  • Diagrams with no extra program. Any layer draws to SVG, Mermaid or DOT: components and connections, the incidence of equations against variables, or blocks in solution order.
  • FMI 3.0, started. modelica.fmi writes a model's modelDescription.xml and holds the FMI calling sequence as a state machine. Importing or exporting a whole FMU is not done.
  • A bundled Modelica Standard Library slice under the real Modelica.* names: Units.SI, Electrical.Analog, Mechanics.Translational and Rotational, Thermal.HeatTransfer, and a piece of Blocks. A curated subset, not the whole library. It ships in the wheel and flatten adds it on its own, so Modelica.Electrical.Analog.Basic.Resistor resolves after pip install modelica; a Modelica package you pass yourself is used instead.

The layers

Each layer is usable without the ones above it.

Module What it does
modelica.lang Modelica source to tokens to an AST, with source positions.
modelica.build Python classes to the same AST values. A second front end.
modelica.ir AST to a flat model; then matching, BLT sorting and index reduction.
modelica.sim Flat model to an ODE/DAE problem; solver backends and events.
modelica.diagram Any of the above to a picture: components, incidence, or blocks.
modelica.fmi FMI 3.0: modelDescription.xml and the calling sequence (experimental).
modelica.ssp SSP 2.0: the co-simulation master's protocol (experimental).

What of Modelica it handles

The language is large, so the summary is a table. About 95 % of the Modelica Standard Library example models the project measures against parse; fewer simulate end to end. The per-model coverage notes track the frontier.

Area Handled Not yet
Equations der, algebraic, connect (flow/potential and signal), when with reinit, terminate, structural if-equations, for, assert an if-equation on a continuous condition
Expressions full arithmetic and relations, if-expressions, every built-in math function, smooth / noEvent / pre / homotopy, der(der(x)) spatialDistribution, delay
Structure components, extends with modification, conditional components, redeclare, import, nested packages operator overloading, external (C) functions, impure
Types Real / Integer / Boolean, discrete variables, enumerations, unit and min/max attributes, type aliases
Functions user functions with algorithm bodies, if and for statements
Arrays indexed access, array parameters and starts, element-wise array equations and products (dot, matrix-vector, matrix-matrix), for loops, multi-dimensional grids array / and ^
Numerics index reduction (Pantelides), alias elimination, BLT sorting, adaptive stiff integration (Radau, LSODA), state events, sparse Jacobian a symplectic integrator
Standards FMI 3.0 modelDescription.xml FMU import and export, SSP archives

Contributing

make install-hooks   # once after cloning: pre-commit bumps the version and runs ruff
make check           # lint, typecheck and test. This is what CI runs.
make docs            # build the documentation site, strictly

.prototools pins the toolchain (moon and uv); proto install brings it up. moon.yml holds every task and CI runs one command, moon run :ci. See AGENTS.md for the full build/test/convention detail and CONTRIBUTING.md for how to send a change.

License

MIT © Jorge Cardona. See LICENSE.

Trademarks

Modelica® is a registered trademark of the Modelica Association. FMI and SSP are also its trademarks. This package is an independent open-source implementation, not affiliated with or endorsed by the Modelica Association; the names identify the language it reads and the standards it implements.

Metadata

Release files for modelica 0.1.52

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for modelica 0.1.52
File Size Uploaded Provenance Actions
modelica-0.1.52.tar.gz 1.0 MB Without provenance Details for modelica-0.1.52.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for modelica 0.1.52
File Actions
modelica-0.1.52-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for modelica-0.1.52-py3-none-win_amd64.whl
modelica-0.1.52-py3-none-manylinux_2_28_x86_64.whl Python 3 none Linux glibc 2.28+ x86-64 Details for modelica-0.1.52-py3-none-manylinux_2_28_x86_64.whl
modelica-0.1.52-py3-none-manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.28+ ARM64 Details for modelica-0.1.52-py3-none-manylinux_2_28_aarch64.whl
modelica-0.1.52-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for modelica-0.1.52-py3-none-macosx_11_0_arm64.whl
modelica-0.1.52-py3-none-any.whl Python 3 none any Details for modelica-0.1.52-py3-none-any.whl

Total release size: 4.0 MB

Release files / modelica-0.1.52.tar.gz

Download URL modelica-0.1.52.tar.gz
Size 1.0 MB
Tags Source
SHA-256 checksum
How to use checksums
e1201be7ce893ff7a928f53fc4977aea497dac03f0c80dde89b7908fc15b9315
BLAKE2b-256 checksum
How to use checksums
b30628022f2abe9d8c178d6268d0747cd7979000537d84fa5a99c0388ef90a3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / modelica-0.1.52-py3-none-win_amd64.whl

Download URL modelica-0.1.52-py3-none-win_amd64.whl
Size 633.5 kB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
d0daf5097c889ae9a7656cbdbf5f32b3bbde14b6e7e76d1afa8296d03c3c3933
BLAKE2b-256 checksum
How to use checksums
68b24ddfcd35d8d8df7cd5f0cc40170f8faf5dbf49fb60176b68b125ec215653
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / modelica-0.1.52-py3-none-manylinux_2_28_x86_64.whl

Download URL modelica-0.1.52-py3-none-manylinux_2_28_x86_64.whl
Size 659.9 kB
Tags Linux glibc 2.28+ x86-64 Python 3
SHA-256 checksum
How to use checksums
27388d89eb1d3e236d7e6bd9a1395af0fbb42d24c60664a6b5d61593ea05cbd0
BLAKE2b-256 checksum
How to use checksums
45a14ac2a2dc11b75811b43a0f6462cad6446f2f9d72bceaaab9e073fadf8d92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / modelica-0.1.52-py3-none-manylinux_2_28_aarch64.whl

Download URL modelica-0.1.52-py3-none-manylinux_2_28_aarch64.whl
Size 651.3 kB
Tags Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
96748ccdb6f3d3f83e9c289f4c1c010085f5098ac78d3944d8f73bf8368f2508
BLAKE2b-256 checksum
How to use checksums
f7f1aab5981df14744ea6489dbf1034186d5e14eb12542a0f6ddbcc3ed5fd55f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / modelica-0.1.52-py3-none-macosx_11_0_arm64.whl

Download URL modelica-0.1.52-py3-none-macosx_11_0_arm64.whl
Size 621.5 kB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
68d8788eb9578f45c539550bf6d4b0ec58fde246e193298702e17284d0080f24
BLAKE2b-256 checksum
How to use checksums
0b049bf3482c97561bfdf3b54a118e712a6c77bc30c3056a12e4cba42ae61f3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / modelica-0.1.52-py3-none-any.whl

Download URL modelica-0.1.52-py3-none-any.whl
Size 395.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a007ff2bcb5d6570bc0d85094b9e4cd8ef39db6313e8e6ae71f35b47ac333151
BLAKE2b-256 checksum
How to use checksums
ed2410d5196b83da2e542c6d190b35abd8d93ab2417fd7ba1a9bd8aca707f9d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for modelica 0.1.52

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.52 This release

6 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page