{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Special `_MIX` Reference State\n",
    "\n",
    "pycalphad also includes special mixing reference state that is referenced to the endmembers for that phase with the `_MIX` suffix (`GM_MIX`, `HM_MIX`, `SM_MIX`, `CPM_MIX`). This is particularly useful for seeing how the mixing contributions from physical or excess models affect the energy.\n",
    "\n",
    "Below is an example for calculating this endmember-referenced mixing enthalpy for the $\\chi$ phase in Nb-Re. Notice that the four endmembers have a mixing enthalpy of zero."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:18:32.010452Z",
     "iopub.status.busy": "2022-02-19T19:18:32.001422Z",
     "iopub.status.idle": "2022-02-19T19:18:32.461953Z",
     "shell.execute_reply": "2022-02-19T19:18:32.461953Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 81624 characters]",
      "text/plain": [
       "<Figure size 900x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pycalphad import Database, calculate\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "dbf = Database(\"nbre_liu.tdb\")\n",
    "comps = [\"NB\", \"RE\", \"VA\"]\n",
    "\n",
    "# Calculate HMR for the Chi at 2800 K from X(RE)=0 to X(RE)=1\n",
    "result = calculate(dbf, comps, \"CHI_RENB\", P=101325, T=2800, output='HM_MIX')\n",
    "\n",
    "# Plot\n",
    "fig = plt.figure(figsize=(9,6))\n",
    "ax = fig.gca()\n",
    "ax.scatter(result.X.sel(component='RE'), result.HM_MIX, marker='.', s=5, label='CHI_RENB')\n",
    "ax.set_xlim((0, 1))\n",
    "ax.set_xlabel('X(RE)')\n",
    "ax.set_ylabel('HM_MIX')\n",
    "ax.set_title('Nb-Re CHI Mixing Enthalpy')\n",
    "ax.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculations at specific site fractions\n",
    "\n",
    "In the previous example, the mixing energy for the CHI phase in Nb-Re is sampled by sampling site fractions linearly between endmembers and then randomly across site fraction space.\n",
    "\n",
    "Imagine now that you'd like to calculate the mixing energy along a single internal degree of freedom (i.e. between two endmembers), referenced to those endmembers.\n",
    "\n",
    "A custom 2D site fraction array can be passed to the `points` argument of `calculate` and the `HM_MIX` property can be calculated as above.\n",
    "\n",
    "The sublattice model for CHI is `RE : NB,RE : NB,RE`.\n",
    "\n",
    "If we are interested in the interaction along the second sublattice when NB occupies the third sublattice, we need to construct a site fraction array of \n",
    "\n",
    "```python\n",
    "# RE, NB, RE, NB, RE\n",
    "[ 1.0, x, 1-x, 1.0, 0.0 ]\n",
    "```\n",
    "\n",
    "where `x` varies from 0 to 1. This fixes the site fraction of RE in the first sublattice to 1 and the site fractions of NB and RE in the third sublattice to 1 and 0, respectively. Note that the site fraction array is sorted first in sublattice order, then in alphabetic order within each sublattice (e.g. NB is always before RE within a sublattice)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:18:32.481334Z",
     "iopub.status.busy": "2022-02-19T19:18:32.471893Z",
     "iopub.status.idle": "2022-02-19T19:18:32.731301Z",
     "shell.execute_reply": "2022-02-19T19:18:32.731301Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Site fractions:\n",
      "[[1.00e+00 1.00e-12 1.00e+00 1.00e+00 0.00e+00]\n",
      " [1.00e+00 1.00e-03 9.99e-01 1.00e+00 0.00e+00]\n",
      " [1.00e+00 2.00e-03 9.98e-01 1.00e+00 0.00e+00]\n",
      " ...\n",
      " [1.00e+00 9.98e-01 2.00e-03 1.00e+00 0.00e+00]\n",
      " [1.00e+00 9.99e-01 1.00e-03 1.00e+00 0.00e+00]\n",
      " [1.00e+00 1.00e+00 0.00e+00 1.00e+00 0.00e+00]]\n",
      "Site fractions shape: (1001, 5) (1001 points, 5 internal degrees of freedom)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 32088 characters]",
      "text/plain": [
       "<Figure size 900x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pycalphad import Database, calculate\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "dbf = Database(\"nbre_liu.tdb\")\n",
    "comps = [\"NB\", \"RE\", \"VA\"]\n",
    "\n",
    "# The values for the internal degree of freedom we will vary\n",
    "n_pts = 1001\n",
    "x = np.linspace(1e-12, 1, n_pts)\n",
    "\n",
    "# Create the site fractions\n",
    "# The site fraction array is ordered first by sublattice, then alphabetically be species within a sublattice.\n",
    "# The site fraction array is therefore `[RE#0, NB#1, RE#1, NB#2, RE#2]`, where `#0` is the sublattice at index 0.\n",
    "# To calculate a RE:NB,RE:NB interaction requires the site fraction array to be [1, x, 1-x, 1, 0]\n",
    "# Note the 1-x is required for site fractions to sum to 1 in sublattice #1.\n",
    "site_fractions = np.array([np.ones(n_pts), x, 1-x, np.ones(n_pts), np.zeros(n_pts)]).T\n",
    "print('Site fractions:')\n",
    "print(site_fractions)\n",
    "print('Site fractions shape: {} ({} points, {} internal degrees of freedom)'.format(site_fractions.shape, site_fractions.shape[0], site_fractions.shape[1]))\n",
    "\n",
    "# Calculate HMR for the Chi at 2800 K from Y(CHI, 1, RE)=0 to Y(CHI, 1, RE)=1\n",
    "# Pass the custom site fractions to the `points` argument\n",
    "result = calculate(dbf, comps, \"CHI_RENB\", P=101325, T=2800, points=site_fractions, output='HM_MIX')\n",
    "# Extract the site fractions of RE in sublattice 1.\n",
    "Y_CHI_1_RE = result.Y.squeeze()[:, 2]\n",
    "\n",
    "# Plot\n",
    "fig = plt.figure(figsize=(9,6))\n",
    "ax = fig.gca()\n",
    "\n",
    "ax.scatter(Y_CHI_1_RE, result.HM_MIX, marker='.', s=5)\n",
    "ax.set_xlim((0, 1))\n",
    "ax.set_xlabel('Y(CHI, 1, RE)')\n",
    "ax.set_ylabel('HM_MIX')\n",
    "ax.set_title('Nb-Re CHI Mixing Enthalpy')\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.10.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "dffc026621927c134bf51c98cc1a2a1db0bb9758f5626f77d77eb66ad657b830"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
