{ "cells": [ { "cell_type": "markdown", "id": "417a4f58-03cd-4da1-b4e0-19f18eb51627", "metadata": {}, "source": [ "# Heat transfer with time-variant boundary conditions\n", "\n", "Heat transfer example based on a Julia example\n", "\n", "see: https://docs.sciml.ai/MethodOfLines/stable/tutorials/heat/" ] }, { "cell_type": "code", "execution_count": 1, "id": "07d255d5-2914-4be9-b7ee-05bc4456d101", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import pyfvtool as pf" ] }, { "cell_type": "code", "execution_count": 2, "id": "0127235f-ea60-49eb-9690-a111f881a685", "metadata": {}, "outputs": [], "source": [ "def T_analytic(x,t):\n", " return np.exp(-t)*np.cos(x)" ] }, { "cell_type": "code", "execution_count": 3, "id": "51fb650a-3734-4ae0-a518-8723cfd3fb29", "metadata": {}, "outputs": [], "source": [ "# Parameters\n", "L = 1.0 # domain length\n", "alfa = 1.0 # heat diffusion\n", "qs = 1000.0 # [W/m^2]\n", "t_sim = L**2/(20*alfa) # [s]\n", "time_steps = 50\n", "dt = t_sim/time_steps # \n", "Nx = 20 # number of cells" ] }, { "cell_type": "code", "execution_count": 4, "id": "684aa3bf-68c4-40a9-8b5b-edc54ed60f8c", "metadata": {}, "outputs": [], "source": [ "m = pf.Grid1D(Nx, L)" ] }, { "cell_type": "code", "execution_count": 5, "id": "4182eff9-1ca5-45ac-a609-dfbd7574346a", "metadata": {}, "outputs": [], "source": [ "# Initial condition\n", "T0 = np.cos(m.cellcenters.x) # cosine temperature profile, amplitude = 1.0 K\n", "T = pf.CellVariable(m, T0) # initial condition" ] }, { "cell_type": "code", "execution_count": 6, "id": "43586d7c-7df2-4727-a2c3-a53088963f28", "metadata": {}, "outputs": [], "source": [ "# Boundary conditions\n", "T.BCs.left.a = 0.0\n", "T.BCs.left.b = 1.0\n", "T.BCs.right.a = 0.0\n", "T.BCs.right.b = 1.0" ] }, { "cell_type": "code", "execution_count": 7, "id": "487586b3-3f9d-4da3-a553-b924ad2a0056", "metadata": {}, "outputs": [], "source": [ "# physical parameters\n", "alfa_cell = pf.CellVariable(m, alfa)\n", "alfa_face = pf.harmonicMean(alfa_cell)" ] }, { "cell_type": "code", "execution_count": 8, "id": "a30faadd-bec3-44a0-aaa0-0ac34b5fed05", "metadata": {}, "outputs": [], "source": [ "M_diff = pf.diffusionTerm(alfa_face)" ] }, { "cell_type": "code", "execution_count": 9, "id": "eccb3a48-9396-4d07-b448-dcab3c63c8c2", "metadata": {}, "outputs": [], "source": [ "t=0\n", "while t" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plt.figure(1)\n", "plt.clf()\n", "plt.plot(x, T_an, x, T_num, 'o')\n", "plt.legend({'Analytic', 'PyFVTool FVM'})\n", "plt.xlabel('x [m]')\n", "plt.ylabel('deltaT [K]')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": 12, "id": "d4b22cc8-0bfe-4373-ba6d-b2cf2ca37eee", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.00014085141590735798\n" ] } ], "source": [ "er = np.sum(np.abs(T_num-T_an)/T_an)/Nx\n", "print(er)" ] }, { "cell_type": "code", "execution_count": 13, "id": "93c24b52-a4e6-4aa5-abeb-c3202a2458fc", "metadata": {}, "outputs": [], "source": [ "assert er<0.0005" ] }, { "cell_type": "code", "execution_count": null, "id": "5fe19eb4-032b-4de3-bd7a-093ab019d125", "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.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }