From abfb41d76fc079446db54bb2377be6f23a85bdc1 Mon Sep 17 00:00:00 2001 From: kabdelhak Date: Tue, 25 Feb 2020 10:59:57 +0100 Subject: [PATCH] [CodegenC] initial non linear start value update - provide surrounding pre expression structure to catch asserts - ticket #5807 --- OMCompiler/Compiler/Template/CodegenC.tpl | 20 ++++++++++++++++--- .../Compiler/Template/CodegenCFunctions.tpl | 4 ++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/OMCompiler/Compiler/Template/CodegenC.tpl b/OMCompiler/Compiler/Template/CodegenC.tpl index 59eeb399695..05234a23447 100644 --- a/OMCompiler/Compiler/Template/CodegenC.tpl +++ b/OMCompiler/Compiler/Template/CodegenC.tpl @@ -5727,7 +5727,14 @@ template equationNonlinear(SimEqSystem eq, Context context, String modelNamePref %> /* get old value */ <%nls.crefs |> name hasindex i0 => - 'data->simulationInfo->nonlinearSystemData[<%nls.indexNonLinearSystem%>].nlsxOld[<%i0%>] = <%if init then crefOrStartCref(name, context) else cref(name)%>;' + let &preExp = buffer "" + let &varDecls = buffer "" + let &auxFunction = buffer "" + let START = if init then crefOrStartCref(name, context, &preExp, &varDecls, &auxFunction) else cref(name) + let PREV = if stringEq(&preExp, "") then "" else &preExp + "\n" + let DECL = if stringEq(&varDecls, "") then "" else &varDecls + "\n" + let AUX = if stringEq(&auxFunction, "") then "" else &auxFunction + "\n" + '<%DECL%><%PREV%><%AUX%>data->simulationInfo->nonlinearSystemData[<%nls.indexNonLinearSystem%>].nlsxOld[<%i0%>] = <%START%>;' ;separator="\n"%> retValue = solve_nonlinear_system(data, threadData, <%nls.indexNonLinearSystem%>); /* check if solution process was successful */ @@ -5771,8 +5778,15 @@ template equationNonlinearAlternativeTearing(SimEqSystem eq, Context context, St if (data->simulationInfo->nonlinearSystemData[<%at.indexNonLinearSystem%>].checkConstraints(data, threadData) == 1) { /* get old value */ - <%at.crefs |> name hasindex i0 => - 'data->simulationInfo->nonlinearSystemData[<%at.indexNonLinearSystem%>].nlsxOld[<%i0%>] = <%if init then crefOrStartCref(name, context) else cref(name)%>;' + <%nls.crefs |> name hasindex i0 => + let &preExp = buffer "" + let &varDecls = buffer "" + let &auxFunction = buffer "" + let START = if init then crefOrStartCref(name, context, &preExp, &varDecls, &auxFunction) else cref(name) + let PREV = if stringEq(&preExp, "") then "" else &preExp + "\n" + let DECL = if stringEq(&varDecls, "") then "" else &varDecls + "\n" + let AUX = if stringEq(&auxFunction, "") then "" else &auxFunction + "\n" + '<%DECL%><%PREV%><%AUX%>data->simulationInfo->nonlinearSystemData[<%nls.indexNonLinearSystem%>].nlsxOld[<%i0%>] = <%START%>;' ;separator="\n"%> retValue = solve_nonlinear_system(data, threadData, <%at.indexNonLinearSystem%>); /* The casual tearing set found a solution */ diff --git a/OMCompiler/Compiler/Template/CodegenCFunctions.tpl b/OMCompiler/Compiler/Template/CodegenCFunctions.tpl index 9d3606c321e..055f149c3b7 100644 --- a/OMCompiler/Compiler/Template/CodegenCFunctions.tpl +++ b/OMCompiler/Compiler/Template/CodegenCFunctions.tpl @@ -4599,14 +4599,14 @@ end contextIteratorName; else crefToCStr(cr, 0, false, false) end cref; -/* public */ template crefOrStartCref(ComponentRef cr, Context context) +/* public */ template crefOrStartCref(ComponentRef cr, Context context, Text &preExp, Text &varDecls, Text &auxFunction) "Only during intialization and if the start expression cannot be evaluated to a constant use the full expression. Otherwise return the cref itself. Used to resolve Ticket: #5807" ::= match cref2simvar(cr, getSimCode()) case SIMVAR(initialValue = SOME(startExp)) then - if boolNot(Expression.isConst(startExp)) then daeExp(startExp, context, "", "", "") + if boolNot(Expression.isConst(startExp)) then daeExp(startExp, context, &preExp, &varDecls, &auxFunction) else cref(cr) else cref(cr) end crefOrStartCref;