-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLL.py
315 lines (299 loc) · 11.9 KB
/
LL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"""
Basic Python Lebwohl-Lasher code. Based on the paper
P.A. Lebwohl and G. Lasher, Phys. Rev. A, 6, 426-429 (1972).
This version in 2D.
Run at the command line by typing:
python LebwohlLasher.py <ITERATIONS> <SIZE> <TEMPERATURE> <PLOTFLAG>
where:
ITERATIONS = number of Monte Carlo steps, where 1MCS is when each cell
has attempted a change once on average (i.e. SIZE*SIZE attempts)
SIZE = side length of square lattice
TEMPERATURE = reduced temperature in range 0.0 - 2.0.
PLOTFLAG = 0 for no plot, 1 for energy plot and 2 for angle plot.
The initial configuration is set at random. The boundaries
are periodic throughout the simulation. During the
time-stepping, an array containing two domains is used; these
domains alternate between old data and new data.
SH 16-Oct-23
"""
import sys
import time
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
#=======================================================================
def initdat(nmax):
"""
Arguments:
nmax (int) = size of lattice to create (nmax,nmax).
Description:
Function to create and initialise the main data array that holds
the lattice. Will return a square lattice (size nmax x nmax)
initialised with random orientations in the range [0,2pi].
Returns:
arr (float(nmax,nmax)) = array to hold lattice.
"""
arr = np.random.random_sample((nmax,nmax))*2.0*np.pi
return arr
#=======================================================================
def plotdat(arr,pflag,nmax):
"""
Arguments:
arr (float(nmax,nmax)) = array that contains lattice data;
pflag (int) = parameter to control plotting;
nmax (int) = side length of square lattice.
Description:
Function to make a pretty plot of the data array. Makes use of the
quiver plot style in matplotlib. Use pflag to control style:
pflag = 0 for no plot (for scripted operation);
pflag = 1 for energy plot;
pflag = 2 for angles plot;
pflag = 3 for black plot.
The angles plot uses a cyclic color map representing the range from
0 to pi. The energy plot is normalised to the energy range of the
current frame.
Returns:
NULL
"""
if pflag==0:
return
u = np.cos(arr)
v = np.sin(arr)
x = np.arange(nmax)
y = np.arange(nmax)
cols = np.zeros((nmax,nmax))
if pflag==1: # colour the arrows according to energy
mpl.rc('image', cmap='rainbow')
for i in range(nmax):
for j in range(nmax):
cols[i,j] = one_energy(arr,i,j,nmax)
norm = plt.Normalize(cols.min(), cols.max())
elif pflag==2: # colour the arrows according to angle
mpl.rc('image', cmap='hsv')
cols = arr%np.pi
norm = plt.Normalize(vmin=0, vmax=np.pi)
else:
mpl.rc('image', cmap='gist_gray')
cols = np.zeros_like(arr)
norm = plt.Normalize(vmin=0, vmax=1)
quiveropts = dict(headlength=0,pivot='middle',headwidth=1,scale=1.1*nmax)
fig, ax = plt.subplots()
q = ax.quiver(x, y, u, v, cols,norm=norm, **quiveropts)
ax.set_aspect('equal')
plt.show()
#=======================================================================
def savedat(arr,nsteps,Ts,runtime,ratio,energy,order,nmax):
"""
Arguments:
arr (float(nmax,nmax)) = array that contains lattice data;
nsteps (int) = number of Monte Carlo steps (MCS) performed;
Ts (float) = reduced temperature (range 0 to 2);
ratio (float(nsteps)) = array of acceptance ratios per MCS;
energy (float(nsteps)) = array of reduced energies per MCS;
order (float(nsteps)) = array of order parameters per MCS;
nmax (int) = side length of square lattice to simulated.
Description:
Function to save the energy, order and acceptance ratio
per Monte Carlo step to text file. Also saves run data in the
header. Filenames are generated automatically based on
date and time at beginning of execution.
Returns:
NULL
"""
# Create filename based on current date and time.
current_datetime = datetime.datetime.now().strftime("%a-%d-%b-%Y-at-%I-%M-%S%p")
filename = "logs/LL-Output-{:s}.txt".format(current_datetime)
FileOut = open(filename,"w")
# Write a header with run parameters
print("#=====================================================",file=FileOut)
print("# File created: {:s}".format(current_datetime),file=FileOut)
print("# Size of lattice: {:d}x{:d}".format(nmax,nmax),file=FileOut)
print("# Number of MC steps: {:d}".format(nsteps),file=FileOut)
print("# Reduced temperature: {:5.3f}".format(Ts),file=FileOut)
print("# Run time (s): {:8.6f}".format(runtime),file=FileOut)
print("#=====================================================",file=FileOut)
print("# MC step: Ratio: Energy: Order:",file=FileOut)
print("#=====================================================",file=FileOut)
# Write the columns of data
for i in range(nsteps+1):
print(" {:05d} {:6.4f} {:12.4f} {:6.4f} ".format(i,ratio[i],energy[i],order[i]),file=FileOut)
FileOut.close()
#=======================================================================
def one_energy(arr,ix,iy,nmax):
"""
Arguments:
arr (float(nmax,nmax)) = array that contains lattice data;
ix (int) = x lattice coordinate of cell;
iy (int) = y lattice coordinate of cell;
nmax (int) = side length of square lattice.
Description:
Function that computes the energy of a single cell of the
lattice taking into account periodic boundaries. Working with
reduced energy (U/epsilon), equivalent to setting epsilon=1 in
equation (1) in the project notes.
Returns:
en (float) = reduced energy of cell.
"""
en = 0.0
ixp = (ix+1)%nmax # These are the coordinates
ixm = (ix-1)%nmax # of the neighbours
iyp = (iy+1)%nmax # with wraparound
iym = (iy-1)%nmax #
# Add together the 4 neighbour contributions
# to the energy
ang = arr[ix,iy]-arr[ixp,iy]
en += 0.5*(1.0 - 3.0*np.cos(ang)**2)
ang = arr[ix,iy]-arr[ixm,iy]
en += 0.5*(1.0 - 3.0*np.cos(ang)**2)
ang = arr[ix,iy]-arr[ix,iyp]
en += 0.5*(1.0 - 3.0*np.cos(ang)**2)
ang = arr[ix,iy]-arr[ix,iym]
en += 0.5*(1.0 - 3.0*np.cos(ang)**2)
return en
#=======================================================================
def all_energy(arr,nmax):
"""
Arguments:
arr (float(nmax,nmax)) = array that contains lattice data;
nmax (int) = side length of square lattice.
Description:
Function to compute the energy of the entire lattice. Output
is in reduced units (U/epsilon).
Returns:
enall (float) = reduced energy of lattice.
"""
enall = 0.0
for i in range(nmax):
for j in range(nmax):
enall += one_energy(arr,i,j,nmax)
return enall
#=======================================================================
def get_order(arr,nmax):
"""
Arguments:
arr (float(nmax,nmax)) = array that contains lattice data;
nmax (int) = side length of square lattice.
Description:
Function to calculate the order parameter of a lattice
using the Q tensor approach, as in equation (3) of the
project notes. Function returns S_lattice = max(eigenvalues(Q_ab)).
Returns:
max(eigenvalues(Qab)) (float) = order parameter for lattice.
"""
Qab = np.zeros((3,3))
delta = np.eye(3,3)
#
# Generate a 3D unit vector for each cell (i,j) and
# put it in a (3,i,j) array.
#
lab = np.vstack((np.cos(arr),np.sin(arr),np.zeros_like(arr))).reshape(3,nmax,nmax)
for a in range(3):
for b in range(3):
for i in range(nmax):
for j in range(nmax):
Qab[a,b] += 3*lab[a,i,j]*lab[b,i,j] - delta[a,b]
Qab = Qab/(2*nmax*nmax)
eigenvalues,eigenvectors = np.linalg.eig(Qab)
return eigenvalues.max()
#=======================================================================
def MC_step(arr,Ts,nmax):
"""
Arguments:
arr (float(nmax,nmax)) = array that contains lattice data;
Ts (float) = reduced temperature (range 0 to 2);
nmax (int) = side length of square lattice.
Description:
Function to perform one MC step, which consists of an average
of 1 attempted change per lattice site. Working with reduced
temperature Ts = kT/epsilon. Function returns the acceptance
ratio for information. This is the fraction of attempted changes
that are successful. Generally aim to keep this around 0.5 for
efficient simulation.
Returns:
accept/(nmax**2) (float) = acceptance ratio for current MCS.
"""
#
# Pre-compute some random numbers. This is faster than
# using lots of individual calls. "scale" sets the width
# of the distribution for the angle changes - increases
# with temperature.
scale=0.1+Ts
accept = 0
xran = np.random.randint(0,high=nmax, size=(nmax,nmax))
yran = np.random.randint(0,high=nmax, size=(nmax,nmax))
aran = np.random.normal(scale=scale, size=(nmax,nmax))
for i in range(nmax):
for j in range(nmax):
ix = xran[i,j]
iy = yran[i,j]
ang = aran[i,j]
en0 = one_energy(arr,ix,iy,nmax)
arr[ix,iy] += ang
en1 = one_energy(arr,ix,iy,nmax)
if en1<=en0:
accept += 1
else:
# Now apply the Monte Carlo test - compare
# exp( -(E_new - E_old) / T* ) >= rand(0,1)
boltz = np.exp( -(en1 - en0) / Ts )
if boltz >= np.random.uniform(0.0,1.0):
accept += 1
else:
arr[ix,iy] -= ang
return accept/(nmax*nmax)
#=======================================================================
def main(program, nsteps, nmax, temp, pflag):
"""
Arguments:
program (string) = the name of the program;
nsteps (int) = number of Monte Carlo steps (MCS) to perform;
nmax (int) = side length of square lattice to simulate;
temp (float) = reduced temperature (range 0 to 2);
pflag (int) = a flag to control plotting.
Description:
This is the main function running the Lebwohl-Lasher simulation.
Returns:
NULL
"""
np.random.seed(42)
# Create and initialise lattice
lattice = initdat(nmax)
# Plot initial frame of lattice
plotdat(lattice,pflag,nmax)
# Create arrays to store energy, acceptance ratio and order parameter
energy = np.zeros(nsteps+1,dtype=np.dtype)
ratio = np.zeros(nsteps+1,dtype=np.dtype)
order = np.zeros(nsteps+1,dtype=np.dtype)
# Set initial values in arrays
energy[0] = all_energy(lattice,nmax)
ratio[0] = 0.5 # ideal value
order[0] = get_order(lattice,nmax)
# Begin doing and timing some MC steps.
initial = time.time()
for it in range(1,nsteps+1):
ratio[it] = MC_step(lattice,temp,nmax)
energy[it] = all_energy(lattice,nmax)
order[it] = get_order(lattice,nmax)
final = time.time()
runtime = final-initial
# Final outputs
print("{}: Size: {:d}, Steps: {:d}, T*: {:5.3f}: Order: {:5.3f}, Time: {:8.6f} s".format(program, nmax,nsteps,temp,order[nsteps-1],runtime))
# Plot final frame of lattice and generate output file
savedat(lattice,nsteps,temp,runtime,ratio,energy,order,nmax)
plotdat(lattice,pflag,nmax)
#=======================================================================
# Main part of program, getting command line arguments and calling
# main simulation function.
#
if __name__ == '__main__':
if int(len(sys.argv)) == 5:
PROGNAME = sys.argv[0]
ITERATIONS = int(sys.argv[1])
SIZE = int(sys.argv[2])
TEMPERATURE = float(sys.argv[3])
PLOTFLAG = int(sys.argv[4])
main(PROGNAME, ITERATIONS, SIZE, TEMPERATURE, PLOTFLAG)
else:
print("Usage: python {} <ITERATIONS> <SIZE> <TEMPERATURE> <PLOTFLAG>".format(sys.argv[0]))
#=======================================================================