Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#2279 closed defect (fixed)

Initialisation problem in equation section when using random generation function

Reported by: e.ypma@… Owned by: lochel
Priority: high Milestone: 1.9.0
Component: Backend Version: trunk
Keywords: random number generation Cc:

Description

When I try to simulate the model 'test_MR' it results in the first time step in a huge output (~1021).
After that, it never recovers.

// within Random;

record SEEDINFO
  Real s1;
  Real s2;
  Real s3;
end SEEDINFO;

function MathRandom

  input SEEDINFO si;
  input Real tim;
  output Real x(start=0.0);
  output SEEDINFO so;

protected
  Real a=0.0,b=0.0,c=0.0; 
  Real s1=0.0, s2=0.0, s3=0.0; 
algorithm
  
  s1 := si.s1;
  s2 := si.s2;
  s3 := si.s3;
     
  a := mod(tim-11,tim+13);
  a := exp(a);
  a := rem(171*s1*a,30269);
  a := abs(a);
   
  b := mod(tim-5,tim+7); 
  b := exp(b);
  b := rem(172*s2*b,30307);
  b := abs(b);
  
  c := mod(tim-23,tim+76);
  c := exp(c);
  c := rem(170*s3*c,30323);  
  c := abs(c);

  if a < 1e-4 then
    a := 1.0;
  end if;
  if b < 1e-4 then
    b := 1.0;
  end if;
  if c < 1e-4 then
    c := 1.0;
  end if;
  x := rem((a/30269.0+b/30307.0+c/30323.0),1.0);
  
  so.s1 := a;
  so.s2 := b;
  so.s3 := c;
 
end MathRandom;

//-----------------------------------------------------------
//- For direct use in OMShell:
//- 	- Initialise ALL variables
//- 	- Call repeatedly: (r, s1,s2,s3) := MR(s1,s2,s3,t);
//-   Result: random r.
function MR
  input Real si1,si2,si3;
  input Real t;
  output Real x;
  output Real so1,so2,so3;
  protected
  SEEDINFO s;
algorithm
  s.s1:=si1;
  s.s2:=si2;
  s.s3:=si3;
  (x,s):=MathRandom(s,t);
  so1 := s.s1;
  so2 := s.s2;
  so3 := s.s3;
end MR;

//-----------------------------------------------------------

model test_MathRandom
  output Real y(start=0.0);
  parameter Real dtSample = 0.1;

protected
  SEEDINFO s(s1(start=1.0), s2(start=2.0), s3(start=3.0));
  Real x(start=0.0);
  Real s1, s2, s3;
algorithm
  (x,s):=MathRandom(s,time/10.0);
  s1 := s.s1;
  s2 := s.s2;
  s3 := s.s3;
equation
  when sample(0.0, dtSample) then
    y = x;
end when;
end test_MathRandom;

//-----------------------------------------------------------

model test_MR
  output Real y(start=0.0);
  parameter Real dtSample = 0.1;

protected
  Real s1(start=1.0), s2(start=2.0), s3(start=3.0);
  Real x(start=0.0);
algorithm
  (x,s1,s2,s3) := MR(s1,s2,s3,time/10.0);
equation
  when sample(0.0, dtSample) then
    y = x;
end when;
end test_MR;

However, when I call the function MR directly in the notebook
repeatedly) then I get the expected output (a changing random number).

The same problem occurs when trying to run the Kalman model in DrControl (it is based on the same noise generation function).

Using the MDT debugger it seems to go wrong when calculating the
remainder from c but only when calling MR in test_MR, not when calling it directly because then it gives correct results. The other thing is that in MDT the values for s1, s2, s3 never change.

Attachments (4)

test_MR.onb (12.4 KB) - added by e.ypma@… 11 years ago.
MathRandom.mo (2.0 KB) - added by e.ypma@… 11 years ago.
build_script.mos (172 bytes) - added by e.ypma@… 11 years ago.
MathRandom.onb (46.4 KB) - added by lochel 11 years ago.

Download all attachments as: .zip

Change History (20)

Changed 11 years ago by e.ypma@…

Changed 11 years ago by e.ypma@…

Changed 11 years ago by e.ypma@…

comment:1 Changed 11 years ago by lochel

Your example does not work for me. When I run your OMNotebook, I get the following output:

record SimulationResult
    resultFile = "",
    messages = "Simulation failed for model: test_MR
