Skip to content

Commit

Permalink
- added initial steps to support BackendDAE.IF_EQUATION
Browse files Browse the repository at this point in the history
   - added missing case in most BackendDAE* files
   - added a new SimCode equation type SES_IFEQUATION
   - added cases to suppport this equation in SimCodeUtil
   - added one first test case for them

 - some more of the AVM models should now able to simulate


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14470 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Dec 19, 2012
1 parent 9cc08ba commit b7c0356
Show file tree
Hide file tree
Showing 13 changed files with 622 additions and 101 deletions.
5 changes: 5 additions & 0 deletions Compiler/BackEnd/BackendDAE.mo
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@ uniontype StrongComponent
list<Integer> vars "be carefule with states, this are solved for der(x)";
end SINGLEWHENEQUATION;

record SINGLEIFEQUATION
Integer eqn;
list<Integer> vars "be carefule with states, this are solved for der(x)";
end SINGLEIFEQUATION;

record TORNSYSTEM
list<Integer> tearingvars;
list<Integer> residualequations;
Expand Down
78 changes: 61 additions & 17 deletions Compiler/BackEnd/BackendDAECreate.mo
Original file line number Diff line number Diff line change
Expand Up @@ -3255,9 +3255,11 @@ algorithm
end matchcontinue;
end updateStatesVars;

/*
* Section with all functions for findZeroCrossings
*/
// =============================================================================
//
// Section for find zero crossings in BackendDAE
// =============================================================================

public function findZeroCrossings "function: findZeroCrossings
This function finds all zerocrossings in the list of equations and
the list of when clauses."
Expand Down Expand Up @@ -3481,27 +3483,20 @@ algorithm
(res1,eq_reslst,wc_reslst,countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars,xs,eq_count, {}, 0,countRelations, countMathFunctions,res,relationsLst, sampleLst);
then
(res1,BackendDAE.RESIDUAL_EQUATION(eres1,source_,diffed)::eq_reslst,wc_reslst,countRelations, countMathFunctions,relationsLst, sampleLst);
/*
case (v,_,((e as BackendDAE.IF_EQUATION(conditions=conds as {DAE.CALL(path=Absyn.IDENT("initial"))},eqnstrue={el},eqnsfalse=eqnselse, source= source_)) :: xs),eq_count,{},_,countRelations, countMathFunctions,zcs,relationsLst, sampleLst)
equation
(res1,eq_reslst,wc_reslst,countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars,eqnselse,eq_count, {}, 0,countRelations, countMathFunctions,zcs,relationsLst, sampleLst);
(res1,eq_reslst,wc_reslst,countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars,xs,eq_count, {}, 0,countRelations, countMathFunctions,zcs,relationsLst, sampleLst);
then
(res1,BackendDAE.IF_EQUATION(conds,{el},el,source_)::eq_reslst,wc_reslst,countRelations, countMathFunctions,relationsLst, sampleLst);
*/
case (v,_,((e as BackendDAE.IF_EQUATION(conditions=conds, eqnstrue=eqnslst, eqnsfalse=el, source= source_)) :: xs),eq_count,{},_,countRelations, countMathFunctions,zcs,relationsLst, sampleLst)

case (v,_,((e as BackendDAE.IF_EQUATION(conditions=_)) :: xs),eq_count,{},_,countRelations, countMathFunctions,zcs,relationsLst, sampleLst)
equation
print("Warning If equations not handled propper in findZeroCrossings2\n");
eq_count = eq_count + 1;
(res1,eq_reslst,wc_reslst,countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars,xs,eq_count, {}, 0,countRelations, countMathFunctions,zcs,relationsLst, sampleLst);
(e, countRelations, countMathFunctions, res,relationsLst, sampleLst) = findZeroCrossingsIfEqns(e,zcs,relationsLst,sampleLst,countRelations, countMathFunctions,eq_count,-1,v,knvars);
(res1,eq_reslst,wc_reslst,countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars,xs,eq_count, {}, 0, countRelations, countMathFunctions, res, relationsLst, sampleLst);
then
(res1,BackendDAE.IF_EQUATION(conds,eqnslst,el,source_)::eq_reslst,wc_reslst,countRelations, countMathFunctions,relationsLst, sampleLst);
(res1,e::eq_reslst,wc_reslst,countRelations, countMathFunctions,relationsLst, sampleLst);

// let when equation pass they are discrete and can't contain ZeroCrossings
case (v,_,(e :: xs),eq_count,{},_,countRelations, countMathFunctions,res,relationsLst, sampleLst)
equation
eq_count = eq_count + 1;
(res1,eq_reslst,wc_reslst,countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars,xs,eq_count, {}, 0,countRelations, countMathFunctions,res,relationsLst, sampleLst);
(res1,eq_reslst,wc_reslst,countRelations, countMathFunctions, relationsLst, sampleLst) = findZeroCrossings2(v, knvars,xs,eq_count, {}, 0, countRelations, countMathFunctions, res, relationsLst, sampleLst);
then
(res1,e::eq_reslst,wc_reslst,countRelations, countMathFunctions,relationsLst, sampleLst);
end matchcontinue;
Expand Down Expand Up @@ -3550,6 +3545,55 @@ algorithm
end match;
end findZeroCrossingsWhenEqns;

