Skip to content

Commit

Permalink
fixing ticket:3786
Browse files Browse the repository at this point in the history
  • Loading branch information
Willi Braun committed Apr 8, 2016
1 parent 1299763 commit f11f59c
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions Compiler/BackEnd/SymbolicJacobian.mo
Expand Up @@ -1002,14 +1002,14 @@ protected function generateSparsePattern "author: wbraun
The coloring is saved as a list of lists, every list contains the
cols with the same color."
input BackendDAE.BackendDAE inBackendDAE;
input list<BackendDAE.Var> inDiffVars "vars";
input list<BackendDAE.Var> inDiffedVars "eqns";
input list<BackendDAE.Var> inIndependentVars "vars";
input list<BackendDAE.Var> inDependentVars "eqns";
output BackendDAE.SparsePattern outSparsePattern;
output BackendDAE.SparseColoring outColoredCols;
protected
constant Boolean debug = false;
constant Boolean debug = true;
algorithm
(outSparsePattern,outColoredCols) := matchcontinue(inBackendDAE,inDiffVars,inDiffedVars)
(outSparsePattern,outColoredCols) := matchcontinue(inBackendDAE,inIndependentVars,inDependentVars)
local
BackendDAE.Shared shared;
BackendDAE.EqSystem syst, syst1;
Expand All @@ -1024,15 +1024,15 @@ algorithm
Integer nonZeroElements, maxColor;
list<Integer> nodesList, nodesEqnsIndex;
list<list<Integer>> sparsepattern,sparsepatternT, coloredlist;
list<BackendDAE.Var> jacDiffVars, indiffVars, indiffedVars;
list<BackendDAE.Var> jacDiffVars, dependentVars, independentVars;
BackendDAE.Variables varswithDiffs;
BackendDAE.EquationArray orderedEqns;
array<Option<list<Integer>>> forbiddenColor;
array<Integer> colored, colored1, ass1, ass2;
array<list<Integer>> coloredArray;

list<DAE.ComponentRef> diffCompRefsLst, diffedCompRefsLst;
array<DAE.ComponentRef> diffCompRefs, diffedCompRefs;
list<DAE.ComponentRef> depCompRefsLst, inDepCompRefsLst;
array<DAE.ComponentRef> depCompRefs, inDepCompRefs;

array<list<Integer>> eqnSparse, varSparse, sparseArray, sparseArrayT;
array<Integer> mark, usedvar;
Expand All @@ -1041,22 +1041,23 @@ algorithm
list<list<DAE.ComponentRef>> translated;
list<tuple<DAE.ComponentRef,list<DAE.ComponentRef>>> sparsetuple, sparsetupleT;

case (_,{},_) then (({},{},({},{}), -1),{});
case (_,_,{}) then (({},{},({},{}), -1),{});
case(BackendDAE.DAE(eqs = (syst as BackendDAE.EQSYSTEM(matching=bdaeMatching as BackendDAE.MATCHING(comps=comps, ass1=ass1)))::{}),indiffVars,indiffedVars)
// if there are no independent var, no pattern needed, otherwise there
// is an empty pattern for the dependent variables
case (_,_,{}) then (({},{},({},{}),-1),{});
case(BackendDAE.DAE(eqs = (syst as BackendDAE.EQSYSTEM(matching=bdaeMatching as BackendDAE.MATCHING(comps=comps, ass1=ass1)))::{}),independentVars,dependentVars)
equation
if Flags.isSet(Flags.DUMP_SPARSE_VERBOSE) then
print(" start getting sparsity pattern diff Vars : " + intString(listLength(indiffedVars)) + " diffed vars: " + intString(listLength(indiffVars)) +"\n");
print(" start getting sparsity pattern for variables : " + intString(listLength(dependentVars)) + " and the independent vars: " + intString(listLength(independentVars)) +"\n");
end if;
if debug then execStat("generateSparsePattern -> do start "); end if;
// prepare crefs
diffCompRefsLst = List.map(indiffVars, BackendVariable.varCref);
diffedCompRefsLst = List.map(indiffedVars, BackendVariable.varCref);
diffCompRefs = listArray(diffCompRefsLst);
diffedCompRefs = listArray(diffedCompRefsLst);
depCompRefsLst = List.map(dependentVars, BackendVariable.varCref);
inDepCompRefsLst = List.map(independentVars, BackendVariable.varCref);
depCompRefs = listArray(depCompRefsLst);
inDepCompRefs = listArray(inDepCompRefsLst);
// create jacobian vars
jacDiffVars = list(BackendVariable.createpDerVar(v) for v in indiffVars);
sizeN = arrayLength(diffCompRefs);
jacDiffVars = list(BackendVariable.createpDerVar(v) for v in independentVars);
sizeN = arrayLength(inDepCompRefs);

