-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoomd_simulation.py
270 lines (224 loc) · 7.74 KB
/
hoomd_simulation.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
import itertools
import math
import hoomd
import os
import freud
import gsd.hoomd
import matplotlib
import numpy
# %matplotlib inline
# matplotlib.style.use('ggplot')
import matplotlib_inline
matplotlib_inline.backend_inline.set_matplotlib_formats('svg')
# fn = os.path.join(os.getcwd(), 'random.gsd')
# ![ -e "$fn" ] && rm "$fn"
import io
import warnings
import fresnel
import IPython
import numpy as np
import packaging.version
import PIL
device = fresnel.Device()
tracer = fresnel.tracer.Path(device=device, w=300, h=300)
FRESNEL_MIN_VERSION = packaging.version.parse('0.13.0')
FRESNEL_MAX_VERSION = packaging.version.parse('0.14.0')
#----- Shape Dictionaries -----
shape_vertices = {'cube': [
[0.5, 0.5, 0.5],
[-0.5, 0.5, 0.5],
[-0.5, -0.5, 0.5],
[-0.5, 0.5, -0.5],
[0.5, -0.5, 0.5],
[0.5, -0.5, -0.5],
[0.5, 0.5, -0.5],
[-0.5, -0.5, -0.5],
], 'octahedron': [
[-0.5, 0, 0],
[0.5, 0, 0],
[0, -0.5, 0],
[0, 0.5, 0],
[0, 0, -0.5],
[0, 0, 0.5],
], 'tetrahedron': [
[0, 0, 0.5],
[0, 0.7071, -0.5],
[0.5*np.sqrt(3/2), -0.3535, -0.5],
[-0.5*np.sqrt(3/2), -0.3535, -0.5],
], 'dodecahedron': [
]}
a_values = {'cube': 1,
'octahedron': np.sqrt(2) * 0.5,
'tetrahedron': np.sqrt(3/2)
}
#----- Inputs -----
import sys
inputs = sys.argv
#file.py | filename | density | N_particles | spacing | shape_name_1 | size_mult_1 | ratio | shape_name_2 | size_mult_2 |
filename = ''
density = 0.42
m = 2
N_particles = 2*m**3
spacing = 1.5
shape_names = ['tetrahedron']
size_mult = [1]
ratio = 1
vertices_1 = shape_vertices[shape_names[0]]
shape_d = 0.15
shape_a = 0.2
target_a = 0.2
seed = 9
sim_length = 1e6
if len(inputs) == 2:
filename = inputs[1]
elif len(inputs) == 3:
filename = inputs[1]
density = float(inputs[2])
elif len(inputs) == 4:
filename = inputs[1]
density = float(inputs[2])
N_particles = int(inputs[3])
elif len(inputs) == 5:
filename = inputs[1]
density = float(inputs[2])
N_particles = int(inputs[3])
spacing = float(inputs[4])
shape_names = [inputs[5]]
elif len(inputs) == 6:
filename = inputs[1]
density = float(inputs[2])
N_particles = int(inputs[3])
spacing = float(inputs[4])
shape_names = [inputs[5]]
size_mult = float(inputs[6])
elif len(inputs) == 7:
filename = inputs[1]
density = float(inputs[2])
N_particles = int(inputs[3])
spacing = float(inputs[4])
shape_names = [inputs[5]]
size_mult = float(inputs[6])
elif len(inputs) > 6:
filename = inputs[1]
density = float(inputs[2])
N_particles = int(inputs[3])
spacing = float(inputs[4])
shape_names = [inputs[5], inputs[8]]
size_mult = [float(inputs[6]), float(inputs[9])]
ratio = float(inputs[7])
#----- Volumes -----
if size_mult[0] != 1:
try:
a_values[shape_names[0]]
except:
print(f'{shape_names[0]} is not among the list of shapes available! :(')
else:
old_a_1 = a_values[shape_names[0]]
a_values[shape_names[0]] = size_mult[0] * old_a_1
if len(size_mult) > 1:
if size_mult[1] != 1:
try:
a_values[shape_names[1]]
except:
print(f'{shape_names[1]} is not among the list of shapes available! :(')
else:
old_a_2 = a_values[shape_names[1]]
a_values[shape_names[1]] = size_mult[1] * old_a_2
shape_volumes = {'cube': a_values['cube']**3,
'octahedron': 1/3 * np.sqrt(2) * a_values['octahedron']**3,
'tetrahedron': a_values['tetrahedron']**3 / (6*np.sqrt(2))
}
shape_surface_area = {'cube': 6*a_values['cube']**2,
'octahedron': 2*np.sqrt(3) * a_values['octahedron']**2,
'tetrahedron': np.sqrt(3) * a_values['tetrahedron']**2
}
#----- Integrator + Initial State -----
mc = hoomd.hpmc.integrate.ConvexPolyhedron()
mc.nselect = 2
cpu = hoomd.device.CPU()
K = math.ceil(N_particles**(1/3))
L = K*spacing
x = np.linspace(-L/2, L/2, K, endpoint=False)
if len(shape_names) == 1:
mc.shape[shape_names[0]] = dict(vertices=vertices_1)
mc.d[shape_names[0]] = shape_d
mc.a[shape_names[0]] = shape_a
shape_position = list(itertools.product(x, repeat=3))
shape_position = shape_position[0:N_particles]
shape_orientation = [(1,0,0,0)] * N_particles
frame = gsd.hoomd.Frame()
frame.particles.N = N_particles
frame.particles.position = shape_position
frame. particles.orientation = shape_orientation
frame.particles.typeid = [0] * N_particles
frame.particles.types = shape_names
frame.particles.type_shapes = [dict(type = "ConvexPolyhedron", rounding_radius = 0.01, vertices = vertices_1)]
frame.configuration.box = [L,L,L,0,0,0]
else:
N_1 = int(N_particles*ratio)
N_2 = N_particles - N_1
#First Shape
mc.shape[shape_names[0]] = dict(vertices=vertices_1)
mc.d[shape_names[0]] = shape_d
mc.a[shape_names[0]] = shape_a
shape_position_1 = list(itertools.product(x, repeat=3))
shape_position_1 = shape_position_1[0:N_1]
shape_orientation_1 = [(1,0,0,0)] * N_1
#Second Shape
mc.shape[shape_names[1]] = dict(vertices=vertices_1)
mc.d[shape_names[1]] = shape_d
mc.a[shape_names[1]] = shape_a
shape_position_2 = list(itertools.product(x, repeat=3))
shape_position_2 = shape_position_2[N_1:N_2]
shape_orientation_2 = [(1,0,0,0)] * N_2
#Combine Shapes
shape_position = shape_position_1 + shape_position_2
shape_orientation = shape_orientation_1 + shape_orientation_2
frame = gsd.hoomd.Frame()
frame.particles.N = N_particles
frame.particles.position = shape_position
frame. particles.orientation = shape_orientation
frame.particles.typeid = [0] * N_1 + [1] * N_2
frame.particles.types = shape_names
frame.configuration.box = [L,L,L,0,0,0]
with gsd.hoomd.open(name='initial_state_'+filename, mode='x') as f:
f.append(frame)
simulation = hoomd.Simulation(device=cpu, seed=seed)
simulation.operations.integrator = mc
simulation.create_state_from_gsd(filename='initial_state_'+filename)
gsd_writer = hoomd.write.GSD(filename=filename, trigger=hoomd.trigger.Periodic(1000), mode='xb')
simulation.operations.writers.append(gsd_writer)
#----- Randomizing -----
simulation.run(10e3)
if mc.overlaps != 0:
print('WARNING: Particles are overlapping!')
print('Number of Overlaps:', mc.overlaps)
#----- Compressing -----
if len(shape_names) == 2:
V_particle_1 = shape_volumes[shape_names[0]]
V_particle_2 = shape_volumes[shape_names[1]]
total_shape_volume = N_1*V_particle_1 + N_2*V_particle_2
else:
V_particle = shape_volumes[shape_names[0]]
total_shape_volume = N_particles * V_particle
initial_volume_fraction = (total_shape_volume/simulation.state.box.volume)
initial_box = simulation.state.box
final_box = hoomd.Box.from_box(initial_box)
final_box.volume = total_shape_volume / density
compress = hoomd.hpmc.update.QuickCompress(trigger=hoomd.trigger.Periodic(10), target_box=final_box)
simulation.operations.updaters.append(compress)
periodic = hoomd.trigger.Periodic(10)
tune = hoomd.hpmc.tune.MoveSize.scale_solver(moves=['a','d'], target=target_a, trigger=periodic, max_translation_move=target_a, max_rotation_move=target_a)
simulation.operations.tuners.append(tune)
while not compress.complete and simulation.timestep < 1e7:
simulation.run(1000)
if not compress.complete:
print('WARNING: Simulation did not successfully compress!')
#remove compress and tune
simulation.operations.remove(compress)
simulation.operations.remove(tune)
#----- Equilibrium -----
tune2 = hoomd.hpmc.tune.MoveSize.scale_solver(moves=['a','d'], target=target_a, trigger=hoomd.trigger.And([hoomd.trigger.Periodic(100), hoomd.trigger.Before(simulation.timestep + 5000)]))
simulation.operations.tuners.append(tune2)
simulation.run(sim_length + 5100)
gsd_writer.flush()