Opened 7 years ago

Closed 7 years ago

#4828 closed defect (fixed)

Connection equations involving Complex numbers are redundant and incorrect

Reported by: Francesco Casella Owned by: Per Östlund
Priority: critical Milestone: 2.0.0
Component: New Instantiation Version:
Keywords: Cc: Mahder Alemseged Gebremedhin

Description

Consider the attached test package. Flattening TestComplexConnect.Circuit1 with the old FE results in the following equations, which are correct and correspond to 8 scalar equations

  V1.p.v.re = V1.v.re;
  V1.p.v.im = V1.v.im;
  Z1.p.v = /*.Modelica.SIunits.ComplexVoltage*/(Complex.'*'.multiply(/*.Complex*/(Z1.Z), /*.Complex*/(Z1.p.i)));
  V1.p.i.re + Z1.p.i.re = 0.0;
  V1.p.i.im + Z1.p.i.im = 0.0;
  V1.p.v.re = Z1.p.v.re;
  V1.p.v.im = Z1.p.v.im;

Flattening with the new FE results in the following set of equations, which correspond to 12 scalar equations (even though the compiler repoorts only 11), on the same 8 variables.

  V1.p.v = Z1.p.v;
  V1.p.i + Z1.p.i = 0.0;
  V1.p.v = V1.v;
  Z1.p.v = Complex.'*'.multiply(Z1.Z, Z1.p.i);
  V1.p.i.re = 0.0;
  V1.p.i.im = 0.0;
  Z1.p.i.re = 0.0;
  Z1.p.i.im = 0.0;

The first issue is that the old FE flattens the simple record equations to their scalar components, while the new one doesn't. I guess this is a new feature to keep the FE workload to a minimum, and I understand the back-end will then expand those equations to their scalar counterparts. Please confirm.

The second issue is that, for some reason, connection equations are generated twice: once correctly (see the first two record equations in the flattened model), then once more as if the connect statement was not there, thus setting the real and imaginary parts of both V1.p.i and Z1.p.i to zero. Flattening Circuit1a further confirms this conjecture.

I guess this is due to the code handling connection equations not being correctly designed to handle complex connector variables. This needs to be fixed, as there are many libraries for power system modelling (also in the MSL) that use complex numbers in the connectors .

Attachments (1)

TestComplexConnect.mo (1.3 KB ) - added by Francesco Casella 7 years ago.

Download all attachments as: .zip

Change History (5)

by Francesco Casella, 7 years ago

Attachment: TestComplexConnect.mo added

comment:1 by Francesco Casella, 7 years ago

Cc: Mahder Alemseged Gebremedhin added

comment:2 by Francesco Casella, 7 years ago

Could also be related to #2361

comment:3 by Per Östlund, 7 years ago

Partially fixed in 4204ff0. The connect equations are now correct, but it seems there are still issues remaining. One of the issues is that the backend doesn't seem to like that the Complex constructors are not evaluated, for that we need function evaluation since Complex uses an overloaded normal function as constructor. Another possible issue is as you say that complex equations aren't split into scalar equations.

comment:4 by Francesco Casella, 7 years ago

Resolution: fixed
Status: newclosed

I flattened the test model attached to this ticket after the last commit. This is the result:

class TestComplexConnect.Circuit1
  parameter Real V1.v.re(quantity = "ElectricPotential", unit = "V") "Real part of complex number";
  parameter Real V1.v.im(quantity = "ElectricPotential", unit = "V") "Imaginary part of complex number";
  Real V1.p.v.re(quantity = "ElectricPotential", unit = "V") "Real part of complex number";
  Real V1.p.v.im(quantity = "ElectricPotential", unit = "V") "Imaginary part of complex number";
  Real V1.p.i.re(quantity = "ElectricCurrent", unit = "A") "Real part of complex number";
  Real V1.p.i.im(quantity = "ElectricCurrent", unit = "A") "Imaginary part of complex number";
  Real Z1.p.v.re(quantity = "ElectricPotential", unit = "V") "Real part of complex number";
  Real Z1.p.v.im(quantity = "ElectricPotential", unit = "V") "Imaginary part of complex number";
  Real Z1.p.i.re(quantity = "ElectricCurrent", unit = "A") "Real part of complex number";
  Real Z1.p.i.im(quantity = "ElectricCurrent", unit = "A") "Imaginary part of complex number";
  parameter Real Z1.Z.re(quantity = "Resistance", unit = "Ohm") "Real part of complex number";
  parameter Real Z1.Z.im(quantity = "Resistance", unit = "Ohm") "Imaginary part of complex number";
equation
  V1.p.v.re = Z1.p.v.re;
  V1.p.v.im = Z1.p.v.im;
  Z1.p.i.re + V1.p.i.re = 0.0;
  Z1.p.i.im + V1.p.i.im = 0.0;
  V1.v = Complex.'constructor'.fromReal(1.0, 0.0);
  V1.p.v = V1.v;
  Z1.Z = Complex.'constructor'.fromReal(0.0, 1.0);
  Z1.p.v = Modelica.SIunits.ComplexImpedance.'*'.multiply(Z1.Z, Z1.p.i);
end TestComplexConnect.Circuit1;

The main issue that I see is that for some reason the binding equations for the Complex parameters V1.v and Z1.Z were moved from the parameter binding equations to the equation section, which is just plain wrong. Apart from that, the flat model would be fine.

It is true that for some reason there are still some Complex equations that are not reduced to their scalar constituents, but this doesn't make them invalid, it only may prevent some useful optimizations. This is the subject of #4835.

I also tried the Circuit1a model of the test package, and the connection equations are correct also in that case. I thus believe that the issue this ticket was opened for (incorrect connection equations for Complex numbers) has been solved successfully and that this ticket can be closed.

I just opened #4872 for the new issue described above.

Note: See TracTickets for help on using tickets.