{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 2 : Gas Laws Ideal and Real Gases"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.1 Page No : 41"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The air pressure in the tyre in bar is : 1.8\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "p1= 2.;\t\t\t# in bar\n",
    "v1= 30.;\t\t\t# in litre\n",
    "T1= 27.+273;\t\t\t# in K\n",
    "T2= -3.+273;\t\t\t# in K\n",
    "v2= v1;\t\t\t# in litre\n",
    "\n",
    "# Calculations\n",
    "# Gas law  p1*v1/T1= p2*v2/T2\n",
    "p2= p1*v1*T2/(T1*v2);\t\t\t# in bar\n",
    "\n",
    "# Results\n",
    "print \"The air pressure in the tyre in bar is :\",p2\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.2 Page No : 42"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The mass of Nitrogen gas stored in the vessel in kg is : 333.64\n",
      "The mass of Oxygen gas stored in the vessel in kg is : 381.08\n",
      "The mass of Carbon dioxide gas stored in the vessel in kg is : 524.00\n",
      "Molar volume of the gas mixture in m**3 is : 2.10\n",
      "density of the gas mixture in kg/m**3 is : 49.55\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "p= 12.;\t\t\t# in bar\n",
    "p=p*10**5;\t\t\t# in N/m**2\n",
    "v= 25.;\t\t\t# in m**3\n",
    "T= 30.+273;\t\t\t# in K\n",
    "# Part (a) Mass of each gas\n",
    "#Formula p*v=m*R*T\n",
    "R_U= 8314.;\t\t\t# in J/kg-mole K\n",
    "M_N2= 28.016;\t\t\t# in mole\n",
    "M_O2= 32.;\t\t\t# in mole\n",
    "M_CO2= 44.;\t\t\t# in mole\n",
    "\n",
    "# Calculations and Results\n",
    "R_N2= R_U/M_N2;\t\t\t# in J/kg K\n",
    "R_O2= R_U/M_O2;\t\t\t# in J/kg K\n",
    "R_CO2= R_U/M_CO2;\t\t\t# in J/kg K\n",
    "m_of_N2= p*v/(R_N2*T);\t\t\t# in kg\n",
    "m_of_O2= p*v/(R_O2*T);\t\t\t# in kg\n",
    "m_of_CO2= p*v/(R_CO2*T);\t\t\t# in kg\n",
    "print \"The mass of Nitrogen gas stored in the vessel in kg is : %.2f\"%(m_of_N2)\n",
    "print \"The mass of Oxygen gas stored in the vessel in kg is : %.2f\"%m_of_O2\n",
    "print \"The mass of Carbon dioxide gas stored in the vessel in kg is : %.2f\"%round(m_of_CO2)\n",
    "\n",
    "# Part (b) Molar Volume\n",
    "# Formula v_molar= M*R*T/p= R_U*T/p\n",
    "v_molar= R_U*T/p;\t\t\t# in m**3\n",
    "print \"Molar volume of the gas mixture in m**3 is : %.2f\"%v_molar\n",
    "\n",
    "# Part (c) Average density\n",
    "# rho_avg= total mass/total volume\n",
    "rho_avg= (m_of_N2+m_of_O2+m_of_CO2)/v;\t\t\t# in kg/m**3\n",
    "print  \"density of the gas mixture in kg/m**3 is : %.2f\"%rho_avg\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.3 Page No : 47"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The value of Cp in kJ/kg°C 15.375\n",
      "The value of Cv in kJ/kg°C 9.9375\n",
      "The value of R in kJ/kg°C 5.4375\n",
      "Molecular weight of the gas is : 1.529\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "Qp = 1230.;\t\t\t# kJ/kg\n",
    "Qv = 795.; \t\t\t# kJ/kg\n",
    "t1 = 16.;\t\t\t# in °C\n",
    "t2 = 96.;\t\t\t# in °C\n",
    "R_U = 8.314;\n",
    "\n",
    "# Calculations and Results\n",
    "delta_T= t2-t1;\t\t\t# in °C\n",
    "Cp= Qp/delta_T;\t\t\t# in kJ/kg °C\n",
    "print \"The value of Cp in kJ/kg°C\",Cp\n",
    "\n",
    "Cv= Qv/delta_T;\t\t\t# in kJ/kg °C\n",
    "print \"The value of Cv in kJ/kg°C\",Cv\n",
    "\n",
    "R= Cp-Cv;\t\t\t# in kJ/kg °C\n",
    "print \"The value of R in kJ/kg°C\",R\n",
    "\n",
    "molecular_weight= R_U/R;\n",
    "print \"Molecular weight of the gas is : %.3f\"%molecular_weight\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.4 Page No : 48"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Change in enthalpy in MJ is : 204.137\n",
      "change in internal energy in MJ is : 136.092\n"
     ]
    }
   ],
   "source": [
    "import math \n",
    "from scipy.integrate import quad \n",
    "\n",
    "# Variables\n",
    "a= 0.85;\n",
    "b= 0.00004;\n",
    "c= 5*10**-5;\n",
    "T1= 300;\t\t\t# in K\n",
    "T2= 2300;\t\t\t# in K\n",
    "gama= 1.5;\t\t\t# the ratio of specific heats\n",
    "m=1;\t\t\t# in kg\n",
    "\n",
    "# Calculations and Results\n",
    "def f1(T): \n",
    "    return a+b*T+c*T**2\n",
    "\n",
    "delta_H= m* quad(f1,T1,T2)[0]\n",
    "\n",
    "print \"Change in enthalpy in MJ is : %.3f\"%(delta_H*10**-3)\n",
    "\n",
    "# Formula delta_U= integration of m*Cv = integration of m*Cp/gama= delta_H/gama\n",
    "delta_U= delta_H/gama;\t\t\t# in kJ\n",
    "print  \"change in internal energy in MJ is : %.3f\"%(delta_U*10**-3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.5 Page No : 52"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using perfect gas law the pressure for unit mass of hydrogen in bar is : 54.457\n",
      "Using Van der waals equation, the pressure in bar is : 56.246\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "v= 0.9/3;\t\t\t# in m**3/kg\n",
    "v= 2*v;\t\t\t# in m**3/kg mole (as M_hydrogen = 2)\n",
    "T=120+273;\t\t\t# in K\n",
    "R=8314;\t\t\t# in J/kg mole K\n",
    "a=2.51*10**4;\t\t\t# in Nm**4/(kg mole)**2\n",
    "b= 0.0262;\n",
    "\n",
    "# Calculations and Results\n",
    "# Part (a)\n",
    "p= R*T/v;\t\t\t# in N/m**2\n",
    "p= p*10**-5;\t\t\t# in bar\n",
    "print \"Using perfect gas law the pressure for unit mass of hydrogen in bar is : %.3f\"%p\n",
    "\n",
    "# Part (b)\n",
    "p= R*T/(v-b)-a/v**2;\t\t\t# N/m**2\n",
    "p= p*10**-5;\t\t\t# in bar\n",
    "print \"Using Van der waals equation, the pressure in bar is : %.3f\"%p\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.6 Page No : 55"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "density of the gas under the changed condition in kg/m**3 is :  1.36\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "p1= 0.98;\t\t\t# in bar\n",
    "p2= 0.6;\t\t\t# in bar\n",
    "v1= 0.45;\t\t\t# in m**3/kg\n",
    "\n",
    "# Calculations\n",
    "# Applying Boyle's law\n",
    "v2= p1*v1/p2;\t\t\t# in m**3/kg\n",
    "rho2= 1/v2; \t\t\t# in kg/m**3\n",
    "\n",
    "# Results\n",
    "print  \"density of the gas under the changed condition in kg/m**3 is : \",round(rho2,2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example 2.7 Page No : 55"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Payload that can be lifted along with the balloon in kg is : 580.67\n"
     ]
    }
   ],
   "source": [
    "# Exa 2.7\n",
    "import math \n",
    "\n",
    "# Variables\n",
    "r=5;\t\t\t    # in cm\n",
    "R_U= 8314\n",
    "T= 27+273;\t\t\t# in K\n",
    "\n",
    "# Calculations\n",
    "V= 4./3*math.pi*r**3;\t\t\t# volume of balloon in cm**3\n",
    "# atmPressure= 75 cm off mercury = 75/76*1.01325 \n",
    "atmPressure= round(75./76*1.01325) ;\t\t\t# in bar\n",
    "p= atmPressure;\t\t\t# pressure of hydrogen in balloon in bar\n",
    "p=p*10**5;\t\t\t# in N/m**2\n",
    "R= R_U/2;\t\t\t# in J/kg K\n",
    "m1= p*V/(R*T);\t\t\t# in kg\n",
    "# The volume of air print laced = the volume of balloon, so\n",
    "R=287;\n",
    "T=20+273;\t\t\t# in K\n",
    "m2= p*V/(R*T);\t\t\t# in kg\n",
    "payload= m2-m1;\t\t\t# in kg\n",
    "\n",
    "# Results\n",
    "print \"Payload that can be lifted along with the balloon in kg is : %.2f\"%payload\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.12 Page No : 57"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The pressure in the space is 7.148 aPa\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "AvogadroNo= 6.023*10**23;\n",
    "n= 5/AvogadroNo;\t\t\t# number of moles\n",
    "v=10**-6;\t\t\t# in m**3\n",
    "T= -270+273;\t\t\t# in K\n",
    "R= 0.287;\n",
    "\n",
    "# Calculations\n",
    "p= n*R*T/v;\t\t\t# in kPa\n",
    "p= p*10**18;\t\t\t# in aPa\n",
    "\n",
    "# Results\n",
    "print \"The pressure in the space is %.3f aPa\"%p;\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.13 Page No : 57"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Change in enthalpy in kJ/kg is : 1934.8\n"
     ]
    }
   ],
   "source": [
    "# Exa 2.13\n",
    "import math \n",
    "from scipy.integrate import quad \n",
    "\n",
    "# Variables\n",
    "T1 = 300;\t\t\t# in K\n",
    "T2 = 900;\t\t\t# in K\n",
    "m = 2;    \t\t\t# in kg\n",
    "\n",
    "# Calculations\n",
    "def f3(T): \n",
    "    return 40-600/math.sqrt(T)+7000/T\n",
    "\n",
    "delta_H=m*  quad(f3,T1,T2)[0]\n",
    "\n",
    "delta_H= delta_H/17.03;\t\t\t# in kJ/kg\n",
    "\n",
    "# Results\n",
    "print \"Change in enthalpy in kJ/kg is : %.1f\"%delta_H\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.14 Page No : 58"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Molecular weight is : 38.84\n",
      "Gas constant in kJ/kg K is : 0.214\n",
      "The pressure of the gas in bar is : 0.569\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "m = 12.;\t\t\t# in kg mol\n",
    "v = 723.7;\t\t\t# in m**3\n",
    "T = 140.;\t\t\t# in °C\n",
    "T = T+273;\t\t\t# in K\n",
    "rho = 0.644;\t\t\t# in kg/m**3\n",
    "Ro = 8314;\t\t\t# in J/kg-mole K\n",
    "\n",
    "# Calculations and Results\n",
    "# rho= m/v, where m in Kg , so rho= m*M/v\n",
    "M = rho*v/m;\n",
    "m = m*M;\t\t\t# in kg\n",
    "print \"Molecular weight is : %.2f\"%M\n",
    "\n",
    "# Part (b)\n",
    "R = Ro/M;\t\t\t# in J/kg K\n",
    "print \"Gas constant in kJ/kg K is : %.3f\"%(R*10**-3)\n",
    "\n",
    "# Part(c)\n",
    "p = m*R*T/v;\t\t\t# in N/m**2\n",
    "p = p*10**-5;\t\t\t# in bar\n",
    "print \"The pressure of the gas in bar is : %.3f\"%p\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.15 Page No : 58"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The load that can be lifted with the air of aerostat in N is : 770.11\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "p = 0.98;   \t\t\t# in bar\n",
    "p = p*10**5;\t\t\t# in N/m**2\n",
    "v = 1000;\t    \t\t# in m**3\n",
    "T = 27+273;\t\t    \t# in K\n",
    "g = 9.8;\n",
    "M = 2;\n",
    "Ro = 8314;\t\t\t    # in J/kg-mole K\n",
    "\n",
    "# Calculations\n",
    "R = Ro/M;\t\t    \t# in kg K\n",
    "m = p*v/(R*T);\t\t\t# in kg\n",
    "W = m*g;\t\t\t    # in N\n",
    "\n",
    "# Results\n",
    "print \"The load that can be lifted with the air of aerostat in N is : %.2f\"%W\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.16 Page No : 59"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Change in enthalpy in kcal/kg mole is : 7459.40\n"
     ]
    }
   ],
   "source": [
    "import math \n",
    "from scipy.integrate import quad \n",
    "\n",
    "# Variables\n",
    "T1= 500;\t\t\t# in K\n",
    "T2= 2000;\t\t\t# in K\n",
    "m=1;\t\t\t# in kg\n",
    "\n",
    "# Calculations\n",
    "def f2(T): \n",
    "    return 11.515-172/math.sqrt(T)-1530/T\n",
    "\n",
    "delta_H=m*  quad(f2,T1,T2)[0]\n",
    "\n",
    "# Results\n",
    "print \"Change in enthalpy in kcal/kg mole is : %.2f\"%delta_H\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2.17 Page No : 59"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The value of Cv in kJ/kg-K is :  0.178\n",
      "The value of Cp in kJ/kg-K is :  1.005\n"
     ]
    }
   ],
   "source": [
    "from scipy.misc import derivative\n",
    "\n",
    "# Variables\t\t\t\n",
    "duBydt= 0.718;\n",
    "\n",
    "# Calculations\n",
    "\n",
    "def f1(t):\n",
    "    return 196. + 0.178 * t\n",
    "\n",
    "Cv = round(derivative(f1,1.0, dx=1e-6),3)\n",
    "\n",
    "def f2(t):\n",
    "    return  273.351 + 1.005*t\n",
    "\n",
    "Cp = round(derivative(f2,1.0, dx=1e-6),3)\n",
    "# Results\n",
    "print \"The value of Cv in kJ/kg-K is : \",Cv\n",
    "print \"The value of Cp in kJ/kg-K is : \",Cp\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
