Changeset 278ccf63 in OpenModelica
- Timestamp:
- 2013-12-12T11:41:31+01:00 (10 years ago)
- Branches:
- Added-citation-metadata, maintenance/v1.14, maintenance/v1.15, maintenance/v1.16, maintenance/v1.17, maintenance/v1.18, maintenance/v1.19, maintenance/v1.20, maintenance/v1.21, maintenance/v1.22, maintenance/v1.23, master, omlib-staging
- Children:
- 803137e5
- Parents:
- 2f686d3
- Location:
- Compiler
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
Compiler/BackEnd/HpcOmSimCode.mo
r2f686d3 r278ccf63 801 801 end checkOdeSystemSize; 802 802 803 //--------------------------------------------------//804 // start new functions for handling linearTornSystems from here805 //-------------------------------------------------//806 807 public function traverseEqSystemsWithIndex "traverse alle EqSystems of the BackendDAE and hold an index of the current torn system.808 author:Waurich TUD 2013-10"809 input Integer eqSysIdx;810 input Integer tornSysIdxIn;811 input BackendDAE.BackendDAE daeIn;812 output BackendDAE.BackendDAE daeOut;813 algorithm814 daeOut := matchcontinue(eqSysIdx,tornSysIdxIn,daeIn)815 local816 Integer tornSysIdx;817 BackendDAE.BackendDAE daeTmp;818 BackendDAE.EqSystem eqSyst;819 BackendDAE.EqSystems eqSysts;820 BackendDAE.Shared shared;821 case(_,_,BackendDAE.DAE(eqs=eqSysts, shared=shared))822 equation823 true = listLength(eqSysts) >= eqSysIdx;824 eqSyst = listGet(eqSysts,eqSysIdx);825 (eqSyst,shared,tornSysIdx) = reduceLinearTornSystem(eqSyst,shared,tornSysIdxIn);826 eqSysts = List.replaceAt(eqSyst,eqSysIdx-1,eqSysts);827 daeTmp = BackendDAE.DAE(eqSysts,shared);828 daeTmp = traverseEqSystemsWithIndex(eqSysIdx+1,tornSysIdx,daeTmp);829 then830 daeTmp;831 case(_,_,BackendDAE.DAE(eqs=eqSysts, shared=shared))832 equation833 true = listLength(eqSysts) < eqSysIdx;834 then835 daeIn;836 end matchcontinue;837 end traverseEqSystemsWithIndex;838 839 840 public function reduceLinearTornSystem "checks the EqSystem for tornSystems in order to dissassemble them into various SingleEquation and a reduced EquationSystem.841 This is useful in order to reduce the execution costs of the equationsystem and generate a bunch of parallel singleEquations. use +d=doLienarTearing,partlintornsystem to activate it.842 Remark: this is still under development843 author:Waurich TUD 2013-09"844 input BackendDAE.EqSystem systIn;845 input BackendDAE.Shared sharedIn;846 input Integer tornSysIdxIn;847 output BackendDAE.EqSystem systOut;848 output BackendDAE.Shared sharedOut;849 output Integer tornSysIdxOut;850 algorithm851 (systOut, sharedOut, tornSysIdxOut) := matchcontinue(systIn,sharedIn,tornSysIdxIn)852 local853 Integer tornSysIdx;854 array<Integer> ass1, ass2;855 BackendDAE.EqSystem systTmp;856 BackendDAE.EquationArray eqs, eqsTmp;857 BackendDAE.Matching matching;858 BackendDAE.Shared sharedTmp;859 BackendDAE.StrongComponents allComps, compsTmp;860 BackendDAE.Variables vars, varsTmp;861 case(_,_,_)862 equation863 BackendDAE.EQSYSTEM(matching = BackendDAE.MATCHING(ass1=ass1, ass2=ass2, comps= allComps)) = systIn;864 //BackendDump.dumpEqSystem(systIn,"original system");865 (systTmp,tornSysIdx) = reduceLinearTornSystem1(1, allComps, ass1, ass2, systIn,sharedIn,tornSysIdxIn);866 //BackendDump.dumpEqSystem(systTmp,"new system");867 sharedTmp = sharedIn;868 then869 (systTmp, sharedTmp, tornSysIdx);870 else871 equation872 print("reduceLinearTornSystem failed!");873 then874 fail();875 end matchcontinue;876 end reduceLinearTornSystem;877 878 879 protected function reduceLinearTornSystem1 "traverses all StrongComponents for tornSystems, reduces them and rebuilds the BLT, the matching and the info about vars and equations880 author: Waurich TUD 2013-09"881 input Integer compIdx;882 input BackendDAE.StrongComponents compsIn;883 input array<Integer> ass1;884 input array<Integer> ass2;885 input BackendDAE.EqSystem systIn;886 input BackendDAE.Shared sharedIn;887 input Integer tornSysIdxIn;888 output BackendDAE.EqSystem systOut;889 output Integer tornSysIdxOut;890 algorithm891 (systOut,tornSysIdxOut) := matchcontinue(compIdx,compsIn,ass1,ass2,systIn,sharedIn,tornSysIdxIn)892 local893 Integer numNewSingleEqs, tornSysIdx;894 Boolean linear;895 array<Integer> ass1New, ass2New, ass1All, ass2All, ass1Other, ass2Other;896 list<Integer> tvarIdcs;897 list<Integer> resEqIdcs;898 list<tuple<Integer,list<Integer>>> otherEqnVarTpl;899 BackendDAE.EqSystem systTmp;900 BackendDAE.EquationArray eqs;901 BackendDAE.Matching matching, matchingNew, matchingOther;902 BackendDAE.Shared sharedTmp;903 BackendDAE.StateSets stateSets;904 BackendDAE.StrongComponent comp;905 BackendDAE.StrongComponents compsNew, compsTmp, otherComps;906 BackendDAE.Variables vars;907 list<BackendDAE.Equation> eqLst, eqsNew, eqsOld, resEqs;908 list<BackendDAE.Var> varLst, varsNew, varsOld, tvars;909 case(_,_,_,_,_,_,_)910 equation911 // completed912 true = listLength(compsIn) < compIdx;913 //print("finished at:"+&intString(compIdx)+&"\n");914 then915 (systIn,tornSysIdxIn);916 case(_,_,_,_,_,_,_)917 equation918 // strongComponent is a linear tornSystem919 true = listLength(compsIn) >= compIdx;920 921 comp = listGet(compsIn,compIdx);922 BackendDAE.TORNSYSTEM(tearingvars = tvarIdcs, residualequations = resEqIdcs, otherEqnVarTpl = otherEqnVarTpl, linear = linear) = comp;923 true = linear;924 Debug.fcall(Flags.HPCOM_DUMP,print,"handle linear torn systems of size: "+&intString(listLength(tvarIdcs)+listLength(otherEqnVarTpl))+&"\n");925 //print("handle tornsystem with compnumber:"+&intString(compIdx)+&"\n");926 //BackendDump.dumpEqSystem(systIn,"the original system");927 // build the new components, the new variables and the new equations928 929 (varsNew,eqsNew,tvars,resEqs,matchingNew) = reduceLinearTornSystem2(systIn,sharedIn,tvarIdcs,resEqIdcs,otherEqnVarTpl,tornSysIdxIn);930 931 BackendDAE.MATCHING(ass1=ass1New, ass2=ass2New, comps=compsNew) = matchingNew;932 933 // add the new vars and equations to the original EqSystem934 BackendDAE.EQSYSTEM(orderedVars = vars, orderedEqs = eqs, stateSets = stateSets) = systIn;935 varsOld = BackendVariable.varList(vars);936 eqsOld = BackendEquation.equationList(eqs);937 938 varLst = listAppend(varsOld,varsNew);939 eqLst = listAppend(eqsOld, eqsNew);940 eqLst = List.fold2(List.intRange(listLength(resEqIdcs)),replaceAtPositionFromList,resEqs,resEqIdcs,eqLst); // replaces the old residualEquations with the new ones941 vars = BackendVariable.listVar1(varLst); // !!! BackendVariable.listVar outputs the reversed order therefore listVar1942 eqs = BackendEquation.listEquation(eqLst);943 Debug.fcall(Flags.HPCOM_DUMP,print,"number of equations added: "+&intString(listLength(eqLst))+&" and the size of the linear torn system: "+&intString(listLength(tvarIdcs))+&"\n");944 //print("new systemsize:"+&intString(listLength(varLst))+&" vars. and "+&intString(listLength(eqLst))+&" eqs\n");945 946 // build the matching947 ass1All = arrayCreate(listLength(varLst),-1);948 ass2All = arrayCreate(listLength(varLst),-1); // actually has to be listLength(eqLst), but there is stille the probelm taht ass1 and ass2 have the same size949 ass1All = Util.arrayCopy(ass1,ass1All); // the comps before and after the tornsystem950 ass2All = Util.arrayCopy(ass2,ass2All);951 ((ass1All, ass2All)) = List.fold2(List.intRange(listLength(tvarIdcs)),updateResidualMatching,tvarIdcs,resEqIdcs,(ass1All,ass2All)); // sets matching info for the tearingVars and residuals952 953 // get the otherComps and and update the matching for the othercomps954 matchingOther = getOtherComps(otherEqnVarTpl,ass1All,ass2All);955 BackendDAE.MATCHING(ass1=ass1Other, ass2=ass2Other, comps=otherComps) = matchingOther;956 957 // insert the new components into the BLT instead of the TornSystem, append the updated blocks for the other equations, update matching for the new equations958 numNewSingleEqs = listLength(compsNew)-listLength(tvarIdcs);959 //print("num of new comps:"+&intString(numNewSingleEqs)+&"\n");960 //BackendDump.dumpComponents(compsNew);961 compsNew = listAppend(compsNew, otherComps);962 compsTmp = List.replaceAtWithList(compsNew,compIdx-1,compsIn);963 ((ass1All,ass2All)) = List.fold2(List.intRange(arrayLength(ass1New)),updateMatching,(listLength(eqsOld),listLength(varsOld)),(ass1New,ass2New),(ass1All,ass2All));964 matching = BackendDAE.MATCHING(ass1All, ass2All, compsTmp);965 966 //build new EqSystem967 systTmp = BackendDAE.EQSYSTEM(vars,eqs,NONE(),NONE(),matching,stateSets);968 (systTmp,_,_) = BackendDAEUtil.getIncidenceMatrix(systTmp, BackendDAE.NORMAL(),NONE());969 //BackendDump.dumpEqSystem(systTmp,"the whole new system");970 (systTmp,tornSysIdx) = reduceLinearTornSystem1(compIdx+1+numNewSingleEqs,compsTmp,ass1All,ass2All,systTmp,sharedIn,tornSysIdxIn+1);971 then972 (systTmp,tornSysIdx);973 else974 // go to next StrongComponent975 equation976 //print("no torn system in comp:"+&intString(compIdx)+&"\n");977 (systTmp,tornSysIdx) = reduceLinearTornSystem1(compIdx+1,compsIn,ass1,ass2,systIn,sharedIn,tornSysIdxIn);978 then979 (systTmp,tornSysIdx);980 end matchcontinue;981 end reduceLinearTornSystem1;982 983 984 protected function reduceLinearTornSystem2 " builds from a torn system various linear equation systems that can be computed in parallel.985 author: Waurich TUD 2013-07"986 input BackendDAE.EqSystem isyst;987 input BackendDAE.Shared ishared;988 input list<Integer> tearingVars;989 input list<Integer> residualEqs;990 input list<tuple<Integer, list<Integer>>> otherEqsVarTpl;991 input Integer tornSysIdx;992 output list<BackendDAE.Var> varsNewOut;993 output list<BackendDAE.Equation> eqsNewOut;994 output list<BackendDAE.Var> tVarsOut;995 output list<BackendDAE.Equation> resEqsOut;996 output BackendDAE.Matching matchingOut;997 protected998 array<Integer> ass1New, ass2New;999 Integer size, otherEqSize, compSize;1000 list<Integer> otherEqnsInts, otherVarsInts, tVarRange, rEqIdx;1001 list<list<Integer>> otherVarsIntsLst;1002 BackendDAE.EqSystem systNew;1003 BackendDAE.EquationArray eqns, oeqns, hs0Eqs;1004 BackendDAE.Matching matchingNew;1005 BackendDAE.StrongComponent rComp;1006 BackendDAE.StrongComponents comps, compsNew, oComps;1007 BackendDAE.Variables vars, kv, diffVars, ovars, dVars;1008 BackendVarTransform.VariableReplacements repl, repl1;1009 DAE.FunctionTree functree;1010 list<BackendDAE.Equation> eqLst,reqns, otherEqnsLst,otherEqnsLstReplaced, eqNew, hs, hs1;1011 list<BackendDAE.EquationArray> gEqs, hEqs, hsEqs;1012 list<BackendDAE.Var> varLst, tvars, tvarsReplaced, ovarsLst, xa0, a_0, varNew;1013 list<BackendDAE.Variables> xaVars, rVars, aVars;1014 list<BackendVarTransform.VariableReplacements> replLst;1015 list<list<BackendDAE.Equation>> g_i_lst, g_i_lst1, h_i_lst, h_i_lst1, hs_i_lst, hs_i_lst1, hs_0_lst;1016 list<list<BackendDAE.Var>> xa_i_lst, xa_i_lst1, r_i_lst, r_i_lst1, a_i_lst, a_i_lst1;1017 list<DAE.ComponentRef> tcrs,ovcrs;1018 Option<list<tuple<Integer, Integer, BackendDAE.Equation>>> jac;1019 algorithm1020 // handle torn systems for the linear case1021 BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs = eqns, matching = BackendDAE.MATCHING(comps=comps)) := isyst;1022 BackendDAE.SHARED(knownVars=kv, functionTree=functree) := ishared;1023 eqLst := BackendEquation.equationList(eqns);1024 varLst := BackendVariable.varList(vars);1025 tvars := List.map1r(tearingVars, BackendVariable.getVarAt, vars);1026 tvarsReplaced := List.map(tvars, SimCodeUtil.transformXToXd);1027 tcrs := List.map(tvarsReplaced, BackendVariable.varCref);1028 1029 // get residual eqns1030 reqns := BackendEquation.getEqns(residualEqs, eqns);1031 reqns := SimCodeUtil.replaceDerOpInEquationList(reqns);1032 1033 // get the other equations and the other variables1034 otherEqnsInts := List.map(otherEqsVarTpl, Util.tuple21);1035 otherEqnsLst := BackendEquation.getEqns(otherEqnsInts, eqns);1036 oeqns := BackendEquation.listEquation(otherEqnsLst);1037 otherEqnsLstReplaced := SimCodeUtil.replaceDerOpInEquationList(otherEqnsLst); // for computing the new equations1038 1039 otherVarsIntsLst := List.map(otherEqsVarTpl, Util.tuple22);1040 otherVarsInts := List.unionList(otherVarsIntsLst);1041 ovarsLst := List.map1r(otherVarsInts, BackendVariable.getVarAt, vars);1042 ovarsLst := List.map(ovarsLst, SimCodeUtil.transformXToXd); //try this1043 ovars := BackendVariable.listVar1(ovarsLst);1044 ovcrs := List.map(ovarsLst, BackendVariable.varCref);1045 1046 //build the components and systems to get the system for computing the tearingVars1047 size := listLength(tvars);1048 otherEqSize := listLength(otherEqnsLst);1049 compSize := listLength(comps);1050 tVarRange := List.intRange2(0,size);1051 repl1 := BackendVarTransform.emptyReplacements();1052 1053 // get g_i(xt=e_i, xa=xa_i) with xa_i as variables to be solved1054 (g_i_lst,xa_i_lst,replLst) := getAlgebraicEquationsForEI(tVarRange,size,otherEqnsLstReplaced,tvarsReplaced,tcrs,ovarsLst,ovcrs,{},{},{},tornSysIdx);1055 (g_i_lst1,xa_i_lst1,repl1) := simplifyEquations(g_i_lst,xa_i_lst,repl1);1056 1057 //dumpVarLstLst(xa_i_lst,"xa");1058 //dumpEqLstLst(g_i_lst,"g");1059 1060 // compute residualValues h_i(xt=e_i,xa_i,r_i) for r_i1061 (h_i_lst,r_i_lst) := addResidualVarToEquation(tVarRange,reqns,{},{},tornSysIdx);1062 h_i_lst := replaceVarsInResidualEquations(tVarRange,h_i_lst,replLst,{});1063 (h_i_lst1,r_i_lst1,repl1) := simplifyEquations(h_i_lst,r_i_lst,repl1);1064 1065 //dumpVarLstLst(r_i_lst,"r");1066 //dumpEqLstLst(h_i_lst,"h");1067 1068 // get the co-efficients for the new residualEquations a_i from hs_i(r_i,xt=e_i, a_i)1069 (hs_i_lst,a_i_lst) := getTornSystemCoefficients(tVarRange,size,r_i_lst,{},{},tornSysIdx);1070 (hs_i_lst1,a_i_lst1,repl1) := simplifyEquations(hs_i_lst,a_i_lst,repl1);1071 1072 //dumpVarLstLst(a_i_lst,"a");1073 //dumpEqLstLst(hs_i_lst,"hs_i");1074 1075 // gather all additional equations and build the strongComponents (not including the new residual equation)1076 eqsNewOut := List.flatten(listAppend(listAppend(g_i_lst1,h_i_lst1),hs_i_lst1));1077 varsNewOut := List.flatten(listAppend(listAppend(xa_i_lst1,r_i_lst1),a_i_lst1));1078 matchingNew := buildSingleEquationSystem(compSize,eqsNewOut,varsNewOut,ishared,{});1079 BackendDAE.MATCHING(ass1=ass1New, ass2=ass2New, comps=compsNew) := matchingNew;1080 1081 //BackendDump.dumpComponents(compsNew);1082 1083 compsNew := List.map2(compsNew,updateIndicesInComp,listLength(varLst),listLength(eqLst));1084 1085 //BackendDump.dumpVarList(varsNewOut,"varsNew");1086 //BackendDump.dumpEquationList(eqsNewOut,"eqsNew");1087 1088 // compute the tearing vars in the new residual equations hs1089 (a_0::a_i_lst) := a_i_lst;1090 //a_0 := listReverse(a_0);1091 1092 hs := buildNewResidualEquation(1,a_i_lst,a_0,tvars,{});1093 (hs1,_) := BackendVarTransform.replaceEquations(hs,repl1,NONE());1094 tVarsOut := tvars;1095 resEqsOut := hs1;1096 1097 //// get the strongComponent for the residual equations and add it at the end of the new StrongComponents1098 1099 jac := buildLinearJacobian(a_i_lst); // TODO: check this flatten1100 //print("Jac:\n" +& BackendDump.dumpJacobianStr(jac) +& "\n");1101 rComp := BackendDAE.EQUATIONSYSTEM(residualEqs,tearingVars,jac,BackendDAE.JAC_TIME_VARYING());1102 //rComp := BackendDAE.EQUATIONSYSTEM(residualEqs,tearingVars,NONE(),BackendDAE.JAC_NO_ANALYTIC());1103 //rComp := BackendDAE.TORNSYSTEM(tearingVars,residualEqs,{},true);1104 //BackendDump.dumpEquationList(resEqsOut,"the equatinos of the system\n");1105 //BackendDump.dumpVarList(tvars, "the vars of the system\n");1106 1107 oComps := List.appendElt(rComp,compsNew);1108 matchingOut := BackendDAE.MATCHING(ass1New,ass2New,oComps);1109 1110 //printPartLinTornInfo(tcrs,reqns,otherEqnsLst,ovcrs,xa_i_lst,g_i_lst,r_i_lst,h_i_lst,a_i_lst,hs_i_lst,hs,compsNew);1111 end reduceLinearTornSystem2;1112 1113 1114 protected function buildLinearJacobian "builds the jac structure out of the given jacobian-entries (all on the diagonal).1115 author:Waurich TUD 2013-12"1116 input list<list<BackendDAE.Var>> inElements; //outer list refers to the row, inner list to the column1117 output Option<list<tuple<Integer, Integer, BackendDAE.Equation>>> outJac;1118 protected1119 list<tuple<Integer, Integer, BackendDAE.Equation>> jac;1120 algorithm1121 jac := List.fold1(List.intRange(listLength(inElements)),buildLinearJacobian1,inElements,{});1122 jac := listReverse(jac);1123 outJac := SOME(jac);1124 end buildLinearJacobian;1125 1126 1127 protected function buildLinearJacobian1 "builds the jac structure out of the given jacobian-entries (all on the diagonal).1128 author:Waurich TUD 2013-12"1129 input Integer rowIdx;1130 input list<list<BackendDAE.Var>> inElements;1131 input list<tuple<Integer, Integer, BackendDAE.Equation>> inJac;1132 output list<tuple<Integer, Integer, BackendDAE.Equation>> outJac;1133 protected1134 list<BackendDAE.Var> elements;1135 list<tuple<Integer, Integer, BackendDAE.Equation>> jac;1136 algorithm1137 elements := listGet(inElements,rowIdx);1138 outJac := List.fold2(List.intRange(listLength(inElements)),buildLinearJacobian2,elements,rowIdx,inJac);1139 end buildLinearJacobian1;1140 1141 1142 protected function buildLinearJacobian2 "builds the jac structure out of the given jacobian-entries (all on the diagonal).1143 author:Waurich TUD 2013-12"1144 input Integer colIdx;1145 input list<BackendDAE.Var> inElements;1146 input Integer rowIdx;1147 input list<tuple<Integer, Integer, BackendDAE.Equation>> inJac;1148 output list<tuple<Integer, Integer, BackendDAE.Equation>> outJac;1149 protected1150 DAE.ComponentRef cref;1151 DAE.Exp exp;1152 BackendDAE.Equation eq;1153 BackendDAE.Var elem;1154 tuple<Integer,Integer,BackendDAE.Equation> entry;1155 list<tuple<Integer, Integer, BackendDAE.Equation>> jac;1156 algorithm1157 elem := listGet(inElements,colIdx);1158 cref := BackendVariable.varCref(elem);1159 exp := DAE.CREF(cref,DAE.T_REAL_DEFAULT);1160 exp := DAE.UNARY(DAE.UMINUS(DAE.T_REAL_DEFAULT),exp);1161 eq := BackendDAE.RESIDUAL_EQUATION(exp,DAE.emptyElementSource,false);1162 entry := (colIdx,rowIdx,eq);1163 outJac := entry::inJac;1164 end buildLinearJacobian2;1165 1166 1167 protected function updateMatching "inserts the information of matching2 into matching1 by adding an index offset for the vars and eqs of matching2.Actually only one assignment for matching 2 is needed.1168 author: Waurich TUD 2013-09"1169 input Integer idx;1170 input tuple<Integer,Integer> offsetTpl;1171 input tuple<array<Integer>,array<Integer>> matching2;1172 input tuple<array<Integer>,array<Integer>> matching1In;1173 output tuple<array<Integer>,array<Integer>> matching1Out;1174 protected1175 Integer eqOffset, varOffset, eqValue, varValue;1176 array<Integer> ass11, ass21, ass12, ass22;1177 algorithm1178 (eqOffset, varOffset) := offsetTpl;1179 (ass12, ass22) := matching2;1180 (ass11, ass21) := matching1In;1181 eqValue := idx + eqOffset;1182 varValue := arrayGet(ass22,idx)+varOffset;1183 ass11 := arrayUpdate(ass11,varValue,eqValue);1184 ass21 := arrayUpdate(ass21, eqValue, varValue);1185 matching1Out := (ass11, ass21);1186 end updateMatching;1187 1188 1189 protected function updateResidualMatching "sets the matching between tearingVars and residuals.1190 author: Waurich TUD 2013-09"1191 input Integer idx;1192 input list<Integer> tvars;1193 input list<Integer> resEqs;1194 input tuple<array<Integer>,array<Integer>> tplIn;1195 output tuple<array<Integer>,array<Integer>> tplOut;1196 protected1197 array<Integer> ass1, ass2;1198 Integer eqIdx, varIdx;1199 algorithm1200 (ass1,ass2) := tplIn;1201 eqIdx := listGet(resEqs,idx);1202 varIdx := listGet(tvars,idx);1203 ass1 := arrayUpdate(ass1,varIdx,eqIdx);1204 ass2 := arrayUpdate(ass2,eqIdx,varIdx);1205 tplOut := (ass1,ass2);1206 end updateResidualMatching;1207 1208 1209 protected function getOtherComps "builds ordered StrongComponents and matching for the other equations.1210 author: Waurich TUD 2013-09"1211 input list<tuple<Integer, list<Integer>>> otherEqsVarTpl;1212 input array<Integer> ass1;1213 input array<Integer> ass2;1214 output BackendDAE.Matching matchingOut;1215 protected1216 array<Integer> ass1Tmp, ass2Tmp;1217 BackendDAE.StrongComponents compsTmp;1218 algorithm1219 ((ass1Tmp,ass2Tmp,compsTmp)) := List.fold(otherEqsVarTpl,getOtherComps1,(ass1,ass2,{}));1220 compsTmp := listReverse(compsTmp);1221 matchingOut := BackendDAE.MATCHING(ass1Tmp,ass2Tmp,compsTmp);1222 end getOtherComps;1223 1224 1225 protected function getOtherComps1 "implementation of getOtherComps1226 author:waurich TUD 2013-09"1227 input tuple<Integer,list<Integer>> otherEqsVarTpl;1228 input tuple<array<Integer>, array<Integer>, BackendDAE.StrongComponents> tplIn;1229 output tuple<array<Integer>, array<Integer>, BackendDAE.StrongComponents> tplOut;1230 algorithm1231 tplOut := matchcontinue(otherEqsVarTpl, tplIn)1232 local1233 Integer eqIdx, varIdx;1234 array<Integer> ass1, ass2;1235 list<Integer> varIdcs;1236 BackendDAE.StrongComponent comp;1237 BackendDAE.StrongComponents compsIn, compsTmp;1238 case((eqIdx, varIdcs),(ass1,ass2,compsIn))1239 equation1240 true = listLength(varIdcs) == 1;1241 varIdx = listGet(varIdcs,1);1242 comp = BackendDAE.SINGLEEQUATION(eqIdx,varIdx);1243 ass1 = arrayUpdate(ass1,varIdx,eqIdx);1244 ass2 = arrayUpdate(ass2,eqIdx,varIdx);1245 compsTmp = comp::compsIn;1246 then1247 ((ass1,ass2,compsTmp));1248 else1249 equation1250 print("getOtherComps failed\n");1251 then1252 fail();1253 end matchcontinue;1254 end getOtherComps1;1255 1256 1257 protected function replaceAtPositionFromList "replaces the entry from inLst indexed by positionLst[n] with with the nth entry in replacingLst. n is first input so it can be used in a folding functions.1258 author: Waurich TUD 2013-09"1259 replaceable type ElementType subtypeof Any;1260 input Integer n;1261 input list<ElementType> replacingLst;1262 input list<Integer> positionLst;1263 input list<ElementType> inLst;1264 output list<ElementType> outLst;1265 protected1266 Integer idx;1267 ElementType entry;1268 algorithm1269 idx := listGet(positionLst,n);1270 entry := listGet(replacingLst,n);1271 outLst := List.replaceAt(entry,idx-1,inLst);1272 end replaceAtPositionFromList;1273 1274 1275 protected function updateIndicesInComp " raises the indices of the vars and eqs in the given component according to the given offsets.1276 author: Waurich TUD 2013-09"1277 input BackendDAE.StrongComponent compIn;1278 input Integer varOffset;1279 input Integer eqOffset;1280 output BackendDAE.StrongComponent compOut;1281 algorithm1282 compOut := matchcontinue(compIn,varOffset,eqOffset)1283 local1284 Integer varIdx;1285 Integer eqIdx;1286 BackendDAE.StrongComponent compTmp;1287 case(BackendDAE.SINGLEEQUATION(eqn=eqIdx, var=varIdx),_,_)1288 equation1289 varIdx = varIdx+varOffset;1290 eqIdx = eqIdx+eqOffset;1291 compTmp = BackendDAE.SINGLEEQUATION(eqIdx, varIdx);1292 then1293 compTmp;1294 else1295 equation1296 print("updateVarEqIndices failed\n");1297 then1298 fail();1299 end matchcontinue;1300 end updateIndicesInComp;1301 1302 protected function buildNewResidualEquation "function to build the new linear residual equations res=0=A*xt+a0 whicht is solved for xt1303 author: Waurich TUD 2013-09"1304 input Integer resIdx;1305 input list<list<BackendDAE.Var>> aCoeffLst;1306 input list<BackendDAE.Var> a0CoeffLst;1307 input list<BackendDAE.Var> tvars;1308 input list<BackendDAE.Equation> resEqsIn;1309 output list<BackendDAE.Equation> resEqsOut;1310 algorithm1311 resEqsOut := matchcontinue(resIdx,aCoeffLst,a0CoeffLst,tvars,resEqsIn)1312 local1313 list<BackendDAE.Equation> eqLstTmp;1314 list<BackendDAE.Var> aCoeffs;1315 BackendDAE.Equation eqTmp, hs;1316 BackendDAE.Var a0Coeff;1317 DAE.Exp lhs, rhs, a0Exp;1318 DAE.Type ty;1319 case(_,_,_,_,_)1320 equation1321 true = resIdx > listLength(tvars);1322 eqLstTmp = listReverse(resEqsIn);1323 then1324 eqLstTmp;1325 case(_,_,_,_,_)1326 equation1327 true = resIdx <= listLength(tvars);1328 //aCoeffs = listGet(aCoeffLst,resIdx);1329 //aCoeffs = listReverse(aCoeffs);1330 aCoeffs = List.map1(aCoeffLst,listGet,resIdx);1331 a0Coeff = listGet(a0CoeffLst,resIdx);1332 a0Exp = varExp(a0Coeff);1333 ty = DAE.T_REAL_DEFAULT;1334 rhs = buildNewResidualEquation2(1,aCoeffs,tvars,DAE.RCONST(0.0)); // the start value is random and will be rejected1335 rhs = DAE.BINARY(rhs, DAE.ADD(ty), a0Exp);1336 lhs = DAE.RCONST(0.0);1337 hs = BackendDAE.EQUATION(lhs,rhs,DAE.emptyElementSource,false);1338 eqLstTmp = hs::resEqsIn;1339 eqLstTmp = buildNewResidualEquation(resIdx+1,aCoeffLst,a0CoeffLst,tvars,eqLstTmp);1340 then1341 eqLstTmp;1342 else1343 equation1344 print("buildNewResidualEquation failed");1345 then1346 fail();1347 end matchcontinue;1348 end buildNewResidualEquation;1349 1350 1351 protected function buildNewResidualEquation2 "function to build the sum of the rhs of the new residual equation, i.e. the sum of all tvars and their coefficients1352 author: Waurich TUD 2013-09"1353 input Integer idx;1354 input list<BackendDAE.Var> coeffs;1355 input list<BackendDAE.Var> tVars;1356 input DAE.Exp expIn;1357 output DAE.Exp expOut;1358 algorithm1359 expOut := matchcontinue(idx,coeffs,tVars,expIn)1360 local1361 BackendDAE.Var coeff;1362 BackendDAE.Var tVar;1363 DAE.Exp coeffExp, tVarExp, expTmp;1364 DAE.Type ty;1365 case(_,_,_,_)1366 equation1367 // the first product of the term1368 true = idx == 1;1369 coeff = listGet(coeffs,idx);1370 coeffExp = varExp(coeff);1371 tVar = listGet(tVars,idx);1372 tVarExp = varExp(tVar);1373 tVarExp = Debug.bcallret1(BackendVariable.isStateVar(tVar), Expression.expDer, tVarExp, tVarExp); // if tvar is a state, use the der(varexp)1374 ty = DAE.T_REAL_DEFAULT;1375 expTmp = DAE.BINARY(coeffExp,DAE.MUL(ty),tVarExp);1376 expTmp = buildNewResidualEquation2(idx+1,coeffs,tVars,expTmp);1377 then1378 expTmp;1379 case(_,_,_,_)1380 equation1381 true = idx <= listLength(tVars);1382 //extend the expression1383 coeff = listGet(coeffs,idx);1384 tVar = listGet(tVars,idx);1385 expTmp = addProductToExp(coeff,tVar,expIn);1386 expTmp = buildNewResidualEquation2(idx+1,coeffs,tVars,expTmp);1387 then1388 expTmp;1389 case(_,_,_,_)1390 equation1391 true = idx > listLength(tVars);1392 then1393 expIn;1394 else1395 equation1396 print("buildNewResidualEquation2 failed!\n");1397 then1398 fail();1399 end matchcontinue;1400 end buildNewResidualEquation2;1401 1402 1403 protected function addProductToExp " function to add the product of the given 2 BackendDAE.Var to the given inExp. expOut = expIn + fac1*fac21404 author: Waurich TUD 2013-09"1405 input BackendDAE.Var var1;1406 input BackendDAE.Var var2;1407 input DAE.Exp inExp;1408 output DAE.Exp expOut;1409 protected1410 DAE.Exp fac1, fac2, prod;1411 DAE.Type ty;1412 algorithm1413 fac1 := varExp(var1);1414 fac2 := varExp(var2);1415 fac2 := Debug.bcallret1(BackendVariable.isStateVar(var2), Expression.expDer, fac2, fac2);1416 ty := DAE.T_REAL_DEFAULT;1417 prod := DAE.BINARY(fac1, DAE.MUL(ty), fac2);1418 expOut := DAE.BINARY(inExp, DAE.ADD(ty), prod);1419 end addProductToExp;1420 1421 1422 protected function buildSingleEquationSystem "function to build a system of singleEquations which can be solved partially parallel, from an EquationArray and Variables.1423 author: Waurich TUD 2013-07"1424 input Integer eqSizeOrig;1425 input list<BackendDAE.Equation> inEqs;1426 input list<BackendDAE.Var> inVars;1427 input BackendDAE.Shared shared;1428 input BackendDAE.StrongComponents compsIn;1429 output BackendDAE.Matching matchingOut;1430 algorithm1431 matchingOut := matchcontinue(eqSizeOrig,inEqs,inVars,shared,compsIn)1432 local1433 array<list<Integer>> mapEqnIncRow;1434 array<Integer> ass1, ass2;1435 array<Integer> mapIncRowEqn;1436 Integer nVars, nEqs, compIdxTmp;1437 BackendDAE.EquationArray eqArr;1438 BackendDAE.EqSystem sysTmp;1439 BackendDAE.IncidenceMatrix m;1440 BackendDAE.IncidenceMatrixT mt;1441 BackendDAE.Matching matching, matchingTmp;1442 BackendDAE.StrongComponents compsTmp;1443 BackendDAE.Variables vars;1444 case(_,_,_,_,_)1445 equation1446 // build a singleEquation from a list<Equation> and list<Var> which are indexed by compIdx;1447 // get the EQSYSTEM, the incidenceMatrix and a matching1448 vars = BackendVariable.listVar1(inVars);1449 eqArr = BackendEquation.listEquation(inEqs);1450 sysTmp = BackendDAE.EQSYSTEM(vars,eqArr,NONE(),NONE(),BackendDAE.NO_MATCHING(),{});1451 (sysTmp,m,mt) = BackendDAEUtil.getIncidenceMatrix(sysTmp,BackendDAE.NORMAL(),NONE());1452 nVars = listLength(inVars);1453 nEqs = listLength(inEqs);1454 ass1 = arrayCreate(nVars, -1);1455 ass2 = arrayCreate(nEqs, -1);1456 Matching.matchingExternalsetIncidenceMatrix(nVars, nEqs, m);1457 BackendDAEEXT.matching(nVars, nEqs, 5, -1, 0.0, 1);1458 BackendDAEEXT.getAssignment(ass2, ass1);1459 matching = BackendDAE.MATCHING(ass1, ass2, {});1460 sysTmp = BackendDAE.EQSYSTEM(vars,eqArr,SOME(m),SOME(mt),matching,{});1461 1462 // perform BLT to order the StrongComponents1463 mapIncRowEqn = listArray(List.intRange(nEqs));1464 mapEqnIncRow = Util.arrayMap(mapIncRowEqn,List.create);1465 (sysTmp,compsTmp) = BackendDAETransform.strongComponentsScalar(sysTmp,shared,mapEqnIncRow,mapIncRowEqn);1466 sysTmp = BackendDAE.EQSYSTEM(vars,eqArr,SOME(m),SOME(mt),matching,{});1467 compsTmp = listAppend(compsIn,compsTmp);1468 matchingTmp = BackendDAE.MATCHING(ass1, ass2, compsTmp);1469 then1470 matchingTmp;1471 else1472 equation1473 print("buildSingleEquationSystem failed\n");1474 then1475 fail();1476 end matchcontinue;1477 end buildSingleEquationSystem;1478 1479 1480 protected function getTornSystemCoefficients "gets the co-efficients for the new residual equations of the linear torn system1481 the first index is for the residualvar and the second for the tearingvar1482 (r1) = (a11 a12..) (xt1)+(a01)1483 (r2) = (a21 a22..)*(xt2)+(a02)1484 (:) = (: : ) ( : )+( : )1485 this is meant to be a matrix :)1486 author: Waurich TUD 2013-08"1487 input list<Integer> iValueRange;1488 input Integer numTVars;1489 input list<list<BackendDAE.Var>> r_i_lstIn;1490 input list<list<BackendDAE.Equation>> hs_i_lstIn;1491 input list<list<BackendDAE.Var>> a_i_lstIn;1492 input Integer tornSysIdx;1493 output list<list<BackendDAE.Equation>> hs_i_lstOut;1494 output list<list<BackendDAE.Var>> a_i_lstOut;1495 algorithm1496 (hs_i_lstOut,a_i_lstOut) := matchcontinue(iValueRange, numTVars, r_i_lstIn, hs_i_lstIn, a_i_lstIn,tornSysIdx)1497 local1498 Integer iValue;1499 String varName;1500 list<Integer> iLstRest;1501 list<BackendDAE.Equation> hs_i;1502 list<BackendDAE.Var> a_i, r_i;1503 list<list<BackendDAE.Equation>> hs_i_lstTmp;1504 list<list<BackendDAE.Var>> a_i_lstTmp;1505 BackendDAE.Var aVar;1506 DAE.ComponentRef varCRef;1507 DAE.Exp varExp;1508 case({},_,_,_,_,_)1509 equation1510 //completed1511 hs_i_lstTmp = listDelete(hs_i_lstIn,0);1512 a_i_lstTmp = listDelete(a_i_lstIn,0);1513 hs_i_lstTmp = listReverse(hs_i_lstTmp);1514 a_i_lstTmp = listReverse(a_i_lstTmp);1515 then1516 (hs_i_lstTmp,a_i_lstTmp);1517 case(iValue::iLstRest,_,_,_,_,_)1518 equation1519 // gets the equations for computing the coefficients for the new residual equations1520 r_i = listGet(r_i_lstIn,iValue+1);1521 (hs_i_lstTmp,a_i_lstTmp) = getTornSystemCoefficients1(List.intRange(numTVars),iValue,r_i,hs_i_lstIn,a_i_lstIn,tornSysIdx);1522 //BackendDump.dumpVarList(listGet(a_i_lstTmp,1),"a_"+&intString(iValue)+&"\n");1523 //BackendDump.dumpEquationList(listGet(hs_i_lstTmp,1),"hs_"+&intString(iValue)+&"\n");1524 (hs_i_lstTmp,a_i_lstTmp) = getTornSystemCoefficients(iLstRest,numTVars,r_i_lstIn,{}::hs_i_lstTmp,{}::a_i_lstTmp,tornSysIdx);1525 then1526 (hs_i_lstTmp,a_i_lstTmp);1527 else1528 equation1529 print("getTornSystemCoefficients failed!\n");1530 then1531 fail();1532 end matchcontinue;1533 end getTornSystemCoefficients;1534 1535 1536 protected function getTornSystemCoefficients1 "gets the equations with coefficients for one e_i1537 author: Waurich TUD 2013-08"1538 input list<Integer> resIdxLst;1539 input Integer iIdx;1540 input list<BackendDAE.Var> resVal_iIn;1541 input list<list<BackendDAE.Equation>>hs_i_lstIn;1542 input list<list<BackendDAE.Var>> a_i_lstIn;1543 input Integer tornSysIdx;1544 output list<list<BackendDAE.Equation>> hs_i_lstOut;1545 output list<list<BackendDAE.Var>> a_i_lstOut;1546 algorithm1547 (hs_i_lstOut, a_i_lstOut) := matchcontinue(resIdxLst, iIdx, resVal_iIn, hs_i_lstIn, a_i_lstIn,tornSysIdx)1548 local1549 Integer resIdx,resIdx1;1550 String aName;1551 list<Integer> resIdxRest;1552 list<list<BackendDAE.Equation>> hs_i_lstTmp;1553 list<list<BackendDAE.Var>> a_i_lstTmp;1554 list<BackendDAE.Equation> hs_iTmp;1555 list<BackendDAE.Var> a_iTmp, d_lst;1556 BackendDAE.Equation hs_ii;1557 BackendDAE.Var a_ii, r_ii, dVar;1558 DAE.ComponentRef aCRef;1559 DAE.Exp aExp, lhs, rhs, dExp;1560 DAE.Type ty;1561 case({},_,_,_,_,_)1562 equation1563 //complete1564 //hs_i_lstTmp = listReverse(hs_i_lstIn);1565 //a_i_lstTmp = listReverse(a_i_lstIn);1566 then1567 (hs_i_lstIn,a_i_lstIn);1568 case(resIdx::resIdxRest,_,_,_,_,_)1569 equation1570 true = intEq(0,iIdx);1571 // build the coefficients (offset d=a_0) of the new residual equations (hs = A*xt+d)1572 aName = "$a_"+&intString(tornSysIdx)+&intString(iIdx)+&"_"+&intString(resIdx);1573 //aName = "$a_"+&intString(resIdx)+&"_"+&intString(iIdx);1574 ty = DAE.T_REAL_DEFAULT;1575 aCRef = ComponentReference.makeCrefIdent(aName,ty,{});1576 aExp = Expression.makeCrefExp(aCRef,ty);1577 a_ii = BackendDAE.VAR(aCRef,BackendDAE.VARIABLE(),DAE.BIDIR(),DAE.NON_PARALLEL(),ty,NONE(),NONE(),{},DAE.emptyElementSource,NONE(),NONE(),DAE.NON_CONNECTOR());1578 // build the equations to solve for the coefficients1579 resIdx1 = listLength(resVal_iIn)+1-resIdx;1580 r_ii = listGet(resVal_iIn,resIdx1);1581 lhs = varExp(r_ii);1582 rhs = varExp(a_ii);1583 hs_ii = BackendDAE.EQUATION(lhs,rhs,DAE.emptyElementSource,false);1584 // update th a_i_lst and the hs_i_lst1585 a_i_lstTmp = Debug.bcallret1(List.isEmpty(a_i_lstIn), List.create, {a_ii},varInFrontList(a_ii,a_i_lstIn));1586 hs_i_lstTmp = Debug.bcallret1(List.isEmpty(hs_i_lstIn), List.create, {hs_ii}, eqInFrontList(hs_ii,hs_i_lstIn));1587 //next residual equation1588 (hs_i_lstTmp,a_i_lstTmp) = getTornSystemCoefficients1(resIdxRest,iIdx,resVal_iIn,hs_i_lstTmp,a_i_lstTmp,tornSysIdx);1589 then1590 (hs_i_lstTmp, a_i_lstTmp);1591 case(resIdx::resIdxRest,_,_,_,_,_)1592 equation1593 true = iIdx > 0;1594 // build the co-efficients (A-matrix-entries) of the new residual equations (hs = A*xt+d)1595 //aName = "$a_"+&intString(iIdx)+&"_"+&intString(resIdx);1596 aName = "$a_"+&intString(tornSysIdx)+&intString(resIdx)+&"_"+&intString(iIdx);1597 ty = DAE.T_REAL_DEFAULT;1598 aCRef = ComponentReference.makeCrefIdent(aName,ty,{});1599 aExp = Expression.makeCrefExp(aCRef,ty);1600 a_ii = BackendDAE.VAR(aCRef,BackendDAE.VARIABLE(),DAE.BIDIR(),DAE.NON_PARALLEL(),ty,NONE(),NONE(),{},DAE.emptyElementSource,NONE(),NONE(),DAE.NON_CONNECTOR());1601 // build the equations to solve for the coefficients1602 resIdx1 = listLength(resVal_iIn)+1-resIdx;1603 d_lst = List.last(a_i_lstIn);1604 dVar = listGet(d_lst, resIdx1);1605 dExp = varExp(dVar);1606 aExp = varExp(a_ii);1607 rhs = DAE.BINARY(aExp,DAE.ADD(ty),dExp);1608 r_ii = listGet(resVal_iIn,resIdx1);1609 lhs = varExp(r_ii);1610 hs_ii = BackendDAE.EQUATION(lhs,rhs,DAE.emptyElementSource,false);1611 // update th a_i_lst and the hs_i_lst1612 a_i_lstTmp = Debug.bcallret1(List.isEmpty(a_i_lstIn), List.create, {a_ii},varInFrontList(a_ii, a_i_lstIn));1613 hs_i_lstTmp = Debug.bcallret1(List.isEmpty(hs_i_lstIn), List.create, {hs_ii}, eqInFrontList(hs_ii, hs_i_lstIn));1614 // next residual equation1615 (hs_i_lstTmp, a_i_lstTmp) = getTornSystemCoefficients1(resIdxRest, iIdx, resVal_iIn, hs_i_lstTmp, a_i_lstTmp,tornSysIdx);1616 then1617 (hs_i_lstTmp, a_i_lstTmp);1618 else1619 equation1620 print("getTornSystemCoefficients1 failed\n");1621 then1622 fail();1623 end matchcontinue;1624 end getTornSystemCoefficients1;1625 1626 1627 protected function varExp "gets an DAE.Exp for the CREF of the given BackendDAE.Var1628 author: Waurich TUD 2013-08"1629 input BackendDAE.Var varIn;1630 output DAE.Exp expOut;1631 protected1632 DAE.ComponentRef cr;1633 DAE.Type ty;1634 algorithm1635 ty := BackendVariable.varType(varIn);1636 cr := BackendVariable.varCref(varIn);1637 expOut := DAE.CREF(cr,ty);1638 end varExp;1639 1640 1641 protected function replaceVarsInResidualEquations "replaces the otherVars with xa_i and the tvars with e_i1642 author: Waurich TUD 2013-08"1643 input list<Integer> iValueRange;1644 input list<list<BackendDAE.Equation>> resEqsIn;1645 input list<BackendVarTransform.VariableReplacements> inReplLst;1646 input list<list<BackendDAE.Equation>> h_i_lstIn;1647 output list<list<BackendDAE.Equation>> h_i_lstOut;1648 algorithm1649 h_i_lstOut := matchcontinue(iValueRange,resEqsIn,inReplLst,h_i_lstIn)1650 local1651 Integer iValue;1652 list<Integer> iLstRest;1653 list<BackendDAE.Equation> h_i_Eqs;1654 BackendVarTransform.VariableReplacements repl;1655 list<list<BackendDAE.Equation>> h_i_lstTmp;1656 case({},_,_,_)1657 //completed1658 equation1659 h_i_lstTmp = listReverse(h_i_lstIn);1660 then1661 h_i_lstTmp;1662 case(iValue::iLstRest,_,_,_)1663 equation1664 iValue = iValue+1;1665 repl = listGet(inReplLst,iValue);1666 h_i_Eqs = listGet(resEqsIn,iValue);1667 (h_i_Eqs,_) = BackendVarTransform.replaceEquations(h_i_Eqs,repl,NONE());1668 h_i_lstTmp = replaceVarsInResidualEquations(iLstRest,resEqsIn,inReplLst,h_i_Eqs::h_i_lstIn);1669 then1670 h_i_lstTmp;1671 else1672 equation1673 print("replaceVarsInResidualEquations failed \n");1674 then1675 fail();1676 end matchcontinue;1677 end replaceVarsInResidualEquations;1678 1679 1680 protected function addResidualVarToEquation "adds a variable r_x to the right hand side of an equation. this corresponds to the residual value in a residual equation1681 author: Waurich TUD 2013-08"1682 input list<Integer> iIn;1683 input list<BackendDAE.Equation> resEqLstIn;1684 input list<list<BackendDAE.Equation>> h_i_lstIn;1685 input list<list<BackendDAE.Var>> r_i_lstIn;1686 input Integer tornSysIdx;1687 output list<list<BackendDAE.Equation>> h_i_lstOut;1688 output list<list<BackendDAE.Var>> r_i_lstOut;1689 algorithm1690 (h_i_lstOut,r_i_lstOut) := matchcontinue(iIn,resEqLstIn,h_i_lstIn,r_i_lstIn,tornSysIdx)1691 local1692 Integer iValue;1693 String resVarName;1694 list<Integer> eqIdxRange;1695 list<Integer> iLstRest;1696 list<list<BackendDAE.Equation>> h_i_lstTmp;1697 list<list<BackendDAE.Var>> r_i_lstTmp;1698 BackendDAE.Equation resEq;1699 DAE.Exp exp1;1700 DAE.Exp exp2;1701 DAE.Exp scalarExp;1702 case({},_,_,_,_)1703 // completed1704 equation1705 h_i_lstTmp = listDelete(h_i_lstIn,0);1706 r_i_lstTmp = listDelete(r_i_lstIn,0);1707 h_i_lstTmp = listReverse(h_i_lstTmp);1708 r_i_lstTmp = listReverse(r_i_lstTmp);1709 then1710 (h_i_lstTmp,r_i_lstTmp);1711 case(iValue::iLstRest,_,_,_,_)1712 // traverse the residualEquations1713 equation1714 eqIdxRange = List.intRange(listLength(resEqLstIn));1715 resVarName = "r_"+&intString(tornSysIdx)+&intString(iValue);1716 ((h_i_lstTmp,r_i_lstTmp)) = List.fold2(eqIdxRange,addResidualVarToEquation1,resEqLstIn,resVarName,(h_i_lstIn, r_i_lstIn));1717 //BackendDump.dumpVarList(listGet(r_i_lstTmp,1),"r_"+&intString(iValue)+&"\n");1718 //BackendDump.dumpEquationList(listGet(h_i_lstTmp,1),"h_"+&intString(iValue)+&"\n");1719 (h_i_lstTmp,r_i_lstTmp) = addResidualVarToEquation(iLstRest,resEqLstIn,{}::h_i_lstTmp,{}::r_i_lstTmp,tornSysIdx);1720 then1721 (h_i_lstTmp,r_i_lstTmp);1722 else1723 equation1724 print("addResidualVarToEquation failed! \n");1725 then1726 fail();1727 end matchcontinue;1728 end addResidualVarToEquation;1729 1730 1731 protected function addResidualVarToEquation1 "function to parse the expressions of one residualEquation. creates the residuumVars and updates the residual Expressions with them1732 author:Waurich TUD 2013-08 "1733 input Integer eqIdx;1734 input list<BackendDAE.Equation> resEqLstIn;1735 input String resVarName;1736 input tuple<list<list<BackendDAE.Equation>>,list<list<BackendDAE.Var>>> tplIn;1737 output tuple<list<list<BackendDAE.Equation>>,list<list<BackendDAE.Var>>> tplOut;1738 protected1739 String resName;1740 list<BackendDAE.Equation> resEqLst;1741 list<BackendDAE.Var> resVarLst;1742 list<list<BackendDAE.Equation>> h_i_lst;1743 list<list<BackendDAE.Var>> r_i_lst;1744 BackendDAE.Equation resEq;1745 BackendDAE.Var resVal;1746 DAE.ComponentRef resCRef;1747 DAE.Exp resExp;1748 DAE.Type ty;1749 algorithm1750 (h_i_lst,r_i_lst):= tplIn;1751 // add the variable for the residuumValue1752 resEq := listGet(resEqLstIn,eqIdx);1753 resName := "$"+&resVarName +&"_"+& intString(eqIdx);1754 ty := DAE.T_REAL_DEFAULT;1755 resCRef := ComponentReference.makeCrefIdent(resName,ty,{});1756 resExp := Expression.makeCrefExp(resCRef,ty);1757 resVal := BackendDAE.VAR(resCRef,BackendDAE.VARIABLE(),DAE.BIDIR(),DAE.NON_PARALLEL(),ty,NONE(),NONE(),{},DAE.emptyElementSource,NONE(),NONE(),DAE.NON_CONNECTOR());1758 (resEq,(resExp,_)) := BackendEquation.traverseBackendDAEExpsEqn(resEq,addResidualVarToEquation2,(resExp,false));1759 // update the resEq and resVar lists1760 r_i_lst := Debug.bcallret1(List.isEmpty(r_i_lst), List.create, {resVal},varInFrontList(resVal,r_i_lst));1761 h_i_lst := Debug.bcallret1(List.isEmpty(h_i_lst), List.create, {resEq}, eqInFrontList(resEq,h_i_lst));1762 tplOut := (h_i_lst,r_i_lst);1763 end addResidualVarToEquation1;1764 1765 1766 protected function varInFrontList " puts the varIn at the front of the first list of lists1767 author: Waurich TUD 2013-08"1768 input BackendDAE.Var varIn;1769 input list<list<BackendDAE.Var>> lstLstIn;1770 output list<list<BackendDAE.Var>> lstLstOut;1771 algorithm1772 lstLstOut := matchcontinue(varIn,lstLstIn)1773 local1774 list<BackendDAE.Var> varLst;1775 case(_,{})1776 then1777 lstLstIn;1778 case(_,_)1779 equation1780 varLst = List.first(lstLstIn);1781 varLst = varIn::varLst;1782 lstLstOut = List.replaceAt(varLst,0,lstLstIn);1783 then1784 lstLstOut;1785 end matchcontinue;1786 end varInFrontList;1787 1788 1789 protected function eqInFrontList " puts the eqIn at the front of the first list of lists1790 author: Waurich TUD 2013-08"1791 input BackendDAE.Equation eqIn;1792 input list<list<BackendDAE.Equation>> lstLstIn;1793 output list<list<BackendDAE.Equation>> lstLstOut;1794 algorithm1795 lstLstOut := matchcontinue(eqIn,lstLstIn)1796 local1797 list<BackendDAE.Equation> eqLst;1798 case(_,{})1799 then1800 lstLstIn;1801 case(_,_)1802 equation1803 eqLst = List.first(lstLstIn);1804 eqLst = eqIn::eqLst;1805 lstLstOut = List.replaceAt(eqLst,0,lstLstIn);1806 then1807 lstLstOut;1808 end matchcontinue;1809 end eqInFrontList;1810 1811 1812 protected function addResidualVarToEquation2 " adds the residual variable to the equation1813 author: waurich TUD 2013-08"1814 input tuple<DAE.Exp,tuple<DAE.Exp,Boolean>> tplIn;1815 output tuple<DAE.Exp,tuple<DAE.Exp,Boolean>> tplOut;1816 algorithm1817 tplOut := matchcontinue(tplIn)1818 local1819 Boolean rhs;1820 tuple<DAE.Exp,Boolean> tpl;1821 DAE.Exp exp1;1822 DAE.Exp exp2;1823 case(((exp1,(exp2,rhs))))1824 equation1825 true = rhs;1826 //print("rhs expression: "+&ExpressionDump.dumpExpStr(exp1,0)+&"\n");1827 //print("\n append with \n");1828 //print("residualValue: "+&ExpressionDump.dumpExpStr(exp2,0)+&"\n");1829 exp1 = Expression.expAdd(exp1,exp2);1830 then1831 ((exp1,(exp2,true)));1832 case(((exp1,(exp2,rhs))))1833 equation1834 false = rhs;1835 then1836 ((exp1,(exp2,true)));1837 end matchcontinue;1838 end addResidualVarToEquation2;1839 1840 1841 protected function getAlgebraicEquationsForEI "computes from otherEqs the equations to solve for xa_i by:1842 -replacing (i+1)-times in all otherEqs the tvars with i=0: all tvars=0, i=1: all tvars=0 but tvar{1}=1, i=2: all tvars=0 but tvar{2}=1 etc.1843 - replacing (i+1)-times in all otherEqs the otherVars(algebraic vars) with $Xai.cref in order to solve for them1844 author: Waurich TUD 2013-08"1845 input list<Integer> iIn;1846 input Integer size;1847 input list<BackendDAE.Equation> otherEqLstIn;1848 input list<BackendDAE.Var> tvarLstIn;1849 input list<DAE.ComponentRef> tVarCRefLstIn;1850 input list<BackendDAE.Var> otherVarLstIn;1851 input list<DAE.ComponentRef> oVarCRefLstIn;1852 input list<BackendVarTransform.VariableReplacements> replacementLstIn;1853 input list<list<BackendDAE.Equation>> g_i_lstIn;1854 input list<list<BackendDAE.Var>> xa_i_lstIn;1855 input Integer tornSysIdx;1856 output list<list<BackendDAE.Equation>> g_i_lstOut;1857 output list<list<BackendDAE.Var>> xa_i_lstOut;1858 output list<BackendVarTransform.VariableReplacements> replacementLstOut;1859 algorithm1860 (g_i_lstOut,xa_i_lstOut,replacementLstOut) := matchcontinue(iIn,size,otherEqLstIn,tvarLstIn,tVarCRefLstIn,otherVarLstIn,oVarCRefLstIn,replacementLstIn,g_i_lstIn,xa_i_lstIn,tornSysIdx)1861 local1862 Integer iValue;1863 String str1,str2;1864 list<Integer> iLstRest;1865 list<BackendDAE.Equation> gEqLstTmp;1866 list<BackendDAE.Var> xaVarLstTmp;1867 list<BackendVarTransform.VariableReplacements> replLstTmp;1868 list<DAE.ComponentRef> tVarCRefLst1;1869 list<list<BackendDAE.Equation>> g_i_lstTmp;1870 list<list<BackendDAE.Var>> xa_i_lstTmp;1871 BackendDAE.Var tvar;1872 BackendVarTransform.VariableReplacements replTmp;1873 DAE.ComponentRef tVarCRef;1874 case({},_,_,_,_,_,_,_,_,_,_)1875 // completed1876 equation1877 g_i_lstOut = listReverse(g_i_lstIn);1878 xa_i_lstOut = listReverse(xa_i_lstIn);1879 replacementLstOut = listReverse(replacementLstIn);1880 then1881 (g_i_lstOut,xa_i_lstOut,replacementLstOut);1882 1883 case(iValue::iLstRest,_,_,_,_,_,_,_,_,_,_)1884 // get xa_o from g_01885 equation1886 true = iValue == 0;1887 replTmp = BackendVarTransform.emptyReplacementsSized(size);1888 replTmp = List.fold1(tVarCRefLstIn,replaceTVarWithReal,0.0,replTmp);1889 ((xaVarLstTmp,replTmp)) = List.fold2(List.intRange(listLength(oVarCRefLstIn)),replaceOtherVarsWithPrefixCref,"$xa0"+&intString(tornSysIdx),oVarCRefLstIn,({},replTmp));1890 (gEqLstTmp,true) = BackendVarTransform.replaceEquations(otherEqLstIn,replTmp,NONE());1891 (g_i_lstTmp,xa_i_lstTmp,replLstTmp) = getAlgebraicEquationsForEI(iLstRest,size,otherEqLstIn,tvarLstIn,tVarCRefLstIn,otherVarLstIn,oVarCRefLstIn, replTmp::replacementLstIn, gEqLstTmp::g_i_lstIn,xaVarLstTmp::xa_i_lstIn, tornSysIdx);1892 then1893 (g_i_lstTmp,xa_i_lstTmp,replLstTmp);1894 1895 case(iValue::iLstRest,_,_,_,_,_,_,_,_,_,_)1896 // computes xa_i from g_i1897 equation1898 true = iValue > 0;1899 str1 = "$xa"+&intString(tornSysIdx)+&intString(iValue);1900 str2 = "$g"+&intString(tornSysIdx)+&intString(iValue);1901 tVarCRef = listGet(tVarCRefLstIn,iValue);1902 tVarCRefLst1 = listDelete(tVarCRefLstIn,iValue-1);1903 replTmp = BackendVarTransform.emptyReplacementsSized(size);1904 replTmp = replaceTVarWithReal(tVarCRef,1.0,replTmp);1905 replTmp = List.fold1(tVarCRefLst1,replaceTVarWithReal,0.0,replTmp);1906 ((xaVarLstTmp,replTmp)) = List.fold2(List.intRange(listLength(oVarCRefLstIn)),replaceOtherVarsWithPrefixCref,str1,oVarCRefLstIn,({},replTmp));1907 (gEqLstTmp,true) = BackendVarTransform.replaceEquations(otherEqLstIn,replTmp,NONE());1908 //BackendVarTransform.dumpReplacements(replTmp);1909 //BackendDump.dumpVarList(xaVarLstTmp,str1);1910 //BackendDump.dumpEquationList(gEqLstTmp,str2);1911 (g_i_lstTmp,xa_i_lstTmp,replLstTmp) = getAlgebraicEquationsForEI(iLstRest,size,otherEqLstIn,tvarLstIn,tVarCRefLstIn,otherVarLstIn,oVarCRefLstIn, replTmp::replacementLstIn, gEqLstTmp::g_i_lstIn,xaVarLstTmp::xa_i_lstIn,tornSysIdx);1912 then1913 (g_i_lstTmp,xa_i_lstTmp,replLstTmp);1914 1915 else1916 equation1917 print("getAlgebraicEquationsForEI1 failed!\n");1918 then1919 fail();1920 end matchcontinue;1921 end getAlgebraicEquationsForEI;1922 1923 1924 protected function replaceTVarWithReal "adds the replacement rule to set the tvar to realIn1925 author: Waurich TUD 2013-08"1926 input DAE.ComponentRef tVarCRefIn;1927 input Real realIn;1928 input BackendVarTransform.VariableReplacements replacementIn;1929 output BackendVarTransform.VariableReplacements replacementOut;1930 algorithm1931 replacementOut := BackendVarTransform.addReplacement(replacementIn,tVarCRefIn,DAE.RCONST(realIn),NONE());1932 end replaceTVarWithReal;1933 1934 1935 protected function replaceOtherVarsWithPrefixCref "adds the replacement rule to set the cref to $prefix.cref1936 author: Waurich TUD 2013-07"1937 input Integer indxIn;1938 input String prefix;1939 input list<DAE.ComponentRef> oVarCRefLstIn;1940 input tuple<list<BackendDAE.Var>,BackendVarTransform.VariableReplacements> tplIn;1941 output tuple<list<BackendDAE.Var>,BackendVarTransform.VariableReplacements> tplOut;1942 protected1943 list<BackendDAE.Var> replVarLstIn, replVarLstOut;1944 BackendDAE.Var replVar;1945 BackendVarTransform.VariableReplacements replacementIn,replacementOut;1946 DAE.ComponentRef cRef;1947 DAE.ComponentRef oVarCRef;1948 DAE.Exp varExp;1949 DAE.Type ty;1950 algorithm1951 (replVarLstIn,replacementIn) := tplIn;1952 oVarCRef := listGet(oVarCRefLstIn,indxIn);1953 cRef := ComponentReference.makeCrefQual(prefix,DAE.T_COMPLEX_DEFAULT,{},oVarCRef);1954 cRef := ComponentReference.replaceSubsWithString(cRef);1955 varExp := Expression.crefExp(cRef);1956 replacementOut := BackendVarTransform.addReplacement(replacementIn,oVarCRef,varExp,NONE());1957 ty := ComponentReference.crefLastType(cRef);1958 replVar := BackendDAE.VAR(cRef,BackendDAE.VARIABLE(),DAE.BIDIR(),DAE.NON_PARALLEL(),ty,NONE(),NONE(),{},DAE.emptyElementSource,NONE(),NONE(),DAE.NON_CONNECTOR());1959 replVarLstOut := replVar::replVarLstIn;1960 tplOut := (replVarLstOut,replacementOut);1961 end replaceOtherVarsWithPrefixCref;1962 1963 1964 protected function dumpVarLstLst "dumps a list<list<BackendDAE.Var>> as a String. TODO: remove when finished1965 author: Waurich TUD 2013-08"1966 input list<list<BackendDAE.Var>> inLstLst;1967 input String heading;1968 protected1969 String str;1970 algorithm1971 print("---------\n"+&heading+&"-variables\n---------\n");1972 str := List.fold1(List.intRange(listLength(inLstLst)),dumpVarLstLst1,inLstLst,heading);1973 end dumpVarLstLst;1974 1975 1976 protected function dumpVarLstLst1 "mapping function for dumpVarLstLst TODO: remove when finished1977 author: Waurich TUD 2013-08"1978 input Integer lstIdx;1979 input list<list<BackendDAE.Var>> inLstLst;1980 input String heading;1981 output String headingOut;1982 protected1983 String str1;1984 list<BackendDAE.Var> inLst;1985 algorithm1986 inLst := listGet(inLstLst,lstIdx);1987 str1 := heading+&"_"+&intString(lstIdx-1);1988 BackendDump.dumpVarList(inLst,str1);1989 headingOut := heading;1990 end dumpVarLstLst1;1991 1992 1993 protected function dumpEqLstLst "dumps a list<list<BackendDAE.Equation>> as a String. TODO: remove when finished1994 author: Waurich TUD 2013-08"1995 input list<list<BackendDAE.Equation>> inLstLst;1996 input String heading;1997 protected1998 String str;1999 algorithm2000 print("---------\n"+&heading+&"-equations\n---------\n");2001 str := List.fold1(List.intRange(listLength(inLstLst)),dumpEqLstLst1,inLstLst,heading);2002 end dumpEqLstLst;2003 2004 2005 protected function dumpEqLstLst1 "mapping function for dumpEqLstLst TODO: remove when finished2006 author: Waurich TUD 2013-08"2007 input Integer lstIdx;2008 input list<list<BackendDAE.Equation>> inLstLst;2009 input String heading;2010 output String headingOut;2011 protected2012 String str1;2013 list<BackendDAE.Equation> inLst;2014 algorithm2015 inLst := listGet(inLstLst,lstIdx);2016 str1 := heading+&"_"+&intString(lstIdx-1);2017 BackendDump.dumpEquationList(inLst,str1);2018 headingOut := heading;2019 end dumpEqLstLst1;2020 2021 2022 protected function printPartLinTornInfo "prints information about the partitioning of a linear torn system2023 author: Waurich TUD 2013-10"2024 input list<DAE.ComponentRef> tcrs;2025 input list<BackendDAE.Equation> reqns;2026 input list<BackendDAE.Equation> otherEqnsLst;2027 input list<DAE.ComponentRef> ovcrs;2028 input list<list<BackendDAE.Var>> xa_i_lst;2029 input list<list<BackendDAE.Equation>> g_i_lst;2030 input list<list<BackendDAE.Var>> r_i_lst;2031 input list<list<BackendDAE.Equation>> h_i_lst;2032 input list<list<BackendDAE.Var>> a_i_lst;2033 input list<list<BackendDAE.Equation>> hs_i_lst;2034 input list<BackendDAE.Equation> hs;2035 input BackendDAE.StrongComponents compsNew;2036 algorithm2037 print("disassemble a linear torn system\n");2038 print("tvars:\n");2039 print(ComponentReference.printComponentRefListStr(tcrs)+&"\n");2040 print("resEqs:\n");2041 BackendDump.printEquationList(reqns);2042 print("otherEqs:\n");2043 BackendDump.printEquationList(otherEqnsLst);2044 print("other vars:\n");2045 print(ComponentReference.printComponentRefListStr(ovcrs)+&"\n");2046 dumpVarLstLst(xa_i_lst,"xa");2047 dumpEqLstLst(g_i_lst,"g");2048 dumpVarLstLst(r_i_lst,"r");2049 dumpEqLstLst(h_i_lst,"h");2050 dumpVarLstLst(a_i_lst,"a");2051 dumpEqLstLst(hs_i_lst,"hs_i");2052 BackendDump.dumpEquationList(hs,"hs");2053 print("components to get the A-matrix\n");2054 BackendDump.dumpComponents(compsNew);2055 print("\n");2056 end printPartLinTornInfo;2057 2058 2059 //--------------------------------------------------//2060 // functions to simplify the generated equations2061 //-------------------------------------------------//2062 2063 protected function simplifyEquations "removes simpleEquations like a=0 and replaces variables from equations like a=b, without holding them in the knownvars or aliasvars2064 author: Waurich TUD 2013-10"2065 input list<list<BackendDAE.Equation>> eqLstLstIn;2066 input list<list<BackendDAE.Var>> varLstLstIn;2067 input BackendVarTransform.VariableReplacements replIn;2068 output list<list<BackendDAE.Equation>> eqLstLstOut;2069 output list<list<BackendDAE.Var>> varLstLstOut;2070 output BackendVarTransform.VariableReplacements replOut;2071 algorithm2072 ((eqLstLstOut,varLstLstOut,replOut)) := List.fold(List.intRange(listLength(eqLstLstIn)),simplifyEquations1,(eqLstLstIn,varLstLstIn,replIn));2073 //(eqLstLstOut,varLstLstOut,replOut) := (eqLstLstIn,varLstLstIn,replIn);2074 end simplifyEquations;2075 2076 2077 protected function simplifyEquations1 "implementation for simplifyEquations. traverses a list of the list<list<equation>>"2078 input Integer idx;2079 input tuple<list<list<BackendDAE.Equation>>,list<list<BackendDAE.Var>>,BackendVarTransform.VariableReplacements> tplIn;2080 output tuple<list<list<BackendDAE.Equation>>,list<list<BackendDAE.Var>>,BackendVarTransform.VariableReplacements> tplOut;2081 protected2082 BackendVarTransform.VariableReplacements repl;2083 list<BackendDAE.Equation> eqLst;2084 list<BackendDAE.Var> varLst;2085 list<list<BackendDAE.Equation>> eqLstLst;2086 list<list<BackendDAE.Var>> varLstLst;2087 algorithm2088 (eqLstLst,varLstLst,repl) := tplIn;2089 eqLst := listGet(eqLstLst,idx);2090 varLst := listGet(varLstLst,idx);2091 // remove all vars that are assigned to a constant or alias, remove the equations, update the replacement rule2092 ((eqLst,varLst,repl)) := simplifyEquations2((eqLst,varLst,repl));2093 eqLstLst := List.replaceAt(eqLst,idx-1,eqLstLst);2094 varLstLst := List.replaceAt(varLst,idx-1,varLstLst);2095 tplOut := ((eqLstLst,varLstLst,repl));2096 end simplifyEquations1;2097 2098 2099 protected function simplifyEquations2 "repeats the simplification until nothing changed anymore"2100 input tuple<list<BackendDAE.Equation>,list<BackendDAE.Var>,BackendVarTransform.VariableReplacements> tplIn;2101 output tuple<list<BackendDAE.Equation>,list<BackendDAE.Var>,BackendVarTransform.VariableReplacements> tplOut;2102 algorithm2103 tplOut := matchcontinue(tplIn)2104 local2105 Boolean changed;2106 BackendVarTransform.VariableReplacements repl, replIn;2107 list<BackendDAE.Equation> eqLst, eqLstIn;2108 list<BackendDAE.Var> varLst, varLstIn;2109 case((eqLstIn,varLstIn,replIn))2110 equation2111 ((varLst,eqLst,repl,changed)) = removeConstOrAlias(1,(varLstIn,eqLstIn,replIn,false));2112 true = changed;2113 (eqLst,_) = BackendVarTransform.replaceEquations(eqLst,repl,NONE());2114 ((eqLst,varLst,repl)) = simplifyEquations2((eqLst,varLst,repl));2115 then2116 ((eqLst,varLst,repl));2117 case((eqLstIn,varLstIn,replIn))2118 equation2119 ((varLst,eqLst,repl,changed)) = removeConstOrAlias(1,(varLstIn,eqLstIn,replIn,false));2120 false = changed;2121 (eqLst,_) = BackendVarTransform.replaceEquations(eqLst,repl,NONE());2122 then2123 ((eqLst,varLst,repl));2124 else2125 equation2126 print("simplifyEquations2 failed!\n");2127 then2128 fail();2129 end matchcontinue;2130 end simplifyEquations2;2131 2132 2133 protected function removeConstOrAlias "removes the equations and vars for which vars are assigned to constants or alias and replaces them.2134 author: Waurich TUD 2013-10"2135 input Integer eqIdx;2136 input tuple<list<BackendDAE.Var>, list<BackendDAE.Equation>, BackendVarTransform.VariableReplacements,Boolean> tplIn;2137 output tuple<list<BackendDAE.Var>, list<BackendDAE.Equation>, BackendVarTransform.VariableReplacements,Boolean> tplOut;2138 algorithm2139 tplOut := matchcontinue(eqIdx,tplIn)2140 local2141 Boolean b, changed, changed1;2142 Integer eqIdxTmp;2143 BackendDAE.Equation eq;2144 BackendDAE.Variables vars;2145 BackendVarTransform.VariableReplacements repl,replIn;2146 DAE.ComponentRef varCref;2147 DAE.Exp varExp, const, exp1, exp2;2148 list<BackendDAE.Equation> eqLst, eqLstIn;2149 list<BackendDAE.Var> varLst, varLstIn;2150 case(_,(varLstIn,eqLstIn,replIn,changed))2151 equation2152 true = listLength(eqLstIn) < eqIdx;2153 then2154 tplIn;2155 case(_,(varLstIn,eqLstIn,replIn,changed))2156 equation2157 //one side of the equation is constant2158 eq = listGet(eqLstIn,eqIdx);2159 (b,SOME((varExp,const))) = oneSideConstant(eq);2160 true = b;2161 //print("\neq with const: "+&BackendDump.dumpEqnsStr({eq})+&"\n");2162 //print("one side is constant\n");2163 (eqLst,varLst,repl,changed1) = handleConstantSide(varExp,const,eqIdx,eqLstIn,varLstIn,replIn,changed);2164 eqIdxTmp = Util.if_(changed1, eqIdx, eqIdx+1);2165 changed = changed1 or changed;2166 //print("changed? "+&boolString(changed)+&"\n");2167 ((varLst,eqLst,repl,changed)) = removeConstOrAlias(eqIdxTmp,(varLst,eqLst,repl,changed));2168 then2169 ((varLst,eqLst,repl,changed));2170 case(_,(varLstIn,eqLstIn,replIn,changed))2171 equation2172 //both sides of the equation are crefs2173 eq = listGet(eqLstIn,eqIdx);2174 BackendDAE.EQUATION(exp= exp1, scalar=exp2) = eq;2175 true = Expression.isCref(exp1);2176 true = Expression.isCref(exp2);2177 //print("\neq with both sides CREFs: "+&BackendDump.dumpEqnsStr({eq})+&"\n");2178 (eqLst,varLst,repl,changed1) = checkForPosAlias(exp1,exp2,eqIdx,eqLstIn,varLstIn,replIn);2179 eqIdxTmp = Util.if_(changed1, eqIdx, eqIdx+1);2180 changed = changed1 or changed;2181 //print("changed? "+&boolString(changed)+&"\n");2182 ((varLst,eqLst,repl,changed)) = removeConstOrAlias(eqIdxTmp,(varLst,eqLst,repl,changed));2183 then2184 ((varLst,eqLst,repl,changed));2185 case(_,(varLstIn,eqLstIn,replIn,changed))2186 equation2187 // nothing to simplify. go to next equation2188 //print("nothing to simplify\n");2189 //print("changed? "+&boolString(changed)+&"\n");2190 ((varLst,eqLst,repl,changed)) = removeConstOrAlias(eqIdx+1,(varLstIn,eqLstIn,replIn,changed));2191 then2192 ((varLst,eqLst,repl,changed));2193 else2194 equation2195 print("removeConstOrAlias failed!\n");2196 then2197 fail();2198 end matchcontinue;2199 end removeConstOrAlias;2200 2201 2202 protected function handleConstantSide "checks if the equation is a constant assignment i.e. a=const, const=a or a simple equation i.e. a+b=02203 author: Waurich TUD 2013-10"2204 input DAE.Exp varExp;2205 input DAE.Exp constExp;2206 input Integer eqIdx;2207 input list<BackendDAE.Equation> eqLstIn;2208 input list<BackendDAE.Var> varLstIn;2209 input BackendVarTransform.VariableReplacements replIn;2210 input Boolean changedIn;2211 output list<BackendDAE.Equation> eqLstOut;2212 output list<BackendDAE.Var> varLstOut;2213 output BackendVarTransform.VariableReplacements replOut;2214 output Boolean changedOut;2215 algorithm2216 (eqLstOut,varLstOut,replOut,changedOut) := matchcontinue(varExp,constExp,eqIdx,eqLstIn,varLstIn,replIn,changedIn)2217 local2218 Boolean changed;2219 Real real;2220 BackendDAE.Variables vars;2221 BackendVarTransform.VariableReplacements repl;2222 DAE.ComponentRef cref;2223 DAE.Exp exp1,exp2;2224 DAE.Operator op;2225 list<BackendDAE.Equation> eqLst;2226 list<BackendDAE.Var> varLst;2227 case(DAE.CREF(componentRef = cref),DAE.RCONST(real=real),_,_,_,_,_)2228 equation2229 // check for simple equations: a = const.2230 vars = BackendVariable.listVar(varLstIn);2231 vars = BackendVariable.removeCref(cref,vars);2232 varLst = BackendVariable.varList(vars);2233 eqLst = listDelete(eqLstIn, eqIdx-1);2234 repl = BackendVarTransform.addReplacement(replIn,cref,constExp,NONE());2235 then2236 (eqLst,varLst,repl,true);2237 case(DAE.BINARY(exp1=exp1,operator=op,exp2=exp2),DAE.RCONST(real=real),_,_,_,_,_)2238 equation2239 // check for alias vars: a+-b = 0.02240 true = real ==. 0.0;2241 (eqLst,varLst,repl,changed) = checkForAlias(exp1,exp2,op,eqIdx,eqLstIn,varLstIn,replIn);2242 then2243 (eqLst,varLst,repl,changed);2244 else2245 equation2246 then2247 (eqLstIn,varLstIn,replIn,false);2248 end matchcontinue;2249 end handleConstantSide;2250 2251 2252 protected function checkForAlias "checks if the there are alias variables for the new vars from varLstIn.2253 author: Waurich TUD 2013-10"2254 input DAE.Exp exp1;2255 input DAE.Exp exp2;2256 input DAE.Operator op;2257 input Integer eqIdx;2258 input list<BackendDAE.Equation> eqLstIn;2259 input list<BackendDAE.Var> varLstIn;2260 input BackendVarTransform.VariableReplacements replIn;2261 output list<BackendDAE.Equation> eqLstOut;2262 output list<BackendDAE.Var> varLstOut;2263 output BackendVarTransform.VariableReplacements replOut;2264 output Boolean changedOut;2265 algorithm2266 (eqLstOut,varLstOut,replOut,changedOut) := match(exp1,exp2,op,eqIdx,eqLstIn,varLstIn,replIn)2267 local2268 Boolean changed;2269 BackendDAE.Variables vars;2270 BackendVarTransform.VariableReplacements repl;2271 DAE.ComponentRef cref1,cref2;2272 list<BackendDAE.Equation> eqLst;2273 list<BackendDAE.Var> varLst;2274 DAE.Exp newExp, unaryExp;2275 case(_,_,DAE.ADD(_),_,_,_,_)2276 equation2277 true = Expression.isCref(exp1);2278 true = Expression.isCref(exp2);2279 // a + otherVar = 0 replace: a--> -otherVar or a + b = 0 replace: a --> -b2280 (eqLst,varLst,repl,changed) = checkForNegAlias(exp1,exp2,eqIdx,eqLstIn,varLstIn,replIn);2281 then2282 (eqLst,varLst,repl,changed);2283 case(_,_,DAE.SUB(_),_,_,_,_)2284 equation2285 true = Expression.isCref(exp1);2286 true = Expression.isCref(exp2);2287 // a - otherVar = 0 replace: a--> otherVar or a - b = 0 replace: a --> b2288 (eqLst,varLst,repl,changed) = checkForPosAlias(exp1,exp2,eqIdx,eqLstIn,varLstIn,replIn);2289 then2290 (eqLst,varLst,repl,changed);2291 case((DAE.CREF(componentRef = cref1)),_,DAE.DIV(_),_,_,_,_)2292 equation2293 // a/otherVar = 0 replace: a --> 02294 vars = BackendVariable.listVar(varLstIn);2295 true = BackendVariable.existsVar(cref1,vars,false);2296 vars = BackendVariable.removeCref(cref1,vars);2297 varLst = BackendVariable.varList(vars);2298 eqLst = listDelete(eqLstIn, eqIdx-1);2299 newExp = DAE.RCONST(0.0);2300 repl = BackendVarTransform.addReplacement(replIn,cref1,newExp,NONE());2301 then2302 (eqLst,varLst,repl,true);2303 case((DAE.UNARY(operator=_,exp=unaryExp)),_,DAE.DIV(_),_,_,_,_)2304 equation2305 true = Expression.isCref(unaryExp);2306 DAE.CREF(componentRef = cref1) = unaryExp;2307 // a/otherVar = 0 replace: a --> 02308 vars = BackendVariable.listVar(varLstIn);2309 true = BackendVariable.existsVar(cref1,vars,false);2310 vars = BackendVariable.removeCref(cref1,vars);2311 varLst = BackendVariable.varList(vars);2312 eqLst = listDelete(eqLstIn, eqIdx-1);2313 newExp = DAE.RCONST(0.0);2314 repl = BackendVarTransform.addReplacement(replIn,cref1,newExp,NONE());2315 then2316 (eqLst,varLst,repl,true);2317 else2318 equation2319 then2320 (eqLstIn,varLstIn,replIn,false);2321 end match;2322 end checkForAlias;2323 2324 2325 protected function checkForNegAlias "checks if there are variables that can be replaced from a+otherVar = 0 to a --> -otherVar. a has to be in the varLstIn2326 author: Waurich TUD 2013-10"2327 input DAE.Exp exp1;2328 input DAE.Exp exp2;2329 input Integer eqIdx;2330 input list<BackendDAE.Equation> eqLstIn;2331 input list<BackendDAE.Var> varLstIn;2332 input BackendVarTransform.VariableReplacements replIn;2333 output list<BackendDAE.Equation> eqLstOut;2334 output list<BackendDAE.Var> varLstOut;2335 output BackendVarTransform.VariableReplacements replOut;2336 output Boolean changed;2337 algorithm2338 (eqLstOut,varLstOut,replOut,changed) := matchcontinue(exp1,exp2,eqIdx,eqLstIn,varLstIn,replIn)2339 local2340 BackendDAE.Variables vars;2341 BackendVarTransform.VariableReplacements repl;2342 DAE.ComponentRef cref1,cref2;2343 DAE.Exp newExp;2344 list<BackendDAE.Equation> eqLst;2345 list<BackendDAE.Var> varLst;2346 case(DAE.CREF(componentRef = cref1),DAE.CREF(componentRef = cref2),_,_,_,_)2347 equation2348 // a + otherVar = 0 replace: a --> -otherVar or a + b = 0 replace: a --> -b2349 vars = BackendVariable.listVar(varLstIn);2350 true = BackendVariable.existsVar(cref1,vars,false);2351 vars = BackendVariable.removeCref(cref1,vars);2352 varLst = BackendVariable.varList(vars);2353 eqLst = listDelete(eqLstIn, eqIdx-1);2354 newExp = DAE.UNARY(DAE.UMINUS(DAE.T_REAL_DEFAULT),exp2);2355 //print("checkForNegAlias: replace "+&ComponentReference.printComponentRefStr(cref2)+&" with "+&ExpressionDump.printExpStr(newExp)+&"\n");2356 repl = BackendVarTransform.addReplacement(replIn,cref1,newExp,NONE());2357 then2358 (eqLst,varLst,repl,true);2359 case(DAE.CREF(componentRef = cref1),DAE.CREF(componentRef = cref2),_,_,_,_)2360 equation2361 // otherVar + a = 0 replace: a --> -otherVar2362 vars = BackendVariable.listVar(varLstIn);2363 true = BackendVariable.existsVar(cref2,vars,false);2364 vars = BackendVariable.removeCref(cref2,vars);2365 varLst = BackendVariable.varList(vars);2366 eqLst = listDelete(eqLstIn, eqIdx-1);2367 newExp = DAE.UNARY(DAE.UMINUS(DAE.T_REAL_DEFAULT),exp1);2368 //print("checkForNegAlias: replace "+&ComponentReference.printComponentRefStr(cref2)+&" with - "+&ExpressionDump.printExpStr(newExp)+&"\n");2369 repl = BackendVarTransform.addReplacement(replIn,cref2,newExp,NONE());2370 then2371 (eqLst,varLst,repl,true);2372 else2373 equation2374 then2375 (eqLstIn,varLstIn,replIn,false);2376 end matchcontinue;2377 end checkForNegAlias;2378 2379 2380 protected function checkForPosAlias"checks if there are variables that can be replaced from a-otherVar = 0 to a --> otherVar. a has to be in the varLstIn2381 author: Waurich TUD 2013-10"2382 input DAE.Exp exp1;2383 input DAE.Exp exp2;2384 input Integer eqIdx;2385 input list<BackendDAE.Equation> eqLstIn;2386 input list<BackendDAE.Var> varLstIn;2387 input BackendVarTransform.VariableReplacements replIn;2388 output list<BackendDAE.Equation> eqLstOut;2389 output list<BackendDAE.Var> varLstOut;2390 output BackendVarTransform.VariableReplacements replOut;2391 output Boolean changed;2392 algorithm2393 (eqLstOut,varLstOut,replOut,changed) := matchcontinue(exp1,exp2,eqIdx,eqLstIn,varLstIn,replIn)2394 local2395 BackendDAE.Variables vars;2396 BackendVarTransform.VariableReplacements repl;2397 DAE.ComponentRef cref1,cref2;2398 list<BackendDAE.Equation> eqLst;2399 list<BackendDAE.Var> varLst;2400 case(DAE.CREF(componentRef = cref1),DAE.CREF(componentRef = cref2),_,_,_,_)2401 equation2402 // a - otherVar = 0 replace: a --> otherVar or a - b = 0 replace: a --> b2403 vars = BackendVariable.listVar(varLstIn);2404 true = BackendVariable.existsVar(cref1,vars,false);2405 //print("checkForPosAlias: replace "+&ComponentReference.printComponentRefStr(cref1)+&" with "+&ExpressionDump.printExpStr(exp2)+&"\n");2406 vars = BackendVariable.removeCref(cref1,vars);2407 varLst = BackendVariable.varList(vars);2408 eqLst = listDelete(eqLstIn, eqIdx-1);2409 repl = BackendVarTransform.addReplacement(replIn,cref1,exp2,NONE());2410 then2411 (eqLst,varLst,repl,true);2412 case(DAE.CREF(componentRef = cref1),DAE.CREF(componentRef = cref2),_,_,_,_)2413 equation2414 // otherVar - a = 0 replace: a --> otherVar2415 vars = BackendVariable.listVar(varLstIn);2416 true = BackendVariable.existsVar(cref2,vars,false);2417 //print("checkForPosAlias: replace "+&ComponentReference.printComponentRefStr(cref2)+&" with "+&ExpressionDump.printExpStr(exp1)+&"\n");2418 vars = BackendVariable.removeCref(cref2,vars);2419 varLst = BackendVariable.varList(vars);2420 eqLst = listDelete(eqLstIn, eqIdx-1);2421 repl = BackendVarTransform.addReplacement(replIn,cref2,exp1,NONE());2422 then2423 (eqLst,varLst,repl,true);2424 else2425 equation2426 then2427 (eqLstIn,varLstIn,replIn,false);2428 end matchcontinue;2429 end checkForPosAlias;2430 2431 2432 protected function oneSideConstant "checks whether the given equation has one side with a constant.2433 author: Waurich TUD 2013-07"2434 input BackendDAE.Equation eqIn;2435 output Boolean hasConst;2436 output Option<tuple<DAE.Exp,DAE.Exp>> varExp; //<non-constant side, constant side>2437 algorithm2438 (hasConst,varExp) := matchcontinue(eqIn)2439 local2440 DAE.Exp lhs,rhs;2441 case(BackendDAE.EQUATION(exp = rhs, scalar = lhs))2442 equation2443 true = Expression.isConst(lhs);2444 then2445 (true,SOME((rhs,lhs)));2446 case(BackendDAE.EQUATION(exp = rhs, scalar = lhs))2447 equation2448 true = Expression.isConst(rhs);2449 then2450 (true,SOME((lhs,rhs)));2451 else2452 equation2453 then2454 (false,NONE());2455 end matchcontinue;2456 end oneSideConstant;2457 2458 //--------------------------------------------------//2459 //2460 //-------------------------------------------------//2461 2462 803 end HpcOmSimCode; -
Compiler/BackEnd/SimCodeMain.mo
rc44e86b r278ccf63 74 74 protected import Error; 75 75 protected import Flags; 76 protected import HpcOmEqSystems; 76 77 protected import HpcOmSimCode; 77 78 protected import Settings; … … 394 395 true = numProc == 0; 395 396 print("hpcom computes the ideal number of processors. If you want to set the number manually, use the flag +n=_ \n"); 396 backendDAE2 = Debug.fcallret3(Flags.PARTLINTORNSYSTEM, HpcOm SimCode.traverseEqSystemsWithIndex, 1,1, inBackendDAE, inBackendDAE);397 backendDAE2 = Debug.fcallret3(Flags.PARTLINTORNSYSTEM, HpcOmEqSystems.traverseEqSystemsWithIndex, 1,1, inBackendDAE, inBackendDAE); 397 398 then HpcOmSimCode.createSimCode(backendDAE2, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, simSettingsOpt, recordDecls, literals, args); 398 399 … … 401 402 numProc = Flags.getConfigInt(Flags.NUM_PROC); 402 403 true = (numProc > 0); 403 backendDAE2 = Debug.fcallret3(Flags.PARTLINTORNSYSTEM, HpcOm SimCode.traverseEqSystemsWithIndex, 1,1, inBackendDAE, inBackendDAE);404 backendDAE2 = Debug.fcallret3(Flags.PARTLINTORNSYSTEM, HpcOmEqSystems.traverseEqSystemsWithIndex, 1,1, inBackendDAE, inBackendDAE); 404 405 then HpcOmSimCode.createSimCode(backendDAE2, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, simSettingsOpt, recordDecls, literals, args); 405 406 406 407 else equation 407 backendDAE2 = Debug.fcallret3(Flags.PARTLINTORNSYSTEM, HpcOm SimCode.traverseEqSystemsWithIndex, 1,1, inBackendDAE, inBackendDAE);408 backendDAE2 = Debug.fcallret3(Flags.PARTLINTORNSYSTEM, HpcOmEqSystems.traverseEqSystemsWithIndex, 1,1, inBackendDAE, inBackendDAE); 408 409 (tmpSimCode, _) = SimCodeUtil.createSimCode(backendDAE2, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, simSettingsOpt, recordDecls, literals, args); 409 410 then tmpSimCode; -
Compiler/Makefile.common
rf43cca0 r278ccf63 158 158 HashTableCrSimVars.mo \ 159 159 HpcOmBenchmark.mo \ 160 HpcOmEqSystems.mo \ 160 161 HpcOmScheduler.mo \ 161 162 HpcOmSimCode.mo \
Note: See TracChangeset
for help on using the changeset viewer.