Skip to content

Commit

Permalink
changed implementation of condition variables for zero crossing varia…
Browse files Browse the repository at this point in the history
…bles in cpp runtime

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@11171 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
niklwors committed Feb 22, 2012
1 parent 57e3fac commit c5bda88
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 36 deletions.
102 changes: 70 additions & 32 deletions Compiler/susan_codegen/SimCode/SimCodeCpp.tpl
Expand Up @@ -171,6 +171,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
{
//Number of equations
<%dimension1(simCode)%>
_dimZeroFunc= <%zerocrosslength(simCode)%>;
//Number of residues
<%if modelInfo.labels then
<<
Expand Down Expand Up @@ -1207,7 +1208,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
<%varDecls%>
<%initVariables%>
<%initFunctions%>
<%initZeroCrossings%>
checkConditions(0,true);
<%initTimeEventFunctions%>
<%initEventHandling%>
_event_handling.init(this,<%helpvarlength(simCode)%>);
Expand All @@ -1219,9 +1220,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
<%initALgloopSolvers%>
//initialize equations
<%initBoundParameters%>
_initial=true;
update(ALL);
_initial=false;
saveConditions();
}
>>
end init;
Expand Down Expand Up @@ -3123,10 +3122,13 @@ case SIMCODE(modelInfo = MODELINFO(vars = vars as SIMVARS(__)))
;separator="\n"%>
_event_handling.saveH();
//save all zero crossing condtions
<%saveconditionvar(zeroCrossings,simCode)%>
}
>>
>>
/*
//save all zero crossing condtions
<%saveconditionvar(zeroCrossings,simCode)%>
*/
end saveall;
template initvar(ModelInfo modelInfo,SimCode simCode)
Expand Down Expand Up @@ -4246,7 +4248,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,
let eStart = daeExp(start, contextOther, &preExp, &varDecls /*BUFD*/,simCode)
let eInterval = daeExp(interval, contextOther, &preExp, &varDecls /*BUFD*/,simCode)
let eIndex = daeExp(index, contextOther, &preExp, &varDecls /*BUFD*/,simCode)
'_condition<%eIndex%>'
'_conditions1[<%eIndex%>]'
case CALL(path=IDENT(name="initial") ) then
'initial()'

Expand Down Expand Up @@ -4876,7 +4878,7 @@ match simCode
case SIMCODE(__) then
let size = listLength(zeroCrossings)
<<
return <%size%>
<%size%>
>>
end zerocrosslength;

