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

Fill a volScalarField

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 17, 2010, 14:44
Default Fill a volScalarField
  #1
Member
 
Diego Villa
Join Date: Mar 2010
Location: Genova Italy
Posts: 37
Rep Power: 16
DiegoNaval is on a distinguished road
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
DiegoNaval is offline   Reply With Quote

Old   November 18, 2010, 09:48
Default
  #2
Senior Member
 
David Boger
Join Date: Mar 2009
Location: Penn State Applied Research Laboratory
Posts: 146
Rep Power: 17
boger is on a distinguished road
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.
__________________
David A. Boger
boger is offline   Reply With Quote

Old   November 18, 2010, 12:37
Default
  #3
Member
 
Diego Villa
Join Date: Mar 2010
Location: Genova Italy
Posts: 37
Rep Power: 16
DiegoNaval is on a distinguished road
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
DiegoNaval is offline   Reply With Quote

Old   November 19, 2010, 09:52
Default
  #4
Senior Member
 
Francois
Join Date: Jun 2010
Posts: 107
Rep Power: 21
Fransje will become famous soon enough
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 View Post
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.
Fransje is offline   Reply With Quote

Old   November 19, 2010, 10:01
Default
  #5
Senior Member
 
David Boger
Join Date: Mar 2009
Location: Penn State Applied Research Laboratory
Posts: 146
Rep Power: 17
boger is on a distinguished road
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.
__________________
David A. Boger
boger is offline   Reply With Quote

Old   November 19, 2010, 10:11
Default
  #6
Senior Member
 
Francois
Join Date: Jun 2010
Posts: 107
Rep Power: 21
Fransje will become famous soon enough
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 is offline   Reply With Quote

Old   November 19, 2010, 10:26
Default
  #7
Senior Member
 
Francois
Join Date: Jun 2010
Posts: 107
Rep Power: 21
Fransje will become famous soon enough
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 .. 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 View Post
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 is offline   Reply With Quote

Reply

Tags
boollabel, volscalarfield


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
How to create initiate a volScalarField p without reading from disk NO_READ does not seem to work dbxmcf OpenFOAM Running, Solving & CFD 14 March 25, 2022 07:08
if-loop, volScalarField comparison volker OpenFOAM 7 March 6, 2020 21:03
Problems with creating a volScalarField georlade OpenFOAM Programming & Development 4 December 4, 2016 13:31
Confused about how OF handles operation between volScalarField and dimensionedScalar Edy OpenFOAM 3 September 30, 2010 11:07
dimensioned volScalarField lions85 OpenFOAM 1 November 12, 2009 03:41


All times are GMT -4. The time now is 18:38.