modelica / ModelicaStandardLibrary

Free (standard conforming) library to model mechanical (1D/3D), electrical (analog, digital, machines), magnetic, thermal, fluid, control systems and hierarchical state machines. Also numerical functions and functions for strings, files and streams are included.
https://doc.modelica.org
BSD 3-Clause "New" or "Revised" License
453 stars 165 forks source link

Result Compare fail caused by atan2 in Modelica Standard Library #4244

Open TongYuan-MC opened 6 months ago

TongYuan-MC commented 6 months ago

This phenomenon has a negative impact on the following situations

  1. Software benchmarking with other Modelica tool venders
  2. Tool certification
  3. Simulation in Critical Control System

Example for illustration

atan2(x, y) will change significantly when x and y change slightly near 0 (same issue for atan3). See blow:

image

The following simple Modelica model which also show this:

model Atan2Issue "z1 or z2 may differ sigificantly around pi while abs(x - y) is small"
  Real x(start = 0);
  Real y = sin(time);
  Real absxy = abs(x - y);
  Real z1, z2;
  annotation(experiment(Interval=0.0001,StartTime=0,StopTime=8));
equation
  der(x) = cos(time);  // x and y are equal in mathematical sense
  z1 = atan2(y, x);
  z2 = atan2(x, y);
end Atan2Issue;

We can see for Atan2Issue:

  1. Different Modelica tools with same numerical setting has non-negligible error.
  2. Same Modelica tool with same numerical setting except tolerance also has non-negligible error.

Modelica Tool:

  1. MWORKS: MWORKS.Sysplorer 2024a
  2. Dymola: Dymola 2024x trival version
  3. OpenModelica: OpenModelica 1.22.1
Simulation Result of Atan2Issue by MWORKS, Dymola and OpenModelica ### Atan2Issue `z1` and `z2` should be $\pm \frac{\pi}{4}$ in mathematical sense. However `x` was got by numerical method such as Dassl, small differences in x and y near 0 are overamplified by `atan2`. As a result, `z1` and `z2` are unpredictable near integer multiples of $\pi$. #### MWORKS with Tolerance `1e-6`(default) and `1e-7` ![Atan2Issue1e_6_vs_1e_7_MWORKS](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/9f87b3ee-b9c4-4ad0-8f98-83c9dae28fa6) #### Dymola with Tolerance `1e-6`(default) and `1e-7` ![Atan2Issue1e_6_vs_1e_7_Dymola png](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/249c1559-f723-4875-87a3-d133292ecc04) #### OpenModelica with Tolerance `1e-6`(default) and `1e-7` ![Atan2Issue1e_6_vs_1e_7_OpenModelica](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/0dcfb592-019b-4467-bbf9-b33cba6c8886) #### MWORKS, Dymola and OpenModelica with Tolerance `1e-6`(default) ![Atan2Issue1e_6](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/eb42bdb4-13f1-4f5e-9f92-a1b1ad3564f2) #### MWORKS, Dymola and OpenModelica with Tolerance `1e-7` ![Atan2Issue1e_7](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/bb910cdc-fb5d-4ee5-bf55-d3844b4d39fc)

There are many models in Modelica Standard Library using atan2 and atan3, This leads to problems when doing result comparing.

The following two example models in Modelica Standard Library 4.0 have same issue as model Atan2Issue

