Ticket #3230: 3230.diff

File 3230.diff, 7.0 KB (added by Lennart Ochel, 11 years ago)
  • Compiler/BackEnd/Initialization.mo

     
    355355      DAE.ElementSource source;
    356356      DAE.Algorithm alg;
    357357      Integer size;
    358       list< DAE.Statement> stmts;
     358      list<DAE.Statement> stmts;
    359359      list< BackendDAE.Equation> eqns;
    360360      BackendDAE.WhenEquation weqn;
    361       list< DAE.ComponentRef> crefLst;
     361      list<DAE.ComponentRef> crefLst;
    362362      HashSet.HashSet leftCrs;
    363363      DAE.Expand crefExpand;
    364364      BackendDAE.EquationAttributes eqAttr;
     
    369369    then eqns;
    370370
    371371    // algorithm
    372     case BackendDAE.ALGORITHM(alg=alg, source=source,expand=crefExpand) equation
     372    case BackendDAE.ALGORITHM(alg=alg, source=source, expand=crefExpand) equation
    373373      DAE.ALGORITHM_STMTS(statementLst=stmts) = alg;
    374       (stmts, leftCrs) = inlineWhenForInitializationWhenAlgorithm(stmts, {}, HashSet.emptyHashSetSized(50));
     374      (stmts, leftCrs) = inlineWhenForInitializationWhenAlgorithm(stmts, {}, HashSet.emptyHashSetSized(50), source);
    375375      alg = DAE.ALGORITHM_STMTS(stmts);
    376376      size = listLength(CheckModel.checkAndGetAlgorithmOutputs(alg, source, crefExpand));
    377377      crefLst = BaseHashSet.hashSetList(leftCrs);
    378       eqns = generateInactiveWhenEquationForInitialization(crefLst, source, inAccEq);
    379       eqns = List.consOnTrue(not listEmpty(stmts), BackendDAE.ALGORITHM(size, alg, source, crefExpand, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC), eqns);
     378      //eqns = generateInactiveWhenEquationForInitialization(crefLst, source, inAccEq);
     379      eqns = List.consOnTrue(not listEmpty(stmts), BackendDAE.ALGORITHM(size, alg, source, crefExpand, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC), inAccEq);
    380380    then eqns;
    381381
    382382    else inEq::inAccEq;
     
    416416
    417417protected function inlineWhenForInitializationWhenAlgorithm "author: lochel
    418418  This function generates out of a given when-algorithm, a algorithm for the initialization-problem."
    419   input list< DAE.Statement> inStmts;
    420   input list< DAE.Statement> inAcc "={}";
     419  input list<DAE.Statement> inStmts;
     420  input list<DAE.Statement> inAcc "={}";
    421421  input HashSet.HashSet inLeftCrs;
    422   output list< DAE.Statement> outStmts;
     422  input DAE.ElementSource inSource;
     423  output list<DAE.Statement> outStmts;
    423424  output HashSet.HashSet outLeftCrs;
    424425algorithm
    425426  (outStmts, outLeftCrs) := matchcontinue(inStmts)
    426427    local
    427428      DAE.Exp condition;
    428       list< DAE.ComponentRef> crefLst;
     429      list<DAE.ComponentRef> crefLst;
    429430      DAE.Statement stmt;
    430       list< DAE.Statement> stmts, rest;
     431      list<DAE.Statement> stmts, rest;
    431432      HashSet.HashSet leftCrs;
    432433      list<tuple<DAE.ComponentRef, Integer>> crintLst;
    433434
     
    438439    case (stmt as DAE.STMT_WHEN())::rest equation
    439440      // for when statements it is not necessary that all branches have the same left hand side variables
    440441      // -> take care that for each left hand side an assigment is generated
    441       (stmts, leftCrs) = inlineWhenForInitializationWhenStmt(stmt, inLeftCrs, inAcc);
    442       (stmts, leftCrs) = inlineWhenForInitializationWhenAlgorithm(rest, stmts, leftCrs);
     442      (stmts, leftCrs) = inlineWhenForInitializationWhenStmt(stmt, inLeftCrs, inAcc, inSource);
     443      (stmts, leftCrs) = inlineWhenForInitializationWhenAlgorithm(rest, stmts, leftCrs, inSource);
    443444    then  (stmts, leftCrs);
    444445
    445446    // no when statement
    446447    case stmt::rest equation
    447       (stmts, leftCrs) = inlineWhenForInitializationWhenAlgorithm(rest, stmt::inAcc, inLeftCrs);
     448      (stmts, leftCrs) = inlineWhenForInitializationWhenAlgorithm(rest, stmt::inAcc, inLeftCrs, inSource);
    448449    then (stmts, leftCrs);
    449450  end matchcontinue;
    450451end inlineWhenForInitializationWhenAlgorithm;
     
    453454  This function generates out of a given when-algorithm, a algorithm for the initialization-problem."
    454455  input DAE.Statement inWhenStatement;
    455456  input HashSet.HashSet inLeftCrs;
    456   input list< DAE.Statement> inAcc;
    457   output list< DAE.Statement> outStmts;
     457  input list<DAE.Statement> inAcc;
     458  input DAE.ElementSource inSource;
     459  output list<DAE.Statement> outStmts;
    458460  output HashSet.HashSet outLeftCrs;
    459461algorithm
    460462  (outStmts, outLeftCrs) := matchcontinue(inWhenStatement)
    461463    local
    462464      DAE.Exp condition;
    463       list< DAE.ComponentRef> crefLst;
     465      list<DAE.ComponentRef> crefLst;
    464466      DAE.Statement stmt;
    465       list< DAE.Statement> stmts;
     467      list<DAE.Statement> stmts;
    466468      HashSet.HashSet leftCrs;
    467469      list<tuple<DAE.ComponentRef, Integer>> crintLst;
    468470
     
    476478    case DAE.STMT_WHEN(exp=condition, statementLst=stmts, elseWhen=NONE()) equation
    477479      false = Expression.containsInitialCall(condition, false);
    478480      crefLst = CheckModel.algorithmStatementListOutputs(stmts, DAE.EXPAND()); // expand as we're in an algorithm
     481
     482      stmts = generateInactiveWhenStatement(crefLst, inSource, {});
     483      stmts = List.foldr(stmts, List.consr, inAcc);
     484
    479485      leftCrs = List.fold(crefLst, BaseHashSet.add, inLeftCrs);
    480     then (inAcc, leftCrs);
     486    then (stmts, leftCrs);
    481487
    482488    // inactive when equation during initialization with elsewhen part
    483489    case DAE.STMT_WHEN(exp=condition, statementLst=stmts, elseWhen=SOME(stmt)) equation
    484490      false = Expression.containsInitialCall(condition, false);
    485491      crefLst = CheckModel.algorithmStatementListOutputs(stmts, DAE.EXPAND()); // expand as we're in an algorithm
     492     
     493      stmts = generateInactiveWhenStatement(crefLst, inSource, {});
     494      stmts = List.foldr(stmts, List.consr, inAcc);
     495     
    486496      leftCrs = List.fold(crefLst, BaseHashSet.add, inLeftCrs);
    487       (stmts, leftCrs) = inlineWhenForInitializationWhenStmt(stmt, leftCrs, inAcc);
     497      (stmts, leftCrs) = inlineWhenForInitializationWhenStmt(stmt, leftCrs, inAcc, inSource);
    488498    then (stmts, leftCrs);
    489499
    490500    else equation
     
    523533 end match;
    524534end generateInactiveWhenEquationForInitialization;
    525535
     536protected function generateInactiveWhenStatement "author: lochel"
     537  input list<DAE.ComponentRef> inCrLst;
     538  input DAE.ElementSource inSource;
     539  input list<DAE.Statement> inStmts;
     540  output list<DAE.Statement> outStmts;
     541algorithm
     542  outStmts := match (inCrLst)
     543    local
     544      DAE.Type identType;
     545      DAE.Exp crefExp, crefPreExp;
     546      DAE.ComponentRef cr;
     547      list<DAE.ComponentRef> rest;
     548      DAE.Statement stmt;
     549      list<DAE.Statement> stmts;
     550
     551    case {}
     552    then inStmts;
     553
     554    case cr::rest equation
     555      identType = ComponentReference.crefTypeConsiderSubs(cr);
     556      crefExp = DAE.CREF(cr, identType);
     557      crefPreExp = Expression.makePureBuiltinCall("pre", {crefExp}, DAE.T_BOOL_DEFAULT);
     558      stmt = DAE.STMT_ASSIGN(identType, crefExp, crefPreExp, inSource);
     559      stmts = generateInactiveWhenStatement(rest, inSource, stmt::inStmts);
     560    then stmts;
     561 end match;
     562end generateInactiveWhenStatement;
     563
    526564// =============================================================================
    527565// section for collecting all variables, of which the left limit is also used.
    528566//