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

Commit a6ef399

Browse files
lochelOpenModelica-Hudson
authored andcommitted
Avoid nested try/matchcontinue clauses
1 parent d82121d commit a6ef399

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

Compiler/BackEnd/Differentiate.mo

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,19 @@ constant Integer defaultMaxIter = 20;
8686
// =============================================================================
8787

8888
public function differentiateEquationTime
89-
"Differentiates an equation with respect to time."
89+
"Differentiates an equation with respect to time.
90+
Returns NONE() if it was not possible to calculate a derivative."
9091
input BackendDAE.Equation inEquation;
9192
input BackendDAE.Variables inVariables;
9293
input BackendDAE.Shared inShared;
93-
output BackendDAE.Equation outEquation;
94-
output BackendDAE.Shared outShared;
94+
output Option<BackendDAE.Equation> outEquation;
95+
output BackendDAE.Shared outShared = inShared;
9596
protected
96-
String msg;
97-
DAE.ElementSource source;
98-
DAE.FunctionTree funcs;
9997
BackendDAE.DifferentiateInputData diffData;
98+
BackendDAE.Equation eqn;
10099
BackendDAE.Variables knvars;
100+
DAE.ElementSource source;
101+
DAE.FunctionTree funcs;
101102
algorithm
102103
try
103104
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
@@ -106,16 +107,16 @@ algorithm
106107
funcs := BackendDAEUtil.getFunctions(inShared);
107108
knvars := BackendDAEUtil.getGlobalKnownVarsFromShared(inShared);
108109
diffData := BackendDAE.DIFFINPUTDATA(NONE(), SOME(inVariables), SOME(knvars), SOME(inVariables), {}, {}, NONE());
109-
(outEquation, funcs) := differentiateEquation(inEquation, DAE.crefTime, diffData, BackendDAE.DIFFERENTIATION_TIME(), funcs);
110+
(eqn, funcs) := differentiateEquation(inEquation, DAE.crefTime, diffData, BackendDAE.DIFFERENTIATION_TIME(), funcs);
111+
outEquation := SOME(eqn);
110112
outShared := BackendDAEUtil.setSharedFunctionTree(inShared, funcs);
111113
if Flags.isSet(Flags.DEBUG_DIFFERENTIATION) then
112-
BackendDump.debugStrEqnStr("### Result of differentiation\n --> ", outEquation, "\n");
114+
BackendDump.debugStrEqnStr("### Result of differentiation\n --> ", eqn, "\n");
113115
end if;
114116
else
115-
msg := "\nDifferentiate.differentiateEquationTime failed for " + BackendDump.equationString(inEquation) + "\n\n";
116117
source := BackendEquation.equationSource(inEquation);
117-
Error.addSourceMessage(Error.INTERNAL_ERROR, {msg}, ElementSource.getElementSourceFileInfo(source));
118-
fail();
118+
Error.addSourceMessage(Error.INTERNAL_ERROR, {"\nDifferentiate.differentiateEquationTime failed for " + BackendDump.equationString(inEquation) + "\n\n"}, ElementSource.getElementSourceFileInfo(source));
119+
outEquation := NONE();
119120
end try;
120121
end differentiateEquationTime;
121122

Compiler/BackEnd/IndexReduction.mo

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -721,34 +721,31 @@ protected function differentiateEqnsLst1
721721
input BackendDAE.Shared inShared;
722722
output Option<tuple<Integer,Option<BackendDAE.Equation>,BackendDAE.Equation>> oEqTpl;
723723
output BackendDAE.Shared oshared;
724+
protected
725+
BackendDAE.Equation eqn;
726+
Option<BackendDAE.Equation> diffEqn;
724727
algorithm
725-
(oEqTpl, oshared) := matchcontinue (eqIdx,vars,eqns,inShared)
726-
local
727-
Integer e;
728-
BackendDAE.Equation eqn,eqn_1;
729-
list<Integer> es;
730-
BackendDAE.Shared shared;
731-
case (e,_,_,_)
732-
equation
733-
eqn = BackendEquation.equationNth1(eqns, e);
734-
true = BackendEquation.isDifferentiated(eqn);
735-
if Flags.isSet(Flags.BLT_DUMP) then
736-
BackendDump.debugStrEqnStr("Skip already differentiated equation\n",eqn,"\n");
737-
end if;
738-
then (SOME((e,NONE(),eqn)),inShared);
739-
case (e,_,_,_)
740-
equation
741-
eqn = BackendEquation.equationNth1(eqns, e);
742-
//if Flags.isSet(Flags.BLT_DUMP) then print("differentiate equation " + intString(e) + " " + BackendDump.equationString(eqn) + "\n"); end if;
743-
(eqn_1, shared) = Differentiate.differentiateEquationTime(eqn, vars, inShared);
744-
//if Flags.isSet(Flags.BLT_DUMP) then print("differentiated equation " + intString(e) + " " + BackendDump.equationString(eqn_1) + "\n"); end if;
745-
eqn = BackendEquation.markDifferentiated(eqn);
746-
then
747-
(SOME((e,SOME(eqn_1),eqn)),shared);
748-
else
749-
equation
750-
then (NONE(),inShared);
751-
end matchcontinue;
728+
eqn := BackendEquation.equationNth1(eqns, eqIdx);
729+
730+
if BackendEquation.isDifferentiated(eqn) then
731+
if Flags.isSet(Flags.BLT_DUMP) then
732+
BackendDump.debugStrEqnStr("Skip already differentiated equation\n",eqn,"\n");
733+
end if;
734+
oEqTpl := SOME((eqIdx, NONE(), eqn));
735+
oshared := inShared;
736+
else
737+
//if Flags.isSet(Flags.BLT_DUMP) then print("differentiate equation " + intString(eqIdx) + " " + BackendDump.equationString(eqn) + "\n"); end if;
738+
(diffEqn, oshared) := Differentiate.differentiateEquationTime(eqn, vars, inShared);
739+
//if Flags.isSet(Flags.BLT_DUMP) then print("differentiated equation " + intString(eqIdx) + " " + BackendDump.equationString(diffEqn) + "\n"); end if;
740+
eqn := BackendEquation.markDifferentiated(eqn);
741+
742+
if isSome(diffEqn) then
743+
oEqTpl := SOME((eqIdx, diffEqn, eqn));
744+
else
745+
oEqTpl := NONE();
746+
oshared := inShared;
747+
end if;
748+
end if;
752749
end differentiateEqnsLst1;
753750

754751
protected function replaceDifferentiatedEqns

0 commit comments

Comments
 (0)