Skip to content

Commit

Permalink
* Fixed initial array equations
Browse files Browse the repository at this point in the history
* Activated back patching of initial if equations with Connection.isRoot() as statements. 

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@5117 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Björn Zachrisson committed Mar 10, 2010
1 parent 5b6f8ef commit fe850cb
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 11 deletions.
29 changes: 21 additions & 8 deletions Compiler/DAEUtil.mo
Expand Up @@ -840,7 +840,14 @@ algorithm
then
();
case (DAE.DAE(elementLst = {})) then ();


//BZ Could be nice to know when this failes (when new elements are introduced)
case(DAE.DAE((_ :: xs),funcs))
equation
Print.printBuf("\n\ndump2 failed to print element\n");
dump2(DAE.DAE(xs,funcs));
then
();
case (_)
equation
Print.printBuf("dump2 failed\n");
Expand Down Expand Up @@ -1078,6 +1085,18 @@ algorithm
Util.listMap0(l, dumpAlgorithm);
end dumpFunctionElements;

public function dumpDAEElementsStr "
Author BZ
print a DAE.DAEList to a string
"
input DAE.DAElist d;
output String str;
algorithm str := matchcontinue(d)
local list<DAE.Element> l;
case(DAE.DAE(elementLst=l)) then dumpElementsStr(l);
end matchcontinue;
end dumpDAEElementsStr;

public function dumpElementsStr "function: dumpElementsStr
Dump elements to a string"
input list<DAE.Element> l;
Expand Down Expand Up @@ -1366,7 +1385,7 @@ algorithm
s4 = Exp.printExpStr(e2);
s4_1 = stringAppend(s3, s4);
s5 = stringAppend(s4_1, ";\n");
s6 = dumpEquationsStr(xs);
s6 = dumpInitialequationsStr(xs);
str = stringAppend(s5, s6);
then
str;
Expand Down Expand Up @@ -2401,12 +2420,6 @@ algorithm
str = s1 +& " = " +& s2;
then str;

case(DAE.INITIAL_ARRAY_EQUATION(exp=e1,array=e2)) equation
s1 = Exp.printExpStr(e1);
s2 = Exp.printExpStr(e2);
str = s1 +& " = " +& s2;
then str;

case(DAE.COMPLEX_EQUATION(lhs=e1,rhs=e2)) equation
s1 = Exp.printExpStr(e1);
s2 = Exp.printExpStr(e2);
Expand Down
132 changes: 130 additions & 2 deletions Compiler/Inst.mo
Expand Up @@ -394,7 +394,127 @@ algorithm
end matchcontinue;
end instantiateClass;

public function instantiatePartialClass
protected function reEvaluateInitialIfEqns "
Author BZ
This is a backpatch to fix the case of 'connection.isRoot' in initial if equations.
After the class is instantiated a second sweep is done to check the initial if equations conditions.
If all conditions are constand, we return only the 'correct' branch equations.
"
input Env.Cache cache;
input Env.Env env;
input DAE.DAElist dae;
input Boolean isTopCall;
output DAE.DAElist odae;
algorithm odae := matchcontinue(cache,env,dae,isTopCall)
local
DAE.FunctionTree funcs;
list<DAE.Element> elems;
case(cache,env,DAE.DAE(elementLst = elems,functions = funcs),true)
equation
elems = reEvaluateInitialIfEqns2(cache,env,elems);
then
DAE.DAE(elems,funcs);
case(_,_,dae,false) then dae;
end matchcontinue;
end reEvaluateInitialIfEqns;

protected function reEvaluateInitialIfEqns2 ""
input Env.Cache cache;
input Env.Env env;
input list<DAE.Element> elems;
output list<DAE.Element> oelems;
algorithm oelems := matchcontinue(cache,env,elems)
local
list<DAE.Exp> conds;
list<Values.Value> valList;
list<list<DAE.Element>> tbs;
list<DAE.Element> fb,selectedBranch;
DAE.Element elem;
DAE.ElementSource source;
list<Boolean> blist;
case(_,_,{}) then {};
case(cache,env,(elem as DAE.INITIAL_IF_EQUATION(condition1 = conds, equations2=tbs, equations3=fb, source=source))::elems)
equation
//print(" (Initial if)To ceval: " +& Util.stringDelimitList(Util.listMap(conds,Exp.printExpStr),", ") +& "\n");
(cache,valList) = Ceval.cevalList(cache,env, conds, true, NONE, Ceval.NO_MSG());
//print(" Ceval res: ("+&Util.stringDelimitList(Util.listMap(valList,ValuesUtil.printValStr),",")+&")\n");

blist = Util.listMap(valList,ValuesUtil.valueBool);
selectedBranch = selectList(blist, tbs, fb);
selectedBranch = makeDAEElementInitial(selectedBranch);
oelems = reEvaluateInitialIfEqns2(cache,env,elems);
oelems = listAppend(selectedBranch,oelems);

//print("RETURN _INITIAL_ DAE: " +& DAEUtil.dumpDAEElementsStr(DAE.DAE(selectedBranch,DAE.AVLTREENODE(NONE,0,NONE,NONE))) +& "\n");
//print(" INSTEAD OF: " +& DAEUtil.dumpDAEElementsStr(DAE.DAE({elem},DAE.AVLTREENODE(NONE,0,NONE,NONE))) +& "\n");
then
oelems;
case(cache,env,elem::elems)
equation
oelems = reEvaluateInitialIfEqns2(cache,env,elems);
then
elem::oelems;
end matchcontinue;
end reEvaluateInitialIfEqns2;

