{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting using the Mapping API\n",
    "\n",
    "Mapping uses the algorithms outlined in B. Sundman, N. Dupin, B. Hallstedt, *Calphad* 75 (2021) 102330 to construct phase diagrams by stepping along phase boundaries.\n",
    "\n",
    "binplot and ternplot are thin wrappers around the binary and ternary mapping strategies; however, additional plotting capabilities are available through the mapping module."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Binary phase diagrams with mapping\n",
    "\n",
    "This shows how to plot a binary phase diagram through mapping as an alternative to binplot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 51064 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pycalphad import Database, variables as v\n",
    "from pycalphad.mapping import BinaryStrategy, plot_binary\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "dbf = Database('alzn_mey.tdb')\n",
    "comps = ['AL', 'ZN', 'VA']\n",
    "conds = {v.N: 1, v.P:101325, v.T: (300, 1000, 10), v.X('ZN'):(0, 1, 0.02)}\n",
    "\n",
    "# Phases will be automatically filtered with components if no phases are passed\n",
    "binary = BinaryStrategy(dbf, comps, phases=None, conditions=conds)\n",
    "binary.initialize()\n",
    "binary.do_map()\n",
    "plot_binary(binary)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 51860 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plotting in weight fraction\n",
    "ax = plot_binary(binary, v.W('ZN'), v.T)\n",
    "ax.set_xlim([0, 1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Similarly, the same can be done for ternaries using TernaryStrategy and plot_ternary."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step plotting\n",
    "\n",
    "Step mapping allows computing equilibrium along a single axis. By default, step plotting will plot phase fraction vs. variable axis, but this can be modified through plotting."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 43484 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pycalphad.mapping import StepStrategy, plot_step\n",
    "\n",
    "dbf = Database('Al-Cu-Y.tdb')\n",
    "comps = ['AL', 'CU', 'Y', 'VA']\n",
    "conds = {v.T: (500, 2000, 10), v.X('AL'): 0.8, v.X('CU'): 0.1, v.P: 101325, v.N: 1}\n",
    "\n",
    "step = StepStrategy(dbf, comps, phases=None, conditions=conds)\n",
    "step.initialize()\n",
    "step.do_map()\n",
    "\n",
    "plot_step(step)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 39292 characters]",
      "text/plain": [
       "<Figure size 1000x400 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plotting composition of Al and Cu in each phase\n",
    "fig, ax = plt.subplots(1, 2, figsize=(10,4))\n",
    "plot_step(step, x=v.T, y=v.X('AL'), ax=ax[0])\n",
    "plot_step(step, x=v.T, y=v.X('CU'), ax=ax[1])\n",
    "ax[0].set_ylim([0, 1])\n",
    "ax[1].set_ylim([0, 1])\n",
    "fig.tight_layout()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Isopleth plotting\n",
    "\n",
    "For multicomponent systems, isopleths can be computed and plotted."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\ury3\\OneDrive - LLNL\\Documents\\Projects\\misc\\pycalphad-workspace\\pycalphad\\pycalphad\\core\\conditions.py:116: UserWarning: Some specified compositions are below the minimum allowed composition of 1e-10.\n",
      "  warnings.warn(\n",
      "Point could not be found from {T: 1150.0, P: 101325, N: 1, X_Y: 0.1, X_CU: np.float64(0.9)}\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 116012 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pycalphad.mapping import IsoplethStrategy, plot_isopleth\n",
    "\n",
    "dbf = Database('Al-Cu-Y.tdb')\n",
    "comps = ['AL', 'CU', 'Y', 'VA']\n",
    "conds = {v.T: (300, 2000, 10), v.P: 101325, v.N: 1, v.X('Y'): 0.1, v.X('CU'): (0, 1, 0.01)}\n",
    "\n",
    "iso = IsoplethStrategy(dbf, comps, phases=None, conditions=conds)\n",
    "iso.initialize()\n",
    "iso.do_map()\n",
    "plot_isopleth(iso)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pycalphad-workspace",
   "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