Simulation Result of SMEE_DOL by MWORKS, Dymola and OpenModelica ### Modelica.Magnetic.FundamentalWave.Examples.BasicMachines.SynchronousMachines.SMEE_DOL (Modelica Standard Library 4.0) #### MWORKS with Tolerance `1e-6`(default) and `1e-7` ![SMEE_DOL1e_6_vs_1e_7_MWORKS](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/bbda140e-ffa6-49d2-b9d6-57279f6f77dd) #### Dymola with Tolerance `1e-6`(default) and `1e-7` ![SMEE_DOL1e_6_vs_1e_7_Dymola](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/14000c18-e58b-414b-9e02-6283293ac019) #### OpenModelica with Tolerance `1e-6`(default) and `1e-7` ![SMEE_DOL1e_6_vs_1e_7_OpenModelica](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/2aab0f00-86e6-4441-a70c-833c318907e7) #### MWORKS, Dymola and OpenModelica with Tolerance `1e-6`(default) ![SMEE_DOL1e_6](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/9d71c2d0-cad6-488d-be08-a3013c672bbc) #### MWORKS, Dymola and OpenModelica with Tolerance `1e-7` ![SMEE_DOL1e_7](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/aaacbebc-f21b-4565-b40a-e9493a36f2d0)
Simulation Result of SoftStarter by Dymola 2024x and OpenModelica 1.22.1 ### Modelica.Electrical.PowerConverters.Examples.ACAC.SoftStarter (Modelica Standard Library 4.0) #### MWORKS with Tolerance `1e-6`(default) and `1e-7` ![SoftStarter1e_6_vs_1e_7_MWORKS](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/f9536bab-bfeb-4ba3-9622-19c905c04e8f) #### Dymola with Tolerance `1e-6`(default) and `1e-7` ![SoftStarter1e_6_vs_1e_7_Dymola](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/dc7eed84-7c95-4614-b1e4-add2e31ea7ba) #### OpenModelica with Tolerance `1e-6`(default) and `1e-7` ![SoftStarter1e_6_vs_1e_7_OpenModelica](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/170ef495-e0d5-41e8-9a97-ced3ecd1f465) #### MWORKS, Dymola and OpenModelica with Tolerance `1e-6`(default) ![SoftStarter1e_6](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/97c64e71-ec4f-489a-a969-52fa8e25006d) #### MWORKS, Dymola and OpenModelica with Tolerance `1e-7` ![SoftStarter1e_7](https://github.com/modelica/ModelicaStandardLibrary/assets/153701357/2e26726b-cc54-48f5-a743-2de1efe185a0)

Could you give us some advice on dealing with such models?

  1. Is there an alternative to the atan2 and atan3 function when Modeling?
  2. Will Modelica Standard Library updating to avoid this phenomenon?
  3. Is there any mechanism at the language level such as HideResult to avoid this phenomenon?
tobolar commented 6 months ago

FYI: Dymola 2024x, 2023-10-06

Modelica.Math.atan2(0, 1e-14);
 = 0.0
Modelica.Math.atan2(1e-14, 1e-14);
 = 0.7853981633974483
Modelica.Math.atan2(-1e-14, 1e-14);
 = -0.7853981633974483
Modelica.Math.atan2(-1e-14, -1);
 = -3.1415926535897833
Modelica.Math.atan2(1e-14, -1);
 = 3.1415926535897833
HansOlsson commented 6 months ago

FYI: Dymola 2024x, 2023-10-06

Modelica.Math.atan2(0, 1e-14);
 = 0.0
Modelica.Math.atan2(1e-14, 1e-14);
 = 0.7853981633974483
Modelica.Math.atan2(-1e-14, 1e-14);
 = -0.7853981633974483
Modelica.Math.atan2(-1e-14, -1);
 = -3.1415926535897833
Modelica.Math.atan2(1e-14, -1);
 = 3.1415926535897833

That is exactly as described in the C/C++-standard, when implementing IEC 60559 (normative appendix F). I thus cannot see that atan2 is a problem.

We would need more information which specific models cause problems - it could be that the result of atan2 should be stored, but not compared for regression testing. (Using HideResult is not a good idea.)

TongYuan-MC commented 6 months ago

We are not saying there's something wrong with atan2.

Our ideal is numerical algorithms always have errors, for model x(start = 0); der(x) = cos(time), x won't be exact sin(time), so atan2(x, sin(time)) will not exact $\pm \frac{\pi}{4}$, and unpredictable when time is near an integer multiples of $\pi$.

When modifying certain strategies of numerical algorithms, we always get result comparison failures.

Please click the simulation result in origin comment, see blow: image

henrikt-ma commented 6 months ago

I believe this (issue or not) should be discussed within https://github.com/modelica-tools/csv-compare instead.

beutlich commented 6 months ago

Or should the corresponding reference signals of SoftStarter and SMEE_DOL be removed?

henrikt-ma commented 6 months ago

Or should the corresponding reference signals of SoftStarter and SMEE_DOL be removed?

I haven't checked all of the signals mentioned above, but if we take SoftStarter's imc.rotorCage.arg_V.m for example, it is not among the comparison signals: https://github.com/modelica/MAP-LIB_ReferenceResults/blob/v4.0.0/Modelica/Electrical/PowerConverters/Examples/ACAC/SoftStarter/comparisonSignals.txt

AHaumer commented 6 months ago

I disagree in removing comparison signals, this makes difficult comparing versions. We should shift the discussion to a "general" atan2-discussion, not related to special models.

AHaumer commented 6 months ago

OM v1.20.0 (64-bit) same results as mentioned by @tobolar and @HansOlsson for Dymola.