From 2ede6dec9ad05e5c5527c9dc39377540e53dba60 Mon Sep 17 00:00:00 2001
From: Ian Bell <ian.bell@nist.gov>
Date: Thu, 7 Jul 2022 11:08:32 -0400
Subject: [PATCH] Start off with examples of critical points

---
 doc/source/algorithms/critical_curves.ipynb | 113 +++++++++++++++++++-
 1 file changed, 110 insertions(+), 3 deletions(-)

diff --git a/doc/source/algorithms/critical_curves.ipynb b/doc/source/algorithms/critical_curves.ipynb
index c90ede6..d6f8ae3 100644
--- a/doc/source/algorithms/critical_curves.ipynb
+++ b/doc/source/algorithms/critical_curves.ipynb
@@ -1,16 +1,123 @@
 {
  "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "d88bffed",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import teqp\n",
+    "teqp.__version__"
+   ]
+  },
   {
    "cell_type": "markdown",
    "id": "8218498b",
    "metadata": {},
    "source": [
-    "# Critical curves\n",
+    "# Critical curves & points\n",
+    "\n",
+    "\n",
+    "## Pure Fluids\n",
+    "\n",
+    "Solving for the critical point involves finding the temperature and density that make\n",
+    "$$\n",
+    "\\left(\\frac{\\partial p}{\\partial \\rho}\\right)_T = \\left(\\frac{\\partial^2 p}{\\partial \\rho^2}\\right)_T = 0\n",
+    "$$\n",
+    "by 2D non-linear rootfinding. Newton steps are taken, and the analytic Jacobian is used (thanks to the ability to do derivatives with automatic differentiation).  This is all handily wrapped up in the\n",
+    "``solve_pure_critical`` method which requires the user to provide guess values for temperature and density"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "46657a96",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# Values taken from http://dx.doi.org/10.6028/jres.121.011\n",
+    "modelPR = teqp.canonical_PR([190.564], [4599200], [0.011])\n",
+    "\n",
+    "# Solve for the critical point from a point close to the critical point\n",
+    "T0 = 192.0\n",
+    "# Critical compressibility factor of P-R is 0.307401308698.. (see https://doi.org/10.1021/acs.iecr.1c00847)\n",
+    "rhoc = (4599200/(8.31446261815324*190.564))/0.3074\n",
+    "rho0 = rhoc*1.2345 # Perturb to make sure we are doing something in the solver\n",
+    "teqp.solve_pure_critical(modelPR, T0, rho0)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "e15eeded",
+   "metadata": {},
+   "source": [
+    "# Mixtures\n",
     "\n",
     "A pure fluid has a single vapor-liquid critical point, but mixtures are different:\n",
     "\n",
-    "* They may have multiple (or zero!) critical points for given mixture composition\n",
-    "* The critical curves may not emanate from the pure fluid endpoints"
+    "* They may have multiple (or zero!) critical points for a given mixture composition\n",
+    "* The critical curves may not emanate from the pure fluid endpoints\n",
+    "\n",
+    "When it comes to critical points, intuition from pure fluids is not helpful, or sometimes even counter-productive. \n",
+    "\n",
+    "teqp has methods for working with the critical loci of binary mixtures (only binary mixtures, for now) and especially, methods for tracing the critical curves emanating from the pure fluid endpoints.\n",
+    "\n",
+    "The tracing method in teqp is based explicitly on the isochoric thermodynamics formalism introduced by Ulrich Deiters and Sergio Quinones-Cisneros.  It uses the Helmholtz energy density as the fundamental potential and all other properties are derived from it.  For critical curves it is based upon the integration of sets of ordinary differential equations; the differential equations are in the form of derivatives of the molar concentrations of each component in the mixture with respect to an integration variable.  The set of ODE is then integrated.\n",
+    "\n",
+    "Here is an example of the construction of the critical curves emanating from the pure fluid endpoints for the mixture nitrogen + ethane."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "81619ae2",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import timeit\n",
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt \n",
+    "import pandas\n",
+    "import teqp\n",
+    "\n",
+    "def get_critical_curve(ipure):\n",
+    "    \"\"\" Return curve as pandas DataFrame \"\"\"\n",
+    "    names = ['Nitrogen', 'Ethane']\n",
+    "    model = teqp.build_multifluid_model(names, teqp.get_datapath())\n",
+    "    T0 = model.get_Tcvec()[ipure]\n",
+    "    rho0 = np.array([1.0/model.get_vcvec()[ipure]]*2)\n",
+    "    rho0[1-ipure] = 0\n",
+    "    o = teqp.TCABOptions() \n",
+    "    o.init_dt = 1.0 # step in the parameter\n",
+    "    o.rel_err = 1e-8\n",
+    "    o.abs_err = 1e-5\n",
+    "    o.integration_order = 5\n",
+    "    o.calc_stability = True\n",
+    "    o.polish = True\n",
+    "    curveJSON = teqp.trace_critical_arclength_binary(model, T0, rho0, '', o)\n",
+    "    df = pandas.DataFrame(curveJSON)\n",
+    "    rhotot = df['rho0 / mol/m^3']+df['rho1 / mol/m^3']\n",
+    "    df['z0 / mole frac.'] = df['rho0 / mol/m^3']/rhotot\n",
+    "    return df\n",
+    "\n",
+    "fig, ax = plt.subplots(1,1,figsize=(7, 6))\n",
+    "tic = timeit.default_timer()\n",
+    "for ipure in [1,0]:\n",
+    "    df = get_critical_curve(ipure)\n",
+    "    first_unstable = np.argmax(~df['locally stable'])\n",
+    "    df = df.iloc[0:(first_unstable if first_unstable else len(df))]\n",
+    "    line, = plt.plot(df['T / K'], df['p / Pa']/1e6, '-')\n",
+    "    plt.plot(df['T / K'].iloc[0], df['p / Pa'].iloc[0]/1e6, 'd', \n",
+    "        color=line.get_color())\n",
+    "\n",
+    "elap = timeit.default_timer()-tic\n",
+    "plt.gca().set(xlabel='$T$ / K', ylabel='$p$ / MPa',\n",
+    "    xlim=(100, 350), ylim=(1, 1e3))\n",
+    "plt.yscale('log')\n",
+    "plt.tight_layout(pad=0.2)\n",
+    "plt.gcf().text(0,0,f'time: {elap:0.1f} s', ha='left', va='bottom', fontsize=7)\n",
+    "plt.gcf().text(1,0,f'teqp: {teqp.__version__}', ha='right', va='bottom', fontsize=7);"
    ]
   }
  ],
-- 
GitLab