Skip to content

Commit

Permalink
- Util.arrayMap now works on empty arrays
Browse files Browse the repository at this point in the history
- BackendDAEUtil.incidenceMatrix now works directly on arrays and can generate normal or absolute index values.
- made some other backend functions work on arrays directly.
- some other memory usage improvements



git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@6998 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Nov 13, 2010
1 parent e0cfa05 commit 28a46b0
Show file tree
Hide file tree
Showing 10 changed files with 353 additions and 158 deletions.
6 changes: 6 additions & 0 deletions Compiler/BackendDAE.mo
Expand Up @@ -359,6 +359,12 @@ type Key = .DAE.ComponentRef "A key is a Component Reference";
public
type Value = Integer "- Value" ;

public
uniontype IndexType
record ABSOLUTE "produce incidence matrix with absolute indexes" end ABSOLUTE;
record NORMAL "produce incidence matrix with positive/negative indexes" end NORMAL;
end IndexType;

public
type IncidenceMatrix = array<list<Integer>>;

Expand Down
6 changes: 3 additions & 3 deletions Compiler/BackendDAEOptimize.mo
Expand Up @@ -759,7 +759,7 @@ algorithm
v2_2 = Util.arrayNCopy(v2_1, v2_2,ll);
v1_2 = arrayCreate(ll, 0);
v1_2 = Util.arrayNCopy(v1_1, v1_2,ll);
m_3 = BackendDAEUtil.incidenceMatrix(dlow1_1);
m_3 = BackendDAEUtil.incidenceMatrix(dlow1_1, BackendDAE.NORMAL());
mT_3 = BackendDAEUtil.transposeMatrix(m_3);
(v1_3,v2_3) = correctAssignments(v1_2,v2_2,residualeqns,tearingvars);
// next Block
Expand Down Expand Up @@ -1016,7 +1016,7 @@ algorithm
dlow_1 = BackendDAE.DAE(vars_1,knvars,exobj,av,eqns_2,remeqns,inieqns,arreqns,algorithms,einfo,eoc);
dlow1_1 = BackendDAE.DAE(ordvars1,knvars,exobj,av,eqns1_1,remeqns,inieqns,arreqns,algorithms,einfo,eoc);
// try causalisation
m_1 = BackendDAEUtil.incidenceMatrix(dlow_1);
m_1 = BackendDAEUtil.incidenceMatrix(dlow_1, BackendDAE.NORMAL());
mT_1 = BackendDAEUtil.transposeMatrix(m_1);
nvars = arrayLength(m_1);
neqns = arrayLength(mT_1);
Expand Down Expand Up @@ -1417,7 +1417,7 @@ algorithm
dlow = BackendDAE.DAE(v,kv,exv,av,e,re,ie,ae,al,ev,eoc);

// figure out new matching and the strong components
m = BackendDAEUtil.incidenceMatrix(dlow);
m = BackendDAEUtil.incidenceMatrix(dlow, BackendDAE.NORMAL());
mT = BackendDAEUtil.transposeMatrix(m);
(v1,v2,dlow,m,mT) = BackendDAETransform.matchingAlgorithm(dlow, m, mT, (BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT(), BackendDAE.KEEP_SIMPLE_EQN()),functionTree);
Debug.fcall("jacdump2", BackendDump.dumpIncidenceMatrix, m);
Expand Down
10 changes: 5 additions & 5 deletions Compiler/BackendDAETransform.mo
Expand Up @@ -170,9 +170,10 @@ algorithm
ae = listArray(ae_lst);
einfo = BackendDAE.EVENT_INFO(whenclauses,zero_crossings);
dae_1 = BackendDAE.DAE(v,kv,exv,av,e,re,ie,ae,al,einfo,eoc);
m_1 = BackendDAEUtil.incidenceMatrix(dae_1) "Rerun matching to get updated assignments and incidence matrices
TODO: instead of rerunning: find out which equations are removed
and remove those from assignments and incidence matrix." ;
m_1 = BackendDAEUtil.incidenceMatrix(dae_1, BackendDAE.NORMAL())
"Rerun matching to get updated assignments and incidence matrices
TODO: instead of rerunning: find out which equations are removed
and remove those from assignments and incidence matrix.";
mt_1 = BackendDAEUtil.transposeMatrix(m_1);
nvars = arrayLength(m_1);
neqns = arrayLength(mt_1);
Expand Down Expand Up @@ -304,8 +305,7 @@ public function assignmentsCreate
list<BackendDAE.Value> lst;
array<BackendDAE.Value> arr;
algorithm
lst := Util.listFill(0, memsize);
arr := listArray(lst) " array_create(memsize,v) => arr &" ;
arr := arrayCreate(memsize, 0);
outAssignments := BackendDAE.ASSIGNMENTS(n,memsize,arr);
end assignmentsCreate;

