Skip to content

Commit

Permalink
- adapt BackendDAEUtil.reduceEqSystem for symbolic jacobians and uti…
Browse files Browse the repository at this point in the history
…lize it.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25366 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Apr 2, 2015
1 parent 9992292 commit 2a962db
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 123 deletions.
82 changes: 56 additions & 26 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -2120,11 +2120,52 @@ algorithm
BackendDAE.SHARED(info=einfo) := shared;
end getExtraInfo;

public function reduceEqSystem

public function reduceEqSystemsInDAE
"Function reduces BackendDAE system by filtering
the equation and select only the one that are needed
to calculate the given varibales.
"
reduce EqSystem for iVarlst
input BackendDAE.BackendDAE inDAE;
input list<BackendDAE.Var> iVarlst;
input Boolean makeMatching = true;
output BackendDAE.BackendDAE outDAE;

Author: wbraun
protected
BackendDAE.Shared shared;
list<BackendDAE.EqSystem> systs;
BackendDAE.EqSystem tmpsyst;
algorithm
BackendDAE.DAE(systs, shared) := inDAE;
outDAE := BackendDAE.DAE(list(tryReduceEqSystem(syst, shared, iVarlst) for syst in systs), shared);
if makeMatching then
outDAE := BackendDAEUtil.transformBackendDAE(outDAE,SOME((BackendDAE.NO_INDEX_REDUCTION(),BackendDAE.EXACT())),NONE(),NONE());
end if;
end reduceEqSystemsInDAE;

public function tryReduceEqSystem
" Helpfunction to reduceEqSystemsInDAE.
"
input BackendDAE.EqSystem iSyst;
input BackendDAE.Shared shared;
input list<BackendDAE.Var> iVarlst;
output BackendDAE.EqSystem oSyst;
algorithm
try
//BackendDump.dumpEqSystem(iSyst,"IN: tryReduceEqSystem");
oSyst := reduceEqSystem(iSyst, shared, iVarlst);
//BackendDump.dumpEqSystem(oSyst,"OUT: tryReduceEqSystem");
else
oSyst := iSyst;
end try;
end tryReduceEqSystem;


public function reduceEqSystem
"Function reduces BackendDAE.EqSystem system by filtering
the equation and select only the one that are needed
to calculate the given varibales. Shared object is used
only to get the functionsTree.
"
input BackendDAE.EqSystem iSyst;
input BackendDAE.Shared shared;
Expand All @@ -2139,16 +2180,12 @@ protected
BackendDAE.Variables iVars = BackendVariable.listVar(iVarlst);
BackendDAE.EquationArray ordererdEqs, arrEqs;
list<Integer> indx_lst_v, indx_lst_e, ind_mark, statevarindx_lst;
Integer ind;
array<Integer> indx_arr;
array<Integer> mapIncRowEqn;
list<BackendDAE.Equation> el;
list<BackendDAE.Var> vl;

DAE.FunctionTree funcs;
BackendDAE.IncidenceMatrix m;
BackendDAE.BackendDAE backendDAE2;
BackendDAE.EqSystem syst;
algorithm

BackendDAE.EQSYSTEM(orderedEqs = ordererdEqs, orderedVars = v, matching = BackendDAE.MATCHING(ass1=ass1, ass2=ass2), partitionKind = partitionKind) := iSyst;
Expand All @@ -2159,38 +2196,31 @@ algorithm
indx_lst_v := List.appendNoCopy(indx_lst_v, statevarindx_lst) "overestimate";
indx_lst_e := List.map1r(indx_lst_v,arrayGet,ass1);


indx_arr := arrayCreate(equationArraySizeDAE(iSyst), 0);
funcs := getFunctions(shared);
(_, m, _, _, mapIncRowEqn) := getIncidenceMatrixScalar(iSyst, BackendDAE.SPARSE(), SOME(funcs));


(_, m, _) := getIncidenceMatrix(iSyst, BackendDAE.SPARSE(), SOME(funcs));

indx_arr := markStateEquationsWork(indx_lst_e, {}, m, ass1, indx_arr);
ind_mark := arrayList(indx_arr);


indx_lst_e := {};
ind := 1;

for mark in ind_mark loop
if mark == 1 then
indx_lst_e := ind :: indx_lst_e;
end if;
ind := ind + 1;
end for;

indx_lst_e := Array.foldIndex(indx_arr, translateArrayList, {});

indx_lst_e := List.map1r(indx_lst_e,arrayGet,mapIncRowEqn);
indx_lst_e := List.unique(indx_lst_e);
el := BackendEquation.getEqns(indx_lst_e, ordererdEqs);
arrEqs := BackendEquation.listEquation(el);

