Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
niklwors committed Jul 1, 2015
2 parents 4609f5a + 69e41fc commit dba5362
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 339 deletions.
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -4636,7 +4636,7 @@ algorithm
end if;
end simplifyLoopExpHelper;

protected function simplifyLoops_SplitTerms
public function simplifyLoops_SplitTerms
input list<DAE.ComponentRef> var_lst;
input DAE.Exp inExp;
output list<DAE.Exp> loopTerms = {};
Expand Down
188 changes: 78 additions & 110 deletions Compiler/BackEnd/Initialization.mo
Expand Up @@ -52,8 +52,8 @@ protected import BackendDAETransform;
protected import BackendDAEUtil;
protected import BackendDump;
protected import BackendEquation;
protected import BackendVariable;
protected import BackendVarTransform;
protected import BackendVariable;
protected import BaseHashSet;
protected import CheckModel;
protected import ComponentReference;
Expand All @@ -66,8 +66,8 @@ protected import ExpressionSimplify;
protected import Flags;
protected import List;
protected import Matching;
protected import Sorting;
protected import SimCodeUtil;
protected import Sorting;

// =============================================================================
// section for all public functions
Expand All @@ -85,28 +85,28 @@ public function solveInitialSystem "author: lochel
output list<BackendDAE.Var> outAllPrimaryParameters "already sorted";
protected
BackendDAE.BackendDAE dae;
BackendDAE.Variables initVars;
BackendDAE.BackendDAE initdae;
BackendDAE.EqSystem initsyst;
BackendDAE.EqSystems systs;
BackendDAE.EquationArray inieqns, eqns, emptyeqns, reeqns;
BackendDAE.ExtraInfo ei;
BackendDAE.Shared shared;
BackendDAE.Variables initVars;
BackendDAE.Variables knvars, vars, fixvars, evars, eavars, avars;
BackendDAE.EquationArray inieqns, eqns, emptyeqns, reeqns;
BackendDAE.EqSystem initsyst;
BackendDAE.BackendDAE initdae;
Boolean b, b1, b2;
Boolean useHomotopy;
DAE.FunctionTree functionTree;
FCore.Cache cache;
FCore.Graph graph;
DAE.FunctionTree functionTree;
list<DAE.Constraint> constraints;
list<DAE.ClassAttributes> classAttrs;
list<BackendDAE.Var> tempVar;
Boolean b, b1, b2;
HashSet.HashSet hs "contains all pre variables";
list<BackendDAE.Equation> removedEqns;
list<BackendDAE.Var> dumpVars, dumpVars2;
list<BackendDAE.Var> tempVar;
list<DAE.ClassAttributes> classAttrs;
list<DAE.Constraint> constraints;
list<tuple<BackendDAEFunc.postOptimizationDAEModule, String, Boolean>> pastOptModules;
tuple<BackendDAEFunc.StructurallySingularSystemHandlerFunc, String, BackendDAEFunc.stateDeselectionFunc, String> daeHandler;
tuple<BackendDAEFunc.matchingAlgorithmFunc, String> matchingAlgorithm;
Boolean useHomotopy;
list<BackendDAE.Var> dumpVars, dumpVars2;
BackendDAE.ExtraInfo ei;
list<BackendDAE.Equation> removedEqns;
algorithm
try
// TODO: remove this once the initialization is moved before post-optimization
Expand Down Expand Up @@ -397,8 +397,6 @@ algorithm
DAE.Exp condition, right, crexp;
BackendDAE.Equation eqn;
list<BackendDAE.Equation> eqns;
BackendDAE.WhenEquation weqn;
BackendDAE.Variables vars;

// active when equation during initialization
case BackendDAE.WHEN_EQ(condition=condition, left=left, right=right) equation
Expand All @@ -425,12 +423,9 @@ protected function inlineWhenForInitializationWhenAlgorithm "author: lochel
algorithm
(outStmts, outLeftCrs) := matchcontinue(inStmts)
local
DAE.Exp condition;
list< DAE.ComponentRef> crefLst;
DAE.Statement stmt;
list< DAE.Statement> stmts, rest;
list<DAE.Statement> stmts, rest;
HashSet.HashSet leftCrs;
list<tuple<DAE.ComponentRef, Integer>> crintLst;

case {}
then (listReverse(inAcc), inLeftCrs);
Expand All @@ -441,7 +436,7 @@ algorithm
// -> take care that for each left hand side an assigment is generated
(stmts, leftCrs) = inlineWhenForInitializationWhenStmt(stmt, inLeftCrs, inAcc);
(stmts, leftCrs) = inlineWhenForInitializationWhenAlgorithm(rest, stmts, leftCrs);
then (stmts, leftCrs);
then (stmts, leftCrs);

// no when statement
case stmt::rest equation
Expand All @@ -461,11 +456,10 @@ algorithm
(outStmts, outLeftCrs) := matchcontinue(inWhenStatement)
local
DAE.Exp condition;
list< DAE.ComponentRef> crefLst;
list<DAE.ComponentRef> crefLst;
DAE.Statement stmt;
list< DAE.Statement> stmts;
list<DAE.Statement> stmts;
HashSet.HashSet leftCrs;
list<tuple<DAE.ComponentRef, Integer>> crintLst;

// active when equation during initialization
case DAE.STMT_WHEN(exp=condition, statementLst=stmts) equation
Expand All @@ -491,7 +485,6 @@ algorithm
else equation
Error.addInternalError("function inlineWhenForInitializationWhenStmt failed", sourceInfo());
then fail();

end matchcontinue;
end inlineWhenForInitializationWhenStmt;

Expand Down Expand Up @@ -547,58 +540,51 @@ public function collectPreVariablesEqSystem
output HashSet.HashSet outHS;
protected
BackendDAE.EquationArray orderedEqs;
BackendDAE.EquationArray eqns;
algorithm
BackendDAE.EQSYSTEM(orderedEqs=orderedEqs) := inEqSystem;
((_,outHS)) := BackendDAEUtil.traverseBackendDAEExpsEqns(orderedEqs, Expression.traverseSubexpressionsHelper, (collectPreVariablesTraverseExp, inHS));
((_, outHS)) := BackendDAEUtil.traverseBackendDAEExpsEqns(orderedEqs, Expression.traverseSubexpressionsHelper, (collectPreVariablesTraverseExp, inHS));
end collectPreVariablesEqSystem;

public function collectPreVariablesTraverseExp
input DAE.Exp e;
input HashSet.HashSet hs;
output DAE.Exp outExp;
output HashSet.HashSet ohs;
input DAE.Exp inExp;
input HashSet.HashSet inHS;
output DAE.Exp outExp = inExp;
output HashSet.HashSet outHS;
algorithm
(outExp,ohs) := match (e,hs)
local
list<DAE.Exp> explst;
case (DAE.CALL(path=Absyn.IDENT(name="pre")), _)
equation
(_, ohs) = Expression.traverseExpBottomUp(e, collectPreVariablesTraverseExp2, hs);
then (e, ohs);

case (DAE.CALL(path=Absyn.IDENT(name="change")), _)
equation
(_, ohs) = Expression.traverseExpBottomUp(e, collectPreVariablesTraverseExp2, hs);
then (e, ohs);

case (DAE.CALL(path=Absyn.IDENT(name="edge")), _)
equation
(_, ohs) = Expression.traverseExpBottomUp(e, collectPreVariablesTraverseExp2, hs);
then (e, ohs);

else (e,hs);
outHS := match (inExp)
case DAE.CALL(path=Absyn.IDENT(name="pre")) equation
(_, outHS) = Expression.traverseExpBottomUp(inExp, collectPreVariablesTraverseExp2, inHS);
then outHS;

case DAE.CALL(path=Absyn.IDENT(name="change")) equation
(_, outHS) = Expression.traverseExpBottomUp(inExp, collectPreVariablesTraverseExp2, inHS);
then outHS;

case DAE.CALL(path=Absyn.IDENT(name="edge")) equation
(_, outHS) = Expression.traverseExpBottomUp(inExp, collectPreVariablesTraverseExp2, inHS);
then outHS;

else inHS;
end match;
end collectPreVariablesTraverseExp;

protected function collectPreVariablesTraverseExp2 "author: lochel"
input DAE.Exp e;
input HashSet.HashSet hs;
output DAE.Exp outExp;
output HashSet.HashSet ohs;
input DAE.Exp inExp;
input HashSet.HashSet inHS;
output DAE.Exp outExp = inExp;
output HashSet.HashSet outHS;
algorithm
(outExp,ohs) := match (e,hs)
outHS := match inExp
local
list<DAE.ComponentRef> crefs;
DAE.ComponentRef cr;

