def test_diff_2d(self): h = 0 # np.random.normal() j = np.random.normal() def get_energy(spins, h, j): return -np.sum(j*spins*( np.roll(spins, 1, axis=0) + np.roll(spins, -1, axis=0) + np.roll(spins, 1, axis=1) + np.roll(spins, -1, axis=1)) ) - h * np.sum(spins) shape = (3, 3) model = IsingModel(np.product(shape)) model.import_2d01(h, j, shape) model.random_state() # model.spins = np.repeat([True], np.product(shape)) for i in range(model.numspin): print(model.spins[i]) state = np.copy(model.spins) state[i] = True e1 = get_energy(state.reshape(shape), h, j) state[i] = False e0 = get_energy(state.reshape(shape), h, j) self.assertAlmostEqual(e0 - e1, model.energydiff(model.spins, i))
def test_diff_mf(self): h = np.random.normal() j = np.random.normal() model = IsingModel(2) model.import_uniform01(h, j) spins = np.random.choice([True, False], size=2) spin = np.random.choice([0, 1]) spins[spin] = True pos_energy = model.hamiltonian(spins) spins[spin] = False neg_energy = model.hamiltonian(spins) p = neg_energy - pos_energy self.assertAlmostEqual(p, model.energydiff(spins, spin))
def test_diff_full(self): h = np.random.normal(size=2) j = np.random.normal() j_matrix = [[0, j], [j, 0]] model = IsingModel(2) model.import_ising01(h, j_matrix) spins = np.random.choice([True, False], size=2) spin = np.random.choice([0, 1]) spins[spin] = True pos_energy = model.hamiltonian(spins) spins[spin] = False neg_energy = model.hamiltonian(spins) p = neg_energy - pos_energy self.assertAlmostEqual(p, model.energydiff(spins, spin))