vl := BackendEquation.equationsVars(arrEqs, v);
vars := BackendVariable.listVar1(vl);

oSyst := BackendDAE.EQSYSTEM(vars, arrEqs, NONE(), NONE(), BackendDAE.NO_MATCHING(), {}, partitionKind);

end reduceEqSystem;

protected function translateArrayList
input Integer inElement;
input Integer inIndex;
input list<Integer> inFoldArg;
output list<Integer> outFoldArg;
algorithm
outFoldArg := if intEq(inElement, 1) then inIndex::inFoldArg else inFoldArg;
end translateArrayList;

public function removeDiscreteAssignments "
Author: wbraun
Expand Down
88 changes: 36 additions & 52 deletions Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -194,7 +194,8 @@ algorithm
outEquationArray := BackendDAE.EQUATION_ARRAY(size, numberOfElement, arrSize, newEquOptArr);
end copyEquationArray;

public function equationsLstVarsWithoutRelations "author: Frenkel TUD 2012-03
public function equationsLstVars
"author: Frenkel TUD 2011-05
From the equations and a variable array return all
occurring variables form the array."
input list<BackendDAE.Equation> inEquationLst;
Expand All @@ -205,62 +206,29 @@ protected
list<Integer> keys;
algorithm
bt := BinaryTreeInt.emptyBinTree;
(_, (_, bt)) := traverseExpsOfEquationList(inEquationLst, checkEquationsVarsWithoutRelations, (inVars, bt));
(_, (_, bt)) := traverseExpsOfEquationList(inEquationLst, checkEquationsVarsExpTopDownTraverseHelper, (inVars, bt));
(keys, _) := BinaryTreeInt.bintreeToList(bt);
outVars := List.map1r(keys, BackendVariable.getVarAt, inVars);
end equationsLstVarsWithoutRelations;

public function equationsVarsWithoutRelations
"author: Frenkel TUD 2012-03
From the equations and a variable array return all
occurring variables form the array without relations."
input BackendDAE.EquationArray inEquations;
input BackendDAE.Variables inVars;
output list<BackendDAE.Var> outVars;
protected
BinaryTreeInt.BinTree bt;
list<Integer> keys;
algorithm
bt := BinaryTreeInt.emptyBinTree;
((_, bt)) := BackendDAEUtil.traverseBackendDAEExpsEqns(inEquations, checkEquationsVarsWithoutRelations, (inVars, bt));
(keys, _) := BinaryTreeInt.bintreeToList(bt);
outVars := List.map1r(keys, BackendVariable.getVarAt, inVars);
end equationsVarsWithoutRelations;

protected function checkEquationsVarsWithoutRelations
input DAE.Exp inExp;
input tuple<BackendDAE.Variables, BinaryTreeInt.BinTree> inTpl;
output DAE.Exp outExp;
output tuple<BackendDAE.Variables, BinaryTreeInt.BinTree> outTpl;
protected
BackendDAE.Variables vars;
BinaryTreeInt.BinTree bt;
algorithm
(vars,_) := inTpl;
outExp := inExp;
(_, (_, (_, bt))) := Expression.traverseExpTopDown(inExp, Expression.traverseSubexpressionsWithoutRelations, (checkEquationsVarsExp, inTpl));
outTpl := (vars,bt);
end checkEquationsVarsWithoutRelations;
end equationsLstVars;

public function equationsLstVars
public function equationsVars
"author: Frenkel TUD 2011-05
From the equations and a variable array return all
occurring variables form the array."
input list<BackendDAE.Equation> inEquationLst;
input BackendDAE.EquationArray inEquations;
input BackendDAE.Variables inVars;
output list<BackendDAE.Var> outVars;
protected
BinaryTreeInt.BinTree bt;
list<Integer> keys;
algorithm
bt := BinaryTreeInt.emptyBinTree;
(_, (_, (_, bt))) := traverseExpsOfEquationList(inEquationLst, Expression.traverseSubexpressionsHelper, (checkEquationsVarsExp, (inVars, bt)));
(_, bt) := BackendDAEUtil.traverseBackendDAEExpsEqns(inEquations, checkEquationsVarsExpTopDownTraverseHelper, (inVars, bt));
(keys, _) := BinaryTreeInt.bintreeToList(bt);
outVars := List.map1r(keys, BackendVariable.getVarAt, inVars);
end equationsLstVars;

end equationsVars;

