Link Math & Connectors as one Model

84 views Asked by At

I would like to link a mathematical model to the Electrical connectors, for example, if I want to make a box with just a parallel resistance but with only the programming language (not the graphical representation or any components (except Interface connectors)):

i1  x--------------x i2
          |
          -
         |R| Resistance
          -
          |
-i1 x--------------x -i2

Code:

model MathBox
  parameter Modelica.Units.SI.Resistance R = 1;
  extends Modelica.Electrical.Analog.Interfaces.TwoPort();

equation
  v1 = v2;
  i1 = i2 + v1/R;

end MathBox;

I just cannot make it work, it compiles & simulates but there is no voltage accross v1 and no current flowing at all through after I add this model with a voltage source on v1 side and another resistor on v2 side. I used TwoPort Interface connector after trying Pin or TwoPin with the same result.

I must forget something, any idea ?

Best regards

2

There are 2 answers

3
Markus A. On BEST ANSWER

The model MathBox seems to be nearly fine, not exactly equivalent to your circuit though, which seems to cause the error.

When setting v1 = v2 you "only" set the voltage difference between the two pins to be equal. Modelica tools usually compute based on potentials, which is not set unless you connect a mass on both sides. So with your current model, the testing circuit should look something like this:

Testing circuit for MathModel

I think there is a sign error in the model, and it should be:

model MathBox
  parameter Modelica.Units.SI.Resistance R = 1;
  extends Modelica.Electrical.Analog.Interfaces.TwoPort;

equation 
  v1 = v2;
  i1 = -i2 + v1/R;

end MathBox;

which seems to give correct results.

In case you really want to have the circuit depicted above, you need to connect potentials, which can be done in multiple ways, one should be:

model ResBox
  extends Modelica.Electrical.Analog.Interfaces.FourPin;

  parameter Modelica.Units.SI.Resistance R = 1;

  Modelica.Units.SI.Current iR "Current through resistor";

equation 

  p1.v = p2.v;
  n1.v = n2.v;

  v1 = iR*R;

  0 = p1.i + p2.i - iR;
  0 = n1.i + n2.i + iR;

  annotation (uses(Modelica(version="4.0.0")));
end ResBox;

Note, that the extends has changed to give the possibility to add some more equations.
Using the ResBox in the testing circuit, you could remove one mass...

0
Hans Olsson On

I'm unsure exactly what you want to model, but let's start with a a simplified Resistor-model based on Modelica.Electrical.Analog.Basic.Resistor (but without heat-part and some other stuff):

model SimpleResistor
  extends Modelica.Electrical.Analog.Interfaces.OnePort;
  parameter Modelica.Units.SI.Resistance R=1;
equation
  // From OnePort: p.i+n.i=0;
  v=R*p.i; // Where v=p.v-n.v;
end SimpleResistor;

So:

  • The p.v and n.v are Voltages relative to ground (i.e., potentials), in equations you should normally use Voltage-differences; like v from OnePort/TwoPin
  • The sign-convention for currents is different from what you assumed. All currents going into a component should sum to zero.
  • Try to avoid division when not needed; so prefer v=R*i over i=v/R. Tools will divide automatically when needed.

In a circuit you could have:

model Unnamed
  Modelica.Electrical.Analog.Basic.Ground ground
    annotation (Placement(transformation(extent={{-38,-6},{-18,14}})));
  SimpleResistor resistor(R=1)
    annotation (Placement(transformation(extent={{-54,44},{-34,64}})));
  Modelica.Electrical.Analog.Sources.SineVoltage sineVoltage(V=20, f=50)
    annotation (Placement(transformation(extent={{-88,44},{-68,64}})));
equation 
  connect(sineVoltage.n, resistor.p)
    annotation (Line(points={{-68,54},{-54,54}}, color={0,0,255}));
  connect(resistor.n, ground.p)
    annotation (Line(points={{-34,54},{-28,54},{-28,14}}, color={0,0,255}));
  connect(sineVoltage.p, ground.p) annotation (Line(points={{-88,54},{-92,54},{-92,
          20},{-28,20},{-28,14}}, color={0,0,255}));
  annotation (uses(Modelica(version="4.0.0")));
end Unnamed;