-
Notifications
You must be signed in to change notification settings - Fork 0
/
2d_DLA.py
executable file
·421 lines (339 loc) · 12.4 KB
/
2d_DLA.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
#!/usr/bin/env python3
import turtle ## for drawing
import random ## for random walk
import math ## distance formula (square root)
import copy ##copying lists so they won't be bound together
import time
import sys
import argparse
EMPTY = 0
FILLED = 1
## currently all circle related functions draw squares instead
def drawGrid(rows, columns, tortoise):
"""Draws an empty grid using turtle graphics.
Parameters:
rows: the number of rows in the grid
columns: the number of columns in the grid
tortoise: a Turtle object
Return value: None
"""
tortoise.pencolor('lightblue')
for row in range(rows + 1):
tortoise.up()
tortoise.goto(0, row)
tortoise.down()
tortoise.goto(columns, row)
for column in range(columns + 1):
tortoise.up()
tortoise.goto(column, 0)
tortoise.down()
tortoise.goto(column, rows)
def emptyGrid(rows, columns):
"""Create a rows x columns grid of zeros.
Parameters:
rows: the number of rows in the grid
columns: the number of columns in the grid
Return value: a list of ROWS lists of COLUMNS zeros
"""
grid = []
for r in range(rows):
row = [EMPTY] * columns
grid.append(row)
return grid
def createCircles(screen, colors, scale):
square = ((0, 0), (0, scale), (scale, scale), (scale, 0))
for color in colors:
squareShape = turtle.Shape('compound')
squareShape.addcomponent(square, color, 'lightblue')
screen.register_shape(color, squareShape)
def drawCircle(pos, color, tortoise):
(row, column) = pos
screen = tortoise.getscreen()
rows = int(screen.canvheight / screen.yscale)
row = rows - row - 1
tortoise.shape(color)
tortoise.up()
tortoise.goto(column, row + 1)
tortoise.stamp()
#############################
def initialize(grid, rows, columns):
'''
Set the center square of the 2d array 'grid' as filled.
Return a map containing all of the empty squares where key is a tuple
with the coords of said square and value is True (value is arbitrary
and not used)
'''
r = int(rows/2 )
c = int(columns/2 )
grid[r][c] = FILLED
empty = {}
for row in range(rows):
for col in range(columns):
if (row, col) != (r,c):
empty[(row, col)] = True
return empty
def neighborhood(grid, row, column):
offsets = [(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)]
count = 0
Rows = len(grid)
Columns = len(grid[0])
for offset in offsets:
r = row + offset[0]
c = column + offset[1]
if (r >= 0 and r < Rows) and (c >= 0 and c < Columns):
if grid[r][c] == FILLED:
count = count + 1
return count
def randomMovement(pos, Rows, Columns):
# for anywhere besides edges (including corners)
# 1 is NORTH
# 2 is NORTHEAST
# 3 is EAST
# 4 is SOUTHEAST
# 5 is SOUTH
# 6 is SOUTHWEST
# 7 is WEST
# 8 is NORTHWEST
row, col = pos
## corners -- 3 options
if row == 0 and col == 0: #top left corner
direction = random.randrange(1,4)
if direction == 1:
newPos = (row, col+1)
elif direction == 2:
newPos = (row+1,col+1)
elif direction == 3:
newPos = (row+1, col)
elif row == Rows - 1 and col == 0: #bottom left corner
direction = random.randrange(1,4)
if direction == 1:
newPos = (row-1, col)
elif direction == 2:
newPos = (row-1, col+1)
elif direction == 3:
newPos = (row, col+1)
elif row == Rows - 1 and col == Columns - 1: #bottom right corner
direction = random.randrange(1,4)
if direction == 1:
newPos = (row, col-1)
elif direction == 2:
newPos = (row-1, col-1)
elif direction == 3:
newPos = (row-1, col)
elif row == 0 and col == Columns - 1: #top right corner
direction = random.randrange(1,4)
if direction == 1:
newPos = (row+1, col)
elif direction == 2:
newPos = (row+1, col-1)
elif direction == 3:
newPos = (row, col-1)
## edges -- 5 options
elif col == Columns - 1: #right edge
direction = random.randrange(1,6)
if direction == 1:
newPos = (row+1, col)
elif direction == 2:
newPos = (row+1, col-1)
elif direction == 3:
newPos = (row, col-1)
elif direction == 4:
newPos = (row-1, col-1)
elif direction == 5:
newPos = (row-1, col)
elif row == Rows - 1: #bottom edge
direction = random.randrange(1,6)
if direction == 1:
newPos = (row, col-1)
elif direction == 2:
newPos = (row-1, col-1)
elif direction == 3:
newPos = (row-1, col)
elif direction == 4:
newPos = (row-1, col+1)
elif direction == 5:
newPos = (row, col+1)
elif row == 0: #top edge
direction = random.randrange(1,6)
if direction == 1:
newPos = (row, col+1)
elif direction == 2:
newPos = (row+1, col+1)
elif direction == 3:
newPos = (row+1, col)
elif direction == 4:
newPos = (row+1, col-1)
elif direction == 5:
newPos = (row, col-1)
elif col == 0: #left edge
direction = random.randrange(1,6)
if direction == 1:
newPos = (row-1, col)
elif direction == 2:
newPos = (row-1, col+1)
elif direction == 3:
newPos = (row, col+1)
elif direction == 4:
newPos = (row+1, col+1)
elif direction == 5:
newPos = (row+1, col)
## anywhere else
else:
direction = random.randrange(1,9)
if direction == 1:
newPos = (row-1, col)
elif direction == 2:
newPos = (row-1, col+1)
elif direction == 3:
newPos = (row, col+1)
elif direction == 4:
newPos = (row+1, col+1)
elif direction == 5:
newPos = (row+1, col)
elif direction == 6:
newPos = (row+1, col-1)
elif direction == 7:
newPos = (row, col-1)
elif direction == 8:
newPos = (row-1, col-1)
return newPos
def distance(pos):
r = int(Rows/2 )
c = int(Columns/2 )
R, C = pos
difX_Sq = (R - r)**2
difY_Sq = (C - c)**2
dist = math.sqrt(difX_Sq + difY_Sq)
return dist
def getSpawnOptions(empty, radius):
'''
Return a dictionary of points that are (radius + 1) from the center
'''
spawnOptions = {}
for item in empty:
if distance(item) == (radius + 1):
spawnOptions[item] = True
return spawnOptions
def DLA(grid, rows, columns, moveLimit, particles):
empty = initialize(grid, rows, columns)
numParts = particles
numUnstuck = 1
partsToIncreaseRadius = 5
movesToStickList = [] #TODO: graph this at the end
tempCounter = 0
# so our logging is there right away, even if it takes a sec to see particles
sys.stdout.write("Particles remaining = %d \r"%(numParts))
sys.stdout.flush()
while numParts > 0:
newGrid = copy.deepcopy(grid)
ELIST = empty
startR, startC = random.choice(list(ELIST.keys()))
newGrid[startR][startC] = FILLED
R = startR
C = startC
stuck = False
counter = 0
while not stuck and counter < moveLimit:
newR, newC = randomMovement((R, C), rows, columns)
# it's possible that random movement tells us to move to a squat that isn't empty
# loop until we've got a square that is
while (newR, newC) not in ELIST:
newR, newC = randomMovement((R, C), rows, columns)
tempCounter += 1
newGrid[R][C] = EMPTY # the particle used to be at this spot
newGrid[newR][newC] = FILLED # the particle moved to this spot
numNeighbors = neighborhood(grid, newR, newC)
if numNeighbors > 0:
stuck = True
del ELIST[(newR, newC)]
ELIST[(R, C)] = True
numParts -= 1
movesToStickList.append(counter)
sys.stdout.write("Particles remaining = %d \r"%(numParts))
sys.stdout.flush()
R, C = newR, newC # new point becomes the current point
grid = newGrid
counter += 1
empty = ELIST
if not stuck:
#print("- - - Unstuck particle #%s. %s remain"%(numUnstuck, numParts))
numUnstuck += 1
newGrid[R][C] = EMPTY
ELIST[(R, C)] = True
grid = newGrid
print("\n\nTempCounter = %d"%(tempCounter))
return grid
def fillGrid(tortoise, grid):
for r in range(len(grid)):
row = grid[r]
for c in range(len(row)):
square = row[c]
if square == FILLED:
drawCircle((r,c), 'blue', tortoise)
else:
drawCircle((r,c), 'white', tortoise)
def writeToFile(grid, fileName):
with open(fileName, 'w') as f:
for row in grid:
for thing in row:
f.write(str(thing))
f.write("\n")
def readFromFile(fileName):
grid = []
with open(fileName, 'r') as f:
for line in f:
row = []
for letter in line.strip():
row.append(int(letter))
grid.append(row)
return grid
def main():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-p", "--particles", help="the number of particles you'd like to run the simulation with", type=int, default=500)
parser.add_argument("-m", "--movelimit", help="a spawned particle will take up to this many moves without sticking before it dies", type=int, default=2000)
parser.add_argument("-r", "--rows", help="the number of rows in the simulation grid", type=int, default=200)
parser.add_argument("-c", "--columns", help="the number of columns in the simulation grid", type=int, default=200)
parser.add_argument("-f", "--file", help="read the grid in from this file, and display it. No simulation will be run.", type=str)
parser.add_argument("-o", "--outfile", help="if running a simulaton, grid will be serialized into this file so you can view it again later", type=str)
parser.add_argument("-s", "--scale", help="pixel scale of each square that's drawn. You may need to adjust this depending on number of rows/columns", type=int, default=8)
parser.add_argument("-g", "--grow", help="continue generating on top of this grid", type=str)
args = parser.parse_args()
# TODO: make program infer size of grid you passed in and use those dimensions
Rows = args.rows
Columns = args.columns
scale = args.scale
if args.file is not None:
# user specified a file. We read the grid in from there
grid = readFromFile(args.file)
else:
# run the simulation
numParticles = args.particles
moveLimit = args.movelimit
# either init with an emptygrid, or one we'd like to grow
if args.grow is not None:
grid = readFromFile(args.grow)
else:
grid = emptyGrid(Rows, Columns)
print("---- Starting DLA simulation with %s particles ----"%(numParticles))
start_time = time.time()
grid = DLA(grid, Rows, Columns, moveLimit, numParticles)
end_time = time.time()
print("Simulation for: %s particles with move limit: %s took %s seconds"%(numParticles, moveLimit, int(end_time - start_time)))
if args.outfile is not None:
outFile = args.outfile
print("Writing grid out to file: %s"%(outFile))
writeToFile(grid, outFile)
george = turtle.Turtle()
screen = george.getscreen()
screen.setup(Columns * scale + 20, Rows * scale + 20) # page 707
screen.setworldcoordinates(0, 0, Columns, Rows)
screen.tracer(100)
george.hideturtle()
createCircles(screen, ['blue', 'white'], scale)
drawGrid(Rows, Columns, george)
fillGrid(george, grid)
screen.update()
x = input("Press any button to quit . . .")
main()