|
[Sponsors] |
August 30, 2014, 12:04 |
output the face centroids
|
#1 |
Senior Member
Join Date: Jan 2013
Posts: 372
Rep Power: 14 |
Dear All,
Due to some reason, I need to output the boundary face centroid coordinates. The output code is as follows: Code:
#include <fstream> #include <iostream> #include "fvMesh.H" #include "Time.H" //#include "primitiveMesh.H" #include "argList.H" #include "polyMesh.H" using namespace std; int main(int argc, char *argv[]) { # include "setRootCase.H" # include "createTime.H" # include "createPolyMesh.H" using namespace Foam; Info<< "Output face centres of given patch\n" << endl; //change the patch name to your boundary name label patchI = mesh.boundaryMesh().findPatchID("outlet"); label iFace = 3600; scalar x[iFace]; scalar y[iFace]; scalar z[iFace]; forAll(mesh.boundaryMesh()[patchI].faceCentres(), faceI) { x[faceI] = mesh.boundaryMesh()[patchI].faceCentres()[faceI].x(); y[faceI] = mesh.boundaryMesh()[patchI].faceCentres()[faceI].y(); z[faceI] = mesh.boundaryMesh()[patchI].faceCentres()[faceI].z(); // Info<<faceI<<" "<<x[faceI]<<" "<<y[faceI]<<" "<<z[faceI]<<" "<<endl; } //output the file Info<<"Output the coordinates of face centers..."<<endl; ofstream o_file; o_file.open("OUTLET.txt"); if(o_file.is_open()) { for(label i=1;i<=iFace;i++) { o_file<<x[i]<<" "<<y[i]<<" "<<z[i]<<" "<<endl; } } o_file.close(); } Code:
file: PatchFaceCenter.C EXE = $(FOAM_USER_APPBIN)/PatchFaceCenter options: EXE_INC = \ -I$(LIB_SRC)/finiteVolume/lnInclude EXE_LIBS = \ -lfiniteVolume \ -lgenericPatchFields Code:
/home/user/OpenFOAM/OpenFOAM-2.1.1/tutorials/combustion/fireFoam/les/smallPoolFire3D Code:
......... 0.491667 0 0.391667 0.491667 0 0.408333 0.491667 0 0.425 0.491667 0 0.441667 0.491667 0 0.458333 0.491667 0 0.475 0.491667 0 0.491667 0.00289776 0 0 Then I use the above code to extract the face centroids of another boundary face outlet, the results are as follows: Code:
...... 0.491667 1 0.391667 0.491667 1 0.408333 0.491667 1 0.425 0.491667 1 0.441667 0.491667 1 0.458333 0.491667 1 0.475 0.491667 1 0.491667 0.00289776 0 0 I checked the boundary face centroid coordinate of my other meshes and found that the point (0.00289776 0 0) is always there in the last row of the face centroid list. Could anyone tell me what happen to the face centroid coordinates? Thank you so much. OFFO |
|
August 30, 2014, 12:30 |
|
#2 |
Retired Super Moderator
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128 |
Greetings OFFO,
It seems to me that you need to study more about C/C++. The reason why I say this, for example, is because of this piece of code: Code:
label iFace = 3600; scalar x[iFace]; scalar y[iFace]; scalar z[iFace]; In addition, your "for" loop for saving to the file is starting at 1 when it should start at 0, namely it should be like this: Code:
for(label i=0;i<iFace;i++) Code:
x[faceI] = mesh.boundaryMesh()[patchI].faceCentres()[faceI].x(); When programming, never assume anything. You should always try to diagnose, assess and confirm if things are working as intended, not as you hope they should. Best regards, Bruno
__________________
|
|
August 30, 2014, 13:07 |
|
#3 |
Senior Member
Join Date: Jan 2013
Posts: 372
Rep Power: 14 |
Dear Bruno,
After changing to the following, it works now: Code:
for(label i=0;i<iFace-1;i++) Thank you so much for your suggestion. |
|
August 30, 2014, 13:18 |
|
#4 | ||
Retired Super Moderator
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128 |
Quote:
C/C++ arrays are 0 indexed. Which means that an array of size 3600, will start in 0 and end in 3599. Your "for" loop is ordered to run only while "i<iFace-1" is true. In other words, only while "i<3599", which means that you are not accessing the position "3599", because the last valid iteration is "3598<3599". Quote:
Code:
y[0] z[0] garbage If you were using a more restrictive Operating System or security measure, the application should have crashed when it tried to access outside of the array size. |
|||
August 30, 2014, 13:24 |
|
#5 |
Senior Member
Join Date: Jan 2013
Posts: 372
Rep Power: 14 |
Thank you.
About your first comment, I double check my code, it is like the following (i<=iFace-1): Code:
for(label i=0;i<=iFace-1;i++) Thank you very much for your help. |
|
August 31, 2014, 08:34 |
|
#6 |
Senior Member
Join Date: Jan 2013
Posts: 372
Rep Power: 14 |
Dear Bruno,
In openfoam, is there any defined class to write out the data into a txt file or other kind of external files? In the code I posted here, I use: Code:
ofstream o_file; o_file.open("OUTLET.txt"); OFFO |
|
August 31, 2014, 18:29 |
|
#7 |
Retired Super Moderator
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128 |
Hi OFFO,
There are many ways to solve a problem. But most of the times, what matters is that the problem is solved and not how it is solved... but then again, the "end doesn't justify the means", but still... if it works for you, why spend more time searching for another solution? This to say that at this moment I can't remember any better way to do this. To do so I would have to look at OpenFOAM's source code and find examples on this could be done. Therefore, I'll delegate that task to you and suggest that you study the source code examples that are located at the path given by this command: Code:
echo $FOAM_APP/test Happy studying! Best regards, Bruno
__________________
|
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
area does not match neighbour by ... % -- possible face ordering problem | St.Pacholak | OpenFOAM | 11 | September 4, 2024 05:28 |
[snappyHexMesh] How to define to right point for locationInMesh | Mirage12 | OpenFOAM Meshing & Mesh Conversion | 7 | March 13, 2016 15:07 |
mixerVesselAMI2D's mass is not balancing | sharonyue | OpenFOAM Running, Solving & CFD | 6 | June 10, 2013 10:34 |
[blockMesh] non-orthogonal faces and incorrect orientation? | nennbs | OpenFOAM Meshing & Mesh Conversion | 7 | April 17, 2013 06:42 |
(gambit)projecting just a part of mesh on another face? | gholamghar | ANSYS Meshing & Geometry | 2 | March 14, 2011 18:14 |