Expand Down
155 changes: 101 additions & 54 deletions Compiler/BackendDAEUtil.mo
Expand Up @@ -659,8 +659,7 @@ public function emptyVars
algorithm
arr := arrayCreate(10, {});
arr2 := arrayCreate(10, {});
lst := Util.listFill(NONE(), 10);
emptyarr := listArray(lst);
emptyarr := arrayCreate(10, NONE());
outVariables := BackendDAE.VARIABLES(arr,arr2,BackendDAE.VARIABLE_ARRAY(0,10,emptyarr),10,0);
end emptyVars;

Expand All @@ -676,31 +675,32 @@ end emptyAliasVariables;

public function equationList "function: equationList
author: PA

Transform the expandable BackendDAE.Equation array to a list of Equations.
"
Transform the expandable BackendDAE.Equation array to a list of Equations."
input BackendDAE.EquationArray inEquationArray;
output list<BackendDAE.Equation> outEquationLst;
algorithm
outEquationLst:=
matchcontinue (inEquationArray)
outEquationLst := matchcontinue (inEquationArray)
local
array<Option<BackendDAE.Equation>> arr;
BackendDAE.Equation elt;
BackendDAE.Value lastpos,n,size;
list<BackendDAE.Equation> lst;

case (BackendDAE.EQUATION_ARRAY(numberOfElement = 0,equOptArr = arr)) then {};

case (BackendDAE.EQUATION_ARRAY(numberOfElement = 1,equOptArr = arr))
equation
SOME(elt) = arr[0 + 1];
then
{elt};

case (BackendDAE.EQUATION_ARRAY(numberOfElement = n,arrSize = size,equOptArr = arr))
equation
lastpos = n - 1;
lst = equationList2(arr, 0, lastpos);
then
lst;

case (_)
equation
print("- BackendDAEUtil.equationList failed\n");
Expand All @@ -709,27 +709,6 @@ algorithm
end matchcontinue;
end equationList;

public function listEquation "function: listEquation
author: PA
Transform the a list of Equations into an expandable BackendDAE.Equation array."
input list<BackendDAE.Equation> lst;
output BackendDAE.EquationArray outEquationArray;
BackendDAE.Value len,size;
Real rlen,rlen_1;
array<Option<BackendDAE.Equation>> optarr,eqnarr,newarr;
list<Option<BackendDAE.Equation>> eqn_optlst;
algorithm
len := listLength(lst);
rlen := intReal(len);
rlen_1 := rlen *. 1.4;
size := realInt(rlen_1);
optarr := arrayCreate(size, NONE());
eqn_optlst := Util.listMap(lst, Util.makeOption);
eqnarr := listArray(eqn_optlst);
newarr := Util.arrayCopy(eqnarr, optarr);
outEquationArray := BackendDAE.EQUATION_ARRAY(len,size,newarr);
end listEquation;

protected function equationList2 "function: equationList2
author: PA
Helper function to equationList
Expand Down Expand Up @@ -764,6 +743,27 @@ algorithm
end matchcontinue;
end equationList2;

public function listEquation "function: listEquation
author: PA
Transform the a list of Equations into an expandable BackendDAE.Equation array."
input list<BackendDAE.Equation> lst;
output BackendDAE.EquationArray outEquationArray;
protected
BackendDAE.Value len,size;
Real rlen,rlen_1;
array<Option<BackendDAE.Equation>> optarr,eqnarr,newarr;
list<Option<BackendDAE.Equation>> eqn_optlst;
algorithm
len := listLength(lst);
rlen := intReal(len);
rlen_1 := rlen *. 1.4;
size := realInt(rlen_1);
optarr := arrayCreate(size, NONE());
eqn_optlst := Util.listMap(lst, Util.makeOption);
eqnarr := listArray(eqn_optlst);
newarr := Util.arrayCopy(eqnarr, optarr);
outEquationArray := BackendDAE.EQUATION_ARRAY(len,size,newarr);
end listEquation;

public function varList
"function: varList
Expand Down Expand Up @@ -2750,7 +2750,12 @@ algorithm
end matchcontinue;
end getEquationBlock;


public function getNumberOfEquationArray
input BackendDAE.EquationArray inEqArr "equation array";
output Integer noOfElements "number of elements";
algorithm
BackendDAE.EQUATION_ARRAY(numberOfElement = noOfElements) := inEqArr;
end getNumberOfEquationArray;

