Skip to content

Commit

Permalink
[NF] Set structural parameters as final in the DAE
Browse files Browse the repository at this point in the history
- Set structural parameters as final when creating the DAE structure,
  to indicate that they've been evaluated and shouldn't be modified.
- Disable the old way of doing this used by the OF when using the NF,
  to avoid unnecessary traversal of the DAE.
  • Loading branch information
perost committed Jun 4, 2019
1 parent 4e286fa commit 84f52c5
Show file tree
Hide file tree
Showing 35 changed files with 108 additions and 94 deletions.
20 changes: 14 additions & 6 deletions OMCompiler/Compiler/FrontEnd/DAEUtil.mo
Expand Up @@ -5440,15 +5440,23 @@ algorithm
// Transform Modelica state machines to flat data-flow equations
dAElist := StateMachineFlatten.stateMachineToDataFlow(cache, env, inDAElist);

DAE.DAE(elts) := dAElist;
if Flags.isSet(Flags.SCODE_INST) then
// This is stupid, but `outDAElist := dAElist` causes crashes for some reason. GC bug?
DAE.DAE(elts) := dAElist;
outDAElist := DAE.DAE(elts);
else
DAE.DAE(elts) := dAElist;

ht := FCore.getEvaluatedParams(cache);
elts := List.map1(elts, makeEvaluatedParamFinal, ht);

ht := FCore.getEvaluatedParams(cache);
elts := List.map1(elts, makeEvaluatedParamFinal, ht);
if Flags.isSet(Flags.PRINT_STRUCTURAL) then
transformationsBeforeBackendNotification(ht);
end if;

if Flags.isSet(Flags.PRINT_STRUCTURAL) then
transformationsBeforeBackendNotification(ht);
outDAElist := DAE.DAE(elts);
end if;
outDAElist := DAE.DAE(elts);

// Don't even run the function to try and do this; it doesn't work very well
// outDAElist := transformDerInline(outDAElist);
end transformationsBeforeBackend;
Expand Down
21 changes: 13 additions & 8 deletions OMCompiler/Compiler/NFFrontEnd/NFConvertDAE.mo
Expand Up @@ -64,6 +64,7 @@ import Variable = NFVariable;
import ComponentReference;
import Algorithm = NFAlgorithm;
import NFPrefixes.ConnectorType;
import NFPrefixes.Variability;

public
function convert
Expand Down Expand Up @@ -233,23 +234,27 @@ function convertVarAttributes
input Component.Attributes compAttrs;
output Option<DAE.VariableAttributes> attributes;
protected
Option<Boolean> is_final;
Boolean is_final;
Option<Boolean> is_final_opt;
Type elTy;
Boolean is_array = false;
algorithm
if listEmpty(attrs) and not compAttrs.isFinal then
is_final := compAttrs.isFinal or
compAttrs.variability == Variability.STRUCTURAL_PARAMETER;

if listEmpty(attrs) and not is_final then
attributes := NONE();
return;
end if;

is_final := SOME(compAttrs.isFinal);
is_final_opt := SOME(is_final);

attributes := match Type.arrayElementType(ty)
case Type.REAL() then convertRealVarAttributes(attrs, is_final);
case Type.INTEGER() then convertIntVarAttributes(attrs, is_final);
case Type.BOOLEAN() then convertBoolVarAttributes(attrs, is_final);
case Type.STRING() then convertStringVarAttributes(attrs, is_final);
case Type.ENUMERATION() then convertEnumVarAttributes(attrs, is_final);
case Type.REAL() then convertRealVarAttributes(attrs, is_final_opt);
case Type.INTEGER() then convertIntVarAttributes(attrs, is_final_opt);
case Type.BOOLEAN() then convertBoolVarAttributes(attrs, is_final_opt);
case Type.STRING() then convertStringVarAttributes(attrs, is_final_opt);
case Type.ENUMERATION() then convertEnumVarAttributes(attrs, is_final_opt);
else NONE();
end match;
end convertVarAttributes;
Expand Down
4 changes: 2 additions & 2 deletions testsuite/flattening/modelica/scodeinst/Condition3.mo
Expand Up @@ -20,8 +20,8 @@ end Condition3;

