Skip to content

Commit

Permalink
Fix some tail recursion
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@21888 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Aug 16, 2014
1 parent 2c2f567 commit 4f8d37d
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 115 deletions.
71 changes: 47 additions & 24 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -1684,14 +1684,13 @@ algorithm
match(isyst,sharedChanged)
local
BackendDAE.StrongComponents comps;
Boolean b,b1,b2;
Boolean b;
BackendDAE.Shared shared;
BackendDAE.EqSystem syst;
case (syst as BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(comps=comps)),(shared, b1))
case (syst as BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(comps=comps)),(shared, b))
equation
(syst,shared,b2) = constantLinearSystem1(syst,shared,comps);
syst = constantLinearSystem2(b2,syst);
b = b1 or b2;
(syst,shared,b) = constantLinearSystem1(syst,shared,comps,b);
syst = constantLinearSystem2(b,syst);
then
(syst,(shared,b));
end match;
Expand Down Expand Up @@ -1728,17 +1727,51 @@ protected function constantLinearSystem1
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input BackendDAE.StrongComponents inComps;
input Boolean inRunMatching;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output Boolean runMatching;
algorithm
(osyst,oshared,runMatching):=
match (isyst,ishared,inComps,inRunMatching)
local
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
BackendDAE.StrongComponents comps;
BackendDAE.StrongComponent comp,comp1;
Boolean b,b1;
list<BackendDAE.Equation> eqn_lst;
list<BackendDAE.Var> var_lst;
list<Integer> eindex,vindx;
list<tuple<Integer, Integer, BackendDAE.Equation>> jac;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;

case (syst,shared,{},_)
then (syst,shared,inRunMatching);
case (syst,shared,comp::comps,runMatching)
equation
(syst,shared,b) = constantLinearSystemWork(syst,shared,comp);
(syst,shared,runMatching) = constantLinearSystem1(syst,shared,comps,b or runMatching);
then (syst,shared,runMatching);
end match;
end constantLinearSystem1;

protected function constantLinearSystemWork
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input BackendDAE.StrongComponent comp;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output Boolean outRunMatching;
algorithm
(osyst,oshared,outRunMatching):=
matchcontinue (isyst,ishared,inComps)
matchcontinue (isyst,ishared,comp)
local
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
BackendDAE.StrongComponents comps;
BackendDAE.StrongComponent comp,comp1;
BackendDAE.StrongComponent comp1;
Boolean b,b1;
list<BackendDAE.Equation> eqn_lst;
list<BackendDAE.Var> var_lst;
Expand All @@ -1747,29 +1780,19 @@ algorithm
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;

case (syst,shared,{})
then (syst,shared,false);
case (syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),shared,(BackendDAE.EQUATIONSYSTEM(eqns=eindex,vars=vindx,jac=BackendDAE.FULL_JACOBIAN(SOME(jac)),jacType=BackendDAE.JAC_CONSTANT()))::comps)
case (syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),shared,(BackendDAE.EQUATIONSYSTEM(eqns=eindex,vars=vindx,jac=BackendDAE.FULL_JACOBIAN(SOME(jac)),jacType=BackendDAE.JAC_CONSTANT())))
equation
eqn_lst = BackendEquation.getEqns(eindex,eqns);
var_lst = List.map1r(vindx, BackendVariable.getVarAt, vars);
(syst,shared) = solveLinearSystem(syst,shared,eqn_lst,eindex,var_lst,vindx,jac);
(syst,shared,_) = constantLinearSystem1(syst,shared,comps);
then
(syst,shared,true);
case (syst,shared,(BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=comp1))::comps)
then (syst,shared,true);
case (syst,shared,(BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=comp1)))
equation
(syst,shared,b) = constantLinearSystem1(syst,shared,{comp1});
(syst,shared,b1) = constantLinearSystem1(syst,shared,comps);
then
(syst,shared,b1 or b);
case (syst,shared,_::comps)
equation
(syst,shared,b) = constantLinearSystem1(syst,shared,comps);
then
(syst,shared,b);
(syst,shared,b) = constantLinearSystemWork(syst,shared,comp1);
then (syst,shared,b);
else (isyst,ishared,false);
end matchcontinue;
end constantLinearSystem1;
end constantLinearSystemWork;

protected function solveLinearSystem
"function constantLinearSystem1"
Expand Down
190 changes: 120 additions & 70 deletions Compiler/BackEnd/SimCodeUtil.mo

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Compiler/BackEnd/SynchronousFeatures.mo
Expand Up @@ -428,7 +428,7 @@ protected function getVariableLists2
output list<DAE.ComponentRef> outContinuousTimeVars;
output list<DAE.ComponentRef> outClockedVars;
algorithm
(outContinuousTimeVars, outClockedVars) := matchcontinue(inEqnLst)
(outContinuousTimeVars, outClockedVars) := match (inEqnLst)
local
DAE.Exp exp;
list<DAE.ComponentRef> continuousTimeVars1, clockedVars1;
Expand Down Expand Up @@ -531,7 +531,7 @@ algorithm
(_, (continuousTimeVars1, clockedVars1)) = BackendEquation.traverseBackendDAEExpsEqn(curr, getVariableLists, ({}, {}));
(continuousTimeVars2, clockedVars2) = getVariableLists2(rest);
then (listAppend(continuousTimeVars1, continuousTimeVars2), listAppend(clockedVars1, clockedVars2));
end matchcontinue;
end match;
end getVariableLists2;

