Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Pymoca

A Modelica to computer algebra system (CAS) translator written in Python.

CI Coverage DOI

Overview

Pymoca can be used in applications that need to translate Modelica mathematical models into other forms. Pymoca can "flatten" a model containing a connected set of components defined by object-oriented Modelica classes into a set of variables and simultaneous equations that are easier to further process for analysis or simulation. It is particularly suited to provide Modelica models in symbolic form to computer algebra systems. A common use in this context is to provide differential and algebraic equations for use in optimal control problems. Pymoca can translate Modelica to CasADi, SymPy, and ModelicaXML, but most development and usage has been with CasADi.

Install

For parser support without backend dependencies:

pip install pymoca

Other options are:

pip install "pymoca[casadi]"    # CasADi backend dependencies
pip install "pymoca[sympy]"     # SymPy backend dependencies
pip install "pymoca[lxml]"      # ModelicaXML backend dependencies

pip install "pymoca[examples]"  # To run Jupyter notebook examples in the repo

pip install "pymoca[all]"       # All of the above
pip install "pymoca[all,test]"  # Add test dependencies to all

Usage

The Command Line Interface

A pymoca script is installed into your PATH with the PyPI install. Run pymoca --help at the command line for usage help. Pymoca error checking is not always complete or easy to understand, so it is better to develop the Modelica code with other tools and then use Pymoca for translation.

Scripting

Pymoca reads and understands Modelica code (pymoca.parser) and provides access to an internal representation of the code called an Abstract Syntax Tree or AST (pymoca.ast). The AST is further processed to generate output in various formats (pymoca.backends). The pymoca.tree module provides functionality to transform the AST into a form that can be more easily used by the backends to generate the target output. In particular, pymoca.tree provides classes and functions to convert a hierarchical, object-oriented Modelica model of connected components into a "flat" system of equations and associated variables, parameters, and constants.

The test suite contains examples showing how to use Pymoca and the subset of Modelica that it currently supports.

Here is an example using a simple spring and damper model from the test suite:

from pprint import pprint

import pymoca.parser
import pymoca.backends.casadi.generator as casadi_backend


MODELICA_MODEL = """
model Spring
    Real x, v_x;
    parameter Real c = 0.1;
    parameter Real k = 2;
equation
    der(x) = v_x;
    der(v_x) = -k*x - c*v_x;
end Spring;
"""

print("Modelica Model:\n", MODELICA_MODEL)

print("\nEquations from the parsed AST in a JSON representation:")
ast = pymoca.parser.parse(MODELICA_MODEL)
pprint(ast.to_json(ast.classes["Spring"].equations))

print("\nGenerated CasADi model:")
casadi_model = casadi_backend.generate(ast, "Spring")
print(casadi_model)

Some more interesting examples are in Jupyter notebooks:

Roadmap

See the GitHub Projects for plans. In particular, see the Info Panel in the Modelica Flattening project for an overview of a project getting some current focus. Breaking API changes are expected.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymoca-0.12.0rc3.tar.gz (213.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymoca-0.12.0rc3-py3-none-any.whl (175.9 kB view details)

Uploaded Python 3

File details

Details for the file pymoca-0.12.0rc3.tar.gz.

File metadata

  • Download URL: pymoca-0.12.0rc3.tar.gz
  • Upload date:
  • Size: 213.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pymoca-0.12.0rc3.tar.gz
Algorithm Hash digest
SHA256 5ce8ec3f9f7c780cba289922cdfc15e20278ad00b9a5e3ee084ca1bf9307ea66
MD5 7bfaca10373b6215d7d0a203e3773491
BLAKE2b-256 5283c7cafd7b1a6fd0159c0bc26ad0990ffd244652bfcc942de81f74935f2d77

See more details on using hashes here.

Provenance

The following attestation bundles were made for pymoca-0.12.0rc3.tar.gz:

Publisher: ci.yml on pymoca/pymoca

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pymoca-0.12.0rc3-py3-none-any.whl.

File metadata

  • Download URL: pymoca-0.12.0rc3-py3-none-any.whl
  • Upload date:
  • Size: 175.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pymoca-0.12.0rc3-py3-none-any.whl
Algorithm Hash digest
SHA256 2ebb6e072b3938cc6c26e63815a92435b9da3132623e572ef69ffda9db511c77
MD5 084d5a45ca45f713c63d4f28fec8a366
BLAKE2b-256 a12d601e98d368179b48b54b6f496dde8702c5ae92b3bec1df6bf6ac05bd6478

See more details on using hashes here.

Provenance

The following attestation bundles were made for pymoca-0.12.0rc3-py3-none-any.whl:

Publisher: ci.yml on pymoca/pymoca

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page