我正在编写一些代码,以尝试使用Metropolis算法来近似量子力学系统的基态能量和其他特性。因此,首先我定义了一个delta_action函数以及许多参数,如下所示:
import numpy.random as rnd;
import numpy as np;
import math;
#Definition of variables
EL = 10
N = 10 #number of lattice points
A = EL/N #lattice constant
M = 1000000 #number of iterations
ME = 500 #burn in iterations
MA = 5 #every MA'th configuration is measured
BIN = 40 #number of bins for the wavefunction
INTERV = 2 #interval for binning [-INTERV,INTERV]
MASS = 1.0
MU = .5 #coefficient of q**2
LAMBDA = 0.0 #coefficient of q**4
DELTA = 0.5 #change of lattice variable paramater
massl = MASS/A
lambdal = A*LAMBDA
muleff = MASS/A + A*MU
reject = 0
translate = BIN/2.0
stretch = 0.5*BIN/INTERV
q = np.zeros(N)
def del_action(x,y,xs):
return (y-x)*((y+x)*(muleff+lambdal*(y*y+x*x))-massl*xs)
现在我有以下内容:
def metropolis(q):
for i in range(0,MA):
for j in range(0,N-1):
jp = ((j+1)%N)
jm = ((j+N-1)%N)
qnew = q[j] + DELTA*(1-2*rnd.uniform(0,1))
dS = del_action(qnew,q[j],q[jp]+q[jm])
if dS<0:
q[j] = qnew
else:
rn = rnd.uniform(0,1)
if rn <= math.exp(-dS):
q[j] = qnew
return
因此,我遇到的问题是j上的循环基本上每次都停留在0,1,2上,而没有一直前进到N-1。通过在print(j,jp,jm,qnew,dS)
上方添加if dS < 0:
并运行,我得到以下结果:
0 1 9 -0.10299220871627934 -0.01591109258438647
1 2 0 0.4040577654073293 -0.2865088183872116
2 3 1 0.22295515394986587 0.015523260281817304
0 1 9 0.07350728912179683 0.07912210294530213
0 1 9 0.4117795868978783 -0.10955711120902165
1 2 0 0.06283878213693184 0.022387404949800852
0 1 9 0.5864083527452271 -0.2504950330034522
1 2 0 0.16584582581090546 0.048035953984589264
0 1 9 0.20274281147744633 0.3905258343186439
0 1 9 -0.1002558575204866 -0.0036709485216505355
1 2 0 -0.03319832019662894 0.01518148753902383
0 1 9 -0.42958241606820047 -0.25080163430181573
1 2 0 -0.2491635871862874 -0.04684623524276284
2 3 1 0.21831753974485746 0.00422520336014744
0 1 9 -0.533602476871921 -0.12436781520409396
1 2 0 -0.49950732570395673 -0.2022080030198727
2 3 1 0.4907960348437076 -0.425932303885753
3 4 2 0.24493839231060366 0.030222567685481976
0 1 9 -0.20701571624479698 0.19968166541608104
其中的列分别是j,jp,jm,qnew,dS。 jp和jm指的是delta_action函数所需的每个晶格值的相邻点。
所以,我的问题是:为什么j停留在0左右,正如您从上方看到的那样,它最终以0结束的次数多于零,因此,经过数千次迭代之后,可能会有10个j = 0的块。
任何帮助将不胜感激。我还想补充一点,我对Python和编码是完全陌生的,我已经进入数学科学学位课程的最后一年,但是到目前为止,我完全忽略了我所学课程提供的任何计算机编程或编码方面的知识,现在为此付出代价。谢谢!