Skip to content

Commit 33d355b

Browse files
author
Willi Braun
committed
- do not inline functions for jacobians.
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18347 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 2c5a13d commit 33d355b

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

Compiler/BackEnd/BackendDAEUtil.mo

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6379,7 +6379,7 @@ algorithm
63796379
dcrexp = DAE.CALL(Absyn.IDENT("der"),{dcrexp},DAE.callAttrBuiltinReal);
63806380
((e,_)) = Expression.replaceExp(inExp, dcrexp, Expression.crefExp(dcr));
63816381
ft = getFunctions(iShared);
6382-
(e_1, shared) = Differentiate.differentiateExpCref(e, dcr, vars, iShared);
6382+
(e_1, shared) = Differentiate.differentiateExpCrefFullJacobian(e, dcr, vars, iShared);
63836383
(e_2,_) = ExpressionSimplify.simplify(e_1);
63846384
es = calculateJacobianRow3(eqn_indx,vindx,e_2,source,iAcc);
63856385
(es, shared) = calculateJacobianRow2(inExp, vars, eqn_indx, vindxs, differentiateIfExp, shared, source, es);
@@ -6391,7 +6391,7 @@ algorithm
63916391
v = BackendVariable.getVarAt(vars, vindx);
63926392
cr = BackendVariable.varCref(v);
63936393
ft = getFunctions(iShared);
6394-
(e_1, shared) = Differentiate.differentiateExpCref(inExp, cr, vars, iShared);
6394+
(e_1, shared) = Differentiate.differentiateExpCrefFullJacobian(inExp, cr, vars, iShared);
63956395
(e_2,_) = ExpressionSimplify.simplify(e_1);
63966396
es = calculateJacobianRow3(eqn_indx,vindx,e_2,source,iAcc);
63976397
(es, shared) = calculateJacobianRow2(inExp, vars, eqn_indx, vindxs, differentiateIfExp, shared, source, es);
@@ -6980,6 +6980,7 @@ algorithm
69806980
case ((e as DAE.CALL(path = Absyn.IDENT(name = "pre")),(vars,b)))
69816981
then
69826982
((e,false,(vars,b)));
6983+
69836984
// found ?
69846985
case ((e as DAE.CREF(componentRef = cr),(vars,_)))
69856986
equation

Compiler/BackEnd/Differentiate.mo

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,46 @@ algorithm
255255
end matchcontinue;
256256
end differentiateExpCrefFunction;
257257

258+
259+
public function differentiateExpCrefFullJacobian "function: differentiateEquationTime
260+
Differentiates an equation with respect to the time variable."
261+
input DAE.Exp inExp;
262+
input DAE.ComponentRef inCref;
263+
input BackendDAE.Variables inVariables;
264+
input BackendDAE.Shared ishared;
265+
output DAE.Exp outExp;
266+
output BackendDAE.Shared oshared;
267+
algorithm
268+
(outExp, oshared) := matchcontinue(inExp, inCref, inVariables, ishared)
269+
local
270+
String msg;
271+
BackendDAE.Shared shared;
272+
DAE.Exp dexp;
273+
DAE.FunctionTree funcs;
274+
BackendDAE.DifferentiateInputData diffData;
275+
BackendDAE.Variables knvars;
276+
case (_, _, _, _)
277+
equation
278+
funcs = BackendDAEUtil.getFunctions(ishared);
279+
knvars = BackendDAEUtil.getknvars(ishared);
280+
diffData = BackendDAE.DIFFINPUTDATA(NONE(), SOME(inVariables), SOME(knvars), NONE(), SOME({}), NONE(), NONE());
281+
(dexp, funcs) = differentiateExp(inExp, inCref, diffData, BackendDAE.FULL_JACOBIAN(), funcs);
282+
oshared = BackendDAEUtil.addFunctionTree(funcs, ishared);
283+
then (dexp, oshared);
284+
else
285+
equation
286+
// expandDerOperator expectes sometime that differentiate fails,
287+
// so the calling function need to take care of the error messages.
288+
// TODO: change that in expandDerOperator
289+
//Error.addSourceMessage(Error.INTERNAL_ERROR, {msg}, DAEUtil.getElementSourceFileInfo(DAE.emptyElementSource));
290+
291+
true = Flags.isSet(Flags.FAILTRACE);
292+
msg = "\nDifferentiate.differentiateExpTime failed for " +& ExpressionDump.printExpStr(inExp) +& "\n\n";
293+
Debug.fprint(Flags.FAILTRACE, msg);
294+
then fail();
295+
296+
end matchcontinue;
297+
end differentiateExpCrefFullJacobian;
258298
// =============================================================================
259299
// further interface functions to differentiation
260300
// - differentiateEquation
@@ -1027,6 +1067,14 @@ algorithm
10271067
then
10281068
(zero, inFunctionTree);
10291069

1070+
// D(y)/dx => 0
1071+
case (DAE.CREF(componentRef = cr, ty = tp), _, _, BackendDAE.FULL_JACOBIAN(), _)
1072+
equation
1073+
(zero,_) = Expression.makeZeroExpression(Expression.arrayDimension(tp));
1074+
1075+
//print("\nExp-Cref\n d(x)/d(x) = 1");
1076+
then
1077+
(zero, inFunctionTree);
10301078
// Constants, known variables, parameters and discrete variables have a 0-derivative, not the inputs
10311079
case ((e as DAE.CREF(componentRef = cr, ty = tp)), _, BackendDAE.DIFFINPUTDATA(knownVars=SOME(knvars)), _, _)
10321080
equation
@@ -1919,7 +1967,8 @@ algorithm
19191967
case (e as DAE.CALL(path = path,expLst = expl,attr=DAE.CALL_ATTR(tuple_=b,builtin=false,isImpure=isImpure,ty=ty,tailCall=tc)), _, _, _, _)
19201968
equation
19211969
//s1 = ExpressionDump.printExpStr(e);
1922-
//print("\nExp-CALL\n build-funcs force-inline: " +& s1);
1970+
//print("\nExp-CALL\n build-funcs force-inline: " +& s1);
1971+
failure(BackendDAE.FULL_JACOBIAN() = inDiffType);
19231972
(e,_,true) = Inline.forceInlineExp(e,(SOME(inFunctionTree),{DAE.NORM_INLINE(),DAE.NO_INLINE()}),DAE.emptyElementSource);
19241973
e = Expression.addNoEventToRelations(e);
19251974
(e, functions) = differentiateExp(e, inDiffwrtCref, inInputData, inDiffType, inFunctionTree);
@@ -1932,6 +1981,7 @@ algorithm
19321981
// TODO: FIXIT! expressionSolve and analyticJacobian don't
19331982
// return new functionTree, so we can't differentiate functions then.
19341983
failure(BackendDAE.SIMPLE_DIFFERENTAION() = inDiffType);
1984+
failure(BackendDAE.FULL_JACOBIAN() = inDiffType);
19351985

19361986
// get algorithm of the function
19371987
SOME(func) = DAEUtil.avlTreeGet(inFunctionTree,path);

0 commit comments

Comments
 (0)