gwr
gwr

Reputation: 467

How to use flexible array sizes within an expandable connector?

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.

Upvotes: 3

Views: 441

Answers (2)

gwr
gwr

Reputation: 467

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.

Upvotes: 0

f.wue
f.wue

Reputation: 847

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

Upvotes: 1

Related Questions