{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Homework 15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "from scipy.integrate import quad\n",
    "from scipy.optimize import root"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem 1\n",
    "\n",
    "- Use ```quad``` to solve the following integral:\n",
    "$$ I = \\int_0^{\\pi/2}\\cos(x)dx$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem 2\n",
    "\n",
    "Use ```quad``` to find the numerical integral of the following function:\n",
    "\n",
    "$$I = \\int_0^{2\\pi} e^{-x}\\sin(3x)dx.$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem 3\n",
    "\n",
    "Use ```quad``` to find the numerical integral of the following function:\n",
    "$$I = \\int_{x_1}^{x_2}f(x,a,b,c)dx,$$\n",
    "where,\n",
    "$$f(x,a,b,c) = ax^2 + bx + c.$$\n",
    "* That is, $f(x,a,b,c)$ is a polynomial function of $x$ with coefficients $a$, $b$, and $c$.\n",
    "\n",
    "* Solve this for $x_1=1$, $x_2=5$, and for coefficients $a=2$, $b=3$, $c=4$. Use `args`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem 4\n",
    "The following equation describes species heat capacities, with data following.\n",
    "\n",
    "$$c_{p,i}(T) = R_g(a_{0,i} + a_{1,i}T + a_{2,i}T^2 + a_{3,i}T^3 + a_{4,i}T^4 a_{5,i}T^5)$$\n",
    "\n",
    "\n",
    "| Species | $a_0$           | $a_1$             | $a_2$            | $a_3$             | $a_4$             | $a_5$         |\n",
    "|---------|-----------------|-------------------|------------------|-------------------|-------------------|---------------|\n",
    "| CO2     | 2.43285367e+00  |  8.73482125e-03   | -7.23410929e-06  |  3.31988589e-09   | -7.96674425e-13 |  7.75882050e-17 |\n",
    "| H2O     | 3.99372572e+00  | -5.71111971e-04   |  2.86341142e-06  | -1.70578598e-09   |  4.30471370e-13 | -4.13058207e-17 |\n",
    "| O2      | 3.03505333e+00  |  1.75425833e-03   | -5.83241162e-07  | -1.20878070e-10   |  1.19576187e-13 | -1.92204612e-17 |\n",
    "| N2      | 3.60896122e+00  | -1.09689742e-03   |  2.91988900e-06  | -2.05903734e-09   |  6.18649909e-13 | -6.86285024e-17 |\n",
    "| CH4     | 2.22727446e+00  |  5.71428010e-03   |  4.32080053e-06  | -4.90604232e-09   |  1.65822643e-12 | -1.92319706e-16 |\n",
    "\n",
    "The enthalpy of the species is given by\n",
    "\n",
    "$$h_i(T) = h_{f,i} + \\int_{T_{ref}}^{T}c_{p,i}(T)dT.$$\n",
    "- Here, $h_{f,i}$ is the heat of formation of the species at temperature $T_{ref}=298$. \n",
    "\n",
    "| Species | $h_{f,i}$ (J/mol)  |\n",
    "|---------|--------------------|\n",
    "| CO2     | -393509            |\n",
    "| H2O(g)  | -241818            |\n",
    "| O2      | 0                  |\n",
    "| N2      | 0                  |\n",
    "| CH4     | -74520             |\n",
    "\n",
    "### Part a\n",
    "\n",
    "Store the $a$ and $h_{f}$ arrays in dictionaries with the species names as keys.\n",
    "\n",
    "### Part b\n",
    "\n",
    "Write a function to compute $c_p$ that takes the species name and temperature as arguments. It will use your dictionary for the $a$.\n",
    "\n",
    "### Part c\n",
    "\n",
    "Write a function to compute the enthalpy of a species given the species name and temperature as arguments. It should use `quad` and call your $c_p$ function from Part b, and also use your dictionary for $h_{f,i}$.\n",
    "\n",
    "Use your function to find the enthalpy of CO2 at T=1000 K.\n",
    "\n",
    "### Part e\n",
    "Write a function to compute a mixture enthalpy given a dictionary that maps species names to mole fractions, and temperature, where $h_{mix}(x,T)$ is defined as\n",
    "\n",
    "$h_{mix}(x,T) = \\sum_i x_ih_i(T)$.\n",
    "\n",
    "This function will use the $x$ dictionary, and your enthalpy function from Part c.\n",
    "\n",
    "### Part d\n",
    "- Methane combustion follows the reaction \n",
    "$$CH_4 + 2O_2 + 7.52 N_2 \\rightarrow CO_2 + 2H_2O + 7.52N_2$$\n",
    "- The reactant mole fractions are: $x_{CH4}$= 1/(1+2+7.52)=0.095, $x_{O2}$ = 2/(1+2+7.52)=0.19, $x_{N2}$=7.52/(1+2+7.52)=0.715 \n",
    "    - Similarly, for the products.\n",
    "- The adiabatic flame temperature (product temperature) is found by equating the reactant mixture enthalpy at the reactant temperature of 300 K with the product mixture enthalpy at the unknown temperature $T_{ad}$. Use `root` to solve this equation: $h_r(300) = h_p(T_{ad})$. You'll need to make use of the mixture function from Part $d$. \n",
    "\n",
    "### Note\n",
    "The whole point of this problem is to find the adiabatic flame temperature for stoichiometric methane combustion given the combustion reaction, and the $c_p$ polynomial coefficients (the a's), and the heats of formation of the species. Parts a-d are just well-considered organizational steps along the path to solution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "#--------- Part a\n",
    "\n",
    "\n",
    "#--------- Part b\n",
    "\n",
    "\n",
    "#--------- Part c\n",
    "\n",
    "\n",
    "#--------- Part d\n",
    "\n",
    "#--------- Part e\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "base",
   "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.14.7"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
