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

output the face centroids

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 30, 2014, 11:04
Default output the face centroids
  #1
Senior Member
 
Join Date: Jan 2013
Posts: 372
Rep Power: 14
openfoammaofnepo is on a distinguished road
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();

}
For other one to do the tests, I also present the file and option here:

Code:
file:

PatchFaceCenter.C

EXE = $(FOAM_USER_APPBIN)/PatchFaceCenter

options:

EXE_INC = \
    -I$(LIB_SRC)/finiteVolume/lnInclude

EXE_LIBS = \
    -lfiniteVolume \
    -lgenericPatchFields
Let's take the case in the following path as an example:
Code:
/home/user/OpenFOAM/OpenFOAM-2.1.1/tutorials/combustion/fireFoam/les/smallPoolFire3D
Using the above code, I got the list of centroid coordinate of each face. For example, for the boundary face base in it, they are:
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
It is very strange that in the last row of the above list, the coordinate (0.00289776 0 0) is in it. Obviously this point is not on that face.

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
Again the point (0.00289776 0 0) appears in the last and in the last row. Actually it is not on that face.

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
openfoammaofnepo is offline   Reply With Quote

Old   August 30, 2014, 11:30
Default
  #2
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,975
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
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];
This means that you're allocating a fixed size for the arrays x,y and z of 3600 entries only. If your patch has more than 3600 faces, then it's only natural that your program will not work as intended.

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++)
If this wasn't enough, for example, the following code is very risky:
Code:
x[faceI] = mesh.boundaryMesh()[patchI].faceCentres()[faceI].x();
You're assuming that "faceI" will start at 0 or 1.

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
__________________
wyldckat is offline   Reply With Quote

Old   August 30, 2014, 12:07
Default
  #3
Senior Member
 
Join Date: Jan 2013
Posts: 372
Rep Power: 14
openfoammaofnepo is on a distinguished road
Dear Bruno,

After changing to the following, it works now:
Code:
for(label i=0;i<iFace-1;i++)
So the (0.00289776 0 0) are the numbers generated randomly by the code?

Thank you so much for your suggestion.
openfoammaofnepo is offline   Reply With Quote

Old   August 30, 2014, 12:18
Default
  #4
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,975
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
Quote:
Originally Posted by openfoammaofnepo View Post
After changing to the following, it works now:
Code:
for(label i=0;i<iFace-1;i++)
Not reading/studying a good manual can get you into trouble
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:
Originally Posted by openfoammaofnepo View Post
So the (0.00289776 0 0) are the numbers generated randomly by the code?
You were accessing the position "3600" in the arrays, when "3599" was the last valid position. What probably happened is that the last iteration was in fact accessing the first value on the next array, and was actually outputting the same values as if you were using this code:
Code:
y[0] z[0] garbage
I believe that most Linux Distributions will provide RAM all set to "0" for an application to use, which is why the garbage entry is 0.
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.
wyldckat is offline   Reply With Quote

Old   August 30, 2014, 12:24
Default
  #5
Senior Member
 
Join Date: Jan 2013
Posts: 372
Rep Power: 14
openfoammaofnepo is on a distinguished road
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++)
So it give me complete 3600 face centeriod coordinates now.

Thank you very much for your help.
openfoammaofnepo is offline   Reply With Quote

Old   August 31, 2014, 07:34
Default
  #6
Senior Member
 
Join Date: Jan 2013
Posts: 372
Rep Power: 14
openfoammaofnepo is on a distinguished road
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");
I was wondering if there is any better appraoches. Thank you.

OFFO
openfoammaofnepo is offline   Reply With Quote

Old   August 31, 2014, 17:29
Default
  #7
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,975
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
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
If you prefer to use your browser and search online: https://github.com/OpenFOAM/OpenFOAM...lications/test

Happy studying! Best regards,
Bruno
__________________
wyldckat is offline   Reply With Quote

Reply


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
area does not match neighbour by ... % -- possible face ordering problem St.Pacholak OpenFOAM 10 February 7, 2024 21:50
[snappyHexMesh] How to define to right point for locationInMesh Mirage12 OpenFOAM Meshing & Mesh Conversion 7 March 13, 2016 14:07
mixerVesselAMI2D's mass is not balancing sharonyue OpenFOAM Running, Solving & CFD 6 June 10, 2013 09:34
[blockMesh] non-orthogonal faces and incorrect orientation? nennbs OpenFOAM Meshing & Mesh Conversion 7 April 17, 2013 05:42
(gambit)projecting just a part of mesh on another face? gholamghar ANSYS Meshing & Geometry 2 March 14, 2011 17:14


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