From 0c67e1d3f32006c4e57d46fc4baf4a783f239d0e Mon Sep 17 00:00:00 2001 From: Karim Abdelhak Date: Wed, 25 Nov 2020 18:06:39 +0100 Subject: [PATCH] [Template] context based equation handling - solves ticket #6977 - new function that takes a context and calls the correct equation create function (for now only jacobian/normal) - used inside if equations to actually create jacobian if equations (seems to never have been used because it couldn't work) - small fix in backend to have correct size of if equations in advanced adjacency matrices --- OMCompiler/Compiler/BackEnd/BackendDAEUtil.mo | 2 +- OMCompiler/Compiler/Template/CodegenC.tpl | 20 ++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/OMCompiler/Compiler/BackEnd/BackendDAEUtil.mo b/OMCompiler/Compiler/BackEnd/BackendDAEUtil.mo index 697abc8b18b..88f8c009f96 100644 --- a/OMCompiler/Compiler/BackEnd/BackendDAEUtil.mo +++ b/OMCompiler/Compiler/BackEnd/BackendDAEUtil.mo @@ -4866,7 +4866,7 @@ algorithm (row, size) = adjacencyRowEnhancedEqnLst(eqnselse, vars, mark, rowmark, globalKnownVars, trytosolve,shared); lst = List.map(row,Util.tuple31); - (lst, row, size) = List.fold6(eqnslst, adjacencyRowEnhancedEqnLstIfBranches, vars, mark, rowmark, globalKnownVars, trytosolve, shared, (lst, row, size)); + (lst, row, _) = List.fold6(eqnslst, adjacencyRowEnhancedEqnLstIfBranches, vars, mark, rowmark, globalKnownVars, trytosolve, shared, (lst, row, size)); lstall = List.map(row, Util.tuple31); (_, lst, _) = List.intersection1OnTrue(lstall, lst, intEq); diff --git a/OMCompiler/Compiler/Template/CodegenC.tpl b/OMCompiler/Compiler/Template/CodegenC.tpl index b65e48c4c8c..c397a3526e8 100644 --- a/OMCompiler/Compiler/Template/CodegenC.tpl +++ b/OMCompiler/Compiler/Template/CodegenC.tpl @@ -5333,6 +5333,16 @@ template equation_impl2(Integer clockIndex, SimEqSystem eq, Context context, Str ) end equation_impl2; +template equation_call_context(SimEqSystem eq, String modelNamePrefix, Context context) + "Generates either a normal or jacobian equation depending on context. + ToDo: add other cases?" +::= + match context + case JACOBIAN_CONTEXT() then equation_callJacobian(eq, modelNamePrefix) + else equation_call(eq, modelNamePrefix) + end match +end equation_call_context; + template equation_call(SimEqSystem eq, String modelNamePrefix) "Generates an equation. This template should not be used for a SES_RESIDUAL. @@ -5940,13 +5950,13 @@ match eq case SES_IFEQUATION(ifbranches=ifbranches, elsebranch=elsebranch) then let &preExp = buffer "" let IfEquation = (ifbranches |> (e, eqns) hasindex index0 => - let condition = daeExp(e, context, &preExp, &varDecls, &eqnsDecls) - let &eqnsDecls += ( eqns |> eqn => equation_impl(-1, eqn, context, modelNamePrefixStr, init) ; separator="\n" ) - let conditionline = if index0 then 'else if(<%condition%>)' else 'if(<%condition%>)' + let condition = daeExp(e, context, &preExp, &varDecls, &eqnsDecls) + let &eqnsDecls += ( eqns |> eqn => equation_impl(-1, eqn, context, modelNamePrefixStr, init) ; separator="\n" ) + let conditionline = if index0 then 'else if(<%condition%>)' else 'if(<%condition%>)' << <%conditionline%> { - <%( eqns |> eqn => equation_call(eqn, modelNamePrefixStr) ; separator="\n" )%> + <%( eqns |> eqn => equation_call_context(eqn, modelNamePrefixStr, context) ; separator="\n" )%> } >> ;separator="\n") @@ -5955,7 +5965,7 @@ case SES_IFEQUATION(ifbranches=ifbranches, elsebranch=elsebranch) then <%preExp%> <%IfEquation%>else { - <%elsebranch |> eqn => equation_call(eqn, modelNamePrefixStr)%> + <%elsebranch |> eqn => equation_call_context(eqn, modelNamePrefixStr, context)%> } >> end equationIfEquationAssign;