PyCoSim
================
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
## 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
``` sh
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.
``` python
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:
``` python
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](/resources/chassis_0.png)
![Chassis-Plot](/resources/wheel_0.png)
![Chassis-Plot](/resources/ground_0.png)
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:
``` python
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:
``` python
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`](https://kevinksyTRD.github.io/pycosim/osp_command_line_interface.html#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.
``` python
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](/resources/chassis_1.png)
![chassis-plot](/resources/wheel_1.png)
![chassis-plot](/resources/ground_1.png)
Raw data
{
"_id": null,
"home_page": "https://github.com/kevinksyTRD/pycosim",
"name": "pycosim",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "nbdev jupyter notebook python",
"author": "Kevin Koosup Yum",
"author_email": "kevinkoosup.yum@sintef.no",
"download_url": "https://files.pythonhosted.org/packages/5c/8a/0a6a6ab1c75671198aefa159f49009e3833f6fdf5a49a99e5305f74682e3/pycosim-0.6.5.tar.gz",
"platform": null,
"description": "PyCoSim\n================\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\n\n## Features\n\n- Importing an FMU file, getting information of the model description\n and running a single FMU simulation,\n- Importing a system configuration, configuring output logging and\n scenario, running co-simulation and retrieving the results,\n- Creating a system configuration, adding fmus, connections, initial\n values\n\n## Install\n\n``` sh\npip install pycosim\n```\n\n## How to use\n\n### Creating the simulation instance using the given files\n\nIn the case that you already have all the configuration files and fmu\nfiles ready for the simulation, you can just import files to create the\nsimulation instance. From the simulation instance, use `run_simulation`\nmethod to run simulation. It returns the output instance which contains\nlogging, error and result. The result is a dict of names of components\nas keys and time-series outputs as data.\n\n``` python\nimport os\nfrom pycosim.osp_command_line import LoggingLevel\nfrom pycosim.simulation import FMU, SimulationConfiguration\nimport pandas\n\npandas.options.plotting.backend = \"plotly\"\n\npath_to_osp_system_strucuture_file = os.path.join(\"..\", \"test_data\", \"OspSystemStructureTest.xml\")\npath_to_dir = os.path.dirname(path_to_osp_system_strucuture_file)\n\nsimulation_config = SimulationConfiguration(\n system_structure=path_to_osp_system_strucuture_file,\n path_to_fmu=path_to_dir\n)\n```\n\nNote that the path to the directories that contain all the relevant FMUs\nshould be provided together with the source for the system structure\nfile. When the system is configured, you can run the simulation for a\ngiven simulation time with default settings:\n\n``` python\nsimulation_output = simulation_config.run_simulation(\n duration=10,\n logging_level=LoggingLevel.info\n)\nprint(f\"Logging: {simulation_output.log}\")\nprint(f\"Error: {simulation_output.error}\")\n\nfor name, output in simulation_output.result.items():\n fig = output.plot(title=name)\n fig.write_image(os.path.join(\"..\", \"resources\", f\"{name}_0.png\"))\n```\n\n 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\\\n Simulation will run until {10} seconds.\n Running simulation.\n info: Using cache directory: \"C:\\\\Users\\\\keviny\\\\AppData\\\\Local\\\\cosim\"\n info: [FMI Library: FMILIB] Loading 'win64' binary with 'default' platform types\n info: [FMI Library: FMILIB] Loading 'win64' binary with 'default' platform types\n info: [FMI Library: FMILIB] Loading 'win64' binary with 'default' platform types\n info: 10% complete, t=1.000000\n info: 20% complete, t=2.000000\n info: 30% complete, t=3.000000\n info: 40% complete, t=4.000000\n info: 50% complete, t=5.000000\n info: 60% complete, t=6.000000\n info: 70% complete, t=7.000000\n info: 80% complete, t=8.000000\n info: 90% complete, t=9.000000\n info: 100% complete, t=10.000000\n\n\n Error: \n\n![Chassis-Plot](/resources/chassis_0.png)\n![Chassis-Plot](/resources/wheel_0.png)\n![Chassis-Plot](/resources/ground_0.png)\n\nDefault setting for the simulation is: - No scenario - No logging\nconfiguration (All variables will be logged at each time step.) - The\nsystem structure and output files are saved in the same directory as the\ntemporary one where FMUs are deployed. - Only warning from simulation\nsetting up and progress messages are logged.\n\n### Scenario configuration\n\nA scenario is a collection of events that override / bias / reset a\nvariable of components in the target system. A scenario can be created\nas follows:\n\n``` python\nfrom pyOSPParser.scenario import OSPScenario, OSPEvent\n\nsimulation_end_time = 10\nsimulation_config.scenario = OSPScenario(name='test_scenario', end=simulation_end_time)\n\n# Adding an event to the scenario\nsimulation_config.scenario.add_event(OSPEvent(\n time=5, # Time when the event happens\n model='chassis', # Name_of_the_component\n variable='C.mChassis', # name_of_the_variable,\n action=OSPEvent.OVERRIDE, # Type of actions among OVERRIDE, BIAS, RESET\n value=19.4 # Value (only for OVERRIDE and BIAS)\n))\n```\n\n <pyOSPParser.scenario.OSPEvent at 0x1fe02485c40>\n\n### Logging configuration\n\nA logging configuration specifies which variables will be logged as\noutput of the simulation. A logging configuration can be defined using\nOspLoggingConfiguration class:\n\n``` python\nfrom pyOSPParser.logging_configuration import OspVariableForLogging, OspSimulatorForLogging, OspLoggingConfiguration\n\n# Create a variable object for logging\nvariables = [OspVariableForLogging(name=\"zChassis\"), OspVariableForLogging(name=\"p.e\")]\n\n# Create a logging configuration of a component\nname_of_component = 'chassis'\nlogging_config_comp = OspSimulatorForLogging(\n name=name_of_component,\n decimation_factor=10, # Relative period of how often the logging is made. 10 means once every ten time steps\n variables=variables\n)\n\n# Create a logging configuration instance for the system\nsimulation_config.logging_config = OspLoggingConfiguration(simulators=[logging_config_comp])\n```\n\nYou can set the logging level for the messages during setting up and\nrunning a simulation. You can do that by passing the\n[`LoggingLevel`](https://kevinksyTRD.github.io/pycosim/osp_command_line_interface.html#logginglevel)\nmember when running the simulation. If not specified, it will be\n\u2018warning\u2019 by default.\n\nLet\u2019s run the simulation again and see how the new configuration\naffected the outputs.\n\n``` python\nfrom pycosim.simulation import LoggingLevel\n\nsimulation_output = simulation_config.run_simulation(\n duration=simulation_end_time,\n logging_level=LoggingLevel.warning\n)\n\nprint(f\"Logging: {simulation_output.log}\")\nprint(f\"Error: {simulation_output.error}\")\n\nfor name, output in simulation_output.result.items():\n fig = output.plot(title=name)\n fig.write_image(os.path.join(\"..\", \"resources\", f\"{name}_1.png\"))\n```\n\n![chassis-plot](/resources/chassis_1.png)\n![chassis-plot](/resources/wheel_1.png)\n![chassis-plot](/resources/ground_1.png)\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "A python library to configure and run co-simulation using OSP software and standard",
"version": "0.6.5",
"split_keywords": [
"nbdev",
"jupyter",
"notebook",
"python"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "ebbf58308b07ff1ac1d147b41c66dff0",
"sha256": "1347b75144a224dca0bd2b52594c60d975721bb69d830682880c92f328ae7852"
},
"downloads": -1,
"filename": "pycosim-0.6.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ebbf58308b07ff1ac1d147b41c66dff0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 47500926,
"upload_time": "2022-12-13T18:07:45",
"upload_time_iso_8601": "2022-12-13T18:07:45.405788Z",
"url": "https://files.pythonhosted.org/packages/82/9f/f2dbae8ebc067aecc7c43daa2d91613815dfe91272ed61b6affece1bec9a/pycosim-0.6.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "0ce149a1e37790f8b666c775eb0ea667",
"sha256": "3dab58051d3c369cefef6ae235aa10f276a6f255fe81d16dda8faec67c65d819"
},
"downloads": -1,
"filename": "pycosim-0.6.5.tar.gz",
"has_sig": false,
"md5_digest": "0ce149a1e37790f8b666c775eb0ea667",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 47507129,
"upload_time": "2022-12-13T18:07:50",
"upload_time_iso_8601": "2022-12-13T18:07:50.874875Z",
"url": "https://files.pythonhosted.org/packages/5c/8a/0a6a6ab1c75671198aefa159f49009e3833f6fdf5a49a99e5305f74682e3/pycosim-0.6.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-13 18:07:50",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "kevinksyTRD",
"github_project": "pycosim",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "pycosim"
}