CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (http://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Loop over cells slowing down the code (http://www.cfd-online.com/Forums/openfoam-programming-development/112822-loop-over-cells-slowing-down-code.html)

Gloq February 6, 2013 03:57

Loop over cells slowing down the code
 
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

ngj February 6, 2013 04:13

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

Gloq February 6, 2013 05:15

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

ngj February 6, 2013 05:25

Good!

/ Niels


All times are GMT -4. The time now is 10:41.