Skip to main content

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

Using ECCO algorithm

Libcosimpy supports ECCO (Energy-Conservation-based Co-Simulation) algorithm based on the work in [1] for adaptively updating the step size of the simulation. The algorithm uses the law of conservation of energy between FMU models that represent power bonds from bond graph theory.

Creating ECCO algorithm manually

The parameters of the algorithm can be specified via the EccoParam class:

params = EccoParams(
    safety_factor=0.8,
    step_size=1e-4,
    min_step_size=1e-4,
    max_step_size=0.01,
    min_change_rate=0.2,
    max_change_rate=1.5,
    abs_tolerance=1e-4,
    rel_tolerance=1e-4,
    p_gain=0.2,
    i_gain=0.15,
)

The algorithm be created via create_ecco_algorithm, which can be used to create a new execution instance:

# Create an algorithm instance
ecco_algorithm = CosimAlgorithm.create_ecco_algorithm(params)

# Create execution
execution = CosimExecution.from_algorithm(ecco_algorithm)

The power bond between models is represented by input and output connection pair between two models:

# Indicating a power bond between models (indicated by index chassis_index and wheel_index)
ecco_algorithm.add_power_bond(
    chassis_index,
    chassis_v_out,
    chassis_f_in,
    wheel_index,
    wheel_f_out,
    wheel_v_in,
)

The simulation is started as usual via simulate_until function from CosimExecution:

execution.simulate_until(target_time=10e9)

See test_ecco_algorithm for detailed usage of ECCO algorithm.

Creating ECCO algorithm via system structure file

Alternatively, ECCO algorithm can also be created via system structure file:

<OspSystemStructure xmlns="http://opensimulationplatform.com/MSMI/OSPSystemStructure" version="0.1">
    ...
    <!-- Specify ecco algorithm -->
    <Algorithm>ecco</Algorithm>
    ...
    <Connections>
        <!-- Annotate variable connection as power bond via `powerBond` attribute. Specify
             causality of the variable (input or output) -->
        <VariableConnection powerBond="wheelchassis">
            <Variable simulator="chassis" name="velocity" causality="output"/>
            <Variable simulator="wheel" name="in_vel" causality="input"/>
        </VariableConnection>
        <VariableConnection powerBond="wheelchassis">
            <Variable simulator="wheel" name="out_spring_damper_f" causality="output"/>
            <Variable simulator="chassis" name="force" causality="input"/>
        </VariableConnection>
    </Connections>
    <!-- Specify ecco algorithm parameters -->
    <EccoConfiguration>
        <SafetyFactor>0.99</SafetyFactor>
        <StepSize>0.0001</StepSize>
        <MinimumStepSize>0.00001</MinimumStepSize>
        <MaximumStepSize>0.01</MaximumStepSize>
        <MinimumChangeRate>0.2</MinimumChangeRate>
        <MaximumChangeRate>1.5</MaximumChangeRate>
        <ProportionalGain>0.2</ProportionalGain>
        <IntegralGain>0.15</IntegralGain>
        <RelativeTolerance>1e-6</RelativeTolerance>
        <AbsoluteTolerance>1e-6</AbsoluteTolerance>
    </EccoConfiguration>
</OspSystemStructure>

Then this file can be loaded via a usual way via CosimExecution.from_osp_config_file:

execution = CosimExecution.from_osp_config_file(osp_path="tests/data/fmi2/quarter_truck")

See Quarter truck example for detailed usage of ECCO algorithm via system structure file.

Reference

[1] Sadjina, S. and Pedersen, E., 2020. Energy conservation and coupling error reduction in non-iterative co-simulations. Engineering with Computers, 36, pp.1579-1587.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

libcosimpy-0.0.5-cp314-cp314-win_amd64.whl (8.6 MB view details)

Uploaded CPython 3.14Windows x86-64

