DarioMangoni
DarioMangoni

Reputation: 311

Connect parameter variables in expandable connector

Two models are connected via an (empty) expandable connector. One of the two, makes a connection between the expandable connector and a parameter variable.

I didn't expect any issue. On the contrary, I had some issues:

What am I doing wrong? Am I missing something? My final goal is to link a parameter variable to an expandable connector (e.g. inform different vehicle components about the number of battery cells, let's say) without the need of an additional redundant variable. Is this possible?

The test code is the following:

Upvotes: 0

Views: 977

Answers (2)

marco
marco

Reputation: 6655

f.wue already showed how to write a parameter to the bus. This post additionally explains how to read the value without increasing the variability (so it stays a parameter).

To make its usage easier, here is the complete code of a demo package to show how to read and write parameters on busses. It works with Dymola 2019 FD01 in pedantic mode and OMEdit v1.13.2.

package ParmeterToBus
  expandable connector bus
  end bus;

  model bus_param_out
    parameter Real numberPar;
    Modelica.Blocks.Sources.Constant helper(k=numberPar);
    bus controlBus;

  equation 
    connect(controlBus.number, helper.y);
  end bus_param_out;

  model bus_param_in
    Modelica.Blocks.Interfaces.RealOutput buffer;
    bus controlBus;
    final parameter Real num(fixed=false);  // users should not modify it, hence its final
  initial equation 
    num = buffer;
  equation 
    connect(buffer, controlBus.number);
  end bus_param_in;

  model example
    bus_param_in bus_in;
    bus_param_out bus_out(numberPar=7);
  equation 
    connect(bus_in.controlBus, bus_out.controlBus);
  end example;
end ParmeterToBus;

Note that the implementation is far from being straightforward. Some tweaks are necessary along with helper classes to overcome the following restrictions:

  1. Only connectors can be used in connect statements.
    So we need
    • an instance of an constant block to write the value
      (helper in the code above)
    • an instance of an RealOutput connector to read the value
      (buffer in the code above)
  2. Models and blocks are not allowed to have the prefix parameter.
    Therefore wee need the constant block to write the value, we cannot use a RealOutput connector here.
  3. For parameters usually an initial equation is automatically generated from its binding equation.
    To prevent this, we have to set (fixed=false). This allows us to assign the parameter in the initialization phase with the value of a variable of higher variability - in our case the buffer.

Upvotes: 2

f.wue
f.wue

Reputation: 847

You could use

Modelica.Blocks.Interfaces.RealOutput num

to declare a Real that can be used in a connect statement.

EDIT: As far as i know, connecting a parameter to a connector is not possible. Dymola will yield the error:

Connect does not refer to connectors in connect

The official way would be to use Modelica.Blocks.Sources.Constant, which is equivalent to RealOutput. You can directly use a parameter like this:

model bus_param_out
  parameter Real number = 3;
  Modelica.Blocks.Sources.Constant num_con(k=number);
  bus controlBus;
equation 
  connect(controlBus.num, num_con.y);
end bus_param_out;

When using expandable connectors and connect these connectors, you have to make sure to set bus.numonly once. Everything else will result in an error.

Try connecting everything with the graphical interface, that will maybe clear things up.

You can use the expandable connector outside of connect like this:

model bus_param_out
  Real number_of_bus;
  parameter Real number = 3;
  Modelica.Blocks.Sources.Constant num_con(k=number);
  bus controlBus;
equation 
  connect(controlBus.num, num_con.y);
  number_of_bus = controlBus.num;
end bus_param_out;

But trying to declare parameter Real number_of_bus will result in the following error:

The variability of the definition equation: number_of_bus = controlBus.num; is higher than the declared variability of the variables.

because the connector is time-variant and the parameter constant.

Upvotes: 2

Related Questions