| Crates.io | mosaik-api |
| lib.rs | mosaik-api |
| version | 0.1.0 |
| created_at | 2024-10-27 22:58:33.972479+00 |
| updated_at | 2024-10-27 22:58:33.972479+00 |
| description | High-level API for Mosaik, a flexible Smart Grid co-simulation framework. |
| homepage | |
| repository | https://github.com/ekut-es/mosaik-api |
| max_upload_size | |
| id | 1425093 |
| size | 136,061 |
This project allows Rust code to operate with Mosaik simulations in Python. It provides a high-level API for simulators written in Rust to communicate with the Mosaik Smart-Grid co-simulation framework.
The API is based on the Mosaik API and is compatible with Mosaik version 3 but does not support asynchronous communication yet.
The src folder contains the components for communicating between Rust simulators and Mosaik:
The examples folder contains example simulators based on the official Python tutorial to demonstrate the API. See section Running the example scenarios for more information.
For a simulator written in Rust to successfully communicate with Mosaik you must:
mosaik-api = { git = "https://github.com/ekut-es/mosaik-api" } to your Cargo.toml as a dependency.MosaikApi trait for your simulator.run_simulation() function in your main to connect your simulator to Mosaik. This connects your simulator to Mosaik and handles the communication over a TCP channel. The ConnectionDirection depends on how you connect your simulator to Mosaik in Python (see Connection Setup).SIM_CONFIG of your Mosaik python script as described in the following paragraph.We support two ways to connect Rust simulators for communication with Mosaik. It is sufficient to implement one but possible to implement both simultaneously as shown in the examples.
addr to it, via the "cmd" keyword in Mosaik's SIM_CONFIG in Python. For this you need the ConnectionDirecton::ConnectToAddress in Rust with the given address of Mosaik.addr and connect Mosaik to it with the "connect" keyword in Mosaik's SIM_CONFIG in Python. In Rust you need to use ConnectionDirecton::ListenOnAddress for the run_simulation().Example setup to illustrate these two options with ADDR as the address of the communication channel between Mosaik and the Rust simulator, e.g. 127.0.0.1:5678:
Mosaik SIM_CONFIG key |
Rust ConnectionDirection for run_simulation() |
Notes |
|---|---|---|
"cmd": "cargo run SIMULATOR -- -a=%(addr)s" |
ConnectionDirection::ConnectToAddress(ADDR) |
ADDR needs to be read in from the CLI in Rust. |
"connect": "ADDR" |
ConnectionDirection::ListenOnAddress(ADDR) |
Simulator needs to be started before running the Python script on the predefined ADDR. |
The example simulations are located in the examples folder. It includes two scenarios inspired by the Python tutorials: demo1 and demo2. In contrast to the Python tutorials, the simulations are written in Rust and only invoked by a modified Python script to connect the Rust simulators with Mosaik (or connecting Mosaik with the simulators as shown by the Controller in demo2).
Model used in the hybrid ExampleSim both implemented in example_sim.rs and an event-based Monitor called Collector in collector.rs.Controller. This controller is connected via a TCP connection instead of being run by the Python script for the sake of demonstration.To run them, build a virtual environment for python:
virtualenv .venv_examples && \
source .venv_examples/bin/activate && \
pip install "mosaik>=3.3"
Then run the example scenarios with:
cargo build --examples
python examples/demo1.py
or
cargo build --examples
cargo run --example connector & python examples/demo2.py
For further development in this repository, it is recommended that you use pre-commit and install the local pre-commit hook by running pre-commit install in your terminal.