Skip to content

Commit

Permalink
- fix bug #2403 (quite many changes, do make clean)
Browse files Browse the repository at this point in the history
- properly count tuple equation size in the BackEnd
  BackendDAE.ALGORITHM gets a new component called expand (DAE.Expand)
  which tells that this algorithm was translated from an equation as
  in this case we SHOULD NOT expand array crefs to the full size
- added test for bug #2403
  testsuite/simulation/modelica/algorithms_functions/
    ModelicaTest.Fluid.Dissipation.Verifications.HeatTransfer.Channel.kc_evenGapLaminar.mos
- fix counting of tuples in CheckModel.mo

More unrelated fixes:
- add recursion limit depth in Global.mo and display it in the warnings and errors about recursion limit
- fix typing of record bindings for R a[3](eta = {eta[1], eta[2], eta[3]})
- if function path is longer than 50 chars minimize it by keeping the first
  ident.lastIdent.uniqueID (see CevalScript.generateFunctionFileName)
- List.applyAndFold1 -> remove restriction of extra argument to be the same type of the list element.
- add buildModel to ModelicaBuiltin.mo


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@17556 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Oct 5, 2013
1 parent 64bf40e commit 916b920
Show file tree
Hide file tree
Showing 33 changed files with 1,191 additions and 726 deletions.
2 changes: 2 additions & 0 deletions Compiler/BackEnd/BackendDAE.mo
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public import SCode;
public import Values;
public import HashTable3;
public import HashTableCG;
public import CheckModel;

public
type Type = .DAE.Type
Expand Down Expand Up @@ -228,6 +229,7 @@ uniontype Equation
Integer size "size of equation" ;
.DAE.Algorithm alg;
.DAE.ElementSource source "origin of algorithm";
.DAE.Expand expand "this algorithm was translated from an equation. we should not expand array crefs!";
end ALGORITHM;

record WHEN_EQUATION
Expand Down
113 changes: 70 additions & 43 deletions Compiler/BackEnd/BackendDAECreate.mo

Large diffs are not rendered by default.

59 changes: 36 additions & 23 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Original file line number Diff line number Diff line change
Expand Up @@ -3821,6 +3821,7 @@ algorithm
list<list<DAE.Subscript>> subslst;
list<BackendDAE.Var> solvedvars;
String dumpBuffer;
DAE.Expand crefExpand;

case(currEquation as BackendDAE.WHEN_EQUATION(size=_),_, vars, functions, inputVars, paramVars, stateVars, knownVars, _, _, _) equation
Debug.fcall(Flags.JAC_WARNINGS, print,"BackendDAEOptimize.derive: WHEN_EQUATION has been removed.\n");
Expand Down Expand Up @@ -3861,10 +3862,10 @@ algorithm
derivedEqns = List.map1(exps, createResidualEqn, source);
then derivedEqns;

case(currEquation as BackendDAE.ALGORITHM(size=size,alg=alg, source=source),_, vars, functions, inputVars, paramVars, stateVars, knownVars, _, _, _)
case(currEquation as BackendDAE.ALGORITHM(size=size, alg=alg, source=source, expand = crefExpand),_, vars, functions, inputVars, paramVars, stateVars, knownVars, _, _, _)
equation
algs = deriveOneAlg(alg,vars,functions,inputVars,paramVars,stateVars,knownVars,inorderedVars,0,inDiffVars,inMatrixName);
derivedEqns = List.map2(algs, createAlgorithmEqn, size, source);
derivedEqns = List.map3(algs, createAlgorithmEqn, size, source, crefExpand);
then derivedEqns;

