{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PDE session 3\n",
    "## Elliptic PDE and iterative method using python"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "iVBORw0KGgoAAAANSUhEUgAAAfgAAACQCAYAAADk+Hn8AAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAB+KADAAQAAAABAAAAkAAAAABFmKT5AAAMf0lEQVR4Ae3dW+gUVRwH8J/mnVAhpVKLHgIVshcFQaEMQ7Cn6qEoMYIEfRGzIBEsby/ho0GFCEqEFN2o6B6URFFS0eVBkyS1C0q+SGJGlHUGWv43d//Bcf9nj5+FP+zOzM6c8/kd9vufndmZUef/fYQHAQIECBAgUJXA6Kp6ozMECBAgQIBAIyDgDQQCBAgQIFChgICvsKi6RIAAAQIEBLwxQIAAAQIEKhQQ8BUWVZcIECBAgICANwYIECBAgECFAgK+wqLqEgECBAgQEPDGAAECBAgQqFBAwFdYVF0iQIAAAQIC3hggQIAAAQIVCgj4CouqSwQIECBAQMAbAwQIECBAoEIBAV9hUXWJAAECBAgIeGOAAAECBAhUKCDgKyyqLhEgQIAAAQFvDBAgQIAAgQoFBHyFRdUlAgQIECAg4I0BAgQIECBQoYCAr7CoukSAAAECBAS8MUCAAAECBCoUEPAVFlWXCBAgQICAgDcGCBAgQIBAhQICvsKi6hIBAgQIEBDwxgABAgQIEKhQQMBXWFRdIkCAAAECAt4YIECAAAECFQoI+AqLqksECBAgQEDAGwMECBAgQKBCAQFfYVF1iQABAgQICHhjgAABAgQIVCgg4Cssqi4RIECAAAEBbwwQIECAAIEKBQR8hUXVJQIECBAgIOCNAQIECBAgUKGAgK+wqLpEgAABAgQEvDFAgAABAgQqFBDwFRZVlwgQIECAgIA3BggQIECAQIUCAr7CouoSAQIECBAQ8MYAAQIECBCoUEDAV1hUXSJAgAABAgLeGCBAgAABAhUKCPgKi6pL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6iDVhAgQIAAgawCAj4rp5URIECAAIEyBAR8GXXQCgIECBAgkFVAwGfltDICBAgQIFCGgIAvow5aQYAAAQIEsgoI+KycVkaAAAECBMoQ+AelxxM1yB+m/AAAAABJRU5ErkJggg=="
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this session we will use python to solve an elliptic PDE using an iterative method.\n",
    "\n",
    "We will determine the distribution of the temperature in a 1D bar where the  governing PDE is given as:\n",
    "\n",
    "$$ \\frac{\\partial^2 T}{\\partial x^2} = 0$$\n",
    "\n",
    "The dependent variable of this problem is the temperature $T$ while the independent variable is the position along the bar $x$. The figure below shows the problem we are interested in, e.g. a bar of length 2.5 m.\n",
    "\n",
    "![image.png](attachment:image.png)\n",
    "\n",
    "In addition to the PDE itself, we define the following boundary conditions which are defined Dirichlet conditions:\n",
    "\n",
    "$$ T(x=0) = 20 \\text{ deg}$$\n",
    "$$ T(x=2.5) = 100 \\text{ deg}$$\n",
    "\n",
    "We will approximate the second derivative of the temperature $T$ with respect to $x$ using a centered finite difference. The independent variable $x$ will be discretized in 50 points.\n",
    "\n",
    "\n",
    "You should carry the following tasks in this notebook:\n",
    "\n",
    "0. Express the numerical scheme for the chosen finite difference scheme applied to the PDE using a LaTeX format\n",
    "1. Import the necessary python modules\n",
    "2. Discretize the independent variable $x$ with the `linspace` function from `numpy` using 50 points\n",
    "3. Implement a function which solves the system of equations using the SOR method using a fixed number of iterations. This function should take as input the boundary conditions, the number of values in $x$ as well as the relaxation parameter $\\omega$ and the maximum number of iterations. This function should return the temperature along the entire bar.\n",
    "4. Find the temperature distribution in the bar using 25 iterations and a relaxation parameter $\\omega = 1.2$ and plot the results along the bar.\n",
    "5. Find the temperature distribution in the bar using 25 iterations and a relaxation parameter $\\omega = 2.5$ and plot the results along the bar, what can you comment on these results?\n",
    "6. Implement a function which solves the system of equations using the SOR method using a convergence criterion based on the average increment, over the bar, of the temperature. This function should have the same inputs as in question 3 but this time the tolerance should be given instead of the maximum number of iterations. This function should return the temperature along the entire bar as well as the number of iterations required to fulfil the convergence criterion.\n",
    "7. Find the temperature distribution in the bar using a tolerance of 0.1 and a relaxation parameter $\\omega = 1.2$ and plot the results along the bar.\n",
    "8. Find the temperature distribution in the bar using a tolerance of 0.001 and a relaxation parameter $\\omega = 1.2$ and plot the results along the bar.\n",
    "9. Can you comment upon the number of iterations required by the SOR method in questions 7 and 8?\n",
    "10. Is there an optimal value for the relaxation parameter $\\omega$ which minimize the number of iterations when a tolerance of 0.001 is enforced?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
