Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 5056d0b

Browse files
Willi BraunOpenModelica-Hudson
authored andcommitted
[Backend] fix differentiation of functions with function arguments
Belonging to [master]: - #2184 - OpenModelica/OpenModelica-testsuite#850
1 parent 3cc00f3 commit 5056d0b

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

Compiler/BackEnd/Differentiate.mo

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,13 +1365,21 @@ algorithm
13651365
cr = ComponentReference.crefPrefixDer(cr);
13661366
cr = createDifferentiatedCrefName(cr, inDiffwrtCref, matrixName);
13671367
res = Expression.makeCrefExp(cr, tp);
1368+
then
1369+
(res, inFunctionTree);
13681370

1369-
b = ComponentReference.crefEqual(DAE.CREF_IDENT("$",DAE.T_REAL_DEFAULT,{}), inDiffwrtCref);
1370-
(zero,_) = Expression.makeZeroExpression(Expression.arrayDimension(tp));
1371+
/* Differentiate with respect to DAE.CREF_IDENT(ident="$") demands zero expressions */
1372+
case (DAE.CALL(path=Absyn.IDENT(name = "der"),expLst = {e}), DAE.CREF_IDENT(ident="$"), _, _, _)
1373+
equation
1374+
(zero,_) = Expression.makeZeroExpression(Expression.arrayDimension(Expression.typeof(e)));
1375+
then
1376+
(zero, inFunctionTree);
13711377

1372-
res = if b then zero else res;
1378+
case (e as DAE.CALL(attr=DAE.CALL_ATTR(ty=tp)), DAE.CREF_IDENT(ident="$"), _, _, _)
1379+
equation
1380+
(zero,_) = Expression.makeZeroExpression(Expression.arrayDimension(tp));
13731381
then
1374-
(res, inFunctionTree);
1382+
(zero, inFunctionTree);
13751383

13761384
// differentiate builtin calls with 1 argument
13771385
case (DAE.CALL(path=Absyn.IDENT(name),attr=DAE.CALL_ATTR(builtin=true),expLst={e}), _, _, _, _)
@@ -1383,7 +1391,6 @@ algorithm
13831391
//print("\nresults to exp: " + s1);
13841392
then (res, funcs);
13851393

1386-
13871394
// differentiate builtin calls with N arguments with match
13881395
// der(arctan2(y,0)) = der(sign(y)*pi/2) = 0
13891396
case (DAE.CALL(path=Absyn.IDENT("atan2"),attr=DAE.CALL_ATTR(builtin=true),expLst={_,e1 as DAE.RCONST(real=0.0)}), _, _, _, _)
@@ -2301,20 +2308,26 @@ algorithm
23012308
expBoolLst = List.threadTuple(expl, blst);
23022309
expBoolLst = List.filterOnTrue(expBoolLst, Util.tuple22);
23032310
expl1 = List.map(expBoolLst, Util.tuple21);
2304-
(dexpl, functions) = List.map3Fold(expl1, function differentiateExp(maxIter=maxIter), inDiffwrtCref, inInputData, inDiffType, functions);
2305-
(dexplZero, functions) = List.map3Fold(expl1, function differentiateExp(maxIter=maxIter), DAE.CREF_IDENT("$",DAE.T_REAL_DEFAULT,{}), BackendDAE.emptyInputData, BackendDAE.GENERIC_GRADIENT(), functions);
2311+
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION_VERBOSE) then
2312+
print("### Selected Arguments: \n");
2313+
print(stringDelimitList(List.map(expl1, ExpressionDump.printExpStr), ", ") + "\n");
2314+
end if;
23062315

2307-
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
2308-
print("### differentiated argument list:\n");
2316+
(dexpl, functions) = List.map3Fold(expl1, function differentiateExp(maxIter=maxIter), inDiffwrtCref, inInputData, inDiffType, functions);
2317+
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION_VERBOSE) then
23092318
print("### Diffed ExpList: \n");
23102319
print(stringDelimitList(List.map(dexpl, ExpressionDump.printExpStr), ", ") + "\n");
2320+
end if;
2321+
2322+
(dexplZero, functions) = List.map3Fold(expl1, function differentiateExp(maxIter=maxIter), DAE.CREF_IDENT("$",DAE.T_REAL_DEFAULT,{}), BackendDAE.emptyInputData, BackendDAE.GENERIC_GRADIENT(), functions);
2323+
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION_VERBOSE) then
23112324
print("### Diffed ExpList extended: \n");
23122325
print(stringDelimitList(List.map(dexplZero, ExpressionDump.printExpStr), ", ") + "\n");
23132326
end if;
23142327

23152328
e = DAE.CALL(dpath,dexpl,DAE.CALL_ATTR(dtp,b,false,isImpure,false,DAE.NO_INLINE(),tc));
23162329
exp = createPartialArguments(ty, dexpl, dexplZero, expl, e);
2317-
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
2330+
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION_VERBOSE) then
23182331
print("### differentiated Call :\n");
23192332
print(ExpressionDump.printExpStr(e) + "\n");
23202333
print("### -> result exp: \n");

0 commit comments

Comments
 (0)