Skip to content

Commit

Permalink
Avoid nested try/matchcontinue clauses
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel authored and OpenModelica-Hudson committed Jan 16, 2017
1 parent d82121d commit a6ef399
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
23 changes: 12 additions & 11 deletions Compiler/BackEnd/Differentiate.mo
Expand Up @@ -86,18 +86,19 @@ constant Integer defaultMaxIter = 20;
// =============================================================================

public function differentiateEquationTime
"Differentiates an equation with respect to time."
"Differentiates an equation with respect to time.
Returns NONE() if it was not possible to calculate a derivative."
input BackendDAE.Equation inEquation;
input BackendDAE.Variables inVariables;
input BackendDAE.Shared inShared;
output BackendDAE.Equation outEquation;
output BackendDAE.Shared outShared;
output Option<BackendDAE.Equation> outEquation;
output BackendDAE.Shared outShared = inShared;
protected
String msg;
DAE.ElementSource source;
DAE.FunctionTree funcs;
BackendDAE.DifferentiateInputData diffData;
BackendDAE.Equation eqn;
BackendDAE.Variables knvars;
DAE.ElementSource source;
DAE.FunctionTree funcs;
algorithm
try
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
Expand All @@ -106,16 +107,16 @@ algorithm
funcs := BackendDAEUtil.getFunctions(inShared);
knvars := BackendDAEUtil.getGlobalKnownVarsFromShared(inShared);
diffData := BackendDAE.DIFFINPUTDATA(NONE(), SOME(inVariables), SOME(knvars), SOME(inVariables), {}, {}, NONE());
(outEquation, funcs) := differentiateEquation(inEquation, DAE.crefTime, diffData, BackendDAE.DIFFERENTIATION_TIME(), funcs);
(eqn, funcs) := differentiateEquation(inEquation, DAE.crefTime, diffData, BackendDAE.DIFFERENTIATION_TIME(), funcs);
outEquation := SOME(eqn);
outShared := BackendDAEUtil.setSharedFunctionTree(inShared, funcs);
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
BackendDump.debugStrEqnStr("### Result of differentiation\n --> ", outEquation, "\n");
BackendDump.debugStrEqnStr("### Result of differentiation\n --> ", eqn, "\n");
end if;
else
msg := "\nDifferentiate.differentiateEquationTime failed for " + BackendDump.equationString(inEquation) + "\n\n";
source := BackendEquation.equationSource(inEquation);
Error.addSourceMessage(Error.INTERNAL_ERROR, {msg}, ElementSource.getElementSourceFileInfo(source));
fail();
Error.addSourceMessage(Error.INTERNAL_ERROR, {"\nDifferentiate.differentiateEquationTime failed for " + BackendDump.equationString(inEquation) + "\n\n"}, ElementSource.getElementSourceFileInfo(source));
outEquation := NONE();
end try;
end differentiateEquationTime;

Expand Down
51 changes: 24 additions & 27 deletions Compiler/BackEnd/IndexReduction.mo
Expand Up @@ -721,34 +721,31 @@ protected function differentiateEqnsLst1
input BackendDAE.Shared inShared;
output Option<tuple<Integer,Option<BackendDAE.Equation>,BackendDAE.Equation>> oEqTpl;
output BackendDAE.Shared oshared;
protected
BackendDAE.Equation eqn;
Option<BackendDAE.Equation> diffEqn;
algorithm
(oEqTpl, oshared) := matchcontinue (eqIdx,vars,eqns,inShared)
local
Integer e;
BackendDAE.Equation eqn,eqn_1;
list<Integer> es;
BackendDAE.Shared shared;
case (e,_,_,_)
equation
eqn = BackendEquation.equationNth1(eqns, e);
true = BackendEquation.isDifferentiated(eqn);
if Flags.isSet(Flags.BLT_DUMP) then
BackendDump.debugStrEqnStr("Skip already differentiated equation\n",eqn,"\n");
end if;
then (SOME((e,NONE(),eqn)),inShared);
case (e,_,_,_)
equation
eqn = BackendEquation.equationNth1(eqns, e);
//if Flags.isSet(Flags.BLT_DUMP) then print("differentiate equation " + intString(e) + " " + BackendDump.equationString(eqn) + "\n"); end if;
(eqn_1, shared) = Differentiate.differentiateEquationTime(eqn, vars, inShared);
//if Flags.isSet(Flags.BLT_DUMP) then print("differentiated equation " + intString(e) + " " + BackendDump.equationString(eqn_1) + "\n"); end if;
eqn = BackendEquation.markDifferentiated(eqn);
then
(SOME((e,SOME(eqn_1),eqn)),shared);
else
equation
then (NONE(),inShared);
end matchcontinue;
eqn := BackendEquation.equationNth1(eqns, eqIdx);

if BackendEquation.isDifferentiated(eqn) then
if Flags.isSet(Flags.BLT_DUMP) then
BackendDump.debugStrEqnStr("Skip already differentiated equation\n",eqn,"\n");
end if;
oEqTpl := SOME((eqIdx, NONE(), eqn));
oshared := inShared;
else
//if Flags.isSet(Flags.BLT_DUMP) then print("differentiate equation " + intString(eqIdx) + " " + BackendDump.equationString(eqn) + "\n"); end if;
(diffEqn, oshared) := Differentiate.differentiateEquationTime(eqn, vars, inShared);
//if Flags.isSet(Flags.BLT_DUMP) then print("differentiated equation " + intString(eqIdx) + " " + BackendDump.equationString(diffEqn) + "\n"); end if;
eqn := BackendEquation.markDifferentiated(eqn);

if isSome(diffEqn) then
oEqTpl := SOME((eqIdx, diffEqn, eqn));
else
oEqTpl := NONE();
oshared := inShared;
end if;
end if;
end differentiateEqnsLst1;

protected function replaceDifferentiatedEqns
Expand Down

0 comments on commit a6ef399

Please sign in to comment.