Merge fival version of branch 'interactions'

This commit is contained in:
Glen Whitney 2019-09-29 10:59:21 -04:00
commit b8d86d501e

View File

@ -10,7 +10,7 @@ import phue, pygame, colorsys
viewsize = 600 viewsize = 600
shrink = 0.9 shrink = 0.9
vertices = [(viewsize*(1 + shrink*x)/2, viewsize*(1 + shrink*y)/2) for x, y in vertices = [(viewsize*(1 + shrink*x)/2, viewsize*(1 + shrink*y)/2) for x, y in
[(0, 0)] + [(cos(-n*pi/5), sin(-n*pi/5)) for n in range(10)] + [(0, 0)] [(0, 0)] + [(sin(n*pi/5), -cos(n*pi/5)) for n in range(10)] + [(0, 0)]
] ]
# edges as vertex pairs. FireStar will rest on vertices 6 (A), 5 (B), 7 (C) # edges as vertex pairs. FireStar will rest on vertices 6 (A), 5 (B), 7 (C)
@ -18,7 +18,7 @@ vertex_adj = [
(0, 1), (0, 3), (0, 5), (0, 7), (0, 9), (0, 1), (0, 3), (0, 5), (0, 7), (0, 9),
(2, 8), (2, 6), (6, 10), (10, 4), (4, 8), (2, 8), (2, 6), (6, 10), (10, 4), (4, 8),
(2, 9), (2, 11), (2, 5), (2, 9), (2, 11), (2, 5),
(4, 1), (4, 9), (4, 7), (4, 1), (4, 11), (4, 7),
(6, 3), (6, 11), (6, 9), (6, 3), (6, 11), (6, 9),
(8, 5), (8, 11), (8, 1), (8, 5), (8, 11), (8, 1),
(10, 7), (10, 11), (10, 3), (10, 7), (10, 11), (10, 3),
@ -72,7 +72,10 @@ lasthue = 65535
fullbright = 254 fullbright = 254
fullsat = 254 fullsat = 254
# --- control methods # --- control methods and diagnostics
updates = 0
new_updates = 0
def set_light_hsv(e, color, hit_bridge): def set_light_hsv(e, color, hit_bridge):
command = { command = {
@ -80,39 +83,86 @@ def set_light_hsv(e, color, hit_bridge):
'bri' : int(color[2]*fullbright), 'bri' : int(color[2]*fullbright),
'sat' : int(color[1]*fullsat) 'sat' : int(color[1]*fullsat)
} }
if hit_bridge and b != None: if hit_bridge:
b.set_light(edges[e].light_id, command) if b != None:
b.set_light(edges[e].light_id, command)
global new_updates
new_updates += 1
rgb_color = tuple(255*c for c in colorsys.hsv_to_rgb(*color)) rgb_color = tuple(255*c for c in colorsys.hsv_to_rgb(*color))
pygame.draw.aaline(screen, rgb_color, vertices[vertex_adj[e][0]], vertices[vertex_adj[e][1]]) pygame.draw.aaline(screen, rgb_color, vertices[vertex_adj[e][0]], vertices[vertex_adj[e][1]])
# === messages and mode selection ===
mode = 0
# data labels
updates_label = font.render('updates', True, (255, 255, 255))
energy_label = font.render('energy', True, (255, 255, 255))
position_label = [
font.render('temp[0]', True, (255, 255, 255)),
font.render('charge[0]', True, (255, 255, 255))
]
# instructions
mode_inds = [
[
font.render('[d] heat', True, (255, 255, 255)),
font.render('[f] wave', True, (150, 150, 150))
],
[
font.render('[d] heat', True, (150, 150, 150)),
font.render('[f] wave', True, (255, 255, 255))
]
]
bop_instr = [
font.render('press [j] to bop', True, (255, 255, 255)),
font.render('press [j] or [k] to bop', True, (255, 255, 255))
]
drain_instr = font.render('hold [space] to drain', True, (255, 255, 255))
# set up background
background = pygame.Surface(screen.get_size()).convert()
background.fill((32, 32, 32))
background.blit(drain_instr, (340, 60 + viewsize))
# === interaction parameters === # === interaction parameters ===
frame_rate = 2 # these frame rate and threshold settings are untested, but the exponential
# moving average update frequency suggests they should be just barely in bounds
frame_rate = 10
ema_weight = 2/(frame_rate+1)
inductance = 0.001 inductance = 0.001
therm_conduct = 0.001 therm_conduct = 0.01
drain = False drain = False
drain_rate = 0.1 drain_rate = 0.1
# === phase space === # === phase space ===
# heat equation
temp = 30*[0]
new_temp = 30*[0]
# wave equation
charge = 12*[0] charge = 12*[0]
current = 30*[0] current = 30*[0]
last_litness = 30*[float("inf")] litness = 30*[float("inf")]
change_threshold = 0.05 change_threshold = 0.15
# === main loop === # === main loop ===
def heat_evolution(): def heat_evolution():
for e, f in cyc_edge_adj: for e, f in cyc_edge_adj:
flow = therm_conduct*(current[e] - current[f]) flow = therm_conduct*(temp[e] - temp[f])
current[e] -= flow new_temp[e] -= flow
current[f] += flow new_temp[f] += flow
for e in range(30):
if drain: if drain:
current[v] *= 1 - drain_rate new_temp[e] *= 1 - drain_rate
temp[e] = max(new_temp[e], 0)
def wave_evolution(): def wave_evolution():
# use verlet integration, first updating the currents and then using the new # use verlet integration, first updating the currents and then using the new
@ -128,66 +178,115 @@ def wave_evolution():
for v in range(12): for v in range(12):
charge[v] *= 1 - drain_rate charge[v] *= 1 - drain_rate
def set_heat_light(e, i):
new_litness = 1 - exp(-2*i)
if abs(new_litness - litness[e]) > change_threshold:
hit_bridge = True
litness[e] = new_litness
else:
hit_bridge = False
set_light_hsv(e, (0.167*litness[e], 1 - 0.5*litness[e]*litness[e], litness[e]), hit_bridge)
def set_wave_light(e, i): def set_wave_light(e, i):
litness = 1 - exp(-2*i*i) new_litness = 1 - exp(-2*i*i)
hit_bridge = abs(litness - last_litness[e]) > change_threshold if abs(new_litness - litness[e]) > change_threshold:
set_light_hsv(e, (0.45 + litness*0.25, 1 - 0.5*litness*litness, litness), hit_bridge) hit_bridge = True
if hit_bridge: litness[e] = new_litness
last_litness[e] = litness else:
hit_bridge = False
set_light_hsv(e, (0.45 + litness[e]*0.25, 1 - 0.5*litness[e]*litness[e], litness[e]), hit_bridge)
def handle_heat_events():
global drain, mode
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_j:
new_temp[0] += 10
elif event.key == pygame.K_SPACE:
drain = True
elif event.key == pygame.K_f:
mode = 1
elif event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
drain = False
elif event.type == pygame.QUIT:
quit()
def handle_wave_events():
global drain, mode
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_j:
charge[0] += 100
charge[11] -= 100
elif event.key == pygame.K_k:
charge[5] += 100
charge[7] -= 100
elif event.key == pygame.K_SPACE:
drain = True
elif event.key == pygame.K_d:
mode = 0
elif event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
drain = False
elif event.type == pygame.QUIT:
quit()
def show_data(screen): def show_data(screen):
energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current)) # light updates
text = font.render('energy', True, (255, 255, 255)) global updates, new_updates
screen.blit(text, (20, viewsize)) updates = ema_weight*new_updates + (1 - ema_weight)*updates
text = font.render(str(energy), True, (255, 255, 255)) new_updates = 0
screen.blit(text, (120, viewsize)) updates_value = font.render(str(frame_rate * updates), True, (255, 255, 255))
text = font.render('charge[0]', True, (255, 255, 255)) screen.blit(updates_label, (20, viewsize))
screen.blit(text, (20, 30 + viewsize)) screen.blit(updates_value, (120, viewsize))
text = font.render(str(charge[0]), True, (255, 255, 255))
screen.blit(text, (120, 30 + viewsize)) # energy
if mode == 0:
energy = sum(temp)
elif mode == 1:
energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current))
energy_value = font.render(str(energy), True, (255, 255, 255))
screen.blit(energy_label, (20, 30 + viewsize))
screen.blit(energy_value, (120, 30 + viewsize))
# a position
##position_value = None
if mode == 0:
position_value = font.render(str(temp[0]), True, (255, 255, 255))
elif mode == 1:
position_value = font.render(str(charge[0]), True, (255, 255, 255))
screen.blit(position_label[mode], (20, 60 + viewsize))
screen.blit(position_value, (120, 60 + viewsize))
# instructions
screen.blit(mode_inds[mode][0], (340, viewsize))
screen.blit(mode_inds[mode][1], (420, viewsize))
screen.blit(bop_instr[mode], (340, 30 + viewsize))
if __name__ == '__main__': if __name__ == '__main__':
# set up background
background = pygame.Surface(screen.get_size()).convert()
background.fill((32, 32, 32))
text = font.render('press [j] or [k] to pluck', True, (255, 255, 255))
background.blit(text, (340, viewsize))
text = font.render('hold [space] to drain', True, (255, 255, 255))
background.blit(text, (340, 30 + viewsize))
to_light = 0
while True: while True:
# handle events # handle events
for event in pygame.event.get(): if mode == 0:
if event.type == pygame.KEYDOWN: handle_heat_events()
if event.key == pygame.K_j: elif mode == 1:
charge[0] += 100 handle_wave_events()
charge[11] -= 100
elif event.key == pygame.K_k:
charge[5] += 100
charge[7] -= 100
elif event.key == pygame.K_SPACE:
drain = True
pygame.draw.aaline(screen, (255, 255, 255), (0, 0), (viewsize, viewsize))
elif event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
drain = False
elif event.type == pygame.QUIT:
quit()
# clear screen # clear screen
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# show data # show data and instructions
show_data(screen) show_data(screen)
# show state # show state and evolve state
for e in range(30): if mode == 0:
set_wave_light(e, current[e]) for e in range(30):
set_heat_light(e, temp[e])
# evolve state heat_evolution()
wave_evolution() elif mode == 1:
##heat_evolution() for e in range(30):
set_wave_light(e, current[e])
wave_evolution()
# step # step
pygame.display.flip() pygame.display.flip()