#2279 closed defect (fixed)
Initialisation problem in equation section when using random generation function
Reported by: | Owned by: | Lennart Ochel | |
---|---|---|---|
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)
Change History (20)
by , 11 years ago
Attachment: | test_MR.onb added |
---|
by , 11 years ago
Attachment: | MathRandom.mo added |
---|
by , 11 years ago
Attachment: | build_script.mos added |
---|
comment:1 by , 11 years ago
follow-up: 3 comment:2 by , 11 years ago
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 by , 11 years ago
follow-up: 5 comment:4 by , 11 years ago
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 by , 11 years ago
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 by , 11 years ago
What happens if you replace the record with individual variables s1, s2, s3 (same as in the DrControl Kalman example)?
comment:7 by , 11 years ago
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:9 by , 11 years ago
Great that it is reproducible.
In fact you have added another bug: something must have changed between mine- and your version.
comment:12 by , 11 years ago
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 by , 11 years ago
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 by , 11 years ago
Keywords: | Inittilisation removed |
---|---|
Owner: | changed from | to
Status: | new → accepted |
I fixed it and will commit it later on today.
comment:15 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | accepted → 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.
by , 11 years ago
Attachment: | MathRandom.onb added |
---|
comment:16 by , 11 years ago
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.
Your example does not work for me. When I run your OMNotebook, I get the following output: