Skip to content

Commit

Permalink
New api: oms_doStep (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel committed Feb 9, 2021
1 parent ece95bf commit f4e5ae1
Show file tree
Hide file tree
Showing 14 changed files with 760 additions and 654 deletions.
39 changes: 39 additions & 0 deletions doc/UsersGuide/source/api/doStep.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#CAPTION#
doStep
------

Simulates a macro step of the given composite model. The step size
will be determined by the master algorithm and is limited by the
definied minimal and maximal step sizes.
#END#

#LUA#
.. code-block:: lua
-- not available
#END#

#PYTHON#
.. code-block:: python
status = oms.doStep(cref)
#END#

#CAPI#
.. code-block:: c
oms_status_enu_t oms_doStep(const char* cref);
#END#

#OMC#
.. code-block:: Modelica
// not available
#END#

#DESCRIPTION#
#END#
77 changes: 38 additions & 39 deletions src/OMSimulatorLib/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,45 +116,44 @@ class Log
#define logTrace() ((void)0)
#endif

// Common error messages
#define logError_AlreadyInScope(cref) logError("\"" + std::string(cref) + "\" already exists in the scope")
#define logError_AlreadyInScope(cref) logError("\"" + std::string(cref) + "\" already exists in the scope")
#define logError_BusAndConnectorNotSameModel(bus, connector) logError("Bus \"" + std::string(bus) + "\" and connector \"" + std::string(connector) + "\" do not belong to same model")
#define logError_BusAndConnectorNotSameSystem(bus, connector) logError("Bus \"" + std::string(bus) + "\" and connector \"" + std::string(connector) + "\" do not belong to same system")
#define logError_BusNotInComponent(cref, component) logError("Bus connector \"" + std::string(cref) + "\" not found in component \"" + std::string(component->getFullCref()) + "\"")
#define logError_BusNotInSystem(cref, system) logError("Bus connector \"" + std::string(cref) + "\" not found in system \"" + std::string(system->getFullCref()) + "\"")
#define logError_ComponentNotInSystem(system, component) logError("System \"" + std::string(system->getFullCref()) + "\" does not contain component \"" + std::string(component) + "\"")
// common error messages
#define logError_AlreadyInScope(cref) logError("\"" + std::string(cref) + "\" already exists in the scope")
#define logError_BusAndConnectorNotSameModel(bus, connector) logError("Bus \"" + std::string(bus) + "\" and connector \"" + std::string(connector) + "\" do not belong to same model")
#define logError_BusAndConnectorNotSameSystem(bus, connector) logError("Bus \"" + std::string(bus) + "\" and connector \"" + std::string(connector) + "\" do not belong to same system")
#define logError_BusNotInComponent(cref, component) logError("Bus connector \"" + std::string(cref) + "\" not found in component \"" + std::string(component->getFullCref()) + "\"")
#define logError_BusNotInSystem(cref, system) logError("Bus connector \"" + std::string(cref) + "\" not found in system \"" + std::string(system->getFullCref()) + "\"")
#define logError_ComponentNotInSystem(system, component) logError("System \"" + std::string(system->getFullCref()) + "\" does not contain component \"" + std::string(component) + "\"")
#define logError_ConnectionExistsAlready(crefA, crefB, system) logError("Connection <\"" + std::string(crefA) + "\", \"" + std::string(crefB) + "\"> exists already in system \"" + std::string(system->getFullCref()) + "\"")
#define logError_ConnectionNotInSystem(crefA, crefB, system) logError("Connection <\"" + std::string(crefA) + "\", \"" + std::string(crefB) + "\"> not found in system \"" + std::string(system->getFullCref()) + "\"")
#define logError_ConnectorNotInComponent(cref, component) logError("Connector \"" + std::string(cref) + "\" not found in component \"" + std::string(component->getFullCref()) + "\"")
#define logError_ConnectorNotInSystem(cref, system) logError("Connector \"" + std::string(cref) + "\" not found in system \"" + std::string(system->getFullCref()) + "\"")
#define logError_FMUCall(call, fmu) logError(std::string(call) + " failed for FMU \"" + std::string(fmu->getFullCref()) + "\"")
#define logError_Initialization(system) logError("Initialization of system \"" + std::string(system) + "\" failed")
#define logError_InternalError logError("internal error")
#define logError_InvalidIdent(cref) logError("\"" + std::string(cref) + "\" is not a valid ident")
#define logError_InvalidIdent(cref) logError("\"" + std::string(cref) + "\" is not a valid ident")
#define logError_ModelInWrongState(model) logError("Model \"" + std::string(model->getCref()) + "\" is in wrong model state")
#define logError_ModelNotInScope(cref) logError("Model \"" + std::string(cref) + "\" does not exist in the scope")
#define logError_NoConnectorsInTLMBus(cref) logError("No connectors in TLM bus: \"" + std::string(cref) + "\"")
#define logError_NotForExternalModels logError("Not available for external models")
#define logError_NotForScSystem logError("Not available for strongly coupled systems")
#define logError_NotForTlmSystem logError("Not available for TLM systems")
#define logError_NotImplemented logError("Not implemented")
#define logError_OnlyForExternalModels logError("Only available for TLM sub models (aka external models)")
#define logError_OnlyForModel logError("Only implemented for model identifiers")
#define logError_OnlyForRealInputs(cref) logError("Signal \"" + std::string(cref) + "\" is not a real input signal")
#define logError_OnlyForSystemTLM logError("Only available for TLM systems")
#define logError_OnlyForSystemWC logError("Only available for WC systems")
#define logError_ResetFailed(system) logError("failed to reset system \"" + std::string(system) + "\" to instantiation mode")
#define logError_SubSystemNotInSystem(system, subsystem) logError("System \"" + std::string(system) + "\" does not contain subsystem \"" + std::string(subsystem) + "\"")
#define logError_SystemNotInModel(model, system) logError("Model \"" + std::string(model) + "\" does not contain system \"" + std::string(system) + "\"")
#define logError_Termination(system) logError("Termination of system \"" + std::string(system) + "\" failed")
#define logError_TlmBusNotInComponent(cref, component) logError("TLM bus connector \"" + std::string(cref) + "\" not found in component \"" + std::string(component->getFullCref()) + "\"")
#define logError_TlmBusNotInSystem(cref, system) logError("TLM bus connector \"" + std::string(cref) + "\" not found in system \"" + std::string(system->getFullCref()) + "\"")
#define logError_UnknownSignal(cref) logError("Unknown signal \"" + std::string(cref) + "\"")
#define logError_UnknownTLMVariableType(vartype) logError("Unknown TLM variable type: \""+vartype+"\"")
#define logError_VariableTypeAlreadyInTLMBus(cref,vartype) logError("TLM bus connector \"" + std::string(cref) + "\" already contains a variable with type \"" + vartype + "\"")
#define logError_WrongSchema(name) logError("Wrong xml schema detected. Unexpected tag \"" + name + "\"")
#define logWarning_deprecated logWarning("Wrong/deprecated content detected but successfully loaded. Please re-export the SSP file to avoid this message.")
#define logError_ConnectionNotInSystem(crefA, crefB, system) logError("Connection <\"" + std::string(crefA) + "\", \"" + std::string(crefB) + "\"> not found in system \"" + std::string(system->getFullCref()) + "\"")
#define logError_ConnectorNotInComponent(cref, component) logError("Connector \"" + std::string(cref) + "\" not found in component \"" + std::string(component->getFullCref()) + "\"")
#define logError_ConnectorNotInSystem(cref, system) logError("Connector \"" + std::string(cref) + "\" not found in system \"" + std::string(system->getFullCref()) + "\"")
#define logError_FMUCall(call, fmu) logError(std::string(call) + " failed for FMU \"" + std::string(fmu->getFullCref()) + "\"")
#define logError_Initialization(system) logError("Initialization of system \"" + std::string(system) + "\" failed")
#define logError_InternalError logError("internal error")
#define logError_InvalidIdent(cref) logError("\"" + std::string(cref) + "\" is not a valid ident")
#define logError_InvalidIdent(cref) logError("\"" + std::string(cref) + "\" is not a valid ident")
#define logError_ModelInWrongState(model) logError("Model \"" + std::string(model->getCref()) + "\" is in wrong model state")
#define logError_ModelNotInScope(cref) logError("Model \"" + std::string(cref) + "\" does not exist in the scope")
#define logError_NoConnectorsInTLMBus(cref) logError("No connectors in TLM bus: \"" + std::string(cref) + "\"")
#define logError_NotForExternalModels logError("Not available for external models")
#define logError_NotForScSystem logError("Not available for strongly coupled systems")
#define logError_NotForTlmSystem logError("Not available for TLM systems")
#define logError_NotImplemented logError("Not implemented")
#define logError_OnlyForExternalModels logError("Only available for TLM sub models (aka external models)")
#define logError_OnlyForModel logError("Only implemented for model identifiers")
#define logError_OnlyForRealInputs(cref) logError("Signal \"" + std::string(cref) + "\" is not a real input signal")
#define logError_OnlyForSystemTLM logError("Only available for TLM systems")
#define logError_OnlyForSystemWC logError("Only available for WC systems")
#define logError_ResetFailed(system) logError("failed to reset system \"" + std::string(system) + "\" to instantiation mode")
#define logError_SubSystemNotInSystem(system, subsystem) logError("System \"" + std::string(system) + "\" does not contain subsystem \"" + std::string(subsystem) + "\"")
#define logError_SystemNotInModel(model, system) logError("Model \"" + std::string(model) + "\" does not contain system \"" + std::string(system) + "\"")
#define logError_Termination(system) logError("Termination of system \"" + std::string(system) + "\" failed")
#define logError_TlmBusNotInComponent(cref, component) logError("TLM bus connector \"" + std::string(cref) + "\" not found in component \"" + std::string(component->getFullCref()) + "\"")
#define logError_TlmBusNotInSystem(cref, system) logError("TLM bus connector \"" + std::string(cref) + "\" not found in system \"" + std::string(system->getFullCref()) + "\"")
#define logError_UnknownSignal(cref) logError("Unknown signal \"" + std::string(cref) + "\"")
#define logError_UnknownTLMVariableType(vartype) logError("Unknown TLM variable type: \""+vartype+"\"")
#define logError_VariableTypeAlreadyInTLMBus(cref,vartype) logError("TLM bus connector \"" + std::string(cref) + "\" already contains a variable with type \"" + vartype + "\"")
#define logError_WrongSchema(name) logError("Wrong xml schema detected. Unexpected tag \"" + name + "\"")
#define logWarning_deprecated logWarning("Wrong/deprecated content detected but successfully loaded. Please re-export the SSP file to avoid this message.")

#endif
20 changes: 20 additions & 0 deletions src/OMSimulatorLib/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,26 @@ oms_status_enu_t oms::Model::simulate()
return status;
}

