Cortix
2019 07Aug2019
>|
on the tool bar or use the Cell
option on the menu bar).Cell -> Run All
.$ \newcommand{\Amtrx}{\boldsymbol{\mathsf{A}}} \newcommand{\Bmtrx}{\boldsymbol{\mathsf{B}}} \newcommand{\Smtrx}{\boldsymbol{\mathsf{S}}} \newcommand{\xvec}{\boldsymbol{\mathsf{x}}} \newcommand{\vvar}{\boldsymbol{v}} \newcommand{\fvar}{\boldsymbol{f}} \newcommand{\Power}{\mathcal{P}} \newcommand{\bm}[1]{{\boldsymbol{#1}}} $
This Cortix use-case simulates the random motion of particles which simulates the behavior of ideal gas.
The equations for resultant velocity after a 2D elastic collision between two circular objects can be represented as: \begin{equation*} \bm{v'}_{1x} = \frac{\text{v}_{1} cos(\theta_1-\phi){(m_1-m_2)}+2 \text{m}_2 v_2 cos(\theta_2-\phi)}{m_1+m_2} cos(\phi) + v_1 sin(\theta_1 - \phi) cos(\phi + \frac{\pi}{2}) \end{equation*} \begin{equation*} \bm{v'}_{1y} = \frac{\text{v}_{1} cos(\theta_1-\phi){(m_1-m_2)}+2 \text{m}_2 v_2 cos(\theta_2-\phi)}{m_1+m_2} sin(\phi) + v_1 sin(\theta_1 - \phi) sin(\phi + \frac{\pi}{2}) \end{equation*} Where: $\theta_1$ and $\theta_2$ are the movement angles of the object, $v_1$ and $v_2$ are the velocities of the objects, $m_1$ and $m_2$ are the object masses, and $\phi$ is the contact angle.
Upon contact with another particle, the velocity is shifted to a new coordinate system with components in the normal and transverse directions. The collision can then be seperated into 2 1-Dimensional collision problems, then the resultant velocity vector is shifted back to x-y coordinates.
Each Particle object stores its own identifying information as class attributes. For each timestep, a Particle's trajectory is calculated independently. If a collision event is triggered, then the Particle responds with the appropriate function.
# Import various packages; must have the Cortix repository installed
try:
import cortix
except ImportError:
print('Installing the "cortix" package...\n')
!pip install cortix
import cortix
# Import the example modules
from cortix.examples import Run_Particle
# Create a Cortix object with Python multiprocessing
simulation = Run_Particle()
simulation.fps = 100
simulation.runtime = 6
simulation.procs = 2
simulation.n_list = 5
simulation.run()
from IPython.display import HTML
HTML("""
<video width="600" height="600" controls>
<source src="particle_animation.mp4" type="video/mp4">
</video>
""")
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib as mpl
mpl.rcParams['figure.dpi']= 1500
%matplotlib inline
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(111)
ax.axis('off')
img = mpimg.imread('energy_vs_time.png')
ax.imshow(img)
plt.show()
simulation = Run_Particle()
simulation.fps = 100
simulation.runtime = 10
simulation.procs = 7
simulation.n_list = 15
simulation.a=(0,-32.2)
simulation.cor = 1
simulation.r = 0.5
simulation.shape = [(0, 0), (0, 30), (30, 30),(30,25),
(65,25),(65,-20),(85,-20),(85,-40),
(45,-40),(45,-20),(49,-20),(49,15),
(30,15),(30,0),(0,0)]
simulation.run()
from IPython.display import HTML
HTML("""
<video width="600" height="600" controls>
<source src="particle_animation.mp4" type="video/mp4">
</video>
""")