/
Main.py
699 lines (626 loc) · 29.5 KB
/
Main.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#from __future__ import print_function
#try:
#import surface_crns
#except ImportError:
#import sys
#sys.path.append("./")
#import readers as readers
import readers.ReadInitialState
import readers.ColorMap
import readers.manifest_readers
import readers.ordered_dict
import readers.statements
import os
from simulators.synchronous_simulator import SynchronousSimulator
from views.time_display import TimeDisplay
from views.text_display import TextDisplay
from views.grid_display import SquareGridDisplay, HexGridDisplay
from views.legend_display import LegendDisplay
from simulators.event_history import EventHistory
from pygbutton import PygButton
from options.option_processor import SynchronousCellularAutomataOptionParser
import cProfile
import optparse
import sys
from time import process_time
import pygame
import pygame.locals as pygl
pygame.display.init()
pygame.font.init()
PROFILE = False
WHITE = (255,255,255)
BLACK = (0, 0, 0)
#time_font = pygame.font.SysFont('monospace', 24)
time_font = pygame.font.SysFont(pygame.font.get_default_font(), 24)
TEXT_X_BUFFER = 0#10
TEXT_Y_BUFFER = 0#5
TEXT_HEIGHT = time_font.get_linesize() + 2 * TEXT_Y_BUFFER
button_width = 60
button_height = 30
button_buffer = 0#5
MIN_GRID_WIDTH = 6 * button_width + 10 * button_buffer
##############
# Exit Codes #
##############
FINISHED_CLEAN = 0
RUNNING = -1
MAX_TIME = 1
MAX_PIXELS = 2
current_state = RUNNING
###########################
# MOVIE CAPTURE CONSTANTS #
###########################
MOVIE_SUBDIRECTORY = "movies"
DEBUG_SUBDIRECTORY = "debug"
FRAME_SUBDIRECTORY = "frames"
CUTOFF_TIME = 600 # Cut off simulation at 10 minutes
CUTOFF_SIZE = 10000 * 500000 # Cut off simulation at roughly 1000 frames for
# a typical image size.
#############
# VARIABLES #
#############
time = 0
simulation = None
def main():
available_options = optparse.OptionParser()
available_options.add_option("-m", '--manifest', action = "store",
type = 'string', dest = 'manifest_filename',
help = "points to a manifest file to simulate")
available_options.add_option("-v", "--version",
action="store_true", dest="querying_version", default=False,
help="show version and exit")
(command_line_options, args) = available_options.parse_args(sys.argv)
#if command_line_options.querying_version:
#print(f"surface_crns {surface_crns.__version__}")
#return
manifest_filename = command_line_options.manifest_filename
if not manifest_filename:
raise Exception("Manifest file required (use the flag -m <filename>, " +
"where <filename> is the name of your manifest file)")
simulate_flooding(manifest_filename)
def simulate_flooding(manifest_filename, display_class = None,
init_state = None, height = None):
'''
Runs a simulation, and displays it in a GUI window OR saves all frames
as PNG images.
Normal operation is to read all options from a manifest file, given by
manifest_filename. If you want to use a custom surface geometry (anything
other than a square or hex grid), you'll need to supply your own initial
state (whatever your display object will use, but must be an iterable and
contain surface_crns.base.Node objects) and a class that can display your
state (should subclass surface_crns.views.grid_display.SurfaceDisplay),
which you should pass as "init_state" and "DisplayClass", respectively.
'''
################################
# READ MANIFEST AND INITIALIZE #
################################
# Parse the manifest
print("Reading information from manifest file " + manifest_filename + "...")
manifest_options = \
readers.manifest_readers.read_manifest(manifest_filename)
opts = SynchronousCellularAutomataOptionParser(manifest_options)
print(" Done.")
if opts.capture_directory != None:
from signal import signal, SIGPIPE, SIG_DFL
import subprocess as sp
base_dir = opts.capture_directory
MOVIE_DIRECTORY = base_dir
DEBUG_DIRECTORY = os.path.join(base_dir, DEBUG_SUBDIRECTORY)
FRAME_DIRECTORY = os.path.join(base_dir, FRAME_SUBDIRECTORY)
for d in [base_dir, MOVIE_DIRECTORY, DEBUG_DIRECTORY, FRAME_DIRECTORY]:
if not os.path.isdir(d):
os.mkdir(d)
os.environ["SDL_VIDEODRIVER"] = "dummy"
print("SDL_VIDEODRIVER set to 'dummy'")
else:
FRAME_DIRECTORY = ""
# Initialize simulation
if init_state:
grid = init_state
else:
if opts.grid is None:
raise Exception("Initial grid state required.")
grid = opts.grid
if height:
#print("there is already a height")
height_grid = height
else:
#print("there is NOT already a height, need to go to options")
if opts.height is None:
raise Exception("Initial grid state required.")
#print("went to options")
height_grid = opts.height
#print(height_grid)
#print("done print AFTER going to options")
simulation = SynchronousSimulator(surface= grid,
heightgrid = height_grid,
seed= opts.rng_seed,
simulation_duration= opts.max_duration)
simulation.init_wall_time = process_time()
seed = simulation.seed
time = simulation.time
event_history = EventHistory()
if not opts.capture_directory is None:
simulation.pixels_saved = 0
simulation.frame_number = 0
simulation.capture_time = 0
################
# PYGAME SETUP #
################
print("Beginning Pygame setup...")
if opts.grid_type == 'parallel_emulated':
from views.grid_display \
import ParallelEmulatedSquareGridDisplay
grid_display = ParallelEmulatedSquareGridDisplay(grid = grid,
colormap = opts.COLORMAP,
emulation_colormap = opts.emulation_colormap,
horizontal_buffer = opts.horizontal_buffer,
vertical_buffer = opts.vertical_buffer,
cell_height = opts.cell_height,
cell_width = opts.cell_width,
representative_cell_x = opts.representative_cell_x,
representative_cell_y = opts.representative_cell_y,
min_x = MIN_GRID_WIDTH,
min_y = 0,
pixels_per_node = opts.pixels_per_node,
display_text = opts.display_text)
elif opts.grid_type == 'standard':
if display_class:
DisplayClass = display_class
elif opts.grid_type == "standard" and opts.surface_geometry == "square":
DisplayClass = SquareGridDisplay
elif opts.grid_type == "standard" and opts.surface_geometry == "hex":
DisplayClass = HexGridDisplay
grid_display = DisplayClass(grid = grid,
colormap = opts.COLORMAP,
min_x = MIN_GRID_WIDTH,
min_y = 0,
pixels_per_node = opts.pixels_per_node,
display_text = opts.display_text)
else:
raise Exception("Unrecognized grid type '" + opts.grid_type + "'")
legend_display = LegendDisplay(colormap = opts.COLORMAP)
# Width only requires legend and grid sizes to calculate
display_width = grid_display.display_width + legend_display.display_width
# Width used to calculate time label and button placements
time_display = TimeDisplay(display_width)
button_y = time_display.display_height + grid_display.display_height+1
# max(legend_display.display_height, grid_display.display_height) + 1
#(int(display_width/2) - (button_width + button_buffer), button_y,
play_back_button = PygButton(rect =
(legend_display.display_width + button_buffer, button_y,
button_width, button_height),
caption = '<<')
step_back_button = PygButton(rect =
(play_back_button.rect.right + button_buffer, button_y,
button_width, button_height),
caption = '< (1)')
pause_button = PygButton(rect =
(step_back_button.rect.right + button_buffer, button_y,
button_width, button_height),
caption = 'Pause')
step_button = PygButton(rect =
(pause_button.rect.right + button_buffer, button_y,
button_width, button_height),
caption = '(1) >')
play_button = PygButton(rect =
(step_button.rect.right + button_buffer, button_y,
button_width, button_height),
caption = '>>')
clip_button = PygButton(rect =
(play_button.rect.right + 4*button_buffer, button_y,
button_width * 1.1, button_height),
caption = 'Uncache')
display_height = max(legend_display.display_height + \
2*legend_display.VERTICAL_BUFFER + time_display.display_height,
button_y + button_height + 2*button_buffer)
if opts.debug:
print("Initializing display of size " + str(display_width) + ", " +
str(display_height) + ".")
display_surface = pygame.display.set_mode((display_width,
display_height), 0, 32)
simulation.display_surface = display_surface
# except:
# display_surface = pygame.display.set_mode((display_width,
# display_height))
if opts.debug:
print("Display initialized. Setting caption.")
pygame.display.set_caption(f'Surface CRN Simulator (Seed: {seed})')
if opts.debug:
print("Caption set, initializing clock.")
fpsClock = pygame.time.Clock()
if opts.debug:
print("Clock initialized. Filling display with white.")
# Initial render
display_surface.fill(WHITE)
print("Pygame setup done, first render attempted.")
# Make the options menu.
#opts_menu = MainOptionMenu()
#opts_menu.update()
time_display.render(display_surface, x_pos = 0,
y_pos = 0)
legend_display.render(display_surface, x_pos = 0,
y_pos = time_display.y_pos +
time_display.display_height)
grid_display.render(display_surface, x_pos = legend_display.display_width,
y_pos = time_display.y_pos +
time_display.display_height)
if opts.saving_movie:
simulation.display_surface_size = display_height * display_width
else:
play_back_button.draw(display_surface)
step_back_button.draw(display_surface)
pause_button.draw(display_surface)
step_button.draw(display_surface)
play_button.draw(display_surface)
clip_button.draw(display_surface)
pygame.display.flip()
update_display(opts, simulation, FRAME_DIRECTORY)
if "SDL_VIDEODRIVER" in os.environ:
real_time = os.environ["SDL_VIDEODRIVER"] != "dummy"
else:
real_time = True
# State variables for simulation
next_reaction_time = 0
prev_reaction_time = 0
next_reaction = None
prev_reaction = None
running = True #TEMPORARY FIX ME!!!
first_frame = True
last_frame = False
running_backward = False
# Resource limit flags
terminate = False
termination_string = ""
print("Beginning simulation....")
# Iterate through events
while True:
# Check for interface events
for event in pygame.event.get():
if 'click' in play_back_button.handleEvent(event):
running = True
running_backward = True
last_frame = False
if 'click' in step_back_button.handleEvent(event):
running = False
last_frame = False
if event_history.at_beginning():
time = 0
time_display.time = 0
time_display.render(display_surface, x_pos = 0, y_pos = 0)
pygame.display.update()
else:
prev_reaction = event_history.previous_event()
event_history.increment_event(-1)
if not event_history:
continue
prev_reaction_rule = prev_reaction.rule
for i in range(len(prev_reaction.participants)):
cell = prev_reaction.participants[i]
state = prev_reaction_rule.inputs[i]
cell.state = state
display_next_event(prev_reaction, grid_display)
if event_history.at_beginning():
time = 0
else:
# Note that this is NOT the same as the time from the
# "previous event" that we're currently processing --
# it's actually the time of the event *before* the one
# we just undid.
time = event_history.previous_event().time
time_display.time = time
time_display.render(display_surface, x_pos = 0, y_pos = 0)
pygame.display.update()
if 'click' in pause_button.handleEvent(event):
running = False
if 'click' in step_button.handleEvent(event):
running = False
first_frame = False
# Process a single reaction
reading_history = False
if not event_history.at_end():
reading_history = True
next_reaction = event_history.next_event()
event_history.increment_event(1)
next_reaction_rule = next_reaction.rule
for i in range(len(next_reaction.participants)):
cell = next_reaction.participants[i]
state = next_reaction_rule.outputs[i]
cell.state = state
if not next_reaction:
next_reaction = simulation.process_next_reaction()
if not reading_history:
event_history.add_event(next_reaction)
event_history.increment_event(1)
if next_reaction:
next_reaction_time = next_reaction.time
display_next_event(next_reaction, grid_display)
time = next_reaction_time
time_display.time = time
time_display.render(display_surface, x_pos = 0, y_pos = 0)
pygame.display.update()
next_reaction = None
if opts.debug:
print("State after update: " + str(grid))
if 'click' in play_button.handleEvent(event):
running = True
running_backward = False
first_frame = False
if 'click' in clip_button.handleEvent(event):
event_history.clip()
simulation.time = time
simulation.reset()
for rxn in list(simulation.event_queue.queue):
print(rxn)
if event.type == pygl.QUIT:
if opts.saving_movie:
movie_file.close()
current_state = FINISHED_CLEAN
cleanup_and_exit(simulation, current_state)
# Don't do anything if paused.
if not running:
pygame.display.update()
continue
# Update time
if opts.debug:
print(f"Updating time: time = {time}, running_backward = "
f"{running_backward}, first_frame = {first_frame}, "
f"last_frame = {last_frame}")
if running_backward and not first_frame:
#prev_reaction_time = time
time -= opts.speedup_factor * 1./opts.fps
last_frame = False
elif not running_backward and not last_frame:
#next_reaction_time = time
time += opts.speedup_factor * 1./opts.fps
first_frame = False
if opts.debug:
print(f"Updating time to {time}")
time_display.time = time
time_display.render(display_surface, x_pos = 0,
y_pos = 0)
# Process any simulation events that have happened since the last tick.
if opts.debug:
print("Checking for new events...")
if running_backward and not first_frame:
if opts.debug and not event_history.at_beginning():
print(f"While running backwards, checking if there are any "
f"events: time = {time}, previous event time = "
f"{event_history.previous_event().time}")
while not event_history.at_beginning() and \
event_history.previous_event().time > time:
prev_reaction = event_history.previous_event()
if event_history.at_beginning():
first_frame = True
event_history.increment_event(-1)
# if opts.debug:
# print("While running backwards, undoing reaction: "
# f"{prev_reaction}")
for i in range(len(prev_reaction.participants)):
cell = prev_reaction.participants[i]
state = prev_reaction.rule.inputs[i]
cell.state = state
next_reaction_time = prev_reaction_time
prev_reaction_time = prev_reaction.time if prev_reaction \
else 0
if opts.debug:
print("Displaying a new event")
display_next_event(prev_reaction, grid_display)
if opts.debug and not event_history.at_beginning():
print(f"While running backwards, checking if there are any "
f"events: time = {time}, previous event time = "
f"{event_history.previous_event().time}")
elif not running_backward and not last_frame:
while (not event_history.at_end() or not simulation.done()) \
and next_reaction_time < time:
if event_history.at_end():
next_reaction = simulation.process_next_reaction()
if next_reaction:
event_history.add_event(next_reaction)
event_history.increment_event(1)
else:
next_reaction = event_history.next_event()
event_history.increment_event(1)
for i in range(len(next_reaction.participants)):
cell = next_reaction.participants[i]
state = next_reaction.rule.outputs[i]
cell.state = state
prev_reaction_time = next_reaction_time
next_reaction_time = next_reaction.time if next_reaction \
else opts.max_duration + 1
if opts.debug:
print("Displaying a new event")
display_next_event(next_reaction, grid_display)
# Render updates and make the next clock tick.
if opts.debug:
print("Updating display.")
update_display(opts, simulation, FRAME_DIRECTORY)
if real_time:
fpsClock.tick(opts.fps)
# Movie-capturing termination conditions
if not opts.capture_directory is None:
if simulation.pixels_saved > CUTOFF_SIZE:
termination_string = "Simulation terminated after " + \
str(simulation.pixels_saved) + \
" pixels saved (~" + \
str(CUTOFF_SIZE/10000000) +" Mb)."
current_state = MAX_PIXELS
terminate = True
# Check the timer. If it's been more than an hour, terminate.
if process_time() - simulation.init_wall_time > CUTOFF_TIME:
termination_string = "Simulation cut off at max " \
"processing time"
current_state = MAX_TIME
terminate = True
if simulation.done() or time > opts.max_duration:
termination_string = "Simulation finished."
current_state = FINISHED_CLEAN
terminate = True
if terminate:
display_surface = simulation.display_surface
width = display_surface.get_size()[0]
text_display = TextDisplay(width)
text_display.text = termination_string
text_display.render(display_surface, x_pos = 0, y_pos = 0)
frame_filename = os.path.join(FRAME_DIRECTORY,
f"{opts.movie_title}_{simulation.frame_number}.png")
if opts.debug:
print("Saving final frame at: " + frame_filename)
pygame.image.save(display_surface, frame_filename)
# Use ffmpeg to convert images to movie.
if os.name == 'nt':
ffmpeg_name = 'ffmpeg.exe'
elif os.name == 'posix':
# ffmpeg_name = 'ffmpeg'
name_found = False
for possible_name in ['/usr/local/bin/ffmpeg',
'/usr/bin/ffmpeg']:
if os.path.isfile(possible_name):
ffmpeg_name = possible_name # EW: not finding it?
name_found = True
break
if not name_found:
raise Exception("Could not find executable ffmpeg in"
" any of the expected locations!")
else:
raise Exception("Unexpected OS name '" + os.name + "'")
signal(SIGPIPE, SIG_DFL)
# width = display_surface.get_width()
# height = display_surface.get_height()
movie_filename = os.path.join(".", MOVIE_DIRECTORY,
opts.movie_title + ".mp4")
if opts.debug:
print("Writing movie to file " + movie_filename +
"\n")
command = [ffmpeg_name,
'-y', # Overwrite output file
#'-framerate', str(opts.fps),
'-start_number', '1', # EW: might it default to
# starting with 0?
'-i', os.path.join(FRAME_DIRECTORY,
opts.movie_title + "_%d.png"),
# Try to use better-than-default decoder
'-vcodec', 'h264',
# Need this for Quicktime to be able to read it
'-pix_fmt', 'yuv420p',
# Set a higher-than-default bitrate
'-crf', '18',
'-an', #no audio
# Width and height need to be divisible by 2.
# Round up if necessary.
'-vf', 'pad=ceil(iw/2)*2:ceil(ih/2)*2',
movie_filename
]
print("Calling ffmpeg with: " + str(command))
print("And right now the current dir is " + os.getcwd())
print("opts.capture_directory = " + opts.capture_directory)
if opts.debug:
print("Writing movie with command:\n")
print("\t" + str(command) + "\n")
debug_output_stream = open(os.path.join(opts.capture_directory,
"debug",
"ffmpeg_debug.dbg"),'w')
proc = sp.Popen(command,
stdout = debug_output_stream,
stderr = sp.STDOUT)
proc.communicate()
if opts.debug:
print("Finished ffmpeg call.")
cleanup_and_exit(simulation, current_state)
# Live termination conditions
if opts.debug:
print("Checking for simulation completion...")
if (event_history.at_end() and running and not running_backward and \
(simulation.done() or time > opts.max_duration)) \
or terminate:
if opts.debug:
print("Done! Cleaning up now.")
last_frame = True
running = False
# Set the time to final time when done.
time = opts.max_duration
time_display.time = time
time_display.render(display_surface, x_pos = 0,
y_pos = 0)#opts_menu.display_height)
if next_reaction:
display_next_event(next_reaction, grid_display)
update_display(opts, simulation, FRAME_DIRECTORY)
if opts.debug:
print("Simulation state at final time " + \
str(opts.max_duration) + ":")
print(str(grid))
if event_history.at_beginning() or time == 0:
first_frame = True
#end def main()
def update_display(opts, simulation, FRAME_DIRECTORY = None):
if opts.capture_directory is None:
pygame.display.update()
pygame.display.flip()
else:
if FRAME_DIRECTORY is None:
raise Exception("FRAME_DIRECTORY should be set if a capture" +
" directory is set.")
try:
capture_time = simulation.capture_time
except AttributeError:
simulation.capture_time = 0
capture_time = 0
try:
frame_number = simulation.frame_number
except AttributeError:
simulation.frame_number = 1
frame_number = 1
if simulation.time >= capture_time:
if opts.debug:
print("movie title is: " + str(opts.movie_title))
frame_filename = os.path.join(FRAME_DIRECTORY, opts.movie_title
+ "_" + str(frame_number) +
".png")
if opts.debug:
print("Saving frame at: " + frame_filename)
pygame.image.save(simulation.display_surface, frame_filename)
# Determine next capture time
simulation.capture_time = capture_time + 1./opts.capture_rate
simulation.frame_number = frame_number + 1
# Add to space used.
try:
simulation.pixels_saved += simulation.display_surface_size
except AttributeError:
simulation.pixels_saved = simulation.display_surface_size
def display_next_event(next_reaction, grid_display):
# Update the display for a reaction. Reaction will be an Event object (see
# surface_crns.simulators.py).
DEBUG = False
if DEBUG:
print("Moving to next reaction:")
if not next_reaction:
if DEBUG:
print("No reaction returned from event queue. Finished")
return -1
next_reaction_time = next_reaction.time
if DEBUG:
print("Updating display based on event " +
str(next_reaction))
# Display any changes made
participants = next_reaction.participants
inputs = next_reaction.rule.inputs
outputs = next_reaction.rule.outputs
# Update reactants (if changed)
for i in range(len(participants)):
if inputs[i] != outputs[i]:
grid_display.update_node(participants[i])
elif DEBUG:
print("Input " + str(i+1) + " and output " + str(i+1) + " match " +
"for rule " + str(next_reaction.rule) + "\ncell " +
str(participants[0].position) + " not updated.")
# Display current state to stdout.
if DEBUG:
print("Simulation state at time " + str(next_reaction_time) + \
":\n" + str(grid_display.grid))
return next_reaction_time
def cleanup_and_exit(simulation, current_state):
pygame.quit()
print("Program terminated before simulation comlete.")
print("Simulation state at termination (T = " + str(simulation.time) + "):")
print(str(simulation.surface))
sys.exit(current_state)