Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Compiler/FrontEnd/ComponentReference.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 6 additions & 23 deletions Compiler/SimCode/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -13321,28 +13316,16 @@ 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;

case (_, _)
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;

Expand Down
22 changes: 10 additions & 12 deletions Compiler/Template/CodegenC.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
%>

Expand All @@ -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"
%>

Expand All @@ -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"
%>

Expand Down Expand Up @@ -2944,10 +2947,11 @@ template functionUpdateBoundParameters(list<SimEqSystem> 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
Expand Down Expand Up @@ -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<SimEqSystem> algAndEqAssertsEquations, String modelNamePrefix) "template functionAssertsforCheck
Generates function in simulation file.
This is a helper of template simulationFile."
Expand Down
10 changes: 7 additions & 3 deletions Compiler/Template/CodegenCFunctions.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this duplicate case???

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy-pasta I guess. Been writing directly on my laptop.

case SIMVAR(index=-2)
then
if intEq(ix,0) then (if isPre then "$P$PRE")+crefDefine(cr)
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions Compiler/Template/SimCodeTV.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down