Cortix 2019 07Aug2019

Ideal Gas Example

  • This is part of the Cortix-on-Jupyter-Notebook examples.
  • You must be in a Jupyter Notebook server to run this notebook.
  • Select each of the cells below and run them sequentially (use the run button, >| on the tool bar or use the Cell option on the menu bar).
  • Alternatively, on the menu bar run all cells: 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}}} $

Introduction

This Cortix use-case simulates the random motion of particles which simulates the behavior of ideal gas.

Elastic Collision model

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.

Collision Detection

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.

Call the Run File and Modify Simulation Parameters

In [ ]:
# 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
In [ ]:
# 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()

Display the Animated Results

In [ ]:
from IPython.display import HTML

HTML("""
<video width="600" height="600" controls>
  <source src="particle_animation.mp4" type="video/mp4">
</video>
""")

View Kinetic Energy Vs Time

In [ ]:
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 with New Parameters and Geometry

In [ ]:
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()
In [ ]:
from IPython.display import HTML

HTML("""
<video width="600" height="600" controls>
  <source src="particle_animation.mp4" type="video/mp4">
</video>
""")