Skip to content

Commit

Permalink
Add support for HideResult annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel committed Dec 19, 2015
1 parent 1abd255 commit 386cb98
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Compiler/SimCode/SimCodeFunctionUtil.mo
Expand Up @@ -242,7 +242,7 @@ algorithm
"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);
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);
end matchcontinue;
end cref2simvar;

Expand Down
46 changes: 35 additions & 11 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -2316,7 +2316,7 @@ algorithm

case(DAE.CREF(cr, ty)::rest) equation
slst = List.map(dims, intString);
var = SimCodeVar.SIMVAR(cr, BackendDAE.VARIABLE(), "", "", "", 0, NONE(), NONE(), NONE(), NONE(), false, ty, false, SOME(name), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), slst, false, true);
var = SimCodeVar.SIMVAR(cr, BackendDAE.VARIABLE(), "", "", "", 0, NONE(), NONE(), NONE(), NONE(), false, ty, false, SOME(name), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), slst, false, true, false);
tempvars = createTempVarsforCrefs(rest, {var});
then listAppend(listReverse(tempvars), itempvars);
end match;
Expand Down Expand Up @@ -2352,7 +2352,7 @@ algorithm
arrayCref = ComponentReference.getArrayCref(cr);
inst_dims = ComponentReference.crefDims(cr);
numArrayElement = List.map(inst_dims, ExpressionDump.dimensionString);
var = SimCodeVar.SIMVAR(cr, BackendDAE.VARIABLE(), "", "", "", 0, NONE(), NONE(), NONE(), NONE(), false, ty, false, arrayCref, SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), numArrayElement, false, true);
var = SimCodeVar.SIMVAR(cr, BackendDAE.VARIABLE(), "", "", "", 0, NONE(), NONE(), NONE(), NONE(), false, ty, false, arrayCref, SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), numArrayElement, false, true, false);
then createTempVarsforCrefs(rest, var::itempvars);
end match;
end createTempVarsforCrefs;
Expand Down Expand Up @@ -2395,13 +2395,13 @@ algorithm
arraycref := ComponentReference.crefStripSubs(cr);
ty := ComponentReference.crefTypeFull(cr);
var := SimCodeVar.SIMVAR(cr, BackendDAE.VARIABLE(), "", "", "", 0, NONE(), NONE(), NONE(), NONE(), false,
ty, false, SOME(arraycref), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, true);
ty, false, SOME(arraycref), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, true, false);

/* The rest don't need to be marked i.e. we have 'NONE()'. Just create simvars. */
ttmpvars := {var};
for cr in crlst loop
ty := ComponentReference.crefTypeFull(cr);
var := SimCodeVar.SIMVAR(cr, BackendDAE.VARIABLE(), "", "", "", 0, NONE(), NONE(), NONE(), NONE(), false, ty, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, true);
var := SimCodeVar.SIMVAR(cr, BackendDAE.VARIABLE(), "", "", "", 0, NONE(), NONE(), NONE(), NONE(), false, ty, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, true, false);
ttmpvars := var::ttmpvars;
end for;
ttmpvars := listReverse(ttmpvars);
Expand Down Expand Up @@ -3904,6 +3904,7 @@ algorithm
list<BackendDAE.Var> restVar;
Option<DAE.VariableAttributes> dae_var_attr;
Boolean isProtected;
Boolean hideResult = false;
Integer index;

case({}, _, _, _, _, _)
Expand All @@ -3918,30 +3919,30 @@ algorithm
currVar = ComponentReference.crefPrefixDer(currVar);
derivedCref = Differentiate.createDifferentiatedCrefName(currVar, cref, inMatrixName);
isProtected = getProtected(dae_var_attr);
r1 = SimCodeVar.SIMVAR(derivedCref, BackendDAE.STATE_DER(), "", "", "", inIndex, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, isProtected);
r1 = SimCodeVar.SIMVAR(derivedCref, BackendDAE.STATE_DER(), "", "", "", inIndex, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, isProtected, hideResult);
then
createAllDiffedSimVars(restVar, cref, inAllVars, inIndex+1, inMatrixName, r1::iVars);

case(BackendDAE.VAR(varName=currVar, values = dae_var_attr)::restVar, cref, _, _, _, _) equation
({_}, _) = BackendVariable.getVar(currVar, inAllVars);
derivedCref = Differentiate.createDifferentiatedCrefName(currVar, cref, inMatrixName);
isProtected = getProtected(dae_var_attr);
r1 = SimCodeVar.SIMVAR(derivedCref, BackendDAE.STATE_DER(), "", "", "", inIndex, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, isProtected);
r1 = SimCodeVar.SIMVAR(derivedCref, BackendDAE.STATE_DER(), "", "", "", inIndex, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, isProtected, hideResult);
then
createAllDiffedSimVars(restVar, cref, inAllVars, inIndex+1, inMatrixName, r1::iVars);

