{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/bazilevs/anaconda3/lib/python3.6/site-packages/IPython/html.py:14: ShimWarning: The `IPython.html` package has been deprecated since IPython 4.0. You should import from `notebook` instead. `IPython.html.widgets` has moved to `ipywidgets`.\n", " \"`IPython.html.widgets` has moved to `ipywidgets`.\", ShimWarning)\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "470fcde117014c459489d0fc5fe90211", "version_major": 2, "version_minor": 0 }, "text/html": [ "

Failed to display Jupyter Widget of type interactive.

\n", "

\n", " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the Jupyter\n", " Widgets Documentation for setup instructions.\n", "

\n", "

\n", " If you're reading this message in another frontend (for example, a static\n", " rendering on GitHub or NBViewer),\n", " it may mean that your frontend doesn't currently support widgets.\n", "

\n" ], "text/plain": [ "interactive(children=(FloatSlider(value=0.05, description='k', max=0.1, step=0.01), FloatSlider(value=5.5, description='C_A0', max=10.0, min=1.0, step=0.5), Output()), _dom_classes=('widget-interact',))" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "from scipy.integrate import odeint\n", "\n", "k = 0.001 # try this or try k = 1 or try k = 2\n", "\n", "from IPython.html.widgets import interact\n", "from IPython.display import clear_output, display, HTML\n", "def firstorder(k, C_A0):\n", " def f(C_list, time_array):\n", " C_A = C_list[0]\n", " C_B = C_list[1]\n", " dcadt = -k*C_A\n", " dcbdt = k*C_A\n", " return [dcadt, dcbdt]\n", "\n", "# k = 0.001\n", "# C_A0 = 10 \n", " C_B0 = 0 \n", " time_start = 0\n", " time_finish = 1000\n", " N_points = 1000\n", " time_array = np.linspace(time_start, time_finish, N_points)\n", "\n", " C_num_list = odeint(f, [C_A0, C_B0], time_array)\n", "\n", " C_A_num = C_num_list[:,0]\n", " C_B_num = C_num_list[:,1]\n", "\n", " # print(C_num_list)\n", " plt.plot(time_array, C_A_num, 'r--',label='A')\n", " plt.plot(time_array, C_B_num, 'b--',label='B')\n", " plt.xlabel('t, s')\n", " plt.ylabel('C, mol/L')\n", " plt.legend()\n", " plt.show()\n", "\n", "interact(firstorder, k = (0.0, 0.1, 0.01), C_A0 = (1., 10., 0.5))\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "1409d944eaf542709878f05cdb110649", "version_major": 2, "version_minor": 0 }, "text/html": [ "

Failed to display Jupyter Widget of type interactive.

\n", "

\n", " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the Jupyter\n", " Widgets Documentation for setup instructions.\n", "

\n", "

\n", " If you're reading this message in another frontend (for example, a static\n", " rendering on GitHub or NBViewer),\n", " it may mean that your frontend doesn't currently support widgets.\n", "

\n" ], "text/plain": [ "interactive(children=(FloatSlider(value=0.5, description='k1', max=1.0), FloatSlider(value=1.0, description='k2', max=2.0), Output()), _dom_classes=('widget-interact',))" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "from scipy.integrate import odeint\n", "plt.style.use('ggplot')\n", "\n", "# from IPython.html.widgets import interact\n", "# from IPython.display import clear_output, display, HTML\n", "def systemodes(k1,k2):\n", " def f(C_list, time_array):\n", " C_A = C_list[0]\n", " C_B = C_list[1]\n", " C_C = C_list[2]\n", " C_D = C_list[3]\n", " r1 = k1*C_A*C_B\n", " r2 = k2*C_B*C_C\n", " dCadt = -r1\n", " dCbdt = -r1 -r2\n", " dCcdt = r1 - r2\n", " dCddt = r2\n", " return [dCadt, dCbdt, dCcdt, dCddt]\n", "\n", " time_start = 0\n", " time_finish = 3\n", " N_points = 100\n", " time_array = np.linspace(time_start, time_finish, N_points)\n", " \n", " C_0_list = [1, 1, 0, 0] # [C_A0, C_B0, C_C0, C_D0]\n", " C_num_list = odeint(f, C_0_list, time_array)\n", " \n", " \n", " C_A_num = C_num_list[:,0]\n", " C_B_num = C_num_list[:,1]\n", " C_C_num = C_num_list[:,2]\n", " C_D_num = C_num_list[:,3]\n", "\n", " # print(C_num_list)\n", " plt.plot(time_array, C_A_num, 'r--',label='A')\n", " plt.plot(time_array, C_B_num, 'b--',label='B')\n", " plt.plot(time_array, C_C_num, 'g--',label='C')\n", " plt.plot(time_array, C_D_num, 'k--',label='D')\n", " plt.xlabel('t, s')\n", " plt.ylabel('C, mol/L')\n", " plt.legend()\n", " plt.show()\n", "\n", " \n", "interact(systemodes, k1=(0., 1, 0.1),k2=(0., 2, 0.1))\n", "#tips and tricks of an old python enthusiast\n", "\n", "# systemod es(k1=1., k2=2.1)\n", "# cmd - ]\n", "# cntrl - ]\n", "\n", "# shift-enter -run cell\n", "# autocomplete - tab\n", "# doc shift-tab \n", "# comment stuff : cmd - /\n", "# comment stuff control - /\n", "\n", "# k1 = 1.\n", "# k2 = 1.5\n", "\n", "\n", " " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "733a8678cd8b45ad87b66c2b3798058d", "version_major": 2, "version_minor": 0 }, "text/html": [ "

Failed to display Jupyter Widget of type interactive.

\n", "

\n", " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the Jupyter\n", " Widgets Documentation for setup instructions.\n", "

\n", "

\n", " If you're reading this message in another frontend (for example, a static\n", " rendering on GitHub or NBViewer),\n", " it may mean that your frontend doesn't currently support widgets.\n", "

\n" ], "text/plain": [ "interactive(children=(FloatSlider(value=0.05, description='d', max=0.1, step=0.01), Output()), _dom_classes=('widget-interact',))" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "from scipy.integrate import odeint\n", "plt.style.use('ggplot')\n", "\n", "\n", "P = 0 # 10\n", "# d = 0.0001\n", "B = 0.0095\n", "G = 0.001\n", "A = 0.001\n", "\n", "def russians_are_not_scared_of_zombies(d):\n", "\n", " def f(C_list, time_array):\n", " Si = C_list[0]\n", " Zi = C_list[1]\n", " Ri = C_list[2]\n", " dsdt = P - B*Si*Zi - d*Si\n", " dzdt = B*Si*Zi + G*Ri - A*Si*Zi\n", " drdt = d*Si + A*Si*Zi - G*Ri\n", " return [dsdt, dzdt, drdt]\n", "\n", "\n", "\n", " S0 = 500\n", " Z0 = 0\n", " R0 = 0\n", " C_list_0 = [S0, Z0, R0]\n", "\n", " time_start = 0\n", " time_finish = 10.\n", " N_points = 100\n", " time_array = np.linspace(time_start, time_finish, N_points)\n", "\n", " C_all_num = odeint(f, C_list_0, time_array)\n", "\n", "\n", " C_S_num = C_all_num[:,0]\n", " C_Z_num = C_all_num[:,1]\n", " C_R_num = C_all_num[:,2]\n", "\n", " plt.plot(time_array, C_S_num, 'r--',label='living')\n", " plt.plot(time_array, C_Z_num, 'b--',label='zombie')\n", " plt.plot(time_array, C_R_num, 'g--',label='dead')\n", " plt.xlabel('days after the outbreak')\n", " plt.ylabel('population')\n", " plt.legend()\n", " plt.show()\n", "\n", "mymax = 0.1\n", "mymin = 0.00000\n", "interact(russians_are_not_scared_of_zombies, d=(mymin, mymax, (mymax-mymin)/10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 2:\n", "\n", "Calculate the functions $T(t), C_{A}(t)$ in a mixer:\n", "\n", "Stirred Tank reactor:\n", "\n", "$T_{in}, C_{A0} \\to T_f, C_A$\n", "mixer\n", "$q = \\dot{V} = 100 m^3/hr$\n", "$V = 100 m^3$\n", "\n", "\n", "energy balance:\n", "\n", "$V \\dfrac{dT}{dt} = q (T_{f} - T(t))$\n", "\n", "mass balance:\n", "\n", "$V \\dfrac{dC_A}{dt} = q (C_{f} - C_{A}(t))$\n", "\n", "You need to give the program parameters \n", "$C_{A0}=0, C_{Af} = 1$\n", "$T_0 = 350$, $T_f = 300$\n", "\n", "you can call the python function:\n", "\n", "```python\n", "Tf, Cf = ...\n", "def mixer(X_list, t):\n", " ...\n", " return [dTdt, dCa/dt]\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }