Package: libcosimpy
libcosimpyPython wrapper for thelibcosimlibrary. The wrapper uses thelibcosimcC wrapper and thectypeslibrary to make OSP accessible to Python developers.Getting Startedlibcosimpyis available from PyPI. Run the following command to install the package:pipinstalllibcosimpyTo install from the source, run the following command at the root directory of the repository:pipinstall.libcosimpyrequiresctypesto calllibcosimcfunctions.ctypesis included with Python and does not have to be installed.UsageCreate executionImportCosimExecutionfromlibcosimpyfromlibcosimpy.CosimExecutionimportCosimExecutionEmpty execution objectexecution=CosimExecution.from_step_size(step_size=1e3)With a 0.01s fixed time stepFrom OSP configexecution=CosimExecution.from_osp_config_file(osp_path=f'[PATH_TO_OSP_DIRECTORY]')From SSP configexecution=CosimExecution.from_ssp_file(ssp_path=f'[PATH_TO_SSP_DIRECTORY]')Add slaveFMUs can be added manually to execution. OSP and SSP config executions will import all required slaves automatically and this step is not requiredImportCosimLocalSlavefromlibcosimpyfromlibcosimpy.CosimSlaveimportCosimLocalSlaveAdd slave to existing executionlocal_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 modelRun simulationSimulations can either be run continiously for a durationexecution.simulate_until(target_time=10e9)To simulate for 10sOr stepped manuallyexecution.step()With option for stepping multiple steps at onceexecution.step(step_count=10)Finding slave and variable indicesList of slave indices and corresponding indices can be fetched from executionslave_infos=list(execution.slave_infos())List of model variables and corresponding indices can be fetchedvariables=execution.slave_variables(slave_index=slave_index)The indices can also be found by unzipping the FMU-file and inspecting themodelDescription.xmlfileRetrieving values from simulationImportCosimObserverfromlibcosimpyfromlibcosimpy.CosimObserverimportCosimObserverObservers can be used to retrieve values as Python listobserver=CosimObserver.create_last_value()execution.add_observer(observer=observer)# Run simulation...# Retrieve floating point valuesvalues=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 supportedOverriding values in simulationImportCosimManipulatorfromlibcosimpyfromlibcosimpy.CosimManipulatorimportCosimManipulatorManipulators are used to override valuesmanipulator=CosimManipulator.create_override()execution.add_manipulator(manipulator=manipulator)# Run simulation...# Override floating point valuesmanipulator.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 referencesexecution.step()Scenario manipulators are also supportedTestsTests can be run using thepytestcommand in the terminal.libcosimclog level for all tests can be set in the./tests/conftest.pyfile.