Skip to content

Commit

Permalink
- fix usage of storedRelations (formerly hysteresisEnabled)
Browse files Browse the repository at this point in the history
- new, more precise names:
  - array hysteresisEnabled renamed to storedRelations
  - function storeRelations renamed to updateRelationsPre
  - function updateHysteresis renamed to storeRelations
- cleanup:
  - function printHysteresisRelations removed
  - function activateHysteresis removed


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@20635 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed May 16, 2014
1 parent 53c00b5 commit c5895d8
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 149 deletions.
16 changes: 8 additions & 8 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -8415,19 +8415,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.hysteresisEnabled[<%rel.index%>])' else 'Less(<%e1%>,<%e2%>)'
let hysteresisfunction = if isReal then 'LessZC(<%e1%>, <%e2%>, data->simulationInfo.storedRelations[<%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.hysteresisEnabled[<%rel.index%>])' else 'LessEq(<%e1%>,<%e2%>)'
let hysteresisfunction = if isReal then 'LessEqZC(<%e1%>, <%e2%>, data->simulationInfo.storedRelations[<%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.hysteresisEnabled[<%rel.index%>])' else 'Greater(<%e1%>,<%e2%>)'
let hysteresisfunction = if isReal then 'GreaterZC(<%e1%>, <%e2%>, data->simulationInfo.storedRelations[<%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.hysteresisEnabled[<%rel.index%>])' else 'GreaterEq(<%e1%>,<%e2%>)'
let hysteresisfunction = if isReal then 'GreaterEqZC(<%e1%>, <%e2%>, data->simulationInfo.storedRelations[<%rel.index%>])' else 'GreaterEq(<%e1%>,<%e2%>)'
let &preExp += '<%res%> = <%hysteresisfunction%>;<%\n%>'
res
end match
Expand All @@ -8454,19 +8454,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.hysteresisEnabled[<%rel.index%>])' else 'Less(<%e1%>,<%e2%>)'
let hysteresisfunction = if isReal then 'LessZC(<%e1%>, <%e2%>, data->simulationInfo.storedRelations[<%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.hysteresisEnabled[<%rel.index%>])' else 'LessEq(<%e1%>,<%e2%>)'
let hysteresisfunction = if isReal then 'LessEqZC(<%e1%>, <%e2%>, data->simulationInfo.storedRelations[<%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.hysteresisEnabled[<%rel.index%>])' else 'Greater(<%e1%>,<%e2%>)'
let hysteresisfunction = if isReal then 'GreaterZC(<%e1%>, <%e2%>, data->simulationInfo.storedRelations[<%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.hysteresisEnabled[<%rel.index%>])' else 'GreaterEq(<%e1%>,<%e2%>)'
let hysteresisfunction = if isReal then 'GreaterEqZC(<%e1%>, <%e2%>, data->simulationInfo.storedRelations[<%rel.index%>])' else 'GreaterEq(<%e1%>,<%e2%>)'
let &preExp += '<%res%> = <%hysteresisfunction%>;<%\n%>'
res
end match
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/simulation/solver/events.c
Expand Up @@ -377,7 +377,7 @@ void findRoot(DATA* data, LIST *eventList, double *eventTime)

/* determined continuous system */
updateContinuousSystem(data);
storeRelations(data);
updateRelationsPre(data);
/*sim_result_emit(data);*/

data->localData[0]->timeValue = *eventTime;
Expand Down
Expand Up @@ -624,7 +624,7 @@ static int numeric_initialization(DATA *data, int optiMethod, int lambda_steps)
/* and restore start values */
restoreExtrapolationDataOld(data);
initializeStateSetPivoting(data); /* reset state selection */
storeRelations(data);
updateRelationsPre(data);
storePreValues(data);

retVal = initialize(data, optiMethod, lambda_steps);
Expand Down Expand Up @@ -746,16 +746,13 @@ static int symbolic_initialization(DATA *data, long numLambdaSteps)
data->simulationInfo.lambda = 1.0;
data->callback->functionInitialEquations(data);
}

/* update saved value for
hysteresis relations */
updateHysteresis(data);
storeRelations(data);

