私は現在、Python3を使用してイジングモデルのコードを記述しています。私はまだかなり新しいコーディングです。私は作業コードを持っていますが、出力結果は予想通りではなく、エラーを見つけることができません。私のコードは次のとおりです:Pythonでのイジングモデル
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 = 0.1では、整列したスピンの大きなパッチ、すなわち-1sと1sのパッチが見られるはずである。 2.27以前のシステムでは、障害が発生していて、これらのパッチは表示されません。
私たちは、あなたが望む出力の例を示します。私たちはここでは物理学者のすべてではないので、期待される出力の点では「低温で高度に順序付けられた」ものを翻訳しなければなりません。あなたのスピンアレイが2.27 K以下の要素のおおよそ同じ値を持つことを期待していますか? –
また、ホイールを改革しないで[代わりに、これらのオープンソースのいずれかのイジングモデルの実装を既に使用しています](https://github.com/search?l=Python&q=Ising&type=Repositories&utf8= %E2%9C%93)。 –
Tが2.27に近づくにつれて、システムのランダム性が増すはずです。例えば、T = 0.1では、整列したスピンの大きなパッチ、すなわち-1sと1sのパッチが見られるはずである。 2.27以前のシステムでは、障害が発生していて、これらのパッチは表示されません。 –