Python wrapper for the libcosim library
Project description
libcosimpy
Python wrapper for the libcosim library. The wrapper uses the libcosimc C wrapper and the ctypes library to make OSP accessible to Python developers.
Getting Started
libcosimpy
is available from PyPI. Run the following command to install the package:
pip install libcosimpy
To install from the source, run the following command at the root directory of the repository:
pip install .
libcosimpy
requires ctypes to call libcosimc
functions. ctypes
is included with Python and does not have to be installed.
Usage
Create execution
Import CosimExecution
from libcosimpy
from libcosimpy.CosimExecution import CosimExecution
Empty execution object
execution = CosimExecution.from_step_size(step_size=1e3)
With a 0.01s fixed time step
From OSP config
execution = CosimExecution.from_osp_config_file(osp_path=f'[PATH_TO_OSP_DIRECTORY]')
From SSP config
execution = CosimExecution.from_ssp_file(ssp_path=f'[PATH_TO_SSP_DIRECTORY]')
Add slave
FMUs can be added manually to execution. OSP and SSP config executions will import all required slaves automatically and this step is not required
Import CosimLocalSlave
from libcosimpy
from libcosimpy.CosimSlave import CosimLocalSlave
Add slave to existing execution
local_slave = CosimLocalSlave(fmu_path=f'[PATH_WITH_FILENAME_TO_FMU]', instance_name='[SOME_UNIQUE_NAME]')
slave_index = execution.add_local_slave(local_slave=local_slave)
Slave index is used for future referencing to the model
Run simulation
Simulations can either be run continiously for a duration
execution.simulate_until(target_time=10e9)
To simulate for 10s
Or stepped manually
execution.step()
With option for stepping multiple steps at once
execution.step(step_count=10)
Finding slave and variable indices
List of slave indices and corresponding indices can be fetched from execution
slave_infos = list(execution.slave_infos())
List of model variables and corresponding indices can be fetched
variables = execution.slave_variables(slave_index=slave_index)
The indices can also be found by unzipping the FMU-file and inspecting the modelDescription.xml
file
Retrieving values from simulation
Import CosimObserver
from libcosimpy
from libcosimpy.CosimObserver import CosimObserver
Observers can be used to retrieve values as Python list
observer = CosimObserver.create_last_value()
execution.add_observer(observer=observer)
# Run simulation
...
# Retrieve floating point values
values = observer.last_real_values(slave_index=[SLAVE_INDEX], # Model to monitor (integer)
variable_references=[VALUE_REFERENCE(s)]) # List of indices to monitor (integer)
Time series and file export observers are also supported
Overriding values in simulation
Import CosimManipulator
from libcosimpy
from libcosimpy.CosimManipulator import CosimManipulator
Manipulators are used to override values
manipulator = CosimManipulator.create_override()
execution.add_manipulator(manipulator=manipulator)
# Run simulation
...
# Override floating point values
manipulator.slave_real_values(slave_index=[SLAVE_INDEX], # Model to monitor (integer)
variable_references=[VALUE_REFERENCE(s)], # Index or list of indices to manipulate (integer)
values=[SOME_OVERRIDE_VALUE(s)]) # Floating point values used for override. Equal length to variable references
execution.step()
Scenario manipulators are also supported
Tests
Tests can be run using the pytest
command in the terminal. libcosimc
log level for all tests can be set in the ./tests/conftest.py
file.
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 Distributions
Built Distributions
Hashes for libcosimpy-0.0.2-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aeeef8d4fbcabf257a512ea2d626b00d281512204da19b9c07b83fe28ab4e9b2 |
|
MD5 | e4a4343291848b537537ec8d59871862 |
|
BLAKE2b-256 | 69c0fb50b030b7b2af61b22bf21519b6f50f7e2c3b1b54472f7b8b23627394d4 |
Hashes for libcosimpy-0.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f94574843496e9692c86c955989cd411c749d6454b4fd891a3881282682b4bc7 |
|
MD5 | a6272122d1e6afb84f4bdc6d4e0ab329 |
|
BLAKE2b-256 | 06e73dc745a7d41e563c5c50daad1198c86eb463cb522b9f90599dad4445e3b4 |
Hashes for libcosimpy-0.0.2-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93c5acf67f95a53e9ce9a27aafa863de9461a04a04933652b2991c0b0e52b3d2 |
|
MD5 | 7ba5e1d0c0913914fc8850a1a02235b6 |
|
BLAKE2b-256 | 059993fd9c6b0b290a31e29287f3b0721171033f64d7636f7c7b1e9192fd2dbb |
Hashes for libcosimpy-0.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 439198d6ff92521df2fb705f1c0516886854ab7c9d53e2c86c2a8e37422f5dd0 |
|
MD5 | bcaa178c0cabb27724aeb77486e73944 |
|
BLAKE2b-256 | 211d74a1b078692b453022228d0a73695302d764b7d7e69b59ea1a478ab388c4 |
Hashes for libcosimpy-0.0.2-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba2e82245425066464297325c6074bb6829c8282dff1057f9ad28f2df97171e1 |
|
MD5 | 7dea9fdfc3799e7ca60b7d262f8ebc98 |
|
BLAKE2b-256 | 6c9d3a5004840737c1cc84874757b43a0cb5f259518200450faf8986addf4d18 |
Hashes for libcosimpy-0.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb58d5fb9cf9ee8fa460e1cbce66ebe94cfb2719181333dc7435e80727509135 |
|
MD5 | 41ae72fae9972e4ed80b18d8fbfadc90 |
|
BLAKE2b-256 | a4b889b64b5920439bcaab95ce52686bb2bbc22c4cd72683e94f669761293619 |
Hashes for libcosimpy-0.0.2-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d20d231425ad9a5dea7316eddd65e3aa7ea138a1ab5d3519f9eb86344dd585cb |
|
MD5 | 4024d18153f96e888c3bc6f32a92cc91 |
|
BLAKE2b-256 | 7b7a7fff187805e536678ad1d087e6a270a7c619109e6bfc76df90f858a206eb |
Hashes for libcosimpy-0.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 291caf8f867c53857859ce5affe1c528ef89209ea773be7a0025a54361be6739 |
|
MD5 | bad96b208e2003f8410f37e410ac1666 |
|
BLAKE2b-256 | 44f04c3a36a3f0202897aabebd26310ff5780782391ede8c3610dd2d3c0513a9 |
Hashes for libcosimpy-0.0.2-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f61c1089c5d792711db7cd7bb5581091b4d1ae52c1fa91856fb382d314145c93 |
|
MD5 | b3d4a4655d6501932f75ffd495a96896 |
|
BLAKE2b-256 | 062ca67f000bc0162c415c69ae31e8c8b7906a919a3e035c3369e1446dbdbd83 |
Hashes for libcosimpy-0.0.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ffd09bf844af3ff086651768fb9be2fc1a6b167c29c69d4c6c995f1688edc207 |
|
MD5 | e68d2a8dc478b6e7a1202c04607d57f1 |
|
BLAKE2b-256 | e05c249a054adf246d75410ea7090bb3965dd628534e368d9cc3423cd67d0cce |