Skip to content

Commit

Permalink
- fix some bugs in sparse jacobian calculation to support NxM
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14501 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Dec 21, 2012
1 parent e171af4 commit a75a680
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 39 deletions.
2 changes: 2 additions & 0 deletions Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -504,6 +504,8 @@ uniontype StateSet
list< Var> ovars;
list< Equation> eqns;
list< Equation> oeqns;
.DAE.ComponentRef crJ;
list< Var> varJ;
end STATESET;
end StateSet;

Expand Down
34 changes: 22 additions & 12 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -4787,19 +4787,18 @@ algorithm
// build up a graph of pattern
nodesList = List.intRange2(1,adjSize);
sparseGraph = Graph.buildGraph(nodesList,createBipartiteGraph,sparseArray);
sparseGraphT = Graph.buildGraph(nodesList,createBipartiteGraph,sparseArrayT);
sparseGraphT = Graph.buildGraph(List.intRange2(1,sizeN),createBipartiteGraph,sparseArrayT);
Debug.fcall(Flags.JAC_DUMP2,print,"sparse graph: \n");
Debug.fcall(Flags.JAC_DUMP2,Graph.printGraphInt,sparseGraph);
Debug.fcall(Flags.JAC_DUMP2,print,"transposed sparse graph: \n");
Debug.fcall(Flags.JAC_DUMP2,Graph.printGraphInt,sparseGraphT);

Debug.fcall(Flags.JAC_DUMP,print,"analytical Jacobians[SPARSE] -> builded graph for coloring.\n");
// color sparse bipartite graph
forbiddenColor = arrayCreate(adjSize,NONE());
colored = arrayCreate(adjSize,0);
arraysparseGraph = listArray(sparseGraph);
forbiddenColor = arrayCreate(sizeN,NONE());
colored = arrayCreate(sizeN,0);
arraysparseGraph = listArray(sparseGraph);
colored1 = Graph.partialDistance2colorInt(sparseGraphT, forbiddenColor, nodesList, arraysparseGraph, colored);

// get max color used
maxColor = Util.arrayFold(colored1, intMax, 0);
Debug.fcall(Flags.JAC_DUMP, print, "analytical Jacobians[SPARSE] -> colored graph with " +& intString(maxColor) +& " colors. Time : " +& realString(clock()) +& "\n");
Expand Down Expand Up @@ -4831,21 +4830,32 @@ protected function mapIndexColors
output array<list<Integer>> outColors;
algorithm
outColors := matchcontinue(inColors, inMaxIndex, inArray)
local
Integer i, index;
list<Integer> lst;
local
Integer i, index;
list<Integer> lst;
case (_, 0, _) then inArray;
case (_, i, _)
equation
index = arrayGet(inColors, i);
lst = arrayGet(inArray, index);
lst = listAppend({i},lst);
_ = arrayUpdate(inArray, index, lst);
then mapIndexColors(inColors, i-1, inArray);
else
then
mapIndexColors(inColors, i-1, inArray);
/* case (_, i, _)
equation
Error.addMessage(Error.INTERNAL_ERROR, {"BackendDAEOptimize.generateSparsePattern: mapIndexColors failed"});
then fail();
print("i " +& intString(i) +& "\n");
print("inColors " +& intString(arrayLength(inColors)) +& "\n");
index = arrayGet(inColors, i);
print("index " +& intString(index) +& "\n");
print("inArray " +& intString(arrayLength(inArray)) +& "\n");
then
fail();
*/ else
equation
Error.addMessage(Error.INTERNAL_ERROR, {"BackendDAEOptimize.generateSparsePattern: mapIndexColors failed"});
then
fail();
end matchcontinue;
end mapIndexColors;

Expand Down
14 changes: 11 additions & 3 deletions Compiler/BackEnd/IndexReduction.mo
Expand Up @@ -3839,7 +3839,7 @@ algorithm
Debug.fcall(Flags.BLT_DUMP, BackendDump.debuglst,((dstates,BackendDAETransform.dumpStates,"\n","\n")));

// generate Set Vars
(crset,setVars,crA,aVars,tp) = getSetVars(iSetIndex,rang,size);
(crset,setVars,crA,aVars,tp,crJ,varJ) = getSetVars(iSetIndex,rang,size);
// add set states
v = BackendVariable.addVars(setVars,v);

