Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 - Fixed bug FinZeroCrossinsg for algorithms
 - Improved hysteresis relation 
 - don't use hysteresis relation, while the intialization



git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14296 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Dec 9, 2012
1 parent 89de4b1 commit 96f9da7
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 46 deletions.
10 changes: 6 additions & 4 deletions Compiler/BackEnd/BackendDAECreate.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4376,18 +4376,20 @@ algorithm

case (((x as DAE.STMT_WHEN(exp = e,statementLst=stmts,elseWhen=NONE(),helpVarIndices=li, source = source)) :: xs),_, extraArg, _)
equation
((stmts2, extraArg)) = traverseStmtsExps(stmts,func, extraArg, knvars);
/* wbraun: statemenents inside when equations can't contain zero-crossings*/
/*((stmts2, extraArg)) = traverseStmtsExps(stmts,func, extraArg, knvars);*/
((e_1,extraArg)) = Expression.traverseExpTopDown(e, func, extraArg);
((xs_1, extraArg)) = traverseStmtsExps(xs, func, extraArg, knvars);
then ((DAE.STMT_WHEN(e_1,stmts2,NONE(),li,source) :: xs_1,extraArg));
then ((DAE.STMT_WHEN(e_1,stmts,NONE(),li,source) :: xs_1,extraArg));

case (((x as DAE.STMT_WHEN(exp = e,statementLst=stmts,elseWhen=SOME(ew),helpVarIndices=li, source = source)) :: xs),_, extraArg, _)
equation
(({ew_1}, extraArg)) = traverseStmtsExps({ew},func, extraArg, knvars);
((stmts2, extraArg)) = traverseStmtsExps(stmts,func, extraArg, knvars);
/* wbraun: statemenents inside when equations can't contain zero-crossings*/
/*((stmts2, extraArg)) = traverseStmtsExps(stmts,func, extraArg, knvars);*/
((e_1,extraArg)) = Expression.traverseExpTopDown(e, func, extraArg);
((xs_1, extraArg)) = traverseStmtsExps(xs, func, extraArg, knvars);
then ((DAE.STMT_WHEN(e_1,stmts2,SOME(ew),li,source) :: xs_1,extraArg));
then ((DAE.STMT_WHEN(e_1,stmts,SOME(ew_1),li,source) :: xs_1,extraArg));

case (((x as DAE.STMT_ASSERT(cond = e, msg=e2, source = source)) :: xs),_, extraArg, _)
equation
Expand Down
8 changes: 4 additions & 4 deletions Compiler/Template/CodegenC.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6723,19 +6723,19 @@ case rel as RELATION(__) then
let isReal = if isRealType(typeof(rel.exp1)) then (if isRealType(typeof(rel.exp2)) then 'true' else '') else ''
match rel.operator
case LESS(__) then
let hysteresisfunction = if isReal then 'LessZC(<%e1%>,<%e2%>, data->simulationInfo.relations[<%rel.index%>])' else 'Less(<%e1%>,<%e2%>)'
let hysteresisfunction = if isReal then 'LessZC(<%e1%>,<%e2%>, data->simulationInfo.hysteresisEnabled[<%rel.index%>])' else 'Less(<%e1%>,<%e2%>)'
let &preExp += '<%res%> = <%hysteresisfunction%>;<%\n%>'
res
case LESSEQ(__) then
let hysteresisfunction = if isReal then 'LessEqZC(<%e1%>,<%e2%>, data->simulationInfo.relations[<%rel.index%>])' else 'LessEq(<%e1%>,<%e2%>)'
let hysteresisfunction = if isReal then 'LessEqZC(<%e1%>,<%e2%>, data->simulationInfo.hysteresisEnabled[<%rel.index%>])' else 'LessEq(<%e1%>,<%e2%>)'
let &preExp += '<%res%> = <%hysteresisfunction%>;<%\n%>'
res
case GREATER(__) then
let hysteresisfunction = if isReal then 'GreaterZC(<%e1%>,<%e2%>, data->simulationInfo.relations[<%rel.index%>])' else 'Greater(<%e1%>,<%e2%>)'
let hysteresisfunction = if isReal then 'GreaterZC(<%e1%>,<%e2%>, data->simulationInfo.hysteresisEnabled[<%rel.index%>])' else 'Greater(<%e1%>,<%e2%>)'
let &preExp += '<%res%> = <%hysteresisfunction%>;<%\n%>'
res
case GREATEREQ(__) then
let hysteresisfunction = if isReal then 'GreaterEqZC(<%e1%>,<%e2%>, data->simulationInfo.relations[<%rel.index%>])' else 'GreaterEq(<%e1%>,<%e2%>)'
let hysteresisfunction = if isReal then 'GreaterEqZC(<%e1%>,<%e2%>, data->simulationInfo.hysteresisEnabled[<%rel.index%>])' else 'GreaterEq(<%e1%>,<%e2%>)'
let &preExp += '<%res%> = <%hysteresisfunction%>;<%\n%>'
res
end match
Expand Down
21 changes: 0 additions & 21 deletions SimulationRuntime/c/simulation/solver/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,27 +487,13 @@ int handleStateEvent(DATA* data, LIST* eventLst, double *eventTime)
if (listNextNode(it) != NULL) {
INFO(LOG_EVENTS, ", ");
}

