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/)
-   -   Fill a volScalarField (http://www.cfd-online.com/Forums/openfoam-programming-development/82125-fill-volscalarfield.html)

DiegoNaval November 17, 2010 14:44

Fill a volScalarField
 
Hi All,
I start to try to programming in OF only in the last month, so sorry if I ask some stupid things.
I have created a volScalarField and with a boolList I want fill the value inside each cells. In particular if the bool is equal true, the cell value should be 1 otherwise should be 0.

I post the volScalarField costructor:
volScalarField alfa
(
IOobject
(
"alfa",
fileName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_
);

and the costructor of the boolList:

boolList zoneCell(mesh_.nCells(), false);
const labelList& cellLabels = mesh_.cellZones()[cellZoneID_];
forAll(cellLabels, i)
{
zoneCell[cellLabels[i]] = true;
}

obviously mesh_ is a reference to the fvMesh.

If someone can help me I'll be very great-full.

Diego

boger November 18, 2010 09:48

Code:

forAll(zoneCell,cellI)
{
    if (zoneCell[cellI])
    {
        alfa[cellI] = 1;
    }
}

But why not just skip the bool list and set alfa directly?
Code:

forAll(cellLabels, i)
{
    alfa[cellLabels[i]] = 1;
}

If there's a clever way to cast the list without explicitly looping over it, I don't know it.

DiegoNaval November 18, 2010 12:37

I'm agree with you David, the reason is simply that I'm modifying a pre-exist class so for the moment I prefer to don't change much the original code.
In any case your way to fill the alfa is use-full, so thank you very much.

Diego

Fransje November 19, 2010 09:52

Would you not loose the conditional variable setting by setting alfa directly?? The idea was to set alfa = 1 if the value of the boolList was true and 0 if it was false no?

The explicit looping itself is not a problem performance-wise. The looping is implemented in a way that makes it extremely fast.

Quote:

Originally Posted by dab143psuedu (Post 283907)
Code:

forAll(zoneCell,cellI)
{
    if (zoneCell[cellI])
    {
        alfa[cellI] = 1;
    }
}

But why not just skip the bool list and set alfa directly?
Code:

forAll(cellLabels, i)
{
    alfa[cellLabels[i]] = 1;
}

If there's a clever way to cast the list without explicitly looping over it, I don't know it.


boger November 19, 2010 10:01

Yes, but my impression is that boollist is only true in cells that are members of the cellLabels, so unless he needs boollist for some other reason as well, it's more direct to just set alfa based on the cellLabels list and skip the intermediate step. Maybe I'm reading his code wrong.

As far as avoiding the loop, I just thought it would be clever if there was a way to cast the entire boolList into a scalarList in one command. I feel like a failure every time I have to explicitly code a loop into OpenFOAM. :)

Fransje November 19, 2010 10:11

By the way, if zoneCell is the boolList containing the true and false values, and it has the same number of elements as alfa, could you not try:
Code:

forAll(zoneCell, index)
{
    alfa.internalField()[index] = zoneCell[index];
}

You should make sure that the recasting is done correctly, but in C++ booleans are evaluated as numbers. 1 for true, and 0 for false.

Good luck!

Francois.

Fransje November 19, 2010 10:26

Hum... I focused more on the explanation of what he was trying to do than on the code he provided.. But code-wise I can only agree!

As for the explicit looping there is a possibility of avoiding the looping when doing operations among similar data types when using the +, -, *, /, = and == operators. What I understood from Hrvoje was that the different operators have been overloaded quite powerfully, and could, in some cases, even do a variable re-sizing automatically if necessary. But for very specific details, you should refer to the source code because I'm afraid I did not memorize all the examples he was giving :o.. They were coming quite fast in a very short period of time..
The operator overloadings are sometimes also kind enough to allows inter-data operations without looping I noticed.
And downcasting also works quite well from personal experience. (like casting a volScalarField into a scalarField/scalarList.)

I hope it helps!

Kind regards,

Francois.

Ps: I feel the same way about every explicit code loop I implement..... :P


Quote:

Originally Posted by dab143psuedu (Post 284106)
Yes, but my impression is that boollist is only true in cells that are members of the cellLabels, so unless he needs boollist for some other reason as well, it's more direct to just set alfa based on the cellLabels list and skip the intermediate step. Maybe I'm reading his code wrong.

As far as avoiding the loop, I just thought it would be clever if there was a way to cast the entire boolList into a scalarList in one command. I feel like a failure every time I have to explicitly code a loop into OpenFOAM. :)



All times are GMT -4. The time now is 17:52.