CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM (
-   -   Acessing neighbouring values in specific directions (

RoE January 24, 2011 16:31

Acessing neighbouring values in specific directions
Is it possible to access values defined at cell faces or neighbouring cells in specific directions, e.g. the faces in the x-direction? For example, I'd like to sweep through the cells, compare the two snGradients in the x-direction in each cell and chose the larger one. Browsing the forum I only found the commands
mesh.cellCells(), mesh.owner() and mesh.neighbour() which will give me all neighbouring cells and faces, respectively.

Thanks in advance!

akidess January 25, 2011 05:01

From what I know this is not possible in OpenFoam due to it's unstructured nature. You'll have to use mesh.cellCells() & co and then start comparing coordinates (mesh[celli].C()) to find the appropriate face on your own.

nakul January 26, 2011 11:30

Hi Anton,

What exactly do you mean by OpenFoam's "unstructured" nature?

mvoss January 26, 2011 11:36

Answer: that it is not of interest how the cell looks like (hex tet prism ,...).
it is just a matter of: which faces are related to the cell and what is the neighbor the first place youre just asking for flow from one face to another,...and afterwards doing some calc to get the cellcenter value from the cellface values.
That is why there is some kind of "unstructuredness" in OpenFoam. There is no ijk-direction to cycle over like when thinking about "structured" meshes.


@Anton: sorry... ive tagged this thread thats why im answering... no offense.

nakul January 26, 2011 13:09

Thanks for the quick reply.

Could please point me to some reference or starting material where I can read more about this sort of structuredness/unstructuredness? (Not necessarily specific to OpenFOAM!!!)

akidess January 27, 2011 05:32

Any basic CFD book will have a section on grids. I like Ferziger, Peric. Other than that, the CFD wiki on this website and even wikipedia have entries about structured and unstructured grids, and you will find thousands of results on google about this topic.

RoE February 1, 2011 13:04

Thanks for your help!
The advice to compare the coordinates is especially helpful.

As a first test I wanted to extract the neighbours (cellCells) and their coordinates using the following code:

fileName outDir = runTime.path()/"data";
OFstream nStream(outDir/"datafile.dat");

const volVectorField& C = mesh.C();


const unallocLabelList& neigh = mesh.cellCells()[cellI];

nStream << " Cell: " << cellI << " Neighbours: " << neigh << " Center of Cell: " << C[cellI];


vector CNeigh = C[neighI];
nStream << " Neighbour: " << neigh[neighI] << " Center of neighbour: " << CNeigh;
nStream <<" " << endl;

The results, however, are a little strange. Here are the first lines of the output file:

Cell: 0 Neighbours: 2(1 20) Center of Cell: (0.0025 0.0025 0.005) Neighbour: 1 Center of neighbour: (0.0025 0.0025 0.005) Neighbour: 20 Center of neighbour: (0.0075 0.0025 0.005)

The code finds two neighbours (cells 1 and 20) for cell 0 which is correct - cell 0 is a corner of a 2D mesh. It gives the proper coordinates of the center of cell 1. Nevertheless, the coordinates of the first neighbour (cell 1) are erroneous, the code gives the coordinates of cell 0! The coordinates of cell 21 are wrong, too. The code provides the coordinates of cell 1.

The results for cell 1 are also erroneous:
Cell: 1 Neighbours: 3(0 2 21) Center of Cell: (0.0075 0.0025 0.005) Neighbour: 0 Center of neighbour: (0.0025 0.0025 0.005) Neighbour: 2 Center of neighbour: (0.0075 0.0025 0.005) Neighbour: 21 Center of neighbour: (0.0125 0.0025 0.005)

Any advice?

Thank you!

lin February 1, 2011 22:17

try to change "vector CNeigh = C[neighI];"
"vector CNeigh = C[neigh[neighI]];"

RoE February 2, 2011 06:23

Wonderful - that does the trick!

Thank you very much!

hawkeye321 October 3, 2012 21:11

east and north cell values
Is there any function in OF to give the values of a parameter at the north and east cell of a specific cell, say celli?

Bernhard October 4, 2012 03:38

The answer to your question is in the second post of this topic.

All times are GMT -4. The time now is 08:27.