|
1 | 1 | # PythonFMU3 |
2 | 2 |
|
3 | | -> A lightweight framework that enables the packaging of Python 3 code as co-simulation FMUs (following FMI version 3.0). |
| 3 | +> A lightweight framework that enables the packaging of Python 3 code as **Co-simulation** and **Model Exchange** FMUs (following FMI version 3.0). |
4 | 4 |
|
5 | 5 | [](https://opensource.org/licenses/MIT) |
6 | 6 | [](https://github.com/StephenSmith25/PythonFMU3/issues) |
|
9 | 9 | [](https://pypi.org/project/pythonfmu3/) |
10 | 10 | [](https://pythonfmu3.readthedocs.io/) |
11 | 11 |
|
| 12 | +## Overview |
| 13 | + |
| 14 | +PythonFMU3 bridges the gap between Python's rich ecosystem and the Functional Mock-up Interface (FMI) 3.0 standard, enabling seamless integration of Python models into multi-domain simulation environments. Whether you're developing control systems, signal processing algorithms, or complex system models, PythonFMU3 makes it easy to package your Python code as industry-standard FMUs. |
| 15 | + |
| 16 | +### Key Features |
| 17 | + |
| 18 | +- **🔄 Dual FMU Support**: Create both Co-simulation and Model Exchange FMUs |
| 19 | +- **🐍 Pure Python**: Write your models in familiar Python syntax |
| 20 | +- **📦 Easy Packaging**: Simple CLI tool to build FMUs from Python scripts |
| 21 | +- **🔧 Rich Variable Types**: Support for Float64, Int32, Boolean, String, and arrays |
| 22 | +- **⚡ High Performance**: Optimized C++ runtime with embedded Python interpreter |
| 23 | +- **🌐 Cross-Platform**: Windows, Linux, and macOS support |
| 24 | +- **📊 NumPy Integration**: Native support for array operations and scientific computing |
| 25 | +- **🎛️ Parameter Management**: Configurable parameters with units and constraints |
| 26 | +- **🔍 Debugging Support**: Built-in logging and error handling |
| 27 | + |
| 28 | +### Use Cases |
| 29 | + |
| 30 | +**Control Systems** |
| 31 | +- PID controllers, state machines, and adaptive control algorithms |
| 32 | +- Integration with Simulink, OpenModelica, or other simulation tools |
| 33 | + |
| 34 | +**Signal Processing** |
| 35 | +- Digital filters, signal generators, and data analysis algorithms |
| 36 | +- Real-time processing in co-simulation environments |
| 37 | + |
| 38 | +**Machine Learning** |
| 39 | +- Deploy trained models as FMUs for system-level simulation |
| 40 | +- Integration of AI/ML components in engineering workflows |
| 41 | + |
| 42 | +**System Modeling** |
| 43 | +- Battery models, thermal systems, and multi-physics simulations |
| 44 | +- Rapid prototyping of mathematical models |
| 45 | + |
| 46 | +## Quick Start |
| 47 | + |
| 48 | +### Installation |
| 49 | +```bash |
| 50 | +pip install pythonfmu3 |
| 51 | +``` |
| 52 | + |
| 53 | +### Create Your First FMU |
| 54 | + |
| 55 | +**1. Write a Python model:** |
| 56 | +```python |
| 57 | +from pythonfmu3 import Fmi3Slave, Fmi3Causality, Float64 |
| 58 | + |
| 59 | +class MyModel(Fmi3Slave): |
| 60 | + def __init__(self, **kwargs): |
| 61 | + super().__init__(**kwargs) |
| 62 | + |
| 63 | + # Register variables |
| 64 | + self.register_variable(Float64("input", causality=Fmi3Causality.input)) |
| 65 | + self.register_variable(Float64("output", causality=Fmi3Causality.output)) |
| 66 | + |
| 67 | + # Initialize values |
| 68 | + self.input = 0.0 |
| 69 | + self.output = 0.0 |
| 70 | + |
| 71 | + def do_step(self, current_time, step_size): |
| 72 | + # Your model logic here |
| 73 | + self.output = 2.0 * self.input |
| 74 | + return True |
| 75 | +``` |
| 76 | + |
| 77 | +**2. Build the FMU:** |
| 78 | +```bash |
| 79 | +pythonfmu3 build -f my_model.py |
| 80 | +``` |
| 81 | + |
| 82 | +**3. Use in simulation:** |
| 83 | +```python |
| 84 | +import fmpy |
| 85 | +result = fmpy.simulate_fmu('MyModel.fmu', stop_time=10.0) |
| 86 | +``` |
| 87 | + |
| 88 | +## Architecture |
| 89 | + |
| 90 | +PythonFMU3 uses a hybrid architecture combining the performance of C++ with the flexibility of Python: |
| 91 | + |
| 92 | +- **C++ Runtime**: High-performance FMI interface implementation |
| 93 | +- **Embedded Python**: Isolated Python interpreter for model execution |
| 94 | +- **Smart Bridging**: Efficient data exchange between C++ and Python |
| 95 | +- **Memory Management**: Automatic resource cleanup and garbage collection |
12 | 96 |
|
13 | 97 | 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. |
14 | 98 |
|
15 | | -## Support: |
| 99 | +## What's Supported |
| 100 | + |
| 101 | +### FMI 3.0 Features |
| 102 | +- ✅ **Co-simulation FMUs** with `do_step()` method |
| 103 | +- ✅ **Model Exchange FMUs** with derivative functions |
| 104 | +- ✅ **Variable types**: Float64, Int32, UInt64, Boolean, String |
| 105 | +- ✅ **Array variables** with dimensions and structural parameters |
| 106 | +- ✅ **Parameters** with fixed, tunable, and constant variability |
| 107 | +- ✅ **Units and display units** for physical quantities |
| 108 | +- ✅ **Initial values** and causality declarations |
| 109 | +- ✅ **Event handling** and discrete state updates |
| 110 | +- ✅ **State serialization** and deserialization |
| 111 | +- ✅ **Logging** and error reporting |
| 112 | + |
| 113 | +### Advanced Capabilities |
| 114 | +- ✅ **NumPy integration** for scientific computing |
| 115 | +- ✅ **Custom getters/setters** for complex variable access |
| 116 | +- ✅ **Nested variables** with structured naming |
| 117 | +- ✅ **Resource management** for external files and dependencies |
| 118 | +- ✅ **Multi-platform support** (Windows, Linux, macOS) |
| 119 | +- ✅ **Memory-efficient** variable handling |
| 120 | + |
| 121 | +### Limitations |
| 122 | +- ⚠️ **Binary variables** not yet supported |
| 123 | +- ⚠️ **Clock variables** and scheduled execution not implemented |
| 124 | +- ⚠️ **Some FMI 3.0 advanced features** are still in development |
16 | 125 |
|
17 | | -Please take a look at the examples to see the supported features. |
| 126 | +## Examples and Documentation |
18 | 127 |
|
19 | | -## Future: |
| 128 | +Explore comprehensive examples and documentation: |
20 | 129 |
|
21 | | -In no particular order, we plan to add support for: |
| 130 | +- **[Co-simulation Examples](usage.md)**: PID controllers, filters, state machines |
| 131 | +- **[Model Exchange Examples](usageMX.md)**: Van der Pol oscillator, Robertson problem |
| 132 | +- **[API Reference](api.md)**: Complete class and method documentation |
| 133 | +- **[Installation Guide](install.md)**: Setup instructions for all platforms |
22 | 134 |
|
23 | | -- Support more variable types from FMI3 |
24 | | -- Improve array support |
25 | | -- Add event mode |
26 | | -- Include FMI3 related testing |
| 135 | +## Community and Support |
27 | 136 |
|
| 137 | +- 🐛 **Bug Reports**: [GitHub Issues](https://github.com/StephenSmith25/PythonFMU3/issues) |
| 138 | +- 💡 **Feature Requests**: [GitHub Discussions](https://github.com/StephenSmith25/PythonFMU3/discussions) |
| 139 | +- 📖 **Documentation**: [Read the Docs](https://pythonfmu3.readthedocs.io/) |
| 140 | +- 📦 **PyPI Package**: [pythonfmu3](https://pypi.org/project/pythonfmu3/) |
0 commit comments