Skip to content

Commit

Permalink
[CodegenC] initial non linear start value update
Browse files Browse the repository at this point in the history
 - provide surrounding pre expression structure to catch asserts
 - ticket #5807
  • Loading branch information
kabdelhak authored and adrpo committed Feb 25, 2020
1 parent 8422ae5 commit abfb41d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 17 additions & 3 deletions OMCompiler/Compiler/Template/CodegenC.tpl
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/Template/CodegenCFunctions.tpl
Expand Up @@ -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;
Expand Down

0 comments on commit abfb41d

Please sign in to comment.