public function equationsVars
public function equationsVars2
"author: Frenkel TUD 2011-05
From the equations and a variable array return all
occurring variables form the array."
Expand All @@ -272,10 +240,10 @@ protected
list<Integer> keys;
algorithm
bt := BinaryTreeInt.emptyBinTree;
((_, (_,bt))) := BackendDAEUtil.traverseBackendDAEExpsEqns(inEquations, Expression.traverseSubexpressionsHelper, (checkEquationsVarsExp, (inVars, bt)));
(_, bt) := BackendDAEUtil.traverseBackendDAEExpsEqns(inEquations, checkEquationsVarsExpTopDownTraverseHelper, (inVars, bt) );
(keys, _) := BinaryTreeInt.bintreeToList(bt);
outVars := List.map1r(keys, BackendVariable.getVarAt, inVars);
end equationsVars;
end equationsVars2;

public function equationVars
"author: Frenkel TUD 2012-03
Expand All @@ -289,7 +257,7 @@ protected
list<Integer> keys;
algorithm
bt := BinaryTreeInt.emptyBinTree;
(_, (_, (_, bt))) := traverseExpsOfEquation(inEquation, Expression.traverseSubexpressionsHelper, (checkEquationsVarsExp, (inVars, bt)));
(_, (_, bt)) := traverseExpsOfEquation(inEquation, checkEquationsVarsExpTopDownTraverseHelper, (inVars, bt));
(keys, _) := BinaryTreeInt.bintreeToList(bt);
outVars := List.map1r(keys, BackendVariable.getVarAt, inVars);
end equationVars;
Expand All @@ -306,18 +274,30 @@ protected
list<Integer> keys;
algorithm
bt := BinaryTreeInt.emptyBinTree;
(_, (_, bt)) := Expression.traverseExpBottomUp(inExp, checkEquationsVarsExp, (inVars, bt));
(_, (_, bt)) := Expression.traverseExpTopDown(inExp, checkEquationsVarsExpTopDown, (inVars, bt));
(keys, _) := BinaryTreeInt.bintreeToList(bt);
outVars := List.map1r(keys, BackendVariable.getVarAt, inVars);
end expressionVars;

protected function checkEquationsVarsExp
public function checkEquationsVarsExpTopDownTraverseHelper
"This function traverses expssions to-down to collect all
variables that are involed into the expression."
input DAE.Exp inExp;
input tuple<BackendDAE.Variables, BinaryTreeInt.BinTree> itpl;
output DAE.Exp outExp;
output tuple<BackendDAE.Variables, BinaryTreeInt.BinTree> otpl;
algorithm
(outExp, otpl) := Expression.traverseExpTopDown(inExp,checkEquationsVarsExpTopDown,itpl);
end checkEquationsVarsExpTopDownTraverseHelper;

protected function checkEquationsVarsExpTopDown
input DAE.Exp inExp;
input tuple<BackendDAE.Variables, BinaryTreeInt.BinTree> inTuple;
output DAE.Exp outExp;
output Boolean cont;
output tuple<BackendDAE.Variables, BinaryTreeInt.BinTree> outTuple;
algorithm
(outExp,outTuple) := matchcontinue (inExp,inTuple)
(outExp,cont,outTuple) := matchcontinue (inExp,inTuple)
local
DAE.Exp e;
BackendDAE.Variables vars;
Expand All @@ -327,21 +307,25 @@ algorithm

// special case for time, it is never part of the equation system
case (e as DAE.CREF(componentRef=DAE.CREF_IDENT(ident="time")), (vars, bt))
then (e, (vars, bt));
then (e, true, (vars, bt));

// case for function pointers
case (e as DAE.CREF(ty=DAE.T_FUNCTION_REFERENCE_FUNC()), (vars, bt))
then (e, (vars, bt));
then (e, true, (vars, bt));

// case for pre vars
case (e as DAE.CALL(path = Absyn.IDENT(name = "pre")), _)
then (inExp, false, inTuple);

// add it
case (e as DAE.CREF(componentRef = cr), (vars, bt)) equation
(_, ilst) = BackendVariable.getVar(cr, vars);
bt = BinaryTreeInt.treeAddList(bt, ilst);
then (e, (vars, bt));
then (e, true, (vars, bt));

else (inExp,inTuple);
else (inExp, true, inTuple);
end matchcontinue;
end checkEquationsVarsExp;
end checkEquationsVarsExpTopDown;

public function equationsStates "author: Frenkel TUD
From a list of equations return all occurring state variables references."
Expand Down
8 changes: 5 additions & 3 deletions Compiler/BackEnd/SymbolicJacobian.mo
Expand Up @@ -1696,7 +1696,7 @@ algorithm
(outJacobian, outSparsePattern, outSparseColoring, outFunctionTree) :=
matchcontinue (inBackendDAE,inDiffVars,inStateVars,inInputVars,inParameterVars,inDifferentiatedVars,inVars,inName)
local
BackendDAE.BackendDAE backendDAE;
BackendDAE.BackendDAE backendDAE, reduceDAE;

list<DAE.ComponentRef> comref_vars, comref_seedVars, comref_differentiatedVars;

