【发布时间】:2016-04-12 22:52:36
【问题描述】:
我目前正在使用 Python3 为 Ising 模型编写代码。我对编码还是很陌生。我有工作代码,但输出结果与预期不符,我似乎找不到错误。这是我的代码:
import numpy as np
import random
def init_spin_array(rows, cols):
return np.random.choice((-1, 1), size=(rows, cols))
def find_neighbors(spin_array, lattice, x, y):
left = (x , y - 1)
right = (x, y + 1 if y + 1 < (lattice - 1) else 0)
top = (x - 1, y)
bottom = (x + 1 if x + 1 < (lattice - 1) else 0, y)
return [spin_array[left[0], left[1]],
spin_array[right[0], right[1]],
spin_array[top[0], top[1]],
spin_array[bottom[0], bottom[1]]]
def energy(spin_array, lattice, x ,y):
return -1 * spin_array[x, y] * sum(find_neighbors(spin_array, lattice, x, y))
def main():
lattice = eval(input("Enter lattice size: "))
temperature = eval(input("Enter the temperature: "))
sweeps = eval(input("Enter the number of Monte Carlo Sweeps: "))
spin_array = init_spin_array(lattice, lattice)
print("Original System: \n", spin_array)
# the Monte Carlo follows below
for sweep in range(sweeps):
for i in range(lattice):
for j in range(lattice):
e = energy(spin_array, lattice, i, j)
if e <= 0:
spin_array[i, j] *= -1
elif np.exp(-1 * e/temperature) > random.randint(0, 1):
spin_array[i, j] *= -1
else:
continue
print("Modified System: \n", spin_array)
main()
我认为错误出在蒙特卡洛循环中,但我不确定。该系统应在低温下高度有序,并在超过 2.27 的临界温度时变得无序。换句话说,系统的随机性应该随着 T 接近 2.27 而增加。例如,在 T=.1 时,我们应该看到对齐的大片自旋,即 -1 和 1 的片。 2.27以后系统应该是乱的,我们应该看不到这些补丁。
【问题讨论】:
-
给我们一个你想要的输出的例子:) 我们不是这里的所有物理学家,所以你必须根据你的预期输出来翻译“低温下高度有序”的样子.您是否希望您的自旋数组在其低于 2.27 K 的每个元素中具有大致相同的值?
-
系统的随机性应该随着 T 接近 2.27 而增加。例如,在 T=.1 时,我们应该看到对齐的大片自旋,即 -1 和 1 的片。 2.27以后系统应该是无序的,我们应该看不到这些补丁。
-
好的。请编辑问题以包含该信息:)
-
if e <= 0:下方似乎存在缩进错误。我假设 if-elif-else 链都应该缩进,除了第一行。另外,当 if 和 elif 条件都失败时,spin_array[i, j]没有被分配/更改是否正确?
标签: python python-3.x physics montecarlo