{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting Isobaric Binary Phase Diagrams with `binplot`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These are a few examples of how to use Thermo-Calc TDB files to calculate isobaric binary phase diagrams. As long as the TDB file is present, each cell in these examples is self contained and can completely reproduce the figure shown.\n",
    "\n",
    "### binplot\n",
    "\n",
    "The phase diagrams are computed with `binplot`, which has four required arguments:\n",
    "1. The Database object\n",
    "2. A list of active components (vacancies (`VA`), which are present in many databases, must be included explictly).\n",
    "3. A list of phases to consider in the calculation\n",
    "4. A dictionary conditions to consider, with keys of pycalphad StateVariables and values of scalars, 1D arrays, or `(start, stop, step)` ranges\n",
    "\n",
    "Note that, at the time of writing, invariant reactions (three-phase 'regions' on binary diagrams) are not yet automatically detected so they\n",
    "are not drawn on the diagram.\n",
    "\n",
    "Also note that the [magic variable](https://ipython.readthedocs.io/en/stable/interactive/magics.html) `%matplotlib inline` should only be used in Jupyter notebooks.\n",
    "\n",
    "\n",
    "### TDB files\n",
    "\n",
    "The TDB files should be located in the current working directory of the notebook. If you are running using a Jupyter notebook, the default working directory is the directory that that notebook is saved in. \n",
    "\n",
    "To check the working directory, run:\n",
    "\n",
    "```python\n",
    "import os\n",
    "print(os.path.abspath(os.curdir))\n",
    "```\n",
    "\n",
    "TDB files can be found in the literature. The [Thermodynamic DataBase DataBase](https://avdwgroup.engin.brown.edu) (TDBDB) has indexed many available databases and links to the original papers and/or TDB files where possible.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Al-Zn (S. Mey, 1993)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The miscibility gap in the fcc phase is included in the Al-Zn diagram, shown below.\n",
    "\n",
    "The format for specifying a range of a state variable is (*start*, *stop*, *step*).\n",
    "\n",
    "S. an Mey, Zeitschrift für Metallkunde 84(7) (1993) 451-455."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:15:04.831456Z",
     "iopub.status.busy": "2022-02-19T19:15:04.831456Z",
     "iopub.status.idle": "2022-02-19T19:15:09.211526Z",
     "shell.execute_reply": "2022-02-19T19:15:09.211526Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 60816 characters]",
      "text/plain": [
       "<Figure size 900x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "from pycalphad import Database, binplot\n",
    "import pycalphad.variables as v\n",
    "\n",
    "# Load database and choose the phases that will be considered\n",
    "db_alzn = Database('alzn_mey.tdb')\n",
    "my_phases_alzn = ['LIQUID', 'FCC_A1', 'HCP_A3']\n",
    "\n",
    "# Create a matplotlib Figure object and get the active Axes\n",
    "fig = plt.figure(figsize=(9,6))\n",
    "axes = fig.gca()\n",
    "\n",
    "# Compute the phase diagram and plot it on the existing axes using the `plot_kwargs={'ax': axes}` keyword argument\n",
    "binplot(db_alzn, ['AL', 'ZN', 'VA'] , my_phases_alzn, {v.X('ZN'):(0,1,0.02), v.T: (300, 1000, 10), v.P:101325, v.N: 1}, plot_kwargs={'ax': axes})\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Al-Mg (Y. Zhong, 2005)\n",
    "\n",
    "Y. Zhong, M. Yang, Z.-K. Liu, CALPHAD 29 (2005) 303-311 doi:[10.1016/j.calphad.2005.08.004](https://doi.org/10.1016/j.calphad.2005.08.004)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:15:09.221507Z",
     "iopub.status.busy": "2022-02-19T19:15:09.221507Z",
     "iopub.status.idle": "2022-02-19T19:15:20.361578Z",
     "shell.execute_reply": "2022-02-19T19:15:20.361578Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 61692 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "from pycalphad import Database, binplot\n",
    "import pycalphad.variables as v\n",
    "\n",
    "# Load database \n",
    "dbf = Database('Al-Mg_Zhong.tdb')\n",
    "# Define the components\n",
    "comps = ['AL', 'MG', 'VA']\n",
    "# Get all possible phases programmatically\n",
    "phases = dbf.phases.keys()\n",
    "\n",
    "# Plot the phase diagram, if no axes are supplied, a new figure with axes will be created automatically\n",
    "binplot(dbf, comps, phases, {v.N: 1, v.P:101325, v.T: (300, 1000, 10), v.X('MG'):(0, 1, 0.02)})\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Al-Ni (Dupin, 2001)\n",
    "\n",
    "Components and conditions can also be stored as variables and passed to binplot.\n",
    "\n",
    "N. Dupin, I. Ansara, B. Sundman, CALPHAD 25(2) (2001) 279-298 doi:[10.1016/S0364-5916(01)00049-9](https://doi.org/10.1016/S0364-5916(01)00049-9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:15:20.371450Z",
     "iopub.status.busy": "2022-02-19T19:15:20.371450Z",
     "iopub.status.idle": "2022-02-19T19:15:34.521991Z",
     "shell.execute_reply": "2022-02-19T19:15:34.521991Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 96600 characters]",
      "text/plain": [
       "<Figure size 900x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "from pycalphad import Database, binplot\n",
    "import pycalphad.variables as v\n",
    "\n",
    "# Load database\n",
    "dbf = Database('NI_AL_DUPIN_2001.TDB')\n",
    "# Set the components to consider, including vacanies (VA) explictly.\n",
    "comps = ['AL', 'NI', 'VA']\n",
    "# Get all the phases in the database programatically\n",
    "phases = list(dbf.phases.keys())\n",
    "# Create the dictionary of conditions\n",
    "conds = {\n",
    "    v.N: 1, v.P: 101325,\n",
    "    v.T: (300, 2000, 10),  # (start, stop, step)\n",
    "    v.X('AL'): (1e-5, 1, 0.02),   # (start, stop, step)\n",
    "}\n",
    "\n",
    "# Create a matplotlib Figure object and get the active Axes\n",
    "fig = plt.figure(figsize=(9,6))\n",
    "axes = fig.gca()\n",
    "\n",
    "# Plot by passing in all the variables\n",
    "binplot(dbf, comps, phases, conds, plot_kwargs={'ax': axes})\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Al-Fe (M. Seiersten, 1991)\n",
    "\n",
    "Removing tielines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:15:34.531400Z",
     "iopub.status.busy": "2022-02-19T19:15:34.531400Z",
     "iopub.status.idle": "2022-02-19T19:16:16.771602Z",
     "shell.execute_reply": "2022-02-19T19:16:16.771602Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 60136 characters]",
      "text/plain": [
       "<Figure size 900x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "from pycalphad import Database, binplot\n",
    "import pycalphad.variables as v\n",
    "\n",
    "# Load database and choose the phases that will be considered\n",
    "db_alfe = Database('alfe_sei.TDB')\n",
    "my_phases_alfe = ['LIQUID', 'B2_BCC', 'FCC_A1', 'HCP_A3', 'AL5FE2', 'AL2FE', 'AL13FE4', 'AL5FE4']\n",
    "\n",
    "# Create a matplotlib Figure object and get the active Axes\n",
    "fig = plt.figure(figsize=(9,6))\n",
    "axes = fig.gca()\n",
    "\n",
    "# Plot the phase diagram on the existing axes using the `plot_kwargs={'ax': axes}` keyword argument\n",
    "# Tielines are turned off by including `'tielines': False` in the plotting keword argument\n",
    "binplot(db_alfe, ['AL', 'FE', 'VA'] , my_phases_alfe, {v.X('AL'):(0,1,0.01), v.T: (300, 2000, 10), v.P:101325}, plot_kwargs={'ax': axes, 'tielines': False})\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Nb-Re (Liu, 2013)\n",
    "\n",
    "X.L. Liu, C.Z. Hargather, Z.-K. Liu, CALPHAD 41 (2013) 119-127 doi:[10.1016/j.calphad.2013.02.006](https://doi.org/10.1016/j.calphad.2013.02.006)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:16:16.781609Z",
     "iopub.status.busy": "2022-02-19T19:16:16.781609Z",
     "iopub.status.idle": "2022-02-19T19:16:49.936269Z",
     "shell.execute_reply": "2022-02-19T19:16:49.937268Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 64272 characters]",
      "text/plain": [
       "<Figure size 900x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "from pycalphad import Database, binplot, variables as v\n",
    "\n",
    "# Load database and choose the phases that will be plotted\n",
    "db_nbre = Database('nbre_liu.tdb')\n",
    "my_phases_nbre = ['CHI_RENB', 'SIGMARENB', 'FCC_RENB', 'LIQUID_RENB', 'BCC_RENB', 'HCP_RENB']\n",
    "\n",
    "# Create a matplotlib Figure object and get the active Axes\n",
    "fig = plt.figure(figsize=(9,6))\n",
    "axes = fig.gca()\n",
    "\n",
    "# Plot the phase diagram on the existing axes using the `plot_kwargs={'ax': axes}` keyword argument\n",
    "binplot(db_nbre, ['NB', 'RE'] , my_phases_nbre, {v.X('RE'): (0,1,0.01), v.T: (1000, 3500, 20), v.P:101325}, plot_kwargs={'ax': axes})\n",
    "\n",
    "axes.set_xlim(0, 1)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.6 64-bit",
   "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.9.13"
  },
  "vscode": {
   "interpreter": {
    "hash": "dffc026621927c134bf51c98cc1a2a1db0bb9758f5626f77d77eb66ad657b830"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
