forked from Allen-Tildesley/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmc_sc_module.py
More file actions
266 lines (211 loc) · 10.8 KB
/
mc_sc_module.py
File metadata and controls
266 lines (211 loc) · 10.8 KB
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
#!/usr/bin/env python3
# mc_sc_module.py
#------------------------------------------------------------------------------------------------#
# This software was written in 2016/17 #
# by Michael P. Allen <m.p.allen@warwick.ac.uk>/<m.p.allen@bristol.ac.uk> #
# and Dominic J. Tildesley <d.tildesley7@gmail.com> ("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. #
#------------------------------------------------------------------------------------------------#
"""Overlap and move routines for MC simulation, hard spherocylinders."""
fast = True # Change this to replace NumPy overlap evaluation with slower Python
length = 5.0 # Cylinder length L (in units where D=1) used throughout the module
def introduction():
"""Prints out introductory statements at start of run."""
import numpy as np
vmol = np.pi * ( 0.25*length + 1/6 ) # Spherocylinder volume
print('Hard spherocylinder potential')
print("{:40}{:15.6f}".format('Spherocylinder L/D ratio', length))
print("{:40}{:15.6f}".format('Spherocylinder volume/D**3', vmol))
print('Diameter, D = 1')
print('Energy, kT = 1')
if fast:
print('Fast NumPy overlap routine')
else:
print('Slow Python overlap routine')
def conclusion():
"""Prints out concluding statements at end of run."""
print('Program ends')
def overlap ( box, r, e ):
"""Takes in box and coordinate & orientation arrays, and signals any overlap."""
# Actual calculation is performed by function overlap_1
n, d = r.shape
assert d==3, 'Dimension error for r in overlap'
assert d==e.shape[1], 'Dimension error for e in overlap'
assert n==e.shape[0], "{}{:d}{:d}".format('Dimension error for e in overlap',n,e.shape[0])
for i in range(n-1):
if overlap_1 ( r[i,:], e[i,:], box, r[i+1:,:], e[i+1:,:] ):
return True # Immediate return on detection of overlap
return False
def overlap_1 ( ri, ei, box, r, e ):
"""Takes in coordinates and orientations of a molecules and signals any overlap.
Values of box and partner coordinate array are supplied.
"""
import numpy as np
# In general, r will be a subset of the complete set of simulation coordinates
# and none of its rows should be identical to ri
# It is assumed that positions are in units where box = 1
nj, d = r.shape
assert d==3, 'Dimension error for r in overlap_1'
assert d==e.shape[1], 'Dimension error for e in overlap_1'
assert nj==e.shape[0], "{}{:d}{:d}".format('Dimension error for e in overlap_1',nj,e.shape[0])
assert ri.size==3, 'Dimension error for ri in overlap_1'
assert ei.size==3, 'Dimension error for ei in overlap_1'
range = 1.0 + length
assert range <= box/2.0, "{}{:15.6f}{:15.6f}".format('Box too small', box, range)
range_box_sq = ( range / box ) ** 2 # Squared range in box=1 units
box_sq = box**2 # Squared box length
if fast:
rij = ri - r # Get all separation vectors from partners
rij = rij - np.rint(rij) # Periodic boundary conditions in box=1 units
rij_sq = np.sum(rij**2,axis=1) # Squared separations
rij_sq = rij_sq * box_sq # Now in D=1 units
rij = rij * box # Now in D=1 units
rei = np.sum ( rij*ei, axis=1 ) # All dot products
rej = np.sum ( rij*e, axis=1 ) # All dot products
eij = np.sum ( ei *e, axis=1 ) # All dot products
sij_sq = all_dist_sq ( rij_sq, rei, rej, eij ) # Squared distance between line segments
return np.any(sij_sq<1.0)
# Otherwise use slow method
for j,rj in enumerate(r):
rij = ri - rj # 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 > range_box_sq: # Check no possibility of overlap
continue
rij_sq = rij_sq * box_sq # Now in D=1 units
rij = rij * box # Now in D=1 units
rei = np.dot ( rij, ei )
rej = np.dot ( rij, e[j,:] )
eij = np.dot ( ei, e[j,:] )
sij_sq = dist_sq ( rij_sq, rei, rej, eij ) # Squared distance between line segments
if sij_sq<1.0:
return True # Overlap detected, return immediately
return False
def n_overlap ( box, r, e ):
"""Takes in box and coordinate and orientation arrays, and counts overlaps."""
# This routine is used in the calculation of pressure
# Actual calculation is performed by function n_overlap_1
n, d = r.shape
assert d==3, 'Dimension error for r in n_overlap'
assert d==e.shape[1], 'Dimension error for e in n_overlap'
assert n==e.shape[0], "{}{:d}{:d}".format('Dimension error for e in n_overlap',n,e.shape[0])
n_ovr = 0
for i in range(n-1):
n_ovr = n_ovr + n_overlap_1 ( r[i,:], e[i,:], box, r[i+1:,:], e[i+1:,:] )
return n_ovr
def n_overlap_1 ( ri, ei, box, r, e ):
"""Takes in coordinates and orientations of a molecule and counts overlaps.
Values of box and partner coordinate array are supplied.
Fast or slow algorithm selected.
"""
import numpy as np
# In general, r will be a subset of the complete set of simulation coordinates
# and none of its rows should be identical to ri
# It is assumed that positions are in units where box = 1
nj, d = r.shape
assert d==3, 'Dimension error for r in n_overlap_1'
assert d==e.shape[1], 'Dimension error for e in n_overlap_1'
assert nj==e.shape[0], "{}{:d}{:d}".format('Dimension error for e in n_overlap_1',nj,e.shape[0])
assert ri.size==3, 'Dimension error for ri in n_overlap_1'
assert ei.size==3, 'Dimension error for ei in n_overlap_1'
range = 1.0 + length
assert range <= box/2.0, "{}{:15.6f}{:15.6f}".format('Box too small', box, range)
range_box_sq = ( range / box ) ** 2 # Squared range in box=1 units
box_sq = box**2 # Squared box length
if fast:
rij = ri - r # Get all separation vectors from partners
rij = rij - np.rint(rij) # Periodic boundary conditions in box=1 units
rij_sq = np.sum(rij**2,axis=1) # Squared separations
rij_sq = rij_sq * box_sq # Now in D=1 units
rij = rij * box # Now in D=1 units
rei = np.sum ( rij*ei, axis=1 ) # All dot products
rej = np.sum ( rij*e, axis=1 ) # All dot products
eij = np.sum ( ei *e, axis=1 ) # All dot products
sij_sq = all_dist_sq ( rij_sq, rei, rej, eij ) # Squared distance between line segments
return np.count_nonzero(sij_sq<1.0)
# Otherwise use slow method
n_ovr = 0
for j,rj in enumerate(r):
rij = ri - rj # 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 > range_box_sq: # Check no possibility of overlap
continue
rij_sq = rij_sq * box_sq # Now in D=1 units
rij = rij * box # Now in D=1 units
rei = np.dot ( rij, ei )
rej = np.dot ( rij, e[j,:] )
eij = np.dot ( ei, e[j,:] )
sij_sq = dist_sq ( rij_sq, rei, rej, eij ) # Squared distance between line segments
if sij_sq<1.0:
n_ovr = n_ovr + 1
return n_ovr
def dist_sq ( rij_sq, rei, rej, eij ):
import numpy as np
tol = 1.0e-6
ell2 = length/2.0 # Half length
sin_sq = 1.0 - eij**2 # Squared sine of angle between line segments
if sin_sq < tol:
ci = -rei
cj = rej
else:
ci = ( - rei + eij * rej ) / sin_sq
cj = ( rej - eij * rei ) / sin_sq
ai = np.fabs ( ci )
aj = np.fabs ( cj )
if ai > ell2:
ci = ell2*np.sign(ci)
if aj > ell2:
cj = ell2*np.sign(cj)
if ai > aj:
cj = rej + ci * eij
else:
ci = -rei + cj * eij
ai = np.fabs ( ci )
aj = np.fabs ( cj )
if ai > ell2:
ci = ell2*np.sign(ci)
if aj > ell2:
cj = ell2*np.sign(cj)
di = 2.0 * rei + ci - cj * eij
dj = -2.0 * rej + cj - ci * eij
return rij_sq + ci * di + cj * dj # Squared distance between line segments
def all_dist_sq ( rij_sq, rei, rej, eij ):
import numpy as np
tol = 1.0e-6
ell2 = length/2.0 # Half length
sin_sq = 1.0 - eij**2 # Squared sines of angles between line segments
mask = sin_sq>tol
ci = np.where ( mask, (-rei+eij*rej)/sin_sq, -rei )
cj = np.where ( mask, ( rej-eij*rei)/sin_sq, rej )
ai = np.fabs ( ci )
aj = np.fabs ( cj )
ci = np.where ( ai>ell2, ell2*np.sign(ci), ci )
cj = np.where ( aj>ell2, ell2*np.sign(cj), cj )
mask = ai>aj
cj = np.where ( mask, rej+ci*eij, cj )
mask = np.logical_not(mask)
ci = np.where ( mask, -rei+cj*eij, ci )
ai = np.fabs ( ci )
aj = np.fabs ( cj )
ci = np.where ( ai>ell2, ell2*np.sign(ci), ci )
cj = np.where ( aj>ell2, ell2*np.sign(cj), cj )
di = 2.0 * rei + ci - cj * eij
dj = -2.0 * rej + cj - ci * eij
return rij_sq + ci * di + cj * dj # Squared distances between line segments