/* switch the direction of ZeroCrossing */
if (data->simulationInfo.zeroCrossingEnabled[event_id] == -1) {
data->simulationInfo.zeroCrossingEnabled[event_id] = 1;
} else if (data->simulationInfo.zeroCrossingEnabled[event_id] == 1) {
data->simulationInfo.zeroCrossingEnabled[event_id] = -1;
}
}
listClear(eventLst);
INFO(LOG_EVENTS, "\n");

/* update the whole system */
updateDiscreteSystem(data);

/* update relations after an event */
/*
storePreValues(data);
function_updateRelations(data, 0);
storeRelations(data);
*/

saveZeroCrossingsAfterEvent(data);

return 0;
Expand Down Expand Up @@ -540,13 +526,6 @@ int handleSampleEvent(DATA* data)
deactivateSampleEventsandEquations(data);
INFO1(LOG_EVENTS, "event Handling for Sample : %f done!", data->localData[0]->timeValue);

/* update relations after an event */
/*
storePreValues(data);
function_updateRelations(data, 0);
storeRelations(data);
*/

saveZeroCrossingsAfterEvent(data);

return 0;
Expand Down
83 changes: 75 additions & 8 deletions SimulationRuntime/c/simulation/solver/model_help.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ void updateDiscreteSystem(DATA *data)
modelica_boolean relationChanged = 0;
data->simulationInfo.needToIterate = 0;

function_updateRelations(data, 1);
storeRelations(data);
updateHysteresis(data);
if (DEBUG_STREAM(LOG_EVENTS))
printRelations(data);

Expand Down Expand Up @@ -92,6 +95,7 @@ void updateDiscreteSystem(DATA *data)
relationChanged = checkRelations(data);
discreteChanged = checkForDiscreteChanges(data);
}
updateHysteresis(data);
}


Expand Down Expand Up @@ -206,6 +210,8 @@ void printAllVars(DATA *data, int ringSegment)
INFO3(LOG_STDOUT, "%ld: %s = %s", i, mData->stringVarsData[i].info.name, data->localData[ringSegment]->stringVars[i]);
}
RELEASE(LOG_STDOUT);

RELEASE(LOG_STDOUT);
}


Expand Down Expand Up @@ -518,6 +524,66 @@ void resetAllHelpVars(DATA *data)
}
}

/*! \fn printHysteresisRelations
*
*
* \param [out] [data]
*
* \author wbraun
*/
void printHysteresisRelations(DATA *data)
{
long i;

INFO(LOG_STDOUT, "Status of hysteresisEnabled:");
INDENT(LOG_STDOUT);
for(i=0; i<data->modelData.nRelations; i++)
{
INFO5(LOG_STDOUT, "[%ld] %s = %c | relation(%s) = %c", i, relationDescription[i], data->simulationInfo.hysteresisEnabled[i]>0 ? 'T' : 'F', relationDescription[i], data->simulationInfo.relations[i] ? 'T' : 'F');
}
RELEASE(LOG_STDOUT);
}

/*! \fn activateHysteresis
*
*
* \param [out] [data]
*
* \author wbraun
*/
void activateHysteresis(DATA* data){

int i;

MODEL_DATA *mData = &(data->modelData);
SIMULATION_INFO *sInfo = &(data->simulationInfo);

for(i=0;i<mData->nRelations;++i){
sInfo->hysteresisEnabled[i] = sInfo->relations[i]?0:1;
}
}

/*! \fn updateHysteresis
*
*
* \param [out] [data]
*
* \author wbraun
*/
void updateHysteresis(DATA* data){

int i;

MODEL_DATA *mData = &(data->modelData);
SIMULATION_INFO *sInfo = &(data->simulationInfo);

for(i=0;i<mData->nRelations;++i){
sInfo->hysteresisEnabled[i] = sInfo->relations[i]?1:0;
}
}



