# [swak4Foam] how to initialize a random velocity field with funkySetFields

 Register Blogs Members List Search Today's Posts Mark Forums Read

 December 30, 2012, 07:57 how to initialize a random velocity field with funkySetFields #1 Member   Andrea Join Date: Feb 2012 Location: Leeds, UK Posts: 98 Rep Power: 8 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

 January 1, 2013, 06:37 #2 Senior Member   Bernhard Join Date: Sep 2009 Location: Delft Posts: 790 Rep Power: 15 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.

 January 1, 2013, 09:51 #3 Member   Andrea Join Date: Feb 2012 Location: Leeds, UK Posts: 98 Rep Power: 8 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

January 5, 2013, 19:01
#4
Assistant Moderator

Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,034
Rep Power: 43
Quote:
 Originally Posted by Andrea1984 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)
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request

 January 9, 2013, 12:10 #5 Member   Andrea Join Date: Feb 2012 Location: Leeds, UK Posts: 98 Rep Power: 8 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.

January 9, 2013, 17:49
#6
Assistant Moderator

Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,034
Rep Power: 43
Quote:
 Originally Posted by Andrea1984 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
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request

 January 10, 2013, 06:10 #7 Member   Andrea Join Date: Feb 2012 Location: Leeds, UK Posts: 98 Rep Power: 8 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

January 10, 2013, 07:23
#8
Assistant Moderator

Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,034
Rep Power: 43
Quote:
 Originally Posted by Andrea1984 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)
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request

January 10, 2013, 10:10
#9
Assistant Moderator

Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,034
Rep Power: 43
Quote:
 Originally Posted by gschaider 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
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request

February 19, 2013, 21:25
#10
Member

Albert Tong
Join Date: Dec 2010
Location: Perth, WA, Australia
Posts: 76
Blog Entries: 1
Rep Power: 9
Quote:
 Originally Posted by Andrea1984 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.
__________________
Kind regards,

Albert

Last edited by tfuwa; February 19, 2013 at 23:33. Reason: "If seed value is smaller than 0 it will change at every time"

February 20, 2013, 00:15
#11
Member

Albert Tong
Join Date: Dec 2010
Location: Perth, WA, Australia
Posts: 76
Blog Entries: 1
Rep Power: 9
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.
__________________
Kind regards,

Albert

February 20, 2013, 12:46
#12
Assistant Moderator

Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,034
Rep Power: 43
Quote:
 Originally Posted by tfuwa 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?
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request

February 21, 2013, 04:41
#13
Member

Albert Tong
Join Date: Dec 2010
Location: Perth, WA, Australia
Posts: 76
Blog Entries: 1
Rep Power: 9
Quote:
 Originally Posted by gschaider 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,

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
)```
__________________
Kind regards,

Albert

February 21, 2013, 12:23
#14
Assistant Moderator

Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,034
Rep Power: 43
Quote:
 Originally Posted by tfuwa 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 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 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)
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request

 March 6, 2013, 04:42 Bernhard #15 Member   Albert Tong Join Date: Dec 2010 Location: Perth, WA, Australia Posts: 76 Blog Entries: 1 Rep Power: 9 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. __________________ Kind regards, Albert

March 6, 2013, 06:41
#16
Assistant Moderator

Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,034
Rep Power: 43
Quote:
 Originally Posted by tfuwa 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.
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
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request

March 6, 2013, 09:58
#17
Member

Albert Tong
Join Date: Dec 2010
Location: Perth, WA, Australia
Posts: 76
Blog Entries: 1
Rep Power: 9
Hi Bernhard,

Quote:
 Originally Posted by gschaider 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 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 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.

Kind regards,
Albert

March 7, 2013, 15:09
#18
Assistant Moderator

Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,034
Rep Power: 43
Quote:
 Originally Posted by tfuwa 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 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
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request

 August 2, 2016, 06:02 #19 New Member   fluidflow Join Date: Dec 2015 Posts: 5 Rep Power: 4 Hi Andrea1984, I am using openfoam 2.2.0 to generate random number. in the scalar transport foam, i just added rand() as an extra term to the right hand side of the equation to generate random number between 0 and 1 as you said. while compiling i am getting error. can u help about how can i generate random number between -1 and +1 and put it as a source term with regards

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are On Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Martin1 Fluent UDF and Scheme Programming 1 June 18, 2015 08:20 AndoniBM OpenFOAM Running, Solving & CFD 2 March 25, 2015 19:44 LM4112 OpenFOAM Running, Solving & CFD 12 February 5, 2014 12:56 Subhadeep Siemens 3 December 21, 2008 04:40 matteo_gautero OpenFOAM Running, Solving & CFD 0 February 28, 2008 07:51

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