// Result:
// class Condition3
// parameter Boolean b1 = true;
// parameter Boolean b2 = false;
// final parameter Boolean b1 = true;
// final parameter Boolean b2 = false;
// Real x = 2.0;
// end Condition3;
// endResult
2 changes: 1 addition & 1 deletion testsuite/flattening/modelica/scodeinst/ConnectArrays2.mo
Expand Up @@ -17,7 +17,7 @@ end ConnectArrays2;

// Result:
// class ConnectArrays2
// parameter Integer p = 3;
// final parameter Integer p = 3;
// input Real x[1];
// input Real x[2];
// input Real x[3];
Expand Down
12 changes: 6 additions & 6 deletions testsuite/flattening/modelica/scodeinst/ConnectArrays3.mo
Expand Up @@ -21,47 +21,47 @@ end ConnectArrays3;

// Result:
// class ConnectArrays3
// parameter Integer b1[1].p = 3;
// final parameter Integer b1[1].p = 3;
// Real b1[1].x[1];
// Real b1[1].x[2];
// Real b1[1].x[3];
// Real b1[1].y[1];
// Real b1[1].y[2];
// Real b1[1].y[3];
// Real b1[1].y[4];
// parameter Integer b1[2].p = 3;
// final parameter Integer b1[2].p = 3;
// Real b1[2].x[1];
// Real b1[2].x[2];
// Real b1[2].x[3];
// Real b1[2].y[1];
// Real b1[2].y[2];
// Real b1[2].y[3];
// Real b1[2].y[4];
// parameter Integer b1[3].p = 3;
// final parameter Integer b1[3].p = 3;
// Real b1[3].x[1];
// Real b1[3].x[2];
// Real b1[3].x[3];
// Real b1[3].y[1];
// Real b1[3].y[2];
// Real b1[3].y[3];
// Real b1[3].y[4];
// parameter Integer b2[1].p = 3;
// final parameter Integer b2[1].p = 3;
// Real b2[1].x[1];
// Real b2[1].x[2];
// Real b2[1].x[3];
// Real b2[1].y[1];
// Real b2[1].y[2];
// Real b2[1].y[3];
// Real b2[1].y[4];
// parameter Integer b2[2].p = 3;
// final parameter Integer b2[2].p = 3;
// Real b2[2].x[1];
// Real b2[2].x[2];
// Real b2[2].x[3];
// Real b2[2].y[1];
// Real b2[2].y[2];
// Real b2[2].y[3];
// Real b2[2].y[4];
// parameter Integer b2[3].p = 3;
// final parameter Integer b2[3].p = 3;
// Real b2[3].x[1];
// Real b2[3].x[2];
// Real b2[3].x[3];
Expand Down
4 changes: 2 additions & 2 deletions testsuite/flattening/modelica/scodeinst/DimRagged2.mo
Expand Up @@ -15,11 +15,11 @@ end DimRagged2;

// Result:
// class DimRagged2
// parameter Integer arr[1].n = 3;
// final parameter Integer arr[1].n = 3;
// Real arr[1].a[1];
// Real arr[1].a[2];
// Real arr[1].a[3];
// parameter Integer arr[2].n = 3;
// final parameter Integer arr[2].n = 3;
// Real arr[2].a[1];
// Real arr[2].a[2];
// Real arr[2].a[3];
Expand Down
2 changes: 1 addition & 1 deletion testsuite/flattening/modelica/scodeinst/DimUnknown9.mo
Expand Up @@ -12,7 +12,7 @@ end DimUnknown9;

// Result:
// class DimUnknown9
// parameter Integer n = 3;
// final parameter Integer n = 3;
// Real x[1];
// Real x[2];
// Real x[3];
Expand Down
Expand Up @@ -13,7 +13,7 @@ end EvaluateAllParams;

// Result:
// class EvaluateAllParams
// parameter Real p = 10.0;
// final parameter Real p = 10.0;
// Real x;
// equation
// x = time * 10.0;
Expand Down
Expand Up @@ -16,7 +16,7 @@ end EvaluateAllParams2;
// class EvaluateAllParams2
// parameter Real p(fixed = false);
// parameter Real q = 2.0 * p;
// parameter Real r = 2.0;
// final parameter Real r = 2.0;
// initial equation
// p - 2.0 = 0.0;
// end EvaluateAllParams2;
Expand Down
2 changes: 1 addition & 1 deletion testsuite/flattening/modelica/scodeinst/IfConnect1.mo
Expand Up @@ -21,7 +21,7 @@ end IfConnect1;

