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

Commit 702501a

Browse files
Willi BraunOpenModelica-Hudson
authored andcommitted
enable discrete vars in symbolic jacobian
1 parent fff1788 commit 702501a

File tree

4 files changed

+57
-67
lines changed

4 files changed

+57
-67
lines changed

Compiler/BackEnd/Differentiate.mo

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
17901793
end 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;
30043012
end 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+
30063043
protected function lowerVarsElementVars
30073044
input list<DAE.Element> inElementLstVars;
30083045
input DAE.FunctionTree functions;
@@ -3040,7 +3077,6 @@ algorithm
30403077
end try;
30413078
end addElementVars2Dep;
30423079

3043-
30443080
protected function dumpInputData
30453081
input BackendDAE.DifferentiateInputData inDiffData;
30463082
protected

Compiler/BackEnd/SymbolicJacobian.mo

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,11 +2123,6 @@ algorithm
21232123
case({}, _, _, _, _, _)
21242124
then listReverse(iVars);
21252125

2126-
// skip for dicrete variable
2127-
case(BackendDAE.VAR(varKind=BackendDAE.DISCRETE())::restVar, cref, _, _, _, _)
2128-
then
2129-
createAllDiffedVarsWork(restVar,cref,inAllVars,inIndex, inMatrixName,iVars);
2130-
21312126
case(BackendDAE.VAR(varName=currVar,varKind=BackendDAE.STATE())::restVar, cref, _, index, _, _) algorithm
21322127
try
21332128
(_, _) := BackendVariable.getVarSingle(currVar, inAllVars);
@@ -2159,7 +2154,7 @@ algorithm
21592154
end match;
21602155
end createAllDiffedVarsWork;
21612156

2162-
protected function deriveAll "author: lochel"
2157+
protected function deriveAll
21632158
input list<BackendDAE.Equation> inEquations;
21642159
input list<Integer> ass2;
21652160
input DAE.ComponentRef inDiffCref;
@@ -2169,7 +2164,7 @@ protected function deriveAll "author: lochel"
21692164
output DAE.FunctionTree outFunctions = inFunctions;
21702165
protected
21712166
BackendDAE.Variables allVars;
2172-
list<BackendDAE.Equation> currDerivedEquations;
2167+
BackendDAE.Equation currDerivedEquation;
21732168
list<BackendDAE.Var> solvedvars;
21742169
list<Integer> ass2_1 = ass2, solvedfor;
21752170
Boolean b;
@@ -2183,17 +2178,11 @@ algorithm
21832178
print("\n");
21842179
end if;
21852180

2186-
// filter discrete equataions
2187-
(solvedfor,ass2_1) := List.split(ass2_1, BackendEquation.equationSize(currEquation));
2188-
solvedvars := List.map1r(solvedfor,BackendVariable.getVarAt, allVars);
2189-
b := List.mapAllValueBool(solvedvars, BackendVariable.isVarDiscrete, true);
2190-
b := b or BackendEquation.isWhenEquation(currEquation);
2191-
2192-
(currDerivedEquations, outFunctions) := deriveAllHelper(b, currEquation, inDiffCref, inDiffData, outFunctions);
2193-
outDerivedEquations := listAppend(currDerivedEquations, outDerivedEquations);
2181+
(currDerivedEquation, outFunctions) := Differentiate.differentiateEquation(currEquation, inDiffCref, inDiffData, BackendDAE.GENERIC_GRADIENT(), outFunctions);
2182+
outDerivedEquations := currDerivedEquation::outDerivedEquations;
21942183

21952184
if Flags.isSet(Flags.JAC_DUMP_EQN) then
2196-
BackendDump.printEquationList(currDerivedEquations);
2185+
BackendDump.printEquationList(outDerivedEquations);
21972186
print("\n");
21982187
end if;
21992188
end for;
@@ -2206,36 +2195,6 @@ algorithm
22062195
end try;
22072196
end deriveAll;
22082197

2209-
protected function deriveAllHelper "author: wbraun"
2210-
input Boolean isDiscrete;
2211-
input BackendDAE.Equation inEquation;
2212-
input DAE.ComponentRef inDiffCref;
2213-
input BackendDAE.DifferentiateInputData inDiffData;
2214-
input DAE.FunctionTree inFunctions;
2215-
output list<BackendDAE.Equation> outDerivedEquations;
2216-
output DAE.FunctionTree outFunctions;
2217-
algorithm
2218-
(outDerivedEquations, outFunctions) :=
2219-
match (isDiscrete)
2220-
local
2221-
BackendDAE.Equation derEquation;
2222-
DAE.FunctionTree functions;
2223-
list<DAE.ComponentRef> vars;
2224-
BackendDAE.Variables allVars, paramVars, stateVars, globalKnownVars;
2225-
list<Integer> ass2_1, solvedfor;
2226-
2227-
case (true) equation
2228-
if Flags.isSet(Flags.JAC_WARNINGS) then
2229-
print("BackendDAEOptimize.derive: discrete equation has been removed.\n");
2230-
end if;
2231-
then ({}, inFunctions);
2232-
2233-
case (false) equation
2234-
(derEquation, functions) = Differentiate.differentiateEquation(inEquation, inDiffCref, inDiffData, BackendDAE.GENERIC_GRADIENT(), inFunctions);
2235-
then ({derEquation}, functions);
2236-
end match;
2237-
end deriveAllHelper;
2238-
22392198
public function getJacobianMatrixbyName
22402199
input BackendDAE.SymbolicJacobians injacobianMatrixes;
22412200
input String inJacobianName;

Compiler/FrontEnd/DAEDump.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ algorithm
17921792
end match;
17931793
end ppStmtList;
17941794

1795-
protected function ppStmtListStr "
1795+
public function ppStmtListStr "
17961796
Helper function to pp_stmt_str
17971797
"
17981798
input list<DAE.Statement> inAlgorithmStatementLst;

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4905,12 +4905,7 @@ algorithm
49054905

49064906
case({}, _, _, _, _, _) then listReverse(iVars);
49074907

4908-
// skip for dicrete variable
4909-
case(BackendDAE.VAR(varKind=BackendDAE.DISCRETE())::restVar, cref, _, _, _, _) equation
4910-
then
4911-
createAllDiffedSimVars(restVar, cref, inAllVars, inIndex, inMatrixName, iVars);
4912-
4913-
case(BackendDAE.VAR(varName=currVar, varKind=varkind, values = dae_var_attr)::restVar, cref, _, index, _, _) algorithm
4908+
case(BackendDAE.VAR(varName=currVar, varKind=varkind, values = dae_var_attr)::restVar, cref, _, index, _, _) algorithm
49144909
try
49154910
BackendVariable.getVarSingle(currVar, inAllVars);
49164911
r1 := match (varkind)

0 commit comments

Comments
 (0)