{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7.12 Orifice Size for Given Pressure Drop and Velocity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A 12\" schedule 40 steel pipe is 60 feet long; it serves\n",
    "to connect water from a reservoir to the atmosphere.\n",
    "The water is 60 deg F and the elevation distance of the pipe\n",
    "is 12 feet.\n",
    "\n",
    "The pipe also contains a gate valve 10' from the entrance. The entrance into the reservoir is at a distance from the wall.\n",
    "\n",
    "Find the diameter of a standard orifice plate to make the flow velocity exactly 10 ft/s.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2022-08-22T01:18:03.222609Z",
     "iopub.status.busy": "2022-08-22T01:18:03.222057Z",
     "iopub.status.idle": "2022-08-22T01:18:04.154376Z",
     "shell.execute_reply": "2022-08-22T01:18:04.153348Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numerical solver progress:\n",
      "131456.15219585435 0.5 0.6039115256189872\n",
      "131207.12176475654 0.50015 0.6039153509057876\n",
      "45473.862634446166 0.5791807761904849 0.6054825449702658\n",
      "22370.32441146282 0.6210574900828658 0.6055902955169561\n",
      "7540.117114796503 0.6616052047236758 0.6047579998102253\n",
      "1943.8952815589291 0.6822208651128452 0.603821274023946\n",
      "240.43355375056126 0.689381890202793 0.6033941586008842\n",
      "9.046411468669248 0.6903926265655572 0.6033292369143312\n",
      "0.0442207298365247 0.6904321427501375 0.6033266747856294\n",
      "8.184524631360546e-06 0.6904323368622791 0.6033266621954346\n",
      "4.729372449219227e-11 0.6904323368982127 0.6033266621931039\n",
      "Size is:\n",
      "8.241152594519694 inch\n"
     ]
    }
   ],
   "source": [
    "from fluids.units import *\n",
    "from fluids.constants import g\n",
    "from math import pi\n",
    "from scipy.optimize import newton\n",
    "g = g*u.m/u.s**2\n",
    "\n",
    "dH = 12*u.foot\n",
    "L = (60)*u.foot\n",
    "mu = 1.1*u.cP\n",
    "rho = 62.364*u.lb/u.ft**3\n",
    "\n",
    "NPS, Di, Do, t = nearest_pipe(Do=12*u.inch, schedule='40')\n",
    "\n",
    "v = 10*u.foot/u.s\n",
    "A = 0.25*pi*Di**2\n",
    "m = v*A*rho\n",
    "Re = rho*v*Di/mu\n",
    "\n",
    "# Approximately match their friction factor, roughness not specified\n",
    "fd = friction_factor(Re=Re, eD=0.0006*u.inch/Di)\n",
    "ft = ft_Crane(Di) # Not needed\n",
    "\n",
    "K_entrance = entrance_distance(Di=Di, method='Crane')\n",
    "K_exit = exit_normal()\n",
    "K_gate = K_gate_valve_Crane(D1=Di, D2=Di, angle=0.0*u.degrees)\n",
    "\n",
    "K_tot = K_entrance + K_exit + K_gate\n",
    "K_tot += K_from_f(fd=fd, L=L, D=Di)\n",
    "\n",
    "dP = dP_from_K(K=K_tot, rho=rho, V=v)\n",
    "k = 1.33\n",
    "\n",
    "dP_gravity = rho*g*dH\n",
    "\n",
    "dP_meter =  dP_gravity - dP\n",
    "meter_type = 'ISO 5167 orifice'\n",
    "orifice_taps = 'corner'\n",
    "\n",
    "# P2 is assumed - does have an impact but it is minimal\n",
    "P2 = 1e6*u.Pa\n",
    "P1 = (P2 + dP_meter*2.5)\n",
    "print('Numerical solver progress:')\n",
    "def to_solve(beta):\n",
    "    # Do not know orifice measured dP or C, only actual dP  \n",
    "    D2 = Di*beta\n",
    "    \n",
    "    # Solve for upstream pressure given actual flow rate and guessed diameter\n",
    "    P1 = differential_pressure_meter_solver(Di, rho, mu, k, D2=D2, P2=P2, \n",
    "                                       m=m, meter_type=meter_type, \n",
    "                                       taps=orifice_taps)\n",
    "    \n",
    "    # Calculate `C`\n",
    "    C, expansibility = differential_pressure_meter_C_epsilon(Di, D2, m, P1, P2, rho, mu, k, \n",
    "                                              meter_type=meter_type, taps=orifice_taps)\n",
    "    \n",
    "    # Caclulate what the guessed orifice actual dP is\n",
    "    calc_dP = dP_orifice(D=Di, Do=D2, P1=P1, P2=P2, C=C)\n",
    "    err = (calc_dP - dP_meter).to_base_units()\n",
    "    print(err.magnitude, beta, C.magnitude)\n",
    "    return err.magnitude\n",
    "beta = newton(to_solve, .5, tol=1e-13)\n",
    "print('Size is:')\n",
    "print((Di*beta).to(u.inch))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The answer given in Crane is 7.94 inches, but only three iterations are performed there."
   ]
  }
 ],
 "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.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
