{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "604ace42-84fa-4391-bd92-0209e50a4dda", "metadata": { "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from scipy.optimize import curve_fit\n", "import scipy.constants as const\n", "import h5py" ] }, { "cell_type": "markdown", "id": "214268c5-b46c-48bb-8abd-d82131611df3", "metadata": { "tags": [] }, "source": [ "## load HDF5 file with context manager" ] }, { "cell_type": "code", "execution_count": null, "id": "27079012-b1c5-489a-8ae9-f2f1f6901a54", "metadata": { "tags": [] }, "outputs": [], "source": [ "with h5py.File(\n", " r\"kaust_temp_entry_1.nxs\", \"r\"\n", ") as f:\n", " # get the data\n", " data = f[list(f.keys())[0]][\"data\"]\n", " SMU_mesI1 = data[\"Simple_Sweep\"][\"SMU_mesI1\"][:]\n", " SMU_mesV1 = data[\"Simple_Sweep\"][\"SMU_mesV1\"][:]\n", " SMU_setV1 = data[\"Simple_Sweep\"][\"SMU_setV1\"][:]\n", " temperatue_PID = data[\"PID_current_value\"][:]" ] }, { "cell_type": "markdown", "id": "6aaecf64-d685-4308-9a22-9a9ae6c3ff4e", "metadata": { "tags": [] }, "source": [ "## Split data into chucks of 51 points" ] }, { "cell_type": "code", "execution_count": null, "id": "ecf3df2b-0684-46c4-a7ac-fdbdbabe1f23", "metadata": { "tags": [] }, "outputs": [], "source": [ "n = 51\n", "mesV1_split = [SMU_mesV1[i:i + n] for i in range(0, len(SMU_mesV1), n)]\n", "mesI1_split = [SMU_mesI1[i:i + n] for i in range(0, len(SMU_mesI1), n)]\n", "setV1_split = [SMU_setV1[i:i + n] for i in range(0, len(SMU_setV1), n)]" ] }, { "cell_type": "markdown", "id": "f9da0b63-268b-478d-bbfa-6d8e7ef365da", "metadata": { "tags": [] }, "source": [ "## Perform the fit for each chuck and plot it" ] }, { "cell_type": "code", "execution_count": null, "id": "1a5c315c-98cb-4da3-a81a-df0493ae09e0", "metadata": { "tags": [] }, "outputs": [], "source": [ "a_list = []\n", "b_list = []\n", "R = []\n", "for mesV1, mesI1 in zip(mesV1_split, mesI1_split):\n", " fit_result = curve_fit(lambda x, a, b: a * x + b, mesV1, mesI1)\n", " a, b = fit_result[0]\n", " a_list.append(a)\n", " b_list.append(b)\n", " R.append(1/a)\n", "\n", "# Plot the individual IV curves and fits\n", "plt.figure()\n", "for mesV1, mesI1, a, b in zip(mesV1_split, mesI1_split, a_list, b_list):\n", " plt.plot(mesV1, mesI1, 'x')\n", " plt.plot(mesV1, a * mesV1 + b)\n", "plt.xlabel(\"Voltage (V)\")\n", "plt.ylabel(\"Current (A)\")\n", "plt.title(\"IV curves and fits\")" ] }, { "cell_type": "markdown", "id": "aed3a692-2cd8-40c0-bb1f-a845dedbbd90", "metadata": { "tags": [] }, "source": [ "## Fit and plot the resistance vs temperature with an exponential function" ] }, { "cell_type": "code", "execution_count": null, "id": "3fa8b188-8a68-4cd0-98c0-fb90aff1ea5f", "metadata": { "tags": [] }, "outputs": [], "source": [ "def exp_fit(T, R0, Ea):\n", " return R0 * np.exp(Ea*const.e/(T * const.k))\n", "# Add starting values for the fit\n", "fit_result = curve_fit(exp_fit, temperatue_PID, R, p0=[50, 0.5])\n", "R0, Ea = fit_result[0]\n", "\n", "## Plot the resistance against the temperature\n", "plt.figure()\n", "plt.plot(temperatue_PID, R, 'x')\n", "plt.xlabel(\"Temperature (K)\")\n", "plt.ylabel(\"Resistance (Ohm)\")\n", "plt.title(\"Resistance vs Temperature\")\n", "\n", "# Plot the fit\n", "T = np.linspace(temperatue_PID.min(), temperatue_PID.max(), 100)\n", "plt.plot(T, exp_fit(T, R0, Ea))\n", "plt.title(f\"R0 = {R0:.2f}, $E_a$ = {Ea:.2f}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "68f94d29-dfc5-44f6-9d58-dae35c6345fe", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.10" } }, "nbformat": 4, "nbformat_minor": 5 }