Separate things into functions a bit
This commit is contained in:
parent
36077fd872
commit
217bdbec67
@ -44,7 +44,11 @@ cyc_edge_adj = [
|
|||||||
# === interaction parameters ===
|
# === interaction parameters ===
|
||||||
|
|
||||||
frame_rate = 24
|
frame_rate = 24
|
||||||
|
|
||||||
inductance = 0.001
|
inductance = 0.001
|
||||||
|
|
||||||
|
therm_conduct = 0.001
|
||||||
|
|
||||||
drain = False
|
drain = False
|
||||||
drain_rate = 0.1
|
drain_rate = 0.1
|
||||||
|
|
||||||
@ -55,6 +59,39 @@ current = 30*[0]
|
|||||||
|
|
||||||
# === main loop ===
|
# === 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__':
|
if __name__ == '__main__':
|
||||||
# set up display and clock
|
# set up display and clock
|
||||||
pygame.init()
|
pygame.init()
|
||||||
@ -73,11 +110,11 @@ if __name__ == '__main__':
|
|||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.KEYDOWN:
|
if event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_j:
|
if event.key == pygame.K_j:
|
||||||
charge[0] += 10
|
|
||||||
charge[11] -= 10
|
|
||||||
elif event.key == pygame.K_k:
|
|
||||||
charge[0] += 100
|
charge[0] += 100
|
||||||
charge[11] -= 100
|
charge[11] -= 100
|
||||||
|
elif event.key == pygame.K_k:
|
||||||
|
charge[5] += 100
|
||||||
|
charge[7] -= 100
|
||||||
elif event.key == pygame.K_SPACE:
|
elif event.key == pygame.K_SPACE:
|
||||||
drain = True
|
drain = True
|
||||||
pygame.draw.aaline(screen, (255, 255, 255), (0, 0), (viewsize, viewsize))
|
pygame.draw.aaline(screen, (255, 255, 255), (0, 0), (viewsize, viewsize))
|
||||||
@ -87,17 +124,11 @@ if __name__ == '__main__':
|
|||||||
elif event.type == pygame.QUIT:
|
elif event.type == pygame.QUIT:
|
||||||
quit()
|
quit()
|
||||||
|
|
||||||
# print data
|
# clear screen
|
||||||
energy = 0.5*(sum(q*q for q in charge) + inductance*sum(i*i for i in current))
|
|
||||||
screen.blit(background, (0, 0))
|
screen.blit(background, (0, 0))
|
||||||
text = font.render('energy', True, (255, 255, 255))
|
|
||||||
screen.blit(text, (20, viewsize))
|
# show data
|
||||||
text = font.render(str(energy), True, (255, 255, 255))
|
show_data(screen)
|
||||||
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 state
|
# show state
|
||||||
for e in range(30):
|
for e in range(30):
|
||||||
@ -111,19 +142,9 @@ if __name__ == '__main__':
|
|||||||
vertices[edges[e][1]],
|
vertices[edges[e][1]],
|
||||||
)
|
)
|
||||||
|
|
||||||
# evolve state. use verlet integration, first updating the currents and then
|
# evolve state
|
||||||
# using the new currents to update the charges.
|
wave_evolution()
|
||||||
for e in range(30):
|
##heat_evolution()
|
||||||
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
|
|
||||||
|
|
||||||
# step
|
# step
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
Loading…
Reference in New Issue
Block a user