Skip to main content

A lightweight framework that enables the packaging of Python3.x code as co-simulation FMUs.

Project description

PythonFMU3

A lightweight framework that enables the packaging of Python 3 code as FMUs (following FMI version 3.0).

License: MIT contributions welcome

CI PyPI Read the Docs

This project is a fork of the original PythonFMU repository available at https://github.com/NTNU-IHB/PythonFMU, which was used as the basis for adding support for FMI 3.0. While we have made efforts to expand the functionality of this project, it currently has some limitations and does not support all the features of FMI 3.0. We would like to acknowledge and give credit to the original PythonFMU project for their contributions to this work.

Support:

Please take a look at the examples to see the supported features. We currently support both the Model exchange and Cosimulation Interfaces.

How do I build an FMU from python code?

  1. Install pythonfmu3 package:
pip install pythonfmu3
  1. Create a new class extending the Fmi3Slave class declared in the pythonfmu3.fmi3slave module (see below for an example).
  2. Run pythonfmu3 build to create the fmu.
usage: pythonfmu3 build [-h] -f SCRIPT_FILE [-d DEST] [--doc DOCUMENTATION_FOLDER] [--terminals TERMINALS_FILE] [--no-external-tool]
                       [--no-variable-step] [--interpolate-inputs] [--only-one-per-process] [--handle-state]
                       [--serialize-state] [--use-memory-management]
                       [Project files [Project files ...]]

Build an FMU from a Python script.

positional arguments:
  Project files         Additional project files required by the Python script.

optional arguments:
  -h, --help            show this help message and exit
  -f SCRIPT_FILE, --file SCRIPT_FILE
                        Path to the Python script.
  -d DEST, --dest DEST  Where to save the FMU.
  --doc DOCUMENTATION_FOLDER
                        Documentation folder to include in the FMU.
  --terminals TERMINALS_FILE
                        Terminals file (terminalsAndIcons.xml) to include in the FMU.
  --no-external-tool    If given, needsExecutionTool=false
  --no-variable-step    If given, canHandleVariableCommunicationStepSize=false
  --interpolate-inputs  If given, canInterpolateInputs=true
  --only-one-per-process
                        If given, canBeInstantiatedOnlyOncePerProcess=true
  --handle-state        If given, canGetAndSetFMUstate=true
  --serialize-state     If given, canSerializeFMUstate=true

How do I build an FMU from python code with third-party dependencies?

Often, Python scripts depends on non-builtin libraries like numpy, scipy, etc. PythonFMU does not package a full environment within the FMU. However, you can package a requirements.txt or environment.yml file within your FMU following these steps:

  1. Install pythonfmu package: pip install pythonfmu3
  2. Create a new class extending the Fmi3Slave class declared in the pythonfmu3.fmi3slave module (see below for an example).
  3. Create a requirements.txt file (to use pip manager) and/or a environment.yml file (to use conda manager) that defines your dependencies.
  4. Run pythonfmu3 build -f myscript.py requirements.txt to create the fmu including the dependencies file.

And using pythonfmu3 deploy, end users will be able to update their local Python environment. The steps to achieve that:

  1. Install pythonfmu package: pip install pythonfmu3
  2. Be sure to be in the Python environment to be updated. Then execute pythonfmu3 deploy -f my.fmu
usage: pythonfmu3 deploy [-h] -f FMU [-e ENVIRONMENT] [{pip,conda}]

Deploy a Python FMU. The command will look in the `resources` folder for one of the following files:
`requirements.txt` or `environment.yml`. If you specify a environment file but no package manager, `conda` will be selected for `.yaml` and `.yml` otherwise `pip` will be used. The tool assume the Python environment in which the FMU should be executed is the current one.

positional arguments:
  {pip,conda}           Python packages manager

optional arguments:
  -h, --help            show this help message and exit
  -f FMU, --file FMU    Path to the Python FMU.
  -e ENVIRONMENT, --env ENVIRONMENT
                        Requirements or environment file.

Cosimulation Example:

Write the script

from pythonfmu3 import Fmi3Causality, Fmi3Slave, Boolean, Int32, Float64, String


