Skip to main content

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

Project description

PythonFMU

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

License: MIT contributions welcome CI PyPI

Gitter

How do I build an FMU from python code?

  1. Install pythonfmu package:
pip install git+https://github.com/NTNU-IHB/PythonFMU.git
  1. Create a new class extending the Fmi2Slave class declared in the pythonfmu.fmi2slave module.
  2. Run pythonfmu-builder to create the fmu.
usage: pythonfmu-builder [-h] -f SCRIPT_FILE [-d DEST]
                         [--doc DOCUMENTATION_FOLDER]
                         [Project files [Project files ...]]

Build a 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.
Example:
Write the script
from pythonfmu import Fmi2Causality, Fmi2Slave, Boolean, Integer, Real, String

slave_class = "PythonSlave"  # REQUIRED - Name of the class extending Fmi2Slave


class PythonSlave(Fmi2Slave):

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

    def __init__(self, instance_name):
        super().__init__(instance_name)

        self.intOut = 1
        self.realOut = 3.0
        self.booleanVariable = True
        self.stringVariable = "Hello World!"
        self.register_variable(Integer("intOut", causality=Fmi2Causality.output))
        self.register_variable(Real("realOut", causality=Fmi2Causality.output))
        self.register_variable(Boolean("booleanVariable", causality=Fmi2Causality.local))
        self.register_variable(String("stringVariable", causality=Fmi2Causality.local))

    def do_step(self, current_time, step_size):
        return True
Create the FMU
pythonfmu-builder -f pythonslave.py pythonfmu

In this example a python class named PythonSlave that extends Fmi2Slave is declared in a file named pythonslave.py. pythonfmu is a 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.

Note

PythonFMU does not bundle Python, which makes it a tool coupling solution. This means that you can not expect the generated FMU to work on a different system (The system would need a compatible Python version and libraries). But to ease its usage the wrapper is compile using the limited Python API. So the provided binary libraries for Linux and Windows 64-bits should be compatible with any Python 3 environment. If you need to compile the wrapper for on a specific configuration, you will need cmake and a C++ compiler. The commands for building the wrapper on Linux and on Windows can be seen in the GitHub workflow.

PythonFMU does not automatically resolve 3rd party dependencies either. If your code includes e.g. numpy, the target system also needs to have numpy installed.


Would you rather build FMUs in Java? Check out FMI4j!
Need to distribute your FMUs? FMU-proxy to the rescue!

Credits

This works has been possible thanks to the contributions of @markaren from NTNU-IHB and @fcollonval from Safran SA.

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

pythonfmu-0.3.0.tar.gz (199.1 kB view details)

Uploaded Source

Built Distribution

pythonfmu-0.3.0-py3-none-any.whl (214.7 kB view details)

Uploaded Python 3

File details

Details for the file pythonfmu-0.3.0.tar.gz.

File metadata

  • Download URL: pythonfmu-0.3.0.tar.gz
  • Upload date:
  • Size: 199.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.9

File hashes

Hashes for pythonfmu-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e8789f32ca0422aac4d59735b5ad2189e6372816d73c4fd4e00878026163a0f7
MD5 a19b1a251a71278d258af35ce013e6d0
BLAKE2b-256 de42155cf9f4a6d424e2785d3d41b9a8c8fc795c8c9f0c7d2517432a85e6b428

See more details on using hashes here.

File details

Details for the file pythonfmu-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pythonfmu-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 214.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.9

File hashes

Hashes for pythonfmu-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e43ae5db78f56d6c271c7f02c82663c9d013a03b13a2e52535e22fe332952dd5
MD5 80deb81b68db8904dae181bf784e6171
BLAKE2b-256 8509c7c8be3e92e909622293e6ac841dde9d6f9a499cb4606bf3865297d46672

See more details on using hashes here.

Supported by

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