{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Calculate and Plot Activity\n",
    "\n",
    "\n",
    "Given an existing database for Al-Zn, we would like to calculate the activity of the liquid.\n",
    "\n",
    "## Experimental activity results\n",
    "\n",
    "In order to make sure we are correct, we'll compare the values with experimental results.\n",
    "Experimental activities are digtized from Fig 18 in A. Yazawa, Y.K. Lee, Thermodynamic Studies of the Liquid Aluminum Alloy Systems, Trans. Japan Inst. Met. 11 (1970) 411–418. https://doi.org/10.2320/matertrans1960.11.411.\n",
    "\n",
    "The measurements at at 1073 K and they used a reference state of the pure Zn at that temperature.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:18:20.331185Z",
     "iopub.status.busy": "2022-02-19T19:18:20.331185Z",
     "iopub.status.idle": "2022-02-19T19:18:20.341034Z",
     "shell.execute_reply": "2022-02-19T19:18:20.341034Z"
    }
   },
   "outputs": [],
   "source": [
    "exp_x_zn =   [0.0482, 0.1990, 0.3550, 0.5045, 0.6549, 0.8070, 0.9569]\n",
    "exp_acr_zn = [0.1154, 0.3765, 0.5411, 0.6433, 0.7352, 0.8384, 0.9531]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set up the database\n",
    "\n",
    "Al-Zn database is taken from S. Mey, Reevaluation of the Al-Zn system, Zeitschrift F{ü}r Met. 84 (1993) 451–455."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:18:20.341034Z",
     "iopub.status.busy": "2022-02-19T19:18:20.341034Z",
     "iopub.status.idle": "2022-02-19T19:18:23.087387Z",
     "shell.execute_reply": "2022-02-19T19:18:23.086400Z"
    }
   },
   "outputs": [],
   "source": [
    "from pycalphad import Database, equilibrium, variables as v\n",
    "import numpy as np\n",
    "\n",
    "dbf = Database('alzn_mey.tdb') \n",
    "\n",
    "comps = ['AL', 'ZN', 'VA']\n",
    "phases = list(dbf.phases.keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculate the reference state\n",
    "\n",
    "Because all chemical activities must be specified with a reference state, we're going to choose a reference state as the pure element at the same temperature, consistent with the experimental data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:18:23.291415Z",
     "iopub.status.busy": "2022-02-19T19:18:23.211494Z",
     "iopub.status.idle": "2022-02-19T19:18:23.321627Z",
     "shell.execute_reply": "2022-02-19T19:18:23.321627Z"
    }
   },
   "outputs": [],
   "source": [
    "ref_eq = equilibrium(dbf, ['ZN'], phases, {v.P: 101325, v.T: 1023})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculate the equilibria\n",
    "\n",
    "Do the calculation over the composition range"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:18:23.483506Z",
     "iopub.status.busy": "2022-02-19T19:18:23.461343Z",
     "iopub.status.idle": "2022-02-19T19:18:24.011362Z",
     "shell.execute_reply": "2022-02-19T19:18:24.011362Z"
    }
   },
   "outputs": [],
   "source": [
    "eq = equilibrium(dbf, comps, phases, {v.P: 1013325, v.T: 1023, v.X('ZN'): (0, 1, 0.005)})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get the chemical potentials and calculate activity\n",
    "\n",
    "We need to select the chemical potentials from the xarray Dataset and calculate the activity."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:18:24.020970Z",
     "iopub.status.busy": "2022-02-19T19:18:24.020970Z",
     "iopub.status.idle": "2022-02-19T19:18:24.031439Z",
     "shell.execute_reply": "2022-02-19T19:18:24.031439Z"
    }
   },
   "outputs": [],
   "source": [
    "chempot_ref = ref_eq.MU.sel(component='ZN').squeeze()\n",
    "chempot = eq.MU.sel(component='ZN').squeeze()\n",
    "\n",
    "acr_zn = np.exp((chempot - chempot_ref)/(8.315*1023))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot the result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:18:24.051369Z",
     "iopub.status.busy": "2022-02-19T19:18:24.040496Z",
     "iopub.status.idle": "2022-02-19T19:18:24.241790Z",
     "shell.execute_reply": "2022-02-19T19:18:24.241790Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7f4b451cf820>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 34260 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.plot(eq.X.sel(component='ZN', vertex=0).squeeze(), acr_zn, label='Calculated')\n",
    "# add experimental data\n",
    "plt.scatter(exp_x_zn, exp_acr_zn, label='Yazawa 1970')\n",
    "\n",
    "plt.xlim((0, 1))\n",
    "plt.ylim((0, 1))\n",
    "plt.gca().set_aspect(1)\n",
    "plt.xlabel('X(ZN)')\n",
    "plt.ylabel('ACR(ZN)')\n",
    "plt.title('Activity of Zn at 1073K')\n",
    "plt.legend(loc=0)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