case(currEquation as BackendDAE.COMPLEX_EQUATION(source=_),_, vars, functions, inputVars, paramVars, stateVars, knownVars, _, _, _) equation
Expand Down Expand Up @@ -3940,9 +3941,10 @@ protected function createAlgorithmEqn
input DAE.Algorithm Alg;
input Integer Size;
input DAE.ElementSource Source;
input DAE.Expand inCrefExpand;
output BackendDAE.Equation outEqn;
algorithm
outEqn := BackendDAE.ALGORITHM(Size, Alg, Source);
outEqn := BackendDAE.ALGORITHM(Size, Alg, Source, inCrefExpand);
end createAlgorithmEqn;

public function differentiateVarWithRespectToX "author: lochel"
Expand Down Expand Up @@ -6437,34 +6439,41 @@ algorithm
list<BackendDAE.Equation> eqns,beqns;
Integer size;
DAE.ElementSource source,source1;
DAE.Expand crefExpand;

case ({},_,_,_,_)
then
(listReverse(brancheqns1),inEqns);
case (BackendDAE.ALGORITHM(size=size,alg=DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(cond=cond,msg=msg,level=level,source=source1)}),source=source)::eqns,NONE(),_,_,_)

case (BackendDAE.ALGORITHM(size=size,alg=DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(cond=cond,msg=msg,level=level,source=source1)}),source=source, expand=crefExpand)::eqns,NONE(),_,_,_)
equation
e = List.fold(conditions,makeIfExp,cond);
(beqns,eqns) = simplifyIfEquationAsserts1(eqns,condition,conditions,brancheqns1,BackendDAE.ALGORITHM(size,DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(e,msg,level,source1)}),source)::inEqns);
(beqns,eqns) = simplifyIfEquationAsserts1(eqns,condition,conditions,brancheqns1,BackendDAE.ALGORITHM(size,DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(e,msg,level,source1)}),source,crefExpand)::inEqns);
then
(beqns,eqns);
case (BackendDAE.ALGORITHM(size=size,alg=DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(cond=cond,msg=msg,level=level,source=source1)}),source=source)::eqns,SOME(e),_,_,_)

case (BackendDAE.ALGORITHM(size=size,alg=DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(cond=cond,msg=msg,level=level,source=source1)}),source=source,expand=crefExpand)::eqns,SOME(e),_,_,_)
equation
e = DAE.IFEXP(e,cond,DAE.BCONST(true));
e = List.fold(conditions,makeIfExp,e);
(beqns,eqns) = simplifyIfEquationAsserts1(eqns,condition,conditions,brancheqns1,BackendDAE.ALGORITHM(size,DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(e,msg,level,source1)}),source)::inEqns);
(beqns,eqns) = simplifyIfEquationAsserts1(eqns,condition,conditions,brancheqns1,BackendDAE.ALGORITHM(size,DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(e,msg,level,source1)}),source,crefExpand)::inEqns);
then
(beqns,eqns);
case (BackendDAE.ALGORITHM(size=size,alg=DAE.ALGORITHM_STMTS({DAE.STMT_TERMINATE(msg=msg,source=source1)}),source=source)::eqns,NONE(),_,_,_)

case (BackendDAE.ALGORITHM(size=size,alg=DAE.ALGORITHM_STMTS({DAE.STMT_TERMINATE(msg=msg,source=source1)}),source=source,expand=crefExpand)::eqns,NONE(),_,_,_)
equation
e = List.fold(conditions,makeIfExp,DAE.BCONST(true));
(beqns,eqns) = simplifyIfEquationAsserts1(eqns,condition,conditions,brancheqns1,BackendDAE.ALGORITHM(size,DAE.ALGORITHM_STMTS({DAE.STMT_IF(e,{DAE.STMT_TERMINATE(msg,source1)},DAE.NOELSE(),source1)}),source)::inEqns);
(beqns,eqns) = simplifyIfEquationAsserts1(eqns,condition,conditions,brancheqns1,BackendDAE.ALGORITHM(size,DAE.ALGORITHM_STMTS({DAE.STMT_IF(e,{DAE.STMT_TERMINATE(msg,source1)},DAE.NOELSE(),source1)}),source,crefExpand)::inEqns);
then
(beqns,eqns);
case (BackendDAE.ALGORITHM(size=size,alg=DAE.ALGORITHM_STMTS({DAE.STMT_TERMINATE(msg=msg,source=source1)}),source=source)::eqns,SOME(e),_,_,_)

