nania.projects.toolkit

| index | overview | projects | people | examples | links |

Generating regular lattice cell neighbour lists

This page describes how to generate adjacent neighbour lists for regular square and hexagonal lattices where each cell has an x and y position (i,j) and a unique cell identifier, n. The lattice dimension, Nx, refers to the number of cells along each axis. Using this information, the unique cell identifier is calculated using: n = i + j *Nx. Cosequently, n runs from 0 to Nx*Nx-1.

Square lattice

The case of the square lattice is rather trivial. Each cell interacts with each of its 4 nearest neighbours. These neighbours are given by i+/-1 and j+/-1. The exeptions to this are when these coordinates run across a boundary. The choise of neighbour now becomes dependent upon the boundary conditions applied. For the open boundary case, any neighbours which have run off the boundary are set to n=-1. For the periodic case they are looped back onto the other end of the row or column that has over run, see the summary table below.
Neighbour number
Coordinates
Exception
Periodic BC
Open BC
0 (i-1,j)i-1<0 (Nx-1,j)n=-1
1 (i+1,j)i+1=Nx (0,j)n=-1
2 (i,j-1)j-1<0 (i,Nx-1)n=-1
3 (i,j+1)j+1=Nx (i,0)n=-1
Hegaxongal lattice cell numbering

Hexagonal lattice

The hexagonal lattice is more complicated as it has an irregular neighbour pattern that alternates between columns, see inset figure. The row and column numbers are indicated along the i and j axes. In order to distinguish between even and odd columns we test whether the remainder of i/2 is zero using: if(i % 2 == 0){...} else{...}.
The boundary cells are are coloured with either an orange or red outline. The orange exceptions are delt with in the table below, these simply wrap the boundary, similar to the square lattice. Since the lattice also searches diagonally, the two red elements are also linked.

Neighbour number
Even cell neighbours
Odd cell neighbours
Coordinates
Exception
Periodic BC
Open BC
Coordinates
Exception
Periodic BC
Open BC
0 n+1i+1=Nx(0,j)n=-1 n+1i+1=Nx(0,j)n=-1
1 n-1i-1<0(Nx-1,j)n=-1 n-1i-1<0(Nx-1,j)n=-1
2 n+Nxj+1=Nx(i,0)n=-1 n-Nxj-1<0(i,Nx-1)n=-1
3 n-Nxj-1<0(i,Nx-1)n=-1n+Nxj+1=Nx(i,0)n=-1
4 n-Nx+1j-1<0(i+1,Nx-1)n=-1n+Nx+1j+1=Nx(i+1,0)n=-1
5 n-Nx-1j-1<0(i-1,Nx-1)n=-1n+Nx-1j+1=Nx(i-1,0)n=-1