case (DAE.CREF(componentRef=cr), _)
equation
crefs = ComponentReference.expandCref(cr, true);
ohs = List.fold(crefs, BaseHashSet.add, hs);
then (e, ohs);
case DAE.CREF(componentRef=cr) equation
crefs = ComponentReference.expandCref(cr, true);
outHS = List.fold(crefs, BaseHashSet.add, inHS);
then outHS;

else (e,hs);
else inHS;
end match;
end collectPreVariablesTraverseExp2;

Expand Down Expand Up @@ -1850,19 +1836,11 @@ protected function getConsistentEquation "author: mwenzler"
algorithm
(outUnassignedEqns, outConsistent, outRemovedEqns) := matchcontinue(inUnassignedEqn)
local
Integer currEqID, currVarID, currID, nVars, nEqns;
list<Integer> unassignedEqns, unassignedEqns2, listVar, removedEqns;
list<BackendDAE.Equation> eqns_list;
list<BackendDAE.Equation> eqns_list_new;
list<BackendDAE.Equation> eqns_list2;
Integer nVars, nEqns;
list<Integer> listVar;
BackendDAE.EquationArray eqns;
BackendDAE.Equation eqn, eqn2;
DAE.Exp lhs, rhs, exp, x;
String eqStr;
BackendDAE.Var var;
DAE.ComponentRef cref;
BackendDAE.Type type_;
Boolean consistent;
DAE.Exp lhs, rhs, exp;
list<String> listParameter;
BackendDAE.IncidenceMatrix m;
BackendDAE.EqSystem system;
Expand Down Expand Up @@ -1903,7 +1881,7 @@ algorithm
(exp, _) = ExpressionSimplify.simplify(exp);
false = Expression.isZero(exp);

((_, listParameter))=parameterCheck((exp, {}));
((_, listParameter)) = parameterCheck((exp, {}));
true = listEmpty(listParameter);

eqn2 = BackendEquation.equationNth1(inEqnsOrig, inUnassignedEqn);
Expand All @@ -1923,13 +1901,13 @@ algorithm
((_, listParameter))=parameterCheck((exp, {}));
false = listEmpty(listParameter);

list_inEqns=BackendEquation.equationList(inEqns);
list_inEqns = BackendEquation.equationList(inEqns);
list_inEqns = List.set(list_inEqns, inUnassignedEqn, eqn);
eqns = BackendEquation.listEquation(list_inEqns);
funcs = BackendDAEUtil.getFunctions(shared);
system = BackendDAEUtil.createEqSystem(vars, eqns);
(m, _) = BackendDAEUtil.incidenceMatrix(system, BackendDAE.NORMAL(), SOME(funcs));
listVar=m[inUnassignedEqn];
listVar = m[inUnassignedEqn];
false = listEmpty(listVar);

_ = BackendEquation.equationNth1(inEqnsOrig, inUnassignedEqn);
Expand All @@ -1947,7 +1925,7 @@ algorithm
(exp, _) = ExpressionSimplify.simplify(exp);
false = Expression.isZero(exp);

((_, listParameter))=parameterCheck((exp, {}));
((_, listParameter)) = parameterCheck((exp, {}));
false = listEmpty(listParameter);

eqn2 = BackendEquation.equationNth1(inEqnsOrig, inUnassignedEqn);
Expand All @@ -1974,25 +1952,21 @@ protected function parameterCheck2
output DAE.Exp outExp;
output list<String> listParameter;
algorithm
(outExp,listParameter) := match (exp,inParams)
(outExp, listParameter) := match (exp, inParams)
local
DAE.Type ty;
String Para;

case (DAE.CREF(componentRef=DAE.CREF_QUAL(ident=Para)), listParameter)
equation
listParameter=listAppend({Para}, listParameter);
then (exp, listParameter);
case (DAE.CREF(componentRef=DAE.CREF_QUAL(ident=Para)), listParameter) equation
listParameter=listAppend({Para}, listParameter);
then (exp, listParameter);

case (DAE.CREF(componentRef=DAE.CREF_IDENT(ident=Para)), listParameter)
equation
listParameter=listAppend({Para}, listParameter);
then (exp, listParameter);
case (DAE.CREF(componentRef=DAE.CREF_IDENT(ident=Para)), listParameter) equation
listParameter=listAppend({Para}, listParameter);
then (exp, listParameter);

