Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
13 changes: 13 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
pytest = "*"
numpy = "*"

[requires]
python_version = "3.7"
101 changes: 101 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added build/lib/pycontest/__init__.py
Empty file.
48 changes: 48 additions & 0 deletions build/lib/pycontest/elastic_collisions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import numpy as np

def collision_1d(v1_i, v2_i, m1=1, m2=1):
""" 1d elastic collisions

Arguments:
v1_1, v2_i: initial values of particles velocities
m1, m2: masses

Returns:
final velocities after collisions
"""

v1_f = (m1 - m2) / (m1 + m2) * v1_i + 2 * m2 / (m1 + m2) * v2_i
v2_f = (m2 - m1) / (m1 + m2) * v2_i + 2 * m1 / (m1 + m2) * v1_i
return v1_f, v2_f


def collision_2d(v1, v2, r12, m1, m2):
""" 2d elastic collisions

Arguments:
v1_1, v2_i: initial values of particles velocities
m1, m2: masses
r12: vector between particles: r2-r1

Returns:
final velocities after collisions
"""

if np.linalg.norm(r12) == 0:
raise Exception("two particles are exactly in the same place, "
"consider using smaller time step")
n_vect = r12 / np.linalg.norm(r12)

# scalars
v1n = np.dot(v1, n_vect)
v2n = np.dot(v2, n_vect)
v1s = v1 - v1n * n_vect
v2s = v2 - v2n * n_vect

v1n_f, v2n_f = collision_1d(v1n, v2n, m1, m2)
v1n_f *= n_vect
v2n_f *= n_vect

v1_f = v1s + v1n_f
v2_f = v2s + v2n_f
return v1_f, v2_f
126 changes: 126 additions & 0 deletions build/lib/pycontest/movies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import matplotlib.animation as anm
import numpy as np
import math as mt
import pdb

"""
Matplotlib Animation Example

author: Jake Vanderplas
email: [email protected]
website: http://jakevdp.github.com
license: BSD
Please feel free to use and modify this, but keep the above information. Thanks!
"""
class Movie_2d:

def __init__(self, fun, dt, t_max, loc, vel, domain, mass, radius=10):
self.dt = dt
self.fps = int(mt.floor(1 / dt))
self.n_max = int(mt.floor(t_max / dt))
self.loc = np.copy(loc)
self.vel = np.copy(vel)
self.domain = domain
self.mass = mass
self.radius = radius
self.fun = fun

self.fig = plt.figure()
self.fig.subplots_adjust(left=0.1, bottom=0.1, right=.9, top=.9)

self.ax = self.fig.add_subplot(111, aspect='equal', autoscale_on=False, xlim=(domain[0]), ylim=domain[1])
for el in range(loc.shape[0]):
self.circ = plt.Circle(self.loc[el, :], self.radius, color='r', fill=False, linewidth=2)
self.ax.add_patch(self.circ)

self.frame = plt.Rectangle([0, 0], domain[0][1] - domain[0][0], domain[1][1] - domain[1][0], fc='none')
self.ax.add_patch(self.frame)

def generate_frame(self, i):
# pdb.set_trace()
loc, vel = self.fun(self.dt, self.mass, self.radius, self.loc, self.vel, self.domain)
#pdb.set_trace()
for el in range(loc.shape[0]):
self.circ.set_center(loc[el, :])
return self.circ, self.frame

def animate(self, name):
animation = anm.FuncAnimation(self.fig, self.generate_frame, frames=self.n_max, interval=10, blit=True)

fps = self.fps
try:
animation.save(name + '.mp4', fps=fps, extra_args=['-vcodec', 'libx264'])
except ValueError:
print("\n mp4 file can not be created, will try to create html")
try:
animation.save(name + '.html', fps=fps, extra_args=['-vcodec', 'libx264'])
except:
print("\n html also can not be created")

class Plot_2d:

def __init__(self, dt, t_max, loc1_0, loc2_0, vel1_0, vel2_0,
domain, radius, m1=1, m2=1, nr=4, nc=5):
self.fig = plt.figure()
self.nr = nr
self.nc = nc
self.dt = dt
self.t_max = t_max
self.loc1_0 = loc1_0
self.loc2_0 = loc2_0
self.vel1_0 = vel1_0
self.vel2_0 = vel2_0
self.domain = domain
self.radius = radius
self.m1 = m1
self.m2 = m2


def simulation(self):
tt = 0
ii = 0
loc1, loc2 = self.loc1_0, self.loc2_0
vel1, vel2 = self.vel1_0, self.vel2_0
while tt <= t_max:
[loc1, loc2], [vel1, vel2] = \
simulation_step(dt=self.dt, m1=self.m1, m2=self.m2, radius=self.radius,
loc1_0=loc1, loc2_0=loc2, v1_0=vel1, v2_0=vel2,
domain=self.domain)
tt += dt
if ii % 5 == 0:
#pdb.set_trace()
self.plotting(ii/5+1, loc1.copy(), loc2.copy(), vel1.copy(), vel2.copy())
ii += 1
plt.show()

def plotting(self, ii, loc1, loc2, vel1, vel2):
ax = self.fig.add_subplot(self.nr, self.nc, ii, aspect='equal')
# ax = plt.gca()
# ax.quiver(r1[0], r1[1], r12[0], r12[1], angles='xy', scale_units='xy', scale=1, color="k", width=1.e-3)
# ax.quiver(0, 0, r1[0], r1[1], angles='xy', scale_units='xy', scale=1, color="k", width=1.e-3)
# ax.quiver(0, 0, r2[0], r2[1], angles='xy', scale_units='xy', scale=1, color="k", width=1.e-3)
# ax.quiver(r1[0], r1[1], v1[0], v1[1], angles='xy', scale_units='xy', scale=1, color="g")
# ax.quiver(r2[0], r2[1], v2[0], v2[1], angles='xy', scale_units='xy', scale=1, color="g")
# ax.quiver(r1[0], r1[1], v1n[0], v1n[1], angles='xy', scale_units='xy', scale=1, color="y")
# ax.quiver(r2[0], r2[1], v2n[0], v2n[1], angles='xy', scale_units='xy', scale=1, color="y")
# ax.quiver(r1[0], r1[1], v1s[0], v1s[1], angles='xy', scale_units='xy', scale=1, color="y")
# ax.quiver(r2[0], r2[1], v2s[0], v2s[1], angles='xy', scale_units='xy', scale=1, color="y")
#
# ax.quiver(r1[0], r1[1], u1[0], u1[1], angles='xy', scale_units='xy', scale=1, color="b")
# ax.quiver(r2[0], r2[1], u2[0], u2[1], angles='xy', scale_units='xy', scale=1, color="b")
# ax.quiver(r1[0], r1[1], u1n[0], u1n[1], angles='xy', scale_units='xy', scale=1, color="c")
# ax.quiver(r2[0], r2[1], u2n[0], u2n[1], angles='xy', scale_units='xy', scale=1, color="c")
# ax.quiver(r1[0], r1[1], u1s[0], u1s[1], angles='xy', scale_units='xy', scale=1, color="c")
# ax.quiver(r2[0], r2[1], u2s[0], u2s[1], angles='xy', scale_units='xy', scale=1, color="c")

# ax.quiver(X, Y, V, U, angles='xy', scale_units='xy', scale=1, color="b")
c1 = plt.Circle(loc1, self.radius, color='r', fill=False)
c2 = plt.Circle(loc2, self.radius, color='r', fill=False)
ax.set_xlim([0, 100])
ax.set_ylim([0, 100])
# ax.set_aspec("equal")
ax.add_patch(c1)
ax.add_patch(c2)
Loading