CFD Online Discussion Forums

CFD Online Discussion Forums (
-   Main CFD Forum (
-   -   Algorithm to find cell no. in O-grid for a point (

Ben June 21, 2007 00:57

Algorithm to find cell no. in O-grid for a point

I am looking for an effective algorithm which can determine the i,j cell number of a point on an O-grid.

Can someone enlighten me?


Mani June 21, 2007 08:50

Re: Algorithm to find cell no. in O-grid for a poi
How is the point defined? By physical coordinates?

The (in)efficiency of any search algorithm will depend on how general it needs to be. Your question implies a 2D grid. Are there any other conditions that may help to specify the problem?

Ben June 21, 2007 09:49

Re: Algorithm to find cell no. in O-grid for a poi

put it simply, I have a pt with coordinates (x,y). I also have an O-grid, in a 2D array xx(i,j), yy(i,j). There's no transformation and they'r all physical coordinates.

So I want to find the i,j cell on the O-grid where the pt (x,y) lies in. I am now looping through the O-grid cells, and check if the pt lies inside the cell (quadrilateral).

I am using overset grids so I'm trying to find the interpolation stencil for the cartesian pt on the O-grid.


TG June 21, 2007 13:37

Re: Algorithm to find cell no. in O-grid for a poi
What you need to do to speed up the process is create a set of zones covering the extent of the mesh, place each cell into a the zone (or zones) it falls into geometrically and then only search the cells in the zone that your point falls in to see if its in or out. You can experiment, but a 10x10 covering the 2-d mesh will probably speed you up a couple of orders of magnitude.

Mani June 21, 2007 13:54

Re: Algorithm to find cell no. in O-grid for a poi
Ok, I assume you are looping in some arbitrary order through the whole grid, and check each cell until you've found the right one. One way to speed it up (a lot) is to use a quad search. Split your grid into four equal parts. Then check each of the four subgrids for inclusion of your point. You do this with a simplified definition of the subgrid boundaries, e.g. in the simplest case you would define a subgrid only by its 4 vertices (as if the whole grid consisted of 4 huge cells). Once you found the right quad, you divide it into 4 parts again, and keep going in this way until you have arrived at the grid cell level and found the right cell. It's hard to describe but I think you get the idea. It's somewhat analogous to the "multigrid" idea. Start with the coarsest grid and work your way down to the actual resolution. In the general case, this will speed up your search enormously. For example, for a grid size of 512*512 cells, the simple search will take up to 262144 evaluations. The quad search will take at most 36 evaluations! One thing to keep in mind though: You'll need to be a little careful with the evaluations on the coarse grids. They will not be exact if you only use 4 vertices and assume straight cell faces. However, even with some extra care, the quad search is a lot faster than the simple approach. It can be elegantly programmed as a recursive procedure. It's ideal if your grid size is in powers of 2, but you can make it work on any grid (the four parts don't have to be of equal size).

Once you've got that working you may also think about successful strategies to optimize your starting position. For example, if you have already found one point, the next node of your overset grid is not going to be far away. Search in the vicinity!

Ben June 22, 2007 11:23

Re: Algorithm to find cell no. in O-grid for a poi
Thank you TG & Mani for the suggestions!

All times are GMT -4. The time now is 15:46.