{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting Ternary Phase Diagrams and Using Triangular Axes\n",
    "\n",
    "Often in thermodynamics, it is useful to use a two dimensional plot to express equilibria in a three component system with fixed potentials. The `ternplot` API provides a straightforward way to calculate equilibria and generate ternary phase diagrams. Alternatively, if you already have an equilibrium calculation, you can directly import and use `eqplot`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ternplot\n",
    "\n",
    "Here we will use the ternplot API to plot an isothermal section of the Al-Cu-Y system from the thermodynamic database found at Jacob et al, Calphad 54 (2016) (1-15) (https://doi.org/10.1016/j.calphad.2016.04.013)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 38.9 s\n",
      "Wall time: 39.2 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<TriangularAxes: title={'center': 'Cr-Fe-Nb'}, xlabel='X(Cr)', ylabel='X(Fe)'>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 150152 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "from pycalphad import Database, ternplot\n",
    "from pycalphad import variables as v\n",
    "\n",
    "db_cr_fe_nb = Database('CrFeNb_Jacob2016.tdb')\n",
    "comps = ['CR', 'FE', 'NB', 'VA']\n",
    "phases = list(db_cr_fe_nb.phases.keys())\n",
    "conds = {v.T: 1323, v.P:101325, v.X('CR'): (0,1,0.015), v.X('FE'): (0,1,0.015)}\n",
    "\n",
    "%time ternplot(db_cr_fe_nb, comps, phases, conds, x=v.X('CR'), y=v.X('FE'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here is another example for plotting the isothermal section of the Al-Cu-Y system from the thermodynamic database found at the [NIST CALPHAD assessments](http://hdl.handle.net/11115/101) assessed by Zhang et al. [Calphad 35 (2011)](https://doi.org/10.1016/j.calphad.2011.09.008). Labels for phases in the three-phase equilibria can be added for clarity."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:18:35.550927Z",
     "iopub.status.busy": "2022-02-19T19:18:35.550927Z",
     "iopub.status.idle": "2022-02-19T19:24:35.882475Z",
     "shell.execute_reply": "2022-02-19T19:24:35.882475Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 40.7 s\n",
      "Wall time: 40.9 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<TriangularAxes: title={'center': 'Al-Cu-Y'}, xlabel='X(Al)', ylabel='X(Y)'>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 131684 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "from pycalphad import Database, ternplot\n",
    "from pycalphad import variables as v\n",
    "\n",
    "db_al_cu_y = Database('Al-Cu-Y.tdb')\n",
    "comps = ['AL', 'CU', 'Y', 'VA']\n",
    "phases = list(db_al_cu_y.phases.keys())\n",
    "conds = {v.T: 830, v.P:101325, v.X('AL'): (0,1,0.015), v.X('Y'): (0,1,0.015)}\n",
    "\n",
    "%time ternplot(db_al_cu_y, comps, phases, conds, x=v.X('AL'), y=v.X('Y'), label_nodes=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Molar Ratios and Linear Combination Conditions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array(0.56818182), array(0.11363636), array(0.31818182)]\n"
     ]
    }
   ],
   "source": [
    "from pycalphad import Database, Workspace, variables as v\n",
    "\n",
    "db_al_cu_y = Database('Al-Cu-Y.tdb')\n",
    "comps = ['AL', 'CU', 'Y', 'VA']\n",
    "phases = list(db_al_cu_y.phases.keys())\n",
    "conds = {v.T: 830, v.P:101325,\n",
    "         v.X('AL')/v.X('CU'): 5, # molar ratio condition\n",
    "         0.1 * v.X('Y') + v.X('AL'): 0.6} # linear combination of mole fractions condition\n",
    "wks = Workspace(db_al_cu_y, comps, phases, conds)\n",
    "print(wks.get('X(AL)', 'X(CU)', 'X(Y)'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  },
  "vscode": {
   "interpreter": {
    "hash": "dffc026621927c134bf51c98cc1a2a1db0bb9758f5626f77d77eb66ad657b830"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