case (BackendDAE.ALGORITHM(size=size,alg=DAE.ALGORITHM_STMTS({DAE.STMT_TERMINATE(msg=msg,source=source1)}),source=source,expand=crefExpand)::eqns,SOME(e),_,_,_)
equation
e = List.fold(conditions,makeIfExp,e);
(beqns,eqns) = simplifyIfEquationAsserts1(eqns,condition,conditions,brancheqns1,BackendDAE.ALGORITHM(size,DAE.ALGORITHM_STMTS({DAE.STMT_IF(e,{DAE.STMT_TERMINATE(msg,source1)},DAE.NOELSE(),source1)}),source)::inEqns);
(beqns,eqns) = simplifyIfEquationAsserts1(eqns,condition,conditions,brancheqns1,BackendDAE.ALGORITHM(size,DAE.ALGORITHM_STMTS({DAE.STMT_IF(e,{DAE.STMT_TERMINATE(msg,source1)},DAE.NOELSE(),source1)}),source,crefExpand)::inEqns);
then
(beqns,eqns);

case (eqn::eqns,_,_,_,_)
equation
(beqns,eqns) = simplifyIfEquationAsserts1(eqns,condition,conditions,eqn::brancheqns1,inEqns);
Expand Down Expand Up @@ -7656,12 +7665,15 @@ algorithm
list<DAE.Exp> outputs;
DAE.ElementSource source;
list<DAE.ComponentRef> crlst;
DAE.Expand crExpand;

case((eqn as BackendDAE.ALGORITHM(size=size, alg=alg as DAE.ALGORITHM_STMTS(statements), source=source), vars)) equation
crlst = CheckModel.algorithmOutputs(alg);
outputs = List.map(crlst, Expression.crefExp);
statements = expandAlgorithmStmts(statements, outputs, vars);
then ((BackendDAE.ALGORITHM(size, DAE.ALGORITHM_STMTS(statements), source), vars));
case((eqn as BackendDAE.ALGORITHM(size=size, alg=alg as DAE.ALGORITHM_STMTS(statements), source=source, expand=crExpand), vars))
equation
crlst = CheckModel.algorithmOutputs(alg, crExpand);
outputs = List.map(crlst, Expression.crefExp);
statements = expandAlgorithmStmts(statements, outputs, vars);
then
((BackendDAE.ALGORITHM(size, DAE.ALGORITHM_STMTS(statements), source, crExpand), vars));

else
then inTpl;
Expand Down Expand Up @@ -7888,6 +7900,7 @@ algorithm
DAE.Algorithm alg_;
list<DAE.Statement> stmts, preStmts;
HashTableExpToIndex.HashTable ht;
DAE.Expand crefExpand;

// when equation
case ((BackendDAE.WHEN_EQUATION(size=size, whenEquation=whenEquation, source=source), (equationArray, vars, eqns, index, ht))) equation
Expand All @@ -7899,24 +7912,24 @@ algorithm
then ((eqn, (equationArray, vars, eqns, index, ht)));

// removed algorithm
case ((BackendDAE.ALGORITHM(size=0, alg=alg_, source=source), (equationArray, vars, eqns, index, ht))) equation
case ((BackendDAE.ALGORITHM(size=0, alg=alg_, source=source, expand=crefExpand), (equationArray, vars, eqns, index, ht))) equation
DAE.ALGORITHM_STMTS(statementLst=stmts) = alg_;
size = -index;
(stmts, preStmts, vars1, index) = encapsulateWhenConditionsForAlgorithms(stmts, vars, index);
sizePre = listLength(preStmts);
size = size+index-sizePre;

