{
 "metadata": {
  "name": "",
  "signature": "sha256:8799af2ceaa4d644ca80e16c2a80db44d1efd7cf44d87a37dfdbbbf39659fba2"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Jacobians to get derivatives in terms of $\\tau$ and $\\delta$"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Based on the paper \"On the Use of Jacobians in Thermodynamics\" of Benjamin Carroll, Journal Chemical Education, 1965\n",
      "\n",
      "Express a partial derivative as\n",
      "$$\\left(\\frac{\\partial T}{\\partial P}\\right)_H = \\frac{\\partial(T,H)}{\\partial(P,H)} = \\left| \\begin{array}{cc} \\left(\\frac{\\partial T}{\\partial P}\\right)_H & \\left(\\frac{\\partial H}{\\partial P}\\right)_H \\\\ \\left(\\frac{\\partial T}{\\partial H}\\right)_P & \\left(\\frac{\\partial H}{\\partial H}\\right)_P \\end{array} \\right| = \\left(\\frac{\\partial T}{\\partial P}\\right)_H\\left(\\frac{\\partial H}{\\partial H}\\right)_P-\\left(\\frac{\\partial T}{\\partial H}\\right)_P\\left(\\frac{\\partial H}{\\partial P}\\right)_H$$\n",
      "\n",
      "$\\left(\\frac{\\partial H}{\\partial P}\\right)_H = 0$ and $\\left(\\frac{\\partial H}{\\partial H}\\right)_P = 1$\n",
      "\n",
      "If there is a common term and it is in the same column, it is the thing held constant (see above).\n",
      "\n",
      "If there is a common term and it is in the opposite column, it is the thing held constant, but you need to add a sign change:\n",
      "\n",
      "$$ \\frac{\\partial(T,H)}{\\partial(H,P)} = -\\left(\\frac{\\partial T}{\\partial P}\\right)_H$$\n",
      "\n",
      "You can do a change of variables by applying it to numerator and denominator\n",
      "\n",
      "$$ \\left(\\frac{\\partial T}{\\partial P}\\right)_H = \\frac{\\partial(T,H)}{\\partial(P,H)} = \\frac{\\frac{\\partial(T,H)}{\\partial(x,y)}}{\\frac{\\partial(P,H)}{\\partial(x,y)}}$$\n",
      "\n",
      "Where... (drumroll please)... we can use $\\tau$ and $\\delta$ as the independent variables $x$ and $y$. So we can express the Jacobian in terms of $\\tau$ and $\\delta$ directly.\n",
      "\n",
      "$$ \\left(\\frac{\\partial T}{\\partial P}\\right)_h = \\frac{\\partial(T,h)}{\\partial(P,h)} = \\frac{\\displaystyle\\frac{\\partial(T,h)}{\\partial(\\tau,\\delta)}}{\\displaystyle\\frac{\\partial(P,h)}{\\partial(\\tau,\\delta)}} = \\displaystyle\\frac{\\left| \\begin{array}{cc} \\left(\\frac{\\partial T}{\\partial \\tau}\\right)_\\delta & \\left(\\frac{\\partial h}{\\partial \\tau}\\right)_\\delta \\\\ \\left(\\frac{\\partial T}{\\partial \\delta}\\right)_\\tau & \\left(\\frac{\\partial h}{\\partial \\delta}\\right)_\\tau \\end{array} \\right|}{\\left| \\begin{array}{cc} \\left(\\frac{\\partial P}{\\partial \\tau}\\right)_\\delta & \\left(\\frac{\\partial h}{\\partial \\tau}\\right)_\\delta \\\\ \\left(\\frac{\\partial P}{\\partial \\delta}\\right)_\\tau & \\left(\\frac{\\partial h}{\\partial \\delta}\\right)_\\tau \\end{array} \\right|} = \\frac{\\left(\\frac{\\partial T}{\\partial \\tau}\\right)_\\delta\\left(\\frac{\\partial h}{\\partial \\delta}\\right)_\\tau-\\left(\\frac{\\partial T}{\\partial \\delta}\\right)_\\tau\\left(\\frac{\\partial h}{\\partial \\tau}\\right)_\\delta}{\\left(\\frac{\\partial P}{\\partial \\tau}\\right)_\\delta\\left(\\frac{\\partial h}{\\partial \\delta}\\right)_\\tau-\\left(\\frac{\\partial P}{\\partial \\delta}\\right)_\\tau\\left(\\frac{\\partial h}{\\partial \\tau}\\right)_\\delta}$$\n",
      "\n",
      "Or more generally\n",
      "\n",
      "$$ \\left(\\frac{\\partial A}{\\partial B}\\right)_C = \\frac{\\left(\\frac{\\partial A}{\\partial \\tau}\\right)_\\delta\\left(\\frac{\\partial C}{\\partial \\delta}\\right)_\\tau-\\left(\\frac{\\partial A}{\\partial \\delta}\\right)_\\tau\\left(\\frac{\\partial C}{\\partial \\tau}\\right)_\\delta}{\\left(\\frac{\\partial B}{\\partial \\tau}\\right)_\\delta\\left(\\frac{\\partial C}{\\partial \\delta}\\right)_\\tau-\\left(\\frac{\\partial B}{\\partial \\delta}\\right)_\\tau\\left(\\frac{\\partial C}{\\partial \\tau}\\right)_\\delta} = \\frac{N}{D}$$\n",
      "\n",
      "The duplicated derivatives should be cached for speed.  Anyway, they will be cached since we will use an AbstractState instance.  Just need to calculate all partial derivatives of fundamental variables (T,p,$\\rho$,h,u,s) with respect to $\\tau$ and $\\delta$. 12 derivatives, all easy to numerically validate and 2 are trivial ($dT/d\\tau$ and $d\\rho/d\\delta$)\n",
      "\n",
      "As also derived in \"Partial derivatives of thermodynamic state properties for dynamic simulation\", Matthis Thorade & Ali Saadat., 2013, DOI 10.1007/s12665-013-2394-z\n"
     ]
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Second partial"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "$$\\left(\\frac{\\partial N}{\\partial \\delta}\\right)_{\\tau} = \\left(\\frac{\\partial A}{\\partial \\tau}\\right)_\\delta\\left(\\frac{\\partial^2 C}{\\partial \\delta^2}\\right)_{\\tau}+\\left(\\frac{\\partial^2 A}{\\partial \\tau\\partial\\delta}\\right)\\left(\\frac{\\partial C}{\\partial \\delta}\\right)_{\\tau}-\\left(\\frac{\\partial A}{\\partial \\delta}\\right)_\\tau\\left(\\frac{\\partial^2 C}{\\partial \\tau\\partial\\delta}\\right)-\\left(\\frac{\\partial^2 A}{\\partial \\delta^2}\\right)_{\\tau}\\left(\\frac{\\partial C}{\\partial \\tau}\\right)_\\delta$$\n",
      "\n",
      "$$\\left(\\frac{\\partial D}{\\partial \\delta}\\right)_{\\tau} = \\left(\\frac{\\partial B}{\\partial \\tau}\\right)_\\delta\\left(\\frac{\\partial^2 C}{\\partial \\delta^2}\\right)_{\\tau}+\\left(\\frac{\\partial^2 B}{\\partial \\tau\\partial\\delta}\\right)\\left(\\frac{\\partial C}{\\partial \\delta}\\right)_{\\tau}-\\left(\\frac{\\partial B}{\\partial \\delta}\\right)_\\tau\\left(\\frac{\\partial^2 C}{\\partial \\tau\\partial\\delta}\\right)-\\left(\\frac{\\partial^2 B}{\\partial \\delta^2}\\right)_{\\tau}\\left(\\frac{\\partial C}{\\partial \\tau}\\right)_\\delta$$"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "$$\\left(\\frac{\\partial N}{\\partial \\tau}\\right)_{\\delta} = \\left(\\frac{\\partial A}{\\partial \\tau}\\right)_\\delta\\left(\\frac{\\partial^2 C}{\\partial \\delta\\partial\\tau}\\right)+\\left(\\frac{\\partial^2 A}{\\partial \\tau^2}\\right)_\\delta\\left(\\frac{\\partial C}{\\partial \\delta}\\right)_{\\tau}-\\left(\\frac{\\partial A}{\\partial \\delta}\\right)_\\tau\\left(\\frac{\\partial^2 C}{\\partial \\tau^2}\\right)_\\delta-\\left(\\frac{\\partial^2 A}{\\partial \\delta\\partial\\tau}\\right)\\left(\\frac{\\partial C}{\\partial \\tau}\\right)_\\delta$$\n",
      "\n",
      "$$\\left(\\frac{\\partial D}{\\partial \\tau}\\right)_{\\delta} = \\left(\\frac{\\partial B}{\\partial \\tau}\\right)_\\delta\\left(\\frac{\\partial^2 C}{\\partial \\delta\\partial\\tau}\\right)+\\left(\\frac{\\partial^2 B}{\\partial \\tau^2}\\right)_\\delta\\left(\\frac{\\partial C}{\\partial \\delta}\\right)_{\\tau}-\\left(\\frac{\\partial B}{\\partial \\delta}\\right)_\\tau\\left(\\frac{\\partial^2 C}{\\partial \\tau^2}\\right)_\\delta-\\left(\\frac{\\partial^2 B}{\\partial \\delta\\partial\\tau}\\right)\\left(\\frac{\\partial C}{\\partial \\tau}\\right)_\\delta$$"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "$$\\frac{\\partial}{\\partial \\tau}\\left( \\left(\\frac{\\partial A}{\\partial B}\\right)_C \\right)_\\delta = \\frac{D\\left(\\frac{\\partial N}{\\partial \\tau}\\right)_{\\delta}-N\\left(\\frac{\\partial D}{\\partial \\tau}\\right)_{\\delta}}{D^2}$$\n",
      "\n",
      "$$\\frac{\\partial}{\\partial \\delta}\\left( \\left(\\frac{\\partial A}{\\partial B}\\right)_C \\right)_\\tau = \\frac{D\\left(\\frac{\\partial N}{\\partial \\delta}\\right)_{\\tau}-N\\left(\\frac{\\partial D}{\\partial \\delta}\\right)_{\\tau}}{D^2}$$"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "$$ \\frac{\\partial}{\\partial D}\\left(\\left(\\frac{\\partial A}{\\partial B}\\right)_C\\right)_E = \\frac{\\frac{\\partial}{\\partial \\tau}\\left( \\left(\\frac{\\partial A}{\\partial B}\\right)_C \\right)_\\delta\\left(\\frac{\\partial E}{\\partial \\delta}\\right)_\\tau-\\frac{\\partial}{\\partial \\delta}\\left(\\left(\\frac{\\partial A}{\\partial B}\\right)_C\\right)_\\tau\\left(\\frac{\\partial E}{\\partial \\tau}\\right)_\\delta}{\\left(\\frac{\\partial D}{\\partial \\tau}\\right)_\\delta\\left(\\frac{\\partial E}{\\partial \\delta}\\right)_\\tau-\\left(\\frac{\\partial D}{\\partial \\delta}\\right)_\\tau\\left(\\frac{\\partial E}{\\partial \\tau}\\right)_\\delta}$$\n"
     ]
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Converting second derivatives w.r.t. T and rho to tau and delta"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "$$\\frac{\\partial^2 p}{\\partial \\rho^2} = \\frac{\\partial }{\\partial \\rho}\\left(\\frac{\\partial p}{\\partial \\rho}\\right) = \\frac{\\partial }{\\partial \\rho}\\left(\\frac{\\partial \\delta}{\\partial \\rho}\\frac{\\partial p}{\\partial \\delta}\\right) = \\frac{\\partial \\delta}{\\partial \\rho}\\frac{\\partial^2 p}{\\partial \\delta^2} + \\frac{\\partial^2 \\delta}{\\partial \\rho^2}\\frac{\\partial p}{\\partial \\delta}$$"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Now to solve for \n",
      "$$\\frac{\\partial^2 p}{\\partial \\delta^2}$$\n",
      "we can re-arrange to get\n",
      "\n",
      "$$\\frac{\\partial^2 p}{\\partial \\delta^2} = \\frac{\\frac{\\partial^2 p}{\\partial \\rho^2} -\\frac{\\partial^2 \\delta}{\\partial \\rho^2}\\frac{\\partial p}{\\partial \\delta}}{\\frac{\\partial \\delta}{\\partial \\rho}} $$"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}