modelicaopenmodelica

How to use hierarchical connectors in Modelica


I want to use hierarchical connectors in Modelica. I have level 1 connectors made of RealOutput, level 2 connectors made of arrays of level 1, and level 3 connectors made of arrays of level 2.

I managed to make connectors of level 1 and 2 work, but could not figure out how to make level 3 connectors.

Below is a package that illustrates my attempts.

Models M1 and M2 work but model M3 causes an error in OpenModelica.

[1] 13:25:41 Translation Error
[/var/lib/jenkins2/ws/LINUX_BUILDS/tmp.build/openmodelica-1.19.2~dev.beta1/OMCompiler/Compiler/NFFrontEnd/NFCeval.mo: 1053:9-1053:67]: Internal error NFCeval.evalBinarySub failed to evaluate ‘{2, 5}[i] - {1, 3}[i]‘
within Experiment;

package conn 
  connector L1
  "Level 1 connector" 
    parameter Integer n;
    Modelica.Blocks.Interfaces.RealOutput R[n];
  annotation(
      Diagram(graphics = {Ellipse(fillColor = {239, 41, 41}, fillPattern = FillPattern.Sphere, extent = {{-60, 60}, {60, -60}})}),
      Icon(graphics = {Ellipse(fillColor = {239, 41, 41}, fillPattern = FillPattern.Sphere, extent = {{-60, 60}, {60, -60}})}));
  end L1;


  connector L2
  "Level 2 connector"
    parameter Integer r[:];
    L1 l1[size(r, 1)](n=r);
  annotation(
      Icon(graphics = {Ellipse(fillColor = {252, 233, 79}, fillPattern = FillPattern.Sphere, extent = {{-60, 60}, {60, -60}})}),
      Diagram(graphics = {Ellipse(fillColor = {252, 233, 79}, fillPattern = FillPattern.Sphere, extent = {{-60, 60}, {60, -60}})}));
  end L2;


  connector L3
  "Level 3 connector"
    parameter Integer a[:];  // {2, 3}
    parameter Integer r[sum(a)];  // {2, 3, 2, 3, 4}
    parameter Integer start[size(a, 1)];  // r start indices {1, 3}
    parameter Integer stop[size(a,1)];  // r stop indices {2, 5}
    // how to construct connector with a = {2, 3}, r = {2, 3, 2, 3, 4} ?
    // to get l2[1] = L2(r={2, 3})
    // to get l2[2] = L2(r={2, 3, 4})
    L2 l2[size(a,1)](r={r[start[i]:stop[i]] for i in 1:size(a,1)});
  
    annotation(
      Diagram(graphics = {Ellipse(fillColor = {138, 226, 52}, fillPattern = FillPattern.Sphere, extent = {{-60, 60}, {60, -60}})}),
      Icon(graphics = {Ellipse(fillColor = {138, 226, 52}, fillPattern = FillPattern.Sphere, extent = {{-60, 60}, {60, -60}})}));
  end L3;


  model M1
  "Model with level 1 connector"
  Modelica.Blocks.Sources.Constant[5] C(k = {1, 2, 3, 4, 5});
  L1 l1(n=5);
  
  equation
  connect(C.y, l1.R);
  annotation(
      Icon(graphics = {Rectangle(fillColor = {239, 41, 41}, fillPattern = FillPattern.Sphere, extent = {{-100, 100}, {100, -100}})}));
  end M1;


  model M2
  "Model with level 2 connector"
  Modelica.Blocks.Sources.Constant[5] C(k = {1, 2, 3, 4, 5});
  L2 l2(r={2, 3});
  
  equation
  connect(C[1:2].y, l2.l1[1].R[1:2]);
  connect(C[3:5].y, l2.l1[2].R[1:3]);

  annotation(
      Icon(graphics = {Rectangle(fillColor = {252, 233, 79}, fillPattern = FillPattern.Sphere, extent = {{-100, 100}, {100, -100}})}));end M2;


  model M3
  "Model with level 3 connector"
  Modelica.Blocks.Sources.Constant[14] C(k = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14});
  L3 l3(a={2,3}, r={2, 3, 2, 3, 4}, start={1,3}, stop={2,5});
  
  equation
  connect(C[1:2].y, l3.l2[1].l1[1].R[1:2]);
  connect(C[3:5].y, l3.l2[1].l1[2].R[1:3]);
  connect(C[6:7].y, l3.l2[2].l1[1].R[1:2]);
  connect(C[8:10].y, l3.l2[2].l1[2].R[1:3]);
  connect(C[11:14].y, l3.l2[3].l1[2].R[1:4]);
  
  annotation(
      Icon(graphics = {Rectangle(fillColor = {138, 226, 52}, fillPattern = FillPattern.Sphere, extent = {{-100, 100}, {100, -100}})}));end M3;