case(BackendDAE.VAR(varName=currVar, varKind=BackendDAE.STATE(), values = dae_var_attr)::restVar, cref, _, _, _, _) equation
currVar = ComponentReference.crefPrefixDer(currVar);
derivedCref = Differentiate.createDifferentiatedCrefName(currVar, cref, inMatrixName);
isProtected = getProtected(dae_var_attr);
r1 = SimCodeVar.SIMVAR(derivedCref, BackendDAE.VARIABLE(), "", "", "", -1, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, isProtected);
r1 = SimCodeVar.SIMVAR(derivedCref, BackendDAE.VARIABLE(), "", "", "", -1, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, isProtected, hideResult);
then
createAllDiffedSimVars(restVar, cref, inAllVars, inIndex, inMatrixName, r1::iVars);

case(BackendDAE.VAR(varName=currVar, values = dae_var_attr)::restVar, cref, _, _, _, _) equation
derivedCref = Differentiate.createDifferentiatedCrefName(currVar, cref, inMatrixName);
isProtected = getProtected(dae_var_attr);
r1 = SimCodeVar.SIMVAR(derivedCref, BackendDAE.VARIABLE(), "", "", "", -1, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, isProtected);
r1 = SimCodeVar.SIMVAR(derivedCref, BackendDAE.VARIABLE(), "", "", "", -1, NONE(), NONE(), NONE(), NONE(), false, DAE.T_REAL_DEFAULT, false, NONE(), SimCodeVar.NOALIAS(), DAE.emptyElementSource, SimCodeVar.NONECAUS(), NONE(), {}, false, isProtected, hideResult);
then
createAllDiffedSimVars(restVar, cref, inAllVars, inIndex, inMatrixName, r1::iVars);

Expand Down Expand Up @@ -7563,6 +7564,7 @@ algorithm
BackendDAE.Variables vars;
SimCodeVar.Causality caus;
Boolean isProtected;
Boolean hideResult;

