CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM (http://www.cfd-online.com/Forums/openfoam/)
-   -   how to initialize a random velocity field with funkySetFields (http://www.cfd-online.com/Forums/openfoam/111172-how-initialize-random-velocity-field-funkysetfields.html)

Andrea1984 December 30, 2012 07:57

how to initialize a random velocity field with funkySetFields
 
Hi,

I would like to inizialize a random velocity field with funkySetFields. Is it possible?

I've tried something like

{
field U;
expression "vector (0.005*rand(), 0.005*rand(), 0.005*rand())";
}

Do you think this should work?

Thanks in advance


Andrea

Bernhard January 1, 2013 06:37

Well, you can just test whether it will work or not, right? Try to search the forum for "perturbu", this make give you some lead.

Andrea1984 January 1, 2013 09:51

Hi Bernard,

I was pretty sure that the formulation in my previous post is wrong because rand() generates a scalar random field, so it has a defined and unique value in each cell of the domain. In fact I tried to test it, obtaining a velocity field which looks like

(0.000208152 0.000208152 0.000208152)
(0.00227246 0.00227246 0.00227246)
(0.00417409 0.00417409 0.00417409)
...

Furthermore rand() generates random numbers between 0 and 1, while I would like to have velocity components randomly distributed between -0.005 and 0.005.

I will give a look to perturbU, even if I would like to know if it's possible to obtain the goal using funkySetFields.

Thank you for your reply (and, of course, happy 2013),

Andrea

gschaider January 5, 2013 19:01

Quote:

Originally Posted by Andrea1984 (Post 399628)
Furthermore rand() generates random numbers between 0 and 1, while I would like to have velocity components randomly distributed between -0.005 and 0.005.

I will give a look to perturbU, even if I would like to know if it's possible to obtain the goal using funkySetFields.

Of course it is. All you've go to do is do a simple transformation of the range [0,1) to the range [-0.005,0.005) which is done with <drumroll> "0.01*rand()-0.005" (I think it shouldn't be too hard for you to generalize the concept to different ranges)

Andrea1984 January 9, 2013 12:10

Obtaining the desired range starting from [0,1) it's not a problem; my problem is how to have three different random scalar fields (one for each velocity component).

I've also tried to use Random::vector01() but it's not recognized by swak4foam.

gschaider January 9, 2013 17:49

Quote:

Originally Posted by Andrea1984 (Post 400986)
Obtaining the desired range starting from [0,1) it's not a problem; my problem is how to have three different random scalar fields (one for each velocity component).

I've also tried to use Random::vector01() but it's not recognized by swak4foam.

But it is used by it ;)

To create reproducible results random always uses the same seed. But you can change that by simply supplying an integer "random(42)" for instance. If you do that for every component they are "independent"

BTW: if the seed value is bigger than 0 then it will stay the same with every timestep. If it is smaller than 0 it will change at every time. But I think that is mentioned in the README

The same is true for randomNormal

Andrea1984 January 10, 2013 06:10

Bernhard,

Thank you very much for your tips, now I have a wonderful (pseudo-)random velocity field!

If anyone else is interested, the expression i used is:

"vector (0.01*rand(12)-0.005, 0.01*rand(4)-0.005, 0.01*rand(47)-0.005)"

Regards,

Andrea

gschaider January 10, 2013 07:23

Quote:

Originally Posted by Andrea1984 (Post 401152)
Bernhard,

Thank you very much for your tips, now I have a wonderful (pseudo-)random velocity field!

If anyone else is interested, the expression i used is:

"vector (0.01*rand(12)-0.005, 0.01*rand(4)-0.005, 0.01*rand(47)-0.005)"

Regards,

Andrea

Would be nice if you could add an example to http://openfoamwiki.net/index.php/Co...Usage_examples (I'm trying to get people to contribute such things that are commonly used in a central location)

gschaider January 10, 2013 10:10

Quote:

Originally Posted by gschaider (Post 401162)
Would be nice if you could add an example to http://openfoamwiki.net/index.php/Co...Usage_examples (I'm trying to get people to contribute such things that are commonly used in a central location)

That was fast: http://openfoamwiki.net/index.php/Co...RandomVelocity

Great. If everyone who got his question answered did this then there wouldn't be a documentation problem

tfuwa February 19, 2013 21:25

Quote:

Originally Posted by Andrea1984 (Post 401152)
Bernhard,

Thank you very much for your tips, now I have a wonderful (pseudo-)random velocity field!

If anyone else is interested, the expression i used is:

"vector (0.01*rand(12)-0.005, 0.01*rand(4)-0.005, 0.01*rand(47)-0.005)"

Regards,

Andrea

Hi Bernhard and Andrea,

An interesting thread. Just wonder if I would like to see a random velocity distribution different(to the most) from each other at every cell in a zone and changes with time, then how to realize?