alg_ = DAE.ALGORITHM_STMTS(stmts);
eqn = BackendDAE.ALGORITHM(size, alg_, source);
eqn = BackendDAE.ALGORITHM(size, alg_, source, crefExpand);
equationArray = BackendEquation.addEquations({eqn}, equationArray);

alg_ = DAE.ALGORITHM_STMTS(preStmts);
eqn2 = BackendDAE.ALGORITHM(sizePre, alg_, source);
eqn2 = BackendDAE.ALGORITHM(sizePre, alg_, source, crefExpand);
eqns = Util.if_(intGt(sizePre, 0), eqn2::eqns, eqns);
then ((eqn, (equationArray, vars1, eqns, index, ht)));

// algorithm
case ((BackendDAE.ALGORITHM(size=size, alg=alg_, source=source), (equationArray, vars, eqns, index, ht))) equation
case ((BackendDAE.ALGORITHM(size=size, alg=alg_, source=source, expand=crefExpand), (equationArray, vars, eqns, index, ht))) equation
DAE.ALGORITHM_STMTS(statementLst=stmts) = alg_;
size = size-index;
(stmts, preStmts, vars1, index) = encapsulateWhenConditionsForAlgorithms(stmts, vars, index);
Expand All @@ -7925,7 +7938,7 @@ algorithm
stmts = listAppend(preStmts, stmts);

alg_ = DAE.ALGORITHM_STMTS(stmts);
eqn = BackendDAE.ALGORITHM(size, alg_, source);
eqn = BackendDAE.ALGORITHM(size, alg_, source, crefExpand);
equationArray = BackendEquation.addEquations({eqn}, equationArray);
then ((eqn, (equationArray, vars1, eqns, index, ht)));

Expand Down Expand Up @@ -8114,7 +8127,7 @@ algorithm
(conditions, initialCall) = getConditionList(condition);
vars = listAppend(vars, inVars);

{} = CheckModel.algorithmStatementListOutputs({DAE.STMT_WHEN(condition, conditions, initialCall, stmts1, NONE(), source)});
{} = CheckModel.algorithmStatementListOutputs({DAE.STMT_WHEN(condition, conditions, initialCall, stmts1, NONE(), source)}, DAE.EXPAND());

(stmts, preStmts2, vars, index) = encapsulateWhenConditionsForAlgorithms(rest, vars, index);
preStmts = listAppend(preStmts, preStmts2);
Expand Down Expand Up @@ -8143,7 +8156,7 @@ algorithm
preStmts2 = List.stripLast(elseWhenList);
preStmts = listAppend(preStmts, preStmts2);

{} = CheckModel.algorithmStatementListOutputs({DAE.STMT_WHEN(condition, conditions, initialCall, stmts1, SOME(elseWhen), source)});
{} = CheckModel.algorithmStatementListOutputs({DAE.STMT_WHEN(condition, conditions, initialCall, stmts1, SOME(elseWhen), source)}, DAE.EXPAND());

(stmts, preStmts2, vars, index) = encapsulateWhenConditionsForAlgorithms(rest, vars, index);
preStmts = listAppend(preStmts, preStmts2);
Expand Down
28 changes: 24 additions & 4 deletions Compiler/BackEnd/BackendDAETransform.mo
Original file line number Diff line number Diff line change
Expand Up @@ -415,33 +415,40 @@ algorithm
list<DAE.ComponentRef> crlst;
list<String> slst;
Boolean jacConstant;

case (compelem::{},BackendDAE.ALGORITHM(size = _)::{},var_varindx_lst,_,_,_,_,false)
equation
varindxs = List.map(var_varindx_lst,Util.tuple22);
then
BackendDAE.SINGLEALGORITHM(compelem,varindxs);

case (compelem::{},BackendDAE.ARRAY_EQUATION(dimSize = _)::{},var_varindx_lst,_,_,_,_,false)
equation
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);
then
BackendDAE.SINGLECOMPLEXEQUATION(compelem,varindxs);

