Skip to content

Commit

Permalink
- C++ Target and SimulationRuntime: add return value to "update" and …
Browse files Browse the repository at this point in the history
…"handleSystemEvents" which indicates if an state variable had been reinitialized.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@16735 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Matthias Arzt committed Aug 5, 2013
1 parent a34bf4f commit 30bbe40
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 38 deletions.
41 changes: 23 additions & 18 deletions Compiler/Template/CodegenCpp.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,7 @@ template Update(SimCode simCode)
match simCode
case SIMCODE(__) then
<<
<%update(allEquations,whenClauses,simCode,contextOther)%>
<%update(allEquations,whenClauses,simCode,contextOther)%>
>>
end Update;
/*<%update(odeEquations,algebraicEquations,whenClauses,parameterEquations,simCode)%>*/
Expand Down Expand Up @@ -2433,7 +2433,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
//Set variables with given index to the system
virtual void setVars(const double* z);
//Update transfer behavior of the system of equations according to command given by solver
virtual void update(const UPDATE command =IContinuous::UNDEF_UPDATE);
virtual bool update(const UPDATE command =IContinuous::UNDEF_UPDATE);
//Provide the right hand side (according to the index)
virtual void giveRHS(double* f);
//Output routine (to be called by the solver after every successful integration step)
Expand All @@ -2459,7 +2459,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
//Called to check conditions for event-handling
virtual bool checkConditions(const bool* events, bool all);
//Called to handle all events occured at same time
virtual void handleSystemEvents( bool* events);
virtual bool handleSystemEvents( bool* events);
//Called to handle an event
virtual void handleEvent(const bool* events);
//Checks if a discrete variable has changed and triggers an event
Expand Down Expand Up @@ -6359,26 +6359,28 @@ template handleSystemEvents(list<ZeroCrossing> zeroCrossings,list<SimWhenClause>
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
<<
void <%lastIdentOfPath(modelInfo.name)%>::handleSystemEvents(bool* events)
{
bool <%lastIdentOfPath(modelInfo.name)%>::handleSystemEvents(bool* events)
{
bool restart=true;
bool state_vars_reinitialized = false;
int iter=0;
checkConditions(events,false);
while(restart && !(iter++ > 2*_dimZeroFunc))
{
bool st_vars_reinit = false;
//iterate and handle all events inside the eventqueue
saveAll();
restart=_event_handling.IterateEventQueue(_conditions);
restart=_event_handling.IterateEventQueue(st_vars_reinit);
state_vars_reinitialized = state_vars_reinitialized || st_vars_reinit;
}
saveAll();
resetTimeEvents();
if(iter>2*_dimZeroFunc && restart ){
throw std::runtime_error("Number of event iteration steps exceeded. " );}
}
saveAll();
}
resetTimeEvents();
if(iter>2*_dimZeroFunc && restart )
throw std::runtime_error("Number of event iteration steps exceeded. " );
return state_vars_reinitialized;
}
>>
end handleSystemEvents;

