-
Notifications
You must be signed in to change notification settings - Fork 104
/
md_lj_ll_module.py
357 lines (290 loc) · 19.9 KB
/
md_lj_ll_module.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env python3
# md_lj_ll_module.py
#------------------------------------------------------------------------------------------------#
# This software was written in 2016/17 #
# by Michael P. Allen <[email protected]>/<[email protected]> #
# and Dominic J. Tildesley <[email protected]> ("the authors"), #
# to accompany the book "Computer Simulation of Liquids", second edition, 2017 ("the text"), #
# published by Oxford University Press ("the publishers"). #
# #
# LICENCE #
# Creative Commons CC0 Public Domain Dedication. #
# To the extent possible under law, the authors have dedicated all copyright and related #
# and neighboring rights to this software to the PUBLIC domain worldwide. #
# This software is distributed without any warranty. #
# You should have received a copy of the CC0 Public Domain Dedication along with this software. #
# If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. #
# #
# DISCLAIMER #
# The authors and publishers make no warranties about the software, and disclaim liability #
# for all uses of the software, to the fullest extent permitted by applicable law. #
# The authors and publishers do not recommend use of this software for any purpose. #
# It is made freely available, solely to clarify points made in the text. When using or citing #
# the software, you should not imply endorsement by the authors or publishers. #
#------------------------------------------------------------------------------------------------#
"""Force routine for MD simulation, LJ atoms, using neighbour lists."""
fast = True # Change this to replace NumPy force evaluation with slower Python
class PotentialType:
"""A composite variable for interactions."""
def __init__(self, cut, pot, vir, lap, ovr):
self.cut = cut # the potential energy cut (but not shifted) at r_cut
self.pot = pot # the potential energy cut-and-shifted at r_cut
self.vir = vir # the virial
self.lap = lap # the Laplacian
self.ovr = ovr # a flag indicating overlap (i.e. pot too high to use)
def __add__(self, other):
cut = self.cut + other.cut
pot = self.pot + other.pot
vir = self.vir + other.vir
lap = self.lap + other.lap
ovr = self.ovr or other.ovr
return PotentialType(cut,pot,vir,lap,ovr)
def introduction():
"""Prints out introductory statements at start of run."""
print('Lennard-Jones potential')
print('Cut-and-shifted version for dynamics')
print('Cut (but not shifted) version also calculated')
print('Diameter, sigma = 1')
print('Well depth, epsilon = 1')
if fast:
print('Fast NumPy force routine')
else:
print('Slow Python force routine')
print('Uses neighbour lists')
def conclusion():
"""Prints out concluding statements at end of run."""
print('Program ends')
def force ( box, r_cut, r ):
"""Takes in box, cutoff range, and coordinate array, and calculates forces and potentials etc."""
import numpy as np
from itertools import product
import math
# It is assumed that positions are in units where box = 1
# Forces are calculated in units where sigma = 1 and epsilon = 1
# Uses neighbour lists
n = r.shape[0]
# Set up vectors to half the cells in neighbourhood of 3x3x3 cells in cubic lattice
# The cells are chosen so that if (d0,d1,d2) appears, then (-d0,-d1,-d2) does not.
d = np.array ( [ [ 0, 0, 0], [ 1, 0, 0], [ 1, 1, 0], [-1, 1, 0],
[ 0, 1, 0], [ 0, 0, 1], [-1, 0, 1], [ 1, 0, 1], [-1,-1, 1],
[ 0,-1, 1], [ 1,-1, 1], [-1, 1, 1], [ 0, 1, 1], [ 1, 1, 1] ] )
r = r - np.rint(r) # Ensure all atoms in periodic box
sr2_ovr = 1.77 # Overlap threshold (pot > 100)
r_cut_box = r_cut / box
r_cut_box_sq = r_cut_box ** 2
box_sq = box ** 2
# Calculate potential at cutoff
sr2 = 1.0 / r_cut**2 # in sigma=1 units
sr6 = sr2 ** 3
sr12 = sr6 **2
pot_cut = sr12 - sr6 # Without numerical factor 4
# Initialize
f = np.zeros_like(r)
total = PotentialType ( cut=0.0, pot=0.0, vir=0.0, lap=0.0, ovr=False )
# Calculate cell index triplets
sc = math.floor(box/r_cut) # Number of cells along box edge
assert sc >= 3, 'System is too small for cells' # Guard against box being too small
c = np.floor((r+0.5)*sc).astype(np.int_) # N*3 array of cell indices for all atoms
assert np.all(c>=0) and np.all(c<sc), 'Index error' # Simplistic "guard" against roundoff
if fast:
# Build list of arrays, each array holding positions of atoms in a cell
# At the same time, define a matching set of force arrays in each cell
# i and j number the atoms in each cell; we do not refer explicitly to indices in r
rc, fc = [], [] # Initially empty lists of positions and forces
for ci in product(range(sc),repeat=3): # Triple loop over cells
mask = np.all(c==ci,axis=1) # Mask identifies atoms in this cell
rc.append(r[mask,:]) # Copy atom coordinates into array, add to list
fc.append(np.zeros_like(rc[-1])) # Zero corresponding forces, add to list
for ci1, rci in enumerate(rc): # Loop over i-cells, getting all atoms in each i-cell as an array
ci = np.unravel_index(ci1,(sc,sc,sc)) # Get i-cell triple-indices
if rci.size==0: # Handle empty cell
continue
for dj in d: # Loop over neighbouring j-cells
cj = ci + dj # Compute neighbour j-cell triple-indices
cj1 = np.ravel_multi_index(cj,(sc,sc,sc),mode='wrap') # Convert j-cell to single-index
rcj = rc[cj1] # Get atoms in j-cell as an array
if rcj.size==0: # Handle empty cell
continue
rij = rci[:,np.newaxis,:]-rcj[np.newaxis,:,:] # Separation vectors for all i and j
rij = rij - np.rint(rij) # PBCs in box=1 units
rij_sq = np.sum(rij**2,axis=2) # Squared separations
in_range = rij_sq < r_cut_box_sq # Set flags for within cutoff
if ci1==cj1:
np.fill_diagonal(in_range,False) # Eliminate i==j when i-cell==j-cell
np.fill_diagonal(rij_sq,1.0) # Avoid divide-by-zero below
rij_sq = rij_sq * box_sq # Now in sigma=1 units
rij = rij * box # Now in sigma=1 units
sr2 = np.where ( in_range, 1.0/rij_sq, 0.0 ) # (sigma/rij)**2, only if in range
ovr = sr2 > sr2_ovr # Overlap if too close
sr6 = sr2 ** 3
sr12 = sr6 ** 2
cut = sr12 - sr6 # LJ potential (cut but not shifted)
vir = cut + sr12 # LJ virial
pot = np.where ( in_range, cut-pot_cut, 0.0 ) # LJ potential (cut-and-shifted)
lap = ( 22.0*sr12 - 5.0*sr6 ) * sr2 # LJ Laplacian
fij = vir * sr2 # LJ scalar part of forces
fij = rij * fij[:,:,np.newaxis] # LJ pair forces
if ci1==cj1: # Correct for double-counting ij and ji when i-cell==j-cell
fij = fij / 2
total = total + PotentialType ( cut=np.sum(cut)/2, pot=np.sum(pot)/2,
vir=np.sum(vir)/2, lap=np.sum(lap)/2, ovr=np.any(ovr) )
else:
total = total + PotentialType ( cut=np.sum(cut), pot=np.sum(pot),
vir=np.sum(vir), lap=np.sum(lap), ovr=np.any(ovr) )
fc[ci1][:,:] = fc[ci1][:,:] + np.sum(fij,axis=1) # Aggregate force on atoms in i-cell
fc[cj1][:,:] = fc[cj1][:,:] - np.sum(fij,axis=0) # Aggregate force on atoms in j-cell
# Copy forces from list of cell arrays to main force array
for ci in product(range(sc),repeat=3): # Triple loop over cells
mask = np.all(c==ci,axis=1) # Mask identifies atoms in this cell
ci1 = np.ravel_multi_index(ci,(sc,sc,sc),mode='wrap') # Single-index
f[mask,:] = fc[ci1] # Copy atom forces from correct cell
else:
# Build list of arrays, each array holding indices of atoms in a cell
# ki and kj are atom indices in the r array; i and j number the atoms in each cell
k_array = np.arange(n) # Atom indices 0..N-1
kc = [] # Initially empty list of indices
for ci in product(range(sc),repeat=3): # Triple loop over cells
mask = np.all(c==ci,axis=1) # Mask identifies atoms in this cell
kc.append(k_array[mask]) # Copy atom indices into array, add to list
for ci1, kci in enumerate(kc): # Loop over i-cells, getting atom indices as an array
ci = np.unravel_index(ci1,(sc,sc,sc)) # Get i-cell triple-indices
for dj in d: # Loop over neighbouring j-cells
cj = ci + dj # Compute neighbour j-cell triple-indices
cj1 = np.ravel_multi_index(cj,(sc,sc,sc),mode='wrap') # Convert to single-index
kcj = kc[cj1] # Get indices of atoms in j-cell as an array
for i, ki in enumerate(kci): # Loop over individual atoms in i-cell
j0 = i+1 if cj1==ci1 else 0 # Only look upwards if i-cell==j-cell
if j0 >= kcj.size: # Handles (redundantly) empty j-cell and the case
continue # where j-cell==i-cell and i is last atom
for kj in kcj[j0:]: # Loop over individual atoms in j-cell
rij = r[ki,:]-r[kj,:] # Separation vector
rij = rij - np.rint(rij) # Periodic boundary conditions in box=1 units
rij_sq = np.sum(rij**2) # Squared separation
if rij_sq < r_cut_box_sq: # Check within cutoff
rij_sq = rij_sq * box_sq # Now in sigma=1 units
rij = rij * box # Now in sigma=1 units
sr2 = 1.0 / rij_sq # (sigma/rij)**2
ovr = sr2 > sr2_ovr # Overlap if too close
sr6 = sr2 ** 3
sr12 = sr6 ** 2
cut = sr12 - sr6 # LJ potential (cut but not shifted)
vir = cut + sr12 # LJ virial
pot = cut - pot_cut # LJ potential (cut-and-shifted)
lap = ( 22.0*sr12 - 5.0*sr6 ) * sr2 # LJ Laplacian
fij = rij * vir * sr2 # LJ forces
total = total + PotentialType ( cut=cut, pot=pot, vir=vir, lap=lap, ovr=ovr )
f[ki,:] = f[ki,:] + fij
f[kj,:] = f[kj,:] - fij
# Multiply results by numerical factors
f = f * 24.0 # 24*epsilon
total.cut = total.cut * 4.0 # 4*epsilon
total.pot = total.pot * 4.0 # 4*epsilon
total.vir = total.vir * 24.0 / 3.0 # 24*epsilon and divide virial by 3
total.lap = total.lap * 24.0 * 2.0 # 24*epsilon and factor 2 for ij and ji
return total, f
def hessian ( box, r_cut, r, f ):
"""Calculates Hessian function (for 1/N correction to config temp)."""
import numpy as np
from itertools import product
import math
# This routine is only needed in a constant-energy ensemble
# It is assumed that positions are in units where box = 1
# but the result is given in units where sigma = 1 and epsilon = 1
# It is assumed that forces have already been calculated in array f
# Uses neighbour lists
n = r.shape[0]
assert np.all ( r.shape==f.shape ), 'Dimension mismatch in hessian'
# Set up vectors to half the cells in neighbourhood of 3x3x3 cells in cubic lattice
# The cells are chosen so that if (d1,d2,d3) appears, then (-d1,-d2,-d3) does not.
d = np.array ( [ [ 0, 0, 0], [ 1, 0, 0], [ 1, 1, 0], [-1, 1, 0],
[ 0, 1, 0], [ 0, 0, 1], [-1, 0, 1], [ 1, 0, 1], [-1,-1, 1],
[ 0,-1, 1], [ 1,-1, 1], [-1, 1, 1], [ 0, 1, 1], [ 1, 1, 1] ] )
r = r - np.rint(r) # Ensure all atoms in periodic box
r_cut_box = r_cut / box
r_cut_box_sq = r_cut_box ** 2
box_sq = box ** 2
hes = 0.0
# Calculate cell index triplets
sc = math.floor(box/r_cut) # Number of cells along box edge
assert sc >= 3, 'System is too small for cells' # Guard against box being too small
c = np.floor((r+0.5)*sc).astype(np.int_) # N*3 array of cell indices for all atoms
assert np.all(c>=0) and np.all(c<sc), 'Index error' # Simplistic "guard" against roundoff
if fast:
# Build list of arrays, each array holding positions of atoms in a cell
# At the same time, build a matching set of force arrays in each cell
# i and j number the atoms in each cell; we do not refer explicitly to indices in r
rc, fc = [], [] # Initially empty lists of positions and forces
for ci in product(range(sc),repeat=3): # Triple loop over cells
mask = np.all(c==ci,axis=1) # Mask identifies atoms in this cell
rc.append(r[mask,:]) # Copy atom coordinates into array, add to list
fc.append(f[mask,:]) # Copy corresponding forces, add to list
for ci1, rci in enumerate(rc): # Loop over i-cells, getting all atoms in each i-cell as an array
ci = np.unravel_index(ci1,(sc,sc,sc)) # Get i-cell triple-indices
fci = fc[ci1] # Get i-cell atom forces
if rci.size==0: # Handle empty cell
continue
for dj in d: # Loop over neighbouring j-cells
cj = ci + dj # Compute neighbour j-cell triple-indices
cj1 = np.ravel_multi_index(cj,(sc,sc,sc),mode='wrap') # Convert j-cell to single-index
rcj = rc[cj1] # Get atoms in j-cell as an array
fcj = fc[cj1] # Get j-cell atom forces
if rcj.size==0: # Handle empty cell
continue
rij = rci[:,np.newaxis,:]-rcj[np.newaxis,:,:] # Separation vectors for all i and j
rij = rij - np.rint(rij) # PBCs in box=1 units
rij_sq = np.sum(rij**2,axis=2) # Squared separations
in_range = rij_sq < r_cut_box_sq # Set flags for within cutoff
if ci1==cj1:
np.fill_diagonal(in_range,False) # Eliminate i=j when i-cell is j-cell
np.fill_diagonal(rij_sq,1.0) # Avoid divide-by-zero below
rij_sq = rij_sq * box_sq # Now in sigma=1 units
rij = rij * box # Now in sigma=1 units
fij = fci[:,np.newaxis,:]-fcj[np.newaxis,:,:] # Differences in forces for all i and j
ff = np.sum(fij*fij,axis=2)
rf = np.sum(rij*fij,axis=2)
sr2 = np.where ( in_range, 1.0 / rij_sq, 0.0 ) # Only where in range
sr6 = sr2 ** 3
sr8 = sr6 * sr2
sr10 = sr8 * sr2
v1 = 24.0 * ( 1.0 - 2.0 * sr6 ) * sr8
v2 = 96.0 * ( 7.0 * sr6 - 2.0 ) * sr10
if ci1==cj1: # Correct for double-counting ij and ji
hes = hes + np.sum(v1 * ff)/2 + np.sum(v2 * rf**2)/2
else:
hes = hes + np.sum(v1 * ff) + np.sum(v2 * rf**2)
else:
# Build list of arrays, each array holding indices of atoms in a cell
# ki and kj are atom indices in the r array; i and j number the atoms in each cell
k_array = np.arange(n) # Atom indices 0..N-1
kc = [] # Initially empty list of indices covering each cell
for ci in product(range(sc),repeat=3): # Triple loop over cells
mask = np.all(c==ci,axis=1) # Mask identifies atoms in this cell
kc.append(k_array[mask]) # Copy atom indices into array, add to list
for ci1, kci in enumerate(kc): # Loop over i-cells, getting atom indices as an array
ci = np.unravel_index(ci1,(sc,sc,sc)) # Get i-cell triple-indices
for dj in d: # Loop over neighbouring j-cells
cj = ci + dj # Compute neighbour j-cell triple-indices
cj1 = np.ravel_multi_index(cj,(sc,sc,sc),mode='wrap') # Convert to single-index
kcj = kc[cj1] # Get indices of atoms in j-cell as an array
for i, ki in enumerate(kci): # Loop over individual atoms in i-cell
j0 = i+1 if cj1==ci1 else 0 # Only look upwards if i-cell==j-cell
if j0 >= kcj.size: # Handles (redundantly) empty j-cell and the case
continue # where j-cell==i-cell and i is last atom
for kj in kcj[j0:]: # Loop over individual atoms in j-cell
rij = r[ki,:]-r[kj,:] # Separation vector
rij = rij - np.rint(rij) # Periodic boundary conditions in box=1 units
rij_sq = np.sum(rij**2) # Squared separation
if rij_sq < r_cut_box_sq:
rij_sq = rij_sq * box_sq # Now in sigma=1 units
rij = rij * box # Now in sigma=1 units
fij = f[ki,:] - f[kj,:] # Difference in forces
ff = np.dot(fij,fij)
rf = np.dot(rij,fij)
sr2 = 1.0 / rij_sq
sr6 = sr2 ** 3
sr8 = sr6 * sr2
sr10 = sr8 * sr2
v1 = 24.0 * ( 1.0 - 2.0 * sr6 ) * sr8
v2 = 96.0 * ( 7.0 * sr6 - 2.0 ) * sr10
hes = hes + v1 * ff + v2 * rf**2
return hes