/* do pivoting for dynamic state selection if selection changed try again an */
if(stateSelection(data, 1, 1) == 1)
{
data->callback->functionInitialEquations(data);
updateHysteresis(data);
storeRelations(data);

/* report a warning about strange start values */
if(stateSelection(data, 1, 1) == 1)
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/simulation/solver/mixedSearchSolver.c
Expand Up @@ -216,7 +216,7 @@ int solveMixedSearch(DATA *data, int sysNumber)
/* restart if any relation has changed */
if(checkRelations(data))
{
storeRelations(data);
updateRelationsPre(data);
systemData->updateIterationExps(data);
debugStreamPrint(LOG_NLS, 0, "#### System relation changed restart iteration");
if(mixedIterations++ > 200)
Expand Down
99 changes: 26 additions & 73 deletions SimulationRuntime/c/simulation/solver/model_help.c
Expand Up @@ -67,8 +67,8 @@ void updateDiscreteSystem(DATA *data)
data->simulationInfo.needToIterate = 0;

data->callback->function_updateRelations(data, 1);
updateRelationsPre(data);
storeRelations(data);
updateHysteresis(data);

/* should we print the relations before functionDAE?
* printRelations(data, LOG_EVENTS_V);
Expand All @@ -91,7 +91,7 @@ void updateDiscreteSystem(DATA *data)
debugStreamPrint(LOG_EVENTS_V, 0, "discrete Variable changed. Iteration needed.");

storePreValues(data);
storeRelations(data);
updateRelationsPre(data);

printRelations(data, LOG_EVENTS_V);

Expand All @@ -104,7 +104,7 @@ void updateDiscreteSystem(DATA *data)
relationChanged = checkRelations(data);
discreteChanged = data->callback->checkForDiscreteChanges(data);
}
updateHysteresis(data);
storeRelations(data);
}

/*! \fn updateContinuousSystem
Expand Down Expand Up @@ -606,22 +606,6 @@ void storePreValues(DATA *data)
memcpy(sInfo->stringVarsPre, sData->stringVars, sizeof(modelica_string)*mData->nVariablesString);
}

/*! \fn storeRelations
*
* This function copys all the relations results into their pre-values.
*
* \param [ref] [data]
*
* \author wbraun
*/
void storeRelations(DATA *data){

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

memcpy(sInfo->relationsPre, sInfo->relations, sizeof(modelica_boolean)*mData->nRelations);
}

/*! \fn checkRelations
*
* This function check if at least one backupRelation has changed
Expand All @@ -630,16 +614,18 @@ void storeRelations(DATA *data){
*
* \author wbraun
*/
modelica_boolean checkRelations(DATA *data){

modelica_boolean checkRelations(DATA *data)
{
int i;
modelica_boolean check=0;

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

for(i=0;i<mData->nRelations;++i){
if(sInfo->relationsPre[i] != sInfo->relations[i]){
for(i=0;i<mData->nRelations;++i)
{
if(sInfo->relationsPre[i] != sInfo->relations[i])
{
check = 1;
break;
}
Expand All @@ -648,67 +634,33 @@ modelica_boolean checkRelations(DATA *data){
return check;
}

/*! \fn printHysteresisRelations
/*! \fn updateRelationsPre
*
* This function stores a copy of relations into relationsPre.
*
* \param [out] [data]
* \param [ref] [data]
*
* \author wbraun
* \author lochel
*/
void printHysteresisRelations(DATA *data)
void updateRelationsPre(DATA *data)
{
long i;

if (ACTIVE_STREAM(LOG_STDOUT)) {
infoStreamPrint(LOG_STDOUT, 1, "Status of hysteresisEnabled:");
for(i=0; i<data->modelData.nRelations; i++)
{
infoStreamPrint(LOG_STDOUT, 0, "[%ld] %s = %c | relation(%s) = %c", i, data->callback->relationDescription(i), data->simulationInfo.hysteresisEnabled[i]>0 ? 'T' : 'F', data->callback->relationDescription(i), data->simulationInfo.relations[i] ? 'T' : 'F');
}
messageClose(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;
}
memcpy(data->simulationInfo.relationsPre, data->simulationInfo.relations, sizeof(modelica_boolean)*data->modelData.nRelations);
}

/*! \fn updateHysteresis
/*! \fn storeRelations
*
* This function stores a copy of relationPre. This is needed for the event
* iteration.
*
* \param [out] [data]
*
* \author wbraun
* \author lochel
*/
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;
}
void storeRelations(DATA* data)
{
memcpy(data->simulationInfo.storedRelations, data->simulationInfo.relations, sizeof(modelica_boolean)*data->modelData.nRelations);
}



/*! \fn getNextSampleTimeFMU
*
* function return next sample time.
Expand All @@ -719,7 +671,8 @@ void updateHysteresis(DATA* data){
*/
double getNextSampleTimeFMU(DATA *data)
{
if(0 < data->modelData.nSamples){
if(0 < data->modelData.nSamples)
{
infoStreamPrint(LOG_EVENTS, 0, "Next event time = %f", data->simulationInfo.nextSampleEvent);
return data->simulationInfo.nextSampleEvent;
}
Expand Down Expand Up @@ -795,7 +748,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.hysteresisEnabled = (modelica_boolean*) calloc(data->modelData.nRelations, sizeof(modelica_boolean));
data->simulationInfo.storedRelations = (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 @@ -963,7 +916,7 @@ void deInitializeDataStruc(DATA *data)
free(data->simulationInfo.zeroCrossingsPre);
free(data->simulationInfo.relations);
free(data->simulationInfo.relationsPre);
free(data->simulationInfo.hysteresisEnabled);
free(data->simulationInfo.storedRelations);
free(data->simulationInfo.zeroCrossingIndex);

/* free buffer for old state variables */
Expand Down
13 changes: 9 additions & 4 deletions SimulationRuntime/c/simulation/solver/model_help.h
Expand Up @@ -38,7 +38,12 @@ extern "C" {

/* lochel: I guess this is used for discrete relations */
#define RELATION(res,exp1,exp2,index,op_w) { \
if(data->simulationInfo.discreteCall == 0 || data->simulationInfo.solveContinuous) \
if(data->simulationInfo.initial) \
{ \
res = ((op_w)((exp1),(exp2))); \
data->simulationInfo.relations[index] = res; \
} \
else if(data->simulationInfo.discreteCall == 0 || data->simulationInfo.solveContinuous) \
{ \
res = data->simulationInfo.relationsPre[index]; \
} \
Expand All @@ -62,7 +67,7 @@ extern "C" {
} \
else \
{ \
res = ((op_w##ZC)((exp1),(exp2),data->simulationInfo.hysteresisEnabled[index])); \
res = ((op_w##ZC)((exp1),(exp2),data->simulationInfo.relationsPre[index])); \
data->simulationInfo.relations[index] = res; \
} \
}
Expand Down Expand Up @@ -106,13 +111,13 @@ void restoreOldValues(DATA *data);

void storePreValues(DATA *data);

void storeRelations(DATA *data);
void updateRelationsPre(DATA *data);

modelica_boolean checkRelations(DATA *data);

void printHysteresisRelations(DATA *data);
void activateHysteresis(DATA* data);
void updateHysteresis(DATA* data);
void storeRelations(DATA* data);
void setZCtol(double relativeTol);

double getNextSampleTimeFMU(DATA *data);
Expand Down
Expand Up @@ -666,7 +666,7 @@ int solveHybrd(DATA *data, int sysNumber)
if(scaling)
solverData->useXScaling = 1;

storeRelations(data);
updateRelationsPre(data);
}
}

Expand Down
Expand Up @@ -299,7 +299,7 @@ int solveNewton(DATA *data, int sysNumber)
wrapper_fvec_newton(&solverData->n, solverData->x, solverData->fvec, &iflag, data, sysNumber);

((DATA*)data)->simulationInfo.solveContinuous = 1;
storeRelations(data);
updateRelationsPre(data);
}
}
/* check for error */
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/simulation_data.h
Expand Up @@ -482,7 +482,7 @@ typedef struct SIMULATION_INFO
modelica_real* zeroCrossingsPre;
modelica_boolean* relations;
modelica_boolean* relationsPre;
modelica_boolean* hysteresisEnabled;
modelica_boolean* storedRelations; /* this array contains a copy of relations each time the event iteration starts */
modelica_real* mathEventsValuePre;
long* zeroCrossingIndex; /* := {0, 1, 2, ..., data->modelData.nZeroCrossings-1}; pointer for a list events at event instants */

Expand Down

0 comments on commit c5895d8

Please sign in to comment.