{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Saturation density specified"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Two residuals are \n",
      "\n",
      "1. Equating pressure of the two phases\n",
      "\n",
      "    $r_1 = \\delta''[1+\\delta''\\alpha_{\\delta}^r(\\tau,\\delta'')]-\\delta'[1+\\delta'\\alpha_{\\delta}^r(\\tau,\\delta')]$ \n",
      "\n",
      "2. Equating Gibbs energy of the two phases\n",
      "\n",
      "    $r_2 = \\delta''\\alpha_{\\delta}^r(\\tau,\\delta'')+\\alpha^r(\\tau,\\delta'')+\\ln\\delta''-[\\delta'\\alpha_{\\delta}^r(\\tau,\\delta')+\\alpha^r(\\tau,\\delta')+\\ln\\delta']$\n",
      "    \n",
      "Independent (unknown) variables are either $\\tau,\\delta'$ or $\\tau,\\delta''$.  Obtain all the partial derivatives:\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_1}{\\partial \\tau}\\right|_{\\delta',\\delta''} = (\\delta'')^2\\alpha^r_{\\delta\\tau}(\\tau,\\delta'')-(\\delta')^2\\alpha^r_{\\delta\\tau}(\\tau,\\delta')$\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_1}{\\partial \\delta''}\\right|_{\\tau,\\delta'} = [1+\\delta''\\alpha^r_{\\delta}(\\tau,\\delta'')]+\\delta''[\\delta''\\alpha^r_{\\delta\\delta}(\\tau,\\delta'')+\\alpha^r_{\\delta}(\\tau,\\delta'')] = 1+2\\delta''\\alpha^r_{\\delta}(\\tau,\\delta'')+(\\delta'')^2\\alpha^r_{\\delta\\delta}(\\tau,\\delta'')$\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_1}{\\partial \\delta'}\\right|_{\\tau,\\delta''} = -[1+\\delta'\\alpha^r_{\\delta}(\\tau,\\delta')]-\\delta'[\\delta'\\alpha^r_{\\delta\\delta}(\\tau,\\delta')+\\alpha^r_{\\delta}(\\tau,\\delta')] = -1-2\\delta'\\alpha^r_{\\delta}(\\tau,\\delta')-(\\delta')^2\\alpha^r_{\\delta\\delta}(\\tau,\\delta')$\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_2}{\\partial \\tau}\\right|_{\\delta',\\delta''} = \\delta''\\alpha^r_{\\delta\\tau}(\\tau,\\delta'') + \\alpha^r_{\\tau}(\\tau,\\delta'')-[\\delta'\\alpha^r_{\\delta\\tau}(\\tau,\\delta') + \\alpha^r_{\\tau}(\\tau,\\delta')]$\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_2}{\\partial \\delta''}\\right|_{\\tau,\\delta'} = \\delta''\\alpha^r_{\\delta\\delta}(\\tau,\\delta'')+\\alpha^r_{\\delta}(\\tau,\\delta'') +\\alpha^r_{\\delta}(\\tau,\\delta'') +\\displaystyle\\frac{1}{\\delta''} = \\delta''\\alpha^r_{\\delta\\delta}(\\tau,\\delta'')+2\\alpha^r_{\\delta}(\\tau,\\delta'') +\\displaystyle\\frac{1}{\\delta''} $\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_2}{\\partial \\delta'}\\right|_{\\tau,\\delta''} = -\\delta'\\alpha^r_{\\delta\\delta}(\\tau,\\delta')-\\alpha^r_{\\delta}(\\tau,\\delta') -\\alpha^r_{\\delta}(\\tau,\\delta') -\\displaystyle\\frac{1}{\\delta'} = -\\delta'\\alpha^r_{\\delta\\delta}(\\tau,\\delta')-2\\alpha^r_{\\delta}(\\tau,\\delta') -\\displaystyle\\frac{1}{\\delta'} $\n",
      "If $\\delta'$ is imposed, the Jacobian is given by\n",
      "\n",
      "$\\mathbf{J} = \\left[ \\begin{array}{cc} \\left.\\displaystyle\\frac{\\partial r_1}{\\partial \\tau}\\right|_{\\delta',\\delta''} & \\left.\\displaystyle\\frac{\\partial r_1}{\\partial \\delta''}\\right|_{\\tau,\\delta'} \\\\ \\left.\\displaystyle\\frac{\\partial r_2}{\\partial \\tau}\\right|_{\\delta',\\delta''} & \\left.\\displaystyle\\frac{\\partial r_2}{\\partial \\delta''}\\right|_{\\tau,\\delta'}\\end{array} \\right]$\n",
      "\n",
      "with the increment $\\mathbf{v}$ obtained from \n",
      "\n",
      "$\\mathbf{J}\\left[\\begin{array}{c}\\Delta \\tau \\\\ \\Delta\\delta''\\end{array}\\right] = -\\left[\\begin{array}{c}r_1 \\\\ r_2\\end{array}\\right]$\n",
      "\n",
      "If $\\delta''$ is imposed, the Jacobian is given by\n",
      "\n",
      "$\\mathbf{J} = \\left[ \\begin{array}{cc} \\left.\\displaystyle\\frac{\\partial r_1}{\\partial \\tau}\\right|_{\\delta',\\delta''} & \\left.\\displaystyle\\frac{\\partial r_1}{\\partial \\delta'}\\right|_{\\tau,\\delta''} \\\\ \\left.\\displaystyle\\frac{\\partial r_2}{\\partial \\tau}\\right|_{\\delta',\\delta''} & \\left.\\displaystyle\\frac{\\partial r_2}{\\partial \\delta'}\\right|_{\\tau,\\delta''}\\end{array} \\right]$\n",
      "\n",
      "with the increment $\\mathbf{v}$ obtained from \n",
      "\n",
      "$\\mathbf{J}\\left[\\begin{array}{c}\\Delta \\tau \\\\ \\Delta\\delta'\\end{array}\\right] = -\\left[\\begin{array}{c}r_1 \\\\ r_2\\end{array}\\right]$\n",
      "The method needs to start off with decent guesses for densities and temperature, which can be obtained by the ancillaries.  First solve $\\rho(T)=\\rho_{spec}$ to get $T$, then use $T$ to get the other density using the ancillary.  And then kick into the full Newton-Raphson solution.\n",
      "Or alternatively, use $\\ln\\delta'$ and $\\ln\\delta''$ as independent variables, would bring liquid an vapor density values closer in magnitude.  Rather than being 16 orders of magnitude different, they would be much closer to one order of magnitude in difference.  For instance:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Propane at triple point \n",
      "rhoL = 16625.80554 #mol/m3\n",
      "rhoV = 0.0000002419454458 #mol/m3\n",
      "from math import log10\n",
      "print log10(rhoL), log10(rhoV)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4.22078269657 -6.61628254831\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "That should help with the truncation error.  But make sure to distribute through the \\delta in order to avoid truncation in internal calculation"
     ]
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Saturation pressure/enthalpy/entropy/internal energy specified"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Three residuals are \n",
      "\n",
      "1. Equating pressure of the two phases\n",
      "\n",
      "    $r_1 = \\delta''[1+\\delta''\\alpha_{\\delta}^r(\\tau,\\delta'')]-\\delta'[1+\\delta'\\alpha_{\\delta}^r(\\tau,\\delta')]$ \n",
      "\n",
      "2. Equating Gibbs energy of the two phases\n",
      "\n",
      "    $r_2 = \\delta''\\alpha_{\\delta}^r(\\tau,\\delta'')+\\alpha^r(\\tau,\\delta'')+\\ln\\delta''-[\\delta'\\alpha_{\\delta}^r(\\tau,\\delta')+\\alpha^r(\\tau,\\delta')+\\ln\\delta']$\n",
      "    \n",
      "3. Specified value must equal the desired value\n",
      "\n",
      "    $r_3 = \\ln(X_S) - \\ln(X_{specified})$ for $p$\n",
      "    \n",
      "    $r_3 = X_S - X_{specified}$ for $h$, $s$, $u$\n",
      "    \n",
      "Three unknowns are $\\tau$, $\\ln\\delta'$, $\\ln\\delta''$.\n",
      "\n",
      "For the first two residuals, the partials can be obtained in the same way as for density imposed\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_1}{\\partial \\tau}\\right|_{\\delta',\\delta''} = (\\delta'')^2\\alpha^r_{\\delta\\tau}(\\tau,\\delta'')-(\\delta')^2\\alpha^r_{\\delta\\tau}(\\tau,\\delta')$\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_1}{\\partial \\delta''}\\right|_{\\tau,\\delta'} = 1+2\\delta''\\alpha^r_{\\delta}(\\tau,\\delta'')+(\\delta'')^2\\alpha^r_{\\delta\\delta}(\\tau,\\delta'')$\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_1}{\\partial \\delta'}\\right|_{\\tau,\\delta''} = -1-2\\delta'\\alpha^r_{\\delta}(\\tau,\\delta')-(\\delta')^2\\alpha^r_{\\delta\\delta}(\\tau,\\delta')$\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_2}{\\partial \\tau}\\right|_{\\delta',\\delta''} = \\delta''\\alpha^r_{\\delta\\tau}(\\tau,\\delta'') + \\alpha^r_{\\tau}(\\tau,\\delta'')-[\\delta'\\alpha^r_{\\delta\\tau}(\\tau,\\delta') + \\alpha^r_{\\tau}(\\tau,\\delta')]$\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_2}{\\partial \\delta''}\\right|_{\\tau,\\delta'} = \\delta''\\alpha^r_{\\delta\\delta}(\\tau,\\delta'')+2\\alpha^r_{\\delta}(\\tau,\\delta'') +\\displaystyle\\frac{1}{\\delta''} $\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_2}{\\partial \\delta'}\\right|_{\\tau,\\delta''} = -\\delta'\\alpha^r_{\\delta\\delta}(\\tau,\\delta')-2\\alpha^r_{\\delta}(\\tau,\\delta') -\\displaystyle\\frac{1}{\\delta'} $\n",
      "\n",
      "The third residual's partials when one of h,s,u are specified can be obtained from\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_3}{\\partial \\tau}\\right|_{\\delta', \\delta''} = \\left.\\displaystyle\\frac{\\partial X_S}{\\partial \\tau}\\right|_{\\delta',\\delta''}$\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_3}{\\partial \\delta'}\\right|_{\\tau,\\delta''} = \\left.\\displaystyle\\frac{\\partial X_S}{\\partial \\delta'}\\right|_{\\tau,\\delta''}$\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_3}{\\partial \\delta''}\\right|_{\\tau,\\delta'} = \\left.\\displaystyle\\frac{\\partial X_S}{\\partial \\delta''}\\right|_{\\tau,\\delta'}$\n",
      "\n",
      "Or when $\\ln p$ is specified\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_3}{\\partial \\tau}\\right|_{\\delta', \\delta''} = \\dfrac{1}{X_S}\\left.\\dfrac{\\partial X_S}{\\partial \\tau}\\right|_{\\delta',\\delta''}$\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_3}{\\partial \\delta'}\\right|_{\\tau,\\delta''} = \\dfrac{1}{X_S}\\left.\\dfrac{\\partial X_S}{\\partial \\delta'}\\right|_{\\tau,\\delta''}$\n",
      "\n",
      "$\\left.\\displaystyle\\frac{\\partial r_3}{\\partial \\delta''}\\right|_{\\tau,\\delta'} = \\dfrac{1}{X_S}\\left.\\dfrac{\\partial X_S}{\\partial \\delta''}\\right|_{\\tau,\\delta'}$\n"
     ]
    }
   ],
   "metadata": {}
  }
 ]
}