CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Community Contributions

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

Register Blogs Community New Posts Updated Threads Search

Like Tree6Likes
  • 1 Post By gschaider
  • 1 Post By gschaider
  • 1 Post By Andrea1984
  • 2 Post By gschaider
  • 1 Post By tfuwa

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 30, 2012, 07:57
Default how to initialize a random velocity field with funkySetFields
  #1
Senior Member
 
Andrea
Join Date: Feb 2012
Location: Leeds, UK
Posts: 179
Rep Power: 16
Andrea1984 is on a distinguished road
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
Andrea1984 is offline   Reply With Quote

Old   January 1, 2013, 06:37
Default
  #2
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 22
Bernhard is on a distinguished road
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.
Bernhard is offline   Reply With Quote

Old   January 1, 2013, 09:51
Default
  #3
Senior Member
 
Andrea
Join Date: Feb 2012
Location: Leeds, UK
Posts: 179
Rep Power: 16
Andrea1984 is on a distinguished road
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
Andrea1984 is offline   Reply With Quote

Old   January 5, 2013, 19:01
Default
  #4
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by Andrea1984 View Post
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)
anishtain4 likes this.
__________________
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
gschaider is offline   Reply With Quote

Old   January 9, 2013, 12:10
Default
  #5
Senior Member
 
Andrea
Join Date: Feb 2012
Location: Leeds, UK
Posts: 179
Rep Power: 16
Andrea1984 is on a distinguished road
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.
Andrea1984 is offline   Reply With Quote

Old   January 9, 2013, 17:49
Default
  #6
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by Andrea1984 View Post
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
Ali Srvr likes this.
__________________
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
gschaider is offline   Reply With Quote

Old   January 10, 2013, 06:10
Default
  #7
Senior Member
 
Andrea
Join Date: Feb 2012
Location: Leeds, UK
Posts: 179
Rep Power: 16
Andrea1984 is on a distinguished road
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
Danillo likes this.
Andrea1984 is offline   Reply With Quote

Old   January 10, 2013, 07:23
Default
  #8
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by Andrea1984 View Post
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
gschaider is offline   Reply With Quote

Old   January 10, 2013, 10:10
Default
  #9
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by gschaider View Post
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
sunliming and kingfaycel5 like this.
__________________
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
gschaider is offline   Reply With Quote

Old   February 19, 2013, 21:25
Default
  #10
Member
 
Albert Tong
Join Date: Dec 2010
Location: Perth, WA, Australia
Posts: 76
Blog Entries: 1
Rep Power: 16
tfuwa is on a distinguished road
Quote:
Originally Posted by Andrea1984 View Post
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"
tfuwa is offline   Reply With Quote

Old   February 20, 2013, 00:15
Default
  #11
Member
 
Albert Tong
Join Date: Dec 2010
Location: Perth, WA, Australia
Posts: 76
Blog Entries: 1
Rep Power: 16
tfuwa is on a distinguished road
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
tfuwa is offline   Reply With Quote

Old   February 20, 2013, 12:46
Default
  #12
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by tfuwa View Post
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
gschaider is offline   Reply With Quote

Old   February 21, 2013, 04:41
Default
  #13
Member
 
Albert Tong
Join Date: Dec 2010
Location: Perth, WA, Australia
Posts: 76
Blog Entries: 1
Rep Power: 16
tfuwa is on a distinguished road
Quote:
Originally Posted by gschaider View Post
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.
__________________
Kind regards,

Albert
tfuwa is offline   Reply With Quote

Old   February 21, 2013, 12:23
Default
  #14
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by tfuwa View Post
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 View Post
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 View Post
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
gschaider is offline   Reply With Quote

Old   March 6, 2013, 04:42
Default Bernhard
  #15
Member
 
Albert Tong
Join Date: Dec 2010
Location: Perth, WA, Australia
Posts: 76
Blog Entries: 1
Rep Power: 16
tfuwa is on a distinguished road
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
tfuwa is offline   Reply With Quote

Old   March 6, 2013, 06:41
Default
  #16
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by tfuwa View Post
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
gschaider is offline   Reply With Quote

Old   March 6, 2013, 09:58
Default
  #17
Member
 
Albert Tong
Join Date: Dec 2010
Location: Perth, WA, Australia
Posts: 76
Blog Entries: 1
Rep Power: 16
tfuwa is on a distinguished road
Hi Bernhard,

Thanks for your quick reply.

Quote:
Originally Posted by gschaider View Post
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 View Post
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 View Post
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
mm.abdollahzadeh likes this.
tfuwa is offline   Reply With Quote

Old   March 7, 2013, 15:09
Default
  #18
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by tfuwa View Post
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 View Post
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
gschaider is offline   Reply With Quote

Old   August 2, 2016, 07:02
Default
  #19
New Member
 
fluidflow
Join Date: Dec 2015
Posts: 5
Rep Power: 11
ARUNAVA SENGUPTA is on a distinguished road
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
ARUNAVA SENGUPTA is offline   Reply With Quote

Reply


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 Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having problems getting the mean vorticity field from mean velocity field Martin1 Fluent UDF and Scheme Programming 1 June 18, 2015 09:20
''unknown radialModelType type Gidaspow'' PROBLEM WITH THE BED TUTORIAL AndoniBM OpenFOAM Running, Solving & CFD 2 March 25, 2015 19:44
change velocity field LM4112 OpenFOAM Running, Solving & CFD 12 February 5, 2014 12:56
Initial velocity field in StarCCM+ Subhadeep Siemens 3 December 21, 2008 04:40
Problem with rhoSimpleFoam matteo_gautero OpenFOAM Running, Solving & CFD 0 February 28, 2008 07:51


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