Skip to content

Commit

Permalink
- add some functions, needed for further improvements
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15520 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Mar 9, 2013
1 parent 540b235 commit 71f4f98
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 80 deletions.
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
129 changes: 79 additions & 50 deletions Compiler/BackEnd/BackendVariable.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2334,7 +2334,7 @@ algorithm
equation
(n < size) = true "Have space to add array elt." ;
n_1 = n + 1;
arr_1 = arrayUpdate(arr, n + 1, SOME(v));
arr_1 = arrayUpdate(arr, n_1, SOME(v));
then
BackendDAE.VARIABLE_ARRAY(n_1,size,arr_1);
case (BackendDAE.VARIABLE_ARRAY(numberOfElements = n,arrSize = size,varOptArr = arr),v)
Expand All @@ -2347,7 +2347,7 @@ algorithm
newsize = expandsize_1 + size;
arr_1 = Util.arrayExpand(expandsize_1, arr,NONE());
n_1 = n + 1;
arr_2 = arrayUpdate(arr_1, n + 1, SOME(v));
arr_2 = arrayUpdate(arr_1, n_1, SOME(v));
then
BackendDAE.VARIABLE_ARRAY(n_1,newsize,arr_2);
case (BackendDAE.VARIABLE_ARRAY(numberOfElements = n,arrSize = size,varOptArr = arr),_)
Expand Down Expand Up @@ -2452,6 +2452,25 @@ algorithm
outVariables := BackendDAE.VARIABLES(arr,BackendDAE.VARIABLE_ARRAY(0, arrSize, emptyarr), bucketSize, 0);
end emptyVars;

public function emptyVarsSized "function emptyVarsSized
author: Frenkel TUD 2013-02
Returns a Variable datastructure that is empty.
Using the bucketsize 10000 and array size 1000."
input Integer size;
output BackendDAE.Variables outVariables;
protected
array<list<BackendDAE.CrefIndex>> arr;
list<Option<BackendDAE.Var>> lst;
array<Option<BackendDAE.Var>> emptyarr;
Integer bucketSize, arrSize;
algorithm
arrSize := intMax(BaseHashTable.lowBucketSize,size);
bucketSize := realInt(realMul(intReal(arrSize), 1.4));
arr := arrayCreate(bucketSize, {});
emptyarr := arrayCreate(arrSize, NONE());
outVariables := BackendDAE.VARIABLES(arr,BackendDAE.VARIABLE_ARRAY(0, arrSize, emptyarr), bucketSize, 0);
end emptyVarsSized;

public function varList
"function: varList
Takes BackendDAE.Variables and returns a list of \'Var\', useful for e.g. dumping."
Expand Down Expand Up @@ -2496,14 +2515,27 @@ algorithm
end match;
end listVar;

public function listVarSized "function listVarSized
author: Frenkel TUD 2012-05
Takes BackendDAE.Var list and creates a BackendDAE.Variables structure, see also var_list."
input list<BackendDAE.Var> inVarLst;
input Integer size;
output BackendDAE.Variables outVariables;
algorithm
outVariables := List.fold(inVarLst,addVar,emptyVarsSized(size));
end listVarSized;

public function listVar1 "function listVar1
author: Frenkel TUD 2012-05
ToDo: replace all listVar calls with this function, tailrecursive implementation
Takes BackendDAE.Var list and creates a BackendDAE.Variables structure, see also var_list."
input list<BackendDAE.Var> inVarLst;
output BackendDAE.Variables outVariables;
protected
Integer size;
algorithm
outVariables := List.fold(inVarLst,addVar,emptyVars());
size := listLength(inVarLst);
outVariables := List.fold(inVarLst,addVar,emptyVarsSized(size));
end listVar1;

