@@ -2561,6 +2561,8 @@ protected function mergeZeroCrossing "function: mergeZeroCrossing
25612561
25622562 Merges two zero crossings into one by makeing the union of the lists of
25632563 equaions and when clauses they appear in.
2564+ modified: 2011-01 by wbraun
2565+ merge to ZeroCrosssing with the lowest index
25642566"
25652567 input BackendDAE . ZeroCrossing inZeroCrossing1;
25662568 input BackendDAE . ZeroCrossing inZeroCrossing2;
@@ -2643,15 +2645,14 @@ public function findZeroCrossings "function: findZeroCrossings
26432645 output list< BackendDAE . MultiDimEquation > outMultiDimEqs;
26442646 output list< BackendDAE . WhenClause > outWhenClauseLst;
26452647 output list< DAE . Algorithm > outAlgs;
2646- // output Integer numberRelations;
26472648algorithm
26482649 (res_1,outEquationLst,outMultiDimEqs,outWhenClauseLst,outAlgs) := findZeroCrossings2(vars, knvars,eq,multiDimEqs,0 , wc, 0 , algs,0 ,{},0 );
2649- // numberRelations := listLength(res_1);
26502650end findZeroCrossings;
26512651
26522652protected function findZeroCrossings2 "function: findZeroCrossings2
26532653
26542654 Helper function to find_zero_crossing.
2655+ modified: 2011-01 by wbraun
26552656"
26562657 input BackendDAE . Variables inVariables1;
26572658 input BackendDAE . Variables knvars;
@@ -2701,6 +2702,26 @@ algorithm
27012702 list< Integer > dimsize;
27022703
27032704 case (_,_,{},mdeqs,_,{},_,{},_,res,_) then (res,{},mdeqs,{},{});
2705+
2706+ // all algorithm stmts are processed firstly
2707+ case (v,knvars,el,mdeqs,eq_count,xsWhen,wc_count,(DAE . ALGORITHM_STMTS (stmts)::algs),countZC,zcs,alg_indx)
2708+ equation
2709+ ((stmts_1,(_,_,_,(res,countZC),(_,_,_,_)))) = traverseStmtsExps(stmts, collectZCAlgs, (DAE . RCONST (0 . 0 ),{},DAE . RCONST (0 . 0 ),(zcs,countZC),(alg_indx,v,knvars,el)),knvars);
2710+ alg_indx = alg_indx + 1 ;
2711+ (res1,eq_reslst,mdeqs_res1,wc_reslst,algs_res) = findZeroCrossings2(v, knvars,el,mdeqs,eq_count, xsWhen, wc_count,algs,countZC,res,alg_indx);
2712+ then
2713+ (res1,eq_reslst,mdeqs_res1,wc_reslst,DAE . ALGORITHM_STMTS (stmts_1)::algs_res);
2714+
2715+ // then all when clauses are processed
2716+ case (v,knvars,el,mdeqs,eq_count,((wc as BackendDAE . WHEN_CLAUSE (condition = daeExp,reinitStmtLst= whenOperations , elseClause = elseClause_ )) :: xsWhen),wc_count,{},countZC,zcs,_)
2717+ equation
2718+ wc_count = wc_count + 1 ;
2719+ (eres1,countZC,res) = findZeroCrossings3(daeExp,zcs,countZC,-1 ,wc_count,v,knvars);
2720+ (res1,eq_reslst,mdeqs_res1,wc_reslst,algs) = findZeroCrossings2(v, knvars,el,mdeqs,eq_count, xsWhen, wc_count,{},countZC,res,0 );
2721+ then
2722+ (res1,eq_reslst,mdeqs_res1,BackendDAE . WHEN_CLAUSE (eres1,whenOperations,elseClause_)::wc_reslst,algs);
2723+
2724+ // after all algorithms and when clauses are processed, all equations are processed
27042725 case (v,knvars,((e as BackendDAE . EQUATION (exp = e1,scalar = e2, source= source_)) :: xs),mdeqs,eq_count,{},_,{},countZC,zcs,_)
27052726 equation
27062727 eq_count = eq_count + 1 ;
@@ -2744,29 +2765,14 @@ algorithm
27442765 then
27452766 (res1,BackendDAE . RESIDUAL_EQUATION (eres1,source_)::eq_reslst,mdeqs_res1,wc_reslst,algs);
27462767
2747- // let when equation pass they are discrete and can't contain ZeroCrossings
2768+ // let when equation pass they are discrete and can't contain ZeroCrossings
27482769 case (v,knvars,(e :: xs),mdeqs,eq_count,{},_,{},countZC,res,_)
27492770 equation
27502771 eq_count = eq_count + 1 ;
27512772 (res1,eq_reslst,mdeqs_res1,wc_reslst,algs) = findZeroCrossings2(v, knvars,xs,mdeqs,eq_count, {}, 0 ,{},countZC,res,0 );
27522773 then
27532774 (res1,e::eq_reslst,mdeqs_res1,wc_reslst,algs);
2754-
2755- case (v,knvars,el,mdeqs,eq_count,((wc as BackendDAE . WHEN_CLAUSE (condition = daeExp,reinitStmtLst= whenOperations , elseClause = elseClause_ )) :: xsWhen),wc_count,{},countZC,zcs,_)
2756- equation
2757- wc_count = wc_count + 1 ;
2758- (eres1,countZC,res) = findZeroCrossings3(daeExp,zcs,countZC,-1 ,wc_count,v,knvars);
2759- (res1,eq_reslst,mdeqs_res1,wc_reslst,algs) = findZeroCrossings2(v, knvars,el,mdeqs,eq_count, xsWhen, wc_count,{},countZC,res,0 );
2760- then
2761- (res1,eq_reslst,mdeqs_res1,BackendDAE . WHEN_CLAUSE (eres1,whenOperations,elseClause_)::wc_reslst,algs);
2762- case (v,knvars,el,mdeqs,eq_count,xsWhen,wc_count,(DAE . ALGORITHM_STMTS (stmts)::algs),countZC,zcs,alg_indx)
2763- equation
2764- ((stmts_1,(_,_,_,(res,countZC),(_,_,_,_)))) = traverseStmtsExps(stmts, collectZCAlgs, (DAE . RCONST (0 . 0 ),{},DAE . RCONST (0 . 0 ),(zcs,countZC),(alg_indx,v,knvars,el)),knvars);
2765- alg_indx = alg_indx + 1 ;
2766- (res1,eq_reslst,mdeqs_res1,wc_reslst,algs_res) = findZeroCrossings2(v, knvars,el,mdeqs,eq_count, xsWhen, wc_count,algs,countZC,res,alg_indx);
2767- then
2768- (res1,eq_reslst,mdeqs_res1,wc_reslst,DAE . ALGORITHM_STMTS (stmts_1)::algs_res);
2769- end matchcontinue;
2775+ end matchcontinue;
27702776end findZeroCrossings2;
27712777
27722778protected function findZeroCrossings3
@@ -2788,7 +2794,8 @@ end findZeroCrossings3;
27882794
27892795protected function collectZC "function: collectZeroCrossings
27902796
2791- Collects zero crossings
2797+ Collects zero crossings in equations
2798+ modified: 2011-01 by wbraun
27922799"
27932800 input tuple< DAE . Exp , tuple< tuple< list< BackendDAE . ZeroCrossing > ,Integer > , tuple< Integer ,Integer ,BackendDAE . Variables ,BackendDAE . Variables >>> inTplExpExpTplExpExpLstVariables;
27942801 output tuple< DAE . Exp , Boolean , tuple< tuple< list< BackendDAE . ZeroCrossing > ,Integer > , tuple< Integer ,Integer ,BackendDAE . Variables ,BackendDAE . Variables >>> outTplExpExpTplExpExpLstVariables;
@@ -2813,14 +2820,14 @@ algorithm
28132820 zc = mergeZeroCrossings(zc);
28142821 indx = indx + (listLength(zc) - listLength(zeroCrossings));
28152822 then ((e,true ,((zc,indx),(eq_count,wc_count,vars,knvars))));
2816- /* function with discrete expressions generate no zerocrossing */
2823+ // function with discrete expressions generate no zerocrossing
28172824 case (((e as DAE . RELATION (exp1 = e1,operator = op,exp2 = e2)),((zeroCrossings,indx),(eq_count,wc_count,vars,knvars))))
28182825 equation
28192826 true = BackendDAEUtil . isDiscreteExp(e1, vars,knvars);
28202827 true = BackendDAEUtil . isDiscreteExp(e2, vars,knvars);
28212828 then
28222829 ((e,true ,((zeroCrossings,indx),(eq_count,wc_count,vars,knvars))));
2823- /* All other functions generate zerocrossing. */
2830+ // All other functions generate zerocrossing.
28242831 case (((e as DAE . RELATION (exp1 = e1,operator = op,exp2 = e2)),((zeroCrossings,indx),(eq_count,wc_count,vars,knvars))))
28252832 equation
28262833 e_1 = DAE . RELATION (e1,op,e2,indx,NONE ());
@@ -2838,7 +2845,10 @@ end collectZC;
28382845
28392846protected function collectZCAlgs "function: collectZeroCrossings
28402847
2841- Collects zero crossings
2848+ Collects zero crossings in algorithms stamts, beside for loops those are
2849+ processed by collectZCAlgsFor
2850+
2851+ modified: 2011-01 by wbraun
28422852"
28432853 input tuple< DAE . Exp , tuple< DAE . Exp , list< DAE . Exp > ,DAE . Exp , tuple< list< BackendDAE . ZeroCrossing > ,Integer > , tuple< Integer ,BackendDAE . Variables ,BackendDAE . Variables ,list< BackendDAE . Equation >>>> inTplExpExpTplExpExpLstVariables;
28442854 output tuple< DAE . Exp , Boolean , tuple< DAE . Exp , list< DAE . Exp > , DAE . Exp , tuple< list< BackendDAE . ZeroCrossing > ,Integer > , tuple< Integer ,BackendDAE . Variables ,BackendDAE . Variables ,list< BackendDAE . Equation >>>> outTplExpExpTplExpExpLstVariables;
@@ -2867,13 +2877,13 @@ algorithm
28672877 zc = mergeZeroCrossings(zc);
28682878 indx = indx + (listLength(zc) - listLength(zeroCrossings));
28692879 then ((e,true ,(iterator,le,range,(zc,indx),(alg_indx,vars,knvars,eqns))));
2870- /* function with discrete expressions generate no zerocrossing */
2880+ // function with discrete expressions generate no zerocrossing
28712881 case (((e as DAE . RELATION (exp1 = e1,operator = op,exp2 = e2)),(iterator,le,range,(zeroCrossings,indx),(alg_indx,vars,knvars,eqns))))
28722882 equation
28732883 true = BackendDAEUtil . isDiscreteExp(e1, vars,knvars);
28742884 true = BackendDAEUtil . isDiscreteExp(e2, vars,knvars);
28752885 then ((e,true ,(iterator,le,range,(zeroCrossings,indx),(alg_indx,vars,knvars,eqns))));
2876- /* All other functions generate zerocrossing. */
2886+ // All other functions generate zerocrossing.
28772887 case (((e as DAE . RELATION (exp1 = e1,operator = op,exp2 = e2)),(iterator,le,range,(zeroCrossings,indx),(alg_indx,vars,knvars,eqns))))
28782888 equation
28792889 e_1 = DAE . RELATION (e1,op,e2,indx,NONE ());
@@ -2890,9 +2900,10 @@ algorithm
28902900end collectZCAlgs;
28912901
28922902protected function traverseStmtsExps "function: traverseStmtExps
2893- Helper function for traverseStmtsExps.
28942903 Handles the traversing of list<DAE.Statement>.
2895- Works with the help of Expression.traverseExpTopDown"
2904+ Works with the help of Expression.traverseExpTopDown to find
2905+ ZeroCrossings in algorithm statements
2906+ modified: 2011-01 by wbraun"
28962907 input list< DAE . Statement > inStmts;
28972908 input FuncExpType func ;
28982909 input tuple< DAE . Exp , list< DAE . Exp > , DAE . Exp , tuple< list< BackendDAE . ZeroCrossing > ,Integer > , tuple< Integer ,BackendDAE . Variables ,BackendDAE . Variables ,list< BackendDAE . Equation >>> extraArg;
@@ -2902,7 +2913,6 @@ protected function traverseStmtsExps "function: traverseStmtExps
29022913 input tuple< DAE . Exp ,tuple< DAE . Exp , list< DAE . Exp > , DAE . Exp , tuple< list< BackendDAE . ZeroCrossing > ,Integer > , tuple< Integer ,BackendDAE . Variables ,BackendDAE . Variables ,list< BackendDAE . Equation >>>> arg;
29032914 output tuple< DAE . Exp , Boolean , tuple< DAE . Exp , list< DAE . Exp > , DAE . Exp , tuple< list< BackendDAE . ZeroCrossing > ,Integer > , tuple< Integer ,BackendDAE . Variables ,BackendDAE . Variables ,list< BackendDAE . Equation >>>> oarg;
29042915 end FuncExpType ;
2905- // replaceable type Type_a subtypeof Any;
29062916algorithm
29072917 (outTplStmtTypeA) := match(inStmts,func ,extraArg,knvars)
29082918 local
@@ -3008,8 +3018,6 @@ algorithm
30083018
30093019 case (((x as DAE . STMT_REINIT (var = e,value= e2, source = source)) :: xs),func , extraArg, knvars)
30103020 equation
3011- // ((e_1,extraArg)) = Expression.traverseExpTopDown(e, func, extraArg);
3012- // ((e_2,extraArg)) = Expression.traverseExpTopDown(e2, func, extraArg);
30133021 ((xs_1, extraArg)) = traverseStmtsExps(xs, func , extraArg, knvars);
30143022 then ((x :: xs_1,extraArg));
30153023
@@ -3064,8 +3072,9 @@ end traverseStmtsExps;
30643072
30653073protected function traverseStmtsElseExps "
30663074Author: BZ, 2008-12
3067- Helper function for traverseStmt
3068- "
3075+ Helper function for traverseStmtsExps
3076+ to find ZeroCrosssings in algorithm Else statements
3077+ modified: 2011-01 by wbraun"
30693078 input Algorithm . Else inElse;
30703079 input FuncExpType func ;
30713080 input tuple< DAE . Exp , list< DAE . Exp > , DAE . Exp , tuple< list< BackendDAE . ZeroCrossing > ,Integer > , tuple< Integer ,BackendDAE . Variables ,BackendDAE . Variables ,list< BackendDAE . Equation >>> extraArg;
@@ -3075,7 +3084,6 @@ Helper function for traverseStmt
30753084 input tuple< DAE . Exp ,tuple< DAE . Exp , list< DAE . Exp > , DAE . Exp , tuple< list< BackendDAE . ZeroCrossing > ,Integer > , tuple< Integer ,BackendDAE . Variables ,BackendDAE . Variables ,list< BackendDAE . Equation >>>> arg;
30763085 output tuple< DAE . Exp ,Boolean , tuple< DAE . Exp , list< DAE . Exp > , DAE . Exp , tuple< list< BackendDAE . ZeroCrossing > ,Integer > , tuple< Integer ,BackendDAE . Variables ,BackendDAE . Variables ,list< BackendDAE . Equation >>>> oarg;
30773086 end FuncExpType ;
3078- // replaceable type Type_a subtypeof Any;
30793087algorithm
30803088 outTplStmtTypeA := matchcontinue(inElse,func ,extraArg,knvars)
30813089 local
@@ -3099,7 +3107,8 @@ end traverseStmtsElseExps;
30993107
31003108protected function collectZCAlgsFor "function: collectZeroCrossings
31013109
3102- Collects zero crossings in for loops
3110+ Collects zero crossings in for loops
3111+ added: 2011-01 by wbraun
31033112"
31043113 input tuple< DAE . Exp , tuple< DAE . Exp , list< DAE . Exp > , DAE . Exp , tuple< list< BackendDAE . ZeroCrossing > ,Integer > , tuple< Integer ,BackendDAE . Variables ,BackendDAE . Variables ,list< BackendDAE . Equation >>>> inTplExpExpTplExpExpLstVariables;
31053114 output tuple< DAE . Exp , Boolean , tuple< DAE . Exp , list< DAE . Exp > , DAE . Exp , tuple< list< BackendDAE . ZeroCrossing > ,Integer > , tuple< Integer ,BackendDAE . Variables ,BackendDAE . Variables ,list< BackendDAE . Equation >>>> outTplExpExpTplExpExpLstVariables;
@@ -3131,13 +3140,13 @@ algorithm
31313140 zc = mergeZeroCrossings(zc);
31323141 indx = indx + (listLength(zc) - listLength(zeroCrossings));
31333142 then ((e,true ,(iterator,inExpLst,range,(zc,indx),(alg_indx,vars,knvars,eqns))));
3134- /* function with discrete expressions generate no zerocrossing */
3143+ // function with discrete expressions generate no zerocrossing.
31353144 case (((e as DAE . RELATION (exp1 = e1,operator = op,exp2 = e2)),(iterator,inExpLst,range,(zeroCrossings,indx),(alg_indx,vars,knvars,eqns))))
31363145 equation
31373146 true = BackendDAEUtil . isDiscreteExp(e1, vars,knvars);
31383147 true = BackendDAEUtil . isDiscreteExp(e2, vars,knvars);
31393148 then ((e,true ,(iterator,inExpLst,range,(zeroCrossings,indx),(alg_indx,vars,knvars,eqns))));
3140- /* All other functions generate zerocrossing. */
3149+ // All other functions generate zerocrossing.
31413150 case (((e as DAE . RELATION (exp1 = e1,operator = op,exp2 = e2)),(iterator,inExpLst,range as DAE . RANGE (exp= startvalue,expOption= stepvalueopt),(zeroCrossings,indx),(alg_indx,vars,knvars,eqns))))
31423151 equation
31433152 b1 = Expression . expContains(e1,iterator);
@@ -3147,7 +3156,6 @@ algorithm
31473156 istart = expInt(startvalue,knvars);
31483157 istep = expInt(stepvalue,knvars);
31493158 e_1 = DAE . RELATION (e1,op,e2,indx,SOME ((iterator,istart,istep)));
3150- // (stmts,_) = DAEUtil.traverseDAEEquationsStmts(replaceExp,((e,e_1)));
31513159 (explst,indx) = replaceIteratorwithStaticValues(e,iterator,inExpLst,indx);
31523160 eqs = BackendEquation . equationAlgorithmEqnsNr(eqns,alg_indx,0 );
31533161 zc = makeZeroCrossings(explst, eqs, {});
@@ -3156,14 +3164,13 @@ algorithm
31563164 itmp = (listLength(zc)- listLength(zeroCrossings));
31573165 eres = Util . if_((itmp> 0 ),e_1,e);
31583166 then ((e,true ,(iterator,inExpLst,range,(zc,indx),(alg_indx,vars,knvars,eqns))));
3159- /* All other functions generate zerocrossing. */
3167+ // All other functions generate zerocrossing.
31603168 case (((e as DAE . RELATION (exp1 = e1,operator = op,exp2 = e2)),(iterator,inExpLst,range,(zeroCrossings,indx),(alg_indx,vars,knvars,eqns))))
31613169 equation
31623170 b1 = Expression . expContains(e1,iterator);
31633171 b2 = Expression . expContains(e2,iterator);
31643172 false = Util . boolOrList({b1,b2});
31653173 e_1 = DAE . RELATION (e1,op,e2,indx,NONE ());
3166- // (stmts,_) = DAEUtil.traverseDAEEquationsStmts(stmts,replaceExp,((e,e_1)));
31673174 eqs = BackendEquation . equationAlgorithmEqnsNr(eqns,alg_indx,0 );
31683175 zc = makeZeroCrossings({e_1}, eqs, {});
31693176 zc = listAppend(zeroCrossings, zc);
@@ -3210,8 +3217,9 @@ end replaceIteratorwithStaticValues;
32103217
32113218
32123219protected function traverseStmtsForExps
3213- "function: traverseStmtsForExps
3214- "
3220+ "Helper function for traverseStmtsExps
3221+ to processed for loops to search ZeroCrosssings
3222+ modified: 2011-01 by wbraun"
32153223 input DAE . Exp inIteratorExp;
32163224 input list< DAE . Exp > inExplst;
32173225 input DAE . Exp inRange;
@@ -3224,7 +3232,6 @@ protected function traverseStmtsForExps
32243232 input tuple< DAE . Exp , tuple< DAE . Exp , list< DAE . Exp > , DAE . Exp ,tuple< list< BackendDAE . ZeroCrossing > ,Integer > , tuple< Integer ,BackendDAE . Variables ,BackendDAE . Variables ,list< BackendDAE . Equation >>>> arg;
32253233 output tuple< DAE . Exp , Boolean , tuple< DAE . Exp , list< DAE . Exp > , DAE . Exp ,tuple< list< BackendDAE . ZeroCrossing > ,Integer > , tuple< Integer ,BackendDAE . Variables ,BackendDAE . Variables ,list< BackendDAE . Equation >>>> oarg;
32263234 end FuncExpType ;
3227- // replaceable type Type_a subtypeof Any;
32283235algorithm
32293236 outTplStmtTypeA := matchcontinue (inIteratorExp,inExplst,inRange,inStmts,knvars,func ,extraArg)
32303237 local
0 commit comments