program main
integer :: i,a,b,nn,ll,nst,H,x,y
integer, allocatable :: spin(:)
real(8) :: Z,T,k
real(8), allocatable :: W(:)
k=1 !!玻尔兹曼常数
Z=0
ll=2 !!长度
nn=ll*ll
nst=2**nn !!2的n次方个态,指数用双星号
T=2.00 !!温度
allocate (spin(0:nn-1))
allocate (W(0:nst-1))
do a=0,nst-1
H=0
do i=0,nn-1
if (btest(a,i)==true) then
spin(i)=1
end if
else if (btest(a,i)==false) then
spin(i)=-1
end if
enddo
do i=0,nn-1
x=mod(i,ll)
y=i/ll
s1=spin(mod(x+1,ll)+y*ll)
s2=spin(x+mod(y+1,ll)*ll)
s3=spin(mod(x-1+ll,ll)+y*ll)
s4=spin(x+mod(y-1+ll,ll)*ll)
H=(-1)*spin(i)*(s1+s2+s3+s4)+H
enddo
H=H/2
W(a)=exp(-H/k/T)
Z=Z+W(a)
enddo
print *, "H:", H
print *, W
end