public function equationSystemsVarsLst
Expand Down Expand Up @@ -2927,75 +2959,58 @@ public function compressVariables
algorithm
oVars := matchcontinue(iVars)
local
array<list<BackendDAE.CrefIndex>> crefIdxLstArr;
Integer bucketSize,nvars,arrSize,nvars1;
Integer arrSize,size;
array<Option<BackendDAE.Var>> varOptArr;
BackendDAE.VariableArray varArr;
case(BackendDAE.VARIABLES(varArr=BackendDAE.VARIABLE_ARRAY(nvars,arrSize,varOptArr),bucketSize=bucketSize))
equation
crefIdxLstArr = arrayCreate(bucketSize, {});
nvars1 = compressVariables1(1,nvars,1,varOptArr,crefIdxLstArr,bucketSize);
// clear old elements
varOptArr = Util.arraySet(nvars1+1,nvars,varOptArr,NONE());
// generate updated data structs
varArr = BackendDAE.VARIABLE_ARRAY(nvars1,arrSize,varOptArr);
case(BackendDAE.VARIABLES(varArr=BackendDAE.VARIABLE_ARRAY(numberOfElements=arrSize,varOptArr=varOptArr),numberOfVars=size))
equation
oVars = emptyVarsSized(size);
then
BackendDAE.VARIABLES(crefIdxLstArr, varArr, bucketSize, nvars1);
compressVariables1(1,size,varOptArr,oVars);
else
equation
print("compressVariables failed\n");
print("BackendVariable.compressVariables failed\n");
then
fail();
fail();
end matchcontinue;
end compressVariables;

protected function compressVariables1
" function: compressVariables1
author: Frenkel TUD 2012-09"
input Integer index;
input Integer numberOfElement;
input Integer insertindex;
input Integer nVars;
input array<Option<BackendDAE.Var>> varOptArr;
input array<list<BackendDAE.CrefIndex>> crefIdxLstArr;
input Integer bucketSize;
output Integer newnumberOfElement;
input BackendDAE.Variables iVars;
output BackendDAE.Variables oVars;
algorithm
newnumberOfElement := matchcontinue(index,numberOfElement,insertindex,varOptArr,crefIdxLstArr,bucketSize)
oVars := matchcontinue(index,nVars,varOptArr,iVars)
local
BackendDAE.Var var;
DAE.ComponentRef cr;
Integer e,indx,i;
list<BackendDAE.CrefIndex> indexes;
BackendDAE.Variables vars;
// found element
case(_,_,_,_,_,_)
equation
true = intLe(index,numberOfElement);
SOME(var as BackendDAE.VAR(varName=cr)) = varOptArr[index];
// insert on new pos
_ = arrayUpdate(varOptArr,insertindex,SOME(var));
// add to hash vec
indx = ComponentReference.hashComponentRefMod(cr, bucketSize);
indexes = crefIdxLstArr[indx + 1];
i = insertindex-1;
_ = arrayUpdate(crefIdxLstArr, indx + 1, (BackendDAE.CREFINDEX(cr,i) :: indexes));
case(_,_,_,_)
equation
true = intLe(index,nVars);
SOME(var) = varOptArr[index];
vars = addVar(var,iVars);
then
compressVariables1(index+1,numberOfElement,insertindex+1,varOptArr,crefIdxLstArr,bucketSize);
compressVariables1(index+1,nVars,varOptArr,vars);
// found non element
case(_,_,_,_,_,_)
case(_,_,_,_)
equation
true = intLe(index,numberOfElement);
true = intLe(index,nVars);
NONE() = varOptArr[index];
then
compressVariables1(index+1,numberOfElement,insertindex,varOptArr,crefIdxLstArr,bucketSize);
compressVariables1(index+1,nVars,varOptArr,iVars);
// at the end
case(_,_,_,_,_,_)
case(_,_,_,_)
equation
false = intLe(index,numberOfElement);
false = intLe(index,nVars);
then
insertindex-1;
iVars;
else
equation
print("compressVariables1 failed\n");
print("BackendVariable.compressVariables1 failed for index " +& intString(index) +& " and Number of Variables " +& intString(nVars) +& "\n");
then
fail();
end matchcontinue;
Expand Down Expand Up @@ -3147,10 +3162,11 @@ algorithm
failure((_,_) = getVar(cr, vars));
// print("adding when not existing previously\n");
indx = ComponentReference.hashComponentRefMod(cr, bsize);
indx_1 = indx + 1;
newpos = vararrayLength(varr);
varr_1 = vararrayAdd(varr, v);
indexes = hashvec[indx + 1];
hashvec_1 = arrayUpdate(hashvec, indx + 1, (BackendDAE.CREFINDEX(cr,newpos) :: indexes));
indexes = hashvec[indx_1];
hashvec_1 = arrayUpdate(hashvec, indx_1, (BackendDAE.CREFINDEX(cr,newpos) :: indexes));
n_1 = vararrayLength(varr_1);
//fastht = BaseHashTable.add((cr,{newpos}),fastht);
then
Expand Down Expand Up @@ -3692,9 +3708,9 @@ algorithm
case (vars1,vars2)
equation
// to avoid side effects from arrays copy first
vars1 = copyVariables(vars1);
varlst = varList(vars2);
vars1_1 = List.fold(varlst, addVar, vars1);
vars1_1 = emptyVarsSized(varsSize(vars1)+varsSize(vars2));
vars1_1 = traverseBackendDAEVars(vars1, mergeVariables1, vars1_1);
vars1_1 = traverseBackendDAEVars(vars2, mergeVariables1, vars1_1);
then
vars1_1;
case (_,_)
Expand All @@ -3705,6 +3721,19 @@ algorithm
end matchcontinue;
end mergeVariables;

