#4793 closed defect (fixed)
Complex division is not working in MSL simulation example
Reported by: | Owned by: | Karim Adbdelhak | |
---|---|---|---|
Priority: | blocker | Milestone: | 1.14.0 |
Component: | Backend | Version: | |
Keywords: | Cc: | Lennart Ochel, Willi Braun, Francesco Casella |
Description
I recently implemented a Bode block in the MSL, see https://github.com/modelica/Modelica/issues/2473. Today I improved the Bode model in the MSL order to overcome some numerical and initialization problems.
Now, please consider example Modelica.Electrical.QuasiStationary.SinglePhase.Examples.SeriesBode
(on master):
- The simulation example simulates with no obvious complaints
- The complex inputs bode.u and bode.divisor look reasonable
- The division of the two complex inputs, however, is incorrect (see attached file), i.e.,
divison.y.re = division.y.im = 0
... the results shall NOT be zero
I do not understand where this comes from, as the simple test example
model TestComplexDivision Modelica.ComplexBlocks.Sources.ComplexConstant complexConst1(k = Complex(1, 1)) annotation( Placement(visible = true, transformation(origin = {-70, 10}, extent = {{-10, -10}, {10, 10}}, rotation = 0))); Modelica.ComplexBlocks.Sources.ComplexConstant complexConst(k = Complex(2, 3)) annotation( Placement(visible = true, transformation(origin = {-70, -30}, extent = {{-10, -10}, {10, 10}}, rotation = 0))); Modelica.ComplexBlocks.ComplexMath.Division division1 annotation( Placement(visible = true, transformation(origin = {-30, -10}, extent = {{-10, -10}, {10, 10}}, rotation = 0))); equation connect(complexConst.y, division1.u2) annotation( Line(points = {{-58, -30}, {-50, -30}, {-50, -16}, {-42, -16}, {-42, -16}}, color = {85, 170, 255})); connect(complexConst1.y, division1.u1) annotation( Line(points = {{-58, 10}, {-50, 10}, {-50, -4}, {-42, -4}, {-42, -4}}, color = {85, 170, 255})); end TestComplexDivision;
works OK and the division1.y shows the correct result.
So TestComplexDivision
works OK, but Modelica.Electrical.QuasiStationary.SinglePhase.Examples.SeriesBode
does not work.
This issue applies to
OMEdit 1.13.0~dev-106-g36aa34a Connected to OpenModelica 1.13.0~dev-729-gdbcd957
on a 64bit Linux machine.
Attachments (6)
Change History (34)
by , 7 years ago
Attachment: | screen.png added |
---|
comment:1 by , 7 years ago
Cc: | added |
---|---|
Component: | OMEdit → Backend |
Milestone: | Future → 1.13.0 |
Owner: | changed from | to
Priority: | high → critical |
The variable seems to be wrongly assumed to be time-independent and the equation is probably calculated without even using the correct time=0 values of the input for this reason... The first place where this occurs seems to be at "pre-optimization module removeSimpleEquations (simulation)". --removeSimpleEquations=none
makes the model simulate fine.
comment:2 by , 6 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
After the last commits to the NF, with v1.13.0-dev-1096-g6770e6300 this test model now works and produces the correct result, namely 0.3846 - j0.0769.
comment:3 by , 6 years ago
I just checked again on Modelica.Electrical.QuasiStationary.SinglePhase.Examples.SeriesBode
and it turns out that this example is not simulating correctly.
OMEdit 1.14.0~dev-48-g43ebf04 Connected to OpenModelica 1.14.0~dev-353-g7912862 Connected to OMSimulator unknown-linux
I do not know if this is still relevant for the "old compiler" or if I have to use the option --removeSimpleEquations=none
(which I could not get to work; I do not know where to put in the simulation setup dialog): However, I just wanted to report it. You may respond, that this is not relevant any more...
by , 6 years ago
Attachment: | Modelica.Electrical.QuasiStationary.SinglePhase.Examples.SeriesBode.png added |
---|
Simulation result
comment:4 by , 6 years ago
To be more precise: "not simulating correctly" means the shown "Simulation result" shall NOT be zero.
comment:5 by , 6 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
The issue also appears using -d=newInst
.
comment:7 by , 6 years ago
I can't reproduce this, simulating the SeriesBode
model from MSL 3.2.3 with both the old and the new instantiation gives the same results, see the attached image.
by , 6 years ago
Attachment: | SeriesBode.png added |
---|
comment:8 by , 6 years ago
We do test against the reference results of MA, but these seem to only compare one variable (inductor.pin_p.reference.gamma)...
follow-ups: 10 11 comment:9 by , 6 years ago
This is strange: I attached a screenshot of my results including the info on the MSL and version number of OMEdit. Could that be a Linux issue?
However, comparing just inductor.pin_p.reference.gamma
may not be sufficient. Could it actually be possible that I have no access to the MA link you provided? Even https://github.com/modelica/MAP-LIB_ReferenceResults seems not to be accessible to me. In both cases I get a 404 error message (this is not the web page you are looking for)
by , 6 years ago
Attachment: | SeriesBode.2.png added |
---|
SeriesBode simulation result including MSL version number
comment:10 by , 6 years ago
Replying to Christian Kral <dr.christian.kral@…>:
Could that be a Linux issue?
Probably not, I also used 64-bit Linux when simulating the model. I used a slightly newer version of OMC than you have (git master at this moment), and there seems to have been a few library updates since your version. So I guess it's possible that this was actually broken before but somehow got fixed in the last few days. Can you update to the latest nightly build and try again?
comment:11 by , 6 years ago
Replying to Christian Kral <dr.christian.kral@…>:
Could it actually be possible that I have no access to the link you provided? Even https://github.com/modelica/MAP-LIB_ReferenceResults seems not to be accessible to me. In both cases I get a 404 error message (this is not the web page you are looking for)
Most probably. I have added the MAP-LIB group to have read access to that for now (it's a new repo so it's missing some rights).
follow-up: 14 comment:12 by , 6 years ago
I installed
OMEdit 1.14.0~dev-48-g43ebf04 Connected to OpenModelica 1.14.0~dev-355-gd61da53 Connected to OMSimulator unknown-linux
and I see no difference. The output of two variables abs_y
and arg_y
is always identical to zero.
comment:13 by , 6 years ago
I actually noticed that this thicket was closed three months ago, referring to v1.13.0-dev-1096-g6770e6300
. I just now tested the model with
OMEdit v1.13.1 (64-bit) Connected to v1.13.1 (64-bit) Connected to OMSimulator v2.0.0-mingw
on a Windows system remotely, which shows also zero results for abs_y
and arg_y
.
by , 6 years ago
Attachment: | SeriesBode_windows.png added |
---|
SeriesBode on Windows 7 with OMEdit 1.13.1
comment:14 by , 6 years ago
When re-reading my response I realized that the version number did not change after updating from the repository
I installed
OMEdit 1.14.0~dev-48-g43ebf04 Connected to OpenModelica 1.14.0~dev-355-gd61da53 Connected to OMSimulator unknown-linuxand I see no difference. The output of two variables
abs_y
andarg_y
is always identical to zero.
This seems to be the most recent version on the repository.
follow-up: 16 comment:15 by , 6 years ago
Milestone: | 1.13.0 → 1.14.0 |
---|---|
Owner: | changed from | to
Priority: | critical → blocker |
Status: | reopened → assigned |
I double-checked with yesterday's nightly on Windows
OMEdit v1.14.0-dev-48-g43ebf040 (64-bit)
Connected to v1.14.0-dev-154-g04f106144 (64-bit)
Connected to OMSimulator v2.1.0-dev-64-gafc407f-mingw
The issue is located in the bode.division
complex division block: the two inputs u1
and u2
are computed correctly, but the output y
is incorrectly computed to zero. This happens independently of the used frontend.
I tried to figure out what goes wrong by looking at the -d=optdaedump
output. As already mentioned in comment:2, for some reason the back-end considers the output of that block as a constant expression, so it moves the computation to the initial known variables section, and somehow evaluates it to zero during initialization.
Known variables only depending on parameters and constants - globalKnownVars (49) ======================================== <snip> 32: bode.y.im:VARIABLE(flow=false fixed = false ) = ((if bode.division.useConjugateInput1 then Complex(bode.division.u1.re, -bode.division.u1.im) else bode.division.u1).im * (if bode.division.useConjugateInput2 then Complex(bode.division.u2.re, -bode.division.u2.im) else bode.division.u2).re - (if bode.division.useConjugateInput1 then Complex(bode.division.u1.re, -bode.division.u1.im) else bode.division.u1).re * (if bode.division.useConjugateInput2 then Complex(bode.division.u2.re, -bode.division.u2.im) else bode.division.u2).im) / ((if bode.division.useConjugateInput2 then Complex(bode.division.u2.re, -bode.division.u2.im) else bode.division.u2).re ^ 2.0 + (if bode.division.useConjugateInput2 then Complex(bode.division.u2.re, -bode.division.u2.im) else bode.division.u2).im ^ 2.0) "Imaginary part of complex number" type: Real 33: bode.y.re:VARIABLE(flow=false fixed = false ) = ((if bode.division.useConjugateInput1 then Complex(bode.division.u1.re, -bode.division.u1.im) else bode.division.u1).re * (if bode.division.useConjugateInput2 then Complex(bode.division.u2.re, -bode.division.u2.im) else bode.division.u2).re + (if bode.division.useConjugateInput1 then Complex(bode.division.u1.re, -bode.division.u1.im) else bode.division.u1).im * (if bode.division.useConjugateInput2 then Complex(bode.division.u2.re, -bode.division.u2.im) else bode.division.u2).im) / ((if bode.division.useConjugateInput2 then Complex(bode.division.u2.re, -bode.division.u2.im) else bode.division.u2).re ^ 2.0 + (if bode.division.useConjugateInput2 then Complex(bode.division.u2.re, -bode.division.u2.im) else bode.division.u2).im ^ 2.0) "Real part of complex number" type: Real
The right-hand sides of these two equations seem correct to me, but for some reason they are considered to be known variables. This is of course wrong because bode.division.u1
and bode.division.u2
show up in their expression, and they are most definitely not known variables. I'm not sure where the generated code picks up the constant values of bode.division.u1
and bode.division.u2
during initialization.
As noticed in comment:2, setting --removeSimpleEquations=none
solves the problem, so I guess there's still something wrong there. I would suggest to investigate this with high priority, because it's really a bad bug: simulation works, but results are wrong.
We also need to improve the MSL reference result files for these state-less systems, otherwise we risk not catching other similar errors, but this is a MAP-LIB issue.
comment:16 by , 6 years ago
We also need to improve the MSL reference result files for these state-less systems, otherwise we risk not catching other similar errors, but this is a MAP-LIB issue.
I very much agree that we should avoid to not sense issues due to non-existing states. I had a look at the Model Modelica.Electrical.QuasiStationary.SinglePhase.Examples.SeriesBode
and there the following output variables are defined:
output Real abs_y = bode.abs_y "Magnitude of voltage ratio"; output Modelica.SIunits.AmplitudeLevelDifference dB_y = bode.dB_y "Log10 of magnitude of voltage ratio in dB"; output Modelica.SIunits.Angle arg_y = bode.arg_y "Angle of voltage ratio";
From my understanding there should exist reference results for all variables identified as output quantities, isn't it? From my understanding Leo Gall is managing the reference results of the MSL. Is that correct?
comment:17 by , 6 years ago
Yes, Leo handles the reference results. But I'm sure he would accept a pull request to https://github.com/modelica/MAP-LIB_ReferenceResults or other feedback (the ticket system seems disabled for it though).
comment:19 by , 6 years ago
The main issue regarding Modelica.Electrical.QuasiStationary.SinglePhase.Examples.SeriesBode
should be fixed in commit. Can we close this ticket or are there new issues?
comment:20 by , 6 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
I just ran the Modelica.Electrical.QuasiStationary.SinglePhase.Examples.SeriesBode
example again with the latest Windows nightly v1.14.0-dev-165-gf63f7ac40
, and it seems to me that the output of the bode
block is now correct.
@Christian, please double-check and reopen the ticket if you still find issues.
Thanks @Karim for the quick fix!
follow-up: 22 comment:21 by , 6 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
@Karim: I wouldn't call that a fix if it is hard-coded for Complex.mo and breaks for any other package (or possibly aliases of Complex). We have very little hard-coded code referencing the MSL (mostly things for ModelicaExternalC).
comment:22 by , 6 years ago
Replying to sjoelund.se:
@Karim: I wouldn't call that a fix if it is hard-coded for Complex.mo and breaks for any other package (or possibly aliases of Complex). We have very little hard-coded code referencing the MSL (mostly things for ModelicaExternalC).
Yes that is true, but i did not see another way to detect if something is complex to avoid this special kind of alias. I guess the best way to handle this is, once again, consider stuff like this as array equations and handle them properly. Since i will be working on that in the future i will (hopefully) propose a better solution, but for know i thought this would do.
Do you have other ideas how to handle this?
follow-up: 28 comment:24 by , 6 years ago
I created new reference results for SeriesBode
and sent them to Leo Gall. Unfortunately I cannot create a pull request, as the https://github.com/modelica/MAP-LIB_ReferenceResults is a private repository which cannot be forked...
I expect Leo Gall to update the new reference data anytime soon.
comment:25 by , 6 years ago
@Karim, can you implement @sjoelund's suggestion in comment:23 so we can close this ticket in 1.14.0?
Thanks!
comment:26 by , 6 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
I changed it in commit. It avoids records now, should work with all libraries.
comment:27 by , 6 years ago
This also fixed a numerical initialization failure with Modelica.Fluid.Examples.Explanatory.MomentumBalanceFittings
, which nows passes verification successfully.
comment:28 by , 5 years ago
Replying to Christian Kral <dr.christian.kral@…>:
I created new reference results for
SeriesBode
and sent them to Leo Gall. Unfortunately I cannot create a pull request, as the https://github.com/modelica/MAP-LIB_ReferenceResults is a private repository which cannot be forked...
I expect Leo Gall to update the new reference data anytime soon.
The new reference results are now available.
Result plot of Modelica.Electrical.QuasiStationary.SinglePhase.Examples.SeriesBode