Merge branch 'heat' into interactions

Wave code hasn't been re-tested yet.
This commit is contained in:
Aaron Fenyes 2019-09-29 15:36:12 +02:00
commit a52ed7a90e

View File

@ -100,13 +100,18 @@ ema_weight = 2/(frame_rate+1)
inductance = 0.001
therm_conduct = 0.001
therm_conduct = 0.01
drain = False
drain_rate = 0.1
# === phase space ===
# heat equation
temp = 30*[0]
new_temp = 30*[0]
# wave equation
charge = 12*[0]
current = 30*[0]
@ -117,11 +122,13 @@ change_threshold = 0.15
def heat_evolution():
for e, f in cyc_edge_adj:
flow = therm_conduct*(current[e] - current[f])
current[e] -= flow
current[f] += flow
flow = therm_conduct*(temp[e] - temp[f])
new_temp[e] -= flow
new_temp[f] += flow
for e in range(30):
if drain:
current[v] *= 1 - drain_rate
new_temp[e] *= 1 - drain_rate
temp[e] = max(new_temp[e], 0)
def wave_evolution():
# use verlet integration, first updating the currents and then using the new
@ -137,6 +144,15 @@ def wave_evolution():
for v in range(12):
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):
new_litness = 1 - exp(-2*i*i)
if abs(new_litness - litness[e]) > change_threshold:
@ -146,6 +162,38 @@ def set_wave_light(e, i):
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
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.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
drain = False
elif event.type == pygame.QUIT:
quit()
def handle_wave_events():
global drain
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.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
drain = False
elif event.type == pygame.QUIT:
quit()
def show_data(screen):
# light updates
global updates, new_updates
@ -157,23 +205,27 @@ def show_data(screen):
screen.blit(text, (120, viewsize))
# energy
energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current))
##energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current))
energy = sum(temp)
text = font.render('energy', True, (255, 255, 255))
screen.blit(text, (20, 30 + viewsize))
text = font.render(str(energy), True, (255, 255, 255))
screen.blit(text, (120, 30 + viewsize))
# charge[0]
text = font.render('charge[0]', True, (255, 255, 255))
##text = font.render('charge[0]', True, (255, 255, 255))
text = font.render('temp[0]', True, (255, 255, 255))
screen.blit(text, (20, 60 + viewsize))
text = font.render(str(charge[0]), True, (255, 255, 255))
##text = font.render(str(charge[0]), True, (255, 255, 255))
text = font.render(str(temp[0]), True, (255, 255, 255))
screen.blit(text, (120, 60 + viewsize))
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))
##text = font.render('press [j] or [k] to pluck', True, (255, 255, 255))
text = font.render('press [j] 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))
@ -181,22 +233,8 @@ if __name__ == '__main__':
to_light = 0
while True:
# handle events
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
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()
##handle_wave_events()
handle_heat_events()
# clear screen
screen.blit(background, (0, 0))
@ -206,11 +244,12 @@ if __name__ == '__main__':
# show state
for e in range(30):
set_wave_light(e, current[e])
##set_wave_light(e, current[e])
set_heat_light(e, temp[e])
# evolve state
wave_evolution()
##heat_evolution()
##wave_evolution()
heat_evolution()
# step
pygame.display.flip()