{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Homework 10\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.optimize import root"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 1\n",
    "Use fsolve to find the two roots of the polynomial $f(x) = 2x^2 + 3x - 10$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 2\n",
    "\n",
    "Use `root` to find the solution of the following two equations:\n",
    "\n",
    "$$f(x,y) = 2x^{2/3}+y^{2/3}-9^{1/3},$$\n",
    "\n",
    "$$g(x,y) = \\frac{x^2}{4} + \\sqrt{y} - 1. $$\n",
    "\n",
    "Use an initial guess of $x_0=1$, $y_0$ = 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 3\n",
    "\n",
    "Species heat capacities are given by:\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).$$\n",
    "\n",
    "| Species | $a_0$           | $a_1$             | $a_2$            | $a_3$             | $a_4$             |\n",
    "|---------|-----------------|-------------------|------------------|-------------------|-------------------|\n",
    "| 'CH4'| 1.28392437e+00|  1.00851346e-02| -2.66241814e-06|  4.99192762e-11|  5.62132385e-14|\n",
    "| 'O2' | 2.92112373e+00|  2.25021636e-03| -1.33975172e-06|  3.97866288e-10| -4.37971416e-14|\n",
    "| 'N2' | 3.30542104e+00|  3.63227936e-04|  5.26679110e-07| -3.30019712e-10|  5.24837849e-14|\n",
    "| 'CO2'| 2.78249997e+00|  7.06457251e-03| -4.50912355e-06|  1.35740239e-09| -1.55514712e-13|\n",
    "| 'H2O'| 3.80784936e+00|  3.17295233e-04|  1.41348052e-06| -6.61323323e-10|  8.91786378e-14|\n",
    "\n",
    "The enthalpy of the species at temperature $T$ 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",
    "**Methane combustion is given by the following reaction**\n",
    "$$CH_4 + 2O_2 + 7.52N_2 \\rightarrow CO_2 + 2H_2O + 7.52N_2.$$\n",
    "* The reactants are at room temperature (298 K).\n",
    "* The products have the same enthalpy as the reactants, but are much hotter because internal energy in the chemical bonds has been converted to sensible energy.\n",
    "* The product temperature is called the **adiabatic flame temperature**, and without heat losses is the temperature of a stoichiometric methane (~natural gas) flame.\n",
    "\n",
    "**Find the adiabatic flame temperature in K and $^oF$** given by the following equation:\n",
    "\n",
    "$$h_{CH4}(298) + 2h_{O2}(298) + 7.52h_{N_2}(298) = h_{CO2}(T_{ad}) + 2h_{H2O}(T_{ad}) + 7.52h_{N_2}(T_{ad}).$$\n",
    "\n",
    "* This is one equation in one unknown.\n",
    "* $h_{CH4}(298)$, etc., refers to the enthalpy of the species at a temperature of 298 K.\n",
    "\n",
    "**In solving the problem, create a function `h(T,a,hf)` that returns the enthalpy at a given temperature `T` for a given species with coefficients `a` and `hf`.**\n",
    "- Then, `hCH4 = h(T,aCH4,hfCH4)` etc.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aCH4 = np.array([1.28392437e+00,  1.00851346e-02, -2.66241814e-06,  4.99192762e-11,  5.62132385e-14])\n",
    "aO2  = np.array([2.92112373e+00,  2.25021636e-03, -1.33975172e-06,  3.97866288e-10, -4.37971416e-14])\n",
    "aN2  = np.array([3.30542104e+00,  3.63227936e-04,  5.26679110e-07, -3.30019712e-10,  5.24837849e-14])\n",
    "aCO2 = np.array([2.78249997e+00,  7.06457251e-03, -4.50912355e-06,  1.35740239e-09, -1.55514712e-13])\n",
    "aH2O = np.array([3.80784936e+00,  3.17295233e-04,  1.41348052e-06, -6.61323323e-10,  8.91786378e-14])\n",
    "\n",
    "hfCO2 = -393509\n",
    "hfH2O = -241818\n",
    "hfO2  = 0\n",
    "hfN2  = 0\n",
    "hfCH4 = -74520\n",
    "\n",
    "#------------------------\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 4 \n",
    "\n",
    "We have a parallel pipe network with three pips. We want to find the flow rates $Q$ through each pipe. The flow rates are written in terms of the friction factors $f$ for each pipe.\n",
    "\n",
    "We have the following unknowns $Q_0$, $Q_1$, $Q_2$, $f_0$, $f_1$, $f_2$. \n",
    "\n",
    "We have the following six equations in these unknowns:\n",
    "\n",
    "$$Q_0 + Q_1 + Q_2 = Q_{tot},$$\n",
    "\n",
    "$$\\frac{f_0L_0\\rho}{2D_0}\\left(\\frac{4Q_0}{\\pi D_0^2}\\right)^2 = \\frac{f_1L_1\\rho}{2D_1}\\left(\\frac{4Q_1}{\\pi D_1^2}\\right)^2,$$\n",
    "\n",
    "$$\\frac{f_0L_0\\rho}{2D_0}\\left(\\frac{4Q_0}{\\pi D_0^2}\\right)^2 = \\frac{f_2L_2\\rho}{2D_2}\\left(\\frac{4Q_2}{\\pi D_2^2}\\right)^2,$$\n",
    "\n",
    "$$\\frac{1}{\\sqrt{f_0}} = -2\\log_{10}\\left(\\frac{\\epsilon_0}{3.7D_0} + \\frac{2.51\\mu\\pi D_0}{4\\rho Q_0\\sqrt{f_0}}\\right),$$\n",
    "\n",
    "$$\\frac{1}{\\sqrt{f_1}} = -2\\log_{10}\\left(\\frac{\\epsilon_1}{3.7D_1} + \\frac{2.51\\mu\\pi D_1}{4\\rho Q_1\\sqrt{f_1}}\\right),$$\n",
    " \n",
    "$$\\frac{1}{\\sqrt{f_2}} = -2\\log_{10}\\left(\\frac{\\epsilon_2}{3.7D_2} + \\frac{2.51\\mu\\pi D_2}{4\\rho Q_2\\sqrt{f_2}}\\right).$$\n",
    "\n",
    "The following quantities are given:\n",
    "\n",
    "$$Q_{tot} = 0.01333\\,m^3/s,$$\n",
    "$$L_0 = 100\\,{m},$$\n",
    "$$L_1 = 150\\,{m},$$\n",
    "$$L_2 = 80\\,{m},$$\n",
    "$$D_0 = 0.05\\,{m},$$\n",
    "$$D_1 = 0.045\\,{m},$$\n",
    "$$D_2 = 0.04\\,{m},$$\n",
    "$$\\epsilon_0 = 0.00024\\,{m},$$\n",
    "$$\\epsilon_1 = 0.00012\\,{m},$$\n",
    "$$\\epsilon_2 = 0.0002\\,{m},$$\n",
    "$$\\mu = 1.002\\times 10^{-3}\\,kg/(m\\cdot s),$$\n",
    "$$\\rho = 998\\,kg/m^3.$$\n",
    "\n",
    "It is a good idea to make your guesses consistent. That, make sure your guesses for $Q_0$, $Q_1$, and $Q_2$ add up to the given $Q_{tot}$. Reasonable guesses for $f_0$, $f_1$, $f_2$ are 0.01. \n",
    "\n",
    "Notes: \n",
    "* The first equation says the total flow rate is the sum of the flow rates through each pipe. \n",
    "* The second equation says that the pressure drop through pipe 0 is the same as that through pipe 1, since pipes 0 and 1 are connected at their ends.\n",
    "* The third equation says that the pressure drop through pipe 1 is the same as that through pipe 2, since pipes 1 and 2 are connected at their ends.\n",
    "* The last three equations relate the friction factor in each pipe to its flow rate and pipe properties.\n",
    "* In the second two equations, $4Q/(\\pi D^2)$ is just the velocity in the pipe. But we substituted $v=4Q/(\\pi D^2)$ for convenience even though it makes the equations a bit harder to read. An alternative would be to define local terms like $v_0 = 4Q_0/(\\pi D_0^2)$, and then use $v_0$ in the equations above instead of $4Q_0/(\\pi D_0^2)$.\n",
    "* In the friction factor equations, $\\mu\\pi D/(4\\rho Q)$ is just $1/Re$. We made the substitution for convenience, but we could have written local variables for the $Re$'s and then used the $Re$'s in the equations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "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": 1
}
