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 co-simulation FMUs (following FMI version 3.0).
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:
We currently support a basic FMU which follows the FMI3 standard. This includes support for:
- Float64, Int32, UInt64, String, Boolean variables
- Step Mode
- Arrays, which requires the numpy package
Please take a look at the examples bouncingBall.py and linearTransform.py for a demonstration of these features.
Future
In no particular order, we plan to add support for:
- Refactor Real, Integer ect .to match FMI3 specification
- Support more variable types from FMI3
- Improve array support
- Add event mode
- Include FMI3 related testing
How do I build an FMU from python code?
- Install
pythonfmu3
package:
pip install pythonfmu3
- Create a new class extending the
Fmi3Slave
class declared in thepythonfmu3.fmi3slave
module (see below for an example). - Run
pythonfmu3 build
to create the fmu.
usage: pythonfmu3 build [-h] -f SCRIPT_FILE [-d DEST] [--doc DOCUMENTATION_FOLDER] [--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.
--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:
- Install pythonfmu package:
pip install pythonfmu3
- Create a new class extending the
Fmi3Slave
class declared in thepythonfmu3.fmi3slave
module (see below for an example). - Create a
requirements.txt
file (to use pip manager) and/or aenvironment.yml
file (to use conda manager) that defines your dependencies. - 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:
- Install pythonfmu package:
pip install pythonfmu3
- 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.
Example:
Write the script
from pythonfmu3 import Fmi3Causality, Fmi3Slave, Boolean, Integer, Real, String
class PythonSlave(Fmi3Slave):
author = "John Doe"
description = "A simple description"
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.intOut = 1
self.realOut = 3.0
self.booleanVariable = True
self.stringVariable = "Hello World!"
self.register_variable(Integer("intOut", causality=Fmi3Causality.output))
self.register_variable(Real("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(Real("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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pythonfmu3-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2e43509fd1c9f76c85f85e78d4c9fa6b9815151b72fc13bd00a50b5f6e39021 |
|
MD5 | ae0673aff40c7375720fb1aeea34b2d7 |
|
BLAKE2b-256 | e14e76dae0fc2b73d7f8a18d357e01cb1b565ba63e25fd8c89468522bb11cc7e |