diff --git a/Compiler/BackEnd/BackendDAE.mo b/Compiler/BackEnd/BackendDAE.mo index 4cdf243908c..f0d4626f213 100644 --- a/Compiler/BackEnd/BackendDAE.mo +++ b/Compiler/BackEnd/BackendDAE.mo @@ -273,8 +273,8 @@ end EqSystem; uniontype Matching record NO_MATCHING "matching has not yet been performed" end NO_MATCHING; record MATCHING "not yet used" - array ass1; - array ass2; + array ass1 "ass[varindx]=eqnindx"; + array ass2 "ass[eqnindx]=varindx"; StrongComponents comps; end MATCHING; end Matching; diff --git a/Compiler/BackEnd/BackendDAEOptimize.mo b/Compiler/BackEnd/BackendDAEOptimize.mo index e986fbce0e2..a0c9f02a8ef 100644 --- a/Compiler/BackEnd/BackendDAEOptimize.mo +++ b/Compiler/BackEnd/BackendDAEOptimize.mo @@ -5018,7 +5018,7 @@ algorithm dlow_1 = BackendDAE.DAE(BackendDAE.EQSYSTEM(vars_1,eqns_2,NONE(),NONE(),BackendDAE.NO_MATCHING())::{},shared); dlow1_1 = BackendDAE.DAE(BackendDAE.EQSYSTEM(ordvars1,eqns1_1,NONE(),NONE(),BackendDAE.NO_MATCHING())::{},shared); // try causalisation - (dlow_2 as BackendDAE.DAE(eqs=BackendDAE.EQSYSTEM(m=SOME(m_2),mT=SOME(mT_2),matching=BackendDAE.MATCHING(v1_1,v2_1,comps))::{})) = BackendDAEUtil.transformBackendDAE(dlow_1,DAEUtil.avlTreeNew(),SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())),NONE()); + (dlow_2 as BackendDAE.DAE(eqs=BackendDAE.EQSYSTEM(m=SOME(m_2),mT=SOME(mT_2),matching=BackendDAE.MATCHING(v1_1,v2_1,comps))::{})) = BackendDAEUtil.transformBackendDAE(dlow_1,DAEUtil.avlTreeNew(),SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())),NONE(),NONE()); comps_1 = List.map(comps,getEqnIndxFromComp); // check strongComponents and split it into two lists: len(comp)==1 and len(comp)>1 (morecomps,onecomp) = splitComps(comps_1); @@ -5444,7 +5444,7 @@ algorithm //Debug.fcall(Flags.EXEC_STAT,print, "*** analytical Jacobians -> removed simply equations: " +& realString(clock()) +& "\n" ); // figure out new matching and the strong components - (dlow as BackendDAE.DAE(eqs={BackendDAE.EQSYSTEM(matching=matching as BackendDAE.MATCHING(comps=comps1))})) = BackendDAEUtil.transformBackendDAE(dlow,functionTree,SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())),NONE()); + (dlow as BackendDAE.DAE(eqs={BackendDAE.EQSYSTEM(matching=matching as BackendDAE.MATCHING(comps=comps1))})) = BackendDAEUtil.transformBackendDAE(dlow,functionTree,SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())),NONE(),NONE()); Debug.fcall(Flags.JAC_DUMP2, BackendDump.bltdump, ("jacdump2",dlow)); Debug.fcall(Flags.JAC_DUMP2, BackendDump.dumpFullMatching, matching); //Debug.fcall(Flags.EXEC_STAT,print, "*** analytical Jacobians -> performed matching and sorting: " +& realString(clock()) +& "\n" ); diff --git a/Compiler/BackEnd/BackendDAETransform.mo b/Compiler/BackEnd/BackendDAETransform.mo index a0c1bfd7cd6..c339fe7a201 100644 --- a/Compiler/BackEnd/BackendDAETransform.mo +++ b/Compiler/BackEnd/BackendDAETransform.mo @@ -107,6 +107,7 @@ public function matchingAlgorithm output BackendDAE.EqSystem osyst; output BackendDAE.Shared oshared; partial function daeHandlerFunc + input list eqns; input BackendDAE.DAEHandlerJop inJop; input BackendDAE.EqSystem syst; input BackendDAE.Shared shared; @@ -116,6 +117,7 @@ public function matchingAlgorithm input list> inDerivedAlgs; input list> inDerivedMultiEqn; input BackendDAE.DAEHandlerArg inArg; + output list changedEqns; output BackendDAE.EqSystem osyst; output BackendDAE.Shared oshared; output BackendDAE.Assignments outAssignments1; @@ -164,9 +166,9 @@ algorithm assign1 = assignmentsCreate(nvars, memsize, 0); assign2 = assignmentsCreate(nvars, memsize, 0); arg = emptyDAEHandlerArg(); - (syst,shared,assign1, assign2,_,_,arg1) = daeHandler(BackendDAE.STARTSTEP(),syst,ishared,inFunctions, assign1, assign2,{},{},arg); - (ass1,ass2,syst as BackendDAE.EQSYSTEM(vars,eqs,SOME(m),SOME(mt),_),shared,_,_,arg2) = matchingAlgorithm2(syst,shared,nvars, neqns, 1, assign1, assign2, inMatchingOptions, daeHandler, arg1, inFunctions, {}, {}); - (syst,shared,ass1,ass2,_,_,_) = daeHandler(BackendDAE.ENDSTEP(),syst,shared,inFunctions,ass1,ass2,{},{},arg2); + (_,syst,shared,assign1, assign2,_,_,arg1) = daeHandler({},BackendDAE.STARTSTEP(),syst,ishared,inFunctions, assign1, assign2,{},{},arg); + (ass1,ass2,syst,shared,_,_,arg2) = matchingAlgorithm2(syst,shared,nvars, neqns, 1, assign1, assign2, inMatchingOptions, daeHandler, arg1, inFunctions, {}, {}); + (_,syst as BackendDAE.EQSYSTEM(vars,eqs,SOME(m),SOME(mt),_),shared,ass1,ass2,_,_,_) = daeHandler({},BackendDAE.ENDSTEP(),syst,shared,inFunctions,ass1,ass2,{},{},arg2); vec1 = assignmentsVector(ass1); vec2 = assignmentsVector(ass2); then @@ -421,6 +423,7 @@ public function matchingAlgorithm2 output list> outDerivedMultiEqn; output BackendDAE.DAEHandlerArg outArg; partial function daeHandlerFunc + input list eqns; input BackendDAE.DAEHandlerJop inJop; input BackendDAE.EqSystem syst; input BackendDAE.Shared shared; @@ -430,6 +433,7 @@ public function matchingAlgorithm2 input list> inDerivedAlgs; input list> inDerivedMultiEqn; input BackendDAE.DAEHandlerArg inArg; + output list changedEqns; output BackendDAE.EqSystem osyst; output BackendDAE.Shared oshared; output BackendDAE.Assignments outAssignments1; @@ -449,7 +453,7 @@ algorithm BackendDAE.MatchingOptions match_opts; BackendDAE.EquationArray eqns; BackendDAE.EquationConstraints eq_cons; - list eqn_lst,var_lst; + list eqn_lst,var_lst,meqns; String eqn_str,var_str; list> derivedAlgs,derivedAlgs1; list> derivedMultiEqn,derivedMultiEqn1; @@ -478,7 +482,8 @@ algorithm case (_,_,_,_,_,_,_,match_opts as (BackendDAE.INDEX_REDUCTION(),eq_cons),_,_,_,_,_) equation - (syst,shared,ass1_1,ass2_1,derivedAlgs,derivedMultiEqn,arg) = daeHandler(BackendDAE.REDUCE_INDEX(),isyst,ishared,inFunctions,ass1,ass2,inDerivedAlgs,inDerivedMultiEqn,inArg) + meqns = BackendDAEEXT.getMarkedEqns(); + (_,syst,shared,ass1_1,ass2_1,derivedAlgs,derivedMultiEqn,arg) = daeHandler(meqns,BackendDAE.REDUCE_INDEX(),isyst,ishared,inFunctions,ass1,ass2,inDerivedAlgs,inDerivedMultiEqn,inArg) "path_found failed, Try index reduction using dummy derivatives. When a constraint exist between states and index reduction is needed the dummy derivative will select one of the states as a dummy state @@ -2180,6 +2185,7 @@ public function reduceIndexDynamicStateSelection "function: reduceIndexDynamicStateSelection author: Frenkel TUD 2011-05 index reduction by using dynamic state selection." + input list eqns; input BackendDAE.DAEHandlerJop inJop; input BackendDAE.EqSystem isyst; input BackendDAE.Shared ishared; @@ -2189,6 +2195,7 @@ public function reduceIndexDynamicStateSelection input list> inDerivedAlgs; input list> inDerivedMultiEqn; input BackendDAE.DAEHandlerArg inArg; + output list changedEqns; output BackendDAE.EqSystem osyst; output BackendDAE.Shared oshared; output BackendDAE.Assignments outAssignments1; @@ -2197,10 +2204,10 @@ public function reduceIndexDynamicStateSelection output list> outDerivedMultiEqn; output BackendDAE.DAEHandlerArg outArg; algorithm - (osyst,oshared,outAssignments1,outAssignments2,outDerivedAlgs,outDerivedMultiEqn,outArg):= - matchcontinue (inJop,isyst,ishared,inFunctions,inAssignments1,inAssignments2,inDerivedAlgs,inDerivedMultiEqn,inArg) + (changedEqns,osyst,oshared,outAssignments1,outAssignments2,outDerivedAlgs,outDerivedMultiEqn,outArg):= + matchcontinue (eqns,inJop,isyst,ishared,inFunctions,inAssignments1,inAssignments2,inDerivedAlgs,inDerivedMultiEqn,inArg) local - list eqns,diff_eqns,eqns_1,deqns,reqns,changedeqns,eqnsindxs; + list diff_eqns,eqns_1,deqns,reqns,changedeqns,eqnsindxs; BackendDAE.BackendDAE dae; BackendDAE.IncidenceMatrix m; BackendDAE.IncidenceMatrixT mt; @@ -2227,9 +2234,8 @@ algorithm BackendDAE.EqSystem syst; BackendDAE.Shared shared; - case (BackendDAE.REDUCE_INDEX(),syst as BackendDAE.EQSYSTEM(m=SOME(m),mT=SOME(mt)),shared as BackendDAE.SHARED(arrayEqs=ae),inFunctions,ass1,ass2,derivedAlgs,derivedMultiEqn,(so,orgEqnsLst)) + case (eqns,BackendDAE.REDUCE_INDEX(),syst as BackendDAE.EQSYSTEM(m=SOME(m),mT=SOME(mt)),shared as BackendDAE.SHARED(arrayEqs=ae),inFunctions,ass1,ass2,derivedAlgs,derivedMultiEqn,(so,orgEqnsLst)) equation - eqns = BackendDAEEXT.getMarkedEqns(); diff_eqns = BackendDAEEXT.getDifferentiatedEqns(); eqns_1 = List.setDifferenceOnTrue(eqns, diff_eqns, intEq); true = intEq(listLength(eqns_1),0); @@ -2247,9 +2253,8 @@ algorithm Debug.fcall(Flags.BLT_DUMP, dumpEqnsX, (orgEqnsLst,ae)); then fail(); - case (BackendDAE.REDUCE_INDEX(),syst,shared,inFunctions,ass1,ass2,derivedAlgs,derivedMultiEqn,(so,orgEqnsLst)) + case (eqns,BackendDAE.REDUCE_INDEX(),syst,shared,inFunctions,ass1,ass2,derivedAlgs,derivedMultiEqn,(so,orgEqnsLst)) equation - eqns = BackendDAEEXT.getMarkedEqns(); diff_eqns = BackendDAEEXT.getDifferentiatedEqns(); eqns_1 = List.setDifferenceOnTrue(eqns, diff_eqns, intEq); Debug.fcall(Flags.BLT_DUMP, print, "Reduce Index\nmarked equations: "); @@ -2260,18 +2265,18 @@ algorithm smeqs = BackendDump.dumpMarkedEqns(syst, eqns_1); Debug.fcall(Flags.BLT_DUMP, print, smeqs); true = intGt(listLength(eqns),0); - (syst,shared,derivedAlgs1,derivedMultiEqn1,so1,orgEqnsLst2) = differentiateEqnsDynamicState(syst,shared,eqns_1,inFunctions,derivedAlgs,derivedMultiEqn,so,orgEqnsLst); + (syst,shared,derivedAlgs1,derivedMultiEqn1,so1,orgEqnsLst2,changedeqns) = differentiateEqnsDynamicState(syst,shared,eqns_1,inFunctions,derivedAlgs,derivedMultiEqn,so,orgEqnsLst,{}); then - (syst,shared,ass1,ass2,derivedAlgs1,derivedMultiEqn1,(so1,orgEqnsLst2)); + (changedeqns,syst,shared,ass1,ass2,derivedAlgs1,derivedMultiEqn1,(so1,orgEqnsLst2)); - case (BackendDAE.STARTSTEP(),syst,shared,inFunctions,ass1,ass2,derivedAlgs,derivedMultiEqn,(so,orgEqnsLst)) + case (eqns,BackendDAE.STARTSTEP(),syst,shared,inFunctions,ass1,ass2,derivedAlgs,derivedMultiEqn,(so,orgEqnsLst)) equation Debug.fcall(Flags.BLT_DUMP, print, "Reduce Index Startstep\n"); ((so,_)) = BackendEquation.traverseBackendDAEEqns(BackendEquation.daeEqns(syst),traverseStateOrderFinder,(so,BackendVariable.daeVars(syst))); Debug.fcall(Flags.BLT_DUMP, dumpStateOrder, so); - then (syst,shared,ass1,ass2,derivedAlgs,derivedMultiEqn,(so,orgEqnsLst)); + then ({},syst,shared,ass1,ass2,derivedAlgs,derivedMultiEqn,(so,orgEqnsLst)); - case (BackendDAE.ENDSTEP(),syst as BackendDAE.EQSYSTEM(m=SOME(m),mT=SOME(mt)),shared as BackendDAE.SHARED(arrayEqs=ae),inFunctions,ass1,ass2,derivedAlgs,derivedMultiEqn,(so,orgEqnsLst)) + case (eqns,BackendDAE.ENDSTEP(),syst as BackendDAE.EQSYSTEM(m=SOME(m),mT=SOME(mt)),shared as BackendDAE.SHARED(arrayEqs=ae),inFunctions,ass1,ass2,derivedAlgs,derivedMultiEqn,(so,orgEqnsLst)) equation vec1 = assignmentsVector(ass1); vec2 = assignmentsVector(ass2); @@ -2294,11 +2299,10 @@ algorithm vec2 = assignmentsVector(ass2); syst = BackendDAEUtil.setEqSystemMatching(syst,BackendDAE.MATCHING(vec1,vec2,{})); Debug.fcall(Flags.BLT_DUMP, BackendDump.bltdump, ("new DAE",BackendDAE.DAE({syst},shared))); - then (syst,shared,ass1,ass2,derivedAlgs,derivedMultiEqn,(so,orgEqnsLst)); + then ({},syst,shared,ass1,ass2,derivedAlgs,derivedMultiEqn,(so,orgEqnsLst)); - case (BackendDAE.REDUCE_INDEX(),syst,shared,_,_,_,_,_,_) + case (eqns,BackendDAE.REDUCE_INDEX(),syst,shared,_,_,_,_,_,_) equation - eqns = BackendDAEEXT.getMarkedEqns(); diff_eqns = BackendDAEEXT.getDifferentiatedEqns(); eqns_1 = List.setDifferenceOnTrue(eqns, diff_eqns, intEq); es = List.map(eqns_1, intString); @@ -2461,29 +2465,30 @@ protected function differentiateEqnsDynamicState author: Frenkel TUD 2011-05 differentiates the constraint equations for dynamic state selection method." - input BackendDAE.EqSystem syst; - input BackendDAE.Shared shared; + input BackendDAE.EqSystem isyst; + input BackendDAE.Shared ishared; input list inIntegerLst6; input DAE.FunctionTree inFunctions; input list> inDerivedAlgs; input list> inDerivedMultiEqn; input BackendDAE.StateOrder inStateOrd; input BackendDAE.ConstraintEquations inOrgEqnsLst; + input list inchangedEqns; output BackendDAE.EqSystem osyst; output BackendDAE.Shared oshared; output list> outDerivedAlgs; output list> outDerivedMultiEqn; output BackendDAE.StateOrder outStateOrd; output BackendDAE.ConstraintEquations outOrgEqnsLst; + output list outchangedEqns; algorithm - (osyst,oshared,outDerivedAlgs,outDerivedMultiEqn,outStateOrd,outOrgEqnsLst):= - matchcontinue (syst,shared,inIntegerLst6,inFunctions,inDerivedAlgs,inDerivedMultiEqn,inStateOrd,inOrgEqnsLst) + (osyst,oshared,outDerivedAlgs,outDerivedMultiEqn,outStateOrd,outOrgEqnsLst,outchangedEqns):= + matchcontinue (isyst,ishared,inIntegerLst6,inFunctions,inDerivedAlgs,inDerivedMultiEqn,inStateOrd,inOrgEqnsLst,inchangedEqns) local - BackendDAE.BackendDAE dae,dae1,dae2; BackendDAE.Value e_1,e,e1,i,l,newvar,stateno,assarg,i1,sl,eqnss,eqnss1; BackendDAE.Equation eqn,eqn_1; BackendDAE.EquationArray eqns_1,eqns,seqns,ie; - list es,lst,slst,ilst,eqnslst,eqnslst1; + list es,lst,slst,ilst,eqnslst,eqnslst1,changedEqns; BackendDAE.Variables v,kv,ev,v1; BackendDAE.AliasVariables av; array ae,ae1; @@ -2510,8 +2515,18 @@ algorithm BackendDAE.Shared shared; BackendDAE.BackendDAEType btp; BackendDAE.Matching matching; - case (syst,shared,{},_,inDerivedAlgs,inDerivedMultiEqn,inStateOrd,inOrgEqnsLst) then (syst,shared,inDerivedAlgs,inDerivedMultiEqn,inStateOrd,inOrgEqnsLst); - case (syst as BackendDAE.EQSYSTEM(v,eqns,SOME(m),SOME(mt),matching),shared as BackendDAE.SHARED(kv,ev,av,ie,seqns,ae,al,complEqs,BackendDAE.EVENT_INFO(wclst,zc),eoc,btp),(e :: es),inFunctions,inDerivedAlgs,inDerivedMultiEqn,inStateOrd,inOrgEqnsLst) + case (_,_,{},_,_,_,_,_,_) then (isyst,ishared,inDerivedAlgs,inDerivedMultiEqn,inStateOrd,inOrgEqnsLst,inchangedEqns); + case (syst as BackendDAE.EQSYSTEM(v,eqns,SOME(m),SOME(mt),matching),shared as BackendDAE.SHARED(kv,ev,av,ie,seqns,ae,al,complEqs,BackendDAE.EVENT_INFO(wclst,zc),eoc,btp),(e :: es),_,_,_,_,_,_) + equation + e_1 = e - 1; + eqn = BackendDAEUtil.equationNth(eqns, e_1); + ev = BackendEquation.equationsLstVarsWithoutRelations({eqn},v,BackendDAEUtil.emptyVars()); + false = BackendVariable.hasContinousVar(BackendDAEUtil.varList(ev)); + BackendDAEEXT.markDifferentiated(e) "length gives index of new equation Mark equation as differentiated so it won\'t be differentiated again" ; + (syst,shared,derivedAlgs1,derivedMultiEqn1,so1,orgEqnsLst,changedEqns) = differentiateEqnsDynamicState(syst,shared, es, inFunctions,inDerivedAlgs,inDerivedMultiEqn,inStateOrd,inOrgEqnsLst,inchangedEqns); + then + (syst,shared,derivedAlgs1,derivedMultiEqn1,so1,orgEqnsLst,changedEqns); + case (syst as BackendDAE.EQSYSTEM(v,eqns,SOME(m),SOME(mt),matching),shared as BackendDAE.SHARED(kv,ev,av,ie,seqns,ae,al,complEqs,BackendDAE.EVENT_INFO(wclst,zc),eoc,btp),(e :: es),_,_,_,_,_,_) equation e_1 = e - 1; eqn = BackendDAEUtil.equationNth(eqns, e_1); @@ -2531,9 +2546,10 @@ algorithm shared = BackendDAE.SHARED(kv,ev,av,ie,seqns,ae1,al1,complEqs,BackendDAE.EVENT_INFO(wclst1,zc),eoc,btp); syst = BackendDAEUtil.updateIncidenceMatrix(syst, shared, eqnslst1); orgEqnsLst = addOrgEqn(inOrgEqnsLst,e,eqn,false); - (syst,shared,derivedAlgs1,derivedMultiEqn1,so1,orgEqnsLst) = differentiateEqnsDynamicState(syst,shared, es, inFunctions,derivedAlgs,derivedMultiEqn,so,orgEqnsLst); + changedEqns = listAppend(inchangedEqns,eqnslst1); + (syst,shared,derivedAlgs1,derivedMultiEqn1,so1,orgEqnsLst,changedEqns) = differentiateEqnsDynamicState(syst,shared, es, inFunctions,derivedAlgs,derivedMultiEqn,so,orgEqnsLst,changedEqns); then - (syst,shared,derivedAlgs1,derivedMultiEqn1,so1,orgEqnsLst); + (syst,shared,derivedAlgs1,derivedMultiEqn1,so1,orgEqnsLst,changedEqns); else equation Error.addMessage(Error.INTERNAL_ERROR, {"BackendDAETransform.differentiateEqnsDynamicState failed!"}); @@ -4705,6 +4721,7 @@ public function reduceIndexDummyDer inputs: (BackendDAE, BackendDAE.IncidenceMatrix, BackendDAE.IncidenceMatrixT, int /* number of vars */, int /* number of eqns */, int /* i */) outputs: (BackendDAE, BackendDAE.IncidenceMatrix, IncidenceMatrixT)" + input list eqns; input BackendDAE.DAEHandlerJop inJop; input BackendDAE.EqSystem isyst; input BackendDAE.Shared ishared; @@ -4714,6 +4731,7 @@ public function reduceIndexDummyDer input list> inDerivedAlgs; input list> inDerivedMultiEqn; input BackendDAE.DAEHandlerArg inArg; + output list changedEqns; output BackendDAE.EqSystem osyst; output BackendDAE.Shared oshared; output BackendDAE.Assignments outAssignments1; @@ -4722,10 +4740,10 @@ public function reduceIndexDummyDer output list> outDerivedMultiEqn; output BackendDAE.DAEHandlerArg outArg; algorithm - (osyst,oshared,outAssignments1,outAssignments2,outDerivedAlgs,outDerivedMultiEqn,outArg):= - matchcontinue (inJop,isyst,ishared,inFunctions,inAssignments1,inAssignments2,inDerivedAlgs,inDerivedMultiEqn,inArg) + (changedEqns,osyst,oshared,outAssignments1,outAssignments2,outDerivedAlgs,outDerivedMultiEqn,outArg):= + matchcontinue (eqns,inJop,isyst,ishared,inFunctions,inAssignments1,inAssignments2,inDerivedAlgs,inDerivedMultiEqn,inArg) local - list eqns,diff_eqns,eqns_1,stateindx,deqns,reqns,changedeqns; + list diff_eqns,eqns_1,stateindx,deqns,reqns,changedeqns; list states; BackendDAE.BackendDAE dae; array> m,mt; @@ -4741,9 +4759,8 @@ algorithm BackendDAE.EqSystem syst; BackendDAE.Shared shared; - case (BackendDAE.REDUCE_INDEX(),syst as BackendDAE.EQSYSTEM(mT=SOME(mt)),shared,inFunctions,ass1,ass2,derivedAlgs,derivedMultiEqn,inArg) + case (eqns,BackendDAE.REDUCE_INDEX(),syst as BackendDAE.EQSYSTEM(mT=SOME(mt)),shared,inFunctions,ass1,ass2,derivedAlgs,derivedMultiEqn,inArg) equation - eqns = BackendDAEEXT.getMarkedEqns(); // BackendDump.dumpStateVariables(BackendVariable.daeVars(syst)); // print("marked equations:");print(stringDelimitList(List.map(eqns,intString),",")); // print("\n"); @@ -4772,23 +4789,23 @@ algorithm syst = makeAlgebraic(syst, state); syst = BackendDAEUtil.updateIncidenceMatrix(syst, shared, changedeqns); // print("new DAE:"); + // BackendDump.dumpEqSystem(syst); // BackendDump.dump(BackendDAE.DAE({syst},shared)); // print("new IM:"); // (_,m,_) = BackendDAEUtil.getIncidenceMatrixfromOption(syst,shared,BackendDAE.SOLVABLE()); // BackendDump.dumpIncidenceMatrix(m); // BackendDump.dumpStateVariables(BackendVariable.daeVars(syst)); then - (syst,shared,ass1,ass2,derivedAlgs1,derivedMultiEqn1,inArg); + (changedeqns,syst,shared,ass1,ass2,derivedAlgs1,derivedMultiEqn1,inArg); - case (BackendDAE.STARTSTEP(),syst,shared,inFunctions,ass1,ass2,derivedAlgs,derivedMultiEqn,inArg) - then (syst,shared,ass1,ass2,derivedAlgs,derivedMultiEqn,inArg); + case (eqns,BackendDAE.STARTSTEP(),syst,shared,inFunctions,ass1,ass2,derivedAlgs,derivedMultiEqn,inArg) + then ({},syst,shared,ass1,ass2,derivedAlgs,derivedMultiEqn,inArg); - case (BackendDAE.ENDSTEP(),syst,shared,inFunctions,ass1,ass2,derivedAlgs,derivedMultiEqn,inArg) - then (syst,shared,ass1,ass2,derivedAlgs,derivedMultiEqn,inArg); + case (eqns,BackendDAE.ENDSTEP(),syst,shared,inFunctions,ass1,ass2,derivedAlgs,derivedMultiEqn,inArg) + then ({},syst,shared,ass1,ass2,derivedAlgs,derivedMultiEqn,inArg); - case (BackendDAE.REDUCE_INDEX(),syst,shared,_,_,_,_,_,_) + case (eqns,BackendDAE.REDUCE_INDEX(),syst,shared,_,_,_,_,_,_) equation - eqns = BackendDAEEXT.getMarkedEqns(); diff_eqns = BackendDAEEXT.getDifferentiatedEqns(); eqns_1 = List.setDifferenceOnTrue(eqns, diff_eqns, intEq); es = List.map(eqns_1, intString); diff --git a/Compiler/BackEnd/BackendDAEUtil.mo b/Compiler/BackEnd/BackendDAEUtil.mo index 48a82db3a11..5a015ef2ab0 100644 --- a/Compiler/BackEnd/BackendDAEUtil.mo +++ b/Compiler/BackEnd/BackendDAEUtil.mo @@ -2530,7 +2530,7 @@ algorithm case (_,_) equation - print("- BackendDAEUtil.invReachableNodes2 failed\n"); + Error.addMessage(Error.INTERNAL_ERROR,{"- BackendDAEUtil.invReachableNodes2 failed\n"}); then fail(); end matchcontinue; @@ -2596,33 +2596,19 @@ public function varsInEqn See also: eqnsForVar and eqnsForVarWithStates inputs: (IncidenceMatrix, int /* equation */) outputs: int list /* variables */" - input BackendDAE.IncidenceMatrix inIncidenceMatrix; - input Integer inInteger; + input BackendDAE.IncidenceMatrix m; + input Integer indx; output list outIntegerLst; algorithm - outIntegerLst := matchcontinue (inIncidenceMatrix,inInteger) - local - BackendDAE.Value n,indx; - list res,res_1; - array> m; - String s; - - case (m,n) - equation - res = m[n]; - res_1 = removeNegative(res); + outIntegerLst := matchcontinue (m,indx) + local String s; + case (_,_) then - res_1; - - case (m,indx) + removeNegative(m[indx]); + else equation - print("- BackendDAEUtil.varsInEqn failed, indx= "); - s = intString(indx); - print(s); - print(" array length: "); - s = intString(arrayLength(m)); - print(s); - print("\n"); + s = "- BackendDAEUtil.varsInEqn failed, indx= " +& intString(indx) +& "array length: " +& intString(arrayLength(m)) +& "\n"; + Error.addMessage(Error.INTERNAL_ERROR,{s}); then fail(); end matchcontinue; @@ -2907,7 +2893,7 @@ algorithm else equation - print("- BackendDAEUtil.treeAdd failed\n"); + Error.addMessage(Error.INTERNAL_ERROR,{"- BackendDAEUtil.treeAdd2 failed\n"}); then fail(); end matchcontinue; @@ -2973,7 +2959,7 @@ algorithm else equation - print("- BackendDAEUtil.treeDelete failed\n"); + Error.addMessage(Error.INTERNAL_ERROR,{"- BackendDAEUtil.treeDelete failed\n"}); then fail(); end matchcontinue; @@ -3021,7 +3007,7 @@ algorithm else equation - print("- Backend.treeDeleteRightmostValue failed\n"); + Error.addMessage(Error.INTERNAL_ERROR,{"- Backend.treeDeleteRightmostValue failed\n"}); then fail(); end matchcontinue; @@ -6156,6 +6142,7 @@ end pastoptimiseDAEModule; partial function daeHandlerFunc "function daeHandlerFunc This is the interface for the index reduction handler." + input list eqns; input BackendDAE.DAEHandlerJop inJop; input BackendDAE.EqSystem syst; input BackendDAE.Shared shared; @@ -6165,6 +6152,7 @@ partial function daeHandlerFunc input list> inDerivedAlgs; input list> inDerivedMultiEqn; input BackendDAE.DAEHandlerArg inArg; + output list changedEqns; output BackendDAE.EqSystem osyst; output BackendDAE.Shared oshared; output BackendDAE.Assignments outAssignments1; @@ -6174,6 +6162,18 @@ partial function daeHandlerFunc output BackendDAE.DAEHandlerArg outArg; end daeHandlerFunc; +partial function matchingAlgorithmFunc +"function: matchingAlgorithmFunc + This is the interface for the matching algorithm" + input BackendDAE.EqSystem isyst; + input BackendDAE.Shared ishared; + input BackendDAE.MatchingOptions inMatchingOptions; + input daeHandlerFunc daeHandler; + input DAE.FunctionTree inFunctions; + output BackendDAE.EqSystem osyst; + output BackendDAE.Shared oshared; +end matchingAlgorithmFunc; + public function getSolvedSystem " function: getSolvedSystem Run the equation system pipeline." @@ -6182,6 +6182,7 @@ public function getSolvedSystem input BackendDAE.BackendDAE inDAE; input DAE.FunctionTree functionTree; input Option> strPreOptModules; + input Option strmatchingAlgorithm; input Option strdaeHandler; input Option> strPastOptModules; output BackendDAE.BackendDAE outSODE; @@ -6195,11 +6196,13 @@ protected list> preOptModules; list> pastOptModules; tuple daeHandler; + tuple matchingAlgorithm; BackendDAE.EqSystem syst; algorithm - + preOptModules := getPreOptModules(strPreOptModules); pastOptModules := getPastOptModules(strPastOptModules); + matchingAlgorithm := getMatchingAlgorithm(strmatchingAlgorithm); daeHandler := getIndexReductionMethod(strdaeHandler); Debug.fcall(Flags.DUMP_DAE_LOW, print, "dumpdaelow:\n"); @@ -6210,11 +6213,11 @@ algorithm (optdae,Util.SUCCESS()) := preoptimiseDAE(inDAE,functionTree,preOptModules); // transformation phase (matching and sorting using a index reduction method - sode := transformDAE(optdae,functionTree,NONE(),daeHandler); + sode := transformDAE(optdae,functionTree,NONE(),matchingAlgorithm,daeHandler); Debug.fcall(Flags.DUMP_DAE_LOW, BackendDump.bltdump, ("bltdump",sode)); // past optimisation phase - (optsode,Util.SUCCESS()) := pastoptimiseDAE(sode,functionTree,pastOptModules,daeHandler); + (optsode,Util.SUCCESS()) := pastoptimiseDAE(sode,functionTree,pastOptModules,matchingAlgorithm,daeHandler); (_,outFunctionTree) := BackendDAEOptimize.removeUnusedFunctions(optsode, functionTree); Debug.execStat("pastOpt removeUnusedFunctions",BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES); sode1 := BackendDAECreate.findZeroCrossings(optsode); @@ -6229,6 +6232,7 @@ algorithm Debug.fcall(Flags.DUMP_INDX_DAE, print, "dumpindxdae:\n"); Debug.fcall(Flags.DUMP_INDX_DAE, BackendDump.dump, outSODE); Debug.fcall(Flags.DUMP_BACKENDDAE_INFO, BackendDump.dumpCompShort, outSODE); + Debug.fcall(Flags.DUMP_EQNINORDER, BackendDump.dumpEqnsSolved, outSODE); end getSolvedSystem; public function preOptimiseBackendDAE @@ -6257,27 +6261,25 @@ algorithm (outDAE,status) := matchcontinue (inDAE,functionTree,optModules) local BackendDAE.BackendDAE dae,dae1,dae2; - DAE.FunctionTree funcs; preoptimiseDAEModule optModule; list> rest; String str,moduleStr; - Option m,mT,m1,mT1,m2,mT2; Boolean b; - case (dae,funcs,{}) then (dae,Util.SUCCESS()); - case (dae,funcs,(optModule,moduleStr,_)::rest) + case (dae,_,{}) then (dae,Util.SUCCESS()); + case (dae,_,(optModule,moduleStr,_)::rest) equation - dae1 = optModule(dae,funcs); + dae1 = optModule(dae,functionTree); Debug.execStat("preOpt " +& moduleStr,BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES); Debug.fcall(Flags.OPT_DAE_DUMP, print, stringAppendList({"\nOptimisation Module ",moduleStr,":\n\n"})); Debug.fcall(Flags.OPT_DAE_DUMP, BackendDump.dump, dae1); - (dae2,status) = preoptimiseDAE(dae1,funcs,rest); + (dae2,status) = preoptimiseDAE(dae1,functionTree,rest); then (dae2,status); - case (dae,funcs,(optModule,moduleStr,b)::rest) + case (dae,_,(optModule,moduleStr,b)::rest) equation Debug.execStat(" preOpt " +& moduleStr,BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES); str = stringAppendList({"Optimisation Module ",moduleStr," failed."}); Debug.bcall2(not b,Error.addMessage, Error.INTERNAL_ERROR, {str}); - (dae,status) = preoptimiseDAE(dae,funcs,rest); + (dae,status) = preoptimiseDAE(dae,functionTree,rest); then (dae,Util.if_(b,Util.FAILURE(),status)); end matchcontinue; end preoptimiseDAE; @@ -6288,13 +6290,16 @@ public function transformBackendDAE input BackendDAE.BackendDAE inDAE; input DAE.FunctionTree functionTree; input Option inMatchingOptions; + input Option strmatchingAlgorithm; input Option strindexReductionMethod; output BackendDAE.BackendDAE outDAE; protected + tuple matchingAlgorithm; tuple indexReductionMethod; algorithm + matchingAlgorithm := getMatchingAlgorithm(strmatchingAlgorithm); indexReductionMethod := getIndexReductionMethod(strindexReductionMethod); - outDAE := transformDAE(inDAE,functionTree,inMatchingOptions,indexReductionMethod); + outDAE := transformDAE(inDAE,functionTree,inMatchingOptions,matchingAlgorithm,indexReductionMethod); end transformBackendDAE; protected function transformDAE @@ -6305,6 +6310,7 @@ protected function transformDAE input BackendDAE.BackendDAE inDAE; input DAE.FunctionTree functionTree; input Option inMatchingOptions; + input tuple matchingAlgorithm; input tuple daeHandler; output BackendDAE.BackendDAE outDAE; protected @@ -6312,7 +6318,7 @@ protected BackendDAE.Shared shared; algorithm BackendDAE.DAE(systs,shared) := inDAE; - (systs,shared) := mapTransformDAE(systs,shared,functionTree,inMatchingOptions,daeHandler,{}); + (systs,shared) := mapTransformDAE(systs,shared,functionTree,inMatchingOptions,matchingAlgorithm,daeHandler,{}); outDAE := BackendDAE.DAE(systs,shared); end transformDAE; @@ -6325,31 +6331,22 @@ protected function mapTransformDAE input BackendDAE.Shared ishared; input DAE.FunctionTree functionTree; input Option inMatchingOptions; + input tuple matchingAlgorithm; input tuple daeHandler; input list acc; output list osysts; output BackendDAE.Shared oshared; algorithm - (osysts,oshared) := matchcontinue (isysts,ishared,functionTree,inMatchingOptions,daeHandler,acc) + (osysts,oshared) := matchcontinue (isysts,ishared,functionTree,inMatchingOptions,matchingAlgorithm,daeHandler,acc) local - BackendDAE.BackendDAE dae,ode,ode2; - DAE.FunctionTree funcs; - String str,methodstr; - Option om,omT; - BackendDAE.IncidenceMatrix m,mT,m1,mT1; - array v1,v2; - BackendDAE.StrongComponents comps; - BackendDAE.MatchingOptions match_opts; - daeHandlerFunc daeHandlerfunc; BackendDAE.EqSystem syst; list systs; BackendDAE.Shared shared; - - case ({},shared,funcs,inMatchingOptions,daeHandler,acc) then (listReverse(acc),shared); - case (syst::systs,shared,funcs,inMatchingOptions,daeHandler,acc) + case ({},_,_,_,_,_,_) then (listReverse(acc),ishared); + case (syst::systs,_,_,_,_,_,_) equation - (syst,shared) = transformDAEWork(syst,shared,funcs,inMatchingOptions,daeHandler); - (systs,shared) = mapTransformDAE(systs,shared,funcs,inMatchingOptions,daeHandler,syst::acc); + (syst,shared) = transformDAEWork(syst,ishared,functionTree,inMatchingOptions,matchingAlgorithm,daeHandler); + (systs,shared) = mapTransformDAE(systs,shared,functionTree,inMatchingOptions,matchingAlgorithm,daeHandler,syst::acc); then (systs,shared); end matchcontinue; end mapTransformDAE; @@ -6363,35 +6360,36 @@ protected function transformDAEWork input BackendDAE.Shared ishared; input DAE.FunctionTree functionTree; input Option inMatchingOptions; + input tuple matchingAlgorithm; input tuple daeHandler; output BackendDAE.EqSystem osyst; output BackendDAE.Shared oshared; algorithm - (osyst,oshared) := matchcontinue (isyst,ishared,functionTree,inMatchingOptions,daeHandler) + (osyst,oshared) := matchcontinue (isyst,ishared,functionTree,inMatchingOptions,matchingAlgorithm,daeHandler) local - BackendDAE.BackendDAE dae,ode,ode2; - DAE.FunctionTree funcs; - String str,methodstr; + String str,methodstr,mAmethodstr; array v1; BackendDAE.StrongComponents comps; BackendDAE.MatchingOptions match_opts; daeHandlerFunc daeHandlerfunc; + matchingAlgorithmFunc matchingAlgorithmfunc; BackendDAE.EqSystem syst; BackendDAE.Shared shared; + Real t; - case (syst,shared,funcs,inMatchingOptions,(daeHandlerfunc,methodstr)) + case (_,_,_,inMatchingOptions,(matchingAlgorithmfunc,mAmethodstr),(daeHandlerfunc,methodstr)) equation - (syst,_,_) = getIncidenceMatrix(syst,shared,BackendDAE.SOLVABLE()); + (syst,_,_) = getIncidenceMatrix(isyst,ishared,BackendDAE.SOLVABLE()); match_opts = Util.getOptionOrDefault(inMatchingOptions,(BackendDAE.INDEX_REDUCTION(), BackendDAE.EXACT())); // matching algorithm - (syst,shared) = BackendDAETransform.matchingAlgorithm(syst,shared, match_opts, daeHandlerfunc, funcs); - Debug.execStat("transformDAE -> index Reduction Method " +& methodstr,BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES); + (syst,shared) = matchingAlgorithmfunc(syst,ishared, match_opts, daeHandlerfunc, functionTree); + Debug.execStat("transformDAE -> matchingAlgorithm " +& mAmethodstr +& " index Reduction Method " +& methodstr,BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES); // sorting algorithm (syst,_,_) = getIncidenceMatrix(syst,shared,BackendDAE.NORMAL()); (syst as BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(ass1=v1)),comps) = BackendDAETransform.strongComponents(syst, shared); // if comps vector does not match to matching size // restart the matching - (syst,shared) = restartMatching(not checkCompsMatching(comps,arrayList(v1)),syst, shared, match_opts, daeHandlerfunc, funcs); + (syst,shared) = restartMatching(not checkCompsMatching(comps,arrayList(v1)),syst, shared, match_opts, matchingAlgorithmfunc, daeHandlerfunc, functionTree); Debug.execStat("transformDAE -> sort components",BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES); then (syst,shared); @@ -6409,18 +6407,19 @@ protected function restartMatching input BackendDAE.EqSystem isyst; input BackendDAE.Shared ishared; input BackendDAE.MatchingOptions opts; + input matchingAlgorithmFunc matchingAlgorithm; input daeHandlerFunc daeHandler; input DAE.FunctionTree funcs; output BackendDAE.EqSystem osyst; output BackendDAE.Shared oshared; algorithm - (osyst,oshared) := match (cond,isyst,ishared,opts,daeHandler,funcs) + (osyst,oshared) := match (cond,isyst,ishared,opts,matchingAlgorithm,daeHandler,funcs) local BackendDAE.EqSystem syst; BackendDAE.Shared shared; - case (false,syst,shared,_,_,_) then (syst,shared); - case (true,syst,shared,opts,daeHandler,funcs) + case (false,_,_,_,_,_,_) then (isyst,ishared); + case (true,_,_,_,_,_,_) equation - (syst,_,_) = getIncidenceMatrix(syst,shared,BackendDAE.SOLVABLE()); - (syst,shared) = BackendDAETransform.matchingAlgorithm(syst, shared, opts, daeHandler, funcs); + (syst,_,_) = getIncidenceMatrix(isyst,ishared,BackendDAE.SOLVABLE()); + (syst,shared) = BackendDAETransform.matchingAlgorithm(syst, ishared, opts, daeHandler, funcs); (syst,_,_) = getIncidenceMatrix(syst,shared,BackendDAE.NORMAL()); (syst,_) = BackendDAETransform.strongComponents(syst,shared); then (syst,shared); @@ -6433,41 +6432,38 @@ protected function pastoptimiseDAE input BackendDAE.BackendDAE inDAE; input DAE.FunctionTree functionTree; input list> optModules; + input tuple matchingAlgorithm; input tuple daeHandler; output BackendDAE.BackendDAE outDAE; output Util.Status status; algorithm (outDAE,status):= - matchcontinue (inDAE,functionTree,optModules,daeHandler) + matchcontinue (inDAE,functionTree,optModules,matchingAlgorithm,daeHandler) local BackendDAE.BackendDAE dae,dae1,dae2; - DAE.FunctionTree funcs; pastoptimiseDAEModule optModule; list> rest; String str,moduleStr; - BackendDAE.IncidenceMatrix m,mT,m1,mT1,m2,mT2; - array v1,v2,v1_1,v2_1,v1_2,v2_2; - BackendDAE.StrongComponents comps,comps1,comps2; Boolean runMatching,b; - case (dae,funcs,{},_) then (dae,Util.SUCCESS()); - case (dae,funcs,(optModule,moduleStr,_)::rest,daeHandler) + case (_,_,{},_,_) then (inDAE,Util.SUCCESS()); + case (_,_,(optModule,moduleStr,_)::rest,_,_) equation - (dae1,runMatching) = optModule(dae,funcs); + (dae,runMatching) = optModule(inDAE,functionTree); Debug.execStat("pastOpt " +& moduleStr,BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES); Debug.fcall(Flags.OPT_DAE_DUMP, print, stringAppendList({"\nOptimisation Module ",moduleStr,":\n\n"})); - Debug.fcall(Flags.OPT_DAE_DUMP, BackendDump.dump, dae1); - dae1 = checktransformDAE(runMatching,dae1,funcs,daeHandler); - (dae2,status) = pastoptimiseDAE(dae1,funcs,rest,daeHandler); + Debug.fcall(Flags.OPT_DAE_DUMP, BackendDump.dump, dae); + dae1 = checktransformDAE(runMatching,dae,functionTree,matchingAlgorithm,daeHandler); + (dae2,status) = pastoptimiseDAE(dae1,functionTree,rest,matchingAlgorithm,daeHandler); then (dae2,status); - case (dae,funcs,(optModule,moduleStr,b)::rest,daeHandler) + case (_,_,(optModule,moduleStr,b)::rest,_,_) equation Debug.execStat("pastOpt " +& moduleStr,BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES); str = stringAppendList({"Optimisation Module ",moduleStr," failed."}); Debug.bcall2(not b,Error.addMessage, Error.INTERNAL_ERROR, {str}); - (dae1,status) = pastoptimiseDAE(dae,funcs,rest,daeHandler); + (dae,status) = pastoptimiseDAE(inDAE,functionTree,rest,matchingAlgorithm,daeHandler); then - (dae1,Util.if_(b,Util.FAILURE(),status)); + (dae,Util.if_(b,Util.FAILURE(),status)); end matchcontinue; end pastoptimiseDAE; @@ -6477,26 +6473,22 @@ protected function checktransformDAE input Boolean inRunMatching; input BackendDAE.BackendDAE inDAE; input DAE.FunctionTree functionTree; + input tuple matchingAlgorithm; input tuple daeHandler; output BackendDAE.BackendDAE outDAE; algorithm outDAE := - match (inRunMatching,inDAE,functionTree,daeHandler) + match (inRunMatching,inDAE,functionTree,matchingAlgorithm,daeHandler) local - BackendDAE.BackendDAE dae,sode; - DAE.FunctionTree funcs; - BackendDAE.IncidenceMatrix m,mT; - array v1,v2; - BackendDAE.StrongComponents comps; - Boolean runMatching; - case (true,dae,funcs,daeHandler) + BackendDAE.BackendDAE sode; + case (true,_,_,_,_) equation -// sode = transformDAE(dae,funcs,SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())),daeHandler); - sode = transformDAE(dae,funcs,NONE(),daeHandler); +// sode = transformDAE(inDAE,functionTree,SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())),matchingAlgorithm,daeHandler); + sode = transformDAE(inDAE,functionTree,NONE(),matchingAlgorithm,daeHandler); Debug.fcall(Flags.BLT_DUMP, BackendDump.bltdump, ("bltdump",sode)); then sode; - case (false,dae,funcs,_) - then dae; + case (false,_,_,_,_) + then inDAE; end match; end checktransformDAE; @@ -6617,6 +6609,63 @@ algorithm end matchcontinue; end selectIndexReductionMethod; +/************************************************* + * matching Algorithm Selection + ************************************************/ + +public function getMatchingAlgorithmString +" function: getMatchingAlgorithmString" + output String strMatchingAlgorithm; +algorithm + strMatchingAlgorithm := Config.getMatchingAlgorithm(); +end getMatchingAlgorithmString; + +protected function getMatchingAlgorithm +" function: getIndexReductionMethod" + input Option ostrMatchingAlgorithm; + output tuple matchingAlgorithm; +protected + list> allMatchingAlgorithms; + String strMatchingAlgorithm; +algorithm + allMatchingAlgorithms := {(BackendDAETransform.matchingAlgorithm,"omc")}; + strMatchingAlgorithm := getMatchingAlgorithmString(); + strMatchingAlgorithm := Util.getOptionOrDefault(ostrMatchingAlgorithm,strMatchingAlgorithm); + matchingAlgorithm := selectMatchingAlgorithm(strMatchingAlgorithm,allMatchingAlgorithms); +end getMatchingAlgorithm; + +protected function selectMatchingAlgorithm +" function: selectMatchingAlgorithm" + input String strMatchingAlgorithm; + input list> inMatchingAlgorithms; + output tuple outMatchingAlgorithm; + replaceable type Type_a subtypeof Any; +algorithm + outMatchingAlgorithm:= + matchcontinue (strMatchingAlgorithm,inMatchingAlgorithms) + local + String name,name1,str; + tuple method; + list> methods; + case (name,(method as (_,name1))::methods) + equation + true = stringEqual(name,name1); + then + method; + case (name,_::methods) + equation + method = selectMatchingAlgorithm(name,methods); + then + method; + case (name,_) + equation + str = stringAppendList({"Selection of Matching Algorithm ",name," failed."}); + Error.addMessage(Error.INTERNAL_ERROR, {str}); + then + fail(); + end matchcontinue; +end selectMatchingAlgorithm; + /************************************************* * Optimisation Selection ************************************************/ diff --git a/Compiler/BackEnd/BackendEquation.mo b/Compiler/BackEnd/BackendEquation.mo index 95446518301..375cf1877bd 100644 --- a/Compiler/BackEnd/BackendEquation.mo +++ b/Compiler/BackEnd/BackendEquation.mo @@ -1073,26 +1073,21 @@ end equationAdd; public function equationAddDAE "function: equationAddDAE - author: Frenkel TUD 2011-05 - Incidence Matrix is lost" + author: Frenkel TUD 2011-05" input BackendDAE.Equation inEquation; input BackendDAE.EqSystem syst; output BackendDAE.EqSystem osyst; algorithm osyst := match (inEquation,syst) local - BackendDAE.Variables ordvars,knvars,exobj; - BackendDAE.AliasVariables aliasVars; - BackendDAE.EquationArray eqns,remeqns,inieqns,eqns1; - array arreqns; - array algorithms; - BackendDAE.EventInfo einfo; - BackendDAE.ExternalObjectClasses eoc; - BackendDAE.Shared shared; - case (inEquation,BackendDAE.EQSYSTEM(orderedVars=ordvars,orderedEqs=eqns)) + BackendDAE.Variables ordvars; + BackendDAE.EquationArray eqns,eqns1; + Option m; + Option mT; + case (inEquation,BackendDAE.EQSYSTEM(orderedVars=ordvars,orderedEqs=eqns,m=m,mT=mT)) equation eqns1 = equationAdd(inEquation,eqns); - then BackendDAE.EQSYSTEM(ordvars,eqns1,NONE(),NONE(),BackendDAE.NO_MATCHING()); + then BackendDAE.EQSYSTEM(ordvars,eqns1,m,mT,BackendDAE.NO_MATCHING()); end match; end equationAddDAE; diff --git a/Compiler/BackEnd/BackendVariable.mo b/Compiler/BackEnd/BackendVariable.mo index 8107c15278b..0b5fbb7e21b 100644 --- a/Compiler/BackEnd/BackendVariable.mo +++ b/Compiler/BackEnd/BackendVariable.mo @@ -1841,6 +1841,16 @@ end getNominalAssert; * ======================================================= */ +public function daenumVariables + input BackendDAE.EqSystem syst; + output Integer n; +protected + BackendDAE.Variables vars; +algorithm + vars := daeVars(syst); + n := numVariables(vars); +end daenumVariables; + public function numVariables input BackendDAE.Variables vars; output Integer n; diff --git a/Compiler/BackEnd/SimCode.mo b/Compiler/BackEnd/SimCode.mo index f406ab29509..bb4a51634f1 100644 --- a/Compiler/BackEnd/SimCode.mo +++ b/Compiler/BackEnd/SimCode.mo @@ -98,12 +98,10 @@ protected import ExpressionDump; protected import ExpressionSimplify; protected import ExpressionSolve; protected import Flags; -protected import InnerOuter; protected import List; protected import Mod; protected import PartFn; protected import PriorityQueue; -protected import SCodeUtil; protected import Settings; protected import SimCodeDump; protected import System; @@ -1023,7 +1021,7 @@ algorithm funcs = Env.getFunctionTree(cache); dlow = BackendDAECreate.lower(dae,funcs,true); (dlow_1,funcs1) = BackendDAEUtil.getSolvedSystem(cache, env, dlow, funcs, - NONE(), NONE(), NONE()); + NONE(), NONE(), NONE(), NONE()); Debug.fprintln(Flags.DYN_LOAD, "translateModel: Generating simulation code and functions."); (indexed_dlow_1,libs,file_dir,timeBackend,timeSimCode,timeTemplates) = generateModelCodeFMU(dlow_1,funcs1,p, dae, className, filenameprefix, inSimSettingsOpt); @@ -1240,7 +1238,7 @@ algorithm funcs = Env.getFunctionTree(cache); dlow = BackendDAECreate.lower(dae,funcs,true); (dlow_1,funcs1) = BackendDAEUtil.getSolvedSystem(cache, env, dlow, funcs, - NONE(), NONE(), NONE()); + NONE(), NONE(), NONE(), NONE()); (indexed_dlow_1,libs,file_dir,timeBackend,timeSimCode,timeTemplates, timeJacobians) = generateModelCode(dlow_1,funcs1,p, dae, className, filenameprefix, inSimSettingsOpt, args); resultValues = @@ -2587,7 +2585,7 @@ algorithm equation true = Flags.isSet(Flags.JACOBIAN) or Flags.isSet(Flags.LINEARIZATION); dlow = BackendDAEOptimize.collapseIndependentBlocks(dlow,functions); - dlow = BackendDAEUtil.transformBackendDAE(dlow,functions,SOME((BackendDAE.NO_INDEX_REDUCTION(),BackendDAE.EXACT())),SOME("dummyDerivative")); + dlow = BackendDAEUtil.transformBackendDAE(dlow,functions,SOME((BackendDAE.NO_INDEX_REDUCTION(),BackendDAE.EXACT())),NONE(),SOME("dummyDerivative")); // The jacobian code requires single systems; // I did not rewrite it to take advantage of any parallelism in the code res = createJacobianCC(functions,dlow); @@ -2793,7 +2791,7 @@ BackendDAE.SHARED(knownVars = kv))) newVars = BackendDAEUtil.emptyVars(); (newEqns, newVars) = splitoutEquationAndVars(blt_states,e,v,newEqns,newVars); dlow = BackendDAE.DAE(BackendDAE.EQSYSTEM(newVars,newEqns,NONE(),NONE(),BackendDAE.NO_MATCHING())::{},shared); - dlow = BackendDAEUtil.transformBackendDAE(dlow,functions,SOME((BackendDAE.NO_INDEX_REDUCTION(),BackendDAE.EXACT())),SOME("dummyDerivative")); + dlow = BackendDAEUtil.transformBackendDAE(dlow,functions,SOME((BackendDAE.NO_INDEX_REDUCTION(),BackendDAE.EXACT())),NONE(),SOME("dummyDerivative")); // Prepare all needed variables varlst_tmp = BackendDAEUtil.varList(v); @@ -4995,7 +4993,7 @@ algorithm (m,mt) = BackendDAEUtil.incidenceMatrix(syst, shared, BackendDAE.ABSOLUTE()); syst = BackendDAE.EQSYSTEM(vars_1,eqns_1,SOME(m),SOME(mt),BackendDAE.NO_MATCHING()); subsystem_dae = BackendDAE.DAE({syst},shared); - (subsystem_dae_1 as BackendDAE.DAE(eqs={BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(v1,v2,comps))})) = BackendDAEUtil.transformBackendDAE(subsystem_dae,DAEUtil.avlTreeNew(),SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())),NONE()); + (subsystem_dae_1 as BackendDAE.DAE(eqs={BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(v1,v2,comps))})) = BackendDAEUtil.transformBackendDAE(subsystem_dae,DAEUtil.avlTreeNew(),SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())),NONE(),NONE()); (subsystem_dae_2 as BackendDAE.DAE(eqs={BackendDAE.EQSYSTEM(m=SOME(m_3),mT=SOME(mT_3))}),v1_1,v2_1,comps_1,r,t) = BackendDAEOptimize.tearingSystem(subsystem_dae_1,v1,v2,comps); true = listLength(r) > 0; true = listLength(t) > 0; @@ -5314,7 +5312,7 @@ algorithm evars = BackendDAEUtil.emptyVars(); eeqns = BackendDAEUtil.listEquation({}); subsystem_dae = BackendDAE.DAE(BackendDAE.EQSYSTEM(v,eqn,NONE(),NONE(),BackendDAE.NO_MATCHING())::{},BackendDAE.SHARED(evars,evars,ave,eeqns,eeqns,ae,algorithms,complEqs,BackendDAE.EVENT_INFO({},{}),{},BackendDAE.ALGEQSYSTEM() )); - (subsystem_dae_1 as BackendDAE.DAE(eqs={BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(v1,v2,comps))})) = BackendDAEUtil.transformBackendDAE(subsystem_dae,DAEUtil.avlTreeNew(),SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())),NONE()); + (subsystem_dae_1 as BackendDAE.DAE(eqs={BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(v1,v2,comps))})) = BackendDAEUtil.transformBackendDAE(subsystem_dae,DAEUtil.avlTreeNew(),SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())),NONE(),NONE()); (subsystem_dae_2,v1_1,v2_1,comps_1,r,t) = BackendDAEOptimize.tearingSystem(subsystem_dae_1,v1,v2,comps); true = listLength(r) > 0; true = listLength(t) > 0; @@ -6053,7 +6051,7 @@ algorithm syst = BackendDAE.EQSYSTEM(vars,eqns_1,NONE(),NONE(),BackendDAE.NO_MATCHING()); shared = BackendDAE.SHARED(evars,evars,av,eeqns,eeqns,ae1,al,complEqs,BackendDAE.EVENT_INFO({},{}),{},BackendDAE.ARRAYSYSTEM()); subsystem_dae = BackendDAE.DAE({syst},shared); - (BackendDAE.DAE({syst as BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(comps=comps))},shared)) = BackendDAEUtil.transformBackendDAE(subsystem_dae,DAEUtil.avlTreeNew(),SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.ALLOW_UNDERCONSTRAINED())),NONE()); + (BackendDAE.DAE({syst as BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(comps=comps))},shared)) = BackendDAEUtil.transformBackendDAE(subsystem_dae,DAEUtil.avlTreeNew(),SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.ALLOW_UNDERCONSTRAINED())),NONE(),NONE()); equations_ = createEquations(false, true, genDiscrete, false, false, syst, shared, offset, comps, helpVarInfo); then equations_; diff --git a/Compiler/BackEnd/Uncertainties.mo b/Compiler/BackEnd/Uncertainties.mo index b9d61d2f7bc..b899fae548a 100644 --- a/Compiler/BackEnd/Uncertainties.mo +++ b/Compiler/BackEnd/Uncertainties.mo @@ -143,7 +143,7 @@ algorithm print("* Lower ok\n"); - (dlow_1,funcs1) = BackendDAEUtil.getSolvedSystem(cache, env, dlow, funcs,SOME({"removeFinalParameters", "removeEqualFunctionCalls","partitionIndependentBlocks", "expandDerOperator"}), NONE(), NONE()); + (dlow_1,funcs1) = BackendDAEUtil.getSolvedSystem(cache, env, dlow, funcs,SOME({"removeFinalParameters", "removeEqualFunctionCalls","partitionIndependentBlocks", "expandDerOperator"}), NONE(), NONE(), NONE()); print("*** Lowered: \n"); BackendDump.dump(dlow_1); print("*** end Lowered: \n"); diff --git a/Compiler/BackEnd/XMLDump.mo b/Compiler/BackEnd/XMLDump.mo index f876a770b1f..7b0d76cfd98 100644 --- a/Compiler/BackEnd/XMLDump.mo +++ b/Compiler/BackEnd/XMLDump.mo @@ -92,7 +92,6 @@ protected import BackendDAETransform; protected import ComponentReference; protected import Config; protected import DAEUtil; -protected import Debug; protected import Dump; protected import Error; protected import Expression; @@ -2748,7 +2747,7 @@ algorithm case (false,false,_) then (); case (true,true,dlow) equation - (BackendDAE.DAE(eqs=BackendDAE.EQSYSTEM(m=SOME(m),mT=SOME(mT),matching=BackendDAE.MATCHING(v1,v2,comps))::{})) = BackendDAEUtil.transformBackendDAE(dlow,DAEUtil.avlTreeNew(),NONE(),NONE()); + (BackendDAE.DAE(eqs=BackendDAE.EQSYSTEM(m=SOME(m),mT=SOME(mT),matching=BackendDAE.MATCHING(v1,v2,comps))::{})) = BackendDAEUtil.transformBackendDAE(dlow,DAEUtil.avlTreeNew(),NONE(),NONE(),NONE()); dumpStrOpenTag(ADDITIONAL_INFO); dumpStrOpenTag(ORIGINAL_INCIDENCE_MATRIX); dumpIncidenceMatrix(m); @@ -2771,7 +2770,7 @@ algorithm then (); case (false,true,dlow) equation - (BackendDAE.DAE(eqs=BackendDAE.EQSYSTEM(m=SOME(m),mT=SOME(mT),matching=BackendDAE.MATCHING(v1,v2,comps))::{})) = BackendDAEUtil.transformBackendDAE(dlow,DAEUtil.avlTreeNew(),NONE(),NONE()); + (BackendDAE.DAE(eqs=BackendDAE.EQSYSTEM(m=SOME(m),mT=SOME(mT),matching=BackendDAE.MATCHING(v1,v2,comps))::{})) = BackendDAEUtil.transformBackendDAE(dlow,DAEUtil.avlTreeNew(),NONE(),NONE(),NONE()); dumpStrOpenTag(ADDITIONAL_INFO); dumpStrOpenTag(SOLVING_INFO); dumpMatching(v1); diff --git a/Compiler/Main/Main.mo b/Compiler/Main/Main.mo index 8002094ce2c..a45fd41e47a 100644 --- a/Compiler/Main/Main.mo +++ b/Compiler/Main/Main.mo @@ -763,7 +763,7 @@ algorithm true = runBackendQ(); funcs = Env.getFunctionTree(cache); dlow = BackendDAECreate.lower(dae,funcs,true); - (dlow_1,funcs_1) = BackendDAEUtil.getSolvedSystem(cache,env,dlow,funcs,NONE(),NONE(),NONE()); + (dlow_1,funcs_1) = BackendDAEUtil.getSolvedSystem(cache,env,dlow,funcs,NONE(),NONE(),NONE(),NONE()); modpar(dlow_1); Debug.execStat("Lowering Done",CevalScript.RT_CLOCK_EXECSTAT_MAIN); simcodegen(dlow_1,funcs_1,classname,ap,dae); diff --git a/Compiler/Script/CevalScript.mo b/Compiler/Script/CevalScript.mo index 5d1ce79fb6c..5d17e8bacf0 100644 --- a/Compiler/Script/CevalScript.mo +++ b/Compiler/Script/CevalScript.mo @@ -3999,7 +3999,7 @@ algorithm funcs = Env.getFunctionTree(cache); dlow = BackendDAECreate.lower(dae, funcs, true); //Verificare cosa fa dlow_1 = BackendDAEUtil.preOptimiseBackendDAE(dlow,funcs,NONE()); - dlow_1 = BackendDAEUtil.transformBackendDAE(dlow_1,funcs,NONE(),NONE()); + dlow_1 = BackendDAEUtil.transformBackendDAE(dlow_1,funcs,NONE(),NONE(),NONE()); dlow_1 = BackendDAECreate.findZeroCrossings(dlow_1); xml_filename = stringAppendList({filenameprefix,".xml"}); funcelems = DAEUtil.getFunctionList(Env.getFunctionTree(cache)); @@ -4025,7 +4025,7 @@ algorithm dae = DAEUtil.transformationsBeforeBackend(cache,dae_1); funcs = Env.getFunctionTree(cache); dlow = BackendDAECreate.lower(dae, funcs, true); - (indexed_dlow,funcs1) = BackendDAEUtil.getSolvedSystem(cache, env, dlow, funcs, NONE(), NONE(), NONE()); + (indexed_dlow,funcs1) = BackendDAEUtil.getSolvedSystem(cache, env, dlow, funcs, NONE(), NONE(), NONE(), NONE()); xml_filename = stringAppendList({filenameprefix,".xml"}); funcelems = DAEUtil.getFunctionList(funcs1); Print.clearBuf();