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

Commit 0a46029

Browse files
WilliOpenModelica-Hudson
authored andcommitted
partial differentiation of function with record outputs
Belonging to [master]: - #2769 - OpenModelica/OpenModelica-testsuite#1083
1 parent 210af85 commit 0a46029

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

Compiler/BackEnd/Differentiate.mo

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ algorithm
555555
*/
556556
(outDiffedExp, outFunctionTree) := match inExp
557557
local
558-
Absyn.Path p;
558+
Absyn.Path p, p1, p2;
559559
Boolean b;
560560
DAE.CallAttributes attr;
561561
DAE.Exp e1, e2, e3, actual, simplified;
@@ -566,6 +566,7 @@ algorithm
566566
Integer i;
567567
String s1, s2, stp;
568568
list<String> strLst;
569+
list<DAE.Var> varLst;
569570
//String se1;
570571
list<DAE.Exp> sub, expl;
571572
list<list<DAE.Exp>> matrix, dmatrix;
@@ -684,15 +685,25 @@ algorithm
684685
then (res, functionTree);
685686

686687

687-
// differentiate tsub
688+
// differentiate rsub
688689
case e1 as DAE.RSUB()
689690
algorithm
690691
(res1, functionTree) := differentiateExp(e1.exp, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
691692
if not referenceEq(e1.exp, res1) then
692-
e1.exp := res1;
693-
(e1,_) := ExpressionSimplify.simplify1(e1);
693+
try
694+
(expl, strLst) := match res1
695+
case DAE.RECORD(exps=expl,comp=strLst) then (expl, strLst);
696+
case DAE.CALL(path=p1,expLst=expl,attr=DAE.CALL_ATTR(ty=DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(path=p2), varLst=varLst)))
697+
guard Absyn.pathEqual(p1,p2)
698+
then (expl, list(v.name for v in varLst));
699+
end match;
700+
res := listGet(expl, List.position1OnTrue(strLst, stringEq, e1.fieldName));
701+
else
702+
e1.exp := res1;
703+
(res,_) := ExpressionSimplify.simplify1(e1);
704+
end try;
694705
end if;
695-
then (e1, functionTree);
706+
then (res, functionTree);
696707

697708
// differentiate tuple
698709
case DAE.TUPLE(PR=expl) equation
@@ -2393,15 +2404,21 @@ protected function createPartialArguments
23932404
algorithm
23942405
outExp := matchcontinue(outputType, inCall)
23952406
local
2396-
Absyn.Path path;
2407+
Absyn.Path path, rPath;
23972408
DAE.CallAttributes attr;
23982409
list<DAE.Exp> expLst;
23992410
DAE.Exp ezero, e;
24002411
DAE.Dimensions dims;
24012412
list<DAE.Type> tys;
2413+
list<DAE.Var> varLst;
2414+
list<String> varNames;
24022415

2403-
case (DAE.T_COMPLEX(complexClassType=ClassInf.RECORD()), DAE.CALL(path=path, attr=attr))
2404-
then DAE.CALL(path, listAppend(inOrginalExpl,inArgs), attr);
2416+
case (DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(path=rPath),varLst=varLst), DAE.CALL(path=path, attr=attr))
2417+
equation
2418+
tys = list(DAEUtil.varType(v) for v in varLst);
2419+
varNames = list(DAEUtil.typeVarIdent(v) for v in varLst);
2420+
expLst = createPartialArgumentsRecord(tys, varNames, inArgs, inDiffedArgs, inOrginalExpl, inCall);
2421+
then DAE.RECORD(rPath, expLst, varNames, outputType);
24052422

24062423
case (DAE.T_COMPLEX(complexClassType=ClassInf.RECORD()), DAE.TSUB(exp=DAE.CALL(path=path, attr=attr)))
24072424
then DAE.CALL(path, listAppend(inOrginalExpl,inArgs), attr);
@@ -2438,6 +2455,19 @@ algorithm
24382455
threaded for tp in inTypesLst, number in 1:listLength(inTypesLst));
24392456
end createPartialArgumentsTuple;
24402457

2458+
protected function createPartialArgumentsRecord
2459+
input list<DAE.Type> inTypesLst;
2460+
input list<DAE.String> inVarNames;
2461+
input list<DAE.Exp> inArgs;
2462+
input list<DAE.Exp> inDiffedArgs;
2463+
input list<DAE.Exp> inOrginalExpl;
2464+
input DAE.Exp inCall;
2465+
output list<DAE.Exp> outExpLst;
2466+
algorithm
2467+
outExpLst := list( createPartialArguments(tp, inArgs, inDiffedArgs, inOrginalExpl, (DAE.RSUB(inCall, -1, name, tp)) )
2468+
threaded for tp in inTypesLst, name in inVarNames);
2469+
end createPartialArgumentsRecord;
2470+
24412471
protected function createPartialDifferentiatedExp
24422472
"Generates an expression with a sum partial derivatives."
24432473
input list<DAE.Exp> inDiffExpl;
@@ -2518,8 +2548,12 @@ algorithm
25182548
DAE.CallAttributes attr;
25192549
DAE.Type ty;
25202550
Integer ix;
2551+
String name;
2552+
2553+
case DAE.RSUB(exp=DAE.CALL(path=path, attr=attr), ix=ix, fieldName=name, ty=ty)
2554+
then DAE.RSUB(DAE.CALL(path, expLst, attr), ix, name, ty);
25212555

2522-
case DAE.TSUB(exp=DAE.CALL(path=path, attr=attr), ix =ix, ty=ty)
2556+
case DAE.TSUB(exp=DAE.CALL(path=path, attr=attr), ix=ix, ty=ty)
25232557
then DAE.TSUB(DAE.CALL(path, expLst, attr), ix, ty);
25242558

25252559
case DAE.CALL(path=path, attr=attr) equation

0 commit comments

Comments
 (0)