Separate things into functions a bit

This commit is contained in:
Aaron Fenyes 2019-09-28 21:18:05 +02:00
parent 36077fd872
commit 217bdbec67

View File

@ -44,7 +44,11 @@ cyc_edge_adj = [
# === interaction parameters ===
frame_rate = 24
inductance = 0.001
therm_conduct = 0.001
drain = False
drain_rate = 0.1
@ -55,6 +59,39 @@ current = 30*[0]
# === main loop ===
def heat_evolution():
for e, f in cyc_edge_adj:
flow = therm_conduct*(current[e] - current[f])
current[e] -= flow
current[f] += flow
if drain:
current[v] *= 1 - drain_rate
def wave_evolution():
# use verlet integration, first updating the currents and then using the new
# currents to update the charges.
for e in range(30):
u, v = edges[e]
current[e] += inductance*(charge[u] - charge[v])
for e in range(30):
u, v = edges[e]
charge[u] -= current[e]
charge[v] += current[e]
if drain:
for v in range(12):
charge[v] *= 1 - drain_rate
def show_data(screen):
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))
screen.blit(text, (20, viewsize))
text = font.render(str(energy), True, (255, 255, 255))
screen.blit(text, (120, viewsize))
text = font.render('charge[0]', True, (255, 255, 255))
screen.blit(text, (20, 30 + viewsize))
text = font.render(str(charge[0]), True, (255, 255, 255))
screen.blit(text, (120, 30 + viewsize))
if __name__ == '__main__':
# set up display and clock
pygame.init()
@ -73,11 +110,11 @@ if __name__ == '__main__':
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_j:
charge[0] += 10
charge[11] -= 10
elif event.key == pygame.K_k:
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))
@ -87,17 +124,11 @@ if __name__ == '__main__':
elif event.type == pygame.QUIT:
quit()
# print data
energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current))
# clear screen
screen.blit(background, (0, 0))
text = font.render('energy', True, (255, 255, 255))
screen.blit(text, (20, viewsize))
text = font.render(str(energy), True, (255, 255, 255))
screen.blit(text, (120, viewsize))
text = font.render('charge[0]', True, (255, 255, 255))
screen.blit(text, (20, 30 + viewsize))
text = font.render(str(charge[0]), True, (255, 255, 255))
screen.blit(text, (120, 30 + viewsize))
# show data
show_data(screen)
# show state
for e in range(30):
@ -111,19 +142,9 @@ if __name__ == '__main__':
vertices[edges[e][1]],
)
# evolve state. use verlet integration, first updating the currents and then
# using the new currents to update the charges.
for e in range(30):
u, v = edges[e]
##current[e] *= 1 - resistance
current[e] += inductance*(charge[u] - charge[v])
for e in range(30):
u, v = edges[e]
charge[u] -= current[e]
charge[v] += current[e]
if drain:
for v in range(12):
charge[v] *= 1 - drain_rate
# evolve state
wave_evolution()
##heat_evolution()
# step
pygame.display.flip()