forked from zektipede/on-lattice-rw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_node_dict.py
441 lines (386 loc) · 15.6 KB
/
create_node_dict.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#create_node_dict.py
# Max Galettis
#14/03/2024
#only do these:
#tetra hedron
#cube
#octahedron
#octahedron + 2*cube #no doesnt work, you get intersections
###########################################################dodecahedrons do not form a lattice, last 2 weeks have been for nothing ahhhhhhhhhhhhhhhhhhh
#################################IT WAS ALL A LIE, DONT USE THE PLATONIC SOLIDS
#octahedron has central height 2, but side height of sqrt(2)
#trunctated dodecahedron
#wait, i can probably compute it for the lattice keeping the 4th dimension constant, since those direction vectors each have a complement
#first i have to check that getting to the path via other directions is possible...
import pickle
from Tree_Node import TreeNode #import the treenode class form the file tree_node.py in the same directory
import os
import time
import numpy as np #needed for the sqrt(5)
import sys
#tetrahedron, dictionary of sets
tetrahedron_lattice_perturbations = {
0: {
(1,1,1, 1),
(1,-1,-1, 1),
(-1,1,-1, 1),
(-1,-1,1, 1)
},
1: {
(-1,-1,-1, -1),
(-1,1,1, -1),
(1,-1,1, -1),
(1,1,-1, -1)
}
} #0 is a, 1 is b, it always switches...
#cube, set
cube_lattice_perturbations = {
(1,0,0),
(-1,0,0),
(0,1,0),
(0,-1,0),
(0,0,1),
(0,0,-1)
}
#octahedron, set
octahedron_lattice_perturbations = {
(1,1,1),
(1,1,-1),
(1,-1,1),
(1,-1,-1),
(-1,1,1),
(-1,1,-1),
(-1,-1,1),
(-1,-1,-1)
}
'''These dont work
#dodecahedron, dictionary of sets
#[1,phi], i should change this to np.array(1,phi) sort of thing... that way they can be summed faster?
dodecahedron_lattice_perturbations = {
0: {
((0,0),(1,0),(0,1), 1),
((0,0),(1,0),(0,-1), 1),
((0,0),(-1,0),(0,1), 1),
((0,0),(-1,0),(0,-1), 1),
((1,0),(0,1),(0,0), 1),
((1,0),(0,-1),(0,0), 1),
((-1,0),(0,1),(0,0), 1),
((-1,0),(0,-1),(0,0), 1),
((0,1),(0,0),(1,0), 1),
((0,1),(0,0),(-1,0), 1),
((0,-1),(0,0),(1,0), 1),
((0,-1),(0,0),(-1,0), 1)
},
1: {
((0,0),(0,1),(1,0), -1),
((0,0),(0,1),(-1,0), -1),
((0,0),(0,-1),(1,0), -1),
((0,0),(0,-1),(-1,0), -1),
((0,1),(1,0),(0,0), -1),
((0,1),(-1,0),(0,0), -1),
((0,-1),(1,0),(0,0), -1),
((0,-1),(-1,0),(0,0), -1),
((1,0),(0,0),(0,1), -1),
((1,0),(0,0),(0,-1), -1),
((-1,0),(0,0),(0,1), -1),
((-1,0),(0,0),(0,-1), -1)
}
} #0 is a, 1 is b
#icosahedron, dictionary of sets of tuples with tuples and ints
#note, 1/phi + 1 = phi, and hence we can write [1,1] instead of [0,0,1] where values correspond to [1/phi, 1]
#[1/phi, 1], phi=[1,1]
icosahedron_lattice_perturbations = {
0: {
((0,1),(0,1),(0,1), 1),
((0,1),(0,1),(0,-1), 1),
((0,1),(0,-1),(0,1), 1),
((0,1),(0,-1),(0,-1), 1),
((0,-1),(0,1),(0,1), 1),
((0,-1),(0,1),(0,-1), 1),
((0,-1),(0,-1),(0,1), 1),
((0,-1),(0,-1),(0,-1), 1),
((0,0),(1,0),(1,1), 1),
((0,0),(1,0),(-1,-1), 1),
((0,0),(-1,0),(1,1), 1),
((0,0),(-1,0),(-1,-1), 1),
((1,0),(1,1),(0,0), 1),
((1,0),(-1,-1),(0,0), 1),
((-1,0),(1,1),(0,0), 1),
((-1,0),(-1,-1),(0,0), 1),
((1,1),(0,0),(1,0), 1),
((1,1),(0,0),(-1,0), 1),
((-1,-1),(0,0),(1,0), 1),
((-1,-1),(0,0),(-1,0), 1)
},
1: {
((0,1),(0,1),(0,1), -1),
((0,1),(0,1),(0,-1), -1),
((0,1),(0,-1),(0,1), -1),
((0,1),(0,-1),(0,-1), -1),
((0,-1),(0,1),(0,1), -1),
((0,-1),(0,1),(0,-1), -1),
((0,-1),(0,-1),(0,1), -1),
((0,-1),(0,-1),(0,-1), -1),
((0,0),(1,1),(1,0), -1),
((0,0),(1,1),(-1,0), -1),
((0,0),(-1,-1),(1,0), -1),
((0,0),(-1,-1),(-1,0), -1),
((1,1),(1,0),(0,0), -1),
((1,1),(-1,0),(0,0), -1),
((-1,-1),(1,0),(0,0), -1),
((-1,-1),(-1,0),(0,0), -1),
((1,0),(0,0),(1,1), -1),
((1,0),(0,0),(-1,-1), -1),
((-1,0),(0,0),(1,1), -1),
((-1,0),(0,0),(-1,-1), -1)
}
} #0 is a, 1 is b
'''
###generate box/lattice
def tetrahedron_generate_box(X,Y,Z, folder=''):
file_name = f'tetrahedron_{X}by{Y}by{Z}.lat'
#'' in os.path.join is current directory
file_path = os.path.join(folder, file_name)
#initialise the box/lattice
box = {}
fresh_points = {(0,0,0,0)} #set of tuples
#grow the lattice
while True:
#add freshpoints to box and find futurepoints
future_points = set()
for fresh_point in fresh_points:
neighbours = {
(fresh_point[0] + perturbation[0],
fresh_point[1] + perturbation[1],
fresh_point[2] + perturbation[2],
fresh_point[3] + perturbation[3])
for perturbation in tetrahedron_lattice_perturbations[fresh_point[3]]
}
valid_neighbours = set()
for neighbour in neighbours:
if (
0<=neighbour[0]<X and
0<=neighbour[1]<Y and
0<=neighbour[2]<Z
):
valid_neighbours.add(neighbour)
box[fresh_point] = TreeNode(fresh_point,valid_neighbours)
future_points.update(valid_neighbours)
#update values for the next iteration of the while loop
fresh_points = future_points - set(box)
if not future_points: break #empty sets return false
#record the lattice, number of nodes (its kind of like the effective volume and used for doing density of neurons calcs)
with open(os.path.join(folder, 'corresponding_nodes.txt'), 'a') as f:
f.write(f'{file_name}, {len(box)}\n')
# Pickle the box
with open(file_path, 'wb') as f:
pickle.dump(box, f)
def cube_generate_box(X,Y,Z, folder=''):
file_name = f'cube_{X}by{Y}by{Z}.lat'
#'' in os.path.join is current directory
file_path = os.path.join(folder, file_name)
#initialise the box/lattice
box = {}
fresh_points = {(0,0,0)} #set of tuples
#grow the lattice
while True:
#add freshpoints to box and find futurepoints
future_points = set()
for fresh_point in fresh_points:
neighbours = {
(fresh_point[0] + perturbation[0],
fresh_point[1] + perturbation[1],
fresh_point[2] + perturbation[2])
for perturbation in cube_lattice_perturbations
}
valid_neighbours = set()
for neighbour in neighbours:
if (
0<=neighbour[0]<X and
0<=neighbour[1]<Y and
0<=neighbour[2]<Z
):
valid_neighbours.add(neighbour)
box[fresh_point] = TreeNode(fresh_point,valid_neighbours)
future_points.update(valid_neighbours)
#update values for the next iteration of the while loop
fresh_points = future_points - set(box)
if not future_points: break #empty sets return false
#record the lattice, number of nodes (its kind of like the effective volume and used for doing density of neurons calcs)
with open(os.path.join(folder, 'corresponding_nodes.txt'), 'a') as f:
f.write(f'{file_name}, {len(box)}\n')
# Pickle the box
with open(file_path, 'wb') as f:
pickle.dump(box, f)
def octahedron_generate_box(X,Y,Z, folder=''):
file_name = f'octahedron_{X}by{Y}by{Z}.lat'
#'' in os.path.join is current directory
file_path = os.path.join(folder, file_name)
#initialise the box/lattice
box = {}
fresh_points = {(0,0,0)} #set of tuples
#grow the lattice
while True:
#add freshpoints to box and find futurepoints
future_points = set()
for fresh_point in fresh_points:
neighbours = {
(fresh_point[0] + perturbation[0],
fresh_point[1] + perturbation[1],
fresh_point[2] + perturbation[2])
for perturbation in octahedron_lattice_perturbations
}
valid_neighbours = set()
for neighbour in neighbours:
if (
0<=neighbour[0]<X and
0<=neighbour[1]<Y and
0<=neighbour[2]<Z
):
valid_neighbours.add(neighbour)
box[fresh_point] = TreeNode(fresh_point,valid_neighbours)
future_points.update(valid_neighbours)
#update values for the next iteration of the while loop
fresh_points = future_points - set(box)
if not future_points: break #empty sets return false
#record the lattice, number of nodes (its kind of like the effective volume and used for doing density of neurons calcs)
with open(os.path.join(folder, 'corresponding_nodes.txt'), 'a') as f:
f.write(f'{file_name}, {len(box)}\n')
# Pickle the box
with open(file_path, 'wb') as f:
pickle.dump(box, f)
'''These dont work
def dodecahedron_generate_box(X,Y,Z, folder=''):
file_name = f'dodecahedron_{X}by{Y}by{Z}.lat'
#'' in os.path.join is current directory
file_path = os.path.join(folder, file_name)
phi = (np.sqrt(5)+1)/2 #golden ratio, or 1 + 1/goldenratio
#initialise the box/lattice
box = {}
fresh_points = {((0,0),(0,0),(0,0),0)} #set of tuples
#grow the lattice
while True:
#add freshpoints to box and find futurepoints
future_points = set()
for fresh_point in fresh_points:
neighbours = {
((fresh_point[0][0] + perturbation[0][0], fresh_point[0][1] + perturbation[0][1]),
(fresh_point[1][0] + perturbation[1][0], fresh_point[1][1] + perturbation[1][1]),
(fresh_point[2][0] + perturbation[2][0], fresh_point[2][1] + perturbation[2][1]),
fresh_point[3] + perturbation[3])
for perturbation in dodecahedron_lattice_perturbations[fresh_point[3]]
}
valid_neighbours = set()
for neighbour in neighbours:
if (
0<=neighbour[0][0] + phi*neighbour[0][1]<X and
0<=neighbour[1][0] + phi*neighbour[1][1]<Y and
0<=neighbour[2][0] + phi*neighbour[2][1]<Z
):
valid_neighbours.add(neighbour)
box[fresh_point] = TreeNode(fresh_point,valid_neighbours)
future_points.update(valid_neighbours)
#update values for the next iteration of the while loop
fresh_points = future_points - set(box)
if not future_points: break #empty sets return false
# Pickle the box
with open(file_path, 'wb') as f:
pickle.dump(box, f)
def icosahedron_generate_box(X,Y,Z, folder=''):
file_name = f'icosahedron_{X}by{Y}by{Z}.lat'
#'' in os.path.join is current directory
file_path = os.path.join(folder, file_name)
#phi = (1 + np.sqrt(5))/2 #the golden ratio
#phi - 1 was closer than 1/phi to the real value so thats what we will use
#actually, phi - 0.9999999999999999 was closer, but that was only visually
one_on_phi = (np.sqrt(5)-1)/2 #so turns out the golden ratio is actually so special that 1+ 1/phi = phi, so changing the table
#initialise the box/lattice
box = {}
fresh_points = {((0,0),(0,0),(0,0),0)} #set of tuples
#grow the lattice
while True:
#add freshpoints to box and find futurepoints
future_points = set()
for fresh_point in fresh_points:
neighbours = {
((fresh_point[0][0] + perturbation[0][0], fresh_point[0][1] + perturbation[0][1]),
(fresh_point[1][0] + perturbation[1][0], fresh_point[1][1] + perturbation[1][1]),
(fresh_point[2][0] + perturbation[2][0], fresh_point[2][1] + perturbation[2][1]),
fresh_point[3] + perturbation[3])
for perturbation in icosahedron_lattice_perturbations[fresh_point[3]]
}
valid_neighbours = set()
for neighbour in neighbours:
if (
0<=one_on_phi*neighbour[0][0] + neighbour[0][1]<X and
0<=one_on_phi*neighbour[1][0] + neighbour[1][1]<Y and
0<=one_on_phi*neighbour[2][0] + neighbour[2][1]<Z
):
valid_neighbours.add(neighbour)
box[fresh_point] = TreeNode(fresh_point,valid_neighbours)
future_points.update(valid_neighbours)
#update values for the next iteration of the while loop
fresh_points = future_points - set(box)
if not future_points: break #empty sets return false
# Pickle the box
with open(file_path, 'wb') as f:
pickle.dump(box, f)
#only use the 0th set, dont change chirality, the complements for the directions exist in its own set
def mad_test_icosahedron_generate_box(X,Y,Z, folder=''):
file_name = f'mad_icosahedron_{X}by{Y}by{Z}.lat'
#'' in os.path.join is current directory
file_path = os.path.join(folder, file_name)
#phi = (1 + np.sqrt(5))/2 #the golden ratio
#phi - 1 was closer than 1/phi to the real value so thats what we will use
#actually, phi - 0.9999999999999999 was closer, but that was only visually
one_on_phi = (np.sqrt(5)-1)/2 #so turns out the golden ratio is actually so special that 1+ 1/phi = phi, so changing the table
#initialise the box/lattice
box = {}
fresh_points = {((0,0),(0,0),(0,0),0)} #set of tuples
#grow the lattice
while True:
#add freshpoints to box and find futurepoints
future_points = set()
for fresh_point in fresh_points:
neighbours = {
((fresh_point[0][0] + perturbation[0][0], fresh_point[0][1] + perturbation[0][1]),
(fresh_point[1][0] + perturbation[1][0], fresh_point[1][1] + perturbation[1][1]),
(fresh_point[2][0] + perturbation[2][0], fresh_point[2][1] + perturbation[2][1]),
0)
for perturbation in icosahedron_lattice_perturbations[0]
}
valid_neighbours = set()
for neighbour in neighbours:
if (
0<=one_on_phi*neighbour[0][0] + neighbour[0][1]<X and
0<=one_on_phi*neighbour[1][0] + neighbour[1][1]<Y and
0<=one_on_phi*neighbour[2][0] + neighbour[2][1]<Z
):
valid_neighbours.add(neighbour)
#if fresh_point in box: print(fresh_point)
print(valid_neighbours)
box[fresh_point] = TreeNode(fresh_point,valid_neighbours)
future_points.update(valid_neighbours)
#update values for the next iteration of the while loop
fresh_points = future_points - set(box)
if not future_points: break #empty sets return false
# Pickle the box
with open(file_path, 'wb') as f:
pickle.dump(box, f)
'''
def main():
X=200
Y=200
Z=200
folder='lattices' #'' is current directory
#box is counted computationally, 27 means 27 positions, values 0 to 26, hence 0<=x<X is used
#158/159 is about 1mil points in the octahedral lattice (2 points per unit cell, each unit cell has a volume of 2^3)
start_time = time.process_time()#time.perf_counter()
tetrahedron_generate_box(X,Y,Z,folder=folder)
#cube_generate_box(X,Y,Z,folder=folder)
#octahedron_generate_box(X,Y,Z, folder=folder)
end_time = time.process_time() #time.perf_counter()
elapsed_time = end_time - start_time
print(f'{X},{Y},{Z}. lattice built in:', elapsed_time)
if __name__ == '__main__':
main()