3

I would like to use expandable connector (e.g. a Bus-like structure) to model (mainly) input and output connections for more complex subsystems (An array will not allow having different units for its elements and flattened lists get unwieldly while a record does not afaik allow for easily connecting only one of its variables without writing equations).

While you may simply connect a variable to an empty expandable connector without problems, its unit (and other attributes) will not be passed on. Thus I would like to flexbily declare variables inside an expandable connector (e.g. like x[:]) and have their dimensions be determined by the actual connection.

Unfortunately neither does this validate in OpenModelica and Wolfram System Modeler:

package FirstAttempt  

  model SimpleBusModel
    DataBus dataBus;
    Modelica.Blocks.Interfaces.RealOutput x[ nout ](each unit = "widgets");
    parameter Integer nout = 2;
  equation 
    x = ones( nout );
    connect( x, dataBus.x );
  end SimpleBusModel;

  expandable connector DataBus
    Real[:] x(each unit = "widgets");
  end DataBus;

end FirstAttempt;

... nor does declaring the connector as a sub-component and passing on the array size via inner and outer work out:

  model SimpleBusModel
    DataBus dataBus;
    Modelica.Blocks.Interfaces.RealOutput x[ nout ](each unit = "widgets");
    inner parameter Integer nout = 2;

    expandable connector DataBus
      Real[nout] x(each unit = "widgets");
      outer parameter Integer nout;
    end DataBus;

  equation 
    x = ones( nout );
    connect( x, dataBus.x );
  end SimpleBusModel;

How can an expandable connector with flexible, predefined array variables be set up?

Update:

It seems that this is an issue pertaining to OpenModelica and the Wolfram System Modeler, as the examples given work out fine in Dymola (cf. f.wue's comment below). I cross-posted a similiar question on Wolfram Community.

In the Modelica Specification (Version 3.2 Revision 2) we find in Section 9.1.3 Expandable Connectors:

Before generating connection equations non-parameter scalar variables and non-parameter array elements declared in expandable connectors are marked as only being potentially present. A non-parameter array element may be declared with array dimensions “:” indicating that the size is unknown. This applies to both variables of simple types, and variables of structured types.

As indicated by f.wue we should connect connectors - not mere inputs and outputs. I changed this in my code above, but it is not fixing the issue.

gwr
  • 465
  • 6
  • 17
  • 1
    Just as an info: Your implementation works fine in dymola. Maybe OpenModelica just does not support this? – f.wue Jun 18 '19 at 11:19
  • @f.wue Unfortunately, it also does not work in the [Wolfram System Modeler (Version 12.0)](https://community.wolfram.com/groups/-/m/t/1706043). I had (naively) believed that Modelica is a clearly specified language? – gwr Jun 18 '19 at 11:22

2 Answers2

1

What could work is using RealInputs and RealOutputs instead of just Real. This way the connection works and the units are passed correctly.

package FirstAttempt

  model OuterModel
   SimpleBusModel sbm;
   Modelica.Blocks.Sources.RealExpression dummy(y=3);
  equation 
    connect(sbm.x[1], dummy.y);
    connect(sbm.x[2], dummy.y);

  end OuterModel;

  model SimpleBusModel
    DataBus dataBus;
    Modelica.Blocks.Interfaces.RealInput x[nout](each unit = "widgets");
    parameter Integer nout = 2;
  equation 
    connect(x, dataBus.x_in);
  end SimpleBusModel;

  expandable connector DataBus
    Real[:] x_in(each unit = "widgets");
  end DataBus;
end FirstAttempt;

You can then connect any other value using the Blocks.Routing models from the Standard Library and connect them the to the RealInput of the SimpleBusModel.

EDIT:

I changed the package and added an OuterModel. This is how the result looks in dymola when i simulate OuterModel. Is this even the behaviour you want to achieve?

enter image description here

f.wue
  • 837
  • 8
  • 15
  • Your code unfortunately does not work in either OpenModelica and Wolfram System Modeler. It is a bit unclear to me, what using `RealInput` will substantially change? The problem seems to be that the array size is not deduced automatically. – gwr Jun 18 '19 at 12:40
  • 1
    I think the correct way to connect to an expandable connector is via a connector itself. Real is not a connector as far as i know, RealInput / RealOutput on the other hand are connectors. It's pretty annoying that modelica-code behaves different on different software. – f.wue Jun 19 '19 at 08:48
  • Please check my edit, maybe i understood your question all wrong – f.wue Jun 19 '19 at 08:55
  • Eventually I will use components with an embedded data bus in outer models. The expandable connector is to help with not drowning in connectors and with separating output according to basic types (especially distinguish rates, times, and counts in business models). I need to check this with WSM again, but I believe the main problem is the use of `:` inside the expandable connector. – gwr Jun 19 '19 at 09:06
  • 1
    Sorry - unfortunately I checked too quickly, neither the use of `RealInput` nor that of `RealOutput` resolves this issue. In System Modeler and OpenModelica there seems to be no way to correctly inform the `x[:]` inside the `expandable connector` of the actual dimensions of the `connector` (e.g. `x[2]`) that is connected to it. There goes a beautiful and practical solution... – gwr Jun 19 '19 at 10:13
  • 1
    No problem. I will keep my answer as this works in dymola. It's a real pitty that modelica code strongly depends on the software one is using. – f.wue Jun 19 '19 at 11:37
0

I have received an answer on a similar question on Wolfram Community confirming, that Wolfram System Modeler (Version 12.0.0 or earlier) currently does not support flexible array sizes inside an expandable connector.

Workaround

In Wolfram System Modeler we currently have to pass on the array-size information in a conventional way (i.e. by modification of size parameter):

model SimpleBusModel
  DataBus dataBus(nout = nout);
  Modelica.Blocks.Interfaces.RealOutput x[nout](each unit = "widgets");
  parameter Integer nout = 2;

  expandable connector DataBus
    Real[nout] x(each unit = "widgets");
    parameter Integer nout;
  end DataBus;

equation
  x = ones(nout);
  connect(x, dataBus.x);
end SimpleBusModel;

This will also work in OpenModelica.

gwr
  • 465
  • 6
  • 17