I am writing code for the following equation with fixed boundary condition on a 2 dimensional lattice of \$L\times L\$ sites:
$$\begin{align} x_{i+1} =&\ (1-\varepsilon)r\, x_i (1-x_i) + \\ &\ 0.25\varepsilon\left((r\,x_{i-1}(1-x_{i-1}) + r\,x_{i+1}(1-x_{i+1}) + r\,x_{i-L}(1-x_{i-L}) + r\,x_{i+L}(1-x_{i+L})\right) \end{align}$$
Fixed boundary condition means for end sites there are no neighboring sites beyond boundary.
Is there a simpler or more sophisticated way to write following code for the above equation with fixed boundary condition ?
def CML2d(x):
eps = 0.3
r = 3.9
xn = np.zeros(N+1, float)
for i in range(1, N+1):
if i>L and i<=(L-1)*L:
if i%L==1:
xl, xr = 0., x[i+1]
xu, xd = x[i-L], x[i+L]
elif i%L==0:
xl, xr = x[i-1], 0.
xu, xd = x[i-L], x[i+L]
else:
xl, xr = x[i-1], x[i+1]
xu, xd = x[i-L], x[i+L]
elif i>1 and i<L:
xl, xr = x[i-1], x[i+1]
xu, xd = 0., x[i+L]
elif i>(L-1)*L+1 and i<L*L:
xl, xr = x[i-1], x[i+1]
xu, xd = x[i-L], 0.
elif i==1:
xl, xr = 0., x[i+1]
xu, xd = 0., x[i+L]
elif i==L:
xl, xr = x[i-1], 0.
xu, xd = 0., x[i+L]
elif i==(L-1)*L+1:
xl, xr = 0., x[i+1]
xu, xd = x[i-L], 0.
elif i==L*L:
xl, xr = x[i-1], 0.
xu, xd = x[i-L], 0.
xn[i] = (1-eps)*r*x[i]*(1-x[i]) + 0.25*eps*( r*xl*(1-xl) + r*xr*(1-xr) + r*xu*(1-xu) + r*xd*(1-xd) )
return xn
L = 10 #side of 2d lattice
N = L*L #number of sites in 2d lattice
x0 = numpy.random.uniform(0.1, 0.9, N+1) #initial values for x
xf = [] # store iterate x
x = x0
for nt in np.arange(0.005, 50.005, 0.005):
x = CML2d(x)
xf.append(x)
numpy
. It looks like the code would work just as well with lists. And the arrays are 1d, despite this being a 2d problem (shape(N,)
rather than(L,L)
). \$\endgroup\$x
andxn
are 101 long - you ignore the first elements. It may make flat indexing a bit easier but it doesn't help with thinking in array terms. \$\endgroup\$