{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 4 : First Law of Thermodynamics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4.1 Page No : 80"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Work done by the pump upon the water in MJ is : 90\n"
     ]
    }
   ],
   "source": [
    "import math \n",
    "\n",
    "# Variables\t\t\t\n",
    "p = 1.0;\t\t\t# in MPa\n",
    "p = p * 10**6;\t\t\t# in N per m**2\n",
    "del_v = 1.5;\t\t\t# in m**3 per min\n",
    "\n",
    "# Calculations\n",
    "del_v = del_v * 60;\t\t\t# in m**3 per h\n",
    "W = p * del_v;\t\t\t# in J\n",
    "W = W * 10**-6;\t\t\t# in MJ\n",
    "\n",
    "# Results\n",
    "print \"Work done by the pump upon the water in MJ is : %.0f\"%W\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4.2 Page No : 81"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Height from which the mass should fall in meter is : 89.63\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "# w = 2*g*h\n",
    "g = 9.81;\n",
    "m =(0.2+10./1000)*10**3 ;\t\t\t# in gm\n",
    "s = 1;\t\t\t# in cal/gm°C\n",
    "del_T = 2;\t\t\t# in °C\n",
    "\n",
    "# Calculations\n",
    "H = m * s * del_T;\t\t\t# in cal\n",
    "H = H * 10**-3;\t\t\t# kcal\n",
    "J = 4.1868 * 1000;\n",
    "# W= 2*g*h= J*H\n",
    "h = J*H/(2 * g);\t\t\t# in m\n",
    "\n",
    "# Results\n",
    "print \"Height from which the mass should fall in meter is : %.2f\"%h\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4.3 Page No : 83"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Third Heat transfer in kJ is  -5\n",
      "That is Third Heat transfer is of  5  kJ from the fluid\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "W1 = -25;\t\t\t# in kJ\n",
    "W2 = 45;\t\t\t# in kJ\n",
    "Q1 = 65;\t\t\t# in kJ\n",
    "Q2 = -40;\t\t\t# in kJ\n",
    "\n",
    "# Calculations\n",
    "# del_U = Q - W and but for a cycle del_U = 0, So\n",
    "# Q = W\n",
    "# Q1 + Q2 +Q3 = W1 +W2\n",
    "Q3 = W1 + W2 - Q1 - Q2;\t\t\t# in kJ\n",
    "\n",
    "# Results\n",
    "print \"Third Heat transfer in kJ is \",Q3\n",
    "print \"That is Third Heat transfer is of \",abs(Q3),\" kJ from the fluid\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4.5 Page No : 98"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The external work done in kcal is : 14.2\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\t\t\t\n",
    "m = 1.5;\t\t\t# in kg\n",
    "T1 = 90;\t\t\t# in °C\n",
    "T1 = T1 + 273;\t\t\t#in K\n",
    "T2 = 225;\t\t\t# in °C\n",
    "T2 = T2 + 273;\t\t\t# in K\n",
    "C_p = 0.24;\n",
    "C_v = 0.17;\n",
    "\n",
    "# Calculations\n",
    "Q = (m * C_p * (T2-T1));\t\t\t# in kcal\n",
    "del_U = (m * C_v * (T2-T1));\t\t\t# in kcal\n",
    "W = Q - del_U;\t\t\t# in kcal\n",
    "\n",
    "# Results\n",
    "print \"The external work done in kcal is : %.1f\"%W\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4.7 Page No : 98"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mass of gas in mole is : 24.20\n",
      "Temperature at the end of compression in °C is : 286.5\n",
      "Value of index n of compression is : 1.3\n",
      "Increase in internal energy of gas in kJ is : 56.2\n",
      "Heat interaction in kJ is : -72.24\n",
      "The -ve sign indicates heat rejection during the process\n"
     ]
    }
   ],
   "source": [
    "import math \n",
    "\n",
    "# Variables\n",
    "v1 = 0.5;\t\t\t# in m**3\n",
    "v2 = 0.125;\t\t\t# in m**3\n",
    "p1 = 1.5;\t\t\t# in bar\n",
    "p1 = p1 * 10**5;\t\t\t# in N per m**2\n",
    "p2 = 9.;\t\t\t#in bar\n",
    "p2 = p2 * 10**5;\t\t\t# in N per m**2\n",
    "T1 = 100.;\t\t\t# in °C\n",
    "T1 = T1 + 273;\t\t\t# in K\n",
    "R = 8.31;\n",
    "\n",
    "# Calculations and Results\n",
    "# Formula p1*v1= n*R*T1\n",
    "n= p1*v1/(R*T1);\t\t\t# in mole\n",
    "print \"Mass of gas in mole is : %.2f\"%n\n",
    "\n",
    "# Part (b)\n",
    "# Formula p1*v1/T1 = p2*v2/T2\n",
    "T2 = (p2 * v2 * T1)/(p1 * v1);\t\t\t# in K\n",
    "print \"Temperature at the end of compression in °C is : %.1f\"%(T2-273)\n",
    "\n",
    "# Part (c)\n",
    "# Formula p1*v1**n = p2*v2**n\n",
    "n1= math.log(p2/p1)/math.log(v1/v2)\n",
    "print \"Value of index n of compression is :\",round(n1,1)\n",
    "\n",
    "# Part (d)\n",
    "F = 3;\n",
    "C_v =1./2*R*F;\n",
    "del_U = (n * C_v * (T2-T1));\t\t\t# in J\n",
    "print \"Increase in internal energy of gas in kJ is : %.1f\"%(del_U*10**-3)\n",
    "\n",
    "# Part(e)\n",
    "Gamma = 1.67;\n",
    "Q_12 = n*(Gamma-n1)/(1-n1)*R*(T2-T1)/(Gamma-1);\t\t\t# in J\n",
    "Q_12 = Q_12 * 10**-3;\t\t\t        # in kJ\n",
    "print \"Heat interaction in kJ is : %.2f\"%Q_12\n",
    "\n",
    "if Q_12<0:\n",
    "    print \"The -ve sign indicates heat rejection during the process\"\n",
    "\n",
    "# Note: There is some difference between the answer of book and coding . Both the answer is right but accurate answer is of coding.\n",
    "# Because in the book, the intermediate values taken are appox. and in the coding the values taken are accurate\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4.8 Page No : 99"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Part (a)    The value of n is : 1.34\n",
      "The process followed during air compression is POLYTROPIC\n",
      "Part (b)    The law of the process is 1.34 p*v**  = constant\n",
      "Part (c)    Work done during the process in kJ is : -73.5\n",
      "The -ve sign indicates that the work has been done on the system\n",
      "Part (d)    Heat transfer during the process in kJ is : -11.029\n",
      "The -ve sign indicates that the heat is rejected from the system\n"
     ]
    }
   ],
   "source": [
    "import math \n",
    "\n",
    "# Variables\n",
    "p1 = 0.01;\t\t\t# in N/mm**2\n",
    "p1 = p1 * 10**3;\t\t\t# in kN/m**2\n",
    "p2 = 50.;\t\t\t# in kN/m**2\n",
    "v1 = 5.;\t\t\t# in m**3\n",
    "v2 = 1.5;\t\t\t# in m**3\n",
    "Gamma = 1.4;\n",
    "\n",
    "# Calculations and Results\n",
    "# Formula p1*v1**n = p2*v2**n\n",
    "n= round(math.log(p2/p1)/math.log(v1/v2),2)\n",
    "print \"Part (a)    The value of n is : %.2f\"%n\n",
    "print (\"The process followed during air compression is POLYTROPIC\");\n",
    "\n",
    "# Part (b)\n",
    "print \"Part (b)    The law of the process is %.2f p*v**\"%n,\" = constant\"\n",
    "\n",
    "# Part (c)\n",
    "W= (p1*v1-p2*v2)/(n-1);\t\t\t# in kNm or (kJ)\n",
    "print \"Part (c)    Work done during the process in kJ is : %.1f\"%W\n",
    "print (\"The -ve sign indicates that the work has been done on the system\")\n",
    "\n",
    "# Part (d)\n",
    "Q = ((Gamma - n)/(Gamma - 1) * W);\t\t\t# in kJ\n",
    "print \"Part (d)    Heat transfer during the process in kJ is : %.3f\"%Q\n",
    "print (\"The -ve sign indicates that the heat is rejected from the system\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4.9 Page No : 101"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Change in internal energy in kJ is 60.0\n",
      "The value of a in kN/m**2 is  1160.0\n",
      "The value of b in kN/m**2 is  -800.0\n",
      "Work done in kJ is : 600.0\n",
      "The net heat transfer in kJ is :  660.0\n"
     ]
    }
   ],
   "source": [
    "from scipy.integrate import quad \n",
    "\n",
    "\n",
    "# Variables\n",
    "# Relation of specific internal energy of the gas\n",
    "# U= 1.5*p*v-85 kJ/kg\n",
    "p1 = 1000;\t\t\t# in kpa\n",
    "p2 = 200;\t\t\t# in pa\n",
    "v1 = 0.20;\t\t\t# in m**3\n",
    "v2 = 1.20;\t\t\t# in m**3\n",
    "m = 1.5;\t\t\t# in kg\n",
    "\n",
    "# Calculations and Results\n",
    "U1= 1.5*p1*v1-85;\t\t\t#  kJ/kg\n",
    "U2= 1.5*p2*v2-85;\t\t\t#  kJ/kg\n",
    "delU= U2-U1;\t    \t\t# in kJ\n",
    "print \"Change in internal energy in kJ is\",delU\n",
    "# p1= a+b*v1          (i)\n",
    "# p2= a+b*v2          (ii)\n",
    "# From eq(i) and (ii)\n",
    "b= (p1-p2)/(v1-v2);\t\t\t# in kN/m**2\n",
    "a= p1-b*v1;     \t\t\t# in kN/m**2 \n",
    "print \"The value of a in kN/m**2 is \",a\n",
    "print \"The value of b in kN/m**2 is \",b\n",
    "\n",
    "# Part (c)\n",
    "# Work done = integration of p w.r.t. v and p = a+b*v1\n",
    "\n",
    "def f5(v): \n",
    "    return a+b*v\n",
    "\n",
    "W=  quad(f5,v1,v2)[0]\n",
    "print \"Work done in kJ is :\",W\n",
    "\n",
    "# Part (d)\n",
    "Q= delU+W;\t\t\t# in kJ\n",
    "print \"The net heat transfer in kJ is : \",Q\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4.10 Page No : 102"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The maximum internal energy of the gas in kJ/kg is :  818.625\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "a= 1160.;\t\t\t# in kN/m**2\n",
    "b= -800.;\t\t\t# in kN/m**2\n",
    "\n",
    "# Calculations\n",
    "v= -a/(2*b)\n",
    "Umax= 1.5*(a*v+b*v**2)-85;\t\t\t# in kJ/kg\n",
    "# For 1.5 kg mass of gas it is\n",
    "Umax= Umax*1.5;\t\t\t# in kJ/kg\n",
    "\n",
    "# Results\n",
    "print \"The maximum internal energy of the gas in kJ/kg is : \",Umax\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4.11 Page No : 103"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The exit velocity of air in m/s is : 594.094\n",
      "Inlet area of the nozzle in square milimeter is : 31.89\n",
      "Exit area of nozzel in square milimeter is : 43.35\n"
     ]
    }
   ],
   "source": [
    "# Exa 4.11\n",
    "import math \n",
    "\n",
    "# Variables\n",
    "T1 = 127.;\t\t\t# in °C\n",
    "T1 = T1 + 273;\t\t\t# in K\n",
    "R = 287.;\n",
    "V1 = 300.;\t\t\t# in m/s\n",
    "p1 = 2.;\t\t\t# in MPa\n",
    "p2 = 0.5;\t\t\t# in MPa\n",
    "p1 = p1 * 10**6;\t\t\t# in Pa\n",
    "p2 = p2 * 10**6;\t\t\t# in Pa\n",
    "C_P = 1.005*10**3;\t\t\t# in J/ kg-K\n",
    "Gamma = 1.4;\n",
    "\n",
    "# Calculations and Results\n",
    "V2 = math.sqrt(2 * C_P *T1 *(1-(p2/p1)**((Gamma-1)/Gamma)) + V1**2);\t\t\t# in m/s\n",
    "print \"The exit velocity of air in m/s is : %.3f\"%V2\n",
    "\n",
    "m = 600.;\t\t\t# in kg/hr\n",
    "m = m / 3600;\t\t\t# in kg/sec\n",
    "v1 = (R * T1)/p1;\t\t\t# in m**3 per kg\n",
    "# m = (A1*V1)/v1 = (A2* V2)/v2\n",
    "A1 = (m * v1)/V1;\t\t\t# in m**2\n",
    "A1 = A1 * 10**6;\t\t\t# in mm**2\n",
    "print \"Inlet area of the nozzle in square milimeter is : %.2f\"%A1\n",
    "\n",
    "T2 = T1*(p2/p1)**((Gamma-1)/Gamma);\t\t\t# in K\n",
    "v2 = (R * T2)/(p2);\t\t\t# in m**3/kg\n",
    "A2 = (m * v2)/V2;\t\t\t# in m**2\n",
    "A2 = A2 * 10**6;\t\t\t# in mm**2\n",
    "print \"Exit area of nozzel in square milimeter is : %.2f\"%A2\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4.17 Page No : 106"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Net heat transfer for the system in MJ is :  -8.6\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "W = -1;\t\t\t# in kWh\n",
    "W = W * 10**3 * 3600;\t\t\t# in J\n",
    "del_U = -5000;\t\t\t# in kj\n",
    "\n",
    "# Calculations\n",
    "del_U = del_U * 10**3;\t\t\t# in J\n",
    "Q = del_U + W;\t\t\t# in J\n",
    "Q = Q * 10**-6;\t\t\t# in MJ\n",
    "\n",
    "# Results\n",
    "print \"Net heat transfer for the system in MJ is : \",Q\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4.20 Page No : 107"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Heat flows into the system along the path a b d in kJ is :  62.5\n",
      "Heat liberated by the system in kJ is :  -73\n",
      "Heat absorbed in processes ad  in kJ is :  52.5\n",
      "Heat absorbed in processes bd in kJ is :  10\n",
      "Heat absorbed in processes in ad and db in kJ is :  10.5\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "Q_acb = 84;\t\t\t#in kJ\n",
    "W_acb = 32;\t\t\t# in kJ\n",
    "\n",
    "# Calculations and Results\n",
    "#Formula Q_acb = del_U+W_acb where del_U = U_b - U_a;\n",
    "del_U = Q_acb - W_acb;\t\t\t# in kJ\n",
    "# Part (a) Path a b d\n",
    "W_abd = 10.5;\t\t\t# in kJ\n",
    "Q_abd = del_U + W_abd;\t\t\t# in kJ\n",
    "print \"Heat flows into the system along the path a b d in kJ is : \",Q_abd\n",
    "\n",
    "# Part (b) curved path b a\n",
    "W_ba = -(21);\t\t\t# in kJ\n",
    "Q_ba = -(del_U) + W_ba;\t\t\t# in kJ\n",
    "print \"Heat liberated by the system in kJ is : \",(Q_ba)\n",
    "\n",
    "# Part (c) process a b and d b\n",
    "W_ad = 10.5;\t\t\t# in kJ\n",
    "del_U1 = 42;\t\t\t# in kJ\n",
    "Q_ad = del_U1 + W_ad;\t\t\t# in kJ\n",
    "print \"Heat absorbed in processes ad  in kJ is : \",Q_ad\n",
    "\n",
    "W_db = -(42);\t\t\t# in kJ\n",
    "del_U2 = 52;\t\t\t# in kJ\n",
    "Q_bd = del_U2 + W_db;\t\t\t# in kJ\n",
    "print \"Heat absorbed in processes bd in kJ is : \",Q_bd\n",
    "\n",
    "W_db = 0;\n",
    "W_abd = W_ad + W_db;\t\t\t# in kJ\n",
    "print \"Heat absorbed in processes in ad and db in kJ is : \",W_abd\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4.21 Page No : 108"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "net work done in kJ is : -94.02\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Variables\n",
    "v1 = 5.;\t\t\t# in m**3\n",
    "p1 = 2.;\t\t\t# in bar\n",
    "p2 = 6.;\t\t\t# in bar\n",
    "p3 = 2.;\t\t\t# in bar\n",
    "\n",
    "# Calculations\n",
    "p1 = p1 * 10**5;\t\t\t# in N/m**2\n",
    "p2 = p2 * 10**5;\t\t\t# in N/m**2\n",
    "p3 = p3 * 10**5;\t\t\t# in N/m**2\n",
    "n = 1.3;\n",
    "v2 = v1 * ((p1/p2)**(1/1.3));\t\t\t# in m**3\n",
    "W1_2 = ((p2 * v2)-(p1 * v1))/(1-n);\t\t\t# in J\n",
    "Gamma = 1.4;\n",
    "v3 = v2 * ((p2/p3)**(1/Gamma));\t\t\t# in m**3\n",
    "W2_3 = ((p3 * v3) - (p2 * v2))/(1-Gamma);\t\t\t# in J\n",
    "W_net = W1_2 + W2_3;\t\t\t# in J\n",
    "W_net = W_net * 10**-3;\t\t\t# in kJ\n",
    "\n",
    "# Results\n",
    "print \"net work done in kJ is : %.2f\"%W_net\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
}