Expand Down Expand Up @@ -6718,13 +6720,15 @@ template update( list<SimEqSystem> allEquationsPlusWhen,list<SimWhenClause> when
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
<<
void <%lastIdentOfPath(modelInfo.name)%>::update(const UPDATE command)
bool <%lastIdentOfPath(modelInfo.name)%>::update(const UPDATE command)
{
bool state_var_reinitialized = false;
<%varDecls%>
if(command & IContinuous::RANKING) checkConditions(0,true);
<%all_equations%>
<%reinit%>
return state_var_reinitialized;
}
>>
end update;
Expand Down Expand Up @@ -6762,6 +6766,7 @@ template functionWhenReinitStatementThen(list<WhenOperator> reinits, Text &varDe
let &preExp = buffer "" /*BUFD*/
let val = daeExp(value, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
<<
state_var_reinitialized = true;
<%preExp%>
<%cref1(stateVar,simCode,contextOther)%> = <%val%>;
>>
Expand All @@ -6776,7 +6781,7 @@ template functionWhenReinitStatementThen(list<WhenOperator> reinits, Text &varDe
assertCommon(condition, message, contextSimulationDiscrete, &varDecls, info,simCode)
;separator="\n")
<<
<%body%>
<%body%>
>>
end functionWhenReinitStatementThen;

Expand Down
6 changes: 3 additions & 3 deletions SimulationRuntime/cpp/Core/Modelica/ModelicaSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ void Modelica::resetTimeEvents()
{
}

void Modelica::update(const UPDATE command)
bool Modelica::update(const UPDATE command)
{

return false;
}

void Modelica::writeOutput(const OUTPUT command)
Expand Down Expand Up @@ -112,7 +112,7 @@ void Modelica::handleEvent(const bool* events)

}

void Modelica::handleSystemEvents( bool* events)
bool Modelica::handleSystemEvents( bool* events)
{

}
Expand Down
4 changes: 2 additions & 2 deletions SimulationRuntime/cpp/Core/Modelica/ModelicaSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Modelica: public IMixedSystem ,public IContinuous ,public IEvent ,public I
virtual void setVars(const double* z);

// Update transfer behavior of the system of equations according to command given by solver
virtual void update(const UPDATE command =IContinuous::UNDEF_UPDATE);
virtual bool update(const UPDATE command =IContinuous::UNDEF_UPDATE);

// Provide the right hand side (according to the index)
virtual void giveRHS(double* f);
Expand Down Expand Up @@ -86,7 +86,7 @@ class Modelica: public IMixedSystem ,public IContinuous ,public IEvent ,public I
virtual void giveZeroFunc(double* f);

//Called to handle all events occured at same time
virtual void handleSystemEvents( bool* events);
virtual bool handleSystemEvents( bool* events);
virtual bool checkConditions(const bool* events, bool all);
//Called to handle an event
virtual void handleEvent(const bool* events);
Expand Down
12 changes: 4 additions & 8 deletions SimulationRuntime/cpp/Core/System/EventHandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,27 +137,23 @@ double EventHandling::sample(double start,double interval)
/**
Handles all events occured a the same time. These are stored the eventqueue
*/
bool EventHandling::IterateEventQueue(bool* conditions)
bool EventHandling::IterateEventQueue(bool& state_vars_reinitialized)
{
IContinuous* countinous_system = dynamic_cast<IContinuous*>(_system);
IEvent* event_system= dynamic_cast<IEvent*>(_system);
IMixedSystem* mixed_system= dynamic_cast<IMixedSystem*>(_system);

bool drestart=false;
bool crestart=true;


//save discrete varibales
event_system->saveDiscreteVars(); // store values of discrete vars vor next check

//Handle all events
countinous_system->update();
state_vars_reinitialized = countinous_system->update();

//check if discrete variables changed
drestart= event_system->checkForDiscreteEvents();
bool drestart= event_system->checkForDiscreteEvents();

//update all conditions
crestart=event_system->checkConditions(0,true);
bool crestart=event_system->checkConditions(0,true);


return((drestart||crestart)); //returns true if new events occured
Expand Down
6 changes: 3 additions & 3 deletions SimulationRuntime/cpp/Include/Core/System/EventHandling.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class BOOST_EXTENSION_EVENTHANDLING_DECL EventHandling
//removes an event from the eventqueue
void removeEvent(long index);
//Handles all events occured a the same time. Returns true if a second event iteration is needed
bool IterateEventQueue(bool* events);
bool IterateEventQueue(bool& state_vars_reinitialized);

void saveDiscreteVar(double var,string key); // I guess, this was used in a wrong way. it might be obsolete.
bool changeDiscreteVar(double var,string key); // I guess, this was used in a wrong way. it might be obsolete.
void saveDiscreteVar(double var,string key);
bool changeDiscreteVar(double var,string key);
void addTimeEvent(long index,double time);
void addTimeEvents( event_times_type times);
event_times_type makePeriodeEvents(double ts,double te,double ti,long index);
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/cpp/Include/Core/System/IContinuous.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class IContinuous
virtual void setVars(const double* z) = 0;

/// Update transfer behavior of the system of equations according to command given by solver
virtual void update(const UPDATE command = UNDEF_UPDATE) = 0;
virtual bool update(const UPDATE command = UNDEF_UPDATE) = 0;

/// Provide the right hand side (according to the index)
virtual void giveRHS(double* f) = 0;
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/cpp/Include/Core/System/IMixedSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class IMixedSystem
virtual void giveConstraint(SparseMatrix matrix) = 0;
virtual IHistory* getHistory()=0;
/// Called to handle all events occured at same time
virtual void handleSystemEvents(bool* events) = 0;
virtual bool handleSystemEvents(bool* events) = 0;
//Saves all variables before an event is handled, is needed for the pre, edge and change operator
virtual void saveAll() = 0;

Expand Down
4 changes: 2 additions & 2 deletions SimulationRuntime/cpp/Include/FMU/FMUWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ fmiStatus FMUWrapper::eventUpdate(fmiBoolean intermediateResults,
for(int i=0; i<NUMBER_OF_EVENT_INDICATORS; i++)
events[i] = f[i] >= 0;
// Handle Zero Crossings if nessesary
_model->handleSystemEvents(events);
bool state_vars_reinitialized = _model->handleSystemEvents(events);
// everything is done
eventInfo.iterationConverged = fmiTrue;
eventInfo.stateValueReferencesChanged = fmiFalse; // will never change for open Modelica Models
eventInfo.stateValuesChanged = fmiFalse; // TODO
eventInfo.stateValuesChanged = state_vars_reinitialized; // TODO
eventInfo.terminateSimulation = fmiFalse;
eventInfo.upcomingTimeEvent = fmiFalse;
//eventInfo.nextEventTime = _time;
Expand Down

0 comments on commit 30bbe40

Please sign in to comment.