Expand Down Expand Up @@ -3880,7 +3880,7 @@ algorithm
varlst = List.map1r(List.map(states,Util.tuple22),BackendVariable.getVarAt,vars);
ovarlst = List.map1r(List.map(dstates,Util.tuple22),BackendVariable.getVarAt,vars);

syst = BackendDAE.EQSYSTEM(v,eqnsarr,om,omT,matching,BackendDAE.STATESET(rang,crA,aVars,varlst,ovarlst,eqnlst,oeqnlst)::stateSets);
syst = BackendDAE.EQSYSTEM(v,eqnsarr,om,omT,matching,BackendDAE.STATESET(rang,crA,aVars,varlst,ovarlst,eqnlst,oeqnlst,crJ,varJ)::stateSets);

// add dummy states
dstates1 = listAppend(states,dstates);
Expand Down Expand Up @@ -6086,6 +6086,8 @@ protected function getSetVars
output DAE.ComponentRef ocrA;
output list<BackendDAE.Var> oAVars;
output DAE.Type realtp;
output DAE.ComponentRef ocrJ;
output list<BackendDAE.Var> oJVars;
protected
DAE.ComponentRef set,crstates;
DAE.Type tp;
Expand All @@ -6105,7 +6107,13 @@ algorithm
DAE.T_ARRAY(DAE.T_REAL_DEFAULT,{DAE.DIM_INTEGER(nStates)}, DAE.emptyTypeSource));
ocrA := ComponentReference.joinCrefs(set,ComponentReference.makeCrefIdent("A",tp,{}));
oAVars := generateArrayVar(ocrA,BackendDAE.VARIABLE(),tp,NONE());
oAVars := List.map1(oAVars,BackendVariable.setVarFixed,false);
oAVars := List.map1(oAVars,BackendVariable.setVarFixed,true);
// add start value A[i,j] = if i==j then 1 else 0
oAVars := List.map1(oAVars,BackendVariable.setVarStartValue,DAE.RCONST(0.0));
tp := Util.if_(intGt(setsize,1),DAE.T_ARRAY(DAE.T_REAL_DEFAULT,{DAE.DIM_INTEGER(setsize)}, DAE.emptyTypeSource),DAE.T_REAL_DEFAULT);
ocrJ := ComponentReference.joinCrefs(set,ComponentReference.makeCrefIdent("J",tp,{}));
oJVars := generateArrayVar(ocrJ,BackendDAE.VARIABLE(),tp,NONE());
oJVars := List.map1(oJVars,BackendVariable.setVarFixed,false);
end getSetVars;

/*
Expand Down
18 changes: 18 additions & 0 deletions Compiler/BackEnd/Initialization.mo
Expand Up @@ -784,10 +784,28 @@ algorithm

((vars, fixvars)) := BackendVariable.traverseBackendDAEVars(orderedVars, collectInitialVars, (vars, fixvars));
((eqns, reqns)) := BackendEquation.traverseBackendDAEEqns(orderedEqs, collectInitialEqns, (eqns, reqns));
fixvars := List.fold(stateSets,collectInitialStateSetVars,fixvars);

oTpl := (vars, fixvars, eqns, reqns);
end collectInitialVarsEqnsSystem;

protected function collectInitialStateSetVars
"function collectInitialStateSetVars
author Frenkel TUD
add the vars for state set to the initial system
Because the statevars are calculated by
set.x = set.A*dummystates we add set.A to the
initial system with set.A = {{1,0,0},{0,1,0}}"
input BackendDAE.StateSet inSet;
input BackendDAE.Variables iFixedVars;
output BackendDAE.Variables oFixedVars;
protected
list<BackendDAE.Var> varJ;
algorithm
BackendDAE.STATESET(varJ=varJ) := inSet;
oFixedVars := BackendVariable.addVars(varJ,iFixedVars);
end collectInitialStateSetVars;

protected function collectInitialVars "function collectInitialVars
author: lochel
This function collects all the vars for the initial system."
Expand Down

0 comments on commit a75a680

Please sign in to comment.