{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.optimize import curve_fit\n",
    "\n",
    "# Extrapolating experimental ln(KIE) in linear range to ln(KIE) of 2.82\n",
    "\n",
    "experimental_dichloromethane = np.array([2.82, 2.37, 2.22, 2.08])\n",
    "temperatures = np.array([195.15, 210.15, 226.15, 250.15])\n",
    "temperatures_inverted = 1 / temperatures\n",
    "\n",
    "def linear_func(x, a, b):\n",
    "    return a * x + b\n",
    "params, covariance = curve_fit(\n",
    "    linear_func,\n",
    "    temperatures_inverted[-3:],\n",
    "    np.log(experimental_dichloromethane)[-3:]\n",
    ")\n",
    "a, b = params\n",
    "desired_x = (np.log(experimental_dichloromethane[0]) - b) / a\n",
    "extrapolation_x = list(temperatures_inverted[-3:])\n",
    "extrapolation_x.insert(0, desired_x)\n",
    "plt.plot(\n",
    "    extrapolation_x, \n",
    "    np.log(experimental_dichloromethane)\n",
    ")\n",
    "plt.plot(\n",
    "    temperatures_inverted,\n",
    "    np.log(experimental_dichloromethane)\n",
    ")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def equation1(a, b, c, T): \n",
    "    # a is free energy difference [kcal/mol], \n",
    "    # b = k2/k1, c = k4/k3, T is temp. in Kelvin\n",
    "    r = np.exp(a * 4184 / (-8.314 * T))\n",
    "    KIE = r * b / (1 + r) + c / (1 + r)\n",
    "    return KIE\n",
    "\n",
    "def equation_paper(a, b, c, T):\n",
    "    k1 = 10\n",
    "    k2 = b * k1\n",
    "    r = np.exp(a * 4184 / (-8.314 * T))\n",
    "    k4 = r * k2\n",
    "    k3 = k4 / c\n",
    "    KIE = (k1 * k2 + 2 * k2 * k4 + k3 * k4) / (k1 * k2 + 2 * k1 * k3 + k3 * k4)\n",
    "    return KIE\n",
    "\n",
    "# e.g.\n",
    "n = 500\n",
    "G_values = np.linspace(-3, 3, n)\n",
    "\n",
    "KIE_values1 = []\n",
    "KIE_values2 = []\n",
    "for i in range(n):\n",
    "    KIE_values1.append(equation1(G_values[i], 2.2, 2.2, 195.15))\n",
    "    KIE_values2.append(equation_paper(G_values[i], 2.2, 2.2, 195.15))\n",
    "\n",
    "f, ax = plt.subplots()\n",
    "plt.plot(\n",
    "    G_values,\n",
    "    KIE_values1\n",
    ")\n",
    "plt.plot(\n",
    "    G_values,\n",
    "    KIE_values2\n",
    ")\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