Expand All @@ -4888,7 +4890,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
<<
int <%lastIdentOfPath(modelInfo.name)%>::getDimZeroFunc()
{
<%zerocrosslength(simCode)%>;
return _dimZeroFunc;
}
>>
end DimZeroFunc;
Expand Down Expand Up @@ -5011,30 +5013,30 @@ template daeExpRelation(Operator op, Integer index,Exp exp1, Exp exp2, Context c
end match
case _ then
match op
case LESS(ty = T_BOOL(__)) then '_condition<%index%>'
case LESS(ty = T_BOOL(__)) then '_conditions1[<%index%>]'
case LESS(ty = T_STRING(__)) then "# string comparison not supported\n"
case LESS(ty = T_INTEGER(__))
case LESS(ty = T_REAL(__)) then '_condition<%index%>'
case GREATER(ty = T_BOOL(__)) then '_condition<%index%>'
case LESS(ty = T_REAL(__)) then '_conditions1[<%index%>]'
case GREATER(ty = T_BOOL(__)) then '_conditions1[<%index%>]'
case GREATER(ty = T_STRING(__)) then "# string comparison not supported\n"
case GREATER(ty = T_INTEGER(__))
case GREATER(ty = T_REAL(__)) then '_condition<%index%>'
case LESSEQ(ty = T_BOOL(__)) then '_condition<%index%>'
case GREATER(ty = T_REAL(__)) then '_conditions1[<%index%>]'
case LESSEQ(ty = T_BOOL(__)) then '_conditions1[<%index%>]'
case LESSEQ(ty = T_STRING(__)) then "# string comparison not supported\n"
case LESSEQ(ty = T_INTEGER(__))
case LESSEQ(ty = T_REAL(__)) then '_condition<%index%>'
case GREATEREQ(ty = T_BOOL(__)) then '_condition<%index%>'
case LESSEQ(ty = T_REAL(__)) then '_conditions1[<%index%>]'
case GREATEREQ(ty = T_BOOL(__)) then '_conditions1[<%index%>]'
case GREATEREQ(ty = T_STRING(__)) then "# string comparison not supported\n"
case GREATEREQ(ty = T_INTEGER(__))
case GREATEREQ(ty = T_REAL(__)) then '_condition<%index%>'
case EQUAL(ty = T_BOOL(__)) then '_condition<%index%>'
case GREATEREQ(ty = T_REAL(__)) then '_conditions1[<%index%>]'
case EQUAL(ty = T_BOOL(__)) then '_conditions1[<%index%>]'
case EQUAL(ty = T_STRING(__))
case EQUAL(ty = T_INTEGER(__))
case EQUAL(ty = T_REAL(__)) then '_condition<%index%>'
case NEQUAL(ty = T_BOOL(__)) then '_condition<%index%>'
case EQUAL(ty = T_REAL(__)) then '_conditions1[<%index%>]'
case NEQUAL(ty = T_BOOL(__)) then '_conditions1[<%index%>]'
case NEQUAL(ty = T_STRING(__))
case NEQUAL(ty = T_INTEGER(__))
case NEQUAL(ty = T_REAL(__)) then '_condition<%index%>'
case NEQUAL(ty = T_REAL(__)) then '_conditions1[<%index%>]'
case _ then "daeExpRelation:ERR"
end match
end daeExpRelation;
Expand Down Expand Up @@ -5707,7 +5709,7 @@ template checkConditions2(Integer index1, Exp relation, Text &varDecls /*BUFP*/,
if(index==<%zerocrossingIndex%> || all)
{
<%res%>=(<%e1%><%op%><%e2%>);
_condition<%zerocrossingIndex%>=<%res%>;
_conditions1[<%zerocrossingIndex%>]=<%res%>;
}
>>
end checkConditions2;
Expand All @@ -5726,14 +5728,15 @@ template handleSystemEvents(list<ZeroCrossing> zeroCrossings,list<SimWhenClause>
int iter=0;
while(restart && !(iter++ > 10))
{
<%zeroCrossingsCode%>
double h[<%helpvarlength(simCode)%>];
<%helpvarvector(whenClauses,simCode)%>
_event_handling.setHelpVars(h);
//iterate and handle all events inside the eventqueue
restart=_event_handling.IterateEventQueue(events,update_event);
}
saveAll();
<%zeroCrossingsCode%>
double h[<%helpvarlength(simCode)%>];
<%helpvarvector(whenClauses,simCode)%>
_event_handling.setHelpVars(h);
//iterate and handle all events inside the eventqueue
restart=_event_handling.IterateEventQueue(events,update_event);
}
saveConditions();
resetTimeEvents();
if(iter>10){
throw std::runtime_error("Number of event iteration steps exceeded. ");}
Expand Down Expand Up @@ -5800,6 +5803,35 @@ template giveZeroFunc2(list<ZeroCrossing> zeroCrossings, Text &varDecls /*BUFP*/
;separator="\n")
end giveZeroFunc2;

template giveZeroFunc3(Integer index1, Exp relation, Text &varDecls /*BUFP*/,SimCode simCode)
::=
let &preExp = buffer "" /*BUFD*/
match relation
case rel as RELATION(index=zerocrossingIndex) then
let e1 = daeExp(exp1, contextOther, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
let e2 = daeExp(exp2, contextOther, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
match rel.operator

case LESS(__)
case LESSEQ(__) then
<<
if(_conditions0[<%zerocrossingIndex%>])
f[<%index1%>]=(<%e1%>-EPSILON-<%e2%>);
else
f[<%index1%>]=(<%e2%>-<%e1%>-EPSILON);
>>
case GREATER(__)
case GREATEREQ(__) then
<<
if(_conditions0[<%zerocrossingIndex%>])
f[<%index1%>]=(<%e2%>-<%e1%>-EPSILON);
else
f[<%index1%>]=(<%e1%>-EPSILON-<%e2%>);
>>
end match
end giveZeroFunc3;

/*
template giveZeroFunc3(Integer index1, Exp relation, Text &varDecls /*BUFP*/,SimCode simCode)
::=
let &preExp = buffer "" /*BUFD*/
Expand Down Expand Up @@ -5827,7 +5859,8 @@ template giveZeroFunc3(Integer index1, Exp relation, Text &varDecls /*BUFP*/,Sim
>>
end match
end giveZeroFunc3;
/*+sgn(<%e2%>-<%e1%>)*eps*/
*/

template conditionvarZero(list<ZeroCrossing> zeroCrossings,SimCode simCode)
::=
(zeroCrossings |> ZERO_CROSSING(__) hasindex i0 =>
Expand Down Expand Up @@ -5885,10 +5918,15 @@ template conditionvariable(list<ZeroCrossing> zeroCrossings,list<SampleCondition
::=
let conditionvariable = conditionvarZero(zeroCrossings,simCode)
let conditionvarsample = conditionvarSample(sampleConditions,simCode)
/*
<<
<%conditionvariable%>
<%conditionvarsample%>
>>
*/
<<
<%conditionvarsample%>
>>
end conditionvariable;

template expTypeFromExpFlag(Exp exp, Integer flag)
Expand Down
Expand Up @@ -12,6 +12,8 @@ SystemDefaultImplementation::SystemDefaultImplementation()
, _z (NULL)
, _zDot (NULL)
, _dimODE (0)
,_conditions0(NULL)
,_conditions1(NULL)
{
}

Expand Down Expand Up @@ -88,8 +90,20 @@ int SystemDefaultImplementation::getDimRHS(const IContinous::INDEX index) const
memset(_z,0,(_dimODE + _dimAE)*sizeof(double));
memset(_zDot,0,(_dimODE + _dimAE)*sizeof(double));
}
if(_dimZeroFunc > 0)
{
if(_conditions0) delete [] _conditions0 ;
if(_conditions1) delete [] _conditions1;
_conditions0 = new bool[_dimZeroFunc];
_conditions1 = new bool[_dimZeroFunc];
memset(_conditions0,false,(_dimZeroFunc)*sizeof(bool));
memset(_conditions1,false,(_dimZeroFunc)*sizeof(bool));
}
};

void SystemDefaultImplementation::saveConditions()
{
memcpy(_conditions0,_conditions1,_dimZeroFunc*sizeof(bool));
}

/// Set current integration time
void SystemDefaultImplementation::setTime(const double& t)
Expand Down
Expand Up @@ -64,7 +64,7 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation

/// Provide the right hand side (according to the index)
void giveRHS(double* f, const IContinous::INDEX index = IContinous::ALL_VARS);

void saveConditions();
// Member variables
//---------------------------------------------------------------
protected:
Expand All @@ -77,15 +77,18 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation
double
*_z, ///< "Extended state vector", containing all states and algebraic variables of all types
*_zDot; ///< "Extended vector of derivatives", containing all right hand sides of differential and algebraic equations

bool
* _conditions0,
* _conditions1;
ostream
*_outputStream; ///< Output stream for results

int
_dimODE1stOrder, ///< Number (dimension) of first order ordinary differential equations
_dimODE2ndOrder, ///< Number (dimension) of second order ordinary differential equations (RHS of a mechanical system)
_dimResidues, ///< Number of residues
_dimAE; ///< Number (dimension) of algebraic equations (e.g. constraints from an algebraic loop)
_dimAE, ///< Number (dimension) of algebraic equations (e.g. constraints from an algebraic loop)
_dimZeroFunc; ///< Number of Zero crossings
bool _initial;

private:
Expand Down

0 comments on commit c5bda88

Please sign in to comment.