protected function getPartitionKind
Expand Down Expand Up @@ -582,4 +582,4 @@ algorithm
end matchcontinue;
end filterVariables;

end SynchronousFeatures;
end SynchronousFeatures;
55 changes: 37 additions & 18 deletions Compiler/BackEnd/Tearing.mo
Expand Up @@ -196,7 +196,7 @@ protected function traverseComponents "author: Frenkel TUD 2012-05"
output BackendDAE.StrongComponents oComps;
output Boolean outRunMatching;
algorithm
(oComps, outRunMatching) := matchcontinue (inComps, isyst, ishared, inMethod, iAcc, iRunMatching)
(oComps, outRunMatching) := match (inComps, isyst, ishared, inMethod, iAcc, iRunMatching)
local
list<Integer> eindex, vindx;
Boolean b, b1;
Expand All @@ -208,7 +208,32 @@ algorithm
case ({}, _, _, _, _, _)
then (listReverse(iAcc), iRunMatching);

case ((BackendDAE.EQUATIONSYSTEM(eqns=eindex, vars=vindx, jac=BackendDAE.FULL_JACOBIAN(ojac), jacType=jacType))::comps, _, _, _, _, _) equation
case (comp::comps, _, _, _, _, _)
equation
(comp, b1) = traverseComponents1(comp, isyst, ishared, inMethod);
(acc, b1) = traverseComponents(comps, isyst, ishared, inMethod, comp::iAcc, b1 or iRunMatching);
then (acc, b1);
end match;
end traverseComponents;

protected function traverseComponents1 "author: Frenkel TUD 2012-05"
input BackendDAE.StrongComponent inComp;
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input TearingMethod inMethod;
output BackendDAE.StrongComponent oComp;
output Boolean outRunMatching;
algorithm
(oComp, outRunMatching) := matchcontinue (inComp, isyst, ishared, inMethod)
local
list<Integer> eindex, vindx;
Boolean b, b1;
BackendDAE.StrongComponents comps, acc;
BackendDAE.StrongComponent comp, comp1;
Option<list<tuple<Integer, Integer, BackendDAE.Equation>>> ojac;
BackendDAE.JacobianType jacType;

case ((BackendDAE.EQUATIONSYSTEM(eqns=eindex, vars=vindx, jac=BackendDAE.FULL_JACOBIAN(ojac), jacType=jacType)), _, _, _) equation
equality(jacType = BackendDAE.JAC_TIME_VARYING());
Debug.fcall(Flags.TEARING_DUMP, print, "\nCase linear in traverseComponents\nUse Flag '+d=tearingdumpV' for more details\n\n");
true = Flags.isSet(Flags.LINEAR_TEARING);
Expand All @@ -217,42 +242,36 @@ algorithm
Debug.fcall(Flags.TEARING_DUMP, print, "Flag 'doLinearTearing' is set\n\n");
Debug.fcall(Flags.TEARING_DUMPVERBOSE, print, "Jacobian:\n" +& BackendDump.dumpJacobianStr(ojac) +& "\n\n");
(comp1, true) = callTearingMethod(inMethod, isyst, ishared, eindex, vindx, ojac, jacType);
(acc, b1) = traverseComponents(comps, isyst, ishared, inMethod, comp1::iAcc, true);
then (acc, b1);
then (comp1, true);

// tearing of non-linear systems
case ((BackendDAE.EQUATIONSYSTEM(eqns=eindex, vars=vindx, jac=BackendDAE.FULL_JACOBIAN(ojac), jacType=jacType))::comps, _, _, _, _, _) equation
case ((BackendDAE.EQUATIONSYSTEM(eqns=eindex, vars=vindx, jac=BackendDAE.FULL_JACOBIAN(ojac), jacType=jacType)), _, _, _) equation
failure(equality(jacType = BackendDAE.JAC_TIME_VARYING()));
Debug.fcall(Flags.TEARING_DUMP, print, "\nCase non-linear in traverseComponents\nUse Flag '+d=tearingdumpV' for more details\n\n");
Debug.fcall(Flags.TEARING_DUMPVERBOSE, print, "Jacobian:\n" +& BackendDump.dumpJacobianStr(ojac) +& "\n\n");
(comp1, true) = callTearingMethod(inMethod, isyst, ishared, eindex, vindx, ojac, jacType);
(acc, b1) = traverseComponents(comps, isyst, ishared, inMethod, comp1::iAcc, true);
then (acc, b1);
then (comp1, true);