libcosimpy-0.0.5-cp314-cp314-manylinux_2_28_x86_64.whl (24.8 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ x86-64

libcosimpy-0.0.5-cp313-cp313-win_amd64.whl (8.3 MB view details)

Uploaded CPython 3.13Windows x86-64

libcosimpy-0.0.5-cp313-cp313-manylinux_2_28_x86_64.whl (24.8 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

libcosimpy-0.0.5-cp312-cp312-win_amd64.whl (8.3 MB view details)

Uploaded CPython 3.12Windows x86-64

libcosimpy-0.0.5-cp312-cp312-manylinux_2_28_x86_64.whl (24.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

libcosimpy-0.0.5-cp311-cp311-win_amd64.whl (8.3 MB view details)

Uploaded CPython 3.11Windows x86-64

libcosimpy-0.0.5-cp311-cp311-manylinux_2_28_x86_64.whl (24.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

File details

Details for the file libcosimpy-0.0.5-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: libcosimpy-0.0.5-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 8.6 MB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for libcosimpy-0.0.5-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 dd249be7a03db16c2e07acd84d239fc671152a1aa4c66c1012cf57c16aa08583
MD5 962ea790a71bf43c8d67ec238dcd3d93
BLAKE2b-256 a3198804ddc341bed64a2bf7523b790c3b8784c0b1718d4d06d5848ae0cc768e

See more details on using hashes here.

File details

Details for the file libcosimpy-0.0.5-cp314-cp314-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for libcosimpy-0.0.5-cp314-cp314-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4e4cff9eafd0d78a238c6f9de73e2d8deab5eaabf6e5369baa70badffaba80f9
MD5 9de50b5ffd145a8246c4f9bd9c9df2c1
BLAKE2b-256 a7b8b079db16cf62b62453abbfe0b0775422962c5f693b2c3b8a4f67faf11774

See more details on using hashes here.

File details

Details for the file libcosimpy-0.0.5-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: libcosimpy-0.0.5-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 8.3 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for libcosimpy-0.0.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 0baa0f0f192841d6f4b4156418d934ccb0a7802395805f4739792c630d9159b5
MD5 1181ff7d98135f187801d59f01f26aba
BLAKE2b-256 da0ba4b7d3688564b113bdf429907f86622433861cef8dcfff96a65e16f88d09

See more details on using hashes here.

File details

Details for the file libcosimpy-0.0.5-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for libcosimpy-0.0.5-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 fdfa9995ac83f1e6c7092197f6b3a1dca4af5640588b19dee91f086e0f8fdfa2
MD5 6157aabb6372bdac5d8d26733e1f4a77
BLAKE2b-256 42239f45b9d04d4aca2f8a44476310d86edcc25d2f8cbf83f2b43982c8dee5c1

See more details on using hashes here.

File details

Details for the file libcosimpy-0.0.5-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: libcosimpy-0.0.5-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 8.3 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for libcosimpy-0.0.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 764898cbd9eae30053c3cd7fcb59f51ecc7a75b806771dcb0c111df502a41d2b
MD5 57c218d1644c9c73f54c54f484cf6437
BLAKE2b-256 1766efd3cd4e916660151946cac4649b43541bb68109283152a22c34d8551f23

See more details on using hashes here.

File details

Details for the file libcosimpy-0.0.5-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for libcosimpy-0.0.5-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ec1fae235b05894fc2af4c8a6db2f0b875f3c9da80378a01f8bd5bb717714a9e
MD5 fc834b6c9ab0960bca87631366ba83f4
BLAKE2b-256 62a755e3871d0b8cd1f304817118b3dccb7ae1ed8097725fdf8e659a842d8fdd

See more details on using hashes here.

File details

Details for the file libcosimpy-0.0.5-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: libcosimpy-0.0.5-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 8.3 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for libcosimpy-0.0.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 cc1a698c6098e4ea0ff1de05824f765b01b3b2e77b560e1055dcb90775a788ff
MD5 42578beea6e3b25dfe08ed69d1cc8984
BLAKE2b-256 5ad107e58cdc47a38d6f2689f3913a5b6185d1f7ca3ea8fc6ca0da0e2c15bf72

See more details on using hashes here.

File details

Details for the file libcosimpy-0.0.5-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for libcosimpy-0.0.5-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dcbf584f5e68c79ed894f8fce5cfc7f2c9c2a3a95db41631e9f4cce7b4b3483d
MD5 b931b1fb4e44cc287ec9e41af55b710c
BLAKE2b-256 45450613db10985bf6316f502e8de65bcd1a700a4fa806404e39064580ccc8ff

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page