diff --git a/Compiler/FrontEnd/ComponentReference.mo b/Compiler/FrontEnd/ComponentReference.mo index 78010ddca6..723912886e 100644 --- a/Compiler/FrontEnd/ComponentReference.mo +++ b/Compiler/FrontEnd/ComponentReference.mo @@ -2078,6 +2078,15 @@ algorithm outCref := makeCrefQual(DAE.previousNamePrefix, DAE.T_UNKNOWN_DEFAULT, {}, inCref); end crefPrefixPrevious; +public function crefRemovePrePrefix + input output DAE.ComponentRef cref; +algorithm + cref := match cref + case DAE.CREF_QUAL(ident=DAE.preNamePrefix) then cref.componentRef; + else cref; + end match; +end crefRemovePrePrefix; + public function crefPrefixStart "public function crefPrefixStart Appends $START to a cref, so a => $START.a" input DAE.ComponentRef inCref; diff --git a/Compiler/SimCode/SimCodeUtil.mo b/Compiler/SimCode/SimCodeUtil.mo index 31eae9b1c9..4dcd6850fa 100644 --- a/Compiler/SimCode/SimCodeUtil.mo +++ b/Compiler/SimCode/SimCodeUtil.mo @@ -8565,14 +8565,9 @@ algorithm outHT := List.fold(vars.intConstVars, addSimVarToHashTable, outHT); outHT := List.fold(vars.boolConstVars, addSimVarToHashTable, outHT); outHT := List.fold(vars.stringConstVars, addSimVarToHashTable, outHT); - if Config.simCodeTarget()=="Cpp" then - // Not needed in the hashtable (actually breaks code generation - // due to bad indexes, no information that they are seed variables - // and so on... - outHT := List.fold(vars.sensitivityVars, addSimVarToHashTable, outHT); - outHT := List.fold(vars.jacobianVars, addSimVarToHashTable, outHT); - outHT := List.fold(vars.seedVars, addSimVarToHashTable, outHT); - end if; + outHT := List.fold(vars.sensitivityVars, addSimVarToHashTable, outHT); + outHT := List.fold(vars.jacobianVars, addSimVarToHashTable, outHT); + outHT := List.fold(vars.seedVars, addSimVarToHashTable, outHT); outHT := List.fold(vars.realOptimizeConstraintsVars, addSimVarToHashTable, outHT); outHT := List.fold(vars.realOptimizeFinalConstraintsVars, addSimVarToHashTable, outHT); else @@ -13321,16 +13316,8 @@ algorithm sv = BaseHashTable.get(cref, crefToSimVarHT); sv = match sv.aliasvar case SimCodeVar.NOALIAS() then sv; - /* The C++ runtime generates a different set of variables... */ - case _ guard Config.simCodeTarget() == "Cpp" then sv; - case SimCodeVar.ALIAS(varName=cref) - algorithm - Error.addSourceMessage(Error.COMPILER_WARNING, {getInstanceName() + " got an alias variable " + ComponentReference.printComponentRefStr(inCref) + " to " + ComponentReference.printComponentRefStr(cref) + ", but before code generation these should have been removed"}, sv.source.info); - then cref2simvar(cref, simCode); - case SimCodeVar.NEGATEDALIAS(varName=cref) - algorithm - Error.addSourceMessage(Error.INTERNAL_ERROR, {getInstanceName() + " got a negated alias variable " + ComponentReference.printComponentRefStr(inCref) + " to " + ComponentReference.printComponentRefStr(cref) + ", but before code generation these should have been removed"}, sv.source.info); - then sv; + case SimCodeVar.ALIAS(varName=cref) then cref2simvar(cref, simCode); /* Possibly not needed; can't really hurt that much though */ + case SimCodeVar.NEGATEDALIAS() then sv; end match; then sv; @@ -13338,11 +13325,7 @@ algorithm equation //print("cref2simvar: " + ComponentReference.printComponentRefStr(inCref) + " not found!\n"); badcref = ComponentReference.makeCrefIdent("ERROR_cref2simvar_failed " + ComponentReference.printComponentRefStr(inCref), DAE.T_REAL_DEFAULT, {}); - /* Todo: This also generates an error for example itearation variables, so i commented out - "Template did not find the simulation variable for "+ ComponentReference.printComponentRefStr(cref) + ". "; - Error.addInternalError(errstr, sourceInfo());*/ - then - SimCodeVar.SIMVAR(badcref, BackendDAE.VARIABLE(), "", "", "", -2, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.INTERNAL(), NONE(), {}, false, true, false, NONE(), NONE()); + then SimCodeVar.SIMVAR(badcref, BackendDAE.VARIABLE(), "", "", "", -2, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.INTERNAL(), NONE(), {}, false, true, false, NONE(), NONE()); end matchcontinue; end cref2simvar; diff --git a/Compiler/Template/CodegenC.tpl b/Compiler/Template/CodegenC.tpl index 70c90b096f..e5481b5372 100644 --- a/Compiler/Template/CodegenC.tpl +++ b/Compiler/Template/CodegenC.tpl @@ -1618,8 +1618,9 @@ template functionInput(SimCode simCode, ModelInfo modelInfo, String modelNamePre <%vars.inputVars |> SIMVAR(__) hasindex i0 => match cref2simvar(name, simCode) - case SIMVAR(__) then + case SIMVAR(aliasvar=NOALIAS()) then 'data->simulationInfo->inputVars[<%i0%>] = data->modelData-><%expTypeShort(type_)%>VarsData[<%index%>].attribute.start;' + else error(sourceInfo(), 'Cannot get attributes of alias variable <%crefStr(name)%>. Alias variables should have been replaced by the compiler before SimCode') ;separator="\n" %> @@ -1633,8 +1634,9 @@ template functionInput(SimCode simCode, ModelInfo modelInfo, String modelNamePre <%vars.inputVars |> SIMVAR(__) hasindex i0 => match cref2simvar(name, simCode) - case SIMVAR(__) then + case SIMVAR(aliasvar=NOALIAS()) then 'data->modelData-><%expTypeShort(type_)%>VarsData[<%index%>].attribute.start = data->simulationInfo->inputVars[<%i0%>];' + else error(sourceInfo(), 'Cannot get attributes of alias variable <%crefStr(name)%>. Alias variables should have been replaced by the compiler before SimCode') ;separator="\n" %> @@ -1647,8 +1649,9 @@ template functionInput(SimCode simCode, ModelInfo modelInfo, String modelNamePre <%vars.inputVars |> simVar as SIMVAR(__) hasindex i0 => match cref2simvar(name, simCode) - case SIMVAR(__) then + case SIMVAR(aliasvar=NOALIAS()) then 'names[<%i0%>] = (char *) data->modelData-><%expTypeShort(type_)%>VarsData[<%index%>].info.name;' + else error(sourceInfo(), 'Cannot get attributes of alias variable <%crefStr(name)%>. Alias variables should have been replaced by the compiler before SimCode') ;separator="\n" %> @@ -2944,10 +2947,11 @@ template functionUpdateBoundParameters(list simpleParameterEquation << <%cref(cref)%> = <%daeExpSimpleLiteral(exp)%>; <%match cref2simvar(cref, simCode) - case SIMVAR(varKind=PARAM()) then + case SIMVAR(aliasvar=NOALIAS(), varKind=PARAM()) then 'data->modelData-><%expTypeShort(type_)%>ParameterData[<%index%>].time_unvarying = 1;' - case SIMVAR(__) then - 'data->modelData-><%expTypeShort(type_)%>VarsData[<%index%>].time_unvarying = 1;'%> + case SIMVAR(aliasvar=NOALIAS()) then + 'data->modelData-><%expTypeShort(type_)%>VarsData[<%index%>].time_unvarying = 1;' + else error(sourceInfo(), 'Cannot get attributes of alias variable <%crefStr(cref)%>. Alias variables should have been replaced by the compiler before SimCode')%> >> ; separator="\n" %> <%fncalls%> TRACE_POP @@ -4459,12 +4463,6 @@ template crefShortType(ComponentRef cr) "template crefType end match end crefShortType; -template crefIndexInArray(ComponentRef cr, SimCode simCode) -::= - match cref2simvar(cr, simCode) - case SIMVAR(__) then index -end crefIndexInArray; - template functionAssertsforCheck(list algAndEqAssertsEquations, String modelNamePrefix) "template functionAssertsforCheck Generates function in simulation file. This is a helper of template simulationFile." diff --git a/Compiler/Template/CodegenCFunctions.tpl b/Compiler/Template/CodegenCFunctions.tpl index b36b18f44b..59901558fa 100644 --- a/Compiler/Template/CodegenCFunctions.tpl +++ b/Compiler/Template/CodegenCFunctions.tpl @@ -4100,10 +4100,14 @@ template crefToCStr(ComponentRef cr, Integer ix, Boolean isPre, Boolean isStart) case SIMVAR(varKind=ALG_STATE_OLD(), index=index) then 'data->simulationInfo->inlineData->algOldVars[<%index%>]' + case SIMVAR(aliasvar=ALIAS(varName=varName)) then crefToCStr(varName, ix, isPre, isStart) + case SIMVAR(aliasvar=NEGATEDALIAS(varName=varName), type_=T_BOOL()) then '!(<%crefToCStr(varName, ix, isPre, isStart)%>)' + case SIMVAR(aliasvar=NEGATEDALIAS(varName=varName)) then '-(<%crefToCStr(varName, ix, isPre, isStart)%>)' case SIMVAR(varKind=JAC_VAR()) case SIMVAR(varKind=JAC_DIFF_VAR()) case SIMVAR(varKind=SEED_VAR()) case SIMVAR(varKind=DAE_RESIDUAL_VAR()) + case SIMVAR(varKind=DAE_RESIDUAL_VAR()) case SIMVAR(index=-2) then if intEq(ix,0) then (if isPre then "$P$PRE")+crefDefine(cr) @@ -6980,11 +6984,11 @@ end varAttributes; template crefAttributes(ComponentRef cr) ::= - match cref2simvar(cr, getSimCode()) + match cref2simvar(crefRemovePrePrefix(cr), getSimCode()) case var as SIMVAR(index=-1, varKind=JAC_VAR()) then "dummyREAL_ATTRIBUTE" - case var as SIMVAR(index=-1) then error(sourceInfo(), 'varAttributes got index=-1 for <%crefStr(name)%>') case var as SIMVAR(__) then - 'data->modelData-><%varArrayName(var)%>Data[<%index%>].attribute /* <%escapeCComments(crefStrNoUnderscore(name))%> */' + if intLt(index,0) then error(sourceInfo(), 'varAttributes got negative index=<%index%> for <%crefStr(name)%>') else + 'data->modelData-><%varArrayName(var)%>Data[<%index%>].attribute /* <%escapeCComments(crefStrNoUnderscore(name))%> */' end crefAttributes; annotation(__OpenModelica_Interface="backend"); diff --git a/Compiler/Template/SimCodeTV.mo b/Compiler/Template/SimCodeTV.mo index 8fd33118ca..c6c2cdf4e4 100644 --- a/Compiler/Template/SimCodeTV.mo +++ b/Compiler/Template/SimCodeTV.mo @@ -3244,6 +3244,10 @@ package ComponentReference output DAE.ComponentRef outCR; end popCref; + function crefRemovePrePrefix + input DAE.ComponentRef inCR; + output DAE.ComponentRef outCR; + end crefRemovePrePrefix; end ComponentReference; package Expression