class PythonSlave(Fmi3Slave):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.author = "John Doe"
        self.description = "A simple description"

        self.time = 0.0
        self.intOut = 1
        self.realOut = 3.0
        self.booleanVariable = True
        self.stringVariable = "Hello World!"
        self.register_variable(Float64("time", causality=Fmi3Causality.independent))
        self.register_variable(Int32("intOut", causality=Fmi3Causality.output))
        self.register_variable(Float64("realOut", causality=Fmi3Causality.output))
        self.register_variable(Boolean("booleanVariable", causality=Fmi3Causality.local))
        self.register_variable(String("stringVariable", causality=Fmi3Causality.local))
        
        # Note:
        # it is also possible to explicitly define getters and setters as lambdas in case the variable is not backed by a Python field.
        # self.register_variable(Float64("myReal", causality=Fmi3Causality.output, getter=lambda: self.realOut, setter=lambda v: set_real_out(v))

    def do_step(self, current_time, step_size):
        return True

Create the FMU

pythonfmu3 build -f pythonslave.py myproject

In this example a python class named PythonSlave that extends Fmi3Slave is declared in a file named pythonslave.py, where myproject is an optional folder containing additional project files required by the python script. Project folders such as this will be recursively copied into the FMU. Multiple project files/folders may be added.

Model Exchange Example

To create a model exchange FMU you must inherit from Fmi3SlaveBase and one of (or both) mixin classes ModelExchange or CoSimulation.

Write the script

from pythonfmu3 import Fmi3Causality, Fmi3SlaveBase, Float64, ModelExchange
from typing import List


class PythonSlaveMX(Fmi3SlaveBase, ModelExchange):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.author = "John Doe"
        self.description = "A simple description"

        self.time = 0.0
        self.mu = 1.0
        self.x0 = 2
        self.x1 = 0
        self.derx0 = 0.0
        self.derx1 = 0.0

        self.register_variable(Float64("time", causality=Fmi3Causality.independent, variability=Fmi3Variability.continuous))
        self.register_variable(Float64("x0", causality=Fmi3Causality.output, start=2, variability=Fmi3Variability.continuous, initial=Fmi3Initial.exact))
        self.register_variable(Float64("x1", causality=Fmi3Causality.output, start=0, variability=Fmi3Variability.continuous, initial=Fmi3Initial.exact))
        self.register_variable(Float64("derx0", causality=Fmi3Causality.local, variability=Fmi3Variability.continuous, derivative=1))
        self.register_variable(Float64("derx1", causality=Fmi3Causality.local, variability=Fmi3Variability.continuous, derivative=2))
        self.register_variable(Float64("mu", causality=Fmi3Causality.parameter, variability=Fmi3Variability.fixed))


    def get_continuous_state_derivatives(self) -> List[float]:
        self.derx0 = self.x1
        self.derx1 = self.mu * ((1 - self.x0**2) * self.x1) - self.x0
        return [self.derx0, self.derx1]
        

Note: There is a hard requirement that we have a self.time member in the derived class.

Create the FMU

pythonfmu3 build -f pythonslaveMX.py

Project details


Download files

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

Source Distribution

pythonfmu3-0.3.4.tar.gz (346.8 kB view details)

Uploaded Source

Built Distribution

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

pythonfmu3-0.3.4-py3-none-any.whl (363.1 kB view details)

Uploaded Python 3

File details

Details for the file pythonfmu3-0.3.4.tar.gz.

File metadata

  • Download URL: pythonfmu3-0.3.4.tar.gz
  • Upload date:
  • Size: 346.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pythonfmu3-0.3.4.tar.gz
Algorithm Hash digest
SHA256 aed8b87de9598b6653f2bbac37453db1e911de75f1bbce471c2df9526a9d6471
MD5 b5b166d1d7ce2f38d2820cb79b1b98ca
BLAKE2b-256 87aad1249d2b3c3e0b5cd7d661e842956f2fb27a6df3cddb3ba5ca7063382704

See more details on using hashes here.

File details

Details for the file pythonfmu3-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: pythonfmu3-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 363.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pythonfmu3-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1723ec5937285de07121e1c4eb8e60efdd7b074063a6906a4133408a489ec1da
MD5 44efb08a41e10b8e6cd0a8540f404820
BLAKE2b-256 7f2e15b0d4a2cf2960e50cd18402078a63e77d381453cfc5253cf0e7d896eb06

See more details on using hashes here.

Supported by

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