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.3-cp313-cp313-win_amd64.whl (8.0 MB view details)

Uploaded CPython 3.13Windows x86-64

libcosimpy-0.0.3-cp313-cp313-manylinux_2_28_x86_64.whl (24.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

libcosimpy-0.0.3-cp312-cp312-win_amd64.whl (8.0 MB view details)

Uploaded CPython 3.12Windows x86-64

libcosimpy-0.0.3-cp312-cp312-manylinux_2_28_x86_64.whl (24.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

libcosimpy-0.0.3-cp311-cp311-win_amd64.whl (8.0 MB view details)

Uploaded CPython 3.11Windows x86-64

libcosimpy-0.0.3-cp311-cp311-manylinux_2_28_x86_64.whl (24.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

libcosimpy-0.0.3-cp310-cp310-win_amd64.whl (8.0 MB view details)

Uploaded CPython 3.10Windows x86-64

libcosimpy-0.0.3-cp310-cp310-manylinux_2_28_x86_64.whl (24.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

File details

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

File metadata

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

File hashes

Hashes for libcosimpy-0.0.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b24cf065e424d5b4fbf89c24da5221c5d501c12793bd2bdcbc647096979b362b
MD5 b25759e8b5c3f35f8240673426043d9f
BLAKE2b-256 2c50f89e26729c57d76549c0a6939d082704f73e836072a44ac68a4ceb19aad9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libcosimpy-0.0.3-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0a4f107f7575f69ba95f31192045e7689367431f7d27f0a7d1db41b45bf97f3a
MD5 c61afd0e4509c8c5db496c3c814a6e6e
BLAKE2b-256 7c2a86ea6b2ede4a4b2c2daa7dd633ed09c3aa544d7d8916c8dff2fc51e0aaba

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for libcosimpy-0.0.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6594f372e6764c07fc586d59a72c42172b51badd4eefa37f79d7e79ef413e761
MD5 8b4d1e1f2cf489ad61aec741efebb3f3
BLAKE2b-256 19308a4712ea8f80e93055383e12799c9cf5fb5502c1160c5d83179c6c06c522

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libcosimpy-0.0.3-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 29d0a502abeeebaa15f7dc96bd1e77904198305b0ca470d230ca8a164dbd4645
MD5 b9fc810d5b1bcf79d2dde5949742383d
BLAKE2b-256 680ad730c00cad60ab5c5eb5ba4c753a19b631834d0e20ff01ec06b7cd2b5d38

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for libcosimpy-0.0.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 bc9a555119801cda5226b5f74429ae683993c5ef1fd1ad33e1d4790a9b5fc553
MD5 16d6b9fa1d38d88da31e563d3ca58c68
BLAKE2b-256 1cf93e24cb1c35391eaca17d8b8e9ec0d602b5ca37363bfcc9c18b8c2b0e5b16

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for libcosimpy-0.0.3-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3cb3e7eb7259c77f1fe640eb9b347c194b99fc4a5023aad6935772e022631458
MD5 b4cfb25149f59a188baa860aee26ac3c
BLAKE2b-256 c2a9ab3f6ad3182109fb6a0ef10057e8436b9d6aba28681bd14c05b7f2128e32

See more details on using hashes here.

File details

Details for the file libcosimpy-0.0.3-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: libcosimpy-0.0.3-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 8.0 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for libcosimpy-0.0.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4b21b00206f16888715ca09990db2d4852d88f081e2b12eba65d0e141b427f69
MD5 bef2d1249d0c52989b62b5d771ef715e
BLAKE2b-256 6c72def648f876cbe36a47185c40e9c4828c7869449172d3bbe1d60a1d6962f9

See more details on using hashes here.

File details

Details for the file libcosimpy-0.0.3-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for libcosimpy-0.0.3-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bb1a3a6cd8aa354c3b69614cde361c7a1d5672ac75e743738f73776fe70c04f0
MD5 3cb0b1abae8c355997914406b6a79d07
BLAKE2b-256 f9035a5d3f564309b137e24bb37592be7ad96c34c8d8ac0cc6850c4471e8f340

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