Start developing heat evolution

This commit is contained in:
Aaron Fenyes 2019-09-29 01:23:02 +02:00
parent 9973abad78
commit 0ac76265a0

View File

@ -55,8 +55,8 @@ clock = pygame.time.Clock()
# --- copied from firestar.py # --- copied from firestar.py
b = phue.Bridge('172.18.130.12') ##b = phue.Bridge('172.18.130.12')
##b = None b = None
Nedges = 30; Nedges = 30;
edgecode = [None] * Nedges; edgecode = [None] * Nedges;
@ -87,17 +87,22 @@ def set_light_hsv(e, color, hit_bridge):
# === interaction parameters === # === interaction parameters ===
frame_rate = 2 frame_rate = 24
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]
@ -108,11 +113,13 @@ change_threshold = 0.05
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,6 +135,15 @@ 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):
litness = 1 - exp(-2*i)
if abs(litness - last_litness[e]) > change_threshold:
hit_bridge = True
last_litness[e] = litness
else:
hit_bridge = False
set_light_hsv(e, (0.167*last_litness[e], 1 - 0.5*last_litness[e]*last_litness[e], last_litness[e]), hit_bridge)
def set_wave_light(e, i): def set_wave_light(e, i):
litness = 1 - exp(-2*i*i) litness = 1 - exp(-2*i*i)
hit_bridge = abs(litness - last_litness[e]) > change_threshold hit_bridge = abs(litness - last_litness[e]) > change_threshold
@ -135,6 +151,38 @@ def set_wave_light(e, i):
if hit_bridge: if hit_bridge:
last_litness[e] = litness last_litness[e] = litness
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): def show_data(screen):
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))
text = font.render('energy', True, (255, 255, 255)) text = font.render('energy', True, (255, 255, 255))
@ -158,22 +206,8 @@ if __name__ == '__main__':
to_light = 0 to_light = 0
while True: while True:
# handle events # handle events
for event in pygame.event.get(): ##handle_wave_events()
if event.type == pygame.KEYDOWN: handle_heat_events()
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()
# clear screen # clear screen
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
@ -183,11 +217,12 @@ if __name__ == '__main__':
# show state # show state
for e in range(30): 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 # evolve state
wave_evolution() ##wave_evolution()
##heat_evolution() heat_evolution()
# step # step
pygame.display.flip() pygame.display.flip()