{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Tabular Taylor Series Extrapolation"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Introduction"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Lookup tables are an effective way of greatly speeding up the computational time when the state variable inputs are variables other than temperature and density (upon which the equations of state are based).\n",
      "\n",
      "The tables are constructed with enthalpy and pressure as the independent variables.  Other inputs are also possible, but the efficiency of the lookup table method is maximized through the use of these independent variables\n",
      "\n",
      "TTSE was added to CoolProp as of version 3.0.  As of version 4.0, bicubic interpolation (see http://en.wikipedia.org/wiki/Bicubic_interpolation) has been added, which greatly improves the accuracy at a small speed penalty (~10%) over TTSE."
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Usage"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The tables can be enabled by calling the function ``enable_TTSE_LUT()`` of the CoolPropStateClass (only C++), or alternatively, the ``enable_TTSE_LUT(FluidName)`` (Python + anything that calls the DLL) function.  This will enable ALL thermodynamic calls for this fluid to use the TTSE method.  The function ``disable_TTSE_LUT`` is used to disable the TTSE method\n",
      "\n",
      "Two types of tables are built:\n",
      "\n",
      "* Tables are constructed along each saturation curve, all the way from the triple point (or minimum) pressure to the critical pressure\n",
      "*  A single-phase table is constructed for the areas outside of the two-phase region\n",
      "\n",
      "When the single phase tables are constructed, a default range is employed, which is taken to be from the triple point pressure to the twice the critical pressure, and from the saturated liquid enthalpy at the triple point pressure to an enthalpy that is three times the latent heat at the triple point pressure plus the saturated liquid enthalpy at the triple point pressure.\n",
      "\n",
      "If you do not like that default range, BEFORE you call enable_TTSE_LUT(), call ``set_TTSESinglePhase_LUT_range(FluidName,hmin,hmax,pmin,pmax)``\n",
      "\n",
      "The single-phase (but not the two-phase) tables are stored in binary form in files for faster loading in the HOME/CoolProp-TTSEData/*FluidName* folder where HOME is your home folder.  If you do not want to save the tables to file, you can call the function disable_TTSE_LUT_writing() to disable the writing of the single phase tables to file BEFORE the tables are built"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example in Python"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#import CoolProp module\n",
      "import CoolProp.CoolProp as CP"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#This will disable the TTSE method for Propane\n",
      "CP.disable_TTSE_LUT_writing('Propane')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 2,
       "text": [
        "True"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#This will enable the TTSE method for Propane\n",
      "In [1]: CP.enable_TTSE_LUT('Propane')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 3,
       "text": [
        "True"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Check if it is enabled for Propane\n",
      "In [1]: CP.isenabled_TTSE_LUT('Propane')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 4,
       "text": [
        "True"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Calculate something with the tables for Propane\n",
      "In [1]: CP.Props('H','P',300,'Q',0,'Propane')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 5,
       "text": [
        "165.25496936313542"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Check the range for Propane (hmin,hmax,pmin,pmax)\n",
      "In [1]: CP.get_TTSESinglePhase_LUT_range('Propane')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 6,
       "text": [
        "(-196643.67321201751, 929157.9196160495, 0.00017204618151214153, 8502400.0)"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Calculate something with the tables for Propane (using TTSE method)\n",
      "In [1]: CP.Props('T','P',300,'H',40,'Propane')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 7,
       "text": [
        "203.3656095382259"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Set the mode to bicubic interpolation\n",
      "In [1]: CP.set_TTSE_mode('Propane','BICUBIC') # or could be 'TTSE'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 8,
       "text": [
        "True"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Calculate something with the tables for Propane (using Bicubic method)\n",
      "In [1]: CP.Props('T','P',300,'H',40,'Propane')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "203.3656097126807"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#This will disable the TTSE method for Propane\n",
      "In [1]: CP.disable_TTSE_LUT('Propane')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 10,
       "text": [
        "True"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Calculate something without the tables for Propane\n",
      "In [1]: CP.Props('T','P',300,'H',40,'Propane')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 11,
       "text": [
        "203.36560971332722"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Note: most other programming languages that are wrappers around the CoolProp.h header should behave in nearly exactly the same fashion, excepting perhaps the ``get_TTSESinglePhase_LUT_range`` function as it uses passing by reference."
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "How it Works (TTSE)"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Tables are built of $T(p,h), s(p,h)$, and $\\rho(p,h)$ as well as the derivatives of each term with respect to $p$ and $h$.  The property of interest is then expanded around the nearest grid point to yield representations like\n",
      "\n",
      "$$T = T_{i,j}+\\Delta h\\left(\\frac{\\partial T}{\\partial h}\\right)_{p}+\\Delta p\\left(\\frac{\\partial T}{\\partial p}\\right)_{h}+\\frac{1}{2}\\Delta h^2\\left(\\frac{\\partial^2 T}{\\partial h^2}\\right)_{p}+\\frac{1}{2}\\Delta p^2\\left(\\frac{\\partial^2T}{\\partial p^2}\\right)_{h}+\\Delta h\\Delta p\\left(\\frac{\\partial^2T}{\\partial p\\partial h}\\right)$$\n",
      "    \n",
      "$$ s = s_{i,j}+\\Delta h\\left(\\frac{\\partial s}{\\partial h}\\right)_{p}+\\Delta p\\left(\\frac{\\partial s}{\\partial p}\\right)_{h}+\\frac{1}{2}\\Delta h^2\\left(\\frac{\\partial^2 s}{\\partial h^2}\\right)_{p}+\\frac{1}{2}\\Delta p^2\\left(\\frac{\\partial^2s}{\\partial p^2}\\right)_{h}+\\Delta h\\Delta p\\left(\\frac{\\partial^2s}{\\partial p\\partial h}\\right)$$\n",
      "    \n",
      "$$ \\rho = s_{i,j}+\\Delta h\\left(\\frac{\\partial \\rho}{\\partial h}\\right)_{p}+\\Delta p\\left(\\frac{\\partial \\rho}{\\partial p}\\right)_{h}+\\frac{1}{2}\\Delta h^2\\left(\\frac{\\partial^2 \\rho}{\\partial h^2}\\right)_{p}+\\frac{1}{2}\\Delta p^2\\left(\\frac{\\partial^2\\rho}{\\partial p^2}\\right)_{h}+\\Delta h\\Delta p\\left(\\frac{\\partial^2\\rho}{\\partial p\\partial h}\\right) $$\n",
      "\n",
      "$$\\Delta h = h-h_i$$\n",
      "    \n",
      "$$\\Delta p = p-p_i$$\n",
      "    \n",
      "See the [IAPWS TTSE report](http://www.iapws.org/relguide/TTSE.pdf) for a description of the method.  Analytic derivatives are used to build the tables"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "How it Works (Bicubic)"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "In the TTSE method, the derivatives are calculated at every grid point.  In the bicubic method (see [bicubic interpolation](http://en.wikipedia.org/wiki/Bicubic_interpolation)) we use the known derivatives at each grid point in order to develop $C_1$ continuous bicubic functions in each cell.  The independent variables ($T,\\rho$) or ($p,h$) are normalized into unit variables that vary between 0 and 1.  Then the bicubic coefficients are found for the cell, and the bicubic form is evaluated."
     ]
    }
   ],
   "metadata": {}
  }
 ]
}