我正在尝试在 Barabasi-Albert 网络中模拟 Ising 相变,并尝试复制一些可观察量的结果,例如在 Ising 网格模拟中观察到的磁化和能量。但是,我在解释我的结果时遇到了麻烦:不确定物理是错误的还是实现中存在错误。这是一个最小的工作示例:
import numpy as np
import networkx as nx
import random
import math
## sim params
# coupling constant
J = 1.0 # ferromagnetic
# temperature range, in units of J/kT
t0 = 1.0
tn = 10.0
nt = 10.
T = np.linspace(t0, tn, nt)
# mc steps
steps = 1000
# generate BA network, 200 nodes with preferential attachment to 3rd node
G = nx.barabasi_albert_graph(200, 3)
# convert csr matrix to adjacency matrix, a_{ij}
adj_matrix = nx.adjacency_matrix(G)
top = adj_matrix.todense()
N = len(top)
# initialize spins in the network, ferromagnetic
def init(N):
return np.ones(N)
# calculate net magnetization
def netmag(state):
return np.sum(state)
# calculate net energy, E = \sum J *a_{ij} *s_i *s_j
def netenergy(N, state):
en = 0.
for i in range(N):
for j in range(N):
en += (-J)* top[i,j]*state[i]*state[j]
return en
# random sampling, metropolis local update
def montecarlo(state, N, beta, top):
# initialize difference in energy between E_{old} and E_{new}
delE = []
# pick a random source node
rsnode = np.random.randint(0,N)
# get the spin of this node
s2 = state[rsnode]
# calculate energy by summing up its interaction and append to delE
for tnode in range(N):
s1 = state[tnode]
delE.append(J * top[tnode, rsnode] *state[tnode]* state[rsnode])
# calculate probability of a flip
prob = math.exp(-np.sum(delE)*beta)
# if this probability is greater than rand[0,1] drawn from an uniform distribution, accept it
# else retain current state
if prob> random.random():
s2 *= -1
state[rsnode] = s2
return state
def simulate(N, top):
# initialize arrays for observables
magnetization = []
energy = []
specificheat = []
susceptibility = []
for count, t in enumerate(T):
# some temporary variables
e0 = m0 = e1 = m1 = 0.
print 't=', t
# initialize spin vector
state = init(N)
for i in range(steps):
montecarlo(state, N, 1/t, top)
mag = netmag(state)
ene = netenergy(N, state)
e0 = e0 + ene
m0 = m0 + mag
e1 = e0 + ene * ene
m1 = m0 + mag * mag
# calculate thermodynamic variables and append to initialized arrays
energy.append(e0/( steps * N))
magnetization.append( m0 / ( steps * N))
specificheat.append( e1/steps - e0*e0/(steps*steps) /(N* t * t))
susceptibility.append( m1/steps - m0*m0/(steps*steps) /(N* t *t))
print energy, magnetization, specificheat, susceptibility
plt.figure(1)
plt.plot(T, np.abs(magnetization), '-ko' )
plt.xlabel('Temperature (kT)')
plt.ylabel('Average Magnetization per spin')
plt.figure(2)
plt.plot(T, energy, '-ko' )
plt.xlabel('Temperature (kT)')
plt.ylabel('Average energy')
plt.figure(3)
plt.plot(T, specificheat, '-ko' )
plt.xlabel('Temperature (kT)')
plt.ylabel('Specific Heat')
plt.figure(4)
plt.plot(T, susceptibility, '-ko' )
plt.xlabel('Temperature (kT)')
plt.ylabel('Susceptibility')
simulate(N, top)
观察结果:
我已经尝试对代码进行大量注释,以防我忽略了某些内容,请询问。
问题:
- 磁化趋势是否正确?磁化强度随温度升高而降低,但不能确定相变的临界温度。
- 随着温度的升高,能量接近于零,这似乎与在伊辛网格中观察到的一致。为什么我得到负比热值?
- 如何选择蒙特卡罗步数?这仅仅是基于网络节点数量的命中和试验吗?