-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPacking Fraction Coding Challenge
249 lines (249 loc) · 10.2 KB
/
Packing Fraction Coding Challenge
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
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "7b7408bd",
"metadata": {},
"outputs": [],
"source": [
"# CODE FOR THE ALGORITHMS #\n",
"from numpy import random as rnd \n",
"import numpy as np\n",
"\n",
"def Simulation(prob, alpha, N, Nrep, Nsample, r1, r2 ): \n",
"############### Algorithm 1: Generating a random number of adsorbed particles ##################\n",
" z = [r1, r2]\n",
" if r1 == r2:\n",
" z.pop(1) # removes 2nd radius size if the first one is the same\n",
" \n",
" choice = rnd.choice(z, N, replace = True, p = prob) # rnd.choice makes the list of given radii in their given probabilities to N quantity.\n",
" \n",
" choices = list(choice)\n",
" x = [rnd.uniform(0,20) for _ in range(choices.count(r1))] # this sets up the co-ordinate (x,y) location for each disk within the 20 x 20 square to model in. \n",
" y = [rnd.uniform(0,20) for _ in range(choices.count(r1))] # can always add the z co-ordinate, but will need to add another layer of code to account for overlap with x and y.\n",
" x1 = [rnd.uniform(0,20) for _ in range(choices.count(r2))] # each co-ordinate is put into a list.\n",
" y1 = [rnd.uniform(0,20) for _ in range(choices.count(r2))]\n",
" \n",
" # Meeting condition 1: Fitting within square boundary of L = 20 \n",
" \n",
" for i in x: \n",
" if (i - r1) < 0 or (i + r1) > 20 : # if the radius + co-ordinate location of disc is greater than the boundaries given, it rejected\n",
" y.remove(y[x.index(i)]) # rule that no particles can overlap the edges of the box (20 x 20)\n",
" x.remove(i)\n",
" \n",
" for i in y:\n",
" if (i - r1) < 0 or (i + r1) > 20 :\n",
" x.remove(x[y.index(i)])\n",
" y.remove(i)\n",
" \n",
" for i in x1:\n",
" if (i - r2) < 0 or (i + r2) > 20 :\n",
" y1.remove(y1[x1.index(i)])\n",
" x1.remove(i)\n",
" \n",
" for i in y1:\n",
" if (i - r2) < 0 or (i + r2) > 20:\n",
" x1.remove(x1[y1.index(i)])\n",
" y1.remove(i)\n",
" \n",
" # Meeting condition 2: Overlapping with other disks \n",
" \n",
" for i,j in zip(x,y): #zip makes lists affected by functions at the same time \n",
" k=0\n",
" while k < len(x): # the variable k is only used to recoginse the position of the co-ordinates' list.\n",
" distance = np.sqrt((i - x[k])**2 + (j - y[k])**2) # this should compare discs within the lists.\n",
" if distance == 0:\n",
" pass\n",
" elif distance < alpha*(r1 + r2):\n",
" y.remove(y[k])\n",
" x.remove(x[k]) \n",
" \n",
" k +=1\n",
" continue\n",
" \n",
" if r1 != r2: # Only processes this for bidisperse situations.\n",
" for i,j in zip(x1,y1): # Overlap of R2-R2\n",
" k=0\n",
" while k < len(x1): #y1 couldve been used instead of x1; they carry the same number of co-ordinates\n",
" distance = np.sqrt((i - x1[k])**2 + (j - y1[k])**2)\n",
" if distance == 0:\n",
" pass\n",
" elif distance < alpha*(r1 + r2):\n",
" y1.remove(y1[k])\n",
" x1.remove(x1[k]) \n",
"\n",
" k +=1\n",
" continue\n",
" # Overlap R1 - R2\n",
" if len(x1) <= len(x): # (if there are less r2 disks than r1)\n",
" for i,j in zip(x,y):\n",
" k=0\n",
" while k < len(x1):\n",
" distance = np.sqrt((x1[k] - i)**2 + (y1[k] - j)**2) # compares each x1/y1 disk co-ordinate with every\n",
" if distance == 0: # x/y disk.\n",
" pass\n",
" elif distance < alpha*(r1 + r2):\n",
" y1.remove(y1[k]) \n",
" x1.remove(x1[k])\n",
" k += 1\n",
" continue\n",
" \n",
" else:\n",
" for i,j in zip(x1,y1): # (if there are less r1 disks than r2) \n",
" k=0\n",
" while k < len(x):\n",
" distance = np.sqrt((x[k] - i)**2 + (y[k] - j)**2)\n",
" if distance == 0:\n",
" pass\n",
" elif distance < alpha*(r1 + r2):\n",
" y.remove(y[k])\n",
" x.remove(x[k])\n",
" k += 1\n",
" continue\n",
" \n",
"########## Algorithm 2: Measuring the packing fraction via Monte Carlo simulation #############\n",
" \n",
" if r1 == r2: # Monodisperse situation\n",
" del x1,y1\n",
" \n",
" listvalid = []\n",
" for p in range(Nrep): # Implementing Nrep - amount of repeat of the simulation\n",
" valid = 0\n",
" for i in range(Nsample): # Implementing Nsample = amount of points\n",
" u = rnd.uniform(0,20) # that will compare to the disks\n",
" v = rnd.uniform(0,20)\n",
" \n",
" j = 0\n",
" while j < len(x): # Comparing points between\n",
" distance = np.sqrt((u - x[j])**2 + (v - y[j])**2) # existing disks\n",
" if distance <= r1:\n",
" valid += 1\n",
" j += 1\n",
" continue\n",
" \n",
" packingfraction = valid/Nsample\n",
" listvalid.append(packingfraction)\n",
" \n",
" average = sum(listvalid)/len(listvalid)\n",
" \n",
" return average\n",
" \n",
" else: # Bidisperse situations\n",
" listvalid = []\n",
"\n",
" for p in range(Nrep): \n",
" valid = 0\n",
" for i in range(Nsample): \n",
" u = rnd.uniform(0,20)\n",
" v = rnd.uniform(0,20)\n",
" \n",
" j = 0\n",
" while j < len(x): # Comparing points between\n",
" distance = np.sqrt((u - x[j])**2 + (v - y[j])**2) # existing R1 disks\n",
" if distance <= r1:\n",
" valid += 1\n",
" j += 1\n",
" continue\n",
" \n",
" while j < len(x1): # Comparing points between\n",
" distance = np.sqrt((u - x1[j])**2 + (v - y1[j])**2) # existing R2 disks\n",
" if distance <= r1 or distance <= r1:\n",
" valid += 1\n",
" j += 1\n",
" continue\n",
" \n",
" packingfraction = valid/Nsample\n",
" listvalid.append(packingfraction)\n",
" \n",
" average = sum(listvalid)/len(listvalid) # Takes an mean packing fraction, ensures accuracy.\n",
" return average # Will help when plotting graphs"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "18d41e18",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"packing fraction of test1: 0.394\n"
]
}
],
"source": [
"############################### MONODISPERSE #######################################\n",
"test1 = Simulation([1], 0.8, 400, 10, 100, np.sqrt(1/(np.pi)), np.sqrt(1/(np.pi)))\n",
"print('packing fraction of test1:', test1)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "cb8ace74",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"packing fraction of test2: 0.46900000000000003\n"
]
}
],
"source": [
"############################### BIDISPERSE 1 ########################################\n",
"test2 = Simulation([0.5,0.5], 0.8, 400, 10, 100, np.sqrt(3/(2*np.pi)), np.sqrt(1/(2*np.pi)))\n",
"print('packing fraction of test2:', test2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "50d982cb",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"packing fraction of test3: 0.344\n"
]
}
],
"source": [
"############################### BIDISPERSE 2 ########################################\n",
"test3 = Simulation([0.8,0.2], 0.8, 400, 10, 100, np.sqrt(15/(16*np.pi)), np.sqrt(5/(4*np.pi)))\n",
"print('packing fraction of test3:', test3)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}