Skip to content

Commit

Permalink
- fix bug in copyVariable
Browse files Browse the repository at this point in the history
- use varsSize to adjust the hashtables of Variables
- bugfix infinit loop for undifferentiable subsets

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15398 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Feb 28, 2013
1 parent 355c866 commit 3f1e6e9
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 144 deletions.
76 changes: 38 additions & 38 deletions Compiler/BackEnd/BackendDAECreate.mo
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,19 @@ protected
list<BackendDAE.ZeroCrossing> zero_crossings;
DAE.FunctionTree functionTree;
BackendDAE.SampleLookup sampleLookup;
Integer nVars,nKnVars,nExtVars;
list<BackendDAE.Var> lstVars,lstKnVars,lstExtVars;
algorithm
System.realtimeTick(CevalScript.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
Debug.execStat("Enter Backend", CevalScript.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
functionTree := Env.getFunctionTree(inCache);
(DAE.DAE(elems), functionTree, sampleLookup) := processBuiltinExpressions(lst, functionTree);
vars := BackendVariable.emptyVars();
knvars := BackendVariable.emptyVars();
extVars := BackendVariable.emptyVars();
(vars, knvars, extVars, eqns, reqns, ieqns, constrs, clsAttrs, whenclauses, extObjCls, aliaseqns, _) :=
lower2(listReverse(elems), functionTree, vars, knvars, extVars, {}, {}, {}, {}, {}, {}, {}, {}, HashTableExpToExp.emptyHashTable());
((lstVars,nVars), (lstKnVars,nKnVars), (lstExtVars,nExtVars), eqns, reqns, ieqns, constrs, clsAttrs, whenclauses, extObjCls, aliaseqns, _) :=
lower2(listReverse(elems), functionTree, ({},0), ({},0), ({},0), {}, {}, {}, {}, {}, {}, {}, {}, HashTableExpToExp.emptyHashTable());
// use sized arrays to have good hashing
vars := BackendVariable.listVarSized(lstVars,nVars);
knvars := BackendVariable.listVarSized(lstKnVars,nKnVars);
extVars := BackendVariable.listVarSized(lstExtVars,nExtVars);
whenclauses_1 := listReverse(whenclauses);
aliasVars := BackendVariable.emptyVars();
// handle alias equations
Expand Down Expand Up @@ -155,9 +158,9 @@ protected function lower2
"Helper function to lower."
input list<DAE.Element> inElements "input is in reverse order. this is faster than reversing all accumulators at the end";
input DAE.FunctionTree functionTree;
input BackendDAE.Variables inVars "The time depend Variables";
input BackendDAE.Variables inKnVars "The time independend Variables";
input BackendDAE.Variables inExVars "The external Variables";
input tuple<list<BackendDAE.Var>,Integer> inVars "The time depend Variables";
input tuple<list<BackendDAE.Var>,Integer> inKnVars "The time independend Variables";
input tuple<list<BackendDAE.Var>,Integer> inExVars "The external Variables";
input list<BackendDAE.Equation> inEqnsLst "The dynamic Equations/Algoritms";
input list<BackendDAE.Equation> inREqnsLst "The algebraic Equations";
input list<BackendDAE.Equation> inIEqnsLst "The initial Equations";
Expand All @@ -167,9 +170,9 @@ protected function lower2
input BackendDAE.ExternalObjectClasses inExtObjClasses;
input list<DAE.Element> iAliaseqns "List with all EqualityEquations";
input HashTableExpToExp.HashTable iInlineHT "workaround to speed up inlining of array parameters";
output BackendDAE.Variables outVariables;
output BackendDAE.Variables outKnownVariables;
output BackendDAE.Variables outExternalVariables;
output tuple<list<BackendDAE.Var>,Integer> outVariables;
output tuple<list<BackendDAE.Var>,Integer> outKnownVariables;
output tuple<list<BackendDAE.Var>,Integer> outExternalVariables;
output list<BackendDAE.Equation> oEqnsLst;
output list<BackendDAE.Equation> oREqnsLst;
output list<BackendDAE.Equation> oIEqnsLst;
Expand All @@ -185,7 +188,7 @@ algorithm
match (inElements, functionTree, inVars, inKnVars, inExVars, inEqnsLst, inREqnsLst, inIEqnsLst,
inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT)
local
BackendDAE.Variables vars, knvars, extVars;
tuple<list<BackendDAE.Var>,Integer> vars, knvars, extVars;
list<BackendDAE.WhenClause> whenclauses;
list<BackendDAE.Equation> eqns, reqns, ieqns;
list<DAE.Constraint> constrs;
Expand Down Expand Up @@ -216,9 +219,9 @@ protected function lower3
"Helper function to lower."
input DAE.Element inElement "input is in reverse order. this is faster than reversing all accumulators at the end";
input DAE.FunctionTree functionTree;
input BackendDAE.Variables inVars "The time depend Variables";
input BackendDAE.Variables inKnVars "The time independend Variables";
input BackendDAE.Variables inExVars "The external Variables";
input tuple<list<BackendDAE.Var>,Integer> inVars "The time depend Variables";
input tuple<list<BackendDAE.Var>,Integer> inKnVars "The time independend Variables";
input tuple<list<BackendDAE.Var>,Integer> inExVars "The external Variables";
input list<BackendDAE.Equation> inEqnsLst "The dynamic Equations/Algoritms";
input list<BackendDAE.Equation> inREqnsLst "The algebraic Equations";
input list<BackendDAE.Equation> inIEqnsLst "The initial Equations";
Expand All @@ -228,9 +231,9 @@ protected function lower3
input BackendDAE.ExternalObjectClasses inExtObjClasses;
input list<DAE.Element> iAliaseqns "List with all EqualityEquations";
input HashTableExpToExp.HashTable iInlineHT "workaround to speed up inlining of array parameters";
output BackendDAE.Variables outVariables;
output BackendDAE.Variables outKnownVariables;
output BackendDAE.Variables outExternalVariables;
output tuple<list<BackendDAE.Var>,Integer> outVariables;
output tuple<list<BackendDAE.Var>,Integer> outKnownVariables;
output tuple<list<BackendDAE.Var>,Integer> outExternalVariables;
output list<BackendDAE.Equation> oEqnsLst;
output list<BackendDAE.Equation> oREqnsLst;
output list<BackendDAE.Equation> oIEqnsLst;
Expand All @@ -252,7 +255,7 @@ algorithm
DAE.ClassAttributes clsattrs_1;
list<DAE.ClassAttributes> clsAttrs;
Absyn.Path path;
BackendDAE.Variables vars, knvars, extVars;
tuple<list<BackendDAE.Var>,Integer> vars, knvars, extVars;
BackendDAE.ExternalObjectClasses extObjCls;
BackendDAE.ExternalObjectClass extObjCl;
list<BackendDAE.Equation> eqns, reqns, ieqns;
Expand Down Expand Up @@ -516,14 +519,14 @@ protected function lowerVar
outputs: Var"
input DAE.Element inElement;
input DAE.FunctionTree functionTree;
input BackendDAE.Variables inVars "The time depend Variables";
input BackendDAE.Variables inKnVars "The time independend Variables";
input BackendDAE.Variables inExVars "The external Variables";
input tuple<list<BackendDAE.Var>,Integer> inVars "The time depend Variables";
input tuple<list<BackendDAE.Var>,Integer> inKnVars "The time independend Variables";
input tuple<list<BackendDAE.Var>,Integer> inExVars "The external Variables";
input list<BackendDAE.Equation> inEqnsLst "The dynamic Equations/Algoritms";
input HashTableExpToExp.HashTable iInlineHT "workaround to speed up inlining of array parameters";
output BackendDAE.Variables outVariables;
output BackendDAE.Variables outKnownVariables;
output BackendDAE.Variables outExternalVariables;
output tuple<list<BackendDAE.Var>,Integer> outVariables;
output tuple<list<BackendDAE.Var>,Integer> outKnownVariables;
output tuple<list<BackendDAE.Var>,Integer> outExternalVariables;
output list<BackendDAE.Equation> oEqnsLst;
output HashTableExpToExp.HashTable oInlineHT "workaround to speed up inlining of array parameters";
algorithm
Expand All @@ -534,46 +537,43 @@ algorithm
DAE.ElementSource source;
BackendDAE.Var backendVar1;
DAE.Exp e1, e2;
BackendDAE.Variables vars, knvars, extvars;
list<BackendDAE.Var> vars, knvars, extvars;
Integer nVars,nKnVars,nExtVars;
String str;
HashTableExpToExp.HashTable inlineHT;
// external object variables
case (DAE.VAR(ty = DAE.T_COMPLEX(complexClassType = ClassInf.EXTERNAL_OBJ(path=_))), _, _, _, _, _, _)
case (DAE.VAR(ty = DAE.T_COMPLEX(complexClassType = ClassInf.EXTERNAL_OBJ(path=_))), _, _, _, (extvars,nExtVars), _, _)
equation
backendVar1 = lowerExtObjVar(inElement, functionTree);
extvars = BackendVariable.addVar(backendVar1, inExVars);
then
(inVars, inKnVars, extvars, inEqnsLst, iInlineHT);
(inVars, inKnVars, (backendVar1::extvars,nExtVars+1), inEqnsLst, iInlineHT);

// variables: states and algebraic variables with binding equation
case (DAE.VAR(componentRef = cr, binding=SOME(e2), source = source), _, _, _, _, _, _)
case (DAE.VAR(componentRef = cr, binding=SOME(e2), source = source), _, (vars,nVars), _, _, _, _)
equation
// adrpo 2009-09-07 - according to MathCore
// add the binding as an equation and remove the binding from variable!
true = isStateOrAlgvar(inElement);
(backendVar1) = lowerDynamicVar(inElement, functionTree);
(e2, source, _) = Inline.inlineExp(e2, (SOME(functionTree), {DAE.NORM_INLINE()}), source);
vars = BackendVariable.addVar(backendVar1, inVars);
e1 = Expression.crefExp(cr);
then
(vars, inKnVars, inExVars, BackendDAE.EQUATION(e1, e2, source, false)::inEqnsLst, iInlineHT);
((backendVar1::vars,nVars+1), inKnVars, inExVars, BackendDAE.EQUATION(e1, e2, source, false)::inEqnsLst, iInlineHT);

// variables: states and algebraic variables with NO binding equation
case (DAE.VAR(binding=NONE(), source = source), _, _, _, _, _, _)
case (DAE.VAR(binding=NONE(), source = source), _, (vars,nVars), _, _, _, _)
equation
true = isStateOrAlgvar(inElement);
(backendVar1) = lowerDynamicVar(inElement, functionTree);
vars = BackendVariable.addVar(backendVar1, inVars);
then
(vars, inKnVars, inExVars, inEqnsLst, iInlineHT);
((backendVar1::vars,nVars+1), inKnVars, inExVars, inEqnsLst, iInlineHT);

// known variables: parameters and constants
case (DAE.VAR(componentRef = _), _, _, _, _, _, _)
case (DAE.VAR(componentRef = _), _, _, (knvars,nKnVars), _, _, _)
equation
(backendVar1, inlineHT) = lowerKnownVar(inElement, functionTree, iInlineHT) "in previous rule, lower_var failed." ;
knvars = BackendVariable.addVar(backendVar1, inKnVars);
then
(inVars, knvars, inExVars, inEqnsLst, inlineHT);
(inVars, (backendVar1::knvars,nKnVars+1), inExVars, inEqnsLst, inlineHT);

else
equation
Expand Down
47 changes: 28 additions & 19 deletions Compiler/BackEnd/BackendEquation.mo
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ algorithm
eqns := listEquation({});
end emptyEqns;

public function emptyEqnsSized
input Integer size;
output BackendDAE.EquationArray outEquationArray;
protected
array<Option<BackendDAE.Equation>> optarr;
algorithm
optarr := arrayCreate(size, NONE());
outEquationArray := BackendDAE.EQUATION_ARRAY(0,0,size,optarr);
end emptyEqnsSized;

public function equationList "function equationList
author: PA
Transform the expandable BackendDAE.Equation array to a list of Equations."
Expand Down Expand Up @@ -1784,13 +1794,13 @@ public function compressEquations
algorithm
outEquationArray := matchcontinue(inEquationArray)
local
Integer size,numberOfElement,arrSize;
Integer numberOfElement,arrSize;
array<Option<BackendDAE.Equation>> equOptArr;
case BackendDAE.EQUATION_ARRAY(size=size,numberOfElement=numberOfElement,arrSize=arrSize,equOptArr=equOptArr)
case BackendDAE.EQUATION_ARRAY(numberOfElement=numberOfElement,equOptArr=equOptArr)
equation
numberOfElement = compressEquations1(1,1,numberOfElement,equOptArr);
outEquationArray = emptyEqnsSized(numberOfElement);
then
BackendDAE.EQUATION_ARRAY(size,numberOfElement,arrSize,equOptArr);
compressEquations1(1,numberOfElement,equOptArr,outEquationArray);
else
equation
print("BackendEquation.compressEquations failed\n");
Expand All @@ -1801,47 +1811,46 @@ end compressEquations;

protected function compressEquations1
" function: compressEquations1
author: Frenkel TUD 2012-11"
author: Frenkel TUD 2012-09"
input Integer index;
input Integer insertindex;
input Integer numberOfElement;
input Integer nEqns;
input array<Option<BackendDAE.Equation>> equOptArr;
output Integer newnumberOfElement;
input BackendDAE.EquationArray iEqns;
output BackendDAE.EquationArray oEqns;
algorithm
newnumberOfElement := matchcontinue(index,insertindex,numberOfElement,equOptArr)
oEqns := matchcontinue(index,nEqns,equOptArr,iEqns)
local
BackendDAE.Equation eqn;
BackendDAE.EquationArray eqns;
// found element
case(_,_,_,_)
equation
true = intLe(index,numberOfElement);
true = intLe(index,nEqns);
SOME(eqn) = equOptArr[index];
// insert on new pos
_ = arrayUpdate(equOptArr,insertindex,SOME(eqn));
eqns = equationAdd(eqn,iEqns);
then
compressEquations1(index+1,insertindex+1,numberOfElement,equOptArr);
compressEquations1(index+1,nEqns,equOptArr,eqns);
// found non element
case(_,_,_,_)
equation
true = intLe(index,numberOfElement);
true = intLe(index,nEqns);
NONE() = equOptArr[index];
then
compressEquations1(index+1,insertindex,numberOfElement,equOptArr);
compressEquations1(index+1,nEqns,equOptArr,iEqns);
// at the end
case(_,_,_,_)
equation
false = intLe(index,numberOfElement);
false = intLe(index,nEqns);
then
insertindex-1;
iEqns;
else
equation
print("BackendEquation.compressEquations1 failed\n");
print("BackendEquation.compressEquations1 failed for index " +& intString(index) +& " and Number of Equations " +& intString(nEqns) +& "\n");
then
fail();
end matchcontinue;
end compressEquations1;


public function equationToScalarResidualForm "function: equationToScalarResidualForm
author: Frenkel TUD 2012-06
This function transforms an equation to its scalar residual form.
Expand Down

0 comments on commit 3f1e6e9

Please sign in to comment.