modelicadymola

Conditionally enabling one of two connectors in modelica


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;
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?


Solution

  • 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.