/******************************************************************
stuff to calculate incidence matrix
Expand All @@ -2763,70 +2768,112 @@ end getEquationBlock;

public function incidenceMatrix
"function: incidenceMatrix
author: PA
Calculates the incidence matrix, i.e. which variables are present
in each equation."
author: PA, adrpo
Calculates the incidence matrix, i.e. which variables are present in each equation.
You can ask for absolute indexes or normal (negative for der) via the IndexType"
input BackendDAE.BackendDAE inBackendDAE;
input BackendDAE.IndexType inIndexType;
output BackendDAE.IncidenceMatrix outIncidenceMatrix;
algorithm
outIncidenceMatrix := matchcontinue (inBackendDAE)
outIncidenceMatrix := matchcontinue (inBackendDAE, inIndexType)
local
list<BackendDAE.Equation> eqnsl;
list<list<BackendDAE.Value>> lstlst;
array<list<BackendDAE.Value>> arr;
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
list<BackendDAE.WhenClause> wc;
case (BackendDAE.DAE(orderedVars = vars,orderedEqs = eqns, eventInfo = BackendDAE.EVENT_INFO(whenClauseLst = wc)))
Integer numberOfEqs;

case (BackendDAE.DAE(orderedVars = vars,orderedEqs = eqns, eventInfo = BackendDAE.EVENT_INFO(whenClauseLst = wc)), inIndexType)
equation
eqnsl = equationList(eqns);
lstlst = incidenceMatrix2(vars, eqnsl, wc);
arr = listArray(lstlst);
// get the size
numberOfEqs = getNumberOfEquationArray(eqns);
// create the array to hold the incidence matrix
arr = arrayCreate(numberOfEqs, {});
arr = incidenceMatrixDispatch(vars, eqns, wc, arr, 0, numberOfEqs, inIndexType);
then
arr;
case (_)

case (_, inIndexType)
equation
print("- BackendDAEUtil.incidenceMatrix failed\n");
then
fail();
end matchcontinue;
end incidenceMatrix;

protected function incidenceMatrix2
"function: incidenceMatrix2
author: PA
Helper function to incidenceMatrix
Calculates the incidence matrix as a list of list of integers"
public function applyIndexType
"@author: adrpo
Applies absolute value to all entries in the given list."
input list<Integer> inLst;
input BackendDAE.IndexType inIndexType;
output list<Integer> outLst;
algorithm
outLst := matchcontinue(inLst, inIndexType)

// leave as it is
case (inLst, BackendDAE.NORMAL()) then inLst;

// transform to absolute indexes
case (inLst, BackendDAE.ABSOLUTE()) then Util.absIntegerList(inLst);

end matchcontinue;
end applyIndexType;

protected function incidenceMatrixDispatch
"@author: adrpo
Calculates the incidence matrix as an array of list of integers"
input BackendDAE.Variables inVariables;
input list<BackendDAE.Equation> inEquationLst;
input BackendDAE.EquationArray inEqsArr;
input list<BackendDAE.WhenClause> inWhenClause;
output list<list<Integer>> outIntegerLstLst;
input array<list<Integer>> inIncidenceArray;
input Integer index;
input Integer numberOfEqs;
input BackendDAE.IndexType inIndexType;
output array<list<Integer>> outIncidenceArray;
algorithm
outIntegerLstLst := matchcontinue (inVariables,inEquationLst,inWhenClause)
outIncidenceArray := matchcontinue (inVariables, inEqsArr, inWhenClause, inIncidenceArray, index, numberOfEqs, inIndexType)
local
list<list<BackendDAE.Value>> lst;
list<BackendDAE.Value> row;
BackendDAE.Variables vars;
BackendDAE.Equation e;
list<BackendDAE.Equation> eqns;
BackendDAE.EquationArray eqArr;
list<BackendDAE.WhenClause> wc;
array<list<Integer>> iArr;
Integer i,n;

case (_,{},_) then {};
// i = n (we reach the end)
case (vars, eqArr, wc, iArr, i, n, inIndexType)
equation
false = intLt(i, n);
then
iArr;

case (vars,(e :: eqns),wc)
// i < n
case (vars, eqArr, wc, iArr, i, n, inIndexType)
equation
lst = incidenceMatrix2(vars, eqns, wc);
true = intLt(i, n);
// get the equation
e = equationNth(eqArr, i);
// compute the row
row = incidenceRow(vars, e, wc);
// only absolute indexes?
row = applyIndexType(row, inIndexType);
// put it in the array
iArr = arrayUpdate(iArr, i+1, row);
iArr = incidenceMatrixDispatch(vars, eqArr, wc, iArr, i + 1, n, inIndexType);
then
(row :: lst);
iArr;

case (_,_,_)
case (vars, eqArr, wc, iArr, i, n, inIndexType)
equation
print("- BackendDAEUtil.incidenceMatrix2 failed\n");
print("- BackendDAEUtil.incidenceMatrixDispatch failed\n");
then
fail();
end matchcontinue;
end incidenceMatrix2;
end incidenceMatrixDispatch;

public function incidenceRow
"function: incidenceRow
Expand Down
6 changes: 3 additions & 3 deletions Compiler/CevalScript.mo
Expand Up @@ -356,7 +356,7 @@ algorithm
ic_1 = Interactive.addInstantiatedClass(ic, Interactive.INSTCLASS(path,dae,env));
/*((daelow as BackendDAE.DAE(orderedVars=vars,orderedEqs=eqnarr,complexEqns = BackendDAE.COMPLEX_EQUATIONS(arrayEqs=ae,ifEqns=ifeqns)))) = BackendDAECreate.lower(dae, false, true) "no dummy state" ;*/
((daelow as BackendDAE.DAE(vars,_,_,_,eqnarr,_,_,ae,_,_,_))) = BackendDAECreate.lower(dae, Env.getFunctionTree(cache), false, true) "no dummy state" ;
m = BackendDAEUtil.incidenceMatrix(daelow);
m = BackendDAEUtil.incidenceMatrix(daelow, BackendDAE.NORMAL());
mt = BackendDAEUtil.transposeMatrix(m);
// jac = BackendDAEUtil.calculateJacobian(vars, eqnarr, ae,ifeqns, m, mt,false);
jac = BackendDAEUtil.calculateJacobian(vars, eqnarr, ae, m, mt,false);
Expand Down Expand Up @@ -3748,7 +3748,7 @@ algorithm
dae = DAEUtil.transformationsBeforeBackend(dae_1);
ic_1 = Interactive.addInstantiatedClass(ic, Interactive.INSTCLASS(classname,dae,env));
dlow = BackendDAECreate.lower(dae, Env.getFunctionTree(cache), true, true);
m = BackendDAEUtil.incidenceMatrix(dlow);
m = BackendDAEUtil.incidenceMatrix(dlow, BackendDAE.NORMAL());
mT = BackendDAEUtil.transposeMatrix(m);
(_,_,dlow_1,m,mT) = BackendDAETransform.matchingAlgorithm(dlow, m, mT, (BackendDAE.INDEX_REDUCTION(),BackendDAE.EXACT(), BackendDAE.REMOVE_SIMPLE_EQN()), Env.getFunctionTree(cache));
xml_filename = stringAppendList({filenameprefix,".xml"});
Expand Down Expand Up @@ -3779,7 +3779,7 @@ algorithm
dae = DAEUtil.transformationsBeforeBackend(dae_1);
ic_1 = Interactive.addInstantiatedClass(ic, Interactive.INSTCLASS(classname,dae,env));
dlow = BackendDAECreate.lower(dae, Env.getFunctionTree(cache), true, true);
m = BackendDAEUtil.incidenceMatrix(dlow);
m = BackendDAEUtil.incidenceMatrix(dlow, BackendDAE.NORMAL());
mT = BackendDAEUtil.transposeMatrix(m);
(ass1,ass2,dlow_1,m,mT) = BackendDAETransform.matchingAlgorithm(dlow, m, mT, (BackendDAE.INDEX_REDUCTION(),BackendDAE.EXACT(), BackendDAE.REMOVE_SIMPLE_EQN()),Env.getFunctionTree(cache));
(comps) = BackendDAETransform.strongComponents(m, mT, ass1, ass2);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Main.mo
Expand Up @@ -781,7 +781,7 @@ algorithm
funcs = Env.getFunctionTree(cache);
dlow = BackendDAECreate.lower(dae, funcs, /* add dummy state if needed */ true, /* simplify */ true);
Debug.fcall("dumpdaelow", BackendDump.dump, dlow);
m = BackendDAEUtil.incidenceMatrix(dlow);
m = BackendDAEUtil.incidenceMatrix(dlow, BackendDAE.NORMAL());
mT = BackendDAEUtil.transposeMatrix(m);
Debug.fcall("bltdump", BackendDump.dumpIncidenceMatrix, m);
Debug.fcall("bltdump", BackendDump.dumpIncidenceMatrixT, mT);
Expand Down

0 comments on commit 28a46b0

Please sign in to comment.