{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Steady state heat equation\n", "======\n", "\n", "This notebook will setup and solve the steady state heat equation:\n", "\n", "\\\\[\n", "\\nabla(k\\nabla)T = h\n", "\\\\]\n", "\n", "where $k$ is the diffusivity, T the temperature field and $h$ the source term.\n", "This model is solved in a rectangular domain with Dirichlet boundary conditions on the top and bottom walls.\n", "\n", "**Keywords:** initial conditions, boundary conditions, heat equation\n", "\n", "\n", "![title](./images/01_temperature.png)\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import underworld as uw\n", "import underworld.visualisation as vis" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Set some python variables for model parameters\n", "boxHeight = 1.0\n", "boxLength = 2.0\n", "resx = 16\n", "resy = 8\n", "\n", "# create mesh and variables\n", "mesh = uw.mesh.FeMesh_Cartesian( elementType = (\"Q1/dQ0\"), \n", " elementRes = (resx, resy), \n", " minCoord = (0., 0.), \n", " maxCoord = (boxLength, boxHeight))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Create mesh variables for the temperature field & initialise. \n", "temperatureField = mesh.add_variable( nodeDofCount=1 )\n", "temperatureField.data[:] = 0." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Using the `specialSets` of the mesh we designate the vertices to \n", "# build the `DirichletCondition` object - Refer to the `Systems` section \n", "# of the user guide.\n", "botWalls = mesh.specialSets[\"Bottom_VertexSet\"]\n", "topWalls = mesh.specialSets[ \"Top_VertexSet\"]\n", "bcWalls = botWalls + topWalls\n", "tempBC = uw.conditions.DirichletCondition( variable=temperatureField, indexSetsPerDof=(bcWalls,) )" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# set bottom wall temperature bc\n", "temperatureField.data[botWalls] = 1.0\n", "# set top wall temperature bc\n", "temperatureField.data[topWalls] = 0.0" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# visualisation of temperature field & mesh\n", "fig = vis.Figure(figsize=(800,400))\n", "fig.append( vis.objects.Mesh(mesh) )\n", "fig.append( vis.objects.Surface( mesh, temperatureField, colours=\"blue white red\" ) )\n", "fig.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Heat Equation System setup & solve\n", "-----\n", "\n", "Temperature field, diffusivity and boundary conditions are passed to the SteadyStateHeat system function." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "heatequation = uw.systems.SteadyStateHeat(temperatureField = temperatureField, \n", " fn_diffusivity = 1.0, \n", " conditions = tempBC)\n", "\n", "# get the default heat equation solver\n", "heatsolver = uw.systems.Solver(heatequation)\n", "# solve\n", "heatsolver.solve()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# visualisation of temperature field & mesh\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# As a test, ensure we reproduce the required average temperature\n", "import numpy as np\n", "tottemp = uw.utils.Integral(temperatureField, mesh)\n", "avtemp = tottemp.evaluate()[0] / (boxHeight*boxLength)\n", "if not np.isclose(avtemp,0.5):\n", " raise RuntimeError(\"Incorrect average temperature produced by model. \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5.3" } }, "nbformat": 4, "nbformat_minor": 1 }