CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

Loop over cells slowing down the code

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 2 Post By ngj

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 6, 2013, 03:57
Default Loop over cells slowing down the code
  #1
New Member
 
Guillaume
Join Date: Nov 2012
Posts: 10
Rep Power: 13
Gloq is on a distinguished road
Hi everybody,

I try to create a field for my variable alphath. The value I want to set for this variable depends on space.
Finally, the algorithm should look like :

For all cells
If ( x < xmax and x > xmin) alphath = f(x)

In order to go step by step in the coding process (I'm not really a senior coder in openFoam ...), for the moment, I just try to impose, for all cells,
alphath(x,y,z) = x

I coded this stuff like that :

forAll (mesh.cells(),celli)
{
alphath[celli] = mesh.C().component(vector::X)()[celli];
}

Adding these lines in the code REALLY slows it down ...
As a simple test, I replaced "mesh.C().component(vector::X)()[celli]" by "1.0" and the code is quick again.

Do you know why the added lines slow the code down ?
Would you have an alternative coding ?

Thanks in advance for your help.

Guillaume
Gloq is offline   Reply With Quote

Old   February 6, 2013, 04:13
Default
  #2
ngj
Senior Member
 
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,903
Rep Power: 37
ngj will become famous soon enoughngj will become famous soon enough
Hi Guillaume,

I have a small guess, since you are first extracting the x-coordinate from the cell centres and then you get the i'th index (checked the code and a new volScalarField is constructed). This actually means that you make a copy of all of the x-values every time you only need one; so in the end of the day you have a process, which scales with N^2 rather than N.

This should be faster:

Code:
forAll( alphath, celli )
     alphath[celli] = mesh.C()[celli].x();
or in this particular case, you could merely have written:

Code:
alphath.internalField() = mesh.C().component(vector::X);
Kind regards,

Niels
mm.abdollahzadeh and mrishi like this.
ngj is offline   Reply With Quote

Old   February 6, 2013, 05:15
Default
  #3
New Member
 
Guillaume
Join Date: Nov 2012
Posts: 10
Rep Power: 13
Gloq is on a distinguished road
Hey Niels,

thanks a lot for having taken some of your time for my problem !
Your answer was quick, detailed and (after a test just performed) very efficient !

Thanks again.

Kind regards,
Guillaume

Last edited by Gloq; February 6, 2013 at 05:42.
Gloq is offline   Reply With Quote

Old   February 6, 2013, 05:25
Default
  #4
ngj
Senior Member
 
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,903
Rep Power: 37
ngj will become famous soon enoughngj will become famous soon enough
Good!

/ Niels
ngj 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
[Gmsh] Problem with Gmsh nishant_hull OpenFOAM Meshing & Mesh Conversion 23 August 5, 2015 03:09
snappyHexMesh in parallel - FOAM Fatal IO Error mturcios777 OpenFOAM Running, Solving & CFD 4 August 10, 2012 20:18
[CAD formats] my stl surface is seen as just a line rcastilla OpenFOAM Meshing & Mesh Conversion 2 January 6, 2010 02:30
for loop inside a cell_loop? MHDWill FLUENT 0 September 26, 2007 22:24
NACA0012 geometry/design software needed Franny Main CFD Forum 13 July 7, 2007 16:57


All times are GMT -4. The time now is 06:44.