1

I am trying to build a component which can be connected through one of two connectors based on a parameter. In the following example, depending on the boolean value, only one connector will be used at a time.

model component_2_connectors

  parameter Boolean isRotational = true;

  Real flux;
  Real potential;

  Modelica.Mechanics.Rotational.Interfaces.Flange_a flange_a if isRotational annotation (Placement(transformation(extent={{-110,30},{-90,50}})));
  Modelica.Mechanics.Translational.Interfaces.Flange_a flange_a1 if not(isRotational) annotation (Placement(transformation(extent={{-110,-50},{-90,-30}})));

equation 

  if isRotational then
    flux = flange_a.tau;
    potential = flange_a.phi;
  else
    flux = flange_a1.f;
    potential = flange_a1.s;
  end if;

  annotation ();
end component_2_connectors;
  • So I have read several threads regarding conditional declaration (especially this one #14668468).
  • I thought of extending two partial models containing each a connector and the associated equations, but I did not find a way to conditionnaly extend a partial model.
  • I don't mind having both connectors enabled at all time, as long as only one is visible in the icon view (see below). But did not work either.
model component_2_connectors

  parameter Boolean isRotational = true;

  Real flux;
  Real potential;

  Modelica.Mechanics.Rotational.Interfaces.Flange_a flange_a annotation (Placement(transformation(extent={{-110,30},{-90,50}}), **visible= isRotational**));
  Modelica.Mechanics.Translational.Interfaces.Flange_a flange_a1 annotation (Placement(transformation(extent={{-110,-50},{-90,-30}}), **visible= not(isRotational)**));

equation 

  if (cardinality(flange_a) == 0) then
    flange_a.tau = 0;
    flange_a.phi = 0;
  end if;

  if (cardinality(flange_a1) == 0) then
    flange_a1.f = 0;
    flange_a1.s = 0;
  end if;

  if isRotational then
    flux = flange_a.tau;
    potential = flange_a.phi;
  else
    flux = flange_a1.f;
    potential = flange_a1.s;
  end if;

  annotation ();
end component_2_connectors;

Do you have any suggestion?

Clement44
  • 13
  • 4

1 Answers1

1

One problem when dealing with conditional components is that they can only be used in connect statements (at least in pedantic mode).

To work around this problem, internal connectors are used often, which are always present. Originally, in this answer they came to use aswell, but this made the model rather complicated and, for reasons I have not fully understood, it was not possible to add an equation for flux or potential.

Here is my third solution attempt: Instead of using internal connectors we make use of the fact that we can add a binding equation when a conditional component is declared. This way we come around the issue that they cannot be used in equations.

model component_2_connectors

  parameter Boolean isRotational = true;

  Real flux;
  Real potential;

  Modelica.Mechanics.Rotational.Interfaces.Flange_a flange_a(tau=flux, phi=potential) if isRotational annotation (Placement(transformation(extent={{-110,30},{-90,50}})));
  Modelica.Mechanics.Translational.Interfaces.Flange_a flange_a1(f=flux, s=potential) if not(isRotational) annotation (Placement(transformation(extent={{-110,-50},{-90,-30}})));

equation 

  flux = 1;

end component_2_connectors;

Regarding your solution attempt: keep in mind that cardinality is deprecated and should not be used any more.

marco
  • 5,944
  • 10
  • 22
  • 1
    Note that the conditional connectors are of different types so they cannot directly be connected to the same intermediate flange. Having two internal flanges of different types, or using a conditional Modelica.Mechanics.Rotational.Components.IdealGearR2T would solve that, but it becomes quite messy. – Hans Olsson Jan 24 '23 at 15:11
  • Thanks, I have totally overseen that. I will come back with a better answer. – marco Jan 24 '23 at 16:49
  • 1
    Thank you both for your replies. Although it works, I can't figure out why it is not possible to input the value of the flux or the potential. Somehow, the internalFlange_r.tau / internalFlange_t.f is set at zero, and adding another equation would overdetermined the system of equations... Any ideas? – Clement44 Jan 25 '23 at 10:59
  • You are right, the second solution was also a mess. I updated my answer. Little tests worked fine, hope it works for you as well. – marco Jan 25 '23 at 12:11
  • Third time's the charm! Thank you @marco for this elegant solution, which works as expected, whether I define a flux or a potential in my component. Now, on to applying this example to my actual problem :) – Clement44 Jan 25 '23 at 12:39