Skip to content

Commit

Permalink
[BE] added debug for diff exp and calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Willi Braun authored and OpenModelica-Hudson committed Feb 16, 2018
1 parent 846ccc5 commit 808888c
Showing 1 changed file with 20 additions and 56 deletions.
76 changes: 20 additions & 56 deletions Compiler/BackEnd/Differentiate.mo
Expand Up @@ -533,7 +533,12 @@ protected function differentiateExp
input Integer maxIter;
output DAE.Exp outDiffedExp;
output DAE.FunctionTree outFunctionTree;
protected
constant Boolean debug = false;
algorithm
if debug then print("\nDifferentiate Exp: "+ExpressionDump.printExpStr(inExp)+
" w.r.t. "+ComponentReference.printComponentRefStr(inDiffwrtCref)+"\n"); end if;

/*
// This check does not seem to be necessary since looking through the stack of expression seems to stop iteration in most cases, and you get a spam of messages from this check.
if maxIter < 1 then
Expand All @@ -552,7 +557,7 @@ algorithm
DAE.Operator op;
DAE.Type tp;
Integer i;
String s1, s2;
String s1, s2, stp;
list<String> strLst;
//String se1;
list<DAE.Exp> sub, expl;
Expand All @@ -578,8 +583,6 @@ algorithm

// differentiate cref
case DAE.CREF(componentRef=cref, ty=tp) equation
//se1 = ExpressionDump.printExpStr(inExp);
//print("\nExp-Cref\nDifferentiate exp: " + se1);

if ComponentReference.isStartCref(cref) then
// differentiate start value
Expand All @@ -589,8 +592,6 @@ algorithm
(res, functionTree) = differentiateCrefs(inExp, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
end if;

//se1 = ExpressionDump.printExpStr(res);
//print("\nresults to exp: " + se1);
then (res, functionTree);

// differentiate homotopy
Expand All @@ -602,93 +603,64 @@ algorithm

// differentiate call
case DAE.CALL() equation
//se1 = ExpressionDump.printExpStr(inExp);
//print("\nExp-Call\nDifferentiate exp: " + se1);

(res, functionTree) = differentiateCalls(inExp, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
(res,_) = ExpressionSimplify.simplify1(res);

//se1 = ExpressionDump.printExpStr(res);
//print("\nresults to exp: " + se1);
then (res, functionTree);

// differentiate binary
case DAE.BINARY() equation
//se1 = ExpressionDump.printExpStr(inExp);
//print("\nExp-BINARY\nDifferentiate exp: " + se1);

(res, functionTree) = differentiateBinary(inExp, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
(res) = ExpressionSimplify.simplifyBinaryExp(res);

//se1 = ExpressionDump.printExpStr(res);
//print("\nresults to exp: " + se1);
then (res, functionTree);

// differentiate operator
case DAE.UNARY(operator=op, exp=e1) equation
//se1 = ExpressionDump.printExpStr(inExp);
//print("\nExp-UNARY\nDifferentiate exp: " + se1);

(res, functionTree) = differentiateExp(e1, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);

res = DAE.UNARY(op,res);
(res) = ExpressionSimplify.simplifyUnaryExp(res);

//se1 = ExpressionDump.printExpStr(res);
//print("\nresults to exp: " + se1);
then (res, functionTree);

// differentiate cast
case DAE.CAST(ty=tp, exp=e1) equation
//se1 = ExpressionDump.printExpStr(inExp);
//print("\nExp-CAST\nDifferentiate exp: " + se1);

(res, functionTree) = differentiateExp(e1, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
(res,_) = ExpressionSimplify.simplify1(res);

//se1 = ExpressionDump.printExpStr(res);
//print("\nresults to exp: " + se1);
then (DAE.CAST(tp, res), functionTree);

// differentiate asub
case DAE.ASUB(exp=e1, sub=sub) equation
//se1 = ExpressionDump.printExpStr(inExp);
//print("\nExp-ASUB\nDifferentiate exp: " + se1);

(res1, functionTree) = differentiateExp(e1, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);

res = Expression.makeASUB(res1,sub);
(res,_) = ExpressionSimplify.simplify1(res);

//se1 = ExpressionDump.printExpStr(res);
//print("\nresults to exp: " + se1);
then (res, functionTree);

case DAE.ARRAY(ty=tp, scalar=b, array=expl) equation
//se1 = ExpressionDump.printExpStr(inExp);
//print("\nExp-ARRAY\nDifferentiate exp: " + se1);

(expl, functionTree) = List.map3Fold(expl, function differentiateExp(maxIter=maxIter-1), inDiffwrtCref, inInputData, inDiffType, inFunctionTree);

res = DAE.ARRAY(tp, b, expl);
(res,_) = ExpressionSimplify.simplify1(res);
//(res,_) = ExpressionSimplify.simplify(res);

//se1 = ExpressionDump.printExpStr(res);
//print("\nresults to exp: " + se1);
then (res, functionTree);

case DAE.MATRIX(ty=tp, integer=i, matrix=matrix) equation
//se1 = ExpressionDump.printExpStr(inExp);
//print("\nExp-MARTIX\nDifferentiate exp: " + se1);

(dmatrix, functionTree) = List.map3FoldList(matrix, function differentiateExp(maxIter=maxIter-1), inDiffwrtCref, inInputData, inDiffType, inFunctionTree);

res = DAE.MATRIX(tp, i, dmatrix);
(res,_) = ExpressionSimplify.simplify1(res);

//se1 = ExpressionDump.printExpStr(res);
//print("\nresults to exp: " + se1);
then (res, functionTree);

// differentiate tsub
Expand All @@ -705,7 +677,7 @@ algorithm
then (res, functionTree);


// differentiate tsub
// differentiate tsub
case e1 as DAE.RSUB()
algorithm
(res1, functionTree) := differentiateExp(e1.exp, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
Expand All @@ -715,32 +687,24 @@ algorithm
end if;
then (e1, functionTree);

// differentiate tuple
// differentiate tuple
case DAE.TUPLE(PR=expl) equation
//se1 = ExpressionDump.printExpStr(inExp);
//print("\nExp-TUPLE\nDifferentiate exp: " + se1);

(expl, functionTree) = List.map3Fold(expl, function differentiateExp(maxIter=maxIter-1), inDiffwrtCref, inInputData, inDiffType, inFunctionTree);

res = DAE.TUPLE(expl);
(res,_) = ExpressionSimplify.simplify1(res);

//se1 = ExpressionDump.printExpStr(res);
//print("\nresults to exp: " + se1);
then (res, functionTree);

case DAE.IFEXP(expCond=e1, expThen=e2, expElse=e3) equation
//se1 = ExpressionDump.printExpStr(inExp);
//print("\nExp-IF-EXP\nDifferentiate exp: " + se1);

(res1, functionTree) = differentiateExp(e2, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
(res2, functionTree) = differentiateExp(e3, inDiffwrtCref, inInputData, inDiffType, functionTree, maxIter-1);

res = DAE.IFEXP(e1, res1, res2);
(res,_) = ExpressionSimplify.simplify1(res);

//se1 = ExpressionDump.printExpStr(res);
//print("\nresults to exp: " + se1);
then (res, functionTree);

// boolean expression, e.g. relation, are left as they are
Expand All @@ -757,9 +721,11 @@ algorithm
true = Flags.isSet(Flags.FAILTRACE);
s1 = ExpressionDump.printExpStr(inExp);
s2 = ComponentReference.printComponentRefStr(inDiffwrtCref);
Debug.trace("- differentiateExp " + s1 + " w.r.t " + s2 + " failed\n");
stp = Types.printTypeStr(Expression.typeof(inExp));
Debug.trace("- differentiateExp " + s1 + " type: " + stp + " w.r.t " + s2 + " failed\n");
then fail();
end match;
if debug then print("Differentiate-Exp-result: " + ExpressionDump.printExpStr(outDiffedExp) + "\n"); end if;
end differentiateExp;

protected function differentiateStatements
Expand Down Expand Up @@ -1230,7 +1196,7 @@ algorithm
then
fail();
end matchcontinue;
if debug then print("Differentiate-Exp-result: " + ExpressionDump.printExpStr(outDiffedExp) + "\n"); end if;
if debug then print("Differentiate-ExpCref-result: " + ExpressionDump.printExpStr(outDiffedExp) + "\n"); end if;
end differentiateCrefs;

public function createDiffedCrefName
Expand Down Expand Up @@ -1320,7 +1286,12 @@ function: differentiateCalls
input Integer maxIter;
output DAE.Exp outDiffedExp;
output DAE.FunctionTree outFunctionTree;
protected
constant Boolean debug = false;
algorithm
if debug then print("\nDifferentiate Exp-Call: "+ExpressionDump.printExpStr(inExp)+
" w.r.t. "+ComponentReference.printComponentRefStr(inDiffwrtCref)+"\n"); end if;

(outDiffedExp, outFunctionTree) :=
match(inExp, inDiffwrtCref, inInputData, inDiffType, inFunctionTree)
local
Expand Down Expand Up @@ -1397,11 +1368,7 @@ algorithm
// differentiate builtin calls with 1 argument
case (DAE.CALL(path=Absyn.IDENT(name),attr=DAE.CALL_ATTR(builtin=true),expLst={e}), _, _, _, _)
equation
//s1 = ExpressionDump.printExpStr(e);
//print("\nExp-CALL\n build-funcs "+ name + "(" + s1 + ")\n");
(res, funcs) = differentiateCallExp1Arg(name, e, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter);
//s1 = ExpressionDump.printExpStr(e);
//print("\nresults to exp: " + s1);
then (res, funcs);

// differentiate builtin calls with N arguments with match
Expand All @@ -1413,11 +1380,7 @@ algorithm
// differentiate builtin calls with N arguments as match
case (DAE.CALL(path=Absyn.IDENT(name),attr=(attr as DAE.CALL_ATTR(builtin=true)),expLst= (expl as (_::_::_))), _, _, _, _)
equation
//s1 = ExpressionDump.printExpStr(e);
//print("\nExp-CALL\n build-funcs "+ name + "(" + s1 + ")\n");
(res, funcs) = differentiateCallExpNArg(name, expl, attr, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter);
//s1 = ExpressionDump.printExpStr(e);
//print("\nresults to exp: " + s1);
then (res, funcs);

case (e as DAE.CALL(), _, _, _, _)
Expand All @@ -1433,7 +1396,7 @@ algorithm
then
fail();
*/
else
else
equation
true = Flags.isSet(Flags.FAILTRACE);
s1 = ExpressionDump.printExpStr(inExp);
Expand All @@ -1442,7 +1405,8 @@ algorithm
Debug.trace(serr);
then
fail();
end match;
end match;
if debug then print("Differentiate-ExpCall-result: " + ExpressionDump.printExpStr(outDiffedExp) + "\n"); end if;
end differentiateCalls;

protected function differentiateCallExp1Arg
Expand Down

0 comments on commit 808888c

Please sign in to comment.