/*! \fn getNextSampleTimeFMU
*
* function return next sample time.
Expand Down Expand Up @@ -600,7 +666,7 @@ void initializeDataStruc(DATA *data)
data->simulationInfo.zeroCrossingsPre = (modelica_real*) calloc(data->modelData.nZeroCrossings, sizeof(modelica_real));
data->simulationInfo.relations = (modelica_boolean*) calloc(data->modelData.nRelations, sizeof(modelica_boolean));
data->simulationInfo.relationsPre = (modelica_boolean*) calloc(data->modelData.nRelations, sizeof(modelica_boolean));
data->simulationInfo.zeroCrossingEnabled = (modelica_boolean*) calloc(data->modelData.nZeroCrossings, sizeof(modelica_boolean));
data->simulationInfo.hysteresisEnabled = (modelica_boolean*) calloc(data->modelData.nRelations, sizeof(modelica_boolean));
data->simulationInfo.zeroCrossingIndex = (long*) malloc(data->modelData.nZeroCrossings*sizeof(long));
data->simulationInfo.mathEventsValuePre = (modelica_real*) malloc(data->modelData.nMathEvents*sizeof(modelica_real));
/* initialize zeroCrossingsIndex with corresponding index is used by events lists */
Expand Down Expand Up @@ -747,7 +813,8 @@ void deInitializeDataStruc(DATA *data)
free(data->simulationInfo.zeroCrossings);
free(data->simulationInfo.zeroCrossingsPre);
free(data->simulationInfo.relations);
free(data->simulationInfo.zeroCrossingEnabled);
free(data->simulationInfo.relationsPre);
free(data->simulationInfo.hysteresisEnabled);
free(data->simulationInfo.zeroCrossingIndex);

/* free buffer for old state variables */
Expand Down Expand Up @@ -805,10 +872,10 @@ static const double tolZC = 1e-10;
modelica_boolean LessZC(double a, double b, modelica_boolean direction)
{
modelica_boolean retVal;
double eps = (direction) ? tolZC*fabs(b)+tolZC: tolZC*fabs(a)+tolZC;
INFO4(LOG_EVENTS, "Relation LESS: %.20e < %.20e = %c (%c)",a, b, (a < b)?'t':'f' , direction?'t':'f');
double eps = (direction)? tolZC*fabs(b)+tolZC : tolZC*fabs(a)+tolZC;
/*INFO4(LOG_EVENTS, "Relation LESS: %.20e < %.20e = %c (%c)",a, b, (a < b)?'t':'f' , direction?'t':'f');*/
retVal = (direction)? (a < b + eps):(a + eps < b);
INFO1(LOG_EVENTS, "Result := %c", retVal?'t':'f');
/*INFO1(LOG_EVENTS, "Result := %c", retVal?'t':'f');*/
return retVal;
}

Expand All @@ -820,10 +887,10 @@ modelica_boolean LessEqZC(double a, double b, modelica_boolean direction)
modelica_boolean GreaterZC(double a, double b, modelica_boolean direction)
{
modelica_boolean retVal;
double eps = (direction) ? tolZC*fabs(a)+tolZC: tolZC*fabs(b)+tolZC;
INFO4(LOG_EVENTS, "Relation GREATER: %.20e > %.20e = %c (%c)",a, b, (a > b)?'t':'f' , direction?'t':'f');
double eps = (direction)? tolZC*fabs(a)+tolZC : tolZC*fabs(b)+tolZC;
/*INFO4(LOG_EVENTS, "Relation GREATER: %.20e > %.20e = %c (%c)",a, b, (a > b)?'t':'f' , direction?'t':'f');*/
retVal = (direction)? (a + eps > b ):(a > b + eps);
INFO1(LOG_EVENTS, "Result := %c", retVal?'t':'f');
/*INFO1(LOG_EVENTS, "Result := %c", retVal?'t':'f');*/
return retVal;
}

