{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Equilibrium Properties and Partial Ordering (Al-Fe and Al-Ni)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:18:09.190653Z",
     "iopub.status.busy": "2022-02-19T19:18:09.172558Z",
     "iopub.status.idle": "2022-02-19T19:18:10.182480Z",
     "shell.execute_reply": "2022-02-19T19:18:10.182480Z"
    }
   },
   "outputs": [],
   "source": [
    "# Only needed in a Jupyter Notebook\n",
    "%matplotlib inline\n",
    "# Optional plot styling\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "matplotlib.style.use('bmh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:18:10.201991Z",
     "iopub.status.busy": "2022-02-19T19:18:10.200454Z",
     "iopub.status.idle": "2022-02-19T19:18:12.092175Z",
     "shell.execute_reply": "2022-02-19T19:18:12.092175Z"
    }
   },
   "outputs": [],
   "source": [
    "from pycalphad import Database, Workspace, as_property, variables as v"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Al-Fe (Heat Capacity and Degree of Ordering)\n",
    "Here we compute equilibrium thermodynamic properties in the Al-Fe system. We know that only B2 and liquid are stable in the temperature range of interest, but we just as easily could have included all the phases in the calculation using `my_phases = list(db.phases.keys())`. Notice that the syntax for specifying a range is `(min, max, step)`. We can also directly specify a list of temperatures using the list syntax, e.g., `[300, 400, 500, 1400]`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:18:12.099168Z",
     "iopub.status.busy": "2022-02-19T19:18:12.098168Z",
     "iopub.status.idle": "2022-02-19T19:18:13.301974Z",
     "shell.execute_reply": "2022-02-19T19:18:13.301974Z"
    }
   },
   "outputs": [],
   "source": [
    "dbf = Database('alfe_sei.TDB')\n",
    "my_phases = ['LIQUID', 'B2_BCC']\n",
    "wks1 = Workspace(dbf, ['AL', 'FE', 'VA'], my_phases,\n",
    "                 {v.X('AL'): 0.25, v.T: (300, 2000, 50), v.P: 101325, v.N: 1})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We also compute degree of ordering at fixed temperature as a function of composition."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:18:13.331442Z",
     "iopub.status.busy": "2022-02-19T19:18:13.331442Z",
     "iopub.status.idle": "2022-02-19T19:18:13.621652Z",
     "shell.execute_reply": "2022-02-19T19:18:13.621652Z"
    }
   },
   "outputs": [],
   "source": [
    "wks2 = Workspace(dbf, ['AL', 'FE', 'VA'], my_phases,\n",
    "                 {v.X('AL'): (0,1,0.01), v.T: 700, v.P: 101325, v.N: 1})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plots\n",
    "Next we plot the degree of ordering versus temperature. We can see that the decrease in the degree of ordering is relatively steady and continuous. This is indicative of a second-order transition from partially ordered B2 to disordered bcc (A2)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Degree of ordering')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 53420 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "degree_of_ordering = as_property('degree_of_ordering(B2_BCC)')\n",
    "degree_of_ordering.display_name = 'Degree of ordering'\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot()\n",
    "ax.set_title('Al-Fe: Degree of bcc ordering vs T [X(AL)=0.25]')\n",
    "ax.plot(wks1.get(v.T), wks1.get(degree_of_ordering))\n",
    "ax.set_xlabel(f'{v.T.display_name} [{v.T.display_units}]')\n",
    "ax.set_ylabel(f'{degree_of_ordering.display_name}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For the heat capacity curve shown below we notice a sharp increase in the heat capacity around 750 K. This is indicative of a magnetic phase transition and, indeed, the temperature at the peak of the curve coincides with 75% of 1043 K, the Curie temperature of pure Fe. (Pure bcc Al is paramagnetic so it has an effective Curie temperature of 0 K.)\n",
    "\n",
    "The decrease in heat capacity near 1250 K corresponds to the order-disorder transition observed in the above figure. We also observe a sharp jump in the heat capacity near 1800 K, corresponding to the melting of the bcc phase."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Heat Capacity [J/mol/K]')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 57388 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "heat_capacity = as_property('HM.T')\n",
    "heat_capacity.display_name = 'Heat Capacity'\n",
    "heat_capacity.display_units = 'J/mol/K'\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot()\n",
    "ax.set_title('Al-Fe: Heat capacity vs T [X(AL)=0.25]')\n",
    "ax.plot(wks1.get(v.T), wks1.get(heat_capacity))\n",
    "ax.set_xlabel(f'{v.T.display_name} [{v.T.display_units}]')\n",
    "ax.set_ylabel(f'{heat_capacity.display_name} [{heat_capacity.display_units}]')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To understand more about what's happening around 700 K, we plot the degree of ordering versus composition. Note that this plot excludes all other phases except `B2_BCC`. We observe the presence of disordered bcc (A2) until around 13% Al or Fe, when the phase begins to order."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Degree of ordering')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 57172 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure()\n",
    "ax = fig.add_subplot()\n",
    "ax.set_title('Al-Fe: Degree of bcc ordering vs X(AL) [T=700 K]')\n",
    "ax.plot(wks2.get(v.X('B2_BCC', 'AL')), wks2.get(degree_of_ordering))\n",
    "ax.set_xlabel(f\"{v.X('B2_BCC', 'AL').display_name} [{v.X('B2_BCC', 'AL').display_units}]\")\n",
    "ax.set_ylabel(f'{degree_of_ordering.display_name}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that the plot abruptly ends around 0.8 fraction of Al, corresponding to the (metastable, because only liquid is entered) limit of stability of B2 on the phase diagram at this temperature. Because we want to plot in the metastable region, we will now tell pycalphad that we want to remove liquid from the calculation and then repeat it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Degree of ordering')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 47056 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "wks2.phases = ['B2_BCC']\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot()\n",
    "ax.plot(wks2.get(v.X('B2_BCC', 'AL')), wks2.get(degree_of_ordering))\n",
    "ax.set_xlabel(f\"{v.X('B2_BCC', 'AL').display_name} [{v.X('B2_BCC', 'AL').display_units}]\")\n",
    "ax.set_ylabel(f'{degree_of_ordering.display_name}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Al-Ni (Degree of Ordering)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:18:14.121543Z",
     "iopub.status.busy": "2022-02-19T19:18:14.121543Z",
     "iopub.status.idle": "2022-02-19T19:18:16.781182Z",
     "shell.execute_reply": "2022-02-19T19:18:16.781182Z"
    }
   },
   "outputs": [],
   "source": [
    "db_alni = Database('NI_AL_DUPIN_2001.TDB')\n",
    "phases = ['LIQUID', 'FCC_L12']\n",
    "wks3 = Workspace(db_alni, ['AL', 'NI', 'VA'], phases,\n",
    "                 {v.X('AL'): 0.10, v.T: (300, 2500, 20), v.P: 101325})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plots\n",
    "In the plot below we observe two phases designated `FCC_L12`. This is indicative of a miscibility gap. The ordered gamma-prime phase steadily decreases in amount with increasing temperature until it completely disappears around 750 K, leaving only the disordered gamma phase."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7f7c66b33940>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 42856 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure()\n",
    "ax = fig.add_subplot()\n",
    "ax.plot(wks3.get(v.T), wks3.get('NP(LIQUID)'), label='Liquid')\n",
    "ax.plot(wks3.get(v.T), wks3.get('NP(FCC_L12#1)'), label='FCC#1')\n",
    "ax.plot(wks3.get(v.T), wks3.get('NP(FCC_L12#2)'), label='FCC#2')\n",
    "ax.set_xlabel(f'{v.T.display_name} [{v.T.display_units}]')\n",
    "ax.set_ylabel('fraction')\n",
    "ax.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the plot below we see that the degree of ordering does not change at all in each phase. There is a very abrupt disappearance of the completely ordered gamma-prime phase, leaving the completely disordered gamma phase. This is a first-order phase transition."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7f7c66b6ceb0>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 37940 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "dis_degree_of_ordering = as_property('degree_of_ordering(FCC_L12#2)')\n",
    "dis_degree_of_ordering.display_name = '$\\gamma$ (disordered fcc)'\n",
    "L12_degree_of_ordering = as_property('degree_of_ordering(FCC_L12#1)')\n",
    "L12_degree_of_ordering.display_name = '$\\gamma^\\prime$ (ordered fcc)'\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot()\n",
    "ax.plot(wks3.get(v.T), wks3.get(dis_degree_of_ordering),\n",
    "        label=dis_degree_of_ordering.display_name)\n",
    "ax.plot(wks3.get(v.T), wks3.get(L12_degree_of_ordering),\n",
    "        label=L12_degree_of_ordering.display_name)\n",
    "ax.set_xlabel(f'{v.T.display_name} [{v.T.display_units}]')\n",
    "ax.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "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.10.6"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  },
  "vscode": {
   "interpreter": {
    "hash": "dffc026621927c134bf51c98cc1a2a1db0bb9758f5626f77d77eb66ad657b830"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