// only continues part of a mixed system
case ((BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=comp1, disc_eqns=eindex, disc_vars=vindx))::comps, _, _, _, _, _) equation
case ((BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=comp1, disc_eqns=eindex, disc_vars=vindx)), _, _, _) equation
Debug.fcall(Flags.TEARING_DUMP, print, "\nCase mixed in traverseComponents\nUse '+d=tearingdumpV' for more details\n\n");
false = Flags.isSet(Flags.MIXED_TEARING);
Debug.fcall(Flags.TEARING_DUMP, print, "Flag 'MixedTearing' is not set\n(disabled by user)\n\n");
(comp1::{}, true) = traverseComponents({comp1}, isyst, ishared, inMethod, {}, false);
(acc, b1) = traverseComponents(comps, isyst, ishared, inMethod, BackendDAE.MIXEDEQUATIONSYSTEM(comp1, eindex, vindx)::iAcc, true);
then (acc, b1);
(comp1, true) = traverseComponents1(comp1, isyst, ishared, inMethod);
then (BackendDAE.MIXEDEQUATIONSYSTEM(comp1, eindex, vindx), true);

// mixed and continues part
case ((comp as BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=comp1, disc_eqns=eindex, disc_vars=vindx))::comps, _, _, _, _, _) equation
case ((comp as BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=comp1, disc_eqns=eindex, disc_vars=vindx)), _, _, _) equation
true = Flags.isSet(Flags.MIXED_TEARING);
Debug.fcall(Flags.TEARING_DUMP, print, "Flag 'MixedTearing' is set\n(enabled by default)\n\n");
(eindex, vindx) = BackendDAETransform.getEquationAndSolvedVarIndxes(comp);
(comp1, true) = callTearingMethod(inMethod, isyst, ishared, eindex, vindx, NONE(), BackendDAE.JAC_NO_ANALYTIC());
(acc, b1) = traverseComponents(comps, isyst, ishared, inMethod, comp1::iAcc, true);
then (acc, b1);
then (comp1, true);

// no component for tearing
case (comp::comps, _, _, _, _, _) equation
(acc, b) = traverseComponents(comps, isyst, ishared, inMethod, comp::iAcc, iRunMatching);
then (acc, b);
else (inComp, false);
end matchcontinue;
end traverseComponents;
end traverseComponents1;



Expand Down
9 changes: 9 additions & 0 deletions Compiler/Util/List.mo
Expand Up @@ -460,6 +460,15 @@ algorithm
end matchcontinue;
end isPrefixOnTrue;

public function prepend
"The same as the builtin cons operator, but RML does not want to pass it as a pointer or name other functions 'cons'."
input ElementType inElement;
input list<ElementType> inList;
output list<ElementType> outList;
algorithm
outList := inElement :: inList;
end prepend;

public function consr
"The same as the builtin cons operator, but with the order of the arguments
swapped."
Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/c/meta/meta_modelica_builtin_boxptr.h
Expand Up @@ -129,6 +129,7 @@ boxptr_unOpThreadData(boxptr_getGlobalRoot,(void*),mmc_unbox_integer,nobox_getGl
boxptr_unOp(boxptr_valueConstructor,mmc_mk_icon,(void*),valueConstructor)
boxptr_wrapper1Arg(boxptr_listFirst,MMC_CAR)
boxptr_wrapper1Arg(boxptr_listRest,MMC_CDR)
boxptr_wrapper2Args(boxptr_cons,mmc_mk_cons)

#undef boxptr_unOp
#undef boxptr_unOpThreadData
Expand Down
2 changes: 2 additions & 0 deletions SimulationRuntime/c/meta/meta_modelica_builtin_boxvar.h
Expand Up @@ -19,6 +19,8 @@ static const MMC_DEFSTRUCTLIT(boxvar_lit_boolNot,2,0) {(modelica_metatype)boxptr
#define boxvar_boolNot MMC_REFSTRUCTLIT(boxvar_lit_boolNot)
static const MMC_DEFSTRUCTLIT(boxvar_lit_boolOr,2,0) {(modelica_metatype)boxptr_boolOr,0}};
#define boxvar_boolOr MMC_REFSTRUCTLIT(boxvar_lit_boolOr)
static const MMC_DEFSTRUCTLIT(boxvar_lit_cons,2,0) {(modelica_metatype)boxptr_cons,0}};
#define boxvar_cons MMC_REFSTRUCTLIT(boxvar_lit_cons)
static const MMC_DEFSTRUCTLIT(boxvar_lit_equality,2,0) {(modelica_metatype)boxptr_equality,0}};
#define boxvar_equality MMC_REFSTRUCTLIT(boxvar_lit_equality)
static const MMC_DEFSTRUCTLIT(boxvar_lit_getGlobalRoot,2,0) {(modelica_metatype)boxptr_getGlobalRoot,0}};
Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/c/meta/meta_modelica_gen_boxvar.py
Expand Up @@ -11,6 +11,7 @@
'boolEq',
'boolNot',
'boolOr',
'cons',
'equality',
'getGlobalRoot',
'intAbs',
Expand Down

0 comments on commit 4f8d37d

Please sign in to comment.