protected function findZeroCrossingsIfEqns
"function: findZeroCrossingsIfEqns
Helper function to findZeroCrossing."
input BackendDAE.Equation inIfEqn;
input list<BackendDAE.ZeroCrossing> inZeroCrossings;
input list<BackendDAE.ZeroCrossing> inrelationsinZC;
input list<BackendDAE.ZeroCrossing> inSamplesLst;
input Integer incountRelations;
input Integer incountMathFunctions;
input Integer counteq;
input Integer countwc;
input BackendDAE.Variables vars;
input BackendDAE.Variables knvars;
output BackendDAE.Equation oIfEqn;
output Integer outcountRelations;
output Integer outcountMathFunctions;
output list<BackendDAE.ZeroCrossing> outZeroCrossings;
output list<BackendDAE.ZeroCrossing> outrelationsinZC;
output list<BackendDAE.ZeroCrossing> outSamplesLst;
algorithm
(oIfEqn,outcountRelations,outcountMathFunctions,outZeroCrossings,outrelationsinZC,outSamplesLst) :=
match(inIfEqn,inZeroCrossings,inrelationsinZC,inSamplesLst,incountRelations,incountMathFunctions,counteq,countwc,vars,knvars)
local
DAE.Exp condition,e;
list<DAE.Exp> conditions, restconditions;
BackendDAE.Equation ifeqn;
list<BackendDAE.Equation> eqnstrue, elseeqns;
list<list<BackendDAE.Equation>> eqnsTrueLst, resteqns;
list<BackendDAE.ZeroCrossing> zc, relations, samples;
Integer countRelations,countMathFunctions;
DAE.ElementSource source_;
case (BackendDAE.IF_EQUATION(conditions={}, eqnstrue={}, eqnsfalse=elseeqns, source=source_),_,_,_,_,_,_,_,_,_)
equation
(zc, elseeqns, _, countRelations, countMathFunctions, relations, samples) = findZeroCrossings2(vars, knvars, elseeqns, counteq, {}, countwc, incountRelations, incountMathFunctions, inZeroCrossings,inrelationsinZC,inSamplesLst);
then
(BackendDAE.IF_EQUATION({}, {}, elseeqns, source_), countRelations, countMathFunctions, zc, relations, samples);
case (BackendDAE.IF_EQUATION(conditions=condition::restconditions, eqnstrue=eqnstrue::resteqns, eqnsfalse=elseeqns, source=source_),_,_,_,_,_,_,_,_,_)
equation
(condition, countRelations, countMathFunctions, zc, relations, samples) = findZeroCrossings3(condition, inZeroCrossings, inrelationsinZC, inSamplesLst, incountRelations, incountMathFunctions,counteq,countwc,vars,knvars);
(zc, eqnstrue, _, countRelations, countMathFunctions, relations, samples) = findZeroCrossings2(vars, knvars, eqnstrue, counteq, {}, countwc, countRelations, countMathFunctions, zc, relations, samples);
ifeqn = BackendDAE.IF_EQUATION(restconditions, resteqns, elseeqns, source_);
(BackendDAE.IF_EQUATION(conditions=conditions, eqnstrue=eqnsTrueLst, eqnsfalse=elseeqns, source=source_), countRelations, countMathFunctions, zc, relations, samples) = findZeroCrossingsIfEqns(ifeqn, zc, relations, samples, countRelations, countMathFunctions, counteq, countwc, vars, knvars);
conditions = listAppend({condition},conditions);
eqnsTrueLst = listAppend({eqnstrue},eqnsTrueLst);
then
(BackendDAE.IF_EQUATION(conditions, eqnsTrueLst, elseeqns, source_), countRelations, countMathFunctions, zc, relations, samples);
end match;
end findZeroCrossingsIfEqns;