case (DAE.CREF(componentRef=DAE.CREF_ITER(ident=Para)), listParameter)
equation
listParameter=listAppend({Para}, listParameter);
then (exp, listParameter);
case (DAE.CREF(componentRef=DAE.CREF_ITER(ident=Para)), listParameter) equation
listParameter=listAppend({Para}, listParameter);
then (exp, listParameter);

else (exp, inParams);
end match;
Expand Down Expand Up @@ -2100,7 +2074,7 @@ protected function collectInitialVars "author: lochel
output BackendDAE.Var outVar;
output tuple<BackendDAE.Variables, BackendDAE.Variables, BackendDAE.EquationArray, HashSet.HashSet> outTpl;
algorithm
(outVar,outTpl) := matchcontinue (inVar,inTpl)
(outVar, outTpl) := matchcontinue (inVar, inTpl)
local
BackendDAE.Var var, preVar, derVar;
BackendDAE.Variables vars, fixvars;
Expand Down Expand Up @@ -2356,19 +2330,18 @@ end collectInitialVars;
protected function collectInitialEqns "author: lochel"
input BackendDAE.Equation inEq;
input tuple<BackendDAE.EquationArray, BackendDAE.EquationArray> inTpl;
output BackendDAE.Equation eqn;
output BackendDAE.Equation outEq = inEq;
output tuple<BackendDAE.EquationArray, BackendDAE.EquationArray> outTpl;
protected
BackendDAE.Equation eqn1;
BackendDAE.EquationArray eqns, reeqns;
Integer size;
Boolean b;
algorithm
eqn := inEq;
(eqns, reeqns) := inTpl;

// replace der(x) with $DER.x and replace pre(x) with $PRE.x
(eqn1, _) := BackendEquation.traverseExpsOfEquation(eqn, Expression.traverseSubexpressionsDummyHelper, replaceDerPreCref);
(eqn1, _) := BackendEquation.traverseExpsOfEquation(inEq, Expression.traverseSubexpressionsDummyHelper, replaceDerPreCref);

// add it, if size is zero (terminate, assert, noretcall) move to removed equations
size := BackendEquation.equationSize(eqn1);
Expand All @@ -2387,17 +2360,14 @@ algorithm
local
DAE.ComponentRef dummyder, cr;
DAE.Type ty;
Integer i;

case DAE.CALL(path = Absyn.IDENT(name = "der"), expLst = {DAE.CREF(componentRef = cr)}, attr=DAE.CALL_ATTR(ty=ty))
equation
dummyder = ComponentReference.crefPrefixDer(cr);
then DAE.CREF(dummyder, ty);
case DAE.CALL(path = Absyn.IDENT(name="der"), expLst = {DAE.CREF(componentRef=cr)}, attr=DAE.CALL_ATTR(ty=ty)) equation
dummyder = ComponentReference.crefPrefixDer(cr);
then DAE.CREF(dummyder, ty);

case DAE.CALL(path = Absyn.IDENT(name = "pre"), expLst = {DAE.CREF(componentRef = cr)}, attr=DAE.CALL_ATTR(ty=ty))
equation
dummyder = ComponentReference.crefPrefixPre(cr);
then DAE.CREF(dummyder, ty);
case DAE.CALL(path = Absyn.IDENT(name="pre"), expLst = {DAE.CREF(componentRef=cr)}, attr=DAE.CALL_ATTR(ty=ty)) equation
dummyder = ComponentReference.crefPrefixPre(cr);
then DAE.CREF(dummyder, ty);

else inExp;
end match;
Expand All @@ -2415,7 +2385,7 @@ protected function collectInitialBindings "author: lochel
output BackendDAE.Var outVar;
output tuple<BackendDAE.EquationArray, BackendDAE.EquationArray> outTpl;
algorithm
(outVar,outTpl) := match (inVar,inTpl)
(outVar,outTpl) := match (inVar, inTpl)
local
BackendDAE.Var var;
DAE.ComponentRef cr;
Expand All @@ -2436,11 +2406,9 @@ algorithm
eqns = BackendEquation.addEquation(eqn, eqns);
then (var, (eqns, reeqns));

else
equation
Error.addInternalError("function collectInitialBindings failed for: " + BackendDump.varString(inVar), sourceInfo());
then fail();

else equation
Error.addInternalError("function collectInitialBindings failed for: " + BackendDump.varString(inVar), sourceInfo());
then fail();
end match;
end collectInitialBindings;

Expand Down

0 comments on commit dba5362

Please sign in to comment.