Guest User

naive n-body simulation

a guest
May 29th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.30 KB | None | 0 0
  1. import itertools
  2. import math
  3. import pyglet
  4. import pyglet.gl as gl
  5. from random import random
  6. from numba import jit
  7.  
  8.  
  9. window = pyglet.window.Window(1400, 800)
  10.  
  11.  
  12. def make_circle_fill():
  13.     """ Adds a vertex list of circle polygon to batch and returns it. """
  14.     num_points = 40
  15.     batch = pyglet.graphics.Batch()
  16.     rad = math.pi * 2 / num_points # getting 360 / n in radians
  17.     index = list(itertools.chain.from_iterable( (0, x-1, x)  for x in range(2, num_points+1) ))
  18.     index += [0, 1, num_points] # end of fan
  19.     vertices = [0, 0] # adding center of fan
  20.     for i in range(1, num_points + 1):
  21.         angle = rad * i
  22.         vertices += [math.cos(angle), math.sin(angle)]
  23.     vertices += [1, 0] # adding end of fan
  24.     circle = pyglet.graphics.vertex_list_indexed(num_points+2, index, ('v2f', vertices))
  25.     return circle
  26.  
  27.  
  28. @jit(nopython=True, cache=True)
  29. def compute_radius(mass):
  30.     return mass**0.333 * 0.4
  31.  
  32.  
  33. # Initialize stuff
  34. circlefill = make_circle_fill()
  35.  
  36. total_objects = 1000
  37.  
  38. masses = []
  39. radii = []
  40. px = []
  41. py = []
  42. vx = []
  43. vy = []
  44. deleted = []
  45. for i in range(total_objects):
  46.     mass = random() * 100
  47.     masses.append(mass)
  48.     radii.append(compute_radius(mass))
  49.     px.append(random() * 1400)
  50.     py.append(random() * 800)
  51.     vx.append((random() - 0.5) * 4)
  52.     vy.append((random() - 0.5) * 4)
  53.     deleted.append(0)
  54.  
  55.  
  56. def timer(dt):
  57.     global total_objects
  58.     total_objects = turn(total_objects, masses, radii, px, py, vx, vy, deleted)
  59.  
  60.  
  61. @jit(nopython=True, cache=True)
  62. def turn(total_objects, masses, radii, px, py, vx, vy, deleted):
  63.     to_clean = set()
  64.     for i in range(total_objects):
  65.         for j in range(i + 1, total_objects):
  66.             # distance on each axis
  67.             dx = px[i] - px[j]
  68.             dy = py[i] - py[j]
  69.  
  70.             # square distances
  71.             dx2 = dx ** 2
  72.             dy2 = dy ** 2
  73.             r2 = dx2 + dy2
  74.             # Used to translate acceleration value into a vector later on
  75.             normalizer = abs(dx) + abs(dy)
  76.  
  77.             # check collision
  78.             # Objects that are already collided with something else can't collide again
  79.             if deleted[j] == 0 and dx2 + dy2 < (radii[i] + radii[j]) ** 2:
  80.                 # j will be removed, i will gain all of j's mass and momentum
  81.                 total_mass = masses[i] + masses[j]
  82.                 # new coordinates
  83.                 px[i] = (px[i] * masses[i] + px[j] * masses[j]) / total_mass
  84.                 py[i] = (py[i] * masses[i] + py[j] * masses[j]) / total_mass
  85.                 # new velocity based on momentum
  86.                 vx[i] = (vx[i] * masses[i] + vx[j] * masses[j]) / total_mass
  87.                 vy[i] = (vy[i] * masses[i] + vy[j] * masses[j]) / total_mass
  88.  
  89.                 radii[i] = compute_radius(total_mass)
  90.                 masses[i] = total_mass
  91.                 # Cleanup j after all of this is over (until then we need to compute its influence on other objects)
  92.                 to_clean.add(j)
  93.                 deleted[j] = True
  94.             # No collision? Compute interaction
  95.             else:
  96.                 vx[i] -= dx * normalizer / r2 * masses[j] / 1000000.0
  97.                 vy[i] -= dy * normalizer / r2 * masses[j] / 1000000.0
  98.                 vx[j] += dx * normalizer / r2 * masses[i] / 1000000.0
  99.                 vy[j] += dy * normalizer / r2 * masses[i] / 1000000.0
  100.  
  101.         px[i] += vx[i]
  102.         py[i] += vy[i]
  103.  
  104.     # Clean up deleted objects after the round completion, in reverse order
  105.     for i in sorted(to_clean)[::-1]:
  106.         masses.pop(i)
  107.         radii.pop(i)
  108.         px.pop(i)
  109.         py.pop(i)
  110.         vx.pop(i)
  111.         vy.pop(i)
  112.         deleted.pop(i)
  113.         total_objects -= 1
  114.     return total_objects
  115.  
  116.  
  117. @window.event
  118. def on_draw():
  119.     global total_objects
  120.     #total_objects = turn(total_objects, masses, radii, px, py, vx, vy)
  121.     print(total_objects)
  122.     gl.glClear(gl.GL_COLOR_BUFFER_BIT)
  123.     for i in range(total_objects):
  124.         gl.glPushMatrix()
  125.         gl.glColor3f(1,1,0)
  126.         gl.glTranslatef(px[i], py[i], 0)
  127.         gl.glScalef(radii[i], radii[i], 1)
  128.         circlefill.draw(gl.GL_TRIANGLES)
  129.         gl.glPopMatrix()
  130.  
  131. # Schedule an update every 1/30th of a second. After each update, on_draw() is called automatically
  132. pyglet.clock.schedule_interval(timer, 1/30.0)
  133. pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment