Compare commits
2 Commits
36077fd872
...
9955a827a7
Author | SHA1 | Date | |
---|---|---|---|
|
9955a827a7 | ||
|
217bdbec67 |
@ -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()
|
||||
@ -66,6 +103,10 @@ 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:
|
||||
@ -73,11 +114,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 +128,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 +146,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()
|
||||
|
Loading…
Reference in New Issue
Block a user