case (compelem::{},BackendDAE.WHEN_EQUATION(size=_)::{},var_varindx_lst,_,_,_,_,false)
equation
varindxs = List.map(var_varindx_lst,Util.tuple22);
then
BackendDAE.SINGLEWHENEQUATION(compelem,varindxs);

case (compelem::{},_,(_,v)::{},_,_,_,ass2,false)
then BackendDAE.SINGLEEQUATION(compelem,v);

case (comp,eqn_lst,var_varindx_lst,syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),shared,ass1,ass2,false)
equation
var_lst = List.map(var_varindx_lst,Util.tuple21);
Expand All @@ -453,6 +460,7 @@ algorithm
sc = analyseStrongComponentBlock(indxcont_eqn,cont_eqn,var_varindx_lst_cond,syst,shared,ass1,ass2,true);
then
BackendDAE.MIXEDEQUATIONSYSTEM(sc,indxdisc_eqn,indxdisc_var);

case (comp,eqn_lst,var_varindx_lst,syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),shared,ass1,ass2,_)
equation
var_lst = List.map(var_varindx_lst,Util.tuple21);
Expand All @@ -473,6 +481,7 @@ algorithm
true = analyzeConstantJacobian(jacConstant,jac,arrayLength(mt),var_lst,eqn_lst,shared);
then
BackendDAE.EQUATIONSYSTEM(comp,varindxs,jac,jac_tp);

case (comp,eqn_lst,var_varindx_lst,syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),shared,ass1,ass2,_)
equation
var_lst = List.map(var_varindx_lst,Util.tuple21);
Expand All @@ -488,11 +497,13 @@ Sorry - Support for Discrete Equation Systems is not yet implemented\n";
Error.addMessage(Error.INTERNAL_ERROR, {msg});
then
fail();

else
equation
Error.addMessage(Error.INTERNAL_ERROR, {"./Compiler/BackEnd/BackendDAETransform.mo: function analyseStrongComponentBlock failed"});
then
fail();

end matchcontinue;
end analyseStrongComponentBlock;

Expand Down Expand Up @@ -1437,21 +1448,25 @@ algorithm
list<list<BackendDAE.Equation>> eqnslst;
Type_a ext_arg_1,ext_arg_2,ext_arg_3;
Boolean diffed;
DAE.Expand crefExpand;

case (BackendDAE.EQUATION(exp = e1,scalar = e2,source = source,differentiated = diffed),_,_)
equation
((e1_1,(ops,ext_arg_1))) = func((e1,({},inTypeA)));
((e2_1,(ops,ext_arg_2))) = func((e2,(ops,ext_arg_1)));
source = List.foldr(ops, DAEUtil.addSymbolicTransformation, source);
then
(BackendDAE.EQUATION(e1_1,e2_1,source,diffed),ext_arg_2);
/* array equation */

// Array equation
case (BackendDAE.ARRAY_EQUATION(dimSize=dimSize,left = e1,right = e2,source = source,differentiated = diffed),_,_)
equation
((e1_1,(ops,ext_arg_1))) = func((e1,({},inTypeA)));
((e2_1,(ops,ext_arg_2))) = func((e2,(ops,ext_arg_1)));
source = List.foldr(ops, DAEUtil.addSymbolicTransformation, source);
then
(BackendDAE.ARRAY_EQUATION(dimSize,e1_1,e2_1,source,diffed),ext_arg_2);

case (BackendDAE.SOLVED_EQUATION(componentRef = cr,exp = e2,source=source,differentiated = diffed),_,_)
equation
e1 = Expression.crefExp(cr);
Expand All @@ -1460,18 +1475,21 @@ algorithm
source = List.foldr(ops, DAEUtil.addSymbolicTransformation, source);
then
(BackendDAE.SOLVED_EQUATION(cr1,e2_1,source,diffed),ext_arg_1);

