6. OpenModelicaScripting¶
This is a shared library that provides a OpenModelica Scripting interface for the OMSimulatorLib library.
6.1. Examples¶
loadOMSimulator();
oms_setTempDirectory("./temp/");
oms_newModel("model");
oms_addSystem("model.root", OpenModelica.Scripting.oms_system.oms_system_sc);
// instantiate FMUs
oms_addSubModel("model.root.system1", "FMUs/System1.fmu");
oms_addSubModel("model.root.system2", "FMUs/System2.fmu");
// add connections
oms_addConnection("model.root.system1.y", "model.root.system2.u");
oms_addConnection("model.root.system2.y", "model.root.system1.u");
// simulation settings
oms_setResultFile("model", "results.mat");
oms_setStopTime("model", 0.1);
oms_setFixedStepSize("model.root", 1e-4);
oms_instantiate("model");
oms_setReal("model.root.system1.x_start", 2.5);
oms_initialize("model");
oms_simulate("model");
oms_terminate("model");
oms_delete("model");
unloadOMSimulator();
6.2. OpenModelica Scripting Commands¶
6.2.2. addConnection¶
Adds a new connection between connectors A and B. The connectors need to be specified as fully qualified component references, e.g., “model.system.component.signal”.
status := oms_addConnection(crefA, crefB, suppressUnitConversion);
The two arguments crefA and crefB get swapped automatically if necessary. The third argument suppressUnitConversion is optional and the default value is false which allows automatic unit conversion between connections, if set to true then automatic unit conversion will be disabled.
6.2.3. addConnector¶
Adds a connector to a given component.
status := oms_addConnector(cref, causality, type);
The second argument "causality", should be any of the following,
"OpenModelica.Scripting.oms_causality.oms_causality_input"
"OpenModelica.Scripting.oms_causality.oms_causality_output"
"OpenModelica.Scripting.oms_causality.oms_causality_parameter"
"OpenModelica.Scripting.oms_causality.oms_causality_bidir"
"OpenModelica.Scripting.oms_causality.oms_causality_undefined"
The third argument type, should be any of the following,
"OpenModelica.Scripting.oms_signal_type.oms_signal_type_real"
"OpenModelica.Scripting.oms_signal_type.oms_signal_type_integer"
"OpenModelica.Scripting.oms_signal_type.oms_signal_type_boolean"
"OpenModelica.Scripting.oms_signal_type.oms_signal_type_string"
"OpenModelica.Scripting.oms_signal_type.oms_signal_type_enum"
"OpenModelica.Scripting.oms_signal_type.oms_signal_type_bus"
6.2.4. addConnectorToBus¶
Adds a connector to a bus.
status := oms_addConnectorToBus(busCref, connectorCref);
6.2.5. addConnectorToTLMBus¶
Adds a connector to a TLM bus.
status := oms_addConnectorToTLMBus(busCref, connectorCref, type);
6.2.6. addExternalModel¶
Adds an external model to a TLM system.
status := oms_addExternalModel(cref, path, startscript);
6.2.7. addSignalsToResults¶
Add all variables that match the given regex to the result file.
status := oms_addSignalsToResults(cref, regex);
The second argument, i.e. regex, is considered as a regular expression (C++11). “.*” and “(.)*” can be used to hit all variables.
6.2.9. addSystem¶
Adds a (sub-)system to a model or system.
status := oms_addSystem(cref, type);
The second argument type, should be any of the following,
"OpenModelica.Scripting.oms_system.oms_system_none"
"OpenModelica.Scripting.oms_system.oms_system_tlm"
"OpenModelica.Scripting.oms_system.oms_system_sc"
"OpenModelica.Scripting.oms_system.oms_system_wc"
6.2.10. addTLMBus¶
Adds a TLM bus.
status := oms_addTLMBus(cref, domain, dimensions, interpolation);
The second argument "domain", should be any of the following,
"OpenModelica.Scripting.oms_tlm_domain.oms_tlm_domain_input"
"OpenModelica.Scripting.oms_tlm_domain.oms_tlm_domain_output"
"OpenModelica.Scripting.oms_tlm_domain.oms_tlm_domain_mechanical"
"OpenModelica.Scripting.oms_tlm_domain.oms_tlm_domain_rotational"
"OpenModelica.Scripting.oms_tlm_domain.oms_tlm_domain_hydraulic"
"OpenModelica.Scripting.oms_tlm_domain.oms_tlm_domain_electric"
The fourth argument "interpolation", should be any of the following,
"OpenModelica.Scripting.oms_tlm_interpolation.oms_tlm_no_interpolation"
"OpenModelica.Scripting.oms_tlm_interpolation.oms_tlm_coarse_grained"
"OpenModelica.Scripting.oms_tlm_interpolation.oms_tlm_fine_grained"
6.2.11. addTLMConnection¶
Connects two TLM connectors.
status := oms_addTLMConnection(crefA, crefB, delay, alpha, linearimpedance, angularimpedance);
6.2.12. compareSimulationResults¶
This function compares a given signal of two result files within absolute and relative tolerances.
status := oms_compareSimulationResults(filenameA, filenameB, var, relTol, absTol);
The following table describes the input values:
Input | Type | Description |
---|---|---|
filenameA | String | Name of first result file to compare. |
filenameB | String | Name of second result file to compare. |
var | String | Name of signal to compare. |
relTol | Number | Relative tolerance. |
absTol | Number | Absolute tolerance. |
The following table describes the return values:
Type | Description |
---|---|
Integer | 1 if the signal is considered as equal, 0 otherwise |
6.2.14. delete¶
Deletes a connector, component, system, or model object.
status := oms_delete(cref);
6.2.15. deleteConnection¶
Deletes the connection between connectors crefA and crefB.
status := oms_deleteConnection(crefA, crefB);
The two arguments crefA and crefB get swapped automatically if necessary.
6.2.16. deleteConnectorFromBus¶
Deletes a connector from a given bus.
status := oms_deleteConnectorFromBus(busCref, connectorCref);
6.2.17. deleteConnectorFromTLMBus¶
Deletes a connector from a given TLM bus.
status := oms_deleteConnectorFromTLMBus(busCref, connectorCref);
6.2.19. exportDependencyGraphs¶
Export the dependency graphs of a given model to dot files.
status := oms_exportDependencyGraphs(cref, initialization, event, simulation);
6.2.20. exportSnapshot¶
Lists the SSD representation of a given model, system, or component.
Memory is allocated for contents. The caller is responsible to free it using the C-API. The Lua and Python bindings take care of the memory and the caller doesn’t need to call free.
(contents, status) := oms_exportSnapshot(cref);
6.2.21. extractFMIKind¶
Extracts the FMI kind of a given FMU from the file system.
(kind,status) := oms_extractFMIKind(filename);
6.2.22. faultInjection¶
Defines a new fault injection block.
status := oms_faultInjection(cref, type, value);
The second argument type, can be any of the following described below
"OpenModelica.Scripting.oms_fault_type.oms_fault_type_bias"
"OpenModelica.Scripting.oms_fault_type.oms_fault_type_gain"
"OpenModelica.Scripting.oms_fault_type.oms_fault_type_const"
type | Description” |
---|---|
oms_fault_type_bias | y = y.$original + faultValue |
oms_fault_type_gain | y = y.$original * faultValue |
oms_fault_type_const | y = faultValue |
6.2.23. freeMemory¶
Free the memory allocated by some other API. Pass the object for which memory is allocated.
This function is not needed for OpenModelicaScripting Interface
6.2.25. getFixedStepSize¶
Gets the fixed step size. Can be used for the communication step size of co-simulation systems and also for the integrator step size in model exchange systems.
(stepSize, status) := oms_setFixedStepSize(cref);
6.2.27. getModelState¶
Gets the model state of the given model cref.
(modelState, status) := oms_getModelState(cref);
6.2.29. getSolver¶
Gets the selected solver method of the given system.
(solver, status) := oms_getSolver(cref);
6.2.30. getStartTime¶
Get the start time from the model.
(startTime, status) := oms_getStartTime(cref);
6.2.32. getSubModelPath¶
Returns the path of a given component.
(path, status) := oms_getSubModelPath(cref);
6.2.33. getSystemType¶
Gets the type of the given system.
(type, status) := oms_getSystemType(cref);
6.2.34. getTime¶
Get the current simulation time from the model.
(time, status) := oms_getTime(cref);
6.2.35. getTolerance¶
Gets the tolerance of a given system or component.
(absoluteTolerance, relativeTolerance, status) := oms_getTolerance(cref);
6.2.36. getVariableStepSize¶
Gets the step size parameters.
(initialStepSize, minimumStepSize, maximumStepSize, status) := oms_getVariableStepSize(cref);
6.2.38. importFile¶
Imports a composite model from a SSP file.
(cref, status) := oms_importFile(filename);
6.2.39. importSnapshot¶
Loads a snapshot to restore a previous model state. The model must be in virgin model state, which means it must not be instantiated.
status := oms_importSnapshot(cref, snapshot);
6.2.42. list¶
Lists the SSD representation of a given model, system, or component.
Memory is allocated for contents. The caller is responsible to free it using the C-API. The Lua and Python bindings take care of the memory and the caller doesn’t need to call free.
(contents, status) := oms_list(cref);
6.2.43. listUnconnectedConnectors¶
Lists all unconnected connectors of a given system.
Memory is allocated for contents. The caller is responsible to free it using the C-API. The Lua and Python bindings take care of the memory and the caller doesn’t need to call free.
(contents, status) := oms_listUnconnectedConnectors(cref);
6.2.44. loadSnapshot¶
Loads a snapshot to restore a previous model state. The model must be in virgin model state, which means it must not be instantiated.
status := oms_loadSnapshot(cref, snapshot);
6.2.46. removeSignalsFromResults¶
Removes all variables that match the given regex to the result file.
status := oms_removeSignalsFromResults(cref, regex);
The second argument, i.e. regex, is considered as a regular expression (C++11). “.*” and “(.)*” can be used to hit all variables.
6.2.48. reset¶
Reset the composite model after a simulation run.
The FMUs go into the same state as after instantiation.
status := oms_reset(cref);
6.2.49. setBoolean¶
Sets the value of a given boolean signal.
status := oms_setBoolean(cref, value);
6.2.50. setCommandLineOption¶
Sets special flags.
status := oms_setCommandLineOption(cmd);
Available flags:
info: Usage: OMSimulator [Options] [Lua script] [FMU] [SSP file]
Options:
--addParametersToCSV=<arg> Export parameters to .csv file (true, [false])
--algLoopSolver=<arg> Specifies the alg. loop solver method (fixedpoint, [kinsol]) used for algebraic loops spanning over multiple components.
--clearAllOptions Reset all flags to default values
--deleteTempFiles=<bool> Deletes temp files as soon as they are no longer needed ([true], false)
--directionalDerivatives=<bool> Specifies whether directional derivatives should be used to calculate the Jacobian for alg. loops or if a numerical approximation should be used instead ([true], false)
--dumpAlgLoops=<bool> Dump information for alg loops (true, [false])
--emitEvents=<bool> Specifies whether events should be emitted or not ([true], false)
--fetchAllVars=<arg> Workaround for certain FMUs that do not update all internal dependencies automatically
--help [-h] Displays the help text
--ignoreInitialUnknowns=<bool> Ignore the initial unknowns from the modelDescription.xml (true, [false])
--inputExtrapolation=<bool> Enables input extrapolation using derivative information (true, [false])
--intervals=<int> [-i] Specifies the number of communication points (arg > 1)
--logFile=<arg> [-l] Specifies the logfile (stdout is used if no log file is specified)
--logLevel=<int> 0 default, 1 debug, 2 debug+trace
--maxEventIteration=<int> Specifies the max. number of iterations for handling a single event
--maxLoopIteration=<int> Specifies the max. number of iterations for solving algebraic loops between system-level components. Internal algebraic loops of components are not affected.
--mode=<arg> [-m] Forces a certain FMI mode iff the FMU provides cs and me (cs, [me])
--numProcs=<int> [-n] Specifies the max. number of processors to use (0=auto, 1=default)
--progressBar=<bool> Shows a progress bar for the simulation progress in the terminal (true, [false])
--realTime=<bool> Experimental feature for (soft) real-time co-simulation (true, [false])
--resultFile=<arg> [-r] Specifies the name of the output result file
--skipCSVHeader=<arg> Skip exporting the scv delimiter in the header ([true], false),
--solver=<arg> Specifies the integration method (euler, [cvode])
--solverStats=<bool> Adds solver stats to the result file, e.g. step size; not supported for all solvers (true, [false])
--startTime=<double> [-s] Specifies the start time
--stepSize=<arg> Specifies the step size (<step size> or <init step,min step,max step>)
--stopTime=<double> [-t] Specifies the stop time
--stripRoot=<bool> Removes the root system prefix from all exported signals (true, [false])
--suppressPath=<bool> Supresses path information in info messages; especially useful for testing ([true], false)
--tempDir=<arg> Specifies the temp directory
--timeout=<int> Specifies the maximum allowed time in seconds for running a simulation (0 disables)
--tolerance=<double> Specifies the relative tolerance
--version [-v] Displays version information
--wallTime=<bool> Add wall time information for to the result file (true, [false])
--workingDir=<arg> Specifies the working directory
--zeroNominal=<bool> Using this flag, FMUs with invalid nominal values will be accepted and the invalid nominal values will be replaced with 1.0
6.2.51. setFixedStepSize¶
Sets the fixed step size. Can be used for the communication step size of co-simulation systems and also for the integrator step size in model exchange systems.
status := oms_setFixedStepSize(cref, stepSize);
6.2.52. setInteger¶
Sets the value of a given integer signal.
status := oms_setInteger(cref, value);
6.2.53. setLogFile¶
Redirects logging output to file or std streams. The warning/error counters are reset.
filename=”” to redirect to std streams and proper filename to redirect to file.
status := oms_setLogFile(filename);
6.2.54. setLoggingInterval¶
Set the logging interval of the simulation.
status := oms_setLoggingInterval(cref, loggingInterval);
6.2.55. setLoggingLevel¶
Enables/Disables debug logging (logDebug and logTrace).
0 default, 1 default+debug, 2 default+debug+trace
oms_setLoggingLevel(logLevel);
6.2.56. setReal¶
Sets the value of a given real signal.
status := oms_setReal(cref, value);
This function can be called in different model states:
- Before instantiation: setReal can be used to set start values or to define initial unknowns (e.g. parameters, states). The values are not immediately applied to the simulation unit, since it isn’t actually instantiated.
- After instantiation and before initialization: Same as before instantiation, but the values are applied immediately to the simulation unit.
- After initialization: Can be used to force external inputs, which might cause discrete changes of continuous signals.
6.2.57. setRealInputDerivative¶
Sets the first order derivative of a real input signal.
This can only be used for CS-FMU real input signals.
status := oms_setRealInputDerivative(cref, value);
6.2.58. setResultFile¶
Set the result file of the simulation.
status := oms_setResultFile(cref, filename);
status := oms_setResultFile(cref, filename, bufferSize);
The creation of a result file is omitted if the filename is an empty string.
6.2.59. setSolver¶
Sets the solver method for the given system.
status := oms_setSolver(cref, solver);
The second argument "solver" should be any of the following,
"OpenModelica.Scripting.oms_solver.oms_solver_none"
"OpenModelica.Scripting.oms_solver.oms_solver_sc_min"
"OpenModelica.Scripting.oms_solver.oms_solver_sc_explicit_euler"
"OpenModelica.Scripting.oms_solver.oms_solver_sc_cvode"
"OpenModelica.Scripting.oms_solver.oms_solver_sc_max"
"OpenModelica.Scripting.oms_solver.oms_solver_wc_min"
"OpenModelica.Scripting.oms_solver.oms_solver_wc_ma"
"OpenModelica.Scripting.oms_solver.oms_solver_wc_mav"
"OpenModelica.Scripting.oms_solver.oms_solver_wc_assc"
"OpenModelica.Scripting.oms_solver.oms_solver_wc_mav2"
"OpenModelica.Scripting.oms_solver.oms_solver_wc_max"
6.2.60. setStartTime¶
Set the start time of the simulation.
status := oms_setStartTime(cref, startTime);
6.2.61. setStopTime¶
Set the stop time of the simulation.
status := oms_setStopTime(cref, stopTime);
6.2.62. setTLMPositionAndOrientation¶
Sets initial position and orientation for a TLM 3D interface.
status := oms_setTLMPositionAndOrientation(cref, x1, x2, x3, A11, A12, A13, A21, A22, A23, A31, A32, A33);
6.2.63. setTLMSocketData¶
Sets data for TLM socket communication.
status := oms_setTLMSocketData(cref, address, managerPort, monitorPort);
6.2.65. setTolerance¶
Sets the tolerance for a given model or system.
status := oms_setTolerance(const char* cref, double tolerance);
status := oms_setTolerance(const char* cref, double absoluteTolerance, double relativeTolerance);
Default values are 1e-4 for both relative and absolute tolerances.
A tolerance specified for a model is automatically applied to its root system, i.e. both calls do exactly the same:
oms_setTolerance("model", absoluteTolerance, relativeTolerance);
oms_setTolerance("model.root", absoluteTolerance, relativeTolerance);
Component, e.g. FMUs, pick up the tolerances from there system. That means it is not possible to define different tolerances for FMUs in the same system right now.
In a strongly coupled system (oms_system_sc), the relative tolerance is used for CVODE and the absolute tolerance is used to solve algebraic loops.
In a weakly coupled system (oms_system_wc), both the relative and absolute tolerances are used for the adaptive step master algorithms and the absolute tolerance is used to solve algebraic loops.
6.2.66. setVariableStepSize¶
Sets the step size parameters for methods with stepsize control.
status := oms_getVariableStepSize(cref, initialStepSize, minimumStepSize, maximumStepSize);
6.2.67. setWorkingDirectory¶
Set a new working directory.
status := oms_setWorkingDirectory(newWorkingDir);
6.2.69. stepUntil¶
Simulates a composite model until a given time value.
status := oms_stepUntil(cref, stopTime);