@@ -781,19 +781,17 @@ end differentiateExp;
781781
782782 case {} then (listReverse(inStmtsAccum), inFunctionTree);
783783
784- // skip discrete statements
785- case currStatement::restStatements guard( isDiscreteAssignStatment(currStatement) )
786- equation
787- derivedStatements1 = currStatement::inStmtsAccum;
788- (derivedStatements2, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, inFunctionTree, maxIter);
789- then (derivedStatements2, functions);
790-
791784 case (currStatement as DAE . STMT_ASSIGN (type_= type_, exp1= lhs, exp= rhs, source= source))::restStatements
792785 equation
793786 (derivedLHS, functions) = differentiateExp(lhs, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter);
794787 (derivedRHS, functions) = differentiateExp(rhs, inDiffwrtCref, inInputData, inDiffType, functions, maxIter);
795788 (derivedRHS,_) = ExpressionSimplify . simplify(derivedRHS);
796- derivedStatements1 = {DAE . STMT_ASSIGN (type_, derivedLHS, derivedRHS, source), currStatement};
789+ /* if assigment is empty remove derived assignment */
790+ if (Expression . isZero(derivedLHS)) then
791+ derivedStatements1 = {currStatement};
792+ else
793+ derivedStatements1 = {DAE . STMT_ASSIGN (type_, derivedLHS, derivedRHS, source), currStatement};
794+ end if ;
797795 derivedStatements1 = listAppend(derivedStatements1, inStmtsAccum);
798796 (derivedStatements2, functions) = differentiateStatements(restStatements, inDiffwrtCref, inInputData, inDiffType, derivedStatements1, functions, maxIter);
799797 then (derivedStatements2, functions);
@@ -825,7 +823,7 @@ end differentiateExp;
825823 equation
826824 cref = ComponentReference . makeCrefIdent(ident, DAE . T_INTEGER_DEFAULT , {});
827825 controlVar = BackendDAE . VAR (cref, BackendDAE . DISCRETE (), DAE . BIDIR (), DAE . NON_PARALLEL (), DAE . T_REAL_DEFAULT , NONE (), NONE (), {}, DAE . emptyElementSource, NONE (), NONE (), DAE . BCONST (false ), NONE (), DAE . NON_CONNECTOR (), DAE . NOT_INNER_OUTER (), false );
828- inputData = addAllVars ({controlVar}, inInputData);
826+ inputData = addGlobalVars ({controlVar}, inInputData);
829827 (derivedStatements1, functions) = differentiateStatements(statementLst, inDiffwrtCref, inputData, inDiffType, {}, inFunctionTree, maxIter);
830828
831829 derivedStatements1 = {DAE . STMT_FOR (type_, iterIsArray, ident, index, exp, derivedStatements1, source)};
@@ -1110,7 +1108,7 @@ algorithm
11101108 (zero, inFunctionTree);
11111109
11121110 // d(discrete)/d(x) = 0
1113- case ((DAE . CREF (componentRef = cr,ty = tp)), _, BackendDAE . DIFFINPUTDATA (allVars= SOME (timevars)), _ , _)
1111+ case ((DAE . CREF (componentRef = cr,ty = tp)), _, BackendDAE . DIFFINPUTDATA (allVars= SOME (timevars)), BackendDAE . DIFFERENTIATION_TIME () , _)
11141112 equation
11151113 (BackendDAE . VAR (varKind = kind),_) = BackendVariable . getVarSingle(cr, timevars);
11161114 // print("\nExp-Cref\n known vars: " + ComponentReference.printComponentRefStr(cr));
@@ -1466,9 +1464,8 @@ algorithm
14661464
14671465 case ("pre" ,_) equation
14681466 tp = Expression . typeof(exp);
1469- (exp_1, funcs) = differentiateExp(exp, inDiffwrtCref, inInputData,inDiffType,inFuncs, maxIter);
1470- exp_2 = Expression . makePureBuiltinCall("pre" , {exp_1}, tp);
1471- then (exp_2, inFuncs);
1467+ exp_1 = Expression . makeZeroExpression(Expression . arrayDimension(tp));
1468+ then (exp_1, inFuncs);
14721469
14731470 case ("previous" ,_) equation
14741471 tp = Expression . typeof(exp);
@@ -1786,6 +1783,12 @@ algorithm
17861783 then
17871784 (res1, funcs);
17881785
1786+ case ("sample" , _, DAE . CALL_ATTR (ty= tp))
1787+ equation
1788+ (res1, _) = Expression . makeZeroExpression(Expression . arrayDimension(tp));
1789+ then
1790+ (res1, inFunctionTree);
1791+
17891792 end match;
17901793end differentiateCallExpNArg;
17911794
@@ -2578,6 +2581,11 @@ algorithm
25782581 // print("Function diff: statemeants");
25792582 (derbodyStmts, functions) = differentiateStatements(listReverse(bodyStmts), inDiffwrtCref, inputData, BackendDAE . DIFFERENTIATION_FUNCTION (), {}, functions, maxIter);
25802583
2584+ if Flags . isSet(Flags . DEBUG_DIFFERENTIATION_VERBOSE ) then
2585+ funstring = DAEDump . ppStmtListStr(derbodyStmts, 0 );
2586+ print("### Differentiate differentiateFunctionCallPartial stmts: \n " + funstring + " \n\n " );
2587+ end if ;
2588+
25812589 // create function and add it to function tree
25822590 dpath = Util . getOptionOrDefault(dpathOption, Absyn . stringPath("$DER" + funcname));
25832591
@@ -3003,6 +3011,35 @@ algorithm
30033011 end match;
30043012end addAllVars;
30053013
3014+ protected function addGlobalVars
3015+ input list< BackendDAE . Var > inVarsLst;
3016+ input BackendDAE . DifferentiateInputData inDiffData;
3017+ output BackendDAE . DifferentiateInputData outDiffData;
3018+ algorithm
3019+ outDiffData := match(inVarsLst, inDiffData)
3020+ local
3021+ Option < BackendDAE . Variables > depVars, allVars, indepVars;
3022+ BackendDAE . Variables glVars;
3023+ list< BackendDAE . Var > algVars;
3024+ list< . DAE . ComponentRef > diffCrefs;
3025+ Option < String > diffname;
3026+
3027+ case ({}, _)
3028+ then inDiffData;
3029+
3030+ case (_, BackendDAE . DIFFINPUTDATA (indepVars, depVars, SOME (glVars), allVars, algVars, diffCrefs, diffname)) equation
3031+ glVars = BackendVariable . addVars(inVarsLst, glVars);
3032+ // BackendDump.dumpVariables(allVars, "indep Vars: ");
3033+ then BackendDAE . DIFFINPUTDATA (indepVars, depVars, SOME (glVars), allVars, algVars, diffCrefs, diffname);
3034+
3035+ case (_, BackendDAE . DIFFINPUTDATA (indepVars, depVars, NONE (), allVars, algVars, diffCrefs, diffname)) equation
3036+ glVars = BackendVariable . listVar(inVarsLst);
3037+ // BackendDump.dumpVariables(allVars, "indep Vars: ");
3038+ then BackendDAE . DIFFINPUTDATA (indepVars, depVars, SOME (glVars), allVars, algVars, diffCrefs, diffname);
3039+
3040+ end match;
3041+ end addGlobalVars;
3042+
30063043protected function lowerVarsElementVars
30073044 input list< DAE . Element > inElementLstVars;
30083045 input DAE . FunctionTree functions;
@@ -3040,7 +3077,6 @@ algorithm
30403077 end try ;
30413078end addElementVars2Dep;
30423079
3043-
30443080protected function dumpInputData
30453081 input BackendDAE . DifferentiateInputData inDiffData;
30463082protected
0 commit comments