{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "df447d16",
   "metadata": {},
   "source": [
    "# General cubics\n",
    "\n",
    "The reduced residual Helmholtz energy for the main cubic EOS (van der Waals, Peng-Robinson, and Soave-Redlich-Kwong) can be written in a common form (see https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7365965/)\n",
    "\n",
    "$$ \\alpha^r = \\psi^{(-)} - \\dfrac{\\tau a_m}{RT_r } \\psi^{(+)} $$\n",
    "\n",
    "$$ \\psi^{(-)} =-\\ln(1-b_m\\rho ) $$\n",
    "\n",
    "$$ \\psi^{(+)} = \\dfrac{\\ln\\left(\\dfrac{\\Delta_1 b_m\\rho+1}{\\Delta_2b_m\\rho+1}\\right)}{b_m(\\Delta_1-\\Delta_2)} $$\n",
    "\n",
    "with the constants given by:\n",
    "\n",
    "* vdW: $\\Delta_1=0$, $\\Delta_2=0$\n",
    "* SRK: $\\Delta_1=1$, $\\Delta_2=0$\n",
    "* PR: $\\Delta_1=1+\\sqrt{2}$, $\\Delta_2=1-\\sqrt{2}$\n",
    "\n",
    "The quantities $a_m$ and $b_m$ are described (with exact solutions for the numerical coefficients) for each of these EOS in https://pubs.acs.org/doi/abs/10.1021/acs.iecr.1c00847.\n",
    "\n",
    "The models in teqp are instantiated based on knowledge of the critical temperature, pressure, and acentric factor.  Thereafter all quantities are obtained from derivatives of $\\alpha^r$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9a884dab",
   "metadata": {},
   "outputs": [],
   "source": [
    "import teqp\n",
    "\n",
    "# Values taken from http://dx.doi.org/10.6028/jres.121.011\n",
    "Tc_K = [ 190.564, 154.581, 150.687 ]\n",
    "pc_Pa = [ 4599200, 5042800, 4863000 ]\n",
    "acentric = [ 0.011, 0.022, -0.002 ]\n",
    "\n",
    "# Instantiate Peng-Robinson model\n",
    "modelPR = teqp.canonical_PR(Tc_K, pc_Pa, acentric)\n",
    "\n",
    "# Instantiate Soave-Redlich-Kwong model\n",
    "modelSRK = teqp.canonical_SRK(Tc_K, pc_Pa, acentric)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "615c7830",
   "metadata": {},
   "source": [
    "## Adjusting k_ij\n",
    "\n",
    "Fine-tuned values of $k_{ij}$ can be provided when instantiating the model, for Peng-Robinson and SRK.  A complete matrix of all the $k_{ij}$ values must be provided. This allows for asymmetric mixing models in which $k_{ij}\\neq k_{ji}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a8c87890",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<teqp.teqp.GenericCubic at 0x18f604c3a70>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k_12 = 0.01\n",
    "kmat = [[0,k_12,0],[k_12,0,0],[0,0,0]]\n",
    "teqp.canonical_PR(Tc_K, pc_Pa, acentric, kmat)\n",
    "teqp.canonical_SRK(Tc_K, pc_Pa, acentric, kmat)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3a62795",
   "metadata": {},
   "source": [
    "## Superancillary\n",
    "\n",
    "The superancillary equation gives the co-existing liquid and vapor (orthobaric) densities as a function of temperature. The set of Chebyshev expansions was developed in https://pubs.acs.org/doi/abs/10.1021/acs.iecr.1c00847 .  These superancillary equations are more accurate than iterative calculations in double precision arithmetic and also at least 10 times faster to calculate, and cannot fail in iterative routines, even extremely close to the critical point. \n",
    "\n",
    "The superancillary equation is only exposed for pure fluids to remove ambiguity when considering mixtures.  The returned tuple is the liquid and vapor densities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1b02ef72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(30846.392909514052, 42.480231719002326)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "teqp.canonical_PR([Tc_K[0]], [pc_Pa[0]], [acentric[0]]).superanc_rhoLV(100)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}