// generate adjacency matrix including diff vars
(syst1 as BackendDAE.EQSYSTEM(orderedVars=varswithDiffs,orderedEqs=orderedEqns)) = BackendDAEUtil.addVarsToEqSystem(syst,jacDiffVars);
Expand All @@ -1074,7 +1075,7 @@ algorithm
end if;

// get indexes of diffed vars (rows)
nodesEqnsIndex = BackendVariable.getVarIndexFromVars(indiffedVars,varswithDiffs);
nodesEqnsIndex = BackendVariable.getVarIndexFromVars(dependentVars,varswithDiffs);
nodesEqnsIndex = List.map1(nodesEqnsIndex, Array.getIndexFirst, ass1);

// debug dump
Expand All @@ -1090,7 +1091,12 @@ algorithm
varSparse = arrayCreate(adjSizeT, {});
mark = arrayCreate(adjSizeT, 0);
usedvar = arrayCreate(adjSizeT, 0);
usedvar = Array.setRange(adjSizeT-(sizeN-1), adjSizeT, usedvar, 1);

// make dependent variables as used if there are some
// otherwise Array.setRange fails start is greater than end
if (sizeN>0) then
usedvar = Array.setRange(adjSizeT-(sizeN-1), adjSizeT, usedvar, 1);
end if;

if debug then execStat("generateSparsePattern -> start "); end if;
eqnSparse = getSparsePattern(comps, eqnSparse, varSparse, mark, usedvar, 1, adjMatrix, adjMatrixT);
Expand Down Expand Up @@ -1124,10 +1130,10 @@ algorithm
end if;

// translated to DAE.ComRefs
translated = list(list(arrayGet(diffCompRefs, i) for i in lst) for lst in sparsepattern);
sparsetuple = list((cr,t) threaded for cr in diffedCompRefs, t in translated);
translated = list(list(arrayGet(diffedCompRefs, i) for i in lst) for lst in sparsepatternT);
sparsetupleT = list((cr,t) threaded for cr in diffCompRefs, t in translated);
translated = list(list(arrayGet(inDepCompRefs, i) for i in lst) for lst in sparsepattern);
sparsetuple = list((cr,t) threaded for cr in depCompRefs, t in translated);
translated = list(list(arrayGet(depCompRefs, i) for i in lst) for lst in sparsepatternT);
sparsetupleT = list((cr,t) threaded for cr in inDepCompRefs, t in translated);

// build up a bi-partied graph of pattern
if Flags.isSet(Flags.DUMP_SPARSE_VERBOSE) then
Expand All @@ -1152,29 +1158,31 @@ algorithm
colored = arrayCreate(sizeN,0);
arraysparseGraph = listArray(sparseGraph);
if debug then execStat("generateSparsePattern -> coloring start "); end if;
Graph.partialDistance2colorInt(sparseGraphT, forbiddenColor, nodesList, arraysparseGraph, colored);
if (sizeN>0) then
Graph.partialDistance2colorInt(sparseGraphT, forbiddenColor, nodesList, arraysparseGraph, colored);
end if;
if debug then execStat("generateSparsePattern -> coloring end "); end if;
// get max color used
maxColor = Array.fold(colored, intMax, 0);

// map index of that array into colors
coloredArray = arrayCreate(maxColor, {});
mapIndexColors(colored, arrayLength(diffCompRefs), coloredArray);
mapIndexColors(colored, arrayLength(inDepCompRefs), coloredArray);

if Flags.isSet(Flags.DUMP_SPARSE) then
print("Print Coloring Cols: \n");
BackendDump.dumpSparsePattern(arrayList(coloredArray));
end if;

coloring = list(list(arrayGet(diffCompRefs, i) for i in lst) for lst in coloredArray);
coloring = list(list(arrayGet(inDepCompRefs, i) for i in lst) for lst in coloredArray);

//without coloring
//coloring = List.transposeList({diffCompRefs});
//coloring = List.transposeList({inDepCompRefs});
if Flags.isSet(Flags.DUMP_SPARSE_VERBOSE) then
print("analytical Jacobians[SPARSE] -> ready! " + realString(clock()) + "\n");
end if;
if debug then execStat("generateSparsePattern -> final end "); end if;
then ((sparsetupleT, sparsetuple, (diffCompRefsLst, diffedCompRefsLst), nonZeroElements), coloring);
then ((sparsetupleT, sparsetuple, (inDepCompRefsLst, depCompRefsLst), nonZeroElements), coloring);
else
algorithm
Error.addInternalError("function generateSparsePattern failed", sourceInfo());
Expand Down

0 comments on commit f11f59c

Please sign in to comment.