2017-04-21 165 views
-2

我有一個for循環,在第一次迭代之後返回零。我把它打印出來,所以我知道它實際上循環着,但由於某種原因,它似乎並沒有在第一次迭代之後調用我的函數new_lattice。Python for循環在第一次迭代後停止工作

N=[4,8,16,20,25] 
for i,j in enumerate(N): 
    print(i) 
    init_lattice=np.ones((j,j)) 
    #new_lattice is a function that returns multiple lists 
    data=new_lattice(init_lattice,j) 
    print (data[1]) 

打印應該打印出由函數返回的列表中的一個,但除了第一次迭代列表中的所有元素都爲零。如果我用N =任何值調用循環外的函數,那麼元素不是零,所以它似乎是循環,這是問題。我有另一個Python文件具有完全相同的循環,但是這個工作,所以我不明白爲什麼不這樣做!

以下是完整的代碼,包括功能:

import numpy as np 
from numpy import random as rn 
import matplotlib.pyplot as plt 

temp1= np.arange(2.0, 3.0, 0.1) 
temp=enumerate(temp1) 
number_of_sweeps=200 
eqm_sweeps=50 

def new_lattice(lattice,L): 
    delta_E=np.zeros((L,L)) 
    mag=np.zeros(number_of_sweeps) 
    mag1=np.zeros(len(temp1)) 
    mag2=np.zeros(len(temp1)) 
    mag4=np.zeros(len(temp1)) 
    for n, T in temp: 
     for sweep in range(number_of_sweeps+eqm_sweeps): 
      for i in range(L): 
       for j in range(L): 
        Si=lattice[i,j] 
        sum_Sj=lattice[i,(j+1)%L]+lattice[(i+1)%L,j]+lattice[i,(j-1)%L]+lattice[(i-1)%L,j] 
        delta_E[i,j]=2*Si*sum_Sj 

        if delta_E[i,j] > 0.0 and rn.random() < np.exp(-1*delta_E[i,j]/(T)): 
         lattice[i,j] *= -1 
        elif delta_E[i,j] <= 0.0: 
         lattice[i,j] *= -1 
      if sweep>=eqm_sweeps: 
       mag[sweep-eqm_sweeps]=abs(np.sum(lattice)) 
       mag1[n]=np.sum(mag)/number_of_sweeps 
       mag2[n]=np.sum(mag**2)/((L**2)*number_of_sweeps) 
       mag4[n]=np.sum(mag**4)/((L**2)*number_of_sweeps) 

    return mag1, mag2,mag4,lattice 

該代碼使用Metropolis算法模擬伊辛模型。 輸出看起來像:

0 
[ 3323.37 3225.43 2912.865 2740.01 2392.66 2266.455 1964.165 
    1804.22 1595.68 1317.135] 
1 
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] 
2 
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] 
3 
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] 
4 
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] 

但我希望最後4只列出了有非零元素。

+3

我們沒有你的'new_lattice'功能,所以這不是一個可覈查的,完整的例子。請提供*足夠的*代碼來重現您的錯誤。 –

+1

和預期的輸出也會很好 – Guillaume

+1

您可以在'new_lattice'中添加一個'print'調用來查看它是否真正進入那裏。 – CristiFati

回答

1

問題是你正在枚舉函數和循環之外。

temp=enumerate(temp1) 
for n, T in temp: 

在j = 4的第一個函數調用之後,枚舉temp的尾氣並且「for n,temp in T」不再起作用。

在new_lattice函數內使用枚舉,以便在每個函數調用上生成一個枚舉對象。

for n, T in enumerate(temp1): 

輸出示例:

0 
(array([ 15.07, 14.17, 14.03, 14.19, 12.95, 11.76, 11.07, 10.71, 
     11.14, 11.01]), array([ 14.43625, 13.16625, 12.90875, 13.02875, 11.32125, 9.9925 , 
     8.89625, 8.52875, 8.89 , 8.88625]), array([ 3479.785, 3070.425, 2948.195, 2955.275, 2419.065, 2109.61 , 
     1747.325, 1640.615, 1700.32 , 1730.785]), array([[ 1., -1., 1., -1.], 
     [ 1., -1., -1., 1.], 
     [-1., -1., -1., -1.], 
     [-1., 1., -1., 1.]])) 
1 
(array([ 57.34, 54.44, 52.75, 42.99, 43.95, 35.48, 31.77, 31.81, 
     25.06, 22.71]), array([ 52.979375 , 48.47  , 45.4803125, 33.8340625, 33.2053125, 
     24.343125 , 20.0590625, 20.0928125, 13.17125 , 11.6246875]), array([ 190270.6775 , 166042.28 , 146420.65625, 100278.72625, 
     87279.49125, 58296.1425 , 41876.73125, 42170.46125, 
     20914.28 , 18993.91375]), array([[ 1., 1., 1., 1., 1., 1., 1., 1.], 
     [ 1., 1., 1., 1., 1., 1., -1., 1.], 
     [-1., -1., 1., 1., -1., 1., -1., 1.], 
     [-1., -1., 1., 1., -1., -1., -1., 1.], 
     [-1., -1., 1., 1., 1., 1., -1., -1.], 
     [-1., -1., 1., 1., 1., 1., 1., -1.], 
     [-1., 1., 1., 1., -1., -1., -1., -1.], 
     [ 1., 1., 1., 1., 1., -1., -1., 1.]])) 
...... 
......