Opened 7 years ago

Closed 7 years ago

#4550 closed defect (invalid)

Integer calculation not working

Reported by: Christian Kral <dr.christian.kral@…> Owned by: Willi Braun
Priority: high Milestone:
Component: Run-time Version:
Keywords: Cc: Adrian Pop

Description

There is a proposal on https://github.com/modelica/Modelica/pull/2341/commits/e46a08514a8d30af3407d5f54ed5a67e9bf0e2fb to implement the function Modelica.Electrical.MultiPhase.Functions.numberOfSymmetricBaseSystems in an alternative way. I thus wrote a test function to check whether the original and the proposed functions calculate the save results.

Consider the following testing package:

package TestBase
  function numberOfSymmetricBaseSystems "Determines the number of symmeric base systems of m phase symmetric system"
    extends Modelica.Icons.Function;
    input Integer m=3 "Number of phases";
    output Integer n "Number of symmetric base systems";
  algorithm
     n := if mod(m, 2) == 0 then product(if mod(m, i) == 0 then 2 else 1 for i in {2^j for j in 1:integer(log(m/2)/log(2))}) else 1;
  end numberOfSymmetricBaseSystems;

  model Test
    parameter Integer n=9;
    parameter Integer error(start=0,fixed=false);
  initial algorithm
    for k in 1:n loop
      error:=error+(Modelica.Electrical.MultiPhase.Functions.numberOfSymmetricBaseSystems(k)-TestBase.numberOfSymmetricBaseSystems(k))*
                   (Modelica.Electrical.MultiPhase.Functions.numberOfSymmetricBaseSystems(k)-TestBase.numberOfSymmetricBaseSystems(k));
    end for;
  end Test;

  model Test2
    parameter Integer B2 = Modelica.Electrical.MultiPhase.Functions.numberOfSymmetricBaseSystems(2);
    parameter Integer b2 = TestBase.numberOfSymmetricBaseSystems(2);
    parameter Integer B4 = Modelica.Electrical.MultiPhase.Functions.numberOfSymmetricBaseSystems(4);
    parameter Integer b4 = TestBase.numberOfSymmetricBaseSystems(4);
    parameter Integer B16 = Modelica.Electrical.MultiPhase.Functions.numberOfSymmetricBaseSystems(16);
    parameter Integer b16 = TestBase.numberOfSymmetricBaseSystems(16);
  end Test2;
  
  annotation (uses(Modelica(version="3.2.2")));
end TestBase; 

The function TestBase.numberOfSymmetricBaseSystems is the alternative function proposal. Example TestBase.Test shall lead to error=0 independent of parameter n. Since the result was not equal to zero, I traced down the problem to even input parameter m investigated in TestBase.Test2.

The result of TestBase.Test2 is:

B[2] == b[2] -> OK
B[4] <> b[4] -> wrong 
B[16] <> b[16] -> wrong 

I double checked the calculations in Dymola where TestBase.Test2 is working OK.

Change History (7)

comment:1 by Adeel Asghar, 7 years ago

Cc: Adrian Pop added
Component: OMEditRun-time
Owner: changed from Adeel Asghar to Willi Braun
Status: newassigned

comment:2 by Martin Sjölund, 7 years ago

OMC thinks numberOfSymmetricBaseSystems always returns 1 because {2^j for j in 1:integer(log(m/2)/log(2))} is an empty array. I'll see if I can fix that...

comment:3 by Martin Sjölund, 7 years ago

This failed due to the bad fix in #3150 (should be fixed in the library upstream in my opinion).

comment:4 by Martin Sjölund, 7 years ago

Milestone: Future1.13.0
Resolution: fixed
Status: assignedclosed

comment:5 by Martin Sjölund, 7 years ago

Resolution: fixed
Status: closedreopened

comment:6 by Martin Sjölund, 7 years ago

The issue with generating the code is fixed with https://github.com/OpenModelica/OMCompiler/pull/1870, but I think the model is not good; rounding errors in the value sent to integer() causes the errors.

comment:7 by Martin Sjölund, 7 years ago

Milestone: 1.13.0
Resolution: invalid
Status: reopenedclosed

I'll close this as invalid despite fixing the most severe bug in the ticket. @tbeu closed the original pull request since floating point calculations are harder to get right than the integer math currently in MSL.

Note: See TracTickets for help on using tickets.