@@ -394,7 +394,127 @@ algorithm
394394 end matchcontinue;
395395end instantiateClass;
396396
397- public function instantiatePartialClass
397+ protected function reEvaluateInitialIfEqns "
398+ Author BZ
399+ This is a backpatch to fix the case of 'connection.isRoot' in initial if equations.
400+ After the class is instantiated a second sweep is done to check the initial if equations conditions.
401+ If all conditions are constand, we return only the 'correct' branch equations.
402+ "
403+ input Env.Cache cache;
404+ input Env.Env env;
405+ input DAE.DAElist dae;
406+ input Boolean isTopCall;
407+ output DAE.DAElist odae;
408+ algorithm odae := matchcontinue(cache,env,dae,isTopCall)
409+ local
410+ DAE.FunctionTree funcs;
411+ list<DAE.Element> elems;
412+ case(cache,env,DAE.DAE(elementLst = elems,functions = funcs),true)
413+ equation
414+ elems = reEvaluateInitialIfEqns2(cache,env,elems);
415+ then
416+ DAE.DAE(elems,funcs);
417+ case(_,_,dae,false) then dae;
418+ end matchcontinue;
419+ end reEvaluateInitialIfEqns;
420+
421+ protected function reEvaluateInitialIfEqns2 ""
422+ input Env.Cache cache;
423+ input Env.Env env;
424+ input list<DAE.Element> elems;
425+ output list<DAE.Element> oelems;
426+ algorithm oelems := matchcontinue(cache,env,elems)
427+ local
428+ list<DAE.Exp> conds;
429+ list<Values.Value> valList;
430+ list<list<DAE.Element>> tbs;
431+ list<DAE.Element> fb,selectedBranch;
432+ DAE.Element elem;
433+ DAE.ElementSource source;
434+ list<Boolean> blist;
435+ case(_,_,{}) then {};
436+ case(cache,env,(elem as DAE.INITIAL_IF_EQUATION(condition1 = conds, equations2=tbs, equations3=fb, source=source))::elems)
437+ equation
438+ //print(" (Initial if)To ceval: " +& Util.stringDelimitList(Util.listMap(conds,Exp.printExpStr),", ") +& "\n");
439+ (cache,valList) = Ceval.cevalList(cache,env, conds, true, NONE, Ceval.NO_MSG());
440+ //print(" Ceval res: ("+&Util.stringDelimitList(Util.listMap(valList,ValuesUtil.printValStr),",")+&")\n");
441+
442+ blist = Util.listMap(valList,ValuesUtil.valueBool);
443+ selectedBranch = selectList(blist, tbs, fb);
444+ selectedBranch = makeDAEElementInitial(selectedBranch);
445+ oelems = reEvaluateInitialIfEqns2(cache,env,elems);
446+ oelems = listAppend(selectedBranch,oelems);
447+
448+ //print("RETURN _INITIAL_ DAE: " +& DAEUtil.dumpDAEElementsStr(DAE.DAE(selectedBranch,DAE.AVLTREENODE(NONE,0,NONE,NONE))) +& "\n");
449+ //print(" INSTEAD OF: " +& DAEUtil.dumpDAEElementsStr(DAE.DAE({elem},DAE.AVLTREENODE(NONE,0,NONE,NONE))) +& "\n");
450+ then
451+ oelems;
452+ case(cache,env,elem::elems)
453+ equation
454+ oelems = reEvaluateInitialIfEqns2(cache,env,elems);
455+ then
456+ elem::oelems;
457+ end matchcontinue;
458+ end reEvaluateInitialIfEqns2;
459+
460+ protected function makeDAEElementInitial "
461+ Author BZ
462+ Helper function for reEvaluateInitialIfEqns, makes the contenst of an initial if equation initial.
463+ "
464+ input list<DAE.Element> inElems;
465+ output list<DAE.Element> outElems;
466+ algorithm
467+ outElems := matchcontinue(inElems)
468+ local
469+ DAE.Element elem;
470+ DAE.ComponentRef cr;
471+ DAE.Exp e1,e2,e3;
472+ DAE.ElementSource s;
473+ list<DAE.Exp> expl;
474+ list<list<DAE.Element>> tbs ;
475+ list<DAE.Element> fb;
476+ DAE.Algorithm al;
477+ list<Integer> dims;
478+ case({}) then {};
479+ case(DAE.DEFINE(cr,e1,s)::inElems)
480+ equation
481+ outElems = makeDAEElementInitial(inElems);
482+ then
483+ DAE.INITIALDEFINE(cr,e1,s)::outElems;
484+ case(DAE.ARRAY_EQUATION(dims,e1,e2,s)::_)
485+ equation
486+ outElems = makeDAEElementInitial(inElems);
487+ then
488+ DAE.INITIAL_ARRAY_EQUATION(dims,e1,e2,s)::outElems;
489+ case(DAE.EQUATION(e1,e2,s)::inElems)
490+ equation
491+ outElems = makeDAEElementInitial(inElems);
492+ then
493+ DAE.INITIALEQUATION(e1,e2,s)::outElems;
494+ case(DAE.IF_EQUATION(expl,tbs,fb,s)::inElems)
495+ equation
496+ outElems = makeDAEElementInitial(inElems);
497+ then
498+ DAE.INITIAL_IF_EQUATION(expl,tbs,fb,s)::outElems;
499+ case(DAE.ALGORITHM(al,s)::inElems)
500+ equation
501+ outElems = makeDAEElementInitial(inElems);
502+ then
503+ DAE.INITIALALGORITHM(al,s)::outElems;
504+ case(DAE.COMPLEX_EQUATION(e1,e2,s)::inElems)
505+ equation
506+ outElems = makeDAEElementInitial(inElems);
507+ then
508+ DAE.INITIAL_COMPLEX_EQUATION(e1,e2,s)::outElems;
509+ case(elem::inElems) // safe "last case" since we can not fail in cases above.
510+ equation
511+ outElems = makeDAEElementInitial(inElems);
512+ then
513+ elem::outElems;
514+ end matchcontinue;
515+ end makeDAEElementInitial;
516+
517+ public function instantiatePartialClass
398518"Author: BZ, 2009-07
399519 This is a function for instantiating partial 'top' classes.
400520 It does so by converting the partial class into a non partial class.
@@ -626,6 +746,10 @@ algorithm
626746 (cache,env_1,ih,_,dae,_,_,_,_,graph) = instClass(cache,env, ih, UnitAbsynBuilder.emptyInstStore(), DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, c, {}, false, TOP_CALL(), ConnectionGraph.EMPTY) "impl" ;
627747 // deal with Overconstrained connections
628748 dae = ConnectionGraph.handleOverconstrainedConnections(graph, dae);
749+
750+ //print(" ********************** backpatch 2 **********************\n");
751+ dae = reEvaluateInitialIfEqns(cache,env_1,dae,true);
752+
629753 // check the models for balancing
630754 //Debug.fcall2("checkModel",checkModelBalancing,SOME(inPath),dae);
631755 then
@@ -911,6 +1035,9 @@ algorithm
9111035 Debug.fcall("execstat",print, "*** Inst -> instClass finished at time: " +& realString(clock()) +& "\n" );
9121036 // deal with Overconstrained connections
9131037 dae = ConnectionGraph.handleOverconstrainedConnections(graph, dae);
1038+ //print(" ********************** backpatch 3 **********************\n");
1039+ dae = reEvaluateInitialIfEqns(cache,env_1,dae,true);
1040+
9141041 // check the models for balancing
9151042 //Debug.fcall2("checkModel",checkModelBalancing,containedInOpt,dae);
9161043
@@ -920,7 +1047,8 @@ algorithm
9201047 // finish with the execution statistics
9211048 Debug.fcall("execstat",print, "*** Inst -> exit at time: " +& realString(clock()) +& "\n" );
9221049
923- daeElts = DAEUtil.daeElements(dae); funcs = DAEUtil.daeFunctionTree(dae);
1050+ daeElts = DAEUtil.daeElements(dae);
1051+ funcs = DAEUtil.daeFunctionTree(dae);
9241052 dae = DAE.DAE({DAE.COMP(n,daeElts,source)},funcs);
9251053 then
9261054 (cache,ih,dae);
0 commit comments