Skip to content

Commit

Permalink
- changed fmu export for event handling
Browse files Browse the repository at this point in the history
- added bug fix in event handling
- removed one old unsed variable



git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@9724 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Aug 31, 2011
1 parent 362a6c7 commit 2536ca0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 87 deletions.
66 changes: 21 additions & 45 deletions c_runtime/fmu_model_interface.c
Expand Up @@ -319,17 +319,9 @@ fmiStatus fmiGetReal(fmiComponent c, const fmiValueReference vr[], size_t nvr, f
return fmiError;
#if NUMBER_OF_REALS>0
// calculate new values
if (comp->eventInfo.stateValuesChanged == fmiTrue)
{
int needToIterate = 0;
functionDAE(&needToIterate);
comp->eventInfo.stateValuesChanged = fmiFalse;
}
if (comp->outputsvalid == fmiFalse)
{
functionAliasEquations();
comp->outputsvalid = fmiTrue;
}
functionODE();
functionAlgebraics();

for (i=0; i<nvr; i++) {
if (vrOutOfRange(comp, "fmiGetReal", vr[i], NUMBER_OF_REALS+NUMBER_OF_STATES))
return fmiError;
Expand All @@ -354,17 +346,9 @@ fmiStatus fmiGetInteger(fmiComponent c, const fmiValueReference vr[], size_t nvr
return fmiError;
#if NUMBER_OF_INTEGERS>0
// calculate new values
if (comp->eventInfo.stateValuesChanged == fmiTrue)
{
int needToIterate = 0;
functionDAE(&needToIterate);
comp->eventInfo.stateValuesChanged = fmiFalse;
}
if (comp->outputsvalid == fmiFalse)
{
functionAliasEquations();
comp->outputsvalid = fmiTrue;
}
functionODE();
functionAlgebraics();

for (i=0; i<nvr; i++) {
if (vrOutOfRange(comp, "fmiGetInteger", vr[i], NUMBER_OF_INTEGERS))
return fmiError;
Expand All @@ -389,17 +373,9 @@ fmiStatus fmiGetBoolean(fmiComponent c, const fmiValueReference vr[], size_t nvr
return fmiError;
#if NUMBER_OF_BOOLEANS>0
// calculate new values
if (comp->eventInfo.stateValuesChanged == fmiTrue)
{
int needToIterate = 0;
functionDAE(&needToIterate);
comp->eventInfo.stateValuesChanged = fmiFalse;
}
if (comp->outputsvalid == fmiFalse)
{
functionAliasEquations();
comp->outputsvalid = fmiTrue;
}
functionODE();
functionAlgebraics();

for (i=0; i<nvr; i++) {
if (vrOutOfRange(comp, "fmiGetBoolean", vr[i], NUMBER_OF_BOOLEANS))
return fmiError;
Expand All @@ -424,17 +400,9 @@ fmiStatus fmiGetString(fmiComponent c, const fmiValueReference vr[], size_t nvr,
return fmiError;
#if NUMBER_OF_STRINGS>0
// calculate new values
if (comp->eventInfo.stateValuesChanged == fmiTrue)
{
functionODE();
comp->eventInfo.stateValuesChanged == fmiFalse;
}
if (comp->outputsvalid == fmiFalse)
{
functionAlgebraics();
functionAliasEquations();
comp->outputsvalid = fmiTrue;
}
functionODE();
functionAlgebraics();

for (i=0; i<nvr; i++) {
if (vrOutOfRange(comp, "fmiGetString", vr[i], NUMBER_OF_STRINGS))
return fmiError;
Expand Down Expand Up @@ -518,7 +486,9 @@ fmiStatus fmiGetDerivatives(fmiComponent c, fmiReal derivatives[], size_t nx) {
//if (comp->eventInfo.stateValuesChanged == fmiTrue)
//{
// calculate new values
functionDAE(&needToIterate);
functionODE();
functionAlgebraics();

for (i=0; i<nx; i++) {
fmiValueReference vr = vrStatesDerivatives[i];
derivatives[i] = getReal(comp, vr); // to be implemented by the includer of this file
Expand Down Expand Up @@ -648,6 +618,12 @@ fmiStatus fmiEventUpdate(fmiComponent c, fmiBoolean intermediateResults, fmiEven
eventInfo->stateValuesChanged = fmiFalse;
eventInfo->terminateSimulation = fmiFalse;
eventInfo->upcomingTimeEvent = fmiFalse;
/*functionODE();
functionAlgebraics();
fmiCompletedIntegratorStep(c,fmiFalse);
*/
update_DAEsystem();
//fmiCompletedIntegratorStep(c,fmiFalse);
eventUpdate(comp, eventInfo); // to be implemented by the includer of this file
return fmiOK;
}
Expand Down
46 changes: 19 additions & 27 deletions c_runtime/simulation_events.cpp
Expand Up @@ -188,29 +188,22 @@ sample(double start, double interval, int hindex)
// adrpo: if we test for inSample == 0 no event is generated when start + 0*interval!
// if (inSample == 0) return 0;
double tmp = ((globalData->timeValue - start) / interval);
if (euler_in_use)
{
tmp = 1;
int tmpindex = globalData->curSampleTimeIx;
if (tmpindex < globalData->nSampleTimes)
{
while ((globalData->sampleTimes[tmpindex]).activated == 1)
{
if ((globalData->sampleTimes[tmpindex]).zc_index == hindex)
{
tmp = 0;
}
tmpindex++;
if (tmpindex == globalData->nSampleTimes)
break;
}
}
//tmp = ((globalData->timeValue - start) / interval);
}
else
{
tmp -= floor(tmp);
}
tmp = 1;
int tmpindex = globalData->curSampleTimeIx;
if (tmpindex < globalData->nSampleTimes)
{
while ((globalData->sampleTimes[tmpindex]).activated == 1)
{
if ((globalData->sampleTimes[tmpindex]).zc_index == hindex)
{
tmp = 0;
}
tmpindex++;
if (tmpindex == globalData->nSampleTimes)
break;
}
}

/* adrpo - 2008-01-15
* comparison was tmp >= 0 fails sometimes on x86 due to extended precision in registers
* TODO - fix the simulation runtime so that the sample event is generated at EXACTLY that time.
Expand Down Expand Up @@ -596,8 +589,6 @@ deactivateSampleEventsandEquations()
int
CheckForNewEvent(int* sampleactived)
{
//std::copy(gout, gout + globalData->nZeroCrossing, gout_old);
//function_onlyZeroCrossings(gout,&globalData->timeValue);
initializeZeroCrossings();
if (sim_verbose >= LOG_EVENTS)
{
Expand Down Expand Up @@ -632,7 +623,8 @@ CheckForNewEvent(int* sampleactived)
EventList.push_front(i);
}
}
if ((gout[i] < 0 && gout_old[i] > 0) || (gout[i] > 0 && gout_old[i] < 0))
if ((gout[i] < 0 && gout_old[i] > 0) ||
(gout[i] > 0 && gout_old[i] < 0))
{
if (sim_verbose >= LOG_EVENTS)
{
Expand Down Expand Up @@ -879,7 +871,7 @@ FindRoot(double *EventTime)

if (tmpEventList.empty())
{
double value = fabs(gout[0]);
double value = fabs(gout[*(EventList.begin())]);;
for ( it=EventList.begin() ; it != EventList.end(); it++ )
{
if (value > fabs(gout[*it]))
Expand Down
6 changes: 0 additions & 6 deletions c_runtime/simulation_events.h
Expand Up @@ -88,16 +88,10 @@ int
checkForSampleEvent();

extern long inUpdate;
extern int euler_in_use;
static const int IterationMax = 200;

#define ZEROCROSSING(ind,exp) { \
if (euler_in_use) { \
gout[ind] = exp; \
} \
else {\
gout[ind] = (zeroCrossingEnabled[ind])?((double)zeroCrossingEnabled[ind])*exp:1.0; \
} \
}

#define RELATIONTOZC(res,exp1,exp2,index,op_w,op) { \
Expand Down
7 changes: 1 addition & 6 deletions c_runtime/solver_main.cpp
Expand Up @@ -218,8 +218,6 @@ const double rungekutta_c[rungekutta_s] =
// Used when calculating residual for its side effects. (alg. var calc)
double *dummy_delta = NULL;

int euler_in_use = 0;

int
solver_main_step(int flag, double &start, double &stop, bool &reset, int* stats)
{
Expand Down Expand Up @@ -294,9 +292,6 @@ solver_main(int argc, char** argv, double &start, double &stop, double &step,
dasslStatsTmp[i] = 0;
}

//Workaround for Relation in simulation_events
euler_in_use = 1;

//Flags for event handling
int dideventstep = 0;
bool reset = false;
Expand Down Expand Up @@ -389,7 +384,7 @@ solver_main(int argc, char** argv, double &start, double &stop, double &step,
activateSampleEvents();
}
update_DAEsystem();
SaveZeroCrossings();
initializeZeroCrossings();
if (sampleEvent_actived)
{
deactivateSampleEventsandEquations();
Expand Down
3 changes: 0 additions & 3 deletions c_runtime/solver_qss/solver_qss.cpp
Expand Up @@ -73,9 +73,6 @@ int qss_main( int argc, char** argv,double &start, double &stop, double &step,
dasslStatsTmp[i] = 0;
}

//Workaround for Relation in simulation_events
euler_in_use = 1;

//Flags for event handling
int dideventstep = 0;
bool reset = false;
Expand Down

0 comments on commit 2536ca0

Please sign in to comment.