protected function findZeroCrossings3
"function: findZeroCrossings3
Helper function to findZeroCrossing."
Expand Down Expand Up @@ -3613,7 +3657,7 @@ algorithm
Debug.fcall(Flags.RELIDX, print, "discrete LUNARY: " +& intString(numRelations) +& "\n");
//Debug.fcall(Flags.RELIDX, BackendDump.debugExpStr, (e, "\n"));
then
((e,true,((zeroCrossings,relations,samples,numRelations, numMathFunctions),(eq_count,wc_count,vars,knvars))));
((e,true,((zeroCrossings,relations,samples,numRelations, numMathFunctions),(eq_count,wc_count,vars,knvars))));
case (((e as DAE.LBINARY(exp1 = e1,operator = op,exp2 = e2)),((zeroCrossings,relations,samples,numRelations, numMathFunctions),(eq_count,wc_count,vars,knvars))))
equation
true = BackendDAEUtil.isDiscreteExp(e1, vars, knvars);
Expand Down
32 changes: 32 additions & 0 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,14 @@ algorithm
comps = List.consOnTrue(intGt(listLength(eqns),0), comp, iAcc);
then
comps;
case (BackendDAE.SINGLEIFEQUATION(eqn=e,vars=vars),_,_,_)
equation
e = eqnindxs[e];
vars = List.map1r(vars,arrayGet,varindxs);
vars = List.select1(vars,intGt,0);
comps = List.consOnTrue(intGt(e,0), BackendDAE.SINGLEIFEQUATION(e,vars), iAcc);
then
comps;
case (BackendDAE.SINGLEARRAY(eqn=e,vars=vars),_,_,_)
equation
e = eqnindxs[e];
Expand Down Expand Up @@ -3012,6 +3020,9 @@ algorithm
case (BackendDAE.SINGLEARRAY(eqn=e))
then
{e};
case (BackendDAE.SINGLEIFEQUATION(eqn=e))
then
{e};
case (BackendDAE.SINGLEALGORITHM(eqn=e))
then
{e};
Expand Down Expand Up @@ -5022,6 +5033,21 @@ algorithm
*/
result = getSparsePattern(rest,result,inMatrix,inMatrixT);
then result;
case(BackendDAE.SINGLEIFEQUATION(eqn=eqn,vars=vars)::rest,result,_,_)
equation
//print("SINGLEARRAY update: ");
rowElementsList = List.map1(vars, Util.arrayGetIndexFirst, inMatrixT);
rowElements = List.unionList(rowElementsList);
eqnlst = arrayGet(result, eqn);
List.map2_0(rowElements, Util.arrayUpdateElementListUnion, eqnlst, result);
/*
BackendDump.dumpIncidenceRow(rowElements);
dumpList = List.map1(rowElements, Util.arrayGetIndexFirst, result);
List.map_0(dumpList, BackendDump.dumpIncidenceRow);
print("\n\n");
*/
result = getSparsePattern(rest,result,inMatrix,inMatrixT);
then result;
case(BackendDAE.SINGLEALGORITHM(eqn=eqn,vars=vars)::rest,result,_,_)
equation
//print("SINGLEALGORITHM update: ");
Expand Down Expand Up @@ -10551,6 +10577,12 @@ algorithm
then
countOperationstraverseComps(rest,isyst,ishared,tpl);
case (BackendDAE.SINGLEARRAY(eqn=e)::rest,_,_,_)
equation
eqn = BackendDAEUtil.equationNth(BackendEquation.daeEqns(isyst), e-1);
(_,tpl) = BackendEquation.traverseBackendDAEExpsEqn(eqn,countOperationsExp,inTpl);
then
countOperationstraverseComps(rest,isyst,ishared,tpl);
case (BackendDAE.SINGLEIFEQUATION(eqn=e)::rest,_,_,_)
equation
eqn = BackendDAEUtil.equationNth(BackendEquation.daeEqns(isyst), e-1);
(_,tpl) = BackendEquation.traverseBackendDAEExpsEqn(eqn,countOperationsExp,inTpl);
Expand Down
15 changes: 15 additions & 0 deletions Compiler/BackEnd/BackendDAETransform.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,11 @@ algorithm
varindxs = List.map(var_varindx_lst,Util.tuple22);
then
BackendDAE.SINGLEARRAY(compelem,varindxs);
case (compelem::{},BackendDAE.IF_EQUATION(conditions = _)::{},var_varindx_lst,_,_,_,_,false)
equation
varindxs = List.map(var_varindx_lst,Util.tuple22);
then
BackendDAE.SINGLEIFEQUATION(compelem,varindxs);
case (compelem::{},BackendDAE.COMPLEX_EQUATION(size=_)::{},var_varindx_lst,_,_,_,_,false)
equation
varindxs = List.map(var_varindx_lst,Util.tuple22);
Expand Down Expand Up @@ -1306,6 +1311,13 @@ algorithm
then
(eqnlst,varlst,e);
case (BackendDAE.SINGLEARRAY(eqn=e,vars=vlst),eqns,vars)
equation
e_1 = e - 1;
eqn = BackendDAEUtil.equationNth(eqns, e_1);
varlst = List.map1r(vlst, BackendVariable.getVarAt, vars);
then
({eqn},varlst,e);
case (BackendDAE.SINGLEIFEQUATION(eqn=e,vars=vlst),eqns,vars)
equation
e_1 = e - 1;
eqn = BackendDAEUtil.equationNth(eqns, e_1);
Expand Down Expand Up @@ -1423,6 +1435,9 @@ algorithm
case BackendDAE.SINGLEARRAY(eqn=e,vars=vlst)
then
({e},vlst);
case BackendDAE.SINGLEIFEQUATION(eqn=e,vars=vlst)
then
({e},vlst);
case BackendDAE.SINGLEALGORITHM(eqn=e,vars=vlst)
then
({e},vlst);
Expand Down

0 comments on commit b7c0356

Please sign in to comment.