Skip to content

Commit

Permalink
further improvments for differentiation
Browse files Browse the repository at this point in the history
  • Loading branch information
Willi Braun authored and OpenModelica-Hudson committed Jan 29, 2016
1 parent 658b927 commit 4b7f96a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
16 changes: 12 additions & 4 deletions Compiler/BackEnd/Differentiate.mo
Expand Up @@ -2093,7 +2093,7 @@ algorithm
fail();

// try to inline
case (DAE.CALL(attr=DAE.CALL_ATTR(builtin=false)), _, _, BackendDAE.DIFFERENTIATION_TIME(), _)
case (DAE.CALL(attr=DAE.CALL_ATTR(builtin=false)), _, _, _, _)
equation
(e,_,true) = Inline.forceInlineExp(inExp,(SOME(inFunctionTree),{DAE.NORM_INLINE(),DAE.NO_INLINE()}),DAE.emptyElementSource);
e = Expression.addNoEventToRelations(e);
Expand All @@ -2115,6 +2115,10 @@ algorithm
BackendDump.debugStrExpStr("### Differentiate call\n ", e, " w.r.t. " + ComponentReference.crefStr(inDiffwrtCref) + "\n");
end if;
(de, functions) = differentiateFunctionCallPartial(e, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter, expStack);
(e,_,b) = Inline.forceInlineExp(de,(SOME(functions),{DAE.NORM_INLINE(),DAE.NO_INLINE()}),DAE.emptyElementSource);
if b then
de = e;
end if;
// Debug dump
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
BackendDump.debugStrExpStr("### result output :\n", de, " w.r.t. " + ComponentReference.crefStr(inDiffwrtCref) + "\n");
Expand Down Expand Up @@ -2446,7 +2450,7 @@ algorithm
DAE.CallAttributes attr;
list<list<DAE.Exp>> rest;
list<DAE.Exp> expLst, restDiff;
DAE.Exp de, res;
DAE.Exp de, res, zero;
DAE.Type ty;
Integer ix;

Expand All @@ -2455,13 +2459,17 @@ algorithm

case (expLst::rest, de::restDiff, DAE.TSUB(exp=DAE.CALL(path=path, attr=attr), ix =ix, ty=ty), _) equation
res = DAE.TSUB(DAE.CALL(path, expLst, attr), ix, ty);
res = Expression.expMul(de, res);
ty = Expression.typeof(de);
(zero,_) = Expression.makeZeroExpression(Expression.arrayDimension(ty));
res = DAE.IFEXP(DAE.RELATION(de, DAE.NEQUAL(ty), zero, -1, NONE()), Expression.expMul(de, res), zero);
res = Expression.expAdd(inAccum, res);
then createPartialSum(rest, restDiff, inCall, res);

case (expLst::rest, de::restDiff, DAE.CALL(path=path, attr=attr), _) equation
res = DAE.CALL(path, expLst, attr);
res = Expression.expMul(de, res);
ty = Expression.typeof(de);
(zero,_) = Expression.makeZeroExpression(Expression.arrayDimension(ty));
res = DAE.IFEXP(DAE.RELATION(de, DAE.NEQUAL(ty), zero, -1, NONE()), Expression.expMul(de, res), zero);
res = Expression.expAdd(inAccum, res);
then createPartialSum(rest, restDiff, inCall, res);
end match;
Expand Down
5 changes: 2 additions & 3 deletions Compiler/BackEnd/SymbolicJacobian.mo
Expand Up @@ -1816,13 +1816,12 @@ algorithm
{"removeEqualFunctionCalls",
"removeSimpleEquations",
"evalFunc",
//"wrapFunctionCalls",
"simplifyAllExpressions"},
NONE(),
NONE(),
{"inlineArrayEqn",
{"wrapFunctionCalls",
"inlineArrayEqn",
"constantLinearSystem",
"removeSimpleEquations",
"tearingSystem",
"calculateStrongComponentJacobians",
"removeConstants",
Expand Down
18 changes: 18 additions & 0 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -3608,6 +3608,24 @@ algorithm
end matchcontinue;
end createSymbolicSimulationJacobian;

protected function getFurtherVars
input BackendDAE.Var v;
input tuple<list<BackendDAE.Var>, DAE.ComponentRef> inTpl;
output BackendDAE.Var outVar = v;
output tuple<list<BackendDAE.Var>, DAE.ComponentRef> outTpl;
protected
DAE.ComponentRef diffCref;
list<BackendDAE.Var> vars;
Boolean b;
algorithm
(vars, diffCref) := inTpl;
b := ComponentReference.crefLastIdentEqual(BackendVariable.varCref(v), diffCref);
if not b then
vars := v::vars;
end if;
outTpl := (vars, diffCref);
end getFurtherVars;

protected function createJacobianLinearCode
input BackendDAE.SymbolicJacobians inSymjacs;
input SimCode.ModelInfo inModelInfo;
Expand Down

0 comments on commit 4b7f96a

Please sign in to comment.