Skip to content

Commit

Permalink
splitted EventHandling class in DiscreteEvents,ContinuousEvents and P…
Browse files Browse the repository at this point in the history
…reVariables

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@24127 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
niklwors committed Jan 21, 2015
1 parent d649b41 commit dad9896
Show file tree
Hide file tree
Showing 17 changed files with 631 additions and 505 deletions.
397 changes: 246 additions & 151 deletions Compiler/Template/CodegenCpp.tpl

Large diffs are not rendered by default.

Expand Up @@ -41,7 +41,7 @@ void Initialization::initializeSystem()
cond_restart = !std::equal (conditions1, conditions1+dim, conditions0);
}

mixed_system->saveAll();
event_system->saveAll();
_system->setInitial(false);

if( _solver->stateSelection())
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/cpp/Core/SimController/SimManager.cpp
Expand Up @@ -211,7 +211,7 @@ void SimManager::runSingleStep(double cycletime)
//Handle time event
_timeevent_system->handleTimeEvent(_timeeventcounter);
_cont_system->evaluateAll(IContinuous::CONTINUOUS);
_mixed_system->saveAll();
_event_system->saveAll();
_timeevent_system->handleTimeEvent(_timeeventcounter);
}

Expand Down
7 changes: 5 additions & 2 deletions SimulationRuntime/cpp/Core/System/CMakeLists.txt
Expand Up @@ -19,12 +19,12 @@ IF(ANALYZATION_MODE)
set_target_properties(${SystemName} PROPERTIES COMPILE_FLAGS -fPIC)
endif(UNIX)
ELSE(ANALYZATION_MODE)
add_library(${SystemName} SHARED AlgLoopDefaultImplementation.cpp AlgLoopSolverFactory.cpp EventHandling.cpp SystemDefaultImplementation.cpp FactoryExport.cpp)
add_library(${SystemName} SHARED AlgLoopDefaultImplementation.cpp AlgLoopSolverFactory.cpp EventHandling.cpp DiscreteEvents.cpp ContinuousEvents.cpp SystemDefaultImplementation.cpp FactoryExport.cpp)
target_link_libraries (${SystemName} ${Boost_LIBRARIES} ${LAPACK_LIBRARIES} ${OMCFactoryName} )
ENDIF(ANALYZATION_MODE)

# add a static library with the system default implementation for generating c++ fmu targets
add_library(${SystemName}_static STATIC AlgLoopDefaultImplementation.cpp AlgLoopSolverFactory.cpp EventHandling.cpp SystemDefaultImplementation.cpp )
add_library(${SystemName}_static STATIC AlgLoopDefaultImplementation.cpp AlgLoopSolverFactory.cpp EventHandling.cpp DiscreteEvents.cpp ContinuousEvents.cpp SystemDefaultImplementation.cpp )

