Skip to main content

PyCoSim

Features

  • Importing an FMU file, getting information of the model description and running a single FMU simulation,
  • Importing a system configuration, configuring output logging and scenario, running co-simulation and retrieving the results,
  • Creating a system configuration, adding fmus, connections, initial values

Install

pip install pycosim

How to use

Creating the simulation instance using the given files

In the case that you already have all the configuration files and fmu files ready for the simulation, you can just import files to create the simulation instance. From the simulation instance, use run_simulation method to run simulation. It returns the output instance which contains logging, error and result. The result is a dict of names of components as keys and time-series outputs as data.

import os
from pycosim.osp_command_line import LoggingLevel
from pycosim.simulation import FMU, SimulationConfiguration
import pandas

pandas.options.plotting.backend = "plotly"

path_to_osp_system_strucuture_file = os.path.join("..", "test_data", "OspSystemStructureTest.xml")
path_to_dir = os.path.dirname(path_to_osp_system_strucuture_file)

simulation_config = SimulationConfiguration(
    system_structure=path_to_osp_system_strucuture_file,
    path_to_fmu=path_to_dir
)

Note that the path to the directories that contain all the relevant FMUs should be provided together with the source for the system structure file. When the system is configured, you can run the simulation for a given simulation time with default settings:

simulation_output = simulation_config.run_simulation(
    duration=10,
    logging_level=LoggingLevel.info
)
print(f"Logging: {simulation_output.log}")
print(f"Error: {simulation_output.error}")

for name, output in simulation_output.result.items():
    fig = output.plot(title=name)
    fig.write_image(os.path.join("..", "resources", f"{name}_0.png"))
Logging: Output csv files will be saved in the following directory: C:\Users\keviny\AppData\Local\Temp\pycosim_tmp\sim_0bb5ed62-1e64-45a4-8e23-0f05dcfab787\
Simulation will run until {10} seconds.
Running simulation.
info: Using cache directory: "C:\\Users\\keviny\\AppData\\Local\\cosim"
info: [FMI Library: FMILIB] Loading 'win64' binary with 'default' platform types
info: [FMI Library: FMILIB] Loading 'win64' binary with 'default' platform types
info: [FMI Library: FMILIB] Loading 'win64' binary with 'default' platform types
info: 10% complete, t=1.000000
info: 20% complete, t=2.000000
info: 30% complete, t=3.000000
info: 40% complete, t=4.000000
info: 50% complete, t=5.000000
info: 60% complete, t=6.000000
info: 70% complete, t=7.000000
info: 80% complete, t=8.000000
info: 90% complete, t=9.000000
info: 100% complete, t=10.000000


Error: 

Chassis-Plot Chassis-Plot Chassis-Plot

Default setting for the simulation is: - No scenario - No logging configuration (All variables will be logged at each time step.) - The system structure and output files are saved in the same directory as the temporary one where FMUs are deployed. - Only warning from simulation setting up and progress messages are logged.

Scenario configuration

A scenario is a collection of events that override / bias / reset a variable of components in the target system. A scenario can be created as follows:

from pyOSPParser.scenario import OSPScenario, OSPEvent

simulation_end_time = 10
simulation_config.scenario = OSPScenario(name='test_scenario', end=simulation_end_time)

# Adding an event to the scenario
simulation_config.scenario.add_event(OSPEvent(
    time=5,  # Time when the event happens
    model='chassis',  # Name_of_the_component
    variable='C.mChassis', # name_of_the_variable,
    action=OSPEvent.OVERRIDE, # Type of actions among OVERRIDE, BIAS, RESET
    value=19.4 # Value (only for OVERRIDE and BIAS)
))
<pyOSPParser.scenario.OSPEvent at 0x1fe02485c40>

Logging configuration

A logging configuration specifies which variables will be logged as output of the simulation. A logging configuration can be defined using OspLoggingConfiguration class:

from pyOSPParser.logging_configuration import OspVariableForLogging, OspSimulatorForLogging, OspLoggingConfiguration

# Create a variable object for logging
variables = [OspVariableForLogging(name="zChassis"), OspVariableForLogging(name="p.e")]

# Create a logging configuration of a component
name_of_component = 'chassis'
logging_config_comp = OspSimulatorForLogging(
    name=name_of_component,
    decimation_factor=10, # Relative period of how often the logging is made. 10 means once every ten time steps
    variables=variables
)

# Create a logging configuration instance for the system
simulation_config.logging_config = OspLoggingConfiguration(simulators=[logging_config_comp])

You can set the logging level for the messages during setting up and running a simulation. You can do that by passing the LoggingLevel member when running the simulation. If not specified, it will be ‘warning’ by default.

Let’s run the simulation again and see how the new configuration affected the outputs.

from pycosim.simulation import LoggingLevel

simulation_output = simulation_config.run_simulation(
    duration=simulation_end_time,
    logging_level=LoggingLevel.warning
)

print(f"Logging: {simulation_output.log}")
print(f"Error: {simulation_output.error}")

for name, output in simulation_output.result.items():
    fig = output.plot(title=name)
    fig.write_image(os.path.join("..", "resources", f"{name}_1.png"))

chassis-plot chassis-plot chassis-plot

Download files

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

Source Distribution

pycosim-0.6.4.tar.gz (47.5 MB view details)

Uploaded Source

Built Distribution

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

pycosim-0.6.4-py3-none-any.whl (47.5 MB view details)

Uploaded Python 3

File details

Details for the file pycosim-0.6.4.tar.gz.

File metadata

  • Download URL: pycosim-0.6.4.tar.gz
  • Upload date:
  • Size: 47.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for pycosim-0.6.4.tar.gz
Algorithm Hash digest
SHA256 2c791828f8c87b68b82b37fb8096b837c36b2adb975f2935e9bc45052d17b8d4
MD5 0830a908185ca41b1121cfe4c2054134
BLAKE2b-256 c1b524c839af3e6f6ebd7e28a3dd968028df290ecd4664cecfa12264f1dc75d7

See more details on using hashes here.

File details

Details for the file pycosim-0.6.4-py3-none-any.whl.

File metadata

  • Download URL: pycosim-0.6.4-py3-none-any.whl
  • Upload date:
  • Size: 47.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for pycosim-0.6.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3cbe6287dad2fad4280229d1f68cc69815f3a68de3c9abcebb7afcb0309d6fdd
MD5 39e90bdf37df83d4d0045d42dc11de33
BLAKE2b-256 82d6ce617e3cf2fe5ca546c4013a5a02d374d4b9468bcc41b9bf45f730af3077

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