私は最初の反復の後にゼロを返す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])
プリントは、関数によって返されたリストの1をプリントアウトする必要がありますが、最初の反復を除いて、リストのすべての要素がゼロです。ループ外の関数をN =任意の値で呼び出すと、要素はゼロではないので、問題のループのようです。私はまったく同じループを持っている別のpythonファイルを持っていますが、その1つが動作するので、なぜこれが理解できないのですか?ここで
は、機能を含む完全なコードです:
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
コードは、イジングモデルをシミュレートするために、メトロポリスアルゴリズムを使用しています。 出力は次のようになります。
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つのリストが非ゼロ要素を持つことが期待されます。
私たちはあなたの 'new_lattice'機能を持っていないので、これは検証可能な、完全な例ではありません。エラーを再現するのに十分な*コードを提供してください。 –
と期待される出力も良いでしょう – Guillaume
'new_lattice'に' print'呼び出しを追加して実際にそこに入るかどうかを調べることができます。 – CristiFati