-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacker.py
More file actions
358 lines (325 loc) · 10.7 KB
/
Copy pathpacker.py
File metadata and controls
358 lines (325 loc) · 10.7 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
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
358
"""
Creates fcc, hcp or bcc lattice filling dist_pebbles cuboid or (hollow) cylindrical volume based on dist_pebbles target packing fraction
"""
#%% Module
import numpy as np
import pandas as pd
import warnings
from plotter import *
import warnings
#%% Functions
# Create lattice with correct lattice
def unit_cell(lattice_type, r_pebbles, nx, ny, nz):
dist_pebbles = 2 * np.sqrt(2) * r_pebbles
k, j, i = [
v.flatten()
for v in np.meshgrid(*([range(nz), range(ny), range(nx)]), indexing="ij")
]
if lattice_type == "fcc":
df = pd.DataFrame(
{
"x": dist_pebbles * i
+ dist_pebbles / 2 * (j % 2)
+ dist_pebbles / 2 * (k % 2),
"y": dist_pebbles / 2 * j,
"z": dist_pebbles / 2 * k,
}
)
# elif lattice_type == 'hcp': # problem with nx,ny,nz to set better
# df = pd.DataFrame({
# 'x': 2 * i + (j + k) % 2,
# 'y': np.sqrt(3) * (j + 1/3 * (k % 2)),
# 'z': 2 * np.sqrt(6) / 3 * k,
# })
elif lattice_type == "sc":
df = pd.DataFrame(
{
"x": 2 * r_pebbles * i,
"y": 2 * r_pebbles * j,
"z": 2 * r_pebbles * k,
}
)
return df
# Fill volume with lattice and shpae
def fill_cube(
lattice_type, dist_pebbles, sizeX, sizeY, sizeZ, centeredR=True, centeredZ=False
):
nx = int(np.ceil(sizeX / (np.sqrt(2) * dist_pebbles)))
ny = int(np.ceil(sizeY / (np.sqrt(2) * dist_pebbles)))
nz = int(np.ceil(sizeZ / (np.sqrt(2) * dist_pebbles)))
df = unit_cell(lattice_type, dist_pebbles, nx, ny, nz)
if centeredR:
df.x -= (df.x.max() - df.x.min()) / 2
df.y -= (df.y.max() - df.y.min()) / 2
if centeredZ:
df.z -= (df.z.max() - df.z.min()) / 2
return df
def shape_lattice(
df, shape, r_pebbles, Rout, H, Rin=0, Zmin=0, *, partial_pebbles=False
):
df["Rdist"] = np.linalg.norm(df[["x", "y"]], axis=1)
df["dist"] = np.linalg.norm(
df[["x", "y", "z"]] - df[["x", "y", "z"]].mean(), axis=1
)
if partial_pebbles:
r_pebbles = 0
if shape == "cyl":
field = "Rdist"
elif shape == "sph":
field = "dist"
if Rin > 0:
df = df[df[field] >= Rin + r_pebbles]
df = df[df[field] <= Rout - r_pebbles]
if shape == "cyl":
df = df[np.logical_and(df.z >= r_pebbles, df.z <= Zmin + H - r_pebbles)]
df = df.reset_index().drop(["index"], axis=1)
return df
def create_lattice(
lattice_type,
r_pebbles,
dist_pebbles,
shape,
Rout,
H,
Rin=0,
Zmin=0,
*,
partial_pebbles=False,
centeredR=True,
centeredZ=False,
same_rows=True
):
if shape == "cyl":
sizeX = 2 * Rout
sizeY = 2 * Rout
sizeZ = H
elif shape == "sph":
sizeX = 2 * Rout
sizeY = 2 * Rout
sizeZ = 2 * Rout
else:
sizeX = Rout - Rin
sizeY = Rout - Rin
sizeZ = H
# Fill cuboid
df = fill_cube(
lattice_type,
dist_pebbles,
sizeX,
sizeY,
sizeZ,
centeredR=centeredR,
centeredZ=centeredZ,
)
df["r_pebbles"] = r_pebbles
# Shape to cylinder
if shape == "cyl" or shape == "sph":
df = shape_lattice(
df, shape, r_pebbles, Rout, H, Rin, Zmin, partial_pebbles=partial_pebbles
)
if not isinstance(Zmin, type(None)) and Zmin != 0:
df.z += Zmin
shaped = False
while not shaped:
# Ensure same number of rows in fcc
if same_rows and shape in ["cyl", "sq"] and lattice_type == "fcc":
unique_z = np.unique(df.z)
if len(unique_z) % 2 != 0:
df = df[df.z < unique_z[-1]]
if shape in ["cyl", "sq"]:
lo_dZ = df.z.min() - Zmin
hi_dZ = Zmin + H - df.z.max()
dZ = (hi_dZ - lo_dZ) / 2
df.z += dZ
# TO DO BETTER
if shape == "cyl":
df = df[np.logical_and(df.z >= r_pebbles, df.z <= Zmin + H - r_pebbles)]
if shape in ["cyl", "sq"]:
lo_dZ = df.z.min() - Zmin
hi_dZ = Zmin + H - df.z.max()
dZ = (hi_dZ - lo_dZ) / 2
df.z += dZ
if shape == "cyl":
df = df[np.logical_and(df.z >= r_pebbles, df.z <= Zmin + H - r_pebbles)]
if same_rows and shape in ["cyl", "sq"] and lattice_type == "fcc":
unique_z = np.unique(df.z)
if len(unique_z) % 2 == 0:
shaped = True
else:
shaped = True
PF = calculate_packing_fraction(df, shape, r_pebbles, Rin, Rout, H)
return df, PF
def calculate_volumes(N, shape, r_spheres, Rin, Rout, H):
Vspheres = N * calculate_volume("sph", 0, r_spheres, 0)
Vcontainer = calculate_volume(shape, Rin, Rout, H)
return Vspheres, Vcontainer
def calculate_packing_fraction(df, shape, r_spheres, Rin, Rout, H):
Vspheres, Vcontainer = calculate_volumes(len(df), shape, r_spheres, Rin, Rout, H)
PF = Vspheres / Vcontainer
return PF
def calculate_volume(shape, Rin, Rout, H):
if shape == "cyl":
return np.pi * (Rout**2 - Rin**2) * H
elif shape == "sph":
return 4 / 3 * np.pi * (Rout**3 - Rin**3)
else:
return (Rout - Rin) ** 2 * H
# Iteratively finds right distance to match packing fraction
def find_lattice(
lattice_type,
r_spheres,
shape,
Rout,
H,
target,
Rin=0,
Zmin=0,
dist_pebbles_ini=1000,
mult_a=0.99,
eps=1e-2,
*,
partial_pebbles=False,
centeredR=True,
centeredZ=False,
same_rows=True,
verbose=False
):
# Choose between N (>1) and PF (<1)
if target >= 1:
Vspheres, Vcontainer = calculate_volumes(target, shape, r_spheres, Rin, Rout, H)
target_PF = Vspheres / Vcontainer
else:
target_PF = target
print('Packing with target = {:.2f}% ...'.format(target_PF*100))
# Quick check
if (
lattice_type == "fcc"
and target_PF > np.pi * np.sqrt(2) / 6
or lattice_type == "bc"
and target_PF > np.pi / 6
or lattice_type == "hcp"
and target_PF > np.pi * np.sqrt(2) / 6
):
raise Exception(
"Cannot obtain such dist_pebbles high packing fraction with this lattice type: {}".format(
lattice_type
)
)
# Initializers
PF = 0
found = False
trying_again = False
dist_pebbles = dist_pebbles_ini
cnt = 0
# Iterate (TO CHECK)
while not found and mult_a < 1:
while PF < target_PF or trying_again:
trying_again = False
df, PF = create_lattice(
lattice_type,
r_spheres,
dist_pebbles,
shape,
Rout,
H,
Rin,
Zmin,
partial_pebbles=partial_pebbles,
centeredR=centeredR,
centeredZ=centeredZ,
same_rows=same_rows,
)
if abs(PF - target_PF) / target_PF < eps:
found = True
print(
"\tFound: dist_pebbles={:.10f}, PF={:.4f}, N={}".format(
dist_pebbles, PF, len(df)
)
)
break
dist_pebbles *= mult_a
if not found:
if verbose:
print(
"Not found with dist_pebbles={:.10f}, mult_a={:.4f} (PF={:.4f}, N={}), trying again from dist_pebbles={:.4f} with mult_a={:.4f}".format(
dist_pebbles / mult_a,
mult_a,
PF,
len(df),
dist_pebbles / mult_a**2,
mult_a + (1 - mult_a) / 2,
)
)
dist_pebbles /= mult_a**2
mult_a += (1 - mult_a) / 2
trying_again = True
cnt += 1
if cnt > 50:
break
if not found:
warnings.warn(
"Did not find any good solution! Taking best with dist_pebbles={:.4f}, (PF={:.4f}, N={})".format(
dist_pebbles * mult_a**2, PF, len(df)
)
)
return df, dist_pebbles, PF, found
if __name__ == "__main__":
#%% Script
# Input parameters
# r_pebbles = 2 # pebbles radius
# Rin = 40 # inner radius, 0 if no inner radius
# Rout = 120 # 120 # core radius
# Zmin = 10 # Minimum elevation
# H = 310 # 310 # core height
# target_PF = 0.6 # packing fraction to reach
# lattice_type = "fcc" # can be hcp (not usable), fcc, sc
# shape = "cyl" # can be cyl or sph whatever (will be cuboid otherwise)
r_pebbles = 4.275000e-02 # pebbles radius
Rin = 1.38 # inner radius, 0 if no inner radius
Rout = 1.80 # 120 # core radius
Zmin = None # Minimum elevation
H = None # 310 # core height
target_PF = 0.22 # packing fraction to reach
lattice_type = "sc" # can be hcp (not usable), fcc, sc
shape = "sph" # can be cyl or sph whatever (will be cuboid otherwise)
# Secondary input
same_rows = True # needed for discrete motion
partial_pebbles = False # allow for pebbles crossing bounds in volume
centeredR = True # To radially center the geometry
centeredZ = False
# Parameters for iterative search with target packing fraction within volume
dist_pebbles_ini = r_pebbles * 5 # initial distance, huge on purpose
mult_a = 0.9 # initial multiplier for dist_pebbles, everytime the PF is too small
eps = 1e-3 # desired precision
# Iterate
df, dist_pebbles, PF, found = find_lattice(
lattice_type,
r_pebbles,
shape,
Rout,
H,
target_PF,
Rin,
Zmin,
dist_pebbles_ini,
mult_a,
eps,
partial_pebbles=partial_pebbles,
centeredR=centeredR,
centeredZ=centeredZ,
same_rows=same_rows,
verbose=True,
)
if not found:
warnings.warn(
"Did not find any good solution! Taking best with dist_pebbles={:.4f}, (PF={:.4f})".format(
dist_pebbles, PF
)
)
for i in range(3):
direction = ["x", "y", "z"][i]
value = np.array(df[direction]).flat[
np.abs(df[direction] - df[direction].mean()).argmin()
]
plot2D(df, i, value, "Rdist")
plot_df(df, "Rdist")