Expand Down
25 changes: 19 additions & 6 deletions SimulationRuntime/c/simulation/solver/model_help.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,24 @@ extern "C" {

#define RELATIONHYSTERESIS(res,exp1,exp2,index,op_w) { \
if (data->simulationInfo.discreteCall == 0){ \
res = data->simulationInfo.relationsPre[index]; \
} else{ \
if (data->simulationInfo.solveContinuous){ \
res = data->simulationInfo.relationsPre[index]; \
data->simulationInfo.relations[index] = ((op_w##ZC)((exp1),(exp2),data->simulationInfo.relationsPre[index])); \
} else{ \
if (data->simulationInfo.initial){\
if (data->simulationInfo.solveContinuous){ \
res = data->simulationInfo.relationsPre[index]; \
data->simulationInfo.relations[index] = ((op_w)((exp1),(exp2))); \
} else { \
res = ((op_w)((exp1),(exp2))); \
data->simulationInfo.relations[index] = res; \
}\
} else { \
res = ((op_w##ZC)((exp1),(exp2),data->simulationInfo.relationsPre[index])); \
data->simulationInfo.relations[index] = res; \
if (data->simulationInfo.solveContinuous){ \
res = data->simulationInfo.relationsPre[index]; \
data->simulationInfo.relations[index] = ((op_w##ZC)((exp1),(exp2),data->simulationInfo.hysteresisEnabled[index])); \
} else { \
res = ((op_w##ZC)((exp1),(exp2),data->simulationInfo.hysteresisEnabled[index])); \
data->simulationInfo.relations[index] = res; \
}\
}\
}\
}
Expand Down Expand Up @@ -99,6 +109,9 @@ void storeRelations(DATA *data);
modelica_boolean checkRelations(DATA *data);

void resetAllHelpVars(DATA* data);
void printHysteresisRelations(DATA *data);
void activateHysteresis(DATA* data);
void updateHysteresis(DATA* data);

double getNextSampleTimeFMU(DATA *data);

Expand Down
21 changes: 19 additions & 2 deletions SimulationRuntime/c/simulation/solver/solver_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,21 @@ int solver_main(DATA* data, const char* init_initMethod,
}
}
storePreValues(data);
storeRelations(data);

/*determined discrete system */
if(checkForNewEvent(data, solverInfo.eventLst)){
handleStateEvent(data, solverInfo.eventLst, &(solverInfo.currentTime));
}else{
updateDiscreteSystem(data);
}
saveZeroCrossings(data);
storePreValues(data);
storeOldValues(data);
function_storeDelayed(data);
function_updateRelations(data,1);
storeRelations(data);
updateHysteresis(data);
saveZeroCrossings(data);
sim_result_emit(data);
overwriteOldSimulationData(data);

Expand Down Expand Up @@ -299,9 +303,11 @@ int solver_main(DATA* data, const char* init_initMethod,
retValIntegrator = solver_main_step(flag, data, &solverInfo);
updateContinuousSystem(data);
saveZeroCrossings(data);

RELEASE(LOG_SOLVER);



/******** Event handling ********/
INDENT(LOG_EVENTS);
if (measure_time_flag)
Expand All @@ -314,14 +320,23 @@ int solver_main(DATA* data, const char* init_initMethod,
}else{
sim_result_emit(data);
}

}

if (checkStateorSampleEvent(data, solverInfo.eventLst, &(solverInfo.currentTime))){
if (DEBUG_STREAM(LOG_DEBUG))
printAllVars(data, 0);

if (DEBUG_STREAM(LOG_EVENTS))
printHysteresisRelations(data);

handleStateEvent(data, solverInfo.eventLst, &(solverInfo.currentTime));

INFO1(LOG_SOLVER," ### State event occurs at time: %.5f", solverInfo.currentTime);

if (DEBUG_STREAM(LOG_EVENTS))
printHysteresisRelations(data);

solverInfo.stateEvents++;
solverInfo.didEventStep = 1;
/* due to an event overwrite old values */
Expand All @@ -330,6 +345,7 @@ int solver_main(DATA* data, const char* init_initMethod,
} else if (simInfo->sampleActivated) {

handleSampleEvent(data);

INFO1(LOG_SOLVER," ### Sample event occurs at time: %.5f", solverInfo.currentTime);

solverInfo.sampleEvents++;
Expand All @@ -341,6 +357,7 @@ int solver_main(DATA* data, const char* init_initMethod,
} else {
solverInfo.laststep = solverInfo.currentTime;
solverInfo.didEventStep = 0;

}
if (measure_time_flag)
rt_accumulate(SIM_TIMER_EVENT);
Expand All @@ -350,9 +367,9 @@ int solver_main(DATA* data, const char* init_initMethod,
/******** Emit this time step ********/
storePreValues(data);
storeOldValues(data);

saveZeroCrossings(data);


if (fmt) {
int flag = 1;
double tmpdbl;
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/simulation_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@
modelica_real* zeroCrossingsPre;
modelica_boolean* relations;
modelica_boolean* relationsPre;
modelica_boolean* zeroCrossingEnabled;
modelica_boolean* hysteresisEnabled;
modelica_real* mathEventsValuePre;
long* zeroCrossingIndex; /* pointer for a list events at event instants */

Expand Down

0 comments on commit 96f9da7

Please sign in to comment.