Warning: The initial conditions are not fully specified. Use +d=initialization for more information.
Error: Error building simulator. Build log: perl D:/workspace/OpenModelica/build/share/omc/scripts/convert_lines.pl test_MR.c test_MR.conv.c.tmp
perl D:/workspace/OpenModelica/build/share/omc/scripts/convert_lines.pl test_MR_functions.c test_MR.conv.c.tmp
gcc  -O0 -g -falign-functions -msse2 -mfpmath=sse   -I\"D:/workspace/OpenModelica/build/include/omc\" -I.   -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o test_MR.conv.o test_MR.conv.c
In file included from test_MR.c:17:
test_MR_functions.c: In function 'omc_MR':
test_MR_functions.c:169: error: 'SEEDINFO' has no member named 's1'
In file included from test_MR.c:17:
test_MR_functions.c:170: error: 'SEEDINFO' has no member named 's2'
In file included from test_MR.c:17:
test_MR_functions.c:171: error: 'SEEDINFO' has no member named 's3'
OMDev\\tools\\MinGW\\bin\\mingw32-make: *** [test_MR.conv.o] Error 1

comment:2 follow-up: Changed 11 years ago by e.ypma@…

That is weird. On my side it runs but not with satisfactory results. I'm using v1.9.0beta4

Sequence:

  • change directory to file 'MathRandom.mo' location
  • load Modelica
  • loadFile("MathRandom.mo")
  • simulate(test_MR)
  • plot({y})

comment:3 in reply to: ↑ 2 Changed 11 years ago by lochel

Replying to e.ypma@…:

Sequence:

  • change directory to file 'MathRandom.mo' location
  • load Modelica
  • loadFile("MathRandom.mo")
  • simulate(test_MR)
  • plot({y})

This is what I did. I am using OpenModelica 1.9.0 beta4+dev (r16563).

BTW: I get the same error message using your build_script.mos

Last edited 11 years ago by lochel (previous) (diff)

comment:4 follow-up: Changed 11 years ago by e.ypma@…

What happens if you:

  • change directory to file 'MathRandom.mo' location
  • load Modelica
  • loadFile("MathRandom.mo")
  • Set:

s1:=1.0;
s2:=2.0;
s3:=3.0;
x:=0.0;

  • (x,s1,s2,s3):=MR(s1,s2,s3,0.0);
  • x

That is, without simulating?

I my case, after executing x it prints a random number. Each time, after executing the function MR a different random number is printed.

comment:5 in reply to: ↑ 4 Changed 11 years ago by lochel

Replying to e.ypma@…:

What happens if you:

  • change directory to file 'MathRandom.mo' location
  • load Modelica
  • loadFile("MathRandom.mo")
  • Set:

s1:=1.0;
s2:=2.0;
s3:=3.0;
x:=0.0;

  • (x,s1,s2,s3):=MR(s1,s2,s3,0.0);
  • x

That works fine.

comment:6 Changed 11 years ago by e.ypma@…

What happens if you replace the record with individual variables s1, s2, s3 (same as in the DrControl Kalman example)?

comment:7 Changed 11 years ago by lochel

I simplified your example a bit. It contains still the same algorithm. I have removed the record and the wrapper function MR (there was no issue with function MR, just needless). Now it works fine.

comment:8 Changed 11 years ago by lochel

It works fine in the sense that I get also ~1e21 ;-)

comment:9 Changed 11 years ago by e.ypma@…

Great that it is reproducible.
In fact you have added another bug: something must have changed between mine- and your version.

comment:10 Changed 11 years ago by lochel

I guess this is a rem issue.

comment:11 Changed 11 years ago by lochel

Using a minor hack, the results are not that bad. (see MathRandom.onb)

comment:12 Changed 11 years ago by e.ypma@…

Now it does workj for me too.
Well, it is caused by

rem

but not when using it directly in a function: in that case there does not seem to be a problem.
So it must be a combination of

rem

and the context within it is used.

comment:13 Changed 11 years ago by lochel

You can replace rem with myRem as a workaround:

function myRem
  input Real x;
  input Real y;
  output Real z;
algorithm
  z := x - (div(x, y) * y);
end myRem;

comment:14 Changed 11 years ago by lochel

  • Keywords Inittilisation removed
  • Owner changed from probably noone to lochel
  • Status changed from new to accepted

I fixed it and will commit it later on today.

comment:15 Changed 11 years ago by lochel

  • Resolution set to fixed
  • Status changed from accepted to closed

fixed in r16571.

Now we get the same weird results as Dymola (see s3).

Feel free to reopen this ticket for further bugs (record/array) that get uncovered by your model. Or just open a new one.

Last edited 11 years ago by lochel (previous) (diff)

Changed 11 years ago by lochel

comment:16 Changed 11 years ago by e.ypma@…

What still puzzles me is the fact that calling the 'standalone' function seems to produce random numbers, whereas calling it inside a 'when' statement drives the whole simulation beserk.

Note: See TracTickets for help on using tickets.