finding grid point neighbors matt craven for: dr. anitescu math 1110 spring 2002
DESCRIPTION
Choosing a data structure " In our data structure, we need to maintain three pieces of information: a vector containing the number of neighbors a node has a matrix containing an adjacency list of a node's neighbors another matrix for which entry in the adjacency list is which neighbor of that nodeTRANSCRIPT
Finding Grid Point Neighbors
Matt Craven
for:Dr. AnitescuMath 1110
Spring 2002
Finding Grid Neighbors
In order to solve some discretized partial differerential equations, we need to know the immediate neighbors of all the points in the domain.
First, we must choose an appropriate data structure.
Choosing a data structure
In our data structure, we need to maintain three pieces of information: a vector containing the number of neighbors a node has a matrix containing an adjacency list of a node's
neighbors another matrix for which entry in the adjacency list is
which neighbor of that node
How the data is stored in the structures
Since we only need to consider the points within our domain, we create a matrix containing the coordinates of our points within the domain.
[i,j] = find(a7);a10 = [i,j];
Storing the data in our structures
Now that we have a10, we can use a simple algorithm to find the neighbors of each point, and
store them in our structures.
nbfind.m[i,j] = size(a10);nb = zeros(i,8);nbor = zeros(i,8);
directions = [ 0, -deltaY; %North (1) -deltaX, -deltaY; %Northwest (2)
-deltaX, 0; %West (3) -deltaX, deltaY; %Southwest (4) 0, deltaY;
%South (5) deltaX, deltaY; %Southeast (6) deltaX, 0; %East (7) deltaX, -deltaY;]; %Northeast (8)
nbfind.m, continuedfor k = 1:i, x = a10(k,2); y = a10(k,1); number = 0;for dir = 1:8.
nx = x + directions(dir, 1);ny = y + directions(dir, 2);
if(ny > 0 & nx > 0) if(a7(ny,nx)) for c = 1:i, if(a10(c, 2) == nx) if(a10(c,1) == ny) n = c; break; end end end number = number + 1; nbnr(k,1) = number; nb(k,number) = n; nbor(k,number) = dir; end endendend
How nbfind works nbfind looks at each point in the domain, and checks each
of the 8 immediate directions to see if a neighbor is present.
If a neighboring point is in the domain, nbfind looks for its name in a10.
When the point's name is found, that name is inserted into the adjacency list, the direction of that point relative to the current point is placed in the order list, and the number of neighbors is increased by one.
How the data is stored in each structure
The number of neighbors (nrnb) vector simply holds how many points are adjacent to each point in the domain.
The neighbor adjacency list (nb) holds the name of the each neighbor, in the order it was found
The neighbor order (nbor) matrix holds a code describing in which direction the corresponding neighbor lies, with respect to the node in the center.
Example entry in our data structurenbnr(10,:) (number of neighbors)
4
9 31 30 29 0 0 0 0
1 6 7 8 0 0 0 0
nb(10,:) (neighbor adjacency list)
nbor(10,:) (neighbor order list) 10
9 29
30
31
Using this structure to solve the Poisson equation
Now that we have the data from our domain into these structures, we can use this information to solve some discretized differential equations.
To solve these equations, we need to know the immediate neighbors of every point.
As an example, we will now use this data to model a solution to the Poisson electrical potential equation
The Poisson Equation
The Poisson equation models electrical potential, and is given by the partial differential equation:
€
∂2u∂x2 +∂
2u∂y2 =1
with the boundary condition u = 5, where u(li) is the potential at point li.
Discretized Poisson Equation
€
∂2u∂x2
C
=uW +uE −2uC(deltaX2)
€
∂2u∂y2
C
=uN +uS −2uC(deltaY2)
At point C (with neighbors in the four cardinal directions, the discretized equation has two components:
Summing these, we get:
€
uNΔy2 + uS
Δy2 + uEΔx2 + uW
Δx2 −4uC =1
poisson.mw = zeros(1532, 1532);rhs = zeros(1532,1);deltaXsq = 1/deltaX^2;deltaYsq = 1/deltaY^2;for k = 1:1532 N = nb(k,find((nbor(k,:) == 1))); W = nb(k,find((nbor(k,:) == 3))); S = nb(k,find((nbor(k,:) == 5))); E = nb(k,find((nbor(k,:) == 7))); if(~isempty(N) & ~isempty(W) & ~isempty(S) & ~isempty(E)) w(k,N) = deltaXsq; w(k,S) = deltaXsq; w(k,E) = deltaYsq; w(k,W) = deltaYsq; w(k,k) = -2*(deltaYsq + deltaXsq); rhs(k,1) = 1; else w(k,k) = 1; rhs(k,1) = 5; endendm=max(a10(:,1));n=max(a10(:,2));u = w\rhs;w1=sparse(a10(:,1)/deltaY,a10(:,2)/deltaX,u);indxx=deltaX:deltaX:n;indxy=deltaY:deltaY:m;surf(indxy,indxx,w1');