Skip to content

Commit

Permalink
- fixed delay handling in non-linear systems
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14245 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Dec 5, 2012
1 parent c9bc455 commit c18a4bd
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Compiler/BackEnd/SimCode.mo
Expand Up @@ -117,7 +117,7 @@ end SimCode;
// Delayed expressions type
uniontype DelayedExpression
record DELAYED_EXPRESSIONS
list<tuple<Integer, DAE.Exp>> delayedExps;
list<tuple<Integer, tuple<DAE.Exp, DAE.Exp, DAE.Exp>>> delayedExps;
Integer maxDelayedIndex;
end DELAYED_EXPRESSIONS;
end DelayedExpression;
Expand Down
8 changes: 4 additions & 4 deletions Compiler/BackEnd/SimCodeUtil.mo
Expand Up @@ -1840,7 +1840,7 @@ algorithm
list<DAE.ComponentRef> discreteModelVars;
SimCode.ExtObjInfo extObjInfo;
SimCode.MakefileParams makefileParams;
list<tuple<Integer, DAE.Exp>> delayedExps;
list<tuple<Integer, tuple<DAE.Exp, DAE.Exp, DAE.Exp>>> delayedExps;
list<BackendDAE.Variables> orderedVars;
BackendDAE.Variables knownVars,vars;
list<BackendDAE.Var> varlst,varlst1,varlst2;
Expand Down Expand Up @@ -2522,7 +2522,7 @@ end findDelaySubExpressions;

protected function extractDelayedExpressions
input BackendDAE.BackendDAE dlow;
output list<tuple<Integer, DAE.Exp>> delayedExps;
output list<tuple<Integer, tuple<DAE.Exp, DAE.Exp, DAE.Exp>>> delayedExps;
output Integer maxDelayedExpIndex;
algorithm
(delayedExps,maxDelayedExpIndex) := matchcontinue(dlow)
Expand All @@ -2545,15 +2545,15 @@ end extractDelayedExpressions;

function extractIdAndExpFromDelayExp
input DAE.Exp delayCallExp;
output tuple<Integer, DAE.Exp> delayedExp;
output tuple<Integer, tuple<DAE.Exp, DAE.Exp, DAE.Exp>> delayedExp;
algorithm
delayedExp :=
match (delayCallExp)
local
DAE.Exp e, delay, delayMax;
Integer i;
case (DAE.CALL(path=Absyn.IDENT("delay"), expLst={DAE.ICONST(i),e,delay,delayMax}))
then ((i, e));
then ((i, (e, delay, delayMax)));
end match;
end extractIdAndExpFromDelayExp;