protected function mergeVariables1
"author: Frenkel TUD 2013-02"
input tuple<BackendDAE.Var, BackendDAE.Variables> inTpl;
output tuple<BackendDAE.Var, BackendDAE.Variables> outTpl;
protected
BackendDAE.Var v;
BackendDAE.Variables vars;
algorithm
(v,vars) := inTpl;
vars := addVar(v,vars);
outTpl := (v,vars);
end mergeVariables1;

public function traverseBackendDAEVars "function: traverseBackendDAEVars
author: Frenkel TUD

Expand Down
18 changes: 7 additions & 11 deletions Compiler/BackEnd/Uncertainties.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1891,8 +1891,8 @@ algorithm
case (e::eqns,_,_,_,_,_,_,_,_,false) equation
//true = RTOpts.eliminationLevel() > 0;
//false = equationHasZeroCrossing(e);
({e},_) = BackendVarTransform.replaceEquations({e},repl,NONE());

({e},_) = BackendVarTransform.replaceEquations({e},repl,NONE()) "this can be dangerous in case of if-equations, because the can be simplified to a list of equations";
// Attempt to solve the equation wrt to the variables to be eliminated.
varIndexList = m[eqnIndex];
(elimVarIndex :: _) = List.intersectionOnTrue(varIndexList, elimVarIndexList, intEq);
Expand Down Expand Up @@ -2057,24 +2057,20 @@ algorithm
DAE.FunctionTree funcs;
BackendDAE.EventInfo eventInfo "eventInfo" ;
BackendDAE.ExternalObjectClasses extObjClasses "classes of external objects, contains constructor & destructor";
BackendDAE.BackendDAEType backendDAEType "indicate for what the BackendDAE is used";
BackendDAE.BackendDAEType backendDAEType "indicate for what the BackendDAE is used";
BackendDAE.SymbolicJacobians symjacs;
list<BackendDAE.Equation> eqnslst;
Boolean b;
BackendDAE.StateSets stateSets;

case(BackendDAE.DAE(
(syst as BackendDAE.EQSYSTEM(orderedVars=orderedVars,orderedEqs=orderedEqs,m=m,mT=mT,matching=matching,stateSets=stateSets))::systList,
(shared as BackendDAE.SHARED(knownVars=knownVars,externalObjects=externalObjects,aliasVars=aliasVars,initialEqs=initialEqs,
removedEqs=removedEqs,constraints=constraints,classAttrs=classAttrs,cache=cache,env=env,
functionTree=funcs,eventInfo=eventInfo,extObjClasses=extObjClasses,backendDAEType=backendDAEType,symjacs=symjacs))),_,_,_)
functionTree=funcs,eventInfo=eventInfo,extObjClasses=extObjClasses,backendDAEType=backendDAEType,symjacs=symjacs))),_,_,_)
equation
orderedVars = BackendVariable.listVar1(replaceVars(BackendVariable.varList(orderedVars),repl,func,replaceVariables));
eqnslst = BackendEquation.equationList(orderedEqs);
(eqnslst,b) = BackendVarTransform.replaceEquations(eqnslst,repl,NONE());
orderedEqs = Debug.bcallret1(b,BackendEquation.listEquation,eqnslst,orderedEqs);
(orderedEqs,_) = BackendVarTransform.replaceEquationsArr(orderedEqs,repl,NONE());
syst = BackendDAE.EQSYSTEM(orderedVars,orderedEqs,m,mT,matching,stateSets);
shared = BackendDAE.SHARED(knownVars,externalObjects,aliasVars,initialEqs,removedEqs,constraints,classAttrs,cache,env,funcs,eventInfo,extObjClasses,backendDAEType,symjacs);
shared = BackendDAE.SHARED(knownVars,externalObjects,aliasVars,initialEqs,removedEqs,constraints,classAttrs,cache,env,funcs,eventInfo,extObjClasses,backendDAEType,symjacs);
then
BackendDAE.DAE(syst::systList,shared);

Expand Down

0 comments on commit 71f4f98

Please sign in to comment.