Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 44 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
# PythonFMU3

> A lightweight framework that enables the packaging of Python 3 code as FMUs (following FMI version 3.0).
> Turn a Python class into an FMI 3.0 FMU that Simulink, OMSimulator, FMPy, `fmusim` and
> other simulation tools can import.

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/StephenSmith25/PythonFMU3/issues)

[![CI](https://github.com/StephenSmith25/PythonFMU3/workflows/CI/badge.svg)](https://github.com/StephenSmith25/PythonFMU3/actions?query=workflow%3ACI)
[![PyPI](https://img.shields.io/pypi/v/pythonfmu3)](https://pypi.org/project/pythonfmu3/)
[![Downloads](https://static.pepy.tech/badge/pythonfmu3)](https://pepy.tech/projects/pythonfmu3)
[![conda-forge](https://img.shields.io/conda/vn/conda-forge/pythonfmu3)](https://anaconda.org/conda-forge/pythonfmu3)
[![Read the Docs](https://readthedocs.org/projects/pythonfmu3/badge/?version=latest)](https://pythonfmu3.readthedocs.io/)

**Try it in your browser, no install:** [PythonFMU3 Studio](https://stephensmith25.github.io/pythonfmu3-app/)
lets you write a model, simulate it and download the FMU, all in the browser. To run it on
your computer instead, use `pythonfmu3 studio` (see [the Studio docs](docs/studio.md)).


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:

Both the Model Exchange and Co-simulation interfaces are supported, with Float64, Int32,
Int64, UInt64, Boolean, String and Enumeration variables (plus NumPy arrays of the numeric
types). See the [documentation](https://pythonfmu3.readthedocs.io/) for the full list of
supported features and current limitations, and the `examples` folder for more models.

See the [changelog](docs/changelog.md) for what changed in each release.
[![Listed on fmi-standard.org](https://img.shields.io/badge/FMI_tools-fmi--standard.org-2F5D8A)](https://fmi-standard.org/tools/)

**Try it in your browser, no install:** [PythonFMU3 Studio on the web](https://stephensmith25.github.io/pythonfmu3-app/)
lets you write a model, simulate it and download the FMU. It has an AI assistant that
writes and tests models for you, and you can share a model as a link.

### Highlights

- **Listed on the [FMI tools page](https://fmi-standard.org/tools/)** at fmi-standard.org.
- **Runs in any FMI 3.0 importer:** Python-based tools such as FMPy, and native tools such
as Simulink, OMSimulator and `fmusim`.
- **Co-Simulation and Model Exchange**, with events, arrays, structural parameters, units,
enumerations and FMU state.
- **[PythonFMU3 Studio](docs/studio.md):** `pythonfmu3 studio` opens a local app to write,
check, simulate, build and inspect FMUs, with live plots and model templates. There is
also a [web version](https://stephensmith25.github.io/pythonfmu3-app/).
- **[Jupyter notebooks](docs/notebooks.md):** write a model in a cell with `%%fmu`,
simulate it into a pandas DataFrame, build the FMU.
- **[AI agents](docs/mcp.md):** `pythonfmu3 mcp` lets Claude, GitHub Copilot and Cursor
write, check, simulate and build FMUs. The docs also publish
[`llms.txt`](https://pythonfmu3.readthedocs.io/en/latest/llms.txt) so AI assistants
get the API right.
- **[Examples](examples/README.md)** tested in FMPy and `fmusim`, including
[1D pipe flow for conjugate heat transfer](examples/pipe_cht/).
- **Coming from PythonFMU (FMI 2.0)?** See the [migration guide](docs/migration.md).

See the [documentation](https://pythonfmu3.readthedocs.io/) for the full feature list and
current limitations, and the [changelog](docs/changelog.md) for what changed in each
release.

### Where can I run the FMU?

Expand Down Expand Up @@ -87,7 +101,7 @@ Then ask, for example: "Make me a bouncing ball FMU and simulate it for 5 s." Se
pip install pythonfmu3
```

2. Create a new class extending the `Fmi3Slave` class declared in the `pythonfmu3.fmi3slave` module (see below for an example).
2. Create a class extending `Fmi3Slave` (see below for an example).
Keep a single FMU class per script.
3. Run `pythonfmu3 build` to create the fmu.

Expand Down Expand Up @@ -124,17 +138,17 @@ options:
### 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.
PythonFMU3 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 the examples below).
1. Install the `pythonfmu3` package: `pip install pythonfmu3`
2. Create a class extending `Fmi3Slave` (see the examples below).
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`
1. Install the `pythonfmu3` package: `pip install pythonfmu3`
2. Be sure to be in the Python environment to be updated. Then execute `pythonfmu3 deploy -f my.fmu`

```
Expand Down Expand Up @@ -255,3 +269,9 @@ registration order starting at 0 (here `time` = 0, `x0` = 1, `x1` = 2).
```
pythonfmu3 build -f pythonslaveMX.py
```

### Credits

PythonFMU3 is a fork of [PythonFMU](https://github.com/NTNU-IHB/PythonFMU), which
supports FMI 2.0, extended to FMI 3.0. Many thanks to the PythonFMU authors for the
foundation this project is built on.
68 changes: 40 additions & 28 deletions docs/dependencies.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@
## FMU Dependencies
# Dependencies

Often, Python scripts depends on non-builtin libraries like `numpy`, `scipy`, etc.
_PythonFMU3_ 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:
PythonFMU3 doesn't package a Python environment inside the FMU. The FMU runs in a
Python installation on the machine that simulates it, so that Python needs the packages
your model imports (NumPy, SciPy, a machine-learning library, ...).

1. Install _pythonfmu3_ package: `pip install pythonfmu3`
2. Create a new class extending the `Fmi3Slave` class declared in the `pythonfmu3.fmi3slave` module (see the [Co-simulation examples](usage.md)).
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.
## Shipping your requirements with the FMU

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

1. Install _pythonfmu3_ package: `pip install pythonfmu3`
2. Be sure to be in the Python environment to be updated. Then execute `pythonfmu3 deploy -f my.fmu`
**1. List the packages** in a `requirements.txt` (for pip) or an `environment.yml` (for
conda):

```text
numpy>=1.24
scipy
```
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.
**2. Pass the file to `pythonfmu3 build`** after the script. It is packed into the FMU's
`resources/` folder:

```bash
pythonfmu3 build -f my_model.py requirements.txt
```

positional arguments:
{pip,conda} Python packages manager
**3. On the machine that runs the FMU,** activate the Python environment the FMU will use
(see below) and install the packages from the FMU:

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.
```bash
pythonfmu3 deploy -f MyModel.fmu
```

### Which Python environment does the FMU use?
`deploy` finds `requirements.txt` or `environment.yml` in the FMU and installs it with pip
or conda. Choose the tool explicitly with `pythonfmu3 deploy -f MyModel.fmu pip` (or
`conda`), or name a different file inside the FMU with `-e`.

The machine running the FMU needs PythonFMU3 installed only if you use `deploy`: the FMU
itself carries its own copy of the `pythonfmu3` package.

- **Python-based importer** (e.g. FMPy): the interpreter running the importer, with its
packages. Run `pythonfmu3 deploy` in that same environment.
- **Native importer on Linux/macOS** (OMSimulator, `fmusim`, Simulink, ...): the Python
## Which Python environment does the FMU use?

- **Python-based importer** (FMPy, for example): the interpreter running the importer,
with its packages. Run `pythonfmu3 deploy` in that same environment.
- **Native importer on Linux or macOS** (OMSimulator, `fmusim`, Simulink, ...): the Python
whose `libpython` the FMU loads (see
[Running FMUs outside Python](install.md#running-fmus-outside-python-linux-and-macos)).
That is a base installation, not a virtual environment: packages installed only in a
venv are not found unless you add its `site-packages` folder to `PYTHONPATH`, e.g.
venv aren't found unless you add its `site-packages` folder to `PYTHONPATH`, for example
`export PYTHONPATH=/path/to/venv/lib/python3.12/site-packages`.
- **Windows**: the Python providing the `python3.dll` found on the `PATH`.
- **Windows:** the Python that provides the `python3.dll` found on the `PATH`.

## Keeping dependencies light

Every package a model imports has to be installed wherever the FMU runs, so the fewer the
better. The standard library and NumPy cover most models. For a trained machine-learning
model, consider exporting its weights and evaluating them with NumPy, rather than
requiring the full framework on every machine that runs the FMU.
Loading
Loading