python:二維正方形晶格Ising模型的 Monte Carlo 模擬

  • 研究5×5,10×10,20×20,40×40,80×80五種系統(tǒng)大小下,平均磁矩與溫度的關(guān)系,如圖1所示。模擬時(shí)先弛豫50步,再以后1000步做統(tǒng)計(jì)。考慮外磁場(chǎng)為0的情況,并且統(tǒng)計(jì)時(shí)對(duì)總磁矩取絕對(duì)值。

  • 從圖中可以發(fā)現(xiàn),隨著溫度的升高,二維Ising模型存在從鐵磁相到順磁相的轉(zhuǎn)變,臨界溫度近似為2.3,并且系統(tǒng)變大,相變點(diǎn)附近平均磁矩的變化越陡峭。

不同系統(tǒng)大小下平均磁矩隨溫度的變化

代碼:

import numpy as np
import matplotlib.pyplot as plt

def cal_delta_E(spin, i, j, Lattice, H, J):
    ## 計(jì)算翻轉(zhuǎn)前后系統(tǒng)能量的變化
    top = i - 1
    bottom = (i + 1) % Lattice
    left = j - 1
    right = (j + 1) % Lattice
    E_loc_former = -1.0 * spin[i,j] * (J * (spin[top,j] + spin[bottom,j] + spin[i,left] + spin[i,right]) +  H )
    E_loc_next = spin[i,j] * (J * (spin[top,j] + spin[bottom,j] + spin[i,left] + spin[i,right]) +  H )
    delta_E = E_loc_next - E_loc_former
    return delta_E 
## 實(shí)驗(yàn)
sweeps = 1000 
relax = 50
K = 1
J = 1
H = 0 # 無(wú)外加磁場(chǎng)
results_dic = {} 
for Lattice in [5, 10, 20, 40, 80]: 
    # 初始化
    spin = np.random.random((Lattice,Lattice))
    spin[np.where(spin >= 0.5)] = 1
    spin[np.where(spin < 0.5)] = -1
    results = []
    for T in np.arange(0.1, 4.0, 0.05):
        mag = []
        # Monte Calro 每步翻轉(zhuǎn)系統(tǒng)所有自旋
        for sweep in range(sweeps + relax):
            for i in range(Lattice):
                for j in range(Lattice):
                    delta_E = cal_delta_E(spin, i, j, Lattice, H, J)
                    if delta_E <= 0:
                        spin[i,j] = - spin[i,j]
                    # 幾率翻轉(zhuǎn)
                    elif np.exp((- delta_E)/(K * T)) > np.random.random():
                        spin[i,j] = - spin[i,j]
            if sweep % 100 == 0:
                print(sweep)
            M = abs(sum(sum(spin))) / (Lattice ** 2) # 平均磁矩正負(fù)等價(jià),故取絕對(duì)值
            mag.append(M)
        result = sum(mag[relax:]) / sweeps # 統(tǒng)計(jì)平均值
        results.append(result)
    results_dic[Lattice] = results

## 作圖
plt.figure(figsize=(12,9))
plt.subplot(231) 
plt.scatter(np.arange(0.1, 4.0, 0.05),results_dic[5],marker='o',c="",edgecolors='black',label='Lattice=5')
plt.grid(True)
plt.ylabel("M")
plt.legend()
plt.subplot(232) 
plt.scatter(np.arange(0.1, 4.0, 0.05),results_dic[10],marker='o',c="",edgecolors='blue',label='Lattice=10')
plt.grid(True)
plt.xlabel("T")
plt.legend()
plt.subplot(233) 
plt.scatter(np.arange(0.1, 4.0, 0.05),results_dic[20],marker='o',c="",edgecolors='red',label='Lattice=20')
plt.grid(True)
plt.legend()
plt.subplot(234) 
plt.scatter(np.arange(0.1, 4.0, 0.05),results_dic[40],marker='o',c="",edgecolors='green',label='Lattice=40')
plt.grid(True)
plt.legend()
plt.ylabel("M")
plt.subplot(235) 
plt.scatter(np.arange(0.1, 4.0, 0.05),results_dic[80],marker='o',c="",edgecolors='yellow',label='Lattice=80')
plt.grid(True)
plt.xlabel("T")
plt.legend()
plt.subplot(236) 
plt.scatter(np.arange(0.1, 4.0, 0.05),results_dic[5],marker='o',c="",edgecolors='black',label='Lattice=5')
plt.scatter(np.arange(0.1, 4.0, 0.05),results_dic[10],marker='o',c="",edgecolors='blue',label='Lattice=10')
plt.scatter(np.arange(0.1, 4.0, 0.05),results_dic[20],marker='o',c="",edgecolors='red',label='Lattice=20')
plt.scatter(np.arange(0.1, 4.0, 0.05),results_dic[40],marker='o',c="",edgecolors='green',label='Lattice=40')
plt.scatter(np.arange(0.1, 4.0, 0.05),results_dic[80],marker='o',c="",edgecolors='yellow',label='Lattice=80')
plt.grid(True)
plt.legend()
plt.savefig("T-Lattice.png")
plt.show()

##保存
f = open('results.txt','w')  
f.write(str(results_dic))  
f.close()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 《武志紅心理學(xué)課》學(xué)習(xí)筆記 命運(yùn)04 | 羅森塔爾效應(yīng) 關(guān)鍵概念: 羅森塔爾效應(yīng) 權(quán)威性謊言 皮格馬利翁效應(yīng) 皮格...
    泥龍蝦閱讀 338評(píng)論 0 0
  • 放到94年的這個(gè)日子,我的腿應(yīng)該有貓腿這么大了。 我的腿長(zhǎng)在我的身上,我也只能替他說(shuō)一句遇人不淑。 我試著用梁實(shí)秋...
    長(zhǎng)馬閱讀 430評(píng)論 0 9
  • 貨幣基金作為一種具有高安全性、高流動(dòng)性、穩(wěn)定收益的金融資產(chǎn),是活期賬戶一種絕佳替代品。然而,生活中,仍然有不少帥哥...
    山水一灣閱讀 378評(píng)論 2 2
  • 寶貝: 我必須得表?yè)P(yáng)你,你很乖很聽話,已經(jīng)給我寫了三封信。同時(shí),也提請(qǐng)你必須認(rèn)識(shí)到,無(wú)數(shù)實(shí)踐證明,聽媽媽的話總是沒(méi)...
    簡(jiǎn)之寧閱讀 658評(píng)論 14 14

友情鏈接更多精彩內(nèi)容