Changeset 7755d79 in OpenModelica


Ignore:
Timestamp:
2023-03-25T10:50:00+01:00 (13 months ago)
Author:
Mahder Gebremedhin <mahder.geb@…>
Children:
194557d
Parents:
5aba6e5
Message:

Do not overwrite values to compute attribute values.

  • The previous implementation used the parameter itself to temporarily store and return the computed attribute values. It then used this value to update the actual attribute value and re-setted the parameter to its start value. This does not always work (I think) because the value of the parameter is different from the start value (which is saved in the init.xml file) if the value is not a constant (?)

On top of that it was quite confusing to read and understand. So modify
the codegen to treate this case specifically and generate in-place
updates to these non-constant min, max, nominal attribute values.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • OMCompiler/Compiler/Template/CodegenC.tpl

    r80e3f4b0 r7755d79  
    34463446let &sub = buffer ""
    34473447  <<
    3448   <%(startValueEquations |> eq as SES_SIMPLE_ASSIGN(__) => equation_impl_options(-1, -1, eq, contextOther, modelNamePrefix, /* Static? */ true, /* No optimization */ true, /* Initial? */ true) ; separator="\n")
    3449   %><%(nominalValueEquations |> eq as SES_SIMPLE_ASSIGN(__) => equation_impl_options(-1, -1, eq, contextOther, modelNamePrefix, /* Static? */ true, /* No optimization */ true, /* Initial? */ true) ; separator="\n")
    3450   %><%(minValueEquations |> eq as SES_SIMPLE_ASSIGN(__) => equation_impl_options(-1, -1, eq, contextOther, modelNamePrefix, /* Static? */ true, /* No optimization */ true, /* Initial? */ true) ; separator="\n")
    3451   %><%(maxValueEquations |> eq as SES_SIMPLE_ASSIGN(__) => equation_impl_options(-1, -1, eq, contextOther, modelNamePrefix, /* Static? */ true, /* No optimization */ true, /* Initial? */ true) ; separator="\n") %>
     3448  <%(startValueEquations |> eq as SES_SIMPLE_ASSIGN(__) => equation_impl2(-1, -1, eq, contextOther, modelNamePrefix, /* Static? */ true, /* No optimization */ true, /* Initial? */ true) ; separator="\n")%>
     3449  <%(nominalValueEquations |> eq as SES_SIMPLE_ASSIGN(__) => functionUpdateBoundVariableAttributesFunctions(eq, "nominal", contextOther, modelNamePrefix, /* Static? */ true, /* No optimization */ true) ; separator="\n") %>
     3450  <%(minValueEquations |> eq as SES_SIMPLE_ASSIGN(__) => functionUpdateBoundVariableAttributesFunctions(eq, "min", contextOther, modelNamePrefix, /* Static? */ true, /* No optimization */ true) ; separator="\n") %>
     3451  <%(maxValueEquations |> eq as SES_SIMPLE_ASSIGN(__) => functionUpdateBoundVariableAttributesFunctions(eq, "max", contextOther, modelNamePrefix, /* Static? */ true, /* No optimization */ true) ; separator="\n") %>
    34523452  OMC_DISABLE_OPT
    34533453  int <%symbolName(modelNamePrefix,"updateBoundVariableAttributes")%>(DATA *data, threadData_t *threadData)
     
    34553455    TRACE_PUSH
    34563456    /* min ******************************************************** */
    3457     <%(minValueEquations |> eq as SES_SIMPLE_ASSIGN(__) => equation_call(eq, modelNamePrefix) ; separator="\n")%>
    3458 
    34593457    infoStreamPrint(LOG_INIT, 1, "updating min-values");
    3460     <%minValueEquations |> SES_SIMPLE_ASSIGN(__) =>
    3461       <<
    3462       <%crefAttributes(cref)%>.min = <%cref(cref, &sub)%>;
    3463         infoStreamPrint(LOG_INIT_V, 0, "%s(min=<%crefToPrintfArg(cref)%>)", <%crefVarInfo(cref)%>.name, (<%crefType(cref)%>) <%crefAttributes(cref)%>.min);
    3464       <%cref(cref, &sub)%> = <%crefAttributes(cref)%>.start;
    3465       >>
    3466       ;separator="\n"
    3467     %>
     3458    <%minValueEquations |> eq as SES_SIMPLE_ASSIGN(__) => equation_call(eq, modelNamePrefix)%>
    34683459    if (ACTIVE_STREAM(LOG_INIT)) messageClose(LOG_INIT);
    34693460
    34703461    /* max ******************************************************** */
    3471     <%(maxValueEquations |> eq as SES_SIMPLE_ASSIGN(__) => equation_call(eq, modelNamePrefix) ; separator="\n")%>
    3472 
    34733462    infoStreamPrint(LOG_INIT, 1, "updating max-values");
    3474     <%maxValueEquations |> SES_SIMPLE_ASSIGN(__) =>
    3475       <<
    3476       <%crefAttributes(cref)%>.max = <%cref(cref, &sub)%>;
    3477         infoStreamPrint(LOG_INIT_V, 0, "%s(max=<%crefToPrintfArg(cref)%>)", <%crefVarInfo(cref)%>.name, (<%crefType(cref)%>) <%crefAttributes(cref)%>.max);
    3478       <%cref(cref, &sub)%> = <%crefAttributes(cref)%>.start;
    3479       >>
    3480       ;separator="\n"
    3481     %>
     3463    <%maxValueEquations |> eq as SES_SIMPLE_ASSIGN(__) => equation_call(eq, modelNamePrefix)%>
    34823464    if (ACTIVE_STREAM(LOG_INIT)) messageClose(LOG_INIT);
    34833465
    34843466    /* nominal **************************************************** */
    3485     <%(nominalValueEquations |> eq as SES_SIMPLE_ASSIGN(__) => equation_call(eq, modelNamePrefix) ; separator="\n")%>
    3486 
    34873467    infoStreamPrint(LOG_INIT, 1, "updating nominal-values");
    3488     <%nominalValueEquations |> SES_SIMPLE_ASSIGN(__) =>
    3489       <<
    3490       <%crefAttributes(cref)%>.nominal = <%cref(cref, &sub)%>;
    3491         infoStreamPrint(LOG_INIT_V, 0, "%s(nominal=<%crefToPrintfArg(cref)%>)", <%crefVarInfo(cref)%>.name, (<%crefType(cref)%>) <%crefAttributes(cref)%>.nominal);
    3492       <%cref(cref, &sub)%> = <%crefAttributes(cref)%>.start;
    3493       >>
    3494       ;separator="\n"
    3495     %>
     3468    <%nominalValueEquations |> eq as SES_SIMPLE_ASSIGN(__) => equation_call(eq, modelNamePrefix)%>
    34963469    if (ACTIVE_STREAM(LOG_INIT)) messageClose(LOG_INIT);
    34973470
     
    35063479  >>
    35073480end functionUpdateBoundVariableAttributes;
     3481
     3482template functionUpdateBoundVariableAttributesFunctions(SimEqSystem eq, String attribute, Context context, String modelNamePrefix, Boolean static, Boolean noOpt)
     3483 "Generates an a function representing an equation for an attribute binding (e.g min, max ...)"
     3484::=
     3485  let OMC_NO_OPT = if noOpt then 'OMC_DISABLE_OPT<%\n%>' else (match eq case SES_LINEAR(__) then 'OMC_DISABLE_OPT<%\n%>')
     3486  match eq
     3487    case SES_SIMPLE_ASSIGN(exp=CALL(path=IDENT(name="fail")))
     3488    case SES_SIMPLE_ASSIGN_CONSTRAINTS(exp=CALL(path=IDENT(name="fail"))) then
     3489      '<%generateThrow()%><%\n%>'
     3490
     3491    case SES_SIMPLE_ASSIGN(__)
     3492    case SES_SIMPLE_ASSIGN_CONSTRAINTS(__) then
     3493
     3494      let ix = equationIndex(eq) /*System.tmpTickIndex(10)*/
     3495      let &varDecls = buffer ""
     3496      let &auxFunction = buffer ""
     3497      let body = functionUpdateBoundVariableAttributesFunctionsSimpleAssign(eq, attribute, context, &varDecls, &auxFunction)
     3498
     3499      <<
     3500      /*
     3501      <%dumpEqs(fill(eq,1))%>
     3502      */
     3503      <%OMC_NO_OPT%><% if static then "static "%>void <%symbolName(modelNamePrefix,"eqFunction")%>_<%ix%>(DATA *data, threadData_t *threadData)
     3504      {
     3505        TRACE_PUSH
     3506        const int equationIndexes[2] = {1,<%ix%>};
     3507        <%&varDecls%>
     3508        <%body%>
     3509        TRACE_POP
     3510      }
     3511
     3512      >>
     3513
     3514    else "NOT IMPLEMENTED EQUATION equation_"
     3515end functionUpdateBoundVariableAttributesFunctions;
     3516
     3517template functionUpdateBoundVariableAttributesFunctionsSimpleAssign(SimEqSystem eq, String attribute, Context context,
     3518                              Text &varDecls, Text &auxFunction)
     3519 "Generates an equation that is just a simple assignment for an arribute binding. The attribute type is given by the
     3520 function argument 'attibute' (e.g min, max ...)"
     3521::=
     3522match eq
     3523case SES_SIMPLE_ASSIGN(__)
     3524case SES_SIMPLE_ASSIGN_CONSTRAINTS(__) then
     3525  let &sub = buffer ""
     3526  let &preExp = buffer ""
     3527  let expPart = daeExp(exp, context, &preExp, &varDecls, &auxFunction)
     3528  let postExp = if isStartCref(cref) then
     3529    <<
     3530    <%cref(popCref(cref), &sub)%> = <%cref(cref, &sub)%>;
     3531    infoStreamPrint(LOG_INIT_V, 0, "updated start value: %s(start=<%crefToPrintfArg(popCref(cref))%>)", <%crefVarInfo(popCref(cref))%>.name, (<%crefType(popCref(cref))%>) <%cref(popCref(cref), &sub)%>);
     3532    >>
     3533  <<
     3534  <%modelicaLine(eqInfo(eq))%>
     3535  <%preExp%>
     3536  <%crefAttributes(cref)%>.<%attribute%> = <%expPart%>;
     3537  infoStreamPrint(LOG_INIT_V, 0, "%s(<%attribute%>=<%crefToPrintfArg(cref)%>)", <%crefVarInfo(cref)%>.name,
     3538        (<%crefType(cref)%>) <%crefAttributes(cref)%>.<%attribute%>);
     3539  <%postExp%>
     3540  <%endModelicaLine()%>
     3541  >>
     3542end functionUpdateBoundVariableAttributesFunctionsSimpleAssign;
     3543
    35083544
    35093545template functionUpdateBoundParameters(list<SimEqSystem> simpleParameterEquations, list<SimEqSystem> parameterEquations, String fileNamePrefix, String fullPathPrefix, String modelNamePrefix, SimCode simCode)
Note: See TracChangeset for help on using the changeset viewer.