2016-04-12 12 views
3

私は現在、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以前のシステムでは、障害が発生していて、これらのパッチは表示されません。

+0

私たちは、あなたが望む出力の例を示します。私たちはここでは物理学者のすべてではないので、期待される出力の点では「低温で高度に順序付けられた」ものを翻訳しなければなりません。あなたのスピンアレイが2.27 K以下の要素のおおよそ同じ値を持つことを期待していますか? –

+2

また、ホイールを改革しないで[代わりに、これらのオープンソースのいずれかのイジングモデルの実装を既に使用しています](https://github.com/search?l=Python&q=Ising&type=Repositories&utf8= %E2%9C%93)。 –

+0

Tが2.27に近づくにつれて、システムのランダム性が増すはずです。例えば、T = 0.1では、整列したスピンの大きなパッチ、すなわち-1sと1sのパッチが見られるはずである。 2.27以前のシステムでは、障害が発生していて、これらのパッチは表示されません。 –

答えて

4

システムサイズ、掃引回数、および平均マネタイズを含める場合、あなたの質問ははるかに意味があります。どのくらいの中間構成が注文され、どのくらいの順序が乱れていますか? MCはサンプリング技術であり、個々の構成は何も意味しないし、高Tで低温および秩序状態で不規則な状態になる可能性がある(そしてそうなる)。意味のあるアセンブリ特性(平均磁化)がある。

とにかく、コードには小さなもの、中程度のもの、本当に重大なものの3つのエラーがあります。

right = (x, y + 1 if y + 1 < (lattice - 1) else 0) 

は次のようになります:

right = (x, y + 1 if y + 1 < lattice else 0) 

またはより良い:

小さなものはfind_neighborsに隣人を探しているときに、行全体および列全体を無視しているということです

right = (x, (y + 1) % lattice) 

同じことがbottomに適用されます。

媒体一方がエネルギー差のあなたの計算は、2つの要因によりオフされることである:因子は、Jは結合定数であり、実際2*Jあり、したがってそこ-1手段を有する

def energy(spin_array, lattice, x ,y): 
    return -1 * spin_array[x, y] * sum(find_neighbors(spin_array, lattice, x, y)) 
      ^^ 

  1. あなたの臨界温度が半分になり、そしてさらに重要なこと...
  2. あなたは反強磁性スピンを持っています相互作用(J < 0)ですので、非常に低い温度でもあなたのための状態はありません。

最悪の間違いは、しかし、棄却サンプリングのためのrandom.randint()の使用である:

elif np.exp(-1 * e/temperature) > random.randint(0, 1): 
    spin_array[i, j] *= -1 

あなたがそうでなければ、遷移確率は常に50%になり、代わりにrandom.random()を使用する必要があります。

ここでは、0.1〜5の温度範囲で自動的にスイープするプログラムの変更を示します。で非常に安定している最初の構成が完全に磁壁の発生を防止するために1を命じている

Average mangentisation as a function of the reduced temperature

:0:

import numpy as np 
import random 


def init_spin_array(rows, cols): 
    return np.ones((rows, cols)) 


def find_neighbors(spin_array, lattice, x, y): 
    left = (x, y - 1) 
    right = (x, (y + 1) % lattice) 
    top = (x - 1, y) 
    bottom = ((x + 1) % lattice, 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 2 * spin_array[x, y] * sum(find_neighbors(spin_array, lattice, x, y)) 


def main(): 
    RELAX_SWEEPS = 50 
    lattice = eval(input("Enter lattice size: ")) 
    sweeps = eval(input("Enter the number of Monte Carlo Sweeps: ")) 
    for temperature in np.arange(0.1, 5.0, 0.1): 
     spin_array = init_spin_array(lattice, lattice) 
     # the Monte Carlo follows below 
     mag = np.zeros(sweeps + RELAX_SWEEPS) 
     for sweep in range(sweeps + RELAX_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.0 * e)/temperature) > random.random(): 
         spin_array[i, j] *= -1 
      mag[sweep] = abs(sum(sum(spin_array)))/(lattice ** 2) 
     print(temperature, sum(mag[RELAX_SWEEPS:])/sweeps) 


main() 

そして、20×20と100×100格子と100回のスイープのための結果低温。また、システムの温度を上げるために、30回の追加スイープが最初に実行されます(臨界温度に近いときには十分ではありませんが、メトロポリス - ヘイスティングスのアルゴリズムは、とにかく臨界減速を適切に処理できません)。

関連する問題