It seems, as Bernhard pointed out, just change the no. in rand() to a value smaller than 0 to make it changeable with time. But how to make the random velocity also change with space? Cheers.

tfuwa February 20, 2013 00:15

Hi All,

To answer my own question. If the position is given, then velocity will change with space. For example,

Quote:

"position=mag(pos().x/pos().x);"
"vector (0.01*rand()*position-0.005, 0.01*rand()*position-0.005, 0.01*rand()*position-0.005)"
Then the velocity will change along x-axis (please point out if there are other smart ways).

However, why the swak4Foam on my computer does not recognize rand(Nn) and gives the following error message? (which disappears after deleting the number in rand() )

Quote:

--> FOAM FATAL ERROR:
Parser Error at "1.23-24" :"syntax error, unexpected number, expecting ')'"
"2*pi*(position*2*rand(21)+18)"
" ^^ "

From function parsingValue
in file lnInclude/CommonValueExpressionDriverI.H at line 493.

FOAM exiting
Many thanks.

gschaider February 20, 2013 12:46

Quote:

Originally Posted by tfuwa (Post 408885)
Hi All,

To answer my own question. If the position is given, then velocity will change with space. For example,

Then the velocity will change along x-axis (please point out if there are other smart ways).

However, why the swak4Foam on my computer does not recognize rand(Nn) and gives the following error message? (which disappears after deleting the number in rand() )

Many thanks.

Pleas put output in CODE not QUOTE. That way the spaces are preserved and I don't have to guess where the ^^ are pointing at

Which version of swak do you use?

tfuwa February 21, 2013 04:41

Quote:

Originally Posted by gschaider (Post 409098)
Pleas put output in CODE not QUOTE. That way the spaces are preserved and I don't have to guess where the ^^ are pointing at

Which version of swak do you use?


Hi Bernhard,

1, Sorry for the ambiguity. Your anticipation is right. I downloaded the latest version of swak and recompiled, then the error was gone. Thanks for your time. Great help.

2, But it seems the seed value cannot be smaller than 0 in rand(),

Code:

--> FOAM FATAL ERROR:
 Parser Error for driver FieldValueExpressionDriver at "1.6" :"invalid character when only an integer parameter is expected"
"rand(-21)"
      ^
-------|

Context of the error:


- From dictionary: /home/tfuwa/OpenFOAM/tfuwa-1.7.x/run/applications/swak4Foam_1.x_release_0.2.1/Examples/IcoFoamWithSources/cavityWithSource-2/constant/momentumSourceDict
  Evaluating expression "rand(-21)"


    From function parsingValue
    in file lnInclude/CommonValueExpressionDriverI.H at line 876.

FOAM exiting

3, how to run randomCavity? (at: swak4Foam_1.x_release_0.2.1/Examples/tests/randomCavity) I would like to output rand() values and it seems there is an example. After setting the b.c. I got the error message

Code:

--> FOAM FATAL ERROR:

    request for volScalarField randField from objectRegistry region0 failed
    available objects of type volScalarField are

1
(
p
)

Many thanks for your help.

gschaider February 21, 2013 12:23

Quote:

Originally Posted by tfuwa (Post 409239)
Hi Bernhard,

1, Sorry for the ambiguity. Your anticipation is right. I downloaded the latest version of swak and recompiled, then the error was gone. Thanks for your time. Great help.

Fix by update. Great

Quote:

Originally Posted by tfuwa (Post 409239)
2, But it seems the seed value cannot be smaller than 0 in rand(),

Code:

--> FOAM FATAL ERROR:
 Parser Error for driver FieldValueExpressionDriver at "1.6" :"invalid character when only an integer parameter is expected"
"rand(-21)"
      ^
-------|

Context of the error:


- From dictionary: /home/tfuwa/OpenFOAM/tfuwa-1.7.x/run/applications/swak4Foam_1.x_release_0.2.1/Examples/IcoFoamWithSources/cavityWithSource-2/constant/momentumSourceDict
  Evaluating expression "rand(-21)"


    From function parsingValue
    in file lnInclude/CommonValueExpressionDriverI.H at line 876.

FOAM exiting


My bad. That has already changed for some time. Now there are two functions: rand(42) which changes during time and randFixed(42) which doesn't

Quote:

Originally Posted by tfuwa (Post 409239)
3, how to run randomCavity? (at: swak4Foam_1.x_release_0.2.1/Examples/tests/randomCavity) I would like to output rand() values and it seems there is an example. After setting the b.c. I got the error message

Code:

--> FOAM FATAL ERROR:

    request for volScalarField randField from objectRegistry region0 failed
    available objects of type volScalarField are

1
(
p
)

Many thanks for your help.

That case can be tested with the replayTransientBC-utilitiy (which basically is a solver without solving anything)

tfuwa March 6, 2013 04:42

