I have to write a 2D Ising model simulation, where I don't neglect the effect of the distant neighbors, so I want to count the spins in a circle.
I've written a simple function which can get the elements of a grid, which are in a circle.
def countInCircle(g, x, y, r):
spinSum = 0
for R in range(0, r + 1, 1):
for i in range(0, g.shape[0], 1):
for j in range(0, g.shape[1], 1):
if ((i - x) ** 2 + (j - y) ** 2) == R:
spinSum = spinSum + g[i][j]
return spinSum
It works like a charm, but it cuts down some parts of the circle if it's ouf of the grid. How should I solve this for periodic boundary condition?
Thanks in advance!