Expand Down
6 changes: 4 additions & 2 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -1048,13 +1048,15 @@ template functionStoreDelayed(DelayedExpression delayed)
"Generates function in simulation file."
::=
let &varDecls = buffer "" /*BUFD*/
let storePart = (match delayed case DELAYED_EXPRESSIONS(__) then (delayedExps |> (id, e) =>
let storePart = (match delayed case DELAYED_EXPRESSIONS(__) then (delayedExps |> (id, (e, d, delayMax)) =>
let &preExp = buffer "" /*BUFD*/
let eRes = daeExp(e, contextSimulationNonDiscrete,
&preExp /*BUFC*/, &varDecls /*BUFD*/)
let delayExp = daeExp(d, contextSimulationNonDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)
let delayExpMax = daeExp(delayMax, contextSimulationNonDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)
<<
<%preExp%>
storeDelayedExpression(data, <%id%>, <%eRes%>, time);<%\n%>
storeDelayedExpression(data, <%id%>, <%eRes%>, time, <%delayExp%>, <%delayExpMax%>);<%\n%>
>>
))
<<
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Template/CodegenJava.tpl
Expand Up @@ -360,7 +360,7 @@ template functionStoreDelayed(DelayedExpression delayed)
"Generates function in simulation file."
::=
let &varDecls = buffer "" /*BUFD*/
let storePart = (match delayed case DELAYED_EXPRESSIONS(__) then (delayedExps |> (id, e) =>
let storePart = (match delayed case DELAYED_EXPRESSIONS(__) then (delayedExps |> (id, (e,_,_)) =>
let &preExp = buffer "" /*BUFD*/
let eRes = daeExp(e, contextSimulationNonDiscrete,
&preExp /*BUFC*/, &varDecls /*BUFC*/)
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Template/SimCodeTV.mo
Expand Up @@ -156,7 +156,7 @@ package SimCode

uniontype DelayedExpression
record DELAYED_EXPRESSIONS
list<tuple<Integer, DAE.Exp>> delayedExps;
list<tuple<Integer, tuple<DAE.Exp, DAE.Exp, DAE.Exp>>> delayedExps;
Integer maxDelayedIndex;
end DELAYED_EXPRESSIONS;
end DelayedExpression;
Expand Down
27 changes: 18 additions & 9 deletions SimulationRuntime/c/simulation/solver/delay.c
Expand Up @@ -62,24 +62,27 @@ static int findTime(double time, RINGBUFFER *delayStruct)
int end = ringBufferLength(delayStruct);
double t;


INFO1(LOG_EVENTS, "findTime %e", time);
do
{
int i = (start + end) / 2;
t = ((TIME_AND_VALUE*)getRingData(delayStruct, i))->time;
INFO4(LOG_EVENTS, "time(%d, %d)[%d] = %e", start, end, i, t);
if(t > time)
end = i;
else
start = i;
}while(t != time && end > start + 1);
INFO3(LOG_EVENTS, "return time[%d, %d] = %e", start, end, t);
return (start);
}

void storeDelayedExpression(DATA* data, int exprNumber, double exprValue, double time)
void storeDelayedExpression(DATA* data, int exprNumber, double exprValue, double time, double delayTime, double delayMax)
{
int i;
TIME_AND_VALUE tpl;

INFO3(LOG_EVENTS, "storeDelayed[%d] %g:%g", exprNumber, time, exprValue);

/* Allocate more space for expressions */
ASSERT1(exprNumber < data->modelData.nDelayExpressions, "storeDelayedExpression: invalid expression number %d", exprNumber);
ASSERT1(0 <= exprNumber, "storeDelayedExpression: invalid expression number %d", exprNumber);
Expand All @@ -88,8 +91,17 @@ void storeDelayedExpression(DATA* data, int exprNumber, double exprValue, double
tpl.time = time;
tpl.value = exprValue;
appendRingData(data->simulationInfo.delayStructure[exprNumber], &tpl);
INFO4(LOG_EVENTS, "storeDelayed[%d] %g:%g position=%d", exprNumber, time, exprValue,ringBufferLength(data->simulationInfo.delayStructure[exprNumber]));

/* dequeue not longer needed values */
i = findTime(time-delayMax+DBL_EPSILON,data->simulationInfo.delayStructure[exprNumber]);
if (i > 0){
dequeueNFirstRingDatas(data->simulationInfo.delayStructure[exprNumber], i-1);
INFO3(LOG_EVENTS, "delayImpl: dequeueNFirstRingDatas[%d] %g = %g", i, time-delayMax+DBL_EPSILON, delayTime);
}
}


double delayImpl(DATA* data, int exprNumber, double exprValue, double time, double delayTime, double delayMax)
{
RINGBUFFER* delayStruct = data->simulationInfo.delayStructure[exprNumber];
Expand Down Expand Up @@ -149,11 +161,14 @@ double delayImpl(DATA* data, int exprNumber, double exprValue, double time, doub
/* find the row for the lower limit */
if(timeStamp > ((TIME_AND_VALUE*)getRingData(delayStruct, length - 1))->time)
{
INFO2(LOG_EVENTS, "delayImpl: find the row %g = %g", timeStamp, ((TIME_AND_VALUE*)getRingData(delayStruct, length - 1))->time);
/* delay between the last accepted time step and the current time */
time0 = ((TIME_AND_VALUE*)getRingData(delayStruct, length - 1))->time;
value0 = ((TIME_AND_VALUE*)getRingData(delayStruct, length - 1))->value;
time1 = time;
value1 = exprValue;
INFO2(LOG_EVENTS, "delayImpl: times %g and %g", time0, time1);
INFO2(LOG_EVENTS, "delayImpl: values %g and %g", value0, value1);
}
else
{
Expand All @@ -165,15 +180,10 @@ double delayImpl(DATA* data, int exprNumber, double exprValue, double time, doub
/* was it the last value? */
if(i+1 == length)
{
if(0 < i && delayMax == delayTime)
dequeueNFirstRingDatas(delayStruct, i-1);
INFO3(LOG_EVENTS, "delayImpl: dequeueNFirstRingDatas[%d] %g = %g", i, delayMax, delayTime);
return value0;
}
time1 = ((TIME_AND_VALUE*)getRingData(delayStruct, i+1))->time;
value1 = ((TIME_AND_VALUE*)getRingData(delayStruct, i+1))->value;
if(0 < i && delayMax == delayTime)
dequeueNFirstRingDatas(delayStruct, i-1);
}
/* was it an exact match?*/
if(time0 == timeStamp){
Expand All @@ -191,7 +201,6 @@ double delayImpl(DATA* data, int exprNumber, double exprValue, double time, doub
double dt1 = timeStamp - time0;
double retVal = (value0 * dt0 + value1 * dt1) / timedif;
INFO3(LOG_EVENTS, "delayImpl: Linear interpolation of %g between %g and %g", timeStamp, time0, time1);

INFO4(LOG_EVENTS, "delayImpl: Linear interpolation of %g value: %g and %g = %g", timeStamp, value0, value1, retVal);
return retVal;
}
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/simulation/solver/delay.h
Expand Up @@ -58,7 +58,7 @@ extern "C" {

void initDelay(DATA* data, double startTime);
double delayImpl(DATA* data, int exprNumber, double exprValue, double time, double delayTime, double maxDelay);
void storeDelayedExpression(DATA* data, int exprNumber, double exprValue, double time);
void storeDelayedExpression(DATA* data, int exprNumber, double exprValue, double time, double delayTime, double delayMax);

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/c/simulation/solver/solver_main.c
Expand Up @@ -217,6 +217,7 @@ int solver_main(DATA* data, const char* init_initMethod,
saveZeroCrossings(data);
storePreValues(data);
storeOldValues(data);
function_storeDelayed(data);
sim_result_emit(data);
overwriteOldSimulationData(data);

Expand Down

0 comments on commit c18a4bd

Please sign in to comment.