I need to calculate the hamiltonian of a grid of particles given their spin. I am given the grid of particles with their spin as well as the values of J and B. The way to find that value is depicted below...
We consider the 2D Ising model, a grid of particles with periodic boundary conditions, such that each particle is spin up +1, or spin down -1. The Hamiltonian (total energy) of this system is $$H = -J\sum_{\langle a,b \rangle} s_a s_b - B \sum_a s_a$$ where $\langle a, b\rangle$ are the pairs of all neighboring particles, $B$ is the external magnetic field, and $J$ the exchange coupling (strength of interaction between neighbors) is positive. Note that both $a$ and $b$ each represent a pair of indices (row,column) for a single particle in the 2D grid of particles.
so you can imagine a grid that looks something like
1 1 1
-1 -1 1
1 1 -1
where the above grid is just numbers I randomly added. My way of calculating it was
sumLeft = 0 // sum of SaSb
sumRight = 0 // sum of Sa
for each particle in the grid:
sumLeft = sumLeft + (spin of A * the sum of the spin of A's neighbors)
sumRight = sumRight + spin of A
Hamiltonian = -J*sumLeft - B*sumRight
but the answer I am getting is wrong. Can anyone point me in the right direction? I think I am just misunderstanding the formula provided to me.