Opened 11 years ago

Closed 10 years ago

#2352 closed defect (fixed)

New Bug in model calling random function with two outputs

Reported by: Peter Fritzson Owned by: Mahder Alemseged Gebremedhin
Priority: high Milestone: 1.9.2
Component: Backend Version: trunk
Keywords: Cc: Mahder Alemseged Gebremedhin

Description (last modified by Lennart Ochel)

This model has worked fine in OpenModelica for several years. It worked fine in March 2013. It is used in several models
in DrModelica and in Peter Fritzsons book.

Now it cannot simulate. OpenModelica r17270,
see the model below.

simulate(TestRandom,stopTime=10)

record SimulationResult
    resultFile = "",
    messages = "Simulation failed for model: TestRandom
Warning: Assuming fixed start value for the following 6 variables:
         randomSeed[1]:DISCRETE(start = 23 fixed = false ) .TestRandom, .Integer type: Integer [3]
         randomSeed[2]:DISCRETE(start = 87 fixed = false ) .TestRandom, .Integer type: Integer [3]
         randomSeed[2]:DISCRETE(start = 87 fixed = false ) .TestRandom, .Integer type: Integer [3]
         randomSeed[3]:DISCRETE(start = 187 fixed = false ) .TestRandom, .Integer type: Integer [3]
         randomSeed[1]:DISCRETE(start = 23 fixed = false ) .TestRandom, .Integer type: Integer [3]
         servTime:DISCRETE(fixed = false ) .TestRandom, .Real type: Real 
Error: Fel vid bygge av simulator. Loggfil: gcc   -falign-functions -msse2 -mfpmath=sse   -I\"C:/OpenModelica1.9.0//include/omc\" -I.   -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o TestRandom.o TestRandom.c
TestRandom.c: In function 'initializeStaticNLSData7':
TestRandom.c:140: error: 'INTEGER_ATTRIBUTE' has no member named 'nominal'
TestRandom.c: In function 'residualFunc7':
TestRandom.c:157: error: lvalue required as unary '&' operand
TestRandom.c:158: error: incompatible types when assigning to type 'double' from type 'integer_array'
mingw32-make: *** [TestRandom.o] Error 1
package Random

import Modelica.Math; // import might not be needed
constant Real NV_MAGICCONST=4*exp(-0.5)/sqrt(2.0);
type Seed = Real[3];

function random "input random number generator with
external storage of the seed"
  input Seed si "input random seed";
  output Real x "uniform random variate between 0 and 1";
  output Seed so "output random seed";
algorithm 
  so[1] := abs(rem((171 * si[1]),30269));
  so[2] := abs(rem((172 * si[2]),30307));
  so[3] := abs(rem((170 * si[3]),30323));
  // zero is a poor Seed, therfore substitute 1;
  if so[1] == 0 then
    so[1] := 1;
  end if;
  if so[2] == 0 then
    so[2] := 1;
   end if;
  if so[3] == 0 then
    so[3] := 1;
  end if;
  x := rem((so[1]/30269.0 +so[2]/30307.0 +
    so[3]/30323.0),1.0);
end random;

function normalvariate "normally distributed random
variable"
  input Real mu "mean value";
  input Real sigma "standard deviation";
  input Seed si "input random seed";
  output Real x "gaussian random variate";
  output Seed so "output random seed";
  protected 
  Seed s1, s2;
  Real z, zz, u1, u2;
  Boolean breakcond=false;
algorithm 
  s1 := si;
  u2 := 1;
  while not breakcond loop
    (u1,s2) := random(s1);
    (u2,s1) := random(s2);
    z := NV_MAGICCONST*(u1-0.5)/u2;
    zz := z*z/4.0;
    breakcond := zz <= (- Math.log(u2));
   end while;
   x := mu + z*sigma;
  so := s1;
end normalvariate;

  model TestRandom
    Real   servTime;
    parameter Real  mean =  2.0;
    parameter Real  stdev = 0.5;
     Random.Seed  randomSeed(start={23,87,187});
  equation 
    when sample(0,1) then
    (servTime,randomSeed) = Random.normalvariate(mean,stdev,pre(randomSeed));
    end when;
  end TestRandom;
end Random;

Change History (6)

comment:1 by Lennart Ochel, 11 years ago

Description: modified (diff)
Owner: changed from probably noone to Lennart Ochel
Status: newassigned

comment:2 by Lennart Ochel, 11 years ago

Cc: Mahder Alemseged Gebremedhin added

This is an issue with the handling of arrays. You can easily replace type Seed with simple Real vars (without an array) and the example is working again.
Anyway we need to fix this soon – Mahder, can you have a look?

I created a test here: testsuite/simulation/modelica/others/Random2.mos

comment:3 by Mahder Alemseged Gebremedhin, 11 years ago

Owner: changed from Lennart Ochel to Mahder Alemseged Gebremedhin
Status: assignedaccepted

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

Milestone: 1.9.01.9.1

Postponed until 1.9.1

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

Milestone: 1.9.11.9.2

This ticket was not closed for 1.9.1, which has now been released. It was batch modified for milestone 1.9.2 (but maybe an empty milestone was more appropriate; feel free to change it).

comment:6 by Lennart Ochel, 10 years ago

Resolution: fixed
Status: acceptedclosed

This is working again.

Note: See TracTickets for help on using tickets.