target_link_libraries (${SystemName}_static ${Boost_LIBRARIES} ${LAPACK_LIBRARIES})
if (UNIX)
Expand All @@ -40,6 +40,9 @@ install (FILES
${CMAKE_SOURCE_DIR}/Include/Core/System/SystemDefaultImplementation.h
${CMAKE_SOURCE_DIR}/Include/Core/System/AlgLoopDefaultImplementation.h
${CMAKE_SOURCE_DIR}/Include/Core/System/EventHandling.h
${CMAKE_SOURCE_DIR}/Include/Core/System/PreVariables.h
${CMAKE_SOURCE_DIR}/Include/Core/System/DiscreteEvents.h
${CMAKE_SOURCE_DIR}/Include/Core/System/ContinuousEvents.h
DESTINATION include/omc/cpp/Core/System)
install (FILES
${CMAKE_SOURCE_DIR}/Include/Core/System/IAlgLoop.h
Expand Down
88 changes: 88 additions & 0 deletions SimulationRuntime/cpp/Core/System/ContinuousEvents.cpp
@@ -0,0 +1,88 @@
#include <Core/Modelica.h>
#include "FactoryExport.h"
#include <Core/System/IEvent.h>
#include <Core/System/ContinuousEvents.h>
#include <Core/Math/Functions.h>


ContinuousEvents::ContinuousEvents() : _countinous_system(NULL), _mixed_system(NULL), _conditions0(NULL), _conditions1(NULL)
{
}

ContinuousEvents::~ContinuousEvents(void)
{

if(_conditions0)
delete[] _conditions0;
if(_conditions1)
delete[] _conditions1;
}

/**
Inits the event variables
*/
void ContinuousEvents::initialize(IEvent* system)
{
// _dimH=dim;
_event_system=system;
_countinous_system = dynamic_cast<IContinuous*>(_event_system);
_mixed_system= dynamic_cast<IMixedSystem*>(_event_system);


if(_conditions0)
delete[] _conditions0;
if(_conditions1)
delete[] _conditions1;

_conditions0 = new bool[_event_system->getDimZeroFunc()];
_conditions1 = new bool[_event_system->getDimZeroFunc()];
}



/**
Handles all events occurred a the same time.
*/
bool ContinuousEvents::startEventIteration(bool& state_vars_reinitialized)
{
//save discrete variables
//Deactivated: _event_system->saveDiscreteVars(); // store values of discrete vars vor next check

unsigned int dim = _event_system->getDimZeroFunc();

_event_system->getConditions(_conditions0);
//Handle all events

state_vars_reinitialized = _countinous_system->evaluateConditions();


//check if discrete variables changed
bool drestart= _event_system->checkForDiscreteEvents(); //discrete time conditions


_event_system->getConditions(_conditions1);
bool crestart = !std::equal (_conditions1, _conditions1+dim,_conditions0);

return((drestart||crestart)); //returns true if new events occured
}

/*
bool ContinuousEvents::checkConditions(const bool* events, bool all)
{
IEvent* event_system= dynamic_cast<IEvent*>(_system);
int dim = event_system->getDimZeroFunc();
bool* conditions0 = new bool[dim];
bool* conditions1 = new bool[dim];
event_system->getConditions(conditions0);
for(int i=0;i<dim;i++)
{
if(all||events[i])
getCondition(i);
}
event_system->getConditions(conditions1);
return !std::equal (conditions1, conditions1+dim,conditions0);
}
*/

171 changes: 171 additions & 0 deletions SimulationRuntime/cpp/Core/System/DiscreteEvents.cpp
@@ -0,0 +1,171 @@
#include <Core/Modelica.h>
#include "FactoryExport.h"
#include <Core/System/PreVariables.h>
#include <Core/System/DiscreteEvents.h>
#include <Core/Math/Functions.h>


DiscreteEvents::DiscreteEvents(PreVariables* preVars)
: _preVars(preVars)
{
}

DiscreteEvents::~DiscreteEvents(void)
{


}

/**
Inits the event variables
*/
void DiscreteEvents::initialize()
{

_preVars->initPreVariables();
_preVars->_pre_vars.resize((boost::extents[_preVars->_pre_real_vars_idx.size()+_preVars->_pre_int_vars_idx.size()+_preVars->_pre_bool_vars_idx.size()]));
}

/*
void DiscreteEvents::savePreVars(double vars[], unsigned int n)
{
_preVars->_pre_vars.assign(vars,vars+n);
}
*/

/**
Saves a variable in _preVars->_pre_vars vector
*/

void DiscreteEvents::save(double& var)
{
unsigned int i = _preVars->_pre_real_vars_idx[&var];
_preVars->_pre_vars[i]=var;
}

/**
Saves a variable in _preVars->_pre_vars vector
*/

void DiscreteEvents::save(int& var)
{
unsigned int i = _preVars->_pre_int_vars_idx[&var];
_preVars->_pre_vars[i]=var;
}

/**
Saves a variable in _preVars->_pre_vars vector
*/

void DiscreteEvents::save(bool& var)
{
unsigned int i = _preVars->_pre_bool_vars_idx[&var];
_preVars->_pre_vars[i]=var;
}

/**
Implementation of the Modelica pre operator
*/
double DiscreteEvents::pre(double& var)
{
unsigned int i = _preVars->_pre_real_vars_idx[&var];
return _preVars->_pre_vars[i];

}

/**
Implementation of the Modelica pre operator
*/
double DiscreteEvents::pre(int& var)
{
unsigned int i = _preVars->_pre_int_vars_idx[&var];
return _preVars->_pre_vars[i];

}

/**
Implementation of the Modelica pre operator
*/
double DiscreteEvents::pre(bool& var)
{
unsigned int i = _preVars->_pre_bool_vars_idx[&var];
return _preVars->_pre_vars[i];

}
/**
Implementation of the Modelica edge operator
Returns true for a variable when it changes from false to true
*/
bool DiscreteEvents::edge(double& var)
{
return var && !pre(var);
}

/**
Implementation of the Modelica edge operator
Returns true for a variable when it changes from false to true
*/
bool DiscreteEvents::edge(int& var)
{
return var && !pre(var);
}

/**
Implementation of the Modelica edge operator
Returns true for a variable when it changes from false to true
*/
bool DiscreteEvents::edge(bool& var)
{
return var && !pre(var);
}

/**
Implementation of the Modelica change operator
Returns true for a variable when it change value
*/
bool DiscreteEvents::change(double& var)
{
return var != pre(var);
}

/**
Implementation of the Modelica change operator
Returns true for a variable when it change value
*/
bool DiscreteEvents::change(int& var)
{
return var != pre(var);
}

/**
Implementation of the Modelica change operator
Returns true for a variable when it change value
*/
bool DiscreteEvents::change(bool& var)
{
return var != pre(var);
}


bool DiscreteEvents::changeDiscreteVar(double& var)
{
unsigned int i = _preVars->_pre_real_vars_idx[&var];
return var != _preVars->_pre_vars[i];

}
bool DiscreteEvents::changeDiscreteVar(int& var)
{
unsigned int i = _preVars->_pre_int_vars_idx[&var];
return var != _preVars->_pre_vars[i];

}

bool DiscreteEvents::changeDiscreteVar(bool& var)
{
unsigned int i = _preVars->_pre_bool_vars_idx[&var];
return var != _preVars->_pre_vars[i];

}



0 comments on commit dad9896

Please sign in to comment.