protected function makeDAEElementInitial "
Author BZ
Helper function for reEvaluateInitialIfEqns, makes the contenst of an initial if equation initial.
"
input list<DAE.Element> inElems;
output list<DAE.Element> outElems;
algorithm
outElems := matchcontinue(inElems)
local
DAE.Element elem;
DAE.ComponentRef cr;
DAE.Exp e1,e2,e3;
DAE.ElementSource s;
list<DAE.Exp> expl;
list<list<DAE.Element>> tbs ;
list<DAE.Element> fb;
DAE.Algorithm al;
list<Integer> dims;
case({}) then {};
case(DAE.DEFINE(cr,e1,s)::inElems)
equation
outElems = makeDAEElementInitial(inElems);
then
DAE.INITIALDEFINE(cr,e1,s)::outElems;
case(DAE.ARRAY_EQUATION(dims,e1,e2,s)::_)
equation
outElems = makeDAEElementInitial(inElems);
then
DAE.INITIAL_ARRAY_EQUATION(dims,e1,e2,s)::outElems;
case(DAE.EQUATION(e1,e2,s)::inElems)
equation
outElems = makeDAEElementInitial(inElems);
then
DAE.INITIALEQUATION(e1,e2,s)::outElems;
case(DAE.IF_EQUATION(expl,tbs,fb,s)::inElems)
equation
outElems = makeDAEElementInitial(inElems);
then
DAE.INITIAL_IF_EQUATION(expl,tbs,fb,s)::outElems;
case(DAE.ALGORITHM(al,s)::inElems)
equation
outElems = makeDAEElementInitial(inElems);
then
DAE.INITIALALGORITHM(al,s)::outElems;
case(DAE.COMPLEX_EQUATION(e1,e2,s)::inElems)
equation
outElems = makeDAEElementInitial(inElems);
then
DAE.INITIAL_COMPLEX_EQUATION(e1,e2,s)::outElems;
case(elem::inElems) // safe "last case" since we can not fail in cases above.
equation
outElems = makeDAEElementInitial(inElems);
then
elem::outElems;
end matchcontinue;
end makeDAEElementInitial;

public function instantiatePartialClass
"Author: BZ, 2009-07
This is a function for instantiating partial 'top' classes.
It does so by converting the partial class into a non partial class.
Expand Down Expand Up @@ -626,6 +746,10 @@ algorithm
(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" ;
// deal with Overconstrained connections
dae = ConnectionGraph.handleOverconstrainedConnections(graph, dae);

//print(" ********************** backpatch 2 **********************\n");
dae = reEvaluateInitialIfEqns(cache,env_1,dae,true);

// check the models for balancing
//Debug.fcall2("checkModel",checkModelBalancing,SOME(inPath),dae);
then
Expand Down Expand Up @@ -911,6 +1035,9 @@ algorithm
Debug.fcall("execstat",print, "*** Inst -> instClass finished at time: " +& realString(clock()) +& "\n" );
// deal with Overconstrained connections
dae = ConnectionGraph.handleOverconstrainedConnections(graph, dae);
//print(" ********************** backpatch 3 **********************\n");
dae = reEvaluateInitialIfEqns(cache,env_1,dae,true);

// check the models for balancing
//Debug.fcall2("checkModel",checkModelBalancing,containedInOpt,dae);

Expand All @@ -920,7 +1047,8 @@ algorithm
// finish with the execution statistics
Debug.fcall("execstat",print, "*** Inst -> exit at time: " +& realString(clock()) +& "\n" );

daeElts = DAEUtil.daeElements(dae); funcs = DAEUtil.daeFunctionTree(dae);
daeElts = DAEUtil.daeElements(dae);
funcs = DAEUtil.daeFunctionTree(dae);
dae = DAE.DAE({DAE.COMP(n,daeElts,source)},funcs);
then
(cache,ih,dae);
Expand Down
30 changes: 29 additions & 1 deletion Compiler/Util.mo
Expand Up @@ -2073,7 +2073,35 @@ algorithm
end matchcontinue;
end listListMap2;

public function listFold "function: listFold
public function listFoldList "
Author BZ
apply a function on the heads of two equally length list of generic type.
"
input list<Type_a> lst1;
input list<Type_a> lst2;
input listAddFunc func;
output list<Type_a> mergedList;
partial function listAddFunc
input Type_a ia1;
input Type_a ia2;
output Type_a oa1;
end listAddFunc;
replaceable type Type_a subtypeof Any;
algorithm
mergedList := matchcontinue(lst1,lst2,func)
local
Type_a a1,a2,aRes;
case({},{},_) then {};
case(a1::lst1,a2::lst2,func)
equation
aRes = func(a1,a2);
mergedList = listFoldList(lst1,lst2,func);
then
aRes::mergedList;
end matchcontinue;
end listFoldList;

public function listFold "function: listFold
Takes a list and a function operating on list elements having an extra argument that is \'updated\'
thus returned from the function. The third argument is the startvalue for the updated value.
listFold will call the function for each element in a sequence, updating the startvalue
Expand Down

0 comments on commit fe850cb

Please sign in to comment.