{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Engines and fuels"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Otto cycle\n",
    "* Gasoline engines\n",
    "* 4-stroke cycle\n",
    "    1. Intake\n",
    "    2. Compression\n",
    "        1. *Combustion at the top*\n",
    "    3. Expansion/power\n",
    "    4. Exhaust "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Idealized cycle\n",
    "<img src=\"http://ignite.byu.edu/che522/lectures/figs/pv_otto.png\" width=400>\n",
    "\n",
    "* 1$\\rightarrow$ 2: ideal compression\n",
    "    * Adiabatic, PV work required by the engine.\n",
    "* 2$\\rightarrow$ 3: sensible energy added through burning of the fuel/air mixture\n",
    "    * Combustion assumed to occur quickly, at constant volume. Pressure rises.    \n",
    "* 3$\\rightarrow$ 4: ideal expansion\n",
    "    * Adiabatic, PV work produced by the engine.\n",
    "* 4$\\rightarrow$ 1: heat rejection\n",
    "    * drop in pressure as the valve is opened at the bottom of the stroke.\n",
    "    * exhaust and intake are the horizontal blue and green lines.\n",
    "\n",
    "#### Net work\n",
    "* The net work produced is the area under curve 3$\\rightarrow$4 minus the area under curve 1$\\rightarrow$2.\n",
    "* Since the compression and expansion are adiabatic, the work is equal to differences in internal energy $u$.\n",
    "* Work required for compression: $W_c=u_2-u_1$.\n",
    "* Work done by expansion: $W_e = u_3-u_4$.\n",
    "* Net work performed by the engine = $W_e-W_c = (u_3-u_4)-(u_2-u_1)$.\n",
    "\n",
    "**Note** that $u_2=u_3$ since process 2$\\rightarrow$3 is adiabatic and constant volume. \n",
    "The \"heat added\" step is really sensible energy generated by combustion. But the internal energy of the mixture remains fixed.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Adiabatic compression and expansion.\n",
    "\n",
    "* Assume ideal gas and adiabatic compression/expansion.\n",
    "\n",
    "$$du = dW = -PdV$$\n",
    "\n",
    "* $du = c_vdT$\n",
    "$$c_vdT = -PdV$$\n",
    "\n",
    "* $P = RT/V$, rearrange\n",
    "\n",
    "$$\\frac{dT}{T} = -\\frac{R}{c_v}\\frac{dV}{V}$$\n",
    "\n",
    "* $R = c_p-c_v$, $\\gamma = c_p/c_v$.\n",
    "* $\\rightarrow$ $R/c_v = \\gamma -1$.\n",
    "\n",
    "$$\\frac{dT}{T} = -(\\gamma-1)\\frac{dV}{V}$$\n",
    "* Assume $\\gamma$ is constant. Then integrate to\n",
    "\n",
    "$$\\ln\\frac{T_2}{T_1} = -(\\gamma-1)\\ln\\frac{V_2}{V_1}$$\n",
    "* Rewrite as\n",
    "<font color=\"blue\">\n",
    "$$\\frac{T_2}{T_1} = \\left(\\frac{V_1}{V_2}\\right)^{\\gamma-1}$$\n",
    "</font>\n",
    "\n",
    "* Using $P=RT/V$, and expressions for $\\gamma$ above, we have\n",
    "<font color=\"blue\">\n",
    "$$\\frac{T_2}{T_1} = \\left(\\frac{P_2}{P_1}\\right)^{(\\gamma-1)/\\gamma}$$\n",
    "</font>\n",
    "\n",
    "* Similarly\n",
    "<font color=\"blue\">\n",
    "$$P_1V_1^\\gamma = P_2V_2^\\gamma$$\n",
    "</font>\n",
    "\n",
    "* If we relax the assumption that $\\gamma$ is constant, we have\n",
    "<font color=\"green\">\n",
    "$$\\ln\\frac{V_2}{V_1} = -\\int_{T_1}^{T_2}\\frac{c_v(T)}{RT}dT$$\n",
    "</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example\n",
    "* Compute the work and efficiency on a lower heating value basis for an Otto cycle with a stoichiometric methane-air mixture. \n",
    "* Use a compression ratio (CR) of 10. That is $V_1/V_2 = V_4/V_3 = 10$\n",
    "* **code below requires [streams.py](http://ignite.byu.edu/che633/lectureNotes/streams.py) and cantera**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NO = 7249.681704\n",
      "1581.0497304280145 541668.9741244189\n",
      "1834.455938236 619897.2037123365\n",
      "NO = 193.1\n",
      "η  = 0.43\n",
      "P1 = 1.00 atm,  T1 = 300 K\n",
      "P2 = 23.02 atm, T2 = 691 K\n",
      "P3 = 96.24 atm, T3 = 2846 K\n",
      "P4 = 5.35 atm,  T4 = 1581 K\n"
     ]
    }
   ],
   "source": [
    "from streams import streams\n",
    "\n",
    "strm = streams({\"O2\":1,\"N2\":3.76}, {\"CH4\":1}, 300, 300, 101325, \"gri30.yaml\")\n",
    "CR = 10\n",
    "\n",
    "#----------- state 1: cold, atmospheric reactants\n",
    "strm.set_gas_mixing_state(strm.ξst)\n",
    "P1 = strm.gas.P\n",
    "T1 = strm.gas.T\n",
    "u1 = strm.gas.int_energy_mass\n",
    "\n",
    "#----------- state 2: compress reactants\n",
    "strm.set_gas_state_adiabatic_compression_expansion(1/CR)\n",
    "P2 = strm.gas.P\n",
    "T2 = strm.gas.T\n",
    "u2 = strm.gas.int_energy_mass\n",
    "\n",
    "#----------- state 3: burnt products\n",
    "strm.gas.equilibrate(\"UV\")\n",
    "P3 = strm.gas.P\n",
    "T3 = strm.gas.T\n",
    "u3 = strm.gas.int_energy_mass\n",
    "\n",
    "print(f'NO = {strm.gas.Y[strm.gas.species_index(\"NO\")]*1000000:0f}')\n",
    "\n",
    "#----------- state 4: expanded products\n",
    "strm.set_gas_state_adiabatic_compression_expansion(CR)\n",
    "P4 = strm.gas.P\n",
    "T4 = strm.gas.T\n",
    "u4 = strm.gas.int_energy_mass\n",
    "print(T4, P4)\n",
    "\n",
    "strm.gas.equilibrate(\"UV\")\n",
    "print(strm.gas.T, strm.gas.P)\n",
    "print(f'NO = {strm.gas.Y[strm.gas.species_index(\"NO\")]*1000000:.1f}')\n",
    "\n",
    "#----------- work and efficiency\n",
    "\n",
    "W   = (u3-u4) - (u2-u1)   # J/kg mixture\n",
    "LHV = strm.get_LHV_pCC()  # J/kg fuel\n",
    "\n",
    "η = W/strm.ξst/LHV        # efficiency; ξst is used to convert W from J/kg mix to J/kg fuel\n",
    "\n",
    "print(f\"η  = {η:.2f}\")\n",
    "\n",
    "print(f'P1 = {P1/101325:.2f} atm,  T1 = {T1:.0f} K')\n",
    "print(f'P2 = {P2/101325:.2f} atm, T2 = {T2:.0f} K')\n",
    "print(f'P3 = {P3/101325:.2f} atm, T3 = {T3:.0f} K')\n",
    "print(f'P4 = {P4/101325:.2f} atm,  T4 = {T4:.0f} K')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* A compression ratio of 5 gives $\\eta=0.33$\n",
    "    * $P_2=9$ atm, $T_2=546$ K\n",
    "* A compression ratio of 10 gives $\\eta=0.43$\n",
    "    * $P_2=23$ atm, $T_2=690$ K\n",
    "* At high CR, the high $T_2$ can lead to auto-ignition and engine knock, which can cause damage.\n",
    "* For methane-air, $\\gamma=1.37$\n",
    "\n",
    "### Octane rating\n",
    "* See [Wikipedia](https://en.wikipedia.org/wiki/Octane_rating) for lots of information\n",
    "* The octane number is the percent of iso-octane (by volume) in a mixture of iso-octane and n-heptane, that has the same knock characteristics as the given fuel. \n",
    "* A higher octane number indicates a fuel that is more resistant to knock.\n",
    "* There are several different measurement methods, including the \"Research Octane Number\" and the \"Motor Octane Number\", an average of which is used in the U.S.\n",
    "* It is possible to have an octane number [greater than 100](https://chemistry.stackexchange.com/questions/133487/how-is-octane-rating-defined-for-negative-values-and-those-over-100).\n",
    "* Higher octane numbers can tolerate higher compression ratios.\n",
    "* Elevation matters.\n",
    "\n",
    "#### [Review article on impact of fuel molecular structure on auto-ignition](https://doi.org/10.1016/j.pecs.2016.12.001)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Diesel cycle\n",
    "\n",
    "<img src=\"http://ignite.byu.edu/che522/lectures/figs/pv_diesel.png\" width=400>\n",
    "https://en.wikipedia.org/wiki/Diesel_cycle#/media/File:DieselCycle_PV.svg\n",
    "\n",
    "* 4 strokes\n",
    "* Compress only air $\\rightarrow$ hot\n",
    "* Then inject fuel $\\rightarrow$ autoignition, but the injection timing is controlled.\n",
    "* No knock issue, allows cheaper fuels.\n",
    "* High compression radio gives higher efficiency.\n",
    "    * CR = 12-24.\n",
    "* In the diagram above, the combustion step 2$\\rightarrow$ 3 occurs with some piston travel $\\approx$ constant pressure.\n",
    "* Heat steps $Q$ again refer to release of sensible energy, and exhaust/intake.\n",
    "* In the code above, change the ```equilibrate(\"UV\")``` to ```equilibrate(\"HP\")```\n",
    "\n",
    "### Other cycles\n",
    "* Brayton cycle (gas turbines)\n",
    "* Rankine cycle (steam cycle, e.g., coal plants)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fuels\n",
    "* Compare diesel fuel and gasoline\n",
    "* Diesel: about 25% aromatics, $C_{12}H_{23}$, ($C_{10}H_{20}$ - $C_{15}H_{28}$)\n",
    "* Gasoline: about 35% aromatics, $C_8H_{18}$, ($C_4$ - $C_{12}$)\n",
    "* $\\rho_D\\approx 0.85$ kg/L, versus $\\rho_G\\approx 0.72$ kg/L\n",
    "* Heating value: $HV_D\\approx 38.6$ MJ/L versus $HV_G\\approx 34.9$ MJ/L\n",
    "* Heating value: $HV_D\\approx 45.4$ MJ/kg versus $HV_G\\approx 48.5$ MJ/kg\n",
    "\n",
    "* On an equal mass basis, the large differences in efficiency are due to diesel having higher compression ratios.\n",
    "\n",
    "* Ethanol additives reduces hydrocarbon emissions (volatile organic compounds, VOCs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exhaust gas recirculation (EGR)\n",
    "\n",
    "* $NO_x$ is a pollutant formed at high temperatures.\n",
    "* To reduce $NO_x$, reduce peak flame temperatures. \n",
    "* One way to do this is by recirculating cool exhaust gases into the reaction mixture. This increases the heat capacity and reduces the flame temperature. \n",
    "    * Also reduces efficiency.\n",
    "* Very common in diesel engines.\n",
    "    * Up to 50% EGR\n",
    "* In SI engines, only 5-15% EGR\n",
    "\n",
    "## Recuperation/Regeneration\n",
    "\n",
    "* Raise flame temperatures by preheating reactants (air).\n",
    "* Exchange heat with hot combustion products.\n",
    "* High temperature applications, glass furnaces.\n",
    "\n",
    "<img src=\"http://ignite.byu.edu/che522/lectures/figs/glass_furnace.jpg\" width=400>\n",
    "https://www.glassonweb.com/news/nsg-cold-repair-float-glass-furnace-us"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.405768344464696"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1-(T1/T2)*(T4/T1-1)/(T3/T2-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