// Result:
// class IfConnect1
// parameter Boolean b = true;
// final parameter Boolean b = true;
// Real c1.e;
// Real c1.f;
// Real c2.e;
Expand Down
2 changes: 1 addition & 1 deletion testsuite/flattening/modelica/scodeinst/IfConnect2.mo
Expand Up @@ -21,7 +21,7 @@ end IfConnect2;

// Result:
// class IfConnect2
// parameter Boolean b = false;
// final parameter Boolean b = false;
// Real c1.e;
// Real c1.f;
// Real c2.e;
Expand Down
2 changes: 1 addition & 1 deletion testsuite/flattening/modelica/scodeinst/IfEquation3.mo
Expand Up @@ -16,7 +16,7 @@ end IfEquation3;

// Result:
// class IfEquation3
// parameter enumeration(one, two, three) e = E.one;
// final parameter enumeration(one, two, three) e = E.one;
// Real x = 1.0;
// equation
// x = 2.0;
Expand Down
2 changes: 1 addition & 1 deletion testsuite/flattening/modelica/scodeinst/IfEquation4.mo
Expand Up @@ -16,7 +16,7 @@ end IfEquation4;

// Result:
// class IfEquation4
// parameter enumeration(one, two, three) e = E.one;
// final parameter enumeration(one, two, three) e = E.one;
// Real x = 1.0;
// end IfEquation4;
// endResult
4 changes: 2 additions & 2 deletions testsuite/flattening/modelica/scodeinst/IfEquation5.mo
Expand Up @@ -20,8 +20,8 @@ end IfEquation5;
// class IfEquation5
// Real x[1];
// Real x[2];
// parameter Boolean p[1] = false;
// parameter Boolean p[2] = true;
// final parameter Boolean p[1] = false;
// final parameter Boolean p[2] = true;
// equation
// x[1] = 1.0;
// x[2] = 2.0;
Expand Down
12 changes: 6 additions & 6 deletions testsuite/flattening/modelica/scodeinst/InStreamReduction1.mo
@@ -1,4 +1,4 @@
// name: ActualStreamVariability1
// name: InStreamReduction1
// keywords: stream actualStream connector
// status: correct
// cflags: -d=newInst
Expand All @@ -10,15 +10,15 @@ connector C
stream Real s;
end C;

model ActualStreamVariability1
model InStreamReduction1
parameter Integer n = 3;
C c[n];
Real as = sum(inStream(c[i].s) for i in 1:n);
end ActualStreamVariability1;
end InStreamReduction1;

// Result:
// class ActualStreamVariability1
// parameter Integer n = 3;
// class InStreamReduction1
// final parameter Integer n = 3;
// Real c[1].r;
// Real c[1].f;
// Real c[1].s;
Expand All @@ -33,5 +33,5 @@ end ActualStreamVariability1;
// c[1].f = 0.0;
// c[2].f = 0.0;
// c[3].f = 0.0;
// end ActualStreamVariability1;
// end InStreamReduction1;
// endResult
2 changes: 1 addition & 1 deletion testsuite/flattening/modelica/scodeinst/RedeclareMod5.mo
Expand Up @@ -28,7 +28,7 @@ end RedeclareMod5;

// Result:
// class RedeclareMod5
// parameter Integer grid.nLinks = 1;
// final parameter Integer grid.nLinks = 1;
// parameter Real grid.l[1,1] = 200.0;
// parameter Real lines[1].l = 1.0;
// end RedeclareMod5;
Expand Down
2 changes: 1 addition & 1 deletion testsuite/flattening/modelica/scodeinst/RedeclareMod6.mo
Expand Up @@ -41,6 +41,6 @@ end RedeclareMod6;