case ((BackendDAE.VAR(varName = cr,
varKind = kind as BackendDAE.PARAM(),
Expand All @@ -7575,6 +7577,7 @@ algorithm
commentStr = unparseCommentOptionNoAnnotationNoQuote(comment);
(unit, displayUnit) = extractVarUnit(dae_var_attr);
isProtected = getProtected(dae_var_attr);
hideResult = getHideResult(comment);
(minValue, maxValue) = getMinMaxValues(dlowVar);
initVal = getStartValue(dlowVar);
nomVal = getNominalValue(dlowVar);
Expand All @@ -7595,7 +7598,7 @@ algorithm
and isFixed;
then
SimCodeVar.SIMVAR(cr, kind, commentStr, unit, displayUnit, -1 /* use -1 to get an error in simulation if something failed */,
minValue, maxValue, initVal, nomVal, isFixed, type_, isDiscrete, arrayCref, aliasvar, source, caus, NONE(), numArrayElement, isValueChangeable, isProtected);
minValue, maxValue, initVal, nomVal, isFixed, type_, isDiscrete, arrayCref, aliasvar, source, caus, NONE(), numArrayElement, isValueChangeable, isProtected, hideResult);

// Start value of states may be changeable
case ((BackendDAE.VAR(varName = cr,
Expand All @@ -7609,6 +7612,7 @@ algorithm
commentStr = unparseCommentOptionNoAnnotationNoQuote(comment);
(unit, displayUnit) = extractVarUnit(dae_var_attr);
isProtected = getProtected(dae_var_attr);
hideResult = getHideResult(comment);
(minValue, maxValue) = getMinMaxValues(dlowVar);
initVal = getStartValue(dlowVar);
nomVal = getNominalValue(dlowVar);
Expand All @@ -7623,7 +7627,7 @@ algorithm
// print("name: " + ComponentReference.printComponentRefStr(cr) + "indx: " + intString(indx) + "\n");
then
SimCodeVar.SIMVAR(cr, kind, commentStr, unit, displayUnit, -1 /* use -1 to get an error in simulation if something failed */,
minValue, maxValue, initVal, nomVal, isFixed, type_, isDiscrete, arrayCref, aliasvar, source, caus, NONE(), numArrayElement, true, isProtected);
minValue, maxValue, initVal, nomVal, isFixed, type_, isDiscrete, arrayCref, aliasvar, source, caus, NONE(), numArrayElement, true, isProtected, hideResult);

case ((BackendDAE.VAR(varName = cr,
varKind = kind,
Expand All @@ -7636,6 +7640,7 @@ algorithm
commentStr = unparseCommentOptionNoAnnotationNoQuote(comment);
(unit, displayUnit) = extractVarUnit(dae_var_attr);
isProtected = getProtected(dae_var_attr);
hideResult = getHideResult(comment);
(minValue, maxValue) = getMinMaxValues(dlowVar);
initVal = getStartValue(dlowVar);
nomVal = getNominalValue(dlowVar);
Expand All @@ -7650,7 +7655,7 @@ algorithm
// print("name: " + ComponentReference.printComponentRefStr(cr) + "indx: " + intString(indx) + "\n");
then
SimCodeVar.SIMVAR(cr, kind, commentStr, unit, displayUnit, -1 /* use -1 to get an error in simulation if something failed */,
minValue, maxValue, initVal, nomVal, isFixed, type_, isDiscrete, arrayCref, aliasvar, source, caus, NONE(), numArrayElement, false, isProtected);
minValue, maxValue, initVal, nomVal, isFixed, type_, isDiscrete, arrayCref, aliasvar, source, caus, NONE(), numArrayElement, false, isProtected, hideResult);
end match;
end dlowvarToSimvar;

Expand Down Expand Up @@ -9750,6 +9755,25 @@ algorithm
end match;
end getProtected;

protected function getHideResult
input Option<SCode.Comment> inComment;
output Boolean outHideResult;
protected
SCode.Annotation ann;
Absyn.Exp val;
algorithm
try
SOME(SCode.COMMENT(annotation_=SOME(ann))) := inComment;
val := SCode.getNamedAnnotation(ann, "HideResult");
outHideResult := match(val)
case Absyn.BOOL(true) then true;
else false;
end match;
else
outHideResult := false;
end try;
end getHideResult;

protected function createVarToArrayIndexMapping "author: marcusw
Creates a mapping for each array-cref to the array dimensions (int list) and to the indices (for the code generation) used to store the array content."
input SimCode.ModelInfo iModelInfo;
Expand Down
1 change: 1 addition & 0 deletions Compiler/SimCode/SimCodeVar.mo
Expand Up @@ -100,6 +100,7 @@ public uniontype SimVar "Information about a variable in a Modelica model."
list<String> numArrayElement;
Boolean isValueChangeable;
Boolean isProtected;
Boolean hideResult;
end SIMVAR;
end SimVar;

Expand Down
4 changes: 2 additions & 2 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -5500,7 +5500,7 @@ template ScalarVariableAttribute(SimVar simVar, Integer classIndex, String class
"Generates code for ScalarVariable Attribute file for FMU target."
::=
match simVar
case SIMVAR(source = SOURCE(info = info)) then
case SIMVAR(source=SOURCE(info=info)) then
let valueReference = '<%System.tmpTick()%>'
let variability = getVariablity(varKind)
let description = if comment then 'description = "<%Util.escapeModelicaStringToXmlString(comment)%>"'
Expand All @@ -5514,7 +5514,7 @@ template ScalarVariableAttribute(SimVar simVar, Integer classIndex, String class
causality = "<%caus%>" isValueChangeable = "<%isValueChangeable%>"
alias = <%alias%>
classIndex = "<%classIndex%>" classType = "<%classType%>"
isProtected = "<%isProtected%>"
isProtected = "<%isProtected%>" hideResult = "<%hideResult%>"
<%getInfoArgs(info)%>
>>
end ScalarVariableAttribute;
Expand Down
1 change: 1 addition & 0 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -226,6 +226,7 @@ package SimCodeVar
list<String> numArrayElement;
Boolean isValueChangeable;
Boolean isProtected;
Boolean hideResult;
end SIMVAR;
end SimVar;

Expand Down
14 changes: 11 additions & 3 deletions SimulationRuntime/c/simulation/simulation_input_xml.c
Expand Up @@ -584,7 +584,7 @@ void read_input_xml(MODEL_DATA* modelData,

/* read all static data from File for every variable */

#define READ_VARIABLES(out,in,attributeKind,read_var_attribute,debugName,start,nStates,mapAlias) \
#define READ_VARIABLES(out, in, attributeKind, read_var_attribute, debugName, start, nStates, mapAlias) \
infoStreamPrint(LOG_DEBUG, 1, "read xml file for %s", debugName); \
for(i = 0; i < nStates; i++) \
{ \
Expand All @@ -594,12 +594,20 @@ void read_input_xml(MODEL_DATA* modelData,
omc_ScalarVariable *v = *findHashLongVar(in, i); \
read_var_info(v, info); \
read_var_attribute(v, attribute); \
if (info->name[0] == '$') { \
if (info->name[0] == '$') \
{ \
out[j].filterOutput = 1; \
} else if (!omc_flag[FLAG_EMIT_PROTECTED] && 0 == strcmp(findHashStringString(v,"isProtected"),"true")) { \
} \
else if (!omc_flag[FLAG_EMIT_PROTECTED] && 0 == strcmp(findHashStringString(v, "isProtected"), "true")) \
{ \
infoStreamPrint(LOG_DEBUG, 0, "filtering protected variable %s", info->name); \
out[j].filterOutput = 1; \
} \
else if (0 == strcmp(findHashStringString(v, "hideResult"), "true")) \
{ \
infoStreamPrint(LOG_DEBUG, 0, "filtering variable %s due to HideResult annotation", info->name); \
out[j].filterOutput = 1; \
} \
addHashStringLong(&mapAlias, info->name, j); /* create a mapping for Alias variable to get the correct index */ \
debugStreamPrint(LOG_DEBUG, 0, "real %s: mapAlias[%s] = %ld", debugName, info->name, (long) j); \
} \
Expand Down

0 comments on commit 386cb98

Please sign in to comment.