end conn;

Solution

  • I managed to make it work by following the suggestion here.

    Here is my solution, where model M3 runs without error:

    within CASP.Experiment;
    
    package conn1
      connector L1
      "Level 1 connector" 
        parameter Integer n;
        Modelica.Blocks.Interfaces.RealOutput R[n];
      annotation(
          Diagram(graphics = {Ellipse(fillColor = {239, 41, 41}, fillPattern = FillPattern.Sphere, extent = {{-60, 60}, {60, -60}})}),
          Icon(graphics = {Ellipse(fillColor = {239, 41, 41}, fillPattern = FillPattern.Sphere, extent = {{-60, 60}, {60, -60}})}));
      end L1;
    
    
      connector L2
      "Level 2 connector"
        parameter R3 r3;
        L1 l1[r3.n](n=r3.r);
      annotation(
          Icon(graphics = {Ellipse(fillColor = {252, 233, 79}, fillPattern = FillPattern.Sphere, extent = {{-60, 60}, {60, -60}})}),
          Diagram(graphics = {Ellipse(fillColor = {252, 233, 79}, fillPattern = FillPattern.Sphere, extent = {{-60, 60}, {60, -60}})}));
      end L2;
      
      record R3
        parameter Integer n;
        Integer r[n];
      end R3;
    
      connector L3
      "Level 3 connector"
        parameter R3 r3[:];
        // how to construct connector to get
        // l3.l2[1] = L2(r={2, 3})
        // l3.l2[2] = L2(r={2, 3, 4})
        L2 l2[size(r3, 1)](r3=r3);
      
        annotation(
          Diagram(graphics = {Ellipse(fillColor = {138, 226, 52}, fillPattern = FillPattern.Sphere, extent = {{-60, 60}, {60, -60}})}),
          Icon(graphics = {Ellipse(fillColor = {138, 226, 52}, fillPattern = FillPattern.Sphere, extent = {{-60, 60}, {60, -60}})}));
      end L3;
    
    
      model M1
      "Model with level 1 connector"
      Modelica.Blocks.Sources.Constant[5] C(k = {1, 2, 3, 4, 5});
      L1 l1(n=5);
      
      equation
      connect(C.y, l1.R);
      annotation(
          Icon(graphics = {Rectangle(fillColor = {239, 41, 41}, fillPattern = FillPattern.Sphere, extent = {{-100, 100}, {100, -100}})}));
      end M1;
    
    
      model M2
      "Model with level 2 connector"
      Modelica.Blocks.Sources.Constant[5] C(k = {1, 2, 3, 4, 5});
      L2 l2(r={2, 3});
      
      equation
      connect(C[1:2].y, l2.l1[1].R[1:2]);
      connect(C[3:5].y, l2.l1[2].R[1:3]);
    
      annotation(
          Icon(graphics = {Rectangle(fillColor = {252, 233, 79}, fillPattern = FillPattern.Sphere, extent = {{-100, 100}, {100, -100}})}));
      end M2;
    
    
      model M3
      "Model with level 3 connector"
      Modelica.Blocks.Sources.Constant[14] C(k = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14});
      L3 l3(r3={R3(n=2, r={2, 3}), R3(n=3, r={2, 3, 4})});
      
      equation
      connect(C[1:2].y, l3.l2[1].l1[1].R[1:2]);
      connect(C[3:5].y, l3.l2[1].l1[2].R[1:3]);
      connect(C[6:7].y, l3.l2[2].l1[1].R[1:2]);
      connect(C[8:10].y, l3.l2[2].l1[2].R[1:3]);
      connect(C[11:14].y, l3.l2[2].l1[3].R[1:4]);
      
      annotation(
          Icon(graphics = {Rectangle(fillColor = {138, 226, 52}, fillPattern = FillPattern.Sphere, extent = {{-100, 100}, {100, -100}})}));
      end M3;
    end conn1;