// Result:
// class RedeclareMod6
// parameter Integer grid.nLinks = 1;
// final parameter Integer grid.nLinks = 1;
// end RedeclareMod6;
// endResult
2 changes: 1 addition & 1 deletion testsuite/flattening/modelica/scodeinst/Size6.mo
Expand Up @@ -28,6 +28,6 @@ end Size6;
// Real a.x[2];
// Real a.x[3];
// Real a.x[4];
// parameter Integer n = 4;
// final parameter Integer n = 4;
// end Size6;
// endResult
2 changes: 1 addition & 1 deletion testsuite/flattening/modelica/scodeinst/StateSelect2.mo
Expand Up @@ -11,7 +11,7 @@ end StateSelect2;

// Result:
// class StateSelect2
// parameter enumeration(never, avoid, default, prefer, always) s = StateSelect.default;
// final parameter enumeration(never, avoid, default, prefer, always) s = StateSelect.default;
// Real x(stateSelect = StateSelect.default);
// end StateSelect2;
// endResult
30 changes: 15 additions & 15 deletions testsuite/flattening/modelica/scodeinst/TestSampleNoClock.mos
Expand Up @@ -102,11 +102,11 @@ instantiateModel(Modelica.Blocks.Examples.BooleanNetwork1); getErrorString();
// end Modelica.Math.BooleanVectors.oneTrue;
//
// class Modelica.Blocks.Examples.BooleanNetwork1 \"Demonstrates the usage of blocks from Modelica.Blocks.MathBoolean\"
// parameter Boolean showValue.use_activePort = true \"= true, if activePort enabled\";
// final parameter Boolean showValue.use_activePort = true \"= true, if activePort enabled\";
// Boolean showValue.active = false \"Boolean variable to visualize if use_activePort=false (time varying)\";
// Boolean showValue.activePort \"Boolean variable to be shown in diagram layer if use_activePort = true\";
// Boolean showValue.showActive;
// parameter Integer and1.nu(min = 0) = 3 \"Number of input connections\";
// final parameter Integer and1.nu(min = 0) = 3 \"Number of input connections\";
// Boolean and1.u[1] \"Vector of Boolean input signals\";
// Boolean and1.u[2] \"Vector of Boolean input signals\";
// Boolean and1.u[3] \"Vector of Boolean input signals\";
Expand All @@ -126,31 +126,31 @@ instantiateModel(Modelica.Blocks.Examples.BooleanNetwork1); getErrorString();
// parameter Real booleanStep.startTime(quantity = \"Time\", unit = \"s\") = 1.5 \"Time instant of step start\";
// parameter Boolean booleanStep.startValue = false \"Output before startTime\";
// Boolean booleanStep.y \"Connector of Boolean output signal\";
// parameter Integer or1.nu(min = 0) = 2 \"Number of input connections\";
// final parameter Integer or1.nu(min = 0) = 2 \"Number of input connections\";
// Boolean or1.u[1] \"Vector of Boolean input signals\";
// Boolean or1.u[2] \"Vector of Boolean input signals\";
// Boolean or1.y \"Boolean output signal\";
// parameter Integer xor1.nu(min = 0) = 2 \"Number of input connections\";
// final parameter Integer xor1.nu(min = 0) = 2 \"Number of input connections\";
// Boolean xor1.u[1] \"Vector of Boolean input signals\";
// Boolean xor1.u[2] \"Vector of Boolean input signals\";
// Boolean xor1.y \"Boolean output signal\";
// parameter Boolean showValue2.use_activePort = true \"= true, if activePort enabled\";
// final parameter Boolean showValue2.use_activePort = true \"= true, if activePort enabled\";
// Boolean showValue2.active = false \"Boolean variable to visualize if use_activePort=false (time varying)\";
// Boolean showValue2.activePort \"Boolean variable to be shown in diagram layer if use_activePort = true\";
// Boolean showValue2.showActive;
// parameter Boolean showValue3.use_activePort = true \"= true, if activePort enabled\";
// final parameter Boolean showValue3.use_activePort = true \"= true, if activePort enabled\";
// Boolean showValue3.active = false \"Boolean variable to visualize if use_activePort=false (time varying)\";
// Boolean showValue3.activePort \"Boolean variable to be shown in diagram layer if use_activePort = true\";
// Boolean showValue3.showActive;
// parameter Integer nand1.nu(min = 0) = 2 \"Number of input connections\";
// final parameter Integer nand1.nu(min = 0) = 2 \"Number of input connections\";
// Boolean nand1.u[1] \"Vector of Boolean input signals\";
// Boolean nand1.u[2] \"Vector of Boolean input signals\";
// Boolean nand1.y \"Boolean output signal\";
// parameter Integer or2.nu(min = 0) = 2 \"Number of input connections\";
// final parameter Integer or2.nu(min = 0) = 2 \"Number of input connections\";
// Boolean or2.u[1] \"Vector of Boolean input signals\";
// Boolean or2.u[2] \"Vector of Boolean input signals\";
// Boolean or2.y \"Boolean output signal\";
// parameter Boolean showValue4.use_activePort = true \"= true, if activePort enabled\";
// final parameter Boolean showValue4.use_activePort = true \"= true, if activePort enabled\";
// Boolean showValue4.active = false \"Boolean variable to visualize if use_activePort=false (time varying)\";
// Boolean showValue4.activePort \"Boolean variable to be shown in diagram layer if use_activePort = true\";
// Boolean showValue4.showActive;
Expand All @@ -168,7 +168,7 @@ instantiateModel(Modelica.Blocks.Examples.BooleanNetwork1); getErrorString();
// Boolean set1.expr[2] \"y = if u[i] then expr[i] else y_default (time varying)\";
// parameter Boolean set1.use_pre_as_default = true \"set true to hold last value as default (y_default = pre(y))\";
// parameter Boolean set1.y_default = false \"Default value of output y if all u[i] = false\";
// parameter Integer set1.nu(min = 0) = 2 \"Number of input connections\";
// final parameter Integer set1.nu(min = 0) = 2 \"Number of input connections\";
// Boolean set1.u[1] \"Set y = expr[i], if u[i] = true\";
// Boolean set1.u[2] \"Set y = expr[i], if u[i] = true\";
// Boolean set1.y \"Output depending on expression\";
Expand All @@ -194,23 +194,23 @@ instantiateModel(Modelica.Blocks.Examples.BooleanNetwork1); getErrorString();
// Boolean changing.y \"Boolean output signal\";
// Integer triggeredAdd.u \"Integer input signal\";
// Integer triggeredAdd.y \"Integer output signal\";
// parameter Boolean triggeredAdd.use_reset = false \"=true, if reset port enabled\";
// parameter Boolean triggeredAdd.use_set = false \"=true, if set port enabled and used as default value when reset\";
// final parameter Boolean triggeredAdd.use_reset = false \"=true, if reset port enabled\";
// final parameter Boolean triggeredAdd.use_set = false \"=true, if set port enabled and used as default value when reset\";
// parameter Integer triggeredAdd.y_start = 0 \"Initial and reset value of y if set port is not used\";
// Boolean triggeredAdd.trigger;
// protected Boolean triggeredAdd.local_reset;
// protected Integer triggeredAdd.local_set;
// parameter Integer integerConstant.k(start = 1) = 2 \"Constant output value\";
// Integer integerConstant.y \"Connector of Integer output signal\";
// parameter Boolean showValue1.use_numberPort = true \"= true, if numberPort enabled\";
// final parameter Boolean showValue1.use_numberPort = true \"= true, if numberPort enabled\";
// Integer showValue1.number = 0 \"Number to visualize if use_numberPort=false (time varying)\";
// Integer showValue1.numberPort \"Number to be shown in diagram layer if use_numberPort = true\";
// Integer showValue1.showNumber;
// parameter Boolean showValue5.use_activePort = true \"= true, if activePort enabled\";
// final parameter Boolean showValue5.use_activePort = true \"= true, if activePort enabled\";
// Boolean showValue5.active = false \"Boolean variable to visualize if use_activePort=false (time varying)\";
// Boolean showValue5.activePort \"Boolean variable to be shown in diagram layer if use_activePort = true\";
// Boolean showValue5.showActive;
// parameter Boolean showValue6.use_activePort = true \"= true, if activePort enabled\";
// final parameter Boolean showValue6.use_activePort = true \"= true, if activePort enabled\";
// Boolean showValue6.active = false \"Boolean variable to visualize if use_activePort=false (time varying)\";
// Boolean showValue6.activePort \"Boolean variable to be shown in diagram layer if use_activePort = true\";
// Boolean showValue6.showActive;
Expand Down

0 comments on commit 84f52c5

Please sign in to comment.