openmodelicainterest  

Re: Random generator in Modelica

Mikael Hagernäs
Mon, 16 Mar 2009 07:37:11 -0700

why 10 equations?

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];

 //Modelica.Math.Random.random;

//Uniform distribution random function

//Distribution uniform between 0 and 1.

function random

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;

end Random;



model brus

//import Modelica.Math.Random;

import Random;

type Seed = Real[3];

Real count;

Seed count2;

//Real data(start=0);

 equation

 (count,count2)=Random.random();

 end brus;


simulate(brus,stopTime=5)

record SimulationResult

resultFile = "Simulation failed.

Too many equations, overdetermined system. The model has 4 variables and 10
equations

"

end SimulationResult;



2009/3/16 Peter Fritzson <pe...@ida.liu.se>

>  This is not part of the Modelica standard library.
> You must explicitly supply this function
> (I sent you the source code)
>
> /PF
>
>  ------------------------------
> *From:* owner-openmodelicainter...@ida.liu.se [mailto:
> owner-openmodelicainter...@ida.liu.se] *On Behalf Of *Mikael Hagernäs
> *Sent:* den 16 mars 2009 12:27
> *To:* openmodelicainterest@ida.liu.se
> *Subject:* Re: Random generator in Modelica
>
> Ok. Can you please tell me why this doesnt work?
>
> loadModel (Modelica.Math.Random);
>
> model brus
>
> import Modelica.Math.Random;
>
> Real count;
>
> equation
>
> count=random();
>
> end brus;
>
>
> simulate(brus,stopTime=5)
>
> record SimulationResult
>
> resultFile = "Simulation failed.
>
> Class Modelica.Math.Random (its type) not found in scope brus.
>
> No matching function found for Modelica.Math.Random
>
> Class Modelica.Math.Random not found in scope brus.
>
> Error occured while flattening model brus
>
> "
>
> end SimulationResult;
>
>
>
> Thanks
>
>
>
>
>
>
> 2009/3/16 Mikael Hagernäs <mikha...@student.liu.se>
>
>> Ok. Can you please explain to me how to use this random function. How do i
>> use this package
>>
>> 2009/3/15 Peter Fritzson <pe...@ida.liu.se>
>>
>>
>>> You can also write a random function
>>> in Modelica. See page 761 in my book.
>>>
>>> /Peter F
>>>
>>> Modelica.Math.Random
>>> This Random package placed as a preliminary subpackage of
>>> Math contains the following two random distribution functions
>>> random() for uniform distributions and normalvariate() for
>>> Normal distribution.
>>> Package header:
>>>
>>> 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];
>>> Modelica.Math.Random.random
>>> Uniform distribution random function
>>> Distribution uniform between 0 and 1.
>>> 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;
>>>
>>> Modelica.Math.Random.normalvariate
>>> Normal distribution random function
>>> 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 break=false;
>>> algorithm
>>> s1 := si;
>>> u2 := 1;
>>> while not break loop
>>> (u1,s2) := random(s1);
>>> (u2,s1) := random(s2);
>>> z := NV_MAGICCONST*(u1-0.5)/u2;
>>> zz := z*z/4.0;
>>> break := zz <= (- Math.log(u2));
>>> end while;
>>> x := mu + z*sigma;
>>> so := s1;
>>> end normalvariate;
>>>
>>>
>>>
>>> -----Original Message-----
>>> From: owner-openmodelicainter...@ida.liu.se
>>> [mailto:owner-openmodelicainter...@ida.liu.se] On Behalf Of Adrian Pop
>>> Sent: den 15 mars 2009 18:10
>>> To: openmodelicainterest@ida.liu.se
>>> Subject: Re: Random generator
>>>
>>>
>>> Hi Mikael,
>>>
>>> Basically you make an external
>>> function to return the random value.
>>> See an example how in OpenModelica1.4.5/testmodels/External*
>>>
>>> Also have a look at this thread (previously on OpenModelicaInterest):
>>>  http://thread.gmane.org/gmane.comp.misc.openmodelica/501/focus=507
>>>
>>> Cheers,
>>> Adrian Pop/
>>>
>>> Mikael Hagernäs wrote:
>>> > Hi!
>>> >
>>> > I need to create a white noise signal. Is there any function or
>>> > algorithm i can use to create random numbers?
>>>
>>>
>>>
>>>
>>>
>>
>