|
[Sponsors] |
November 17, 2010, 14:44 |
Fill a volScalarField
|
#1 |
Member
Diego Villa
Join Date: Mar 2010
Location: Genova Italy
Posts: 37
Rep Power: 16 |
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 |
|
November 18, 2010, 09:48 |
|
#2 |
Senior Member
David Boger
Join Date: Mar 2009
Location: Penn State Applied Research Laboratory
Posts: 146
Rep Power: 17 |
Code:
forAll(zoneCell,cellI) { if (zoneCell[cellI]) { alfa[cellI] = 1; } } Code:
forAll(cellLabels, i) { alfa[cellLabels[i]] = 1; }
__________________
David A. Boger |
|
November 18, 2010, 12:37 |
|
#3 |
Member
Diego Villa
Join Date: Mar 2010
Location: Genova Italy
Posts: 37
Rep Power: 16 |
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 |
|
November 19, 2010, 09:52 |
|
#4 | |
Senior Member
Francois
Join Date: Jun 2010
Posts: 107
Rep Power: 21 |
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:
|
||
November 19, 2010, 10:01 |
|
#5 |
Senior Member
David Boger
Join Date: Mar 2009
Location: Penn State Applied Research Laboratory
Posts: 146
Rep Power: 17 |
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 |
|
November 19, 2010, 10:11 |
|
#6 |
Senior Member
Francois
Join Date: Jun 2010
Posts: 107
Rep Power: 21 |
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]; } Good luck! Francois. |
|
November 19, 2010, 10:26 |
|
#7 | |
Senior Member
Francois
Join Date: Jun 2010
Posts: 107
Rep Power: 21 |
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:
|
||
Tags |
boollabel, volscalarfield |
|
|
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 |