{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Cantera Example: Heating values\n",
    "## Heating value of Methane\n",
    "The complete reaction for heating methane is:\n",
    "\n",
    "$\\mathrm{CH_4+2O_2\\rightarrow CO_2+2H_2O}$\n",
    "\n",
    "We compute the lower heating value (LHV) as the difference in enthalpy (per kg *mixture*) between reactants and products at constant temperature and pressure, divided by the mass fraction of fuel in the reactants."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LHV = 50.026 MJ/kg\n"
     ]
    }
   ],
   "source": [
    "import cantera as ct\n",
    "gas = ct.Solution('gri30.cti')\n",
    "\n",
    "# Set reactants state\n",
    "gas.TPX = 298, 101325, 'CH4:1, O2:2'\n",
    "h1 = gas.enthalpy_mass\n",
    "Y_CH4 = gas['CH4'].Y[0] # returns an array, of which we only want the first element\n",
    "\n",
    "# set state to complete combustion products without changing T or P\n",
    "gas.TPX = None, None, 'CO2:1, H2O:2' \n",
    "h2 = gas.enthalpy_mass\n",
    "\n",
    "print('LHV = {:.3f} MJ/kg'.format(-(h2-h1)/Y_CH4/1e6))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The LHV is calculated assuming that water remains in the gas phase. However, more energy can be extracted from the mixture if this water is condensed. This value is the higher heating value (HHV).\n",
    "\n",
    "The ideal gas mixture model used here cannot calculate this contribution directly. However, Cantera also has a non-ideal equation of state which can be used to compute this contribution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HHV = 55.512 MJ/kg\n"
     ]
    }
   ],
   "source": [
    "water = ct.Water()\n",
    "# Set liquid water state, with vapor fraction x = 0\n",
    "water.TX = 298, 0\n",
    "h_liquid = water.h\n",
    "# Set gaseous water state, with vapor fraction x = 1\n",
    "water.TX = 298, 1\n",
    "h_gas = water.h\n",
    "\n",
    "# Calculate higher heating value\n",
    "Y_H2O = gas['H2O'].Y[0]\n",
    "print('HHV = {:.3f} MJ/kg'.format(-(h2-h1 + (h_liquid-h_gas) * Y_H2O)/Y_CH4/1e6))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generalizing to arbitrary species\n",
    "We can generalize this calculation by determining the composition of the products automatically rather than directly specifying the product composition. This can be done by computing the *elemental mole fractions* of the reactants mixture and noting that for complete combustion, all of the carbon ends up as CO$_2$, all of the hydrogen ends up as H$_2$O, and all of the nitrogen ends up as N$_2$. From this, we can compute the ratio of these species in the products."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fuel   LHV (MJ/kg)   HHV (MJ/kg)\n",
      "H2       119.959      141.788\n",
      "CH4       50.026       55.512\n",
      "C2H6      47.511       51.901\n",
      "C3H8      46.352       50.344\n",
      "NH3       18.604       22.480\n",
      "CH3OH     21.104       23.851\n"
     ]
    }
   ],
   "source": [
    "def heating_value(fuel):\n",
    "    \"\"\" Returns the LHV and HHV for the specified fuel \"\"\"\n",
    "    gas.TP = 298, ct.one_atm\n",
    "    gas.set_equivalence_ratio(1.0, fuel, 'O2:1.0')\n",
    "    h1 = gas.enthalpy_mass\n",
    "    Y_fuel = gas[fuel].Y[0]\n",
    "\n",
    "    # complete combustion products\n",
    "    Y_products = {'CO2': gas.elemental_mole_fraction('C'),\n",
    "                  'H2O': 0.5 * gas.elemental_mole_fraction('H'),\n",
    "                  'N2': 0.5 * gas.elemental_mole_fraction('N')}\n",
    "\n",
    "    gas.TPX = None, None, Y_products\n",
    "    Y_H2O = gas['H2O'].Y[0]\n",
    "    h2 = gas.enthalpy_mass\n",
    "    LHV = -(h2-h1)/Y_fuel\n",
    "    HHV = -(h2-h1 + (h_liquid-h_gas) * Y_H2O)/Y_fuel\n",
    "    return LHV, HHV\n",
    "\n",
    "fuels = ['H2', 'CH4', 'C2H6', 'C3H8', 'NH3', 'CH3OH']\n",
    "print('fuel   LHV (MJ/kg)   HHV (MJ/kg)')\n",
    "for fuel in fuels:\n",
    "    LHV, HHV = heating_value(fuel)\n",
    "    print('{:8s} {:7.3f}      {:7.3f}'.format(fuel, LHV/1e6, HHV/1e6))"
   ]
  }
 ],
 "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
