Skip to content

Commit 11b1a58

Browse files
committed
Finished refactoring
1 parent 539ae44 commit 11b1a58

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed
Binary file not shown.
Binary file not shown.

examples/random_walks_jumps.png

-1.8 MB
Binary file not shown.

nbodies.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
#!/usr/bin/env pypy3
1+
#!/usr/bin/env python
22
import numpy as np
33

44
from src.draw import Canvas
5+
from matplotlib import cm
56

67
small_conf = {
7-
"width": 2000,
8-
"height": 2000,
8+
"width": 4000,
9+
"height": 4000,
910
"bg": (0, 0, 0),
10-
"fg": (255, 255, 255, 0.1)
11+
"fg": (255, 255, 255, 0.25)
1112
}
1213

1314

@@ -22,24 +23,28 @@ def acc_vec(pos, mass):
2223
with Canvas(**small_conf) as c:
2324

2425
# Initial particle conditions
25-
n = 5000
26+
n = 2000
2627
# pos = np.random.multivariate_normal([500, 500], [[1000, 250], [250, 1000]], n)
27-
pos = np.random.uniform(0, 2000, (n, 2))
28+
pos = np.random.uniform(0, 4000, (n, 2))
2829
mass = np.ones(n)*500
2930
# Add radial initial velocity
3031
vel = np.zeros((n, 2), dtype=np.float)
31-
dt = 0.25
32+
dt = 0.5
3233

3334
while pos.size > 1:
3435
acc = acc_vec(pos, mass)
3536
vel += acc*dt
3637
pos_new = pos + vel*dt
3738

38-
for old, new in zip(pos, pos_new):
39-
c.draw_line(old, new)
39+
for old, new, v in zip(pos, pos_new, np.linalg.norm(vel, axis=1)):
40+
alpha = min(1, 1 / v)
41+
color = (*cm.viridis(v/10)[:3], alpha)
42+
c.draw_line(old, new, rgba=color, width=4)
4043

41-
# Remove points no longer on canvas
42-
remove_idx = c.get_oob(pos_new)
44+
# Remove points we don't want to draw
45+
oob_idx = c.get_oob(pos_new)
46+
# too_fast_idx = np.linalg.norm(vel, axis=1) > 8
47+
remove_idx = oob_idx # | too_fast_idx
4348

4449
pos = pos_new[~remove_idx]
4550
mass = mass[~remove_idx]

src/draw.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import cairo as cr
1+
import cairocffi as cr
22
import logging
33
from math import sqrt
44
from time import time
@@ -24,6 +24,7 @@ def __enter__(self):
2424
self.surface = cr.ImageSurface(cr.FORMAT_ARGB32, self.width, self.height)
2525
# Set background color
2626
self.ctx = cr.Context(self.surface)
27+
self.ctx.set_antialias(cr.ANTIALIAS_FAST)
2728
self.ctx.save()
2829
self.set_color(self.bg)
2930
self.ctx.paint()

0 commit comments

Comments
 (0)