{"id":21287683,"url":"https://github.com/ntnu-ihb/pythonfmu","last_synced_at":"2025-04-06T06:07:49.087Z","repository":{"id":41854900,"uuid":"214012255","full_name":"NTNU-IHB/PythonFMU","owner":"NTNU-IHB","description":"A lightweight framework that enables the packaging of Python3.x code as co-simulation FMUs","archived":false,"fork":false,"pushed_at":"2023-12-01T16:59:38.000Z","size":3036,"stargazers_count":117,"open_issues_count":21,"forks_count":38,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-04-24T07:20:19.340Z","etag":null,"topics":["co-simulation","csv","fmi-standard","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NTNU-IHB.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-10-09T20:00:43.000Z","updated_at":"2024-04-22T04:40:31.000Z","dependencies_parsed_at":"2024-01-14T20:16:36.904Z","dependency_job_id":"7f0ac65e-b395-4c6c-9285-56cd3a81441b","html_url":"https://github.com/NTNU-IHB/PythonFMU","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NTNU-IHB%2FPythonFMU","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NTNU-IHB%2FPythonFMU/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NTNU-IHB%2FPythonFMU/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NTNU-IHB%2FPythonFMU/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NTNU-IHB","download_url":"https://codeload.github.com/NTNU-IHB/PythonFMU/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247441050,"owners_count":20939239,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["co-simulation","csv","fmi-standard","python"],"created_at":"2024-11-21T12:14:50.797Z","updated_at":"2025-04-06T06:07:49.070Z","avatar_url":"https://github.com/NTNU-IHB.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PythonFMU\n\n\u003e A lightweight framework that enables the packaging of Python 3 code or CSV files as co-simulation FMUs (following FMI version 2.0).\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/NTNU-IHB/PythonFMU/issues)\n\n[![CI](https://github.com/NTNU-IHB/PythonFMU/workflows/CI/badge.svg)](https://github.com/NTNU-IHB/PythonFMU/actions?query=workflow%3ACI)\n[![PyPI](https://img.shields.io/pypi/v/pythonfmu)](https://pypi.org/project/pythonfmu/)\n[![Conda Version](https://img.shields.io/conda/vn/conda-forge/pythonfmu.svg)](https://anaconda.org/conda-forge/pythonfmu)\n\n[![Gitter](https://badges.gitter.im/NTNU-IHB/FMI4j.svg)](https://gitter.im/NTNU-IHB/PythonFMU?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge)\n\n### How do I build an FMU from python code?\n\n1. Install `pythonfmu` package:\n\n```bash\npip install pythonfmu\n```\n\n2. Create a new class extending the `Fmi2Slave` class declared in the `pythonfmu.fmi2slave` module (see below for an example).\n3. Run `pythonfmu build` to create the fmu.\n\n```\nusage: pythonfmu build [-h] -f SCRIPT_FILE [-d DEST] [--doc DOCUMENTATION_FOLDER] [--no-external-tool]\n                       [--no-variable-step] [--interpolate-inputs] [--only-one-per-process] [--handle-state]\n                       [--serialize-state] [--use-memory-management]\n                       [Project files [Project files ...]]\n\nBuild an FMU from a Python script.\n\npositional arguments:\n  Project files         Additional project files required by the Python script.\n\noptional arguments:\n  -h, --help            show this help message and exit\n  -f SCRIPT_FILE, --file SCRIPT_FILE\n                        Path to the Python script.\n  -d DEST, --dest DEST  Where to save the FMU.\n  --doc DOCUMENTATION_FOLDER\n                        Documentation folder to include in the FMU.\n  --no-external-tool    If given, needsExecutionTool=false\n  --no-variable-step    If given, canHandleVariableCommunicationStepSize=false\n  --interpolate-inputs  If given, canInterpolateInputs=true\n  --only-one-per-process\n                        If given, canBeInstantiatedOnlyOncePerProcess=true\n  --handle-state        If given, canGetAndSetFMUstate=true\n  --serialize-state     If given, canSerializeFMUstate=true\n```\n\n### How do I build an FMU from python code with third-party dependencies?\n\nOften, Python scripts depends on non-builtin libraries like `numpy`, `scipy`, etc.\n_PythonFMU_ does not package a full environment within the FMU.\nHowever, you can package a `requirements.txt` or `environment.yml` file within your FMU following these steps:\n\n1. Install _pythonfmu_ package: `pip install pythonfmu`\n2. Create a new class extending the `Fmi2Slave` class declared in the `pythonfmu.fmi2slave` module (see below for an example).\n3. Create a `requirements.txt` file (to use _pip_ manager) and/or a `environment.yml` file (to use _conda_ manager) that defines your dependencies.\n4. Run `pythonfmu build -f myscript.py requirements.txt` to create the fmu including the dependencies file.\n\nAnd using `pythonfmu deploy`, end users will be able to update their local Python environment. The steps to achieve that:\n\n1. Install _pythonfmu_ package: `pip install pythonfmu`\n2. Be sure to be in the Python environment to be updated. Then execute `pythonfmu deploy -f my.fmu`\n\n```\nusage: pythonfmu deploy [-h] -f FMU [-e ENVIRONMENT] [{pip,conda}]\n\nDeploy a Python FMU. The command will look in the `resources` folder for one of the following files:\n`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.\n\npositional arguments:\n  {pip,conda}           Python packages manager\n\noptional arguments:\n  -h, --help            show this help message and exit\n  -f FMU, --file FMU    Path to the Python FMU.\n  -e ENVIRONMENT, --env ENVIRONMENT\n                        Requirements or environment file.\n```\n\n### Example:\n\n#### Write the script\n\n```python\n\nfrom pythonfmu import Fmi2Causality, Fmi2Slave, Boolean, Integer, Real, String\n\n\nclass PythonSlave(Fmi2Slave):\n\n    author = \"John Doe\"\n    description = \"A simple description\"\n\n    def __init__(self, **kwargs):\n        super().__init__(**kwargs)\n\n        self.intOut = 1\n        self.realOut = 3.0\n        self.booleanVariable = True\n        self.stringVariable = \"Hello World!\"\n        self.register_variable(Integer(\"intOut\", causality=Fmi2Causality.output))\n        self.register_variable(Real(\"realOut\", causality=Fmi2Causality.output))\n        self.register_variable(Boolean(\"booleanVariable\", causality=Fmi2Causality.local))\n        self.register_variable(String(\"stringVariable\", causality=Fmi2Causality.local))\n        \n        # Note:\n        # it is also possible to explicitly define getters and setters as lambdas in case the variable is not backed by a Python field.\n        # self.register_variable(Real(\"myReal\", causality=Fmi2Causality.output, getter=lambda: self.realOut, setter=lambda v: set_real_out(v))\n\n    def do_step(self, current_time, step_size):\n        return True\n\n```\n\n#### Create the FMU\n\n```\npythonfmu build -f pythonslave.py myproject\n```\n\nIn this example a python class named `PythonSlave` that extends `Fmi2Slave` is declared in a file named `pythonslave.py`,\nwhere `myproject` is an optional folder containing additional project files required by the python script.\nProject folders such as this will be recursively copied into the FMU. Multiple project files/folders may be added.\n\n### Note\n\nPythonFMU does not bundle Python, which makes it a tool coupling solution.\nThis 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).\nBut to ease its usage the wrapper uses the limited Python API, making the pre-built binaries for Linux and Windows 64-bits\ncompatible with any Python 3 environment. If you need to compile the wrapper for a specific configuration,\nyou will need CMake and a C++ compiler. The commands for building the wrapper on Linux and on Windows can be seen in\nthe [GitHub workflow](./.github/workflows/main.yml).\n\nPythonFMU does not automatically resolve 3rd party dependencies. If your code includes e.g. `numpy`, the target system also needs to have `numpy` installed.\n\n---\n\nWould you rather build FMUs in Java? Check out [FMI4j](https://github.com/NTNU-IHB/FMI4j)!  \nNeed to distribute your FMUs? [FMU-proxy](https://github.com/NTNU-IHB/FMU-proxy) to the rescue!\n\n### Publications\n\n[Hatledal, Lars Ivar, Frédéric Collonval, and Houxiang Zhang. \"Enabling python driven co-simulation models with PythonFMU.\" Proceedings of the 34th International ECMS-Conference on Modelling and Simulation-ECMS 2020. ECMS European Council for Modelling and Simulation, 2020.](https://doi.org/10.7148/2020-0235)\n\n### Credits\n\nThis work has been possible thanks to the contributions of: \u003cbr\u003e \n@markaren from NTNU-IHB \u003cbr\u003e\n@fcollonval from Safran SA.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fntnu-ihb%2Fpythonfmu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fntnu-ihb%2Fpythonfmu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fntnu-ihb%2Fpythonfmu/lists"}