Opened 8 years ago

Closed 8 years ago

#4089 closed defect (fixed)

HideResult defaults to protected

Reported by: Rüdiger Franke Owned by: Patrick Täuber
Priority: high Milestone: 1.11.0
Component: Backend Version:
Keywords: Cc: Patrick Täuber

Description

The treatment of parameters has significantly improved with d75f648c (see also #4027).

The treatment of the HideResult annotation is still unsatisfactory. According to Modelica Spec 3.3, section 18.3 this annotation overrides the protected status for the translated model. In particular a protected variable can be made available with HideResult=false.

See the following example:

model HideResult
  parameter Real a = 1;
  parameter Real b = 2 annotation(HideResult = true);
  parameter Real e = d;
protected
  parameter Real c = 3;
  parameter Real d = 4 annotation(HideResult = false);
end HideResult;

It should be possible to change the value of d after model translation. Re-simulate should calculate e.

Change History (20)

comment:1 by Rüdiger Franke, 8 years ago

PR1153 attempts to fix hideResult for the generated code.

A couple of tests fail: Hudson

The C runtime appears to omit some variables from the result file after this change. See for instance
Modelica.Blocks.Examples.LogicalNetwork1.
The protected algebraic variable table1.nextTime is gone; the protected parameter table1.n is still there. The init.xml file lists both of them!?

Last edited 8 years ago by Rüdiger Franke (previous) (diff)

comment:2 by Lennart Ochel, 8 years ago

Cc: Patrick Täuber added

I think this really increases the usability of OMEdit.

comment:3 by Rüdiger Franke, 8 years ago

dd59e54 adjusts the filtering of variables in the result file.

The problem remains that parameters are still written to the result file, even if they are protected or hidden -- check the above example with and without parameter qualifiers!?

comment:4 by Francesco Casella, 8 years ago

While you're at this, please also check #3925, which is related

comment:5 by Rüdiger Franke, 8 years ago

I get the desired code generated when replacing in BackendVariable.mo two calls to DAEUtil.getProtectedAttr with the new funtion isHiddenVar. This new function lets the optional HideResult annotation override the result of DAEUtil.getProtectedAttr.

Could this work out?

*** BackendVariable.head.mo 2016-10-24 21:22:46.364719794 +0200
--- BackendVariable.mo  2016-10-24 21:24:10.076716111 +0200
*************** public function isProtectedVar
*** 1297,1308 ****
  "author: Frenkel TUD 2013-01
    Returns the DAE.Protected attribute."
    input BackendDAE.Var v;
!   output Boolean prot;
! protected
!   Option<DAE.VariableAttributes> attr;
! algorithm
!   BackendDAE.VAR(values = attr) := v;
!   prot := DAEUtil.getProtectedAttr(attr);
  end isProtectedVar;
  
  public function hasVarEvaluateAnnotationOrFinal
--- 1297,1303 ----
  "author: Frenkel TUD 2013-01
    Returns the DAE.Protected attribute."
    input BackendDAE.Var v;
!   output Boolean prot = isHiddenVar(v);
  end isProtectedVar;
  
  public function hasVarEvaluateAnnotationOrFinal
*************** algorithm
*** 1340,1345 ****
--- 1335,1359 ----
    end match;
  end hasVarEvaluateAnnotation;
  
+ protected function isHiddenVar
+   input BackendDAE.Var inVar;
+   output Boolean hidden;
+ protected
+   SCode.Annotation anno;
+   Absyn.Exp val;
+ algorithm
+   try
+     BackendDAE.VAR(comment=SOME(SCode.COMMENT(annotation_ = SOME(anno)))) := inVar;
+     val := SCode.getNamedAnnotation(anno, "HideResult");
+     hidden := match(val)
+       case Absyn.BOOL(true) then true;
+       else false;
+     end match;
+   else
+     hidden := DAEUtil.getProtectedAttr(inVar.values); // see Modelica Spec 3.3, section 18.3
+   end try;
+ end isHiddenVar;
+ 
  public function hasAnnotation"checks if the variable has an annotation"
    input BackendDAE.Var inVar;
    output Boolean hasAnnot;
*************** end isFinalVar;
*** 1690,1696 ****
  
  public function isFinalOrProtectedVar
    input BackendDAE.Var inVar;
!   output Boolean b = DAEUtil.getFinalAttr(inVar.values) or DAEUtil.getProtectedAttr(inVar.values);
  end isFinalOrProtectedVar;
  
  public function getVariableAttributes "Returns the DAE.VariableAttributes of a variable."
--- 1704,1710 ----
  
  public function isFinalOrProtectedVar
    input BackendDAE.Var inVar;
!   output Boolean b = DAEUtil.getFinalAttr(inVar.values) or isHiddenVar(inVar);
  end isFinalOrProtectedVar;
  
  public function getVariableAttributes "Returns the DAE.VariableAttributes of a variable."

in reply to:  5 comment:6 by Lennart Ochel, 8 years ago

Replying to rfranke:

I get the desired code generated when replacing in BackendVariable.mo two calls to DAEUtil.getProtectedAttr with the new funtion isHiddenVar. This new function lets the optional HideResult annotation override the result of DAEUtil.getProtectedAttr.

I think this is a good idea, because it would make it easier to handle the protected attribute consistently.

comment:7 by Rüdiger Franke, 8 years ago

Commit 6345c52 adapts the function BackendVariable.isProtectedVar accordingly.

comment:8 by Rüdiger Franke, 8 years ago

Here is a new version of the example. The hiding of b does not work anymore, i.e. evaluation of hide_b does not work?!

model HideResult2
  parameter Real a = 1;
  parameter Boolean hide_b = true annotation(Evaluate = true);
  parameter Real b = 2 annotation(HideResult = hide_b);
  parameter Real e = d;
protected
  parameter Real c = 3;
  parameter Real d = 4 annotation(HideResult = false);
end HideResult2;
Last edited 8 years ago by Rüdiger Franke (previous) (diff)

comment:9 by Patrick Täuber, 8 years ago

I will have a look...

comment:10 by Patrick Täuber, 8 years ago

hide_b is evaluated but evaluated parameters are only replaced in the variable attributes not in annotations. I think we should add hideResult to BackendDAE.VAR in the lowering phase and access it from there (just like it is done for tearingSelect).

comment:11 by Rüdiger Franke, 8 years ago

Hmm... vwaurich and myself just were fighting with all the hardly documented features to distinguish between an array and a scalar during code generation (#4093). Much too much effort for such a simple thing.

Wouldn't it be better to replace evaluated variables instead of introducing yet another special rule?

in reply to:  10 comment:12 by Lennart Ochel, 8 years ago

Replying to ptaeuber:

I think we should add hideResult to BackendDAE.VAR in the lowering phase and access it from there (just like it is done for tearingSelect).

I agree with that.

comment:13 by Patrick Täuber, 8 years ago

Evaluated parameters are now also replaced in the hideResult attribute (70c7422).

comment:14 by Rüdiger Franke, 8 years ago

Resolution: fixed
Status: newclosed

Thank you!

comment:15 by Rüdiger Franke, 8 years ago

Resolution: fixed
Status: closedreopened

Unfortunately this slight modification of the example does not work:

model HideResult3
  parameter Real a = 1;
  parameter Boolean show_b = false annotation(Evaluate = true);
  parameter Real b = 2 annotation(HideResult = not show_b);
end HideResult3;

It gives:

Translation Warning
The hideResult annotation could not be evaluated, probably due to 
missing annotation(Evaluate=true). It is set to false by default.

comment:16 by Patrick Täuber, 8 years ago

Resolution: fixed
Status: reopenedclosed

Fixed in d487be4.

comment:17 by Rüdiger Franke, 8 years ago

Thank you for the fix for not show_b!

This seems to be a tricky issue :-( Evaluation of HideResult does not yet seem to work for composed models. See HideResult4.Test:

package HideResult4
  model Component
    parameter Real a = 1;
    parameter Boolean hide_b = false annotation(Evaluate = true);
    parameter Real b = 2 annotation(HideResult = hide_b);
  end Component;

  model Test
    Component c(hide_b = true);
  end Test;
end HideResult4;

Gives:

Translation Warning
The hideResult annotation could not be evaluated, probably due to missing
annotation(Evaluate=true). It is set to isProtected (=false) by default.

comment:18 by Rüdiger Franke, 8 years ago

Resolution: fixed
Status: closedreopened

comment:19 by Patrick Täuber, 8 years ago

Owner: changed from Lennart Ochel to Patrick Täuber
Status: reopenedaccepted

The issue of HideResult4 is fixed in 901d152. Please check the functionality again.

comment:20 by Rüdiger Franke, 8 years ago

Resolution: fixed
Status: acceptedclosed

Looks good now. Thanks!

Note: See TracTickets for help on using tickets.