Lets say I use an 2D/3D numpy array to model a box consisting of cells. The cells are labeled by increasing numbers starting from 0. Until here, this could be done by
box = np.arange(np.prod(n_cells))
box = box.reshape(n_cells)
where n_cells
is a np.array which stores the number of cells the box should have in each dimensions, basically the dimensions of the box counted in cells.
Now, the hard part for me is to find an intelligent way to find the neighbors for each cell. My goal is to set up an array or a list that contains the neighbors to each cell - ideally lets look at a small 2D example.
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
Here, without periodic boundary conditions the neighbors are
0: 1, 4, 5
1: 0, 2, 4, 5, 6
2: 1, 5, 6, 7, 3
...
But I would like to have it with periodic boundary conditions, such that
0: 1, 4, 5, 3, 7, 12, 13, 15
1: 0, 2, 4, 5, 6, 12, 13, 14
This way, every element has 8 neighbors in 2D. Ideally, I would like to be able do create such a list/array for any dimensions, but I am especially interested in 2D/3D if a generalized solution is not available.
Another issue, I will have to adress is that this way, I count all the pairs twice, i.e. 0 is a neighbor of 1 and 1 is a neighbor of 0. This is something, I need to get rid of as well but it is not the main problem.