CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Meshing & Mesh Conversion (https://www.cfd-online.com/Forums/openfoam-meshing/)
-   -   [Technical] How to identify cell neighbours (https://www.cfd-online.com/Forums/openfoam-meshing/61906-how-identify-cell-neighbours.html)

JasonWang3 September 24, 2015 10:16

Hi Su

Do you have any idea on how to access the neighbour cells of cell(i) in y direction.

The cell P is known now, which is close to the wall, and I want to use the label of cell N which is above P, and the face label of n, which is between cell P and cell N.

Thanks a lot.

JasonWang3 September 24, 2015 11:47

Quote:

Originally Posted by CHARLES (Post 489981)
I'm confused about how to access a specific cell using cellCells. For example, how can I use cellCells to access the NORTH cell of each cell in my domain?

Here's an example of what I would like to do... I know that the code may not compile, I'm just trying to explain what I'm doing.

Code:

V=value; //V is a boundary field ('value' is calculated and varies across the domain)

forAll(U, celli)
{
U[celli]=V[celli];
U[NORTH of celli] = U[celli];
}


hi Charles

Have you found a way to your questions? I meet the same situation.

Thanks a lot.

Xinguang

fs82 October 13, 2015 11:09

I am currently facing with this problem as well. Unfortunately, the idea of a compass like navigation is not possible for unstructured non-orthogonal meshes to my opinion. However, for playing around I assumed a orthogonal mesh aligned with the coordinate axis and by this it is possible to detect the direction of the neighbour by calculating the vector between the cell centres of the neighbour and the master cell. Some quick & and dirty piece of code would look like this

Code:

    forAll(mesh_.cells(),cellI)
    {
        vector cellCenter = mesh_.C()[cellI];
        labelList neighbours = mesh_.cellCells()[cellI];
        forAll(neighbours,cellJ)
        {
            vector direction = mesh_.C()[neighbours[cellJ]] - cellCenter;
            label axis = 0;
            for(label i=0; i<3;i++)
            {
                if (mag(direction[i])>=mag(direction[axis]))
                {
                    axis = i;
                }
            }
            switch(axis)
            {
                case 0:
                    Info << "Seems like x-Axis" << endl;
                    break;
                case 1:
                    Info << "Seems like y-Axis" << endl;
                    break;
                case 2:
                    Info << "Seems like z-Axis" << endl;
                    break;
            }
        }
    }

Best regards,
Fabian

chefkoch89 March 7, 2016 07:51

Neighbours within a radius
 
Hey,

I need to check a value (temperature) of cells within a radius around the celli. Do I understand it right, that it is just possible to get the direct neighbors of the celli with your code?
Right now I have no ideo how to implement such loop over all cells into my code. Do you got any proposals?

Thank you
:rolleyes:

fs82 March 16, 2016 04:09

I would say yes, you can do it with my code sniplet. However, there are some tools who create cellZones and cellSets and have a option to create spheres around a given centre. I would suggest to look into this tools and see how the handle the creation of spheres. As a starting point look at meshTools/sets/cellSources/sphereToCell/sphereToCell.C

Best regrads
Fabian

chefkoch89 March 16, 2016 06:08

Thanks for the response.

I do understand the usage of sphereToCell just to refine the mesh, but not to create a virtual value to identify the cell temperatures within this sphere.
Maybe you can write a short code to show what you mean.

Thanks a lot

fs82 March 16, 2016 10:12

I think the solution is already included in sphereToCell.C in the function combine:
Code:

//This gets all cell centres of all mesh cells
const pointField& ctrs = mesh_.cellCentres();

    //This calculates the radius of your sphere
    const scalar radSquared = radius_*radius_;

    //Loop over all cells
    forAll(ctrs, cellI)
    {
        //calculates the distance between the cell centre and the current cell
        //with the index cellI
        scalar offset = magSqr(centre_ - ctrs[cellI]);
        if (offset <= radSquared)
        {
            //Do something, e.g. store the current cell id in a list
        }
    }

May be you have to think a little bit about the loops and how to define the centre point for your spheres. If you want to have a sphere around each cell, than I guess with the code above you have to add a second loop about all cells around it. However, this should be feasible. Finally, if you stored the list of cell ids for each sphere you can work with it and calculate values, averages or whatever. This is some programming effort.

Best regards
Fabian

chefkoch89 March 17, 2016 10:16

This sounds great and I think that's the right way to get what I need. My problem is that I don't know how to use sphereToCell. I always find the usage within the preprocessor "setFields".

Thank's for the response.

fs82 March 17, 2016 10:36

My suggestion was NOT to use sphereToCell. You have to write your own utility and copy the parts of the code of sphereThoCell which helps you to solve your problem. May be it is possible to use a utility or a function of swak4Foam to yield the same result, but I do not know that at the moment.

aghsin April 7, 2016 08:36

Quote:

Originally Posted by su_junwei (Post 181929)
Hi Khaled

mesh.cellCells()[cellI] gives the neighbours of cell cellI

su junwei

Hi,
How can I use the above command in openFOAM? I mean where must I write this function?

Mahdi2010 June 13, 2017 11:00

I read many threads, but I have a question which can be related to this thread:

Is it possible to access each cell neighbor and assign the value of every 2 cells to an temporary cell list?
Let's imagine we have a 64x64 structured 2D domain. I want to take every two cells in X-direction and Y-direction,
and make a 32x32 imaginary domain with the cell center values coming from surrounding cells.

It is like to hold a magnifier in hand, and zoom out what we see!. Then the cell center values in the 32x32 imaginary
domain can be calculated as average of the every 4 cell center (it is just a temporary list not really a mesh).

fs82 June 15, 2017 06:39

I do not have a solution for your problem, but I would like to add a comment and an idea

You have to have in mind that OpenFOAM uses an unstructured mesh which could consist of tetrahedra, hexahedra or whatever. Furthermore, the mesh in OpenFOAM is face based, which means every face knows about the cells which it belongs to. To conclude, this means you can get a list of all cells, you also can get a list of faces that belong to a certain cell and with this it is possible to get the neighbouring cell for each face. But than you get stuck, because the mesh is unstructured band you do not know which direction you have to move on. Everything you can do is a very costly search, checking the distanced and so on. What you are describing is very easy on structured meshes, especially on equidistant meshes. However, OpenFOAM is built for unstructured meshes and, hence, you are somewhat limited.

But there is the possibility to map between meshes with "mapFields". To create a coarse mesh with blockMesh is quite simple. Furthermore, there are various tools available to manipulate the mesh, e.g., refineMesh. May be this helps to solve your problem.

Best regards
Fabian

Mahdi2010 June 15, 2017 07:46

Quote:

Originally Posted by fs82 (Post 653349)
I do not have a solution for your problem, but I would like to add a comment and an idea

You have to have in mind that OpenFOAM uses an unstructured mesh which could consist of tetrahedra, hexahedra or whatever. Furthermore, the mesh in OpenFOAM is face based, which means every face knows about the cells which it belongs to. To conclude, this means you can get a list of all cells, you also can get a list of faces that belong to a certain cell and with this it is possible to get the neighbouring cell for each face. But than you get stuck, because the mesh is unstructured band you do not know which direction you have to move on. Everything you can do is a very costly search, checking the distanced and so on. What you are describing is very easy on structured meshes, especially on equidistant meshes. However, OpenFOAM is built for unstructured meshes and, hence, you are somewhat limited.

But there is the possibility to map between meshes with "mapFields". To create a coarse mesh with blockMesh is quite simple. Furthermore, there are various tools available to manipulate the mesh, e.g., refineMesh. May be this helps to solve your problem.

Best regards
Fabian


Thanks for your contribution to this discussion, I understand your point as I also spent much time on that. Unlike the equidistant structured grids that we can create with any kind of programming tool, OpenFOAM has this limitation.

Regarding the other ideas like mapping, I think this is not also possible, because I would need this so-called imaginary "coarse grid" during the simulation in every time step. In other words, it is going to act as an intermediate container list storing average of every 4 cells in the intersecting vertex. Then it will be deleted/updated in the next time step. Therefore, this is something completely independent of the mesh, while I assume mapping is something in pre-proceesing phase. What do you think?

fs82 June 16, 2017 02:41

A possible idea could be, in the case you do not use a moving mesh or mesh refinement, to do before starting the simulation a search run and store the relationships between your simulation mesh and the coarse mesh. You can use the pointInCell and findCell functions of polymesh to check if a cell center is located within a certain range of another one. However, I tried this once and it is really a slow process. So you will not be able to do it every time step. Another idea would be to have a second coarse mesh during runtime and instead of averaging do an interpolation?

Best regards
Fabian

Mahdi2010 June 16, 2017 03:15

Quote:

Originally Posted by fs82 (Post 653484)
A possible idea could be, in the case you do not use a moving mesh or mesh refinement, to do before starting the simulation a search run and store the relationships between your simulation mesh and the coarse mesh. You can use the pointInCell and findCell functions of polymesh to check if a cell center is located within a certain range of another one. However, I tried this once and it is really a slow process. So you will not be able to do it every time step. Another idea would be to have a second coarse mesh during runtime and instead of averaging do an interpolation?

Best regards
Fabian


Your first suggestion is something very demanding as you said because, in the case of having a fine grid, it would take too long for even one step. But I am interested in the second idea. You mean having both grids on the simulation, but using "fine" for the simulation and "coarse" for just interpolation of values?Is this doable in OpenFOAM?


This is actually what I wanted to to do from the beginning, but I assumed only one mesh can be imported to the OpenFOAM not two. In addition, I also tried to have two meshes connected at one patch, but this would also result in other issues like BC for the intersection! Perhaps I have gone too far and there is an easier way to proceed with this interpolation idea you also suggested. Could you please tell me if any of OF tutorials does this?Or in any post on this forum has been already discussed?

aghsin June 16, 2017 11:12

overset grid
 
Quote:

Originally Posted by Mahdi2010 (Post 653489)
Your first suggestion is something very demanding as you said because, in the case of having a fine grid, it would take too long for even one step. But I am interested in the second idea. You mean having both grids on the simulation, but using "fine" for the simulation and "coarse" for just interpolation of values?Is this doable in OpenFOAM?


This is actually what I wanted to to do from the beginning, but I assumed only one mesh can be imported to the OpenFOAM not two. In addition, I also tried to have two meshes connected at one patch, but this would also result in other issues like BC for the intersection! Perhaps I have gone too far and there is an easier way to proceed with this interpolation idea you also suggested. Could you please tell me if any of OF tutorials does this?Or in any post on this forum has been already discussed?

Hi have you already knew about overset grid? it might be a solution for you.

Mahdi2010 June 17, 2017 03:40

Quote:

Originally Posted by aghsin (Post 653566)
Hi have you already knew about overset grid? it might be a solution for you.


I have actually heard about overset (not touched though), but as far as I know, this is something for complicated geometries for that a normal uniform meshing is not practical. The domain can use this feature to make a connection between the different type of meshes to cover the whole geometry. Therefore, in overset all the grids are really involved in the solution.

But what I am looking for is much simpler (in concept): The coarser grid is going to be used only as the container of (the averaged values of) what is computed over the finer grid at each time step. So, no involvement in the simulation from the coarse grid is needed.

Have you seen something similar before?

fs82 June 19, 2017 02:46

I think what you are aiming at is often named as chimera grids. I never used it but I have seen a couple of presentations at the OpenFOAM User Conference. I think the mapping between the grids is the most important issue. As far as I know people use external library for mapping, but I am not sure as I never used it, e.g., SUGGAR++ (http://celeritassimtech.com/?page_id=21).

Best regards
Fabian

aghsin June 19, 2017 03:22

Quote:

Originally Posted by Mahdi2010 (Post 653635)
I have actually heard about overset (not touched though), but as far as I know, this is something for complicated geometries for that a normal uniform meshing is not practical. The domain can use this feature to make a connection between the different type of meshes to cover the whole geometry. Therefore, in overset all the grids are really involved in the solution.

But what I am looking for is much simpler (in concept): The coarser grid is going to be used only as the container of (the averaged values of) what is computed over the finer grid at each time step. So, no involvement in the simulation from the coarse grid is needed.

Have you seen something similar before?

Sorry, I have not seen similar thing.

vishwesh November 20, 2017 00:40

Hi!

My problem can also get some help from sphereToCell. I have written a solver which can identify the centre and the radius of the sphere. I want to get all the cells inside that sphere and modify their alpha values. My question is can I directly use the combine function given in sphereToCell.C by putting sphereToCell.C, and other relevant files in the same folder, adding them to Make/files file, including them in the main solver and using the function combine?

Thanks

Vishwesh

Quote:

Originally Posted by fs82 (Post 590076)
I think the solution is already included in sphereToCell.C in the function combine:
Code:

//This gets all cell centres of all mesh cells
const pointField& ctrs = mesh_.cellCentres();

    //This calculates the radius of your sphere
    const scalar radSquared = radius_*radius_;

    //Loop over all cells
    forAll(ctrs, cellI)
    {
        //calculates the distance between the cell centre and the current cell
        //with the index cellI
        scalar offset = magSqr(centre_ - ctrs[cellI]);
        if (offset <= radSquared)
        {
            //Do something, e.g. store the current cell id in a list
        }
    }

May be you have to think a little bit about the loops and how to define the centre point for your spheres. If you want to have a sphere around each cell, than I guess with the code above you have to add a second loop about all cells around it. However, this should be feasible. Finally, if you stored the list of cell ids for each sphere you can work with it and calculate values, averages or whatever. This is some programming effort.

Best regards
Fabian



All times are GMT -4. The time now is 03:26.