Bernhard
 
Hi Bernhard,

Come to bother you again :)

I managed to add a random source term to icoFoam through swak4Foam, and generate the source term in a specific area. The source term is defined as S = [0, 0, 2*A*(rand()-0.5) ], where A is a fixed amplitude of the source term.Basically, S only depends on rand() function and only has values in z-direction.
Several lines in the source term area along z-direction are investigated to see whether S is random. I plot the changes of S with time for those lines, and disappointedly find some of them seemingly not random at all as shown in the attached figure. For the probe lines 5 and 7, S is random, but for lines 6 and 8, S is not.

Those lines are extracted with sample utility of OF. I have tried to change the value in rand(n) or with no seed, but got similar results.

What do you think the reason for the problem?

Is there a way to output the rand() values at each time-step for every cell in the source generated area? Thanks.



http://dl.dropbox.com/u/62722643/line1-4Mz.jpg

gschaider March 6, 2013 06:41

Quote:

Originally Posted by tfuwa (Post 411865)
Hi Bernhard,

Come to bother you again :)

I managed to add a random source term to icoFoam through swak4Foam, and generate the source term in a specific area. The source term is defined as S = [0, 0, 2*A*(rand()-0.5) ], where A is a fixed amplitude of the source term.Basically, S only depends on rand() function and only has values in z-direction.
Several lines in the source term area along z-direction are investigated to see whether S is random. I plot the changes of S with time for those lines, and disappointedly find some of them seemingly not random at all as shown in the attached figure. For the probe lines 5 and 7, S is random, but for lines 6 and 8, S is not.

Those lines are extracted with sample utility of OF. I have tried to change the value in rand(n) or with no seed, but got similar results.

What do you think the reason for the problem?

Is there a way to output the rand() values at each time-step for every cell in the source generated area? Thanks.



http://dl.dropbox.com/u/62722643/line1-4Mz.jpg

Well. rand() uses the Random-class of OpenFOAM which in turn uses the random-number generator of the operating system. These generators used to have a bad reputation in terms of the periodicity.

Just to clarify: this is from the same calculation from the same time only the plane where you sample is different?

To get a "full" field for postprocessing (I guess that is what you want in the last paragraph) you could add a functionObject (typos and missing parameters may be there. I only wrote that stuff. This doesn't mean that I know how to use it properly):
Code:

myRandomField {
  type expressionField;
  autoWrite true;
  fieldName myRandomField;
  expression "rand()";
}

The order in which the numbers are in the internalField is the order in which they are generated. So if you find an algorithm to test the periodicity/internal correlation of the "random" numbers you can apply that to them. Alternative (but this is not for the faint of heart) is to get the field via a variable into a pythonIntegration-function object and analyze it via numpy

tfuwa March 6, 2013 09:58

Hi Bernhard,

Thanks for your quick reply.

Quote:

Originally Posted by gschaider (Post 411918)
Well. rand() uses the Random-class of OpenFOAM which in turn uses the random-number generator of the operating system. These generators used to have a bad reputation in terms of the periodicity.

Ahh, guess I cannot do anything to change this.

Quote:

Originally Posted by gschaider (Post 411918)
Just to clarify: this is from the same calculation from the same time only the plane where you sample is different?

The pictures are from the same calculation, but show the random() along several lines changing with time.

Quote:

Originally Posted by gschaider (Post 411918)
To get a "full" field for postprocessing (I guess that is what you want in the last paragraph) you could add a functionObject (typos and missing parameters may be there. I only wrote that stuff. This doesn't mean that I know how to use it properly):
Code:

myRandomField {
  type expressionField;
  autoWrite true;
  fieldName myRandomField;
  expression "rand()";
}

The order in which the numbers are in the internalField is the order in which they are generated. So if you find an algorithm to test the periodicity/internal correlation of the "random" numbers you can apply that to them. Alternative (but this is not for the faint of heart) is to get the field via a variable into a pythonIntegration-function object and analyze it via numpy

Yes. I would like to see full random fields and am going to try your first approch.

Thanks agian. Really helpful.

Kind regards,
Albert

gschaider March 7, 2013 15:09

Quote:

Originally Posted by tfuwa (Post 411978)
Hi Bernhard,

Thanks for your quick reply.



Ahh, guess I cannot do anything to change this.

Write your own random-number-generator (using some text-book implementation). Using the function-plugin-facility you can also use it as "randomAlbert(2)" (or "albertRandom(22)" if you prefer) in your expressions.

That is always assuming that the system random is to blame

Quote:

Originally Posted by tfuwa (Post 411978)
The pictures are from the same calculation, but show the random() along several lines changing with time.



Yes. I would like to see full random fields and am going to try your first approch.

Thanks agian. Really helpful.

Kind regards,
Albert



All times are GMT -4. The time now is 23:02.