oms_status_enu_t oms::Model::doStep()
{
clock.tic();
if (!validState(oms_modelState_simulation))
{
clock.toc();
return logError_ModelInWrongState(this);
}

if (!system)
{
clock.toc();
return logError("Model doesn't contain a system");
}

oms_status_enu_t status = system->doStep();
clock.toc();
return status;
}

oms_status_enu_t oms::Model::stepUntil(double stopTime)
{
clock.tic();
Expand Down
1 change: 1 addition & 0 deletions src/OMSimulatorLib/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ namespace oms
oms_status_enu_t initialize();
oms_status_enu_t simulate_asynchronous(void (*cb)(const char* cref, double time, oms_status_enu_t status));
oms_status_enu_t simulate();
oms_status_enu_t doStep();
oms_status_enu_t stepUntil(double stopTime);
oms_status_enu_t terminate();
oms_status_enu_t reset();
Expand Down
11 changes: 11 additions & 0 deletions src/OMSimulatorLib/OMSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,17 @@ oms_status_enu_t oms_simulate(const char* cref_)
return model->simulate();
}

oms_status_enu_t oms_doStep(const char* cref_)
{
oms::ComRef cref(cref_);

oms::Model* model = oms::Scope::GetInstance().getModel(cref);
if (!model)
return logError_ModelNotInScope(cref);

return model->doStep();
}

oms_status_enu_t oms_stepUntil(const char* cref_, double stopTime)
{
oms::ComRef cref(cref_);
Expand Down
1 change: 1 addition & 0 deletions src/OMSimulatorLib/OMSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ OMSAPI oms_status_enu_t OMSCALL oms_setVariableStepSize(const char* cref, double
OMSAPI oms_status_enu_t OMSCALL oms_setWorkingDirectory(const char* newWorkingDir);
OMSAPI oms_status_enu_t OMSCALL oms_simulate_asynchronous(const char* cref, void (*cb)(const char* cref, double time, oms_status_enu_t status));
OMSAPI oms_status_enu_t OMSCALL oms_simulate(const char* cref);
OMSAPI oms_status_enu_t OMSCALL oms_doStep(const char* cref);
OMSAPI oms_status_enu_t OMSCALL oms_stepUntil(const char* cref, double stopTime);
OMSAPI oms_status_enu_t OMSCALL oms_terminate(const char* cref);

Expand Down
1 change: 1 addition & 0 deletions src/OMSimulatorLib/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ namespace oms
virtual oms_status_enu_t initialize() = 0;
virtual oms_status_enu_t terminate() = 0;
virtual oms_status_enu_t reset() = 0;
virtual oms_status_enu_t doStep() = 0;
virtual oms_status_enu_t stepUntil(double stopTime, void (*cb)(const char* ident, double time, oms_status_enu_t status)) = 0;

double getTime() const {return time;}
Expand Down

0 comments on commit f4e5ae1

Please sign in to comment.