Skip to content

Commit

Permalink
- do not inline functions for jacobians.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18347 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Nov 29, 2013
1 parent 2c5a13d commit 33d355b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Compiler/BackEnd/BackendDAEUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -6379,7 +6379,7 @@ algorithm
dcrexp = DAE.CALL(Absyn.IDENT("der"),{dcrexp},DAE.callAttrBuiltinReal);
((e,_)) = Expression.replaceExp(inExp, dcrexp, Expression.crefExp(dcr));
ft = getFunctions(iShared);
(e_1, shared) = Differentiate.differentiateExpCref(e, dcr, vars, iShared);
(e_1, shared) = Differentiate.differentiateExpCrefFullJacobian(e, dcr, vars, iShared);
(e_2,_) = ExpressionSimplify.simplify(e_1);
es = calculateJacobianRow3(eqn_indx,vindx,e_2,source,iAcc);
(es, shared) = calculateJacobianRow2(inExp, vars, eqn_indx, vindxs, differentiateIfExp, shared, source, es);
Expand All @@ -6391,7 +6391,7 @@ algorithm
v = BackendVariable.getVarAt(vars, vindx);
cr = BackendVariable.varCref(v);
ft = getFunctions(iShared);
(e_1, shared) = Differentiate.differentiateExpCref(inExp, cr, vars, iShared);
(e_1, shared) = Differentiate.differentiateExpCrefFullJacobian(inExp, cr, vars, iShared);
(e_2,_) = ExpressionSimplify.simplify(e_1);
es = calculateJacobianRow3(eqn_indx,vindx,e_2,source,iAcc);
(es, shared) = calculateJacobianRow2(inExp, vars, eqn_indx, vindxs, differentiateIfExp, shared, source, es);
Expand Down Expand Up @@ -6980,6 +6980,7 @@ algorithm
case ((e as DAE.CALL(path = Absyn.IDENT(name = "pre")),(vars,b)))
then
((e,false,(vars,b)));

// found ?
case ((e as DAE.CREF(componentRef = cr),(vars,_)))
equation
Expand Down
52 changes: 51 additions & 1 deletion Compiler/BackEnd/Differentiate.mo
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,46 @@ algorithm
end matchcontinue;
end differentiateExpCrefFunction;


public function differentiateExpCrefFullJacobian "function: differentiateEquationTime
Differentiates an equation with respect to the time variable."
input DAE.Exp inExp;
input DAE.ComponentRef inCref;
input BackendDAE.Variables inVariables;
input BackendDAE.Shared ishared;
output DAE.Exp outExp;
output BackendDAE.Shared oshared;
algorithm
(outExp, oshared) := matchcontinue(inExp, inCref, inVariables, ishared)
local
String msg;
BackendDAE.Shared shared;
DAE.Exp dexp;
DAE.FunctionTree funcs;
BackendDAE.DifferentiateInputData diffData;
BackendDAE.Variables knvars;
case (_, _, _, _)
equation
funcs = BackendDAEUtil.getFunctions(ishared);
knvars = BackendDAEUtil.getknvars(ishared);
diffData = BackendDAE.DIFFINPUTDATA(NONE(), SOME(inVariables), SOME(knvars), NONE(), SOME({}), NONE(), NONE());
(dexp, funcs) = differentiateExp(inExp, inCref, diffData, BackendDAE.FULL_JACOBIAN(), funcs);
oshared = BackendDAEUtil.addFunctionTree(funcs, ishared);
then (dexp, oshared);
else
equation
// expandDerOperator expectes sometime that differentiate fails,
// so the calling function need to take care of the error messages.
// TODO: change that in expandDerOperator
//Error.addSourceMessage(Error.INTERNAL_ERROR, {msg}, DAEUtil.getElementSourceFileInfo(DAE.emptyElementSource));

true = Flags.isSet(Flags.FAILTRACE);
msg = "\nDifferentiate.differentiateExpTime failed for " +& ExpressionDump.printExpStr(inExp) +& "\n\n";
Debug.fprint(Flags.FAILTRACE, msg);
then fail();

end matchcontinue;
end differentiateExpCrefFullJacobian;
// =============================================================================
// further interface functions to differentiation
// - differentiateEquation
Expand Down Expand Up @@ -1027,6 +1067,14 @@ algorithm
then
(zero, inFunctionTree);

// D(y)/dx => 0
case (DAE.CREF(componentRef = cr, ty = tp), _, _, BackendDAE.FULL_JACOBIAN(), _)
equation
(zero,_) = Expression.makeZeroExpression(Expression.arrayDimension(tp));

//print("\nExp-Cref\n d(x)/d(x) = 1");
then
(zero, inFunctionTree);
// Constants, known variables, parameters and discrete variables have a 0-derivative, not the inputs
case ((e as DAE.CREF(componentRef = cr, ty = tp)), _, BackendDAE.DIFFINPUTDATA(knownVars=SOME(knvars)), _, _)
equation
Expand Down Expand Up @@ -1919,7 +1967,8 @@ algorithm
case (e as DAE.CALL(path = path,expLst = expl,attr=DAE.CALL_ATTR(tuple_=b,builtin=false,isImpure=isImpure,ty=ty,tailCall=tc)), _, _, _, _)
equation
//s1 = ExpressionDump.printExpStr(e);
//print("\nExp-CALL\n build-funcs force-inline: " +& s1);
//print("\nExp-CALL\n build-funcs force-inline: " +& s1);
failure(BackendDAE.FULL_JACOBIAN() = inDiffType);
(e,_,true) = Inline.forceInlineExp(e,(SOME(inFunctionTree),{DAE.NORM_INLINE(),DAE.NO_INLINE()}),DAE.emptyElementSource);
e = Expression.addNoEventToRelations(e);
(e, functions) = differentiateExp(e, inDiffwrtCref, inInputData, inDiffType, inFunctionTree);
Expand All @@ -1932,6 +1981,7 @@ algorithm
// TODO: FIXIT! expressionSolve and analyticJacobian don't
// return new functionTree, so we can't differentiate functions then.
failure(BackendDAE.SIMPLE_DIFFERENTAION() = inDiffType);
failure(BackendDAE.FULL_JACOBIAN() = inDiffType);

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

0 comments on commit 33d355b

Please sign in to comment.