{ "cells": [ { "cell_type": "markdown", "metadata": { "variables": { "\\boldsymbol{#1": {} } }, "source": [ "`Cortix` 2019 **07Aug2019**\n", "\n", "# Ideal Gas Example\n", "* This is part of the [Cortix](https://cortix.org)-on-[Jupyter-Notebook](https://github.com/dpploy/cortix-nb) examples.\n", "* You must be in a Jupyter Notebook server to run this notebook.\n", "* 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).\n", "* Alternatively, on the menu bar run all cells: `Cell -> Run All`.\n", "\n", "$ \n", " \\newcommand{\\Amtrx}{\\boldsymbol{\\mathsf{A}}}\n", " \\newcommand{\\Bmtrx}{\\boldsymbol{\\mathsf{B}}}\n", " \\newcommand{\\Smtrx}{\\boldsymbol{\\mathsf{S}}}\n", " \\newcommand{\\xvec}{\\boldsymbol{\\mathsf{x}}}\n", " \\newcommand{\\vvar}{\\boldsymbol{v}}\n", " \\newcommand{\\fvar}{\\boldsymbol{f}}\n", " \\newcommand{\\Power}{\\mathcal{P}}\n", " \\newcommand{\\bm}[1]{{\\boldsymbol{#1}}}\n", "$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## Table of Contents\n", "* [Introduction](#intro)\n", " - [Elatstic collision model](#collisionmodel)\n", " - [Collision detection model](#collisiondetection)\n", "* [Call the Run File](#collisiondetection)\n", "* [Display the Animated Results](#net)\n", "* [View Kinetic Energy Vs Time](#run)\n", "* [Simulation with New Parameters and Geometry](#new)\n", "\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This Cortix use-case simulates the random motion of particles which simulates the behavior of ideal gas." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Elastic Collision model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The equations for resultant velocity after a 2D elastic collision between two circular objects can be represented as:\n", " \\begin{equation*}\n", " \\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})\n", " \\end{equation*}\n", " \\begin{equation*}\n", " \\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})\n", " \\end{equation*}\n", "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.\n", "\n", "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. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Collision Detection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Call the Run File and Modify Simulation Parameters " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Import various packages; must have the Cortix repository installed\n", "\n", "try:\n", " import cortix\n", "except ImportError:\n", " print('Installing the \"cortix\" package...\\n')\n", " !pip install cortix\n", " import cortix\n", "\n", "# Import the example modules\n", "from cortix.examples import Run_Particle" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [], "scrolled": true }, "outputs": [], "source": [ "# Create a Cortix object with Python multiprocessing\n", "\n", "simulation = Run_Particle()\n", "simulation.fps = 100\n", "simulation.runtime = 6\n", "simulation.procs = 2\n", "simulation.n_list = 5\n", "simulation.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display the Animated Results " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "from IPython.display import HTML\n", "\n", "HTML(\"\"\"\n", "\n", "\"\"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## View Kinetic Energy Vs Time" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import matplotlib.image as mpimg\n", "import matplotlib as mpl\n", "mpl.rcParams['figure.dpi']= 1500\n", "%matplotlib inline\n", "\n", "fig = plt.figure(figsize=(12,12))\n", "ax = fig.add_subplot(111)\n", "ax.axis('off')\n", "img = mpimg.imread('energy_vs_time.png')\n", "ax.imshow(img)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simulation with New Parameters and Geometry " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "simulation = Run_Particle()\n", "simulation.fps = 100\n", "simulation.runtime = 10\n", "simulation.procs = 7\n", "simulation.n_list = 15\n", "simulation.a=(0,-32.2)\n", "simulation.cor = 1\n", "simulation.r = 0.5\n", "simulation.shape = [(0, 0), (0, 30), (30, 30),(30,25),\n", " (65,25),(65,-20),(85,-20),(85,-40),\n", " (45,-40),(45,-20),(49,-20),(49,15),\n", " (30,15),(30,0),(0,0)]\n", "simulation.run()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import HTML\n", "\n", "HTML(\"\"\"\n", "\n", "\"\"\")\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false } }, "nbformat": 4, "nbformat_minor": 2 }