case (BackendDAE.RESIDUAL_EQUATION(exp = e1,source=source,differentiated = diffed),_,_)
equation
((e1_1,(ops,ext_arg_1))) = func((e1,({},inTypeA)));
source = List.foldr(ops, DAEUtil.addSymbolicTransformation, source);
then
(BackendDAE.RESIDUAL_EQUATION(e1_1,source,diffed),ext_arg_1);
/* Algorithms */
case (BackendDAE.ALGORITHM(size = size,alg=DAE.ALGORITHM_STMTS(statementLst = statementLst),source = source),_,_)

// Algorithms
case (BackendDAE.ALGORITHM(size = size,alg=DAE.ALGORITHM_STMTS(statementLst = statementLst),source = source, expand = crefExpand),_,_)
equation
(statementLst,(ops,ext_arg_1)) = DAEUtil.traverseDAEEquationsStmts(statementLst, func, ({},inTypeA));
source = List.foldr(ops, DAEUtil.addSymbolicTransformation, source);
then (BackendDAE.ALGORITHM(size,DAE.ALGORITHM_STMTS(statementLst),source),ext_arg_1);
then (BackendDAE.ALGORITHM(size,DAE.ALGORITHM_STMTS(statementLst),source, crefExpand),ext_arg_1);

case (BackendDAE.WHEN_EQUATION(size=size,whenEquation =
BackendDAE.WHEN_EQ(condition=cond,left = cr,right = e1,elsewhenPart=NONE()),source = source),_,_)
equation
Expand All @@ -1494,6 +1512,7 @@ algorithm
res = BackendDAE.WHEN_EQUATION(size,BackendDAE.WHEN_EQ(cond,cr,e1_1,SOME(elsepartRes)),source);
then
(res,ext_arg_3);

case (BackendDAE.COMPLEX_EQUATION(size=size,left = e1,right = e2,source = source,differentiated = diffed),_,_)
equation
((e1_1,(ops,ext_arg_1))) = func((e1,({},inTypeA)));
Expand All @@ -1510,6 +1529,7 @@ algorithm
(eqns,ext_arg_1) = traverseBackendDAEExpsEqnLstWithSymbolicOperation(eqns,func,ext_arg_1,{});
then
(BackendDAE.IF_EQUATION(expl,eqnslst,eqns,source),ext_arg_1);

else
equation
Error.addMessage(Error.INTERNAL_ERROR, {"./Compiler/BackEnd/BackendDAETransform.mo: function traverseBackendDAEExpsEqnWithSymbolicOperation failed"});
Expand Down
5 changes: 3 additions & 2 deletions Compiler/BackEnd/BackendDAEUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4780,6 +4780,7 @@ algorithm
list<list<BackendDAE.Equation>> eqnslst;
list<BackendDAE.Equation> eqns,eqnselse;
list<DAE.ComponentRef> algoutCrefs;
DAE.Expand crefExpand;

// EQUATION
case (vars,BackendDAE.EQUATION(exp = e1,scalar = e2),_,_,_)
Expand Down Expand Up @@ -4850,10 +4851,10 @@ algorithm

// ALGORITHM For now assume that algorithm will be solvable for
// output variables. Mark this as solved and input variables as unsolvable:
case (vars,BackendDAE.ALGORITHM(size=size,alg=alg),_,_,_)
case (vars,BackendDAE.ALGORITHM(size=size,alg=alg,expand=crefExpand),_,_,_)
equation
// get outputs
algoutCrefs = CheckModel.algorithmOutputs(alg);
algoutCrefs = CheckModel.algorithmOutputs(alg, crefExpand);
// mark outputs as solved
row = adjacencyRowAlgorithmOutputs(algoutCrefs,vars,mark,rowmark,{});
// get inputs
Expand Down

0 comments on commit 916b920

Please sign in to comment.