Git Product home page Git Product logo

libcosimpy's Introduction

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.

libcosimpy's People

Contributors

davidhjp01 avatar msteinsto avatar tibnor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

msteinsto tibnor

libcosimpy's Issues

String variable references for observers and manipulators

Variables does currently have to be referred to by their id to be monitored with a observer or edited by a manipulator. Finding the id requires the user to unzip the FMU. Indexes can also change if additonal inputs and outputs are added and the FMU recreated.

Enhancement suggestion is to fetch all input, output and parameter names and create a mapping between name and index when the model is first created and use variable name instead of index for referencing.

This would also allow data type specific functions such as slave_integer_values to be replaced with general functions such as slave_values.

No usage section in README

There should be some instructions on how to use the core functionality. Tests do contain example usage, however I would argue the README should give some more guidance.

Replace slave index referencing with object

Slaves within a execution are kept track of using indices. This mimics the wrapped libcosimc functions closely, however this is not very intuitive for a Python programmer.

A potential enhancement to usablility is to create a object holding the index to allow slaves to be referred to as objects

Separate out subcategories of manipulators, observers and executions into child classes

There is currently a singular parent class for CosimManipulator, CosimObserver and CosimExecution objects. Initializing each of them currently uses a non-Pythonic [ClassName].from_[specific_object] or [ClassName].create_[specific_object] approach.

This enhancement would also allow functions such as start_time_series to be renamed to start while making it more intuitive to use (all functions within a class will be relevant and can be applied to the object).

Suggested parent class and new child class split:

  • CosimManipulator -> OverrideManipulator, ScenarioManagerManipulator
  • CosimObserver -> LastValueObserver, ToDirectoryObserver, TimeSeriesObserver
  • CosimExecution -> CosimExecution, OspConfigExecution, SspExecution

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.