diff --git a/Compiler/FrontEnd/DAE.mo b/Compiler/FrontEnd/DAE.mo index fb9616f5f3..2564a1f12b 100644 --- a/Compiler/FrontEnd/DAE.mo +++ b/Compiler/FrontEnd/DAE.mo @@ -1345,17 +1345,17 @@ uniontype Exp "Expressions Exp exp; end LUNARY; - record RELATION "Relation, e.g. a <= 0 - Index contains normal an Integer for every ZeroCrossing - but if Relation is in algorithm with for loop the iterator and the range - of static iterator is needed for codegen" + record RELATION "Relation, e.g. a <= 0" Exp exp1; Operator operator; Exp exp2; - Integer index; - Option> optionExpisASUB; end RELATION; + record ZEROCROSSING "Backend expression for zero crossing" + Exp exp; + Integer index; + end ZEROCROSSING; + record IFEXP "If expressions" Exp expCond; Exp expThen; diff --git a/Compiler/Template/CodegenAdevs.tpl b/Compiler/Template/CodegenAdevs.tpl index 1026c77717..05db57b3df 100644 --- a/Compiler/Template/CodegenAdevs.tpl +++ b/Compiler/Template/CodegenAdevs.tpl @@ -3611,6 +3611,7 @@ template daeExp(Exp exp, Context context, Text &preExp /*BUFP*/, Text &varDecls case e as LBINARY(__) then daeExpLbinary(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/) case e as LUNARY(__) then daeExpLunary(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/) case e as RELATION(__) then daeExpRelation(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/) + case e as ZEROCROSSING(__) then daeExpZeroCrossingSim(e, index, context, &preExp /*BUFC*/, &varDecls /*BUFD*/) case e as IFEXP(__) then daeExpIf(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/) case e as CALL(__) then daeExpCall(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/) case e as ARRAY(__) then daeExpArray(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/) @@ -3968,23 +3969,22 @@ case rel as RELATION(__) then else "daeExpRelation:ERR" end daeExpRelation; - -template daeExpRelationSim(Exp exp, Context context, Text &preExp /*BUFP*/, +template daeExpZeroCrossingSim(Exp exp, Integer index, Context context, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/) "Helper to daeExpRelation." ::= match exp -case rel as RELATION(__) then +case zc as ZEROCROSSING(__) then match context case SIMULATION_CONTEXT(genDiscrete=false) then - match rel.optionExpisASUB - case NONE() then - let e1 = daeExp(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - let e2 = daeExp(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - let res = tempDecl("modelica_boolean", &varDecls /*BUFC*/) - match rel.operator + match zc.exp + case rel as RELATION(__) + let e1 = daeExp(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) + let e2 = daeExp(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) + let res = tempDecl("modelica_boolean", &varDecls /*BUFC*/) + match rel.operator case LESS(__) then - let &preExp += 'ADEVS_RELATIONTOZC(<%res%>, <%e1%>, <%e2%>, <%rel.index%>,<);<%\n%>' + let &preExp += 'ADEVS_RELATIONTOZC(<%res%>, <%e1%>, <%e2%>, <%.index%>,<);<%\n%>' res case LESSEQ(__) then let &preExp += 'ADEVS_RELATIONTOZC(<%res%>, <%e1%>, <%e2%>, <%rel.index%>,<=);<%\n%>' diff --git a/Compiler/Template/CodegenCFunctions.tpl b/Compiler/Template/CodegenCFunctions.tpl index db313ebdfa..db8ec1bd22 100644 --- a/Compiler/Template/CodegenCFunctions.tpl +++ b/Compiler/Template/CodegenCFunctions.tpl @@ -4323,6 +4323,7 @@ end getTempDeclMatchOutputName; case e as LBINARY(__) then daeExpLbinary(e, context, &preExp, &varDecls, &auxFunction) case e as LUNARY(__) then daeExpLunary(e, context, &preExp, &varDecls, &auxFunction) case e as RELATION(__) then daeExpRelation(e, context, &preExp, &varDecls, &auxFunction) + case e as ZEROCROSSING(__) then daeExpZeroCrossing(e, context, &preExp, &varDecls, &auxFunction) case e as IFEXP(__) then daeExpIf(e, context, &preExp, &varDecls, &auxFunction) case e as CALL(__) then daeExpCall(e, context, &preExp, &varDecls, &auxFunction) case e as RECORD(__) then daeExpRecord(e, context, &preExp, &varDecls, &auxFunction) @@ -5132,15 +5133,6 @@ template daeExpRelation(Exp exp, Context context, Text &preExp, ::= match exp case rel as RELATION(__) then - let &varDecls2 = buffer "" - let &preExp2 = buffer "" - let simRel = daeExpRelationSim(rel, context, &preExp2, &varDecls2, &auxFunction) - if simRel then - /* Don't add the allocated temp-var unless it is used */ - let &varDecls += varDecls2 - let &preExp += preExp2 - simRel - else let e1 = daeExp(rel.exp1, context, &preExp, &varDecls, &auxFunction) let e2 = daeExp(rel.exp2, context, &preExp, &varDecls, &auxFunction) match rel.operator @@ -5185,175 +5177,21 @@ case rel as RELATION(__) then else error(sourceInfo(), 'daeExpRelation <%ExpressionDumpTpl.dumpExp(exp,"\"")%>') end daeExpRelation; - - -template daeExpRelationSim(Exp exp, Context context, Text &preExp, +template daeExpZeroCrossing(Exp exp, Context context, Text &preExp, Text &varDecls, Text &auxFunction) "Helper to daeExpRelation." ::= match exp -case rel as RELATION(__) then +case zc as ZEROCROSSING(__) then match context case SIMULATION_CONTEXT(__) then - match rel.optionExpisASUB - case NONE() then - let e1 = daeExp(rel.exp1, context, &preExp, &varDecls, &auxFunction) - let e2 = daeExp(rel.exp2, context, &preExp, &varDecls, &auxFunction) - let res = tempDecl("modelica_boolean", &varDecls) - if intEq(rel.index,-1) then - match rel.operator - case LESS(__) then - let &preExp += '<%res%> = Less(<%e1%>,<%e2%>);<%\n%>' - res - case LESSEQ(__) then - let &preExp += '<%res%> = LessEq(<%e1%>,<%e2%>);<%\n%>' - res - case GREATER(__) then - let &preExp += '<%res%> = Greater(<%e1%>,<%e2%>);<%\n%>' - res - case GREATEREQ(__) then - let &preExp += '<%res%> = GreaterEq(<%e1%>,<%e2%>);<%\n%>' - res - end match - else - let isReal = if isRealType(typeof(rel.exp1)) then (if isRealType(typeof(rel.exp2)) then 'true' else '') else '' - let hysteresisfunction = if isReal then 'RELATIONHYSTERESIS' else 'RELATION' - match rel.operator - case LESS(__) then - let &preExp += '<%hysteresisfunction%>(<%res%>, <%e1%>, <%e2%>, <%rel.index%>, Less);<%\n%>' - res - case LESSEQ(__) then - let &preExp += '<%hysteresisfunction%>(<%res%>, <%e1%>, <%e2%>, <%rel.index%>, LessEq);<%\n%>' - res - case GREATER(__) then - let &preExp += '<%hysteresisfunction%>(<%res%>, <%e1%>, <%e2%>, <%rel.index%>, Greater);<%\n%>' - res - case GREATEREQ(__) then - let &preExp += '<%hysteresisfunction%>(<%res%>, <%e1%>, <%e2%>, <%rel.index%>, GreaterEq);<%\n%>' - res - end match - case SOME((exp,i,j)) then - let e1 = daeExp(rel.exp1, context, &preExp, &varDecls, &auxFunction) - let e2 = daeExp(rel.exp2, context, &preExp, &varDecls, &auxFunction) - let iterator = daeExp(exp, context, &preExp, &varDecls, &auxFunction) - let res = tempDecl("modelica_boolean", &varDecls) - if intEq(rel.index,-1) then - match rel.operator - case LESS(__) then - let &preExp += '<%res%> = Less(<%e1%>,<%e2%>);<%\n%>' - res - case LESSEQ(__) then - let &preExp += '<%res%> = LessEq(<%e1%>,<%e2%>);<%\n%>' - res - case GREATER(__) then - let &preExp += '<%res%> = Greater(<%e1%>,<%e2%>);<%\n%>' - res - case GREATEREQ(__) then - let &preExp += '<%res%> = GreaterEq(<%e1%>,<%e2%>);<%\n%>' - res - end match - else - let isReal = if isRealType(typeof(rel.exp1)) then (if isRealType(typeof(rel.exp2)) then 'true' else '') else '' - let hysteresisfunction = if isReal then 'RELATIONHYSTERESIS' else 'RELATION' - match rel.operator - case LESS(__) then - let &preExp += '<%hysteresisfunction%>(<%res%>, <%e1%>, <%e2%>, <%rel.index%> + (<%iterator%> - <%i%>)/<%j%>, Less);<%\n%>' - res - case LESSEQ(__) then - let &preExp += '<%hysteresisfunction%>(<%res%>, <%e1%>, <%e2%>, <%rel.index%> + (<%iterator%> - <%i%>)/<%j%>, LessEq);<%\n%>' - res - case GREATER(__) then - let &preExp += '<%hysteresisfunction%>(<%res%>, <%e1%>, <%e2%>, <%rel.index%> + (<%iterator%> - <%i%>)/<%j%>, Greater);<%\n%>' - res - case GREATEREQ(__) then - let &preExp += '<%hysteresisfunction%>(<%res%>, <%e1%>, <%e2%>, <%rel.index%> + (<%iterator%> - <%i%>)/<%j%>, GreaterEq);<%\n%>' - res - end match - end match - case ZEROCROSSINGS_CONTEXT(__) then - match rel.optionExpisASUB - case NONE() then - let e1 = daeExp(rel.exp1, context, &preExp, &varDecls, &auxFunction) - let e2 = daeExp(rel.exp2, context, &preExp, &varDecls, &auxFunction) - let res = tempDecl("modelica_boolean", &varDecls) - if intEq(rel.index,-1) then - match rel.operator - case LESS(__) then - let &preExp += '<%res%> = Less(<%e1%>,<%e2%>);<%\n%>' - res - case LESSEQ(__) then - let &preExp += '<%res%> = LessEq(<%e1%>,<%e2%>);<%\n%>' - res - case GREATER(__) then - let &preExp += '<%res%> = Greater(<%e1%>,<%e2%>);<%\n%>' - res - case GREATEREQ(__) then - let &preExp += '<%res%> = GreaterEq(<%e1%>,<%e2%>);<%\n%>' - res - end match - else - let isReal = if isRealType(typeof(rel.exp1)) then (if isRealType(typeof(rel.exp2)) then 'true' else '') else '' - match rel.operator - case LESS(__) then - let hysteresisfunction = if isReal then 'LessZC(<%e1%>, <%e2%>, data->simulationInfo->storedRelations[<%rel.index%>])' else 'Less(<%e1%>,<%e2%>)' - let &preExp += '<%res%> = <%hysteresisfunction%>;<%\n%>' - res - case LESSEQ(__) then - let hysteresisfunction = if isReal then 'LessEqZC(<%e1%>, <%e2%>, data->simulationInfo->storedRelations[<%rel.index%>])' else 'LessEq(<%e1%>,<%e2%>)' - let &preExp += '<%res%> = <%hysteresisfunction%>;<%\n%>' - res - case GREATER(__) then - let hysteresisfunction = if isReal then 'GreaterZC(<%e1%>, <%e2%>, data->simulationInfo->storedRelations[<%rel.index%>])' else 'Greater(<%e1%>,<%e2%>)' - let &preExp += '<%res%> = <%hysteresisfunction%>;<%\n%>' - res - case GREATEREQ(__) then - let hysteresisfunction = if isReal then 'GreaterEqZC(<%e1%>, <%e2%>, data->simulationInfo->storedRelations[<%rel.index%>])' else 'GreaterEq(<%e1%>,<%e2%>)' - let &preExp += '<%res%> = <%hysteresisfunction%>;<%\n%>' - res - end match - case SOME((exp,i,j)) then - let e1 = daeExp(rel.exp1, context, &preExp, &varDecls, &auxFunction) - let e2 = daeExp(rel.exp2, context, &preExp, &varDecls, &auxFunction) - let res = tempDecl("modelica_boolean", &varDecls) - if intEq(rel.index,-1) then - match rel.operator - case LESS(__) then - let &preExp += '<%res%> = Less(<%e1%>,<%e2%>);<%\n%>' - res - case LESSEQ(__) then - let &preExp += '<%res%> = LessEq(<%e1%>,<%e2%>);<%\n%>' - res - case GREATER(__) then - let &preExp += '<%res%> = Greater(<%e1%>,<%e2%>);<%\n%>' - res - case GREATEREQ(__) then - let &preExp += '<%res%> = GreaterEq(<%e1%>,<%e2%>);<%\n%>' - res - end match - else - let isReal = if isRealType(typeof(rel.exp1)) then (if isRealType(typeof(rel.exp2)) then 'true' else '') else '' - match rel.operator - case LESS(__) then - let hysteresisfunction = if isReal then 'LessZC(<%e1%>, <%e2%>, data->simulationInfo->storedRelations[<%rel.index%>])' else 'Less(<%e1%>,<%e2%>)' - let &preExp += '<%res%> = <%hysteresisfunction%>;<%\n%>' - res - case LESSEQ(__) then - let hysteresisfunction = if isReal then 'LessEqZC(<%e1%>, <%e2%>, data->simulationInfo->storedRelations[<%rel.index%>])' else 'LessEq(<%e1%>,<%e2%>)' - let &preExp += '<%res%> = <%hysteresisfunction%>;<%\n%>' - res - case GREATER(__) then - let hysteresisfunction = if isReal then 'GreaterZC(<%e1%>, <%e2%>, data->simulationInfo->storedRelations[<%rel.index%>])' else 'Greater(<%e1%>,<%e2%>)' - let &preExp += '<%res%> = <%hysteresisfunction%>;<%\n%>' - res - case GREATEREQ(__) then - let hysteresisfunction = if isReal then 'GreaterEqZC(<%e1%>, <%e2%>, data->simulationInfo->storedRelations[<%rel.index%>])' else 'GreaterEq(<%e1%>,<%e2%>)' - let &preExp += '<%res%> = <%hysteresisfunction%>;<%\n%>' - res - end match - end match + let e1 = daeExp(zc.exp, context, &preExp, &varDecls, &auxFunction) + let res = tempDecl("modelica_boolean", &varDecls) + let &preExp += 'RELATIONHYSTERESIS(<%res%>, <%e1%>, <%zc.index%>);<%\n%>' + res end match end match -end daeExpRelationSim; +end daeExpZeroCrossing; template daeExpIf(Exp exp, Context context, Text &preExp, Text &varDecls, Text &auxFunction) diff --git a/Compiler/Template/CodegenXML.tpl b/Compiler/Template/CodegenXML.tpl index c109fb9391..f2d70777ac 100644 --- a/Compiler/Template/CodegenXML.tpl +++ b/Compiler/Template/CodegenXML.tpl @@ -2754,185 +2754,46 @@ template daeExpRelationSimXml(Exp exp, Context context, Text &preExp /*BUFP*/, ::= match exp case rel as RELATION(__) then - match context - case SIMULATION_CONTEXT(genDiscrete=false) then - match rel.optionExpisASUB - case NONE() then - let e1 = daeExpXml(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - let e2 = daeExpXml(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - let res = tempDeclXml("modelica_boolean", &varDecls /*BUFC*/) - match rel.operator - case LESS(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - case LESSEQ(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - case GREATER(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - case GREATEREQ(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - end match - case SOME((exp,i,j)) then - let e1 = daeExpXml(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - let e2 = daeExpXml(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - let iterator = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - let res = tempDeclXml("modelica_boolean", &varDecls /*BUFC*/) - //let e3 = daeExp(createArray(i), context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - match rel.operator - case LESS(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - case LESSEQ(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - case GREATER(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - case GREATEREQ(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - end match - end match - case SIMULATION_CONTEXT(genDiscrete=true) then - match rel.optionExpisASUB - case NONE() then - let e1 = daeExpXml(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - let e2 = daeExpXml(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - let res = tempDeclXml("modelica_boolean", &varDecls /*BUFC*/) - match rel.operator - case LESS(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - case LESSEQ(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - case GREATER(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - case GREATEREQ(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - end match - case SOME((exp,i,j)) then - let e1 = daeExpXml(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - let e2 = daeExpXml(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - let res = tempDeclXml("modelica_boolean", &varDecls /*BUFC*/) - //let e3 = daeExp(createArray(i), context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - let iterator = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - match rel.operator - case LESS(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - case LESSEQ(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - case GREATER(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - case GREATEREQ(__) then - let &preExp += - << - - <%e1%> - <%e2%> - <%\n%> - >> - res - end match - end match + let e1 = daeExpXml(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) + let e2 = daeExpXml(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) + let res = tempDeclXml("modelica_boolean", &varDecls /*BUFC*/) + match rel.operator + case LESS(__) then + let &preExp += + << + + <%e1%> + <%e2%> + <%\n%> + >> + res + case LESSEQ(__) then + let &preExp += + << + + <%e1%> + <%e2%> + <%\n%> + >> + res + case GREATER(__) then + let &preExp += + << + + <%e1%> + <%e2%> + <%\n%> + >> + res + case GREATEREQ(__) then + let &preExp += + << + + <%e1%> + <%e2%> + <%\n%> + >> + res end match end match end daeExpRelationSimXml; @@ -2945,12 +2806,10 @@ match exp case rel as RELATION(__) then match context case SIMULATION_CONTEXT(genDiscrete=true) then - match rel.optionExpisASUB - case NONE() then - let e1 = daeExpXml(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - let e2 = daeExpXml(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) - let res = tempDeclXml("modelica_boolean", &varDecls /*BUFC*/) - match rel.operator + let e1 = daeExpXml(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) + let e2 = daeExpXml(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/) + let res = tempDeclXml("modelica_boolean", &varDecls /*BUFC*/) + match rel.operator case EQUAL(__) then << @@ -2976,8 +2835,7 @@ case rel as RELATION(__) then << "The XML schema does only support =, >= , <= operators for constraints" >> - end match - end match + end match end match end match end daeExpConstraintXml; diff --git a/Compiler/Template/DAEDumpTV.mo b/Compiler/Template/DAEDumpTV.mo index dce9c235ae..1bf96739e7 100644 --- a/Compiler/Template/DAEDumpTV.mo +++ b/Compiler/Template/DAEDumpTV.mo @@ -1223,17 +1223,17 @@ package DAE Exp exp; end LUNARY; - record RELATION "Relation, e.g. a <= 0 - Index contains normal an Integer for every ZeroCrossing - but if Relation is in algorithm with for loop the iterator and the range - of static iterator is needed for codegen" + record RELATION "Relation, e.g. a <= 0" Exp exp1; Operator operator; Exp exp2; - Integer index; - Option> optionExpisASUB; end RELATION; + record ZEROCROSSING "Backend expression for zero crossing" + Exp exp; + Integer index; + end ZEROCROSSING; + record IFEXP "If expressions" Exp expCond; Exp expThen; diff --git a/Compiler/Template/SimCodeTV.mo b/Compiler/Template/SimCodeTV.mo index 7774a2f03f..caea924d03 100644 --- a/Compiler/Template/SimCodeTV.mo +++ b/Compiler/Template/SimCodeTV.mo @@ -1598,9 +1598,11 @@ package DAE Exp exp1; Operator operator; Exp exp2; - Integer index; - Option> optionExpisASUB; end RELATION; + record ZEROCROSSING "Backend expression for zero crossing" + Exp exp; + Integer index; + end ZEROCROSSING; record IFEXP Exp expCond; Exp expThen;