Expand All @@ -1717,14 +1717,16 @@ algorithm
s = intString(listLength(diffedVars));
comref_differentiatedVars = List.map(diffedVars, BackendVariable.varCref);

reduceDAE = BackendDAEUtil.reduceEqSystemsInDAE(inBackendDAE, diffedVars);

comref_vars = List.map(inDiffVars, BackendVariable.varCref);
seedlst = List.map1(comref_vars, createSeedVars, inName);
s1 = intString(listLength(inVars));

SimCodeUtil.execStat("analytical Jacobians -> starting to generate the jacobian. DiffVars:" + s + " diffed equations: " + s1);

// Differentiate the ODE system w.r.t states for jacobian
(backendDAE as BackendDAE.DAE(shared=shared), funcs) = generateSymbolicJacobian(inBackendDAE, inDiffVars, inDifferentiatedVars, BackendVariable.listVar1(seedlst), inStateVars, inInputVars, inParameterVars, inName);
(backendDAE as BackendDAE.DAE(shared=shared), funcs) = generateSymbolicJacobian(reduceDAE, inDiffVars, inDifferentiatedVars, BackendVariable.listVar1(seedlst), inStateVars, inInputVars, inParameterVars, inName);
if Flags.isSet(Flags.JAC_DUMP2) then
print("analytical Jacobians -> generated equations for Jacobian " + inName + " time: " + realString(clock()) + "\n");
end if;
Expand Down Expand Up @@ -1755,7 +1757,7 @@ algorithm
SimCodeUtil.execStat("analytical Jacobians -> generated optimized jacobians");

// generate sparse pattern
(sparsepattern,colsColors) = generateSparsePattern(inBackendDAE, inDiffVars, diffedVars);
(sparsepattern,colsColors) = generateSparsePattern(reduceDAE, inDiffVars, diffedVars);
SimCodeUtil.execStat("analytical Jacobians -> generated generateSparsePattern");
then
((backendDAE, inName, inDiffVars, diffedVars, inVars), sparsepattern, colsColors, funcs);
Expand Down
48 changes: 6 additions & 42 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -4993,61 +4993,25 @@ public function traverseSubexpressionsTopDownHelper
The extra argument is a tuple of the actul function to call on each subexpression and the extra argument."
replaceable type Type_a subtypeof Any;
input DAE.Exp inExp;
input tuple<FuncExpType,Type_a> itpl;
input tuple<FuncExpType2,Type_a> itpl;
output DAE.Exp outExp;
output tuple<FuncExpType,Type_a> otpl;
partial function FuncExpType
output tuple<FuncExpType2,Type_a> otpl;
partial function FuncExpType2
input DAE.Exp inExp;
input Type_a inTypeA;
output DAE.Exp outExp;
output Boolean cont;
output Type_a outA;
end FuncExpType;
end FuncExpType2;
protected
FuncExpType rel;
Type_a ext_arg;
FuncExpType2 rel;
Type_a ext_arg;
algorithm
(rel,ext_arg) := itpl;
(outExp,ext_arg) := traverseExpTopDown(inExp,rel,ext_arg);
otpl := (rel,ext_arg);
end traverseSubexpressionsTopDownHelper;

public function traverseSubexpressionsWithoutRelations
"This function is used as input to a traverse function that does not traverse all subexpressions.
The extra argument is a tuple of the actul function to call on each subexpression and the extra argument.
"
replaceable type Type_a subtypeof Any;
input DAE.Exp inExp;
input tuple<FuncExpType,Type_a> itpl;
output DAE.Exp e;
output Boolean cont;
output tuple<FuncExpType,Type_a> otpl;
partial function FuncExpType
input DAE.Exp inExp;
input Type_a inTypeA;
output DAE.Exp outExp;
output Type_a outA;
end FuncExpType;
algorithm
// TODO: Implement flags to traverseExpBottomUp to handle special cases like WithoutRelations or without updating the expressions?
(e,cont,otpl) := match (inExp,itpl)
local
FuncExpType f;
Type_a ea1,ea2;
case (DAE.LUNARY(),_)
then (inExp,false,itpl);
case (DAE.LBINARY(),_)
then (inExp,false,itpl);
case (DAE.RELATION(),_)
then (inExp,false,itpl);
case (_,(f,ea1))
equation
(e,ea2) = f(inExp,ea1);
otpl = if referenceEq(ea1,ea2) then itpl else (f,ea2);
then (e,true,otpl);
end match;
end traverseSubexpressionsWithoutRelations;

protected function traverseExpMatrix
"author: PA
Helper function to traverseExpBottomUp, traverses matrix expressions."
Expand Down

0 comments on commit 2a962db

Please sign in to comment.