Thanks.

I use the time as an argument and create a seed based on time, i got a
pseudo-random signal (with a 100secs period ), and f(a) = f(b) if a=b
:

normal_ext.c:
#include <math.h>
#include <limits.h>

double normal_ext(double time)
{
    unsigned int seed = 0;
    double v1, v2, r;

    time /= 100;
    seed = (time - floor(time)) * UINT_MAX;
    srandom(seed);

    do
    {
        v1 = 2 * ((double) random()) /((double) RAND_MAX) - 1;
        v2 = 2 * ((double) random()) /((double) RAND_MAX) - 1;
        r = v1 * v1 + v2 * v2;
    } while((r >= 1.0) || (r == 0.0));

    return v1 * sqrt( - 2.0 * log(r) / r );
}

But i think that i will use random_r which let me have several random
generator in a simulation,  and with other parameter like an
identification for each generator and then create a different seed to
differents blocks on the same time.

2008/12/5 Hakan Lundvall <[EMAIL PROTECTED]>:
> Hello Jorge!
>
> Just a little comment on your model. A function in Modelica should always
> return the same value given the same argument, so a function with no
> arguments is a constant. Since i is not involved in any integration
> nothing tells the compiler when to call f(). A tool could chose to
> evaluate it only once during compile time. I you want to implement a
> random function I would suggest keeping the random seed in a Modelica
> variable and passing it to the random function and also let the random
> function return an updated value for the seed variable. Something like
> this:
>
> class E
>    discrete Real y;
>    discrete Real seed;
>
> equation
>    when sample(0,0.1) then
>         (y, seed) = f(pre(seed));
>    end when;
> end E;
>
> which would give y a new value 10 times per sec. I'm not sure though if
> functions with multiple return values are supported yet.
> In any case if you call f() from within a when-clause you have control of
> how often it is called so you can get away with just passing time as an
> argument since we know it will only be called once per time instant.
>
> class E
>    discrete Real y;
>    discrete Real seed;
>
> equation
>    when sample(0,0.1) then
>         y = f(time);
>    end when;
> end E;
>
> Functions in the continues part might be called both forward and backward
> in time depending on the solver so it is important that they give the same
> result every time.
>
> Best regards,
> HÃ¥kan
> -------------------
>> Greeting foot people.
>>
>> I was creating a external function to create a normal distributed
>> source signal, but i found myself in a little problem, look at this:
>>
>> f.c:
>> #include <stdlib.h>
>>
>> double f(void)
>> {
>>     return random();
>> }
>>
>> f.h:
>> double f(void);
>>
>> f.mo:
>> function f
>>     output Real y;
>>     external
>>         annotation(Library="/tmp/f.o", Include="#include \"/tmp/f.h\"");
>> end f;
>>
>>
>> class A
>>     Real x;
>> initial equation
>>     x = 20;
>> equation
>>     der(x) = -x;
>> end A;
>>
>>
>> class B
>>     Real x;
>>     Real y;
>> initial equation
>>     x = 20;
>> equation
>>     der(x) = -x;
>>     y = 0;
>> end B;
>>
>> class C
>>     Real x(fixed=false);
>>     Real y;
>> initial equation
>>     x = 20;
>> equation
>>     der(x) = -x;
>>     y = 0;
>> end C;
>>
>> class D
>>     Real x(fixed=false);
>>     Real y;
>> initial equation
>>     x = 20;
>> equation
>>     der(x) = -x;
>>     y = f();
>> end D;
>>
>> class E
>>     Real y;
>> equation
>>     y = f();
>> end E;
>>
>> class F
>>     Real x;
>>     Real y;
>> initial equation
>>     x = 20;
>> equation
>>     der(x) = -x;
>>     y = f();
>> end F;
>>
>> well, copy this to /tmp and in a OMShell-terminal:
>> [EMAIL PROTECTED]:/tmp$ OMShell-terminal
>> Open Source Modelica Terminal Shell
>> Copyright 1997-2007, PELAB, Linkoping University
>>
>> To get help on using Mosh and OpenModelica, type "help()" and press
> enter
>> Started server using:/usr/share/omc-omshell/bin/omc +d=interactive >
>> /tmp/error.log 2>&1 &
>>  res = 0
>> >>> loadModel(Modelica)
>> true
>> >>> loadFile("/tmp/f.mo")
>> true
>> >>> system("gcc -c -o /tmp/f.o /tmp/f.c")
>> 0
>> >>> simulate(A,timeStop=10)
>> record
>>     resultFile = "A_res.plt"
>> end record
>> >>> plot(x)
>> true
>> >>> simulate(B,timeStop=10)
>> record
>>     resultFile = "B_res.plt"
>> end record
>> >>> plot({x,y})
>> true
>> >>> simulate(C,timeStop=10)
>> record
>>     resultFile = "C_res.plt"
>> end record
>> >>> plot({x,y})
>> true
>> >>> simulate(D,timeStop=10)
>> record
>>     resultFile = "D_res.plt"
>> end record
>> >>> plot({x,y})
>> true
>> >>> simulate(E,timeStop=10)
>> record
>>     resultFile = "E_res.plt"
>> end record
>> >>> plot(y)
>> true
>> >>> simulate(F,timeStop=10)
>> record
>>     resultFile = "F_res.plt"
>> end record
>> >>> plot({x,y})
>> true
>>
>>
>> Well, the class A only has a variable and decrease from 20 to 0,
>> that's ok. The class B has two variables, one should decrease from 20
>> to 0 and the other one should be constant on 0, but both of them are
>> 0s. At the class C, i declare the x variable with fixed=false, and
>> that work great. At class D, i declard that y is the returned value
>> form f, which return a random number, but the simulation stop silently
>> without error, but without data. At class E, i had deleted the x
>> variable, and then the simulation work great and y has random values,
>> and in class F  i declared again the variable, but without fixed and
>> the simulation works but x is not initialized at 20, so, it is a
>> constant 0.
>>
>> I don't know if i just missing something here, what i need is work
>> with the random generator, and initializa other variables with
>> "initial equation".
>>
>> What can i do to deal whit this?
>>
>> PD: If you need some hands to type some lines of code of OpenModelica,
>> just say, i will be happy to help with it.
>>
>> --
>> Jorge Eduardo Cardona
>> [EMAIL PROTECTED]
>> jorgeecardona.blogspot.com
>> ------------------------------------------------
>> Linux registered user  #391186
>> Registered machine    #291871
>> ------------------------------------------------
>>
>



-- 
Jorge Eduardo Cardona
[EMAIL PROTECTED]
jorgeecardona.blogspot.com
------------------------------------------------
Linux registered user  #391186
Registered machine    #291871
------------------------------------------------

Reply via email to