Skip to content

Commit

Permalink
Added time events for oneStepMode
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23523 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
nimen committed Nov 24, 2014
1 parent 69f1e27 commit ee4e5e6
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 71 deletions.
6 changes: 6 additions & 0 deletions SimulationRuntime/cpp/CMakeLists.txt
Expand Up @@ -47,6 +47,7 @@ SET(SimulationSettings ${LIBPREFIX}SimulationSettings)
SET(SimControllerName ${LIBPREFIX}SimController)
SET(CVodeName ${LIBPREFIX}CVode)
SET(PeerName ${LIBPREFIX}Peer)
SET(RTRKName ${LIBPREFIX}RTRK)
SET(EulerName ${LIBPREFIX}Euler)
SET(IdaName ${LIBPREFIX}Ida)
SET(IdasName ${LIBPREFIX}Idas)
Expand Down Expand Up @@ -318,6 +319,7 @@ add_subdirectory (Solver/Newton)
add_subdirectory (Solver/Hybrj)
add_subdirectory (Solver/UmfPack)
add_subdirectory (Solver/Peer)
#add_subdirectory (Solver/RTRK)


if(REDUCE_DAE)
Expand Down Expand Up @@ -352,6 +354,9 @@ endIf()
GET_TARGET_PROPERTY(libPeer ${PeerName} LOCATION)
GET_FILENAME_COMPONENT(libPeerName ${libPeer} NAME)

GET_TARGET_PROPERTY(libRTRK ${RTRKName} LOCATION)
GET_FILENAME_COMPONENT(libRTRKName ${libRTRK} NAME)

GET_TARGET_PROPERTY(libNewton ${NewtonName} LOCATION)
GET_FILENAME_COMPONENT(libNewtonName ${libNewton} NAME)

Expand Down Expand Up @@ -396,6 +401,7 @@ set (MODELICASYSTEM_LIB ${libModelicaName})
set (NEWTON_LIB ${libNewtonName})
set (UMFPACK_LIB ${libUmfPackName})
set (PEER_LIB ${libPeerName})
set (RTRK_LIB ${libRTRKName})
set (SYSTEM_LIB ${libSystemName})
set (SOLVER_LIB ${libSolverName})
set (MATH_LIB ${libMathName})
Expand Down
209 changes: 143 additions & 66 deletions SimulationRuntime/cpp/Core/SimController/SimManager.cpp
Expand Up @@ -27,6 +27,10 @@ SimManager::SimManager(boost::shared_ptr<IMixedSystem> system, Configuration* co
, _config (config)
, _timeeventcounter (NULL)
, _events (NULL)
, _sampleCycles (NULL)
,_cycleCounter (0)
,_resetCycle (0)
,_lastCycleTime (0)
{
_solver = _config->createSelectedSolver(system.get());
_initialization = boost::shared_ptr<Initialization>(new Initialization(boost::dynamic_pointer_cast<ISystemInitialization>(_mixed_system), _solver));
Expand All @@ -38,14 +42,39 @@ SimManager::~SimManager()
delete [] _timeeventcounter;
if(_events)
delete [] _events;
}
if(_sampleCycles)
delete [] _sampleCycles;
}

void SimManager::initialize()
{
boost::shared_ptr<ISystemInitialization> system_obj = boost::dynamic_pointer_cast<ISystemInitialization>(_mixed_system);
boost::shared_ptr<IContinuous> cont_system = boost::dynamic_pointer_cast<IContinuous>(_mixed_system);
boost::shared_ptr<ITime> timeevent_system = boost::dynamic_pointer_cast<ITime>(_mixed_system);
boost::shared_ptr<IEvent> event_system = boost::dynamic_pointer_cast<IEvent>(_mixed_system);
_cont_system = boost::dynamic_pointer_cast<IContinuous>(_mixed_system);
_timeevent_system = boost::dynamic_pointer_cast<ITime>(_mixed_system);
_event_system = boost::dynamic_pointer_cast<IEvent>(_mixed_system);
_step_event_system = boost::dynamic_pointer_cast<IStepEvent>(_mixed_system);

//Check dynamic casts
if(!_event_system)
{
std::cerr << "Could not get event system" << std::endl;
return;
}
if(!_cont_system)
{
std::cerr << "Could not get continuous-event system" << std::endl;
return;
}
if(!_timeevent_system)
{
std::cerr << "Could not get time-event system" << std::endl;
return;
}
if(!_step_event_system)
{
std::cerr << "Could not get step-event system" << std::endl;
return;
}

/* Logs temporarily disabled
BOOST_LOG_SEV(simmgr_lg::get(), simmgr_info) << "start init";*/

Expand All @@ -58,8 +87,6 @@ void SimManager::initialize()
try
{
// System zusammenbauen und einmal updaten
//system_obj->initialize(_config->getGlobalSettings()->getStartTime(),_config->getGlobalSettings()->getEndTime());
//cont_system->update(IContinuous::UPDATE(IContinuous::ACROSS|IContinuous::THROUGH));
_initialization->initializeSystem();
}
catch(std::exception& ex)
Expand All @@ -72,21 +99,29 @@ void SimManager::initialize()
_accStps = 0;
_rejStps =0;

if(timeevent_system)
if(_timeevent_system)
{
_dimtimeevent = timeevent_system->getDimTimeEvent();
_dimtimeevent = _timeevent_system->getDimTimeEvent();
if(_timeeventcounter)
delete _timeeventcounter;
delete [] _timeeventcounter;
_timeeventcounter = new int[_dimtimeevent];
// compute sampleCycles for RT simulation
if(_config->getGlobalSettings()->useEndlessSim())
{
if(_sampleCycles)
delete [] _sampleCycles;
_sampleCycles = new int[_dimtimeevent];
computeSampleCycles();
}
}
else
_dimtimeevent =0;
_tStart = _config->getGlobalSettings()->getStartTime();
_tEnd =_config->getGlobalSettings()->getEndTime();
// _solver->setTimeOut(_config->getGlobalSettings()->getAlarmTime());
_dimZeroFunc = event_system->getDimZeroFunc();
_dimZeroFunc = _event_system->getDimZeroFunc();
_solverTask = ISolver::SOLVERCALL(ISolver::FIRST_CALL);
if (_dimZeroFunc == event_system->getDimZeroFunc() )
if (_dimZeroFunc == _event_system->getDimZeroFunc() )
{
if(_events)
delete [] _events;
Expand All @@ -103,17 +138,85 @@ void SimManager::initialize()
/* Logs vorübergehend deaktiviert
BOOST_LOG_SEV(simmgr_lg::get(), simmgr_info) << "Assemble completed";*/

#if defined(__TRICORE__) || defined(__vxworks)
//#if defined(__TRICORE__) || defined(__vxworks)
// Initialization for RT simulation
if(_config->getGlobalSettings()->useEndlessSim())
_solver->initialize();
#endif
{
_cycleCounter = 0;
_resetCycle = _sampleCycles[0];
for (int i=1;i<_dimtimeevent;i++)
_resetCycle*=_sampleCycles[i];
_solver->initialize();
}
//#endif
}

void SimManager::runSingleStep(double cycletime)
{
// Increase time event counter
if(_dimtimeevent)
{
if(_lastCycleTime && cycletime != _lastCycleTime)
throw std::runtime_error("Cycle time can not be changed, if time events (samples) are present!");
else
_lastCycleTime = cycletime;

for(int i=0;i<_dimtimeevent;i++)
{
if(_cycleCounter % _sampleCycles[i] == 0)
_timeeventcounter[i]++;
}

//Handle time event
_timeevent_system->handleTimeEvent(_timeeventcounter);
_cont_system->evaluateAll(IContinuous::CONTINUOUS);
_mixed_system->saveAll();
_timeevent_system->handleTimeEvent(_timeeventcounter);
}

// Solve
_solver->setcycletime(cycletime);
_solver->solve(_solverTask);

_cycleCounter++;
// Reset everything to prevent overflows
if( _cycleCounter == _resetCycle + 1)
{
_cycleCounter = 1;
for(int i=0;i<_dimtimeevent;i++)
_timeeventcounter[i] = 0;
}
}

void SimManager::computeSampleCycles()
{
int
counter = 0;
time_event_type timeEventPairs; ///< - Contains start times and time spans

_timeevent_system->getTimeEvent(timeEventPairs);
std::vector<std::pair<double,double> >::iterator iter;
iter = timeEventPairs.begin();
for(; iter != timeEventPairs.end(); ++iter)
{
if(iter->first != 0.0 || iter->second == 0.0)
{
throw std::runtime_error("Time event not starting at t=0.0 or not cyclic!");
}else
{
// Check if sample time is a multiple of the cycle time (with a tolerance)
if ((iter->second/_config->getGlobalSettings()->getEndTime()) - int((iter->second/_config->getGlobalSettings()->getEndTime())+0.5) <= 1e6*UROUND)
{
_sampleCycles[counter] = int((iter->second/_config->getGlobalSettings()->getEndTime())+0.5);
}
else
{
throw std::runtime_error("Sample time is not a multiple of the cycle time!");
}

}
counter++;
}
}

void SimManager::runSimulation()
Expand Down Expand Up @@ -292,7 +395,6 @@ void SimManager::writeProperties()

void SimManager::computeEndTimes(std::vector<std::pair<double,int> > &tStopsSub)
{
boost::shared_ptr<ITime> timeevent_system = boost::dynamic_pointer_cast<ITime>(_mixed_system);
int
counterTimes = 0,
counterEvents = 0;
Expand All @@ -301,7 +403,7 @@ void SimManager::computeEndTimes(std::vector<std::pair<double,int> > &tStopsSub)

if(tStopsSub.size()==0)
{
timeevent_system->getTimeEvent(timeEventPairs);
_timeevent_system->getTimeEvent(timeEventPairs);
std::vector<std::pair<double,double> >::iterator iter;
iter = timeEventPairs.begin();
for(; iter != timeEventPairs.end(); ++iter)
Expand Down Expand Up @@ -391,10 +493,6 @@ void SimManager::computeEndTimes(std::vector<std::pair<double,int> > &tStopsSub)
void SimManager::runSingleProcess()
{

boost::shared_ptr<IEvent> event_system = boost::dynamic_pointer_cast<IEvent>(_mixed_system);
boost::shared_ptr<IContinuous> cont_system = boost::dynamic_pointer_cast<IContinuous>(_mixed_system);
boost::shared_ptr<ITime> timeevent_system = boost::dynamic_pointer_cast<ITime>(_mixed_system);
boost::shared_ptr<IStepEvent> step_event_system = boost::dynamic_pointer_cast<IStepEvent>(_mixed_system);
double
startTime,
endTime,
Expand All @@ -404,27 +502,6 @@ void SimManager::runSingleProcess()

std::vector<std::pair<double,int> > tStopsSub;

if(!event_system)
{
std::cerr << "Could not get event system" << std::endl;
return;
}
if(!cont_system)
{
std::cerr << "Could not get continuous-event system" << std::endl;
return;
}
if(!timeevent_system)
{
std::cerr << "Could not get time-event system" << std::endl;
return;
}
if(!step_event_system)
{
std::cerr << "Could not get step-event system" << std::endl;
return;
}

_H =_tEnd;
_solverTask = ISolver::SOLVERCALL(_solverTask | ISolver::RECORDCALL);
_solver->setStartTime(_tStart);
Expand All @@ -442,24 +519,24 @@ void SimManager::runSingleProcess()
memset(_timeeventcounter,0,_dimtimeevent*sizeof(int));
computeEndTimes(tStopsSub);
_tStops.push_back(tStopsSub);
dimZeroF = event_system->getDimZeroFunc();
dimZeroF = _event_system->getDimZeroFunc();
zeroVal_new = new double[dimZeroF];
timeevent_system->setTime(_tStart);
_timeevent_system->setTime(_tStart);
if (_dimtimeevent)
{
timeevent_system->handleTimeEvent(_timeeventcounter);
_timeevent_system->handleTimeEvent(_timeeventcounter);
}
cont_system->evaluateODE(IContinuous::CONTINUOUS); // vxworksupdate
event_system->getZeroFunc(zeroVal_new);
_cont_system->evaluateODE(IContinuous::CONTINUOUS); // vxworksupdate
_event_system->getZeroFunc(zeroVal_new);

for(int i=0;i<_dimZeroFunc;i++)
_events[i] = bool(zeroVal_new[i]);
_mixed_system->handleSystemEvents(_events);
//cont_system->evaluateODE(IContinuous::CONTINUOUS);
//_cont_system->evaluateODE(IContinuous::CONTINUOUS);
// Reset the time-events
if (_dimtimeevent)
{
timeevent_system->handleTimeEvent(_timeeventcounter);
_timeevent_system->handleTimeEvent(_timeeventcounter);
}


Expand All @@ -481,15 +558,15 @@ void SimManager::runSingleProcess()
{
startTime = endTime;
_timeeventcounter[iter->second]++;
timeevent_system->handleTimeEvent(_timeeventcounter);
cont_system->evaluateODE(IContinuous::CONTINUOUS); // vxworksupdate
event_system->getZeroFunc(zeroVal_new);
_timeevent_system->handleTimeEvent(_timeeventcounter);
_cont_system->evaluateODE(IContinuous::CONTINUOUS); // vxworksupdate
_event_system->getZeroFunc(zeroVal_new);
for(int i=0;i<_dimZeroFunc;i++)
_events[i] = bool(zeroVal_new[i]);
_mixed_system->handleSystemEvents(_events);
//cont_system->evaluateODE(IContinuous::CONTINUOUS);
//_cont_system->evaluateODE(IContinuous::CONTINUOUS);
//reset time-events
timeevent_system->handleTimeEvent(_timeeventcounter);
_timeevent_system->handleTimeEvent(_timeeventcounter);
}
else
{
Expand All @@ -508,15 +585,15 @@ void SimManager::runSingleProcess()
if (_dimtimeevent)
{
_timeeventcounter[iter->second]++;
timeevent_system->handleTimeEvent(_timeeventcounter);
cont_system->evaluateODE(IContinuous::CONTINUOUS); // vxworksupdate
event_system->getZeroFunc(zeroVal_new);
_timeevent_system->handleTimeEvent(_timeeventcounter);
_cont_system->evaluateODE(IContinuous::CONTINUOUS); // vxworksupdate
_event_system->getZeroFunc(zeroVal_new);
for(int i=0;i<_dimZeroFunc;i++)
_events[i] = bool(zeroVal_new[i]);
_mixed_system->handleSystemEvents(_events);
//cont_system->evaluateODE(IContinuous::CONTINUOUS);
//_cont_system->evaluateODE(IContinuous::CONTINUOUS);
//reset time-events
timeevent_system->handleTimeEvent(_timeeventcounter);
_timeevent_system->handleTimeEvent(_timeeventcounter);
}
}

Expand Down Expand Up @@ -571,24 +648,24 @@ void SimManager::runSingleProcess()
{
if(zeroVal_new)
{
timeevent_system->handleTimeEvent(_timeeventcounter);
cont_system->evaluateODE(IContinuous::CONTINUOUS); // vxworksupdate
event_system->getZeroFunc(zeroVal_new);
_timeevent_system->handleTimeEvent(_timeeventcounter);
_cont_system->evaluateODE(IContinuous::CONTINUOUS); // vxworksupdate
_event_system->getZeroFunc(zeroVal_new);
for(int i=0;i<_dimZeroFunc;i++)
_events[i] = bool(zeroVal_new[i]);
_mixed_system->handleSystemEvents(_events);
//cont_system->evaluateODE(IContinuous::CONTINUOUS);
//_cont_system->evaluateODE(IContinuous::CONTINUOUS);
//reset time-events
timeevent_system->handleTimeEvent(_timeeventcounter);
_timeevent_system->handleTimeEvent(_timeeventcounter);
}
}

iter = _tStops[0].begin();
}

} // end while continue
step_event_system->setTerminal(true);
cont_system->evaluateAll(IContinuous::CONTINUOUS);
_step_event_system->setTerminal(true);
_cont_system->evaluateAll(IContinuous::CONTINUOUS);

if(zeroVal_new)
delete [] zeroVal_new;
Expand Down
3 changes: 1 addition & 2 deletions SimulationRuntime/cpp/Include/Core/Math/IBlas.h
Expand Up @@ -6,11 +6,10 @@
jack dongarra, linpack, 3/11/78.
modified 12/3/93, array(1) declarations changed to array(*)
*************************/
#if defined(__vxworks)

extern "C" void DCOPY(long int* n, double* dx, long int* incx, double* dy, long int* incy);
extern "C" void daxpy_(long int *N, double *DA, double *DX, long int *INCX, double *DY, long int *INCY);
extern "C" void dcopy_(long int *n, double *DX, long int *INCX, double *DY, long int *INCY);
// y := alpha*A*x + beta*y
extern "C" void dgemv_(char *trans, long int *m, long int *n, double *alpha, double *a, long int *lda, double *x, long int *incx, double *beta, double *y, long int *incy);
extern "C" void dscal_(long int *n, double *da, double *dx, long int *incx);
#endif

0 comments on commit ee4e5e6

Please sign in to comment.