{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Heat Capacity of Cementite ($Fe_3C$)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Bengt Hallstedt, Dejan Djurovic, Jörg von Appen, Richard Dronskowski, Alexey Dick, Fritz Körmann, Tilmann Hickel, Jörg Neugebauer, Thermodynamic properties of cementite, Calphad, Volume 34, Issue 1, March 2010, Pages 129-133, ISSN 0364-5916, http://dx.doi.org/10.1016/j.calphad.2010.01.004. (http://www.sciencedirect.com/science/article/pii/S0364591610000052)\n",
    "\n",
    "The TDB file used here differs slightly from the published TDB to ensure compatibility with pycalphad's TDB parser. All phases except cementite are omitted. The numerical results should be the same."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:16:53.923299Z",
     "iopub.status.busy": "2022-02-19T19:16:53.923299Z",
     "iopub.status.idle": "2022-02-19T19:16:53.933979Z",
     "shell.execute_reply": "2022-02-19T19:16:53.932979Z"
    }
   },
   "outputs": [],
   "source": [
    "TDB = \"\"\"\n",
    " ELEMENT C    GRAPHITE                   12.011     1054.0      5.7423 ! \n",
    " ELEMENT FE   BCC_A2                     55.847     4489.0     27.2797 ! \n",
    " TYPE_DEFINITION % SEQ * !\n",
    " TYPE_DEFINITION A GES AMEND_PHASE_DESCRIPTION @ MAGNETIC -3 0.28 !\n",
    " PHASE CEMENTITE_D011 %A 2 3 1 !\n",
    " CONSTITUENT CEMENTITE_D011 : FE : C : !\n",
    " PARAMETER  G(CEMENTITE_D011,FE:C;0) 0.01 +GFECEM; 6000 N !\n",
    " PARAMETER  TC(CEMENTITE_D011,FE:C;0) 0.01 485.00; 6000 N !\n",
    " PARAMETER  BMAGN(CEMENTITE_D011,FE:C;0) 0.01 1.008; 6000 N !\n",
    " FUNCTION GFECEM      0.01  +11369.937746-5.641259263*T-8.333E-6*T**4;\n",
    "        43.00  Y  +11622.647246-59.537709263*T+15.74232*T*LN(T)\n",
    "       -0.27565*T**2;\n",
    "       163.00  Y  -10195.860754+690.949887637*T-118.47637*T*LN(T)\n",
    "                  -0.0007*T**2+590527*T**(-1);\n",
    "      6000.00  N !\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Do some initial setup, including setting up the workspace."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:16:53.938980Z",
     "iopub.status.busy": "2022-02-19T19:16:53.937980Z",
     "iopub.status.idle": "2022-02-19T19:16:54.901699Z",
     "shell.execute_reply": "2022-02-19T19:16:54.901699Z"
    }
   },
   "outputs": [],
   "source": [
    "# Only needed in a Jupyter Notebook\n",
    "%matplotlib inline\n",
    "# Optional plot styling\n",
    "import matplotlib\n",
    "matplotlib.style.use('fivethirtyeight')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We compute the molar heat capacity at all temperatures from 1K to 2000K with a step size of 0.1K."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:16:54.901699Z",
     "iopub.status.busy": "2022-02-19T19:16:54.901699Z",
     "iopub.status.idle": "2022-02-19T19:16:56.661508Z",
     "shell.execute_reply": "2022-02-19T19:16:56.661508Z"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from pycalphad import Workspace, as_property, variables as v\n",
    "\n",
    "wks = Workspace(TDB, ['FE', 'C'], 'CEMENTITE_D011',\n",
    "                {v.N: 1, v.P: 1e5, v.T: (1, 2000, 0.1), v.X('C'): 0.25})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The isobaric molar heat capacity is defined as the derivative of the total molar enthalpy (`HM`) with respect to temperature (`T`). We use \"Jansson derivative\" syntax to specify this as a property. In addition, we give this property a legible name and specify our desired physical units."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-19T19:16:56.761125Z",
     "iopub.status.busy": "2022-02-19T19:16:56.761125Z",
     "iopub.status.idle": "2022-02-19T19:16:56.981139Z",
     "shell.execute_reply": "2022-02-19T19:16:56.981139Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Isobaric Heat Capacity [J/mol/K]')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 42236 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 = 'Isobaric Heat Capacity'\n",
    "heat_capacity.display_units = 'J/mol/K'\n",
    "plt.plot(wks.get(v.T), wks.get(heat_capacity))\n",
    "plt.xlabel(f'{v.T.display_name} [{v.T.display_units}]')\n",
    "plt.ylabel(f'{heat_capacity.display_name} [{heat_capacity.display_units}]')"
   ]
  },
  {
   "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"
  },
  "vscode": {
   "interpreter": {
    "hash": "dffc026621927c134bf51c98cc1a2a1db0bb9758f5626f77d77eb66ad657b830"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
