{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Analysis of schemes for the diffusion equation\n", "
\n", "\n", "\n", "The numerical experiments in the sections [diffu:pde1:FE:experiments](#diffu:pde1:FE:experiments) and [diffu:pde1:theta:experiments](#diffu:pde1:theta:experiments)\n", "reveal that there are some\n", "numerical problems with the Forward Euler and Crank-Nicolson schemes:\n", "sawtooth-like noise is sometimes present in solutions that are,\n", "from a mathematical point of view, expected to be smooth.\n", "This section presents a mathematical analysis that explains the\n", "observed behavior and arrives at criteria for obtaining numerical\n", "solutions that reproduce the qualitative properties of the exact\n", "solutions. In short, we shall explain what is observed in\n", "Figures [diffu:pde1:FE:fig:F=0.5](#diffu:pde1:FE:fig:F=0.5)-[diffu:pde1:CN:fig:F=10](#diffu:pde1:CN:fig:F=10).\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "## Properties of the solution\n", "
\n", "\n", "\n", "A particular characteristic of diffusive processes, governed\n", "by an equation like" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u_t = \\dfc u_{xx},\n", "\\label{diffu:pde1:eq} \\tag{1}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "is that the initial shape $u(x,0)=I(x)$ spreads out in space with\n", "time, along with a decaying amplitude. Three different examples will\n", "illustrate the spreading of $u$ in space and the decay in time.\n", "\n", "### Similarity solution\n", "\n", "The diffusion equation ([1](#diffu:pde1:eq)) admits solutions\n", "that depend on $\\eta = (x-c)/\\sqrt{4\\dfc t}$ for a given value\n", "of $c$. One particular solution\n", "is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u(x,t) = a\\,\\mbox{erf}(\\eta) + b,\n", "\\label{diffu:pdf1:erf:sol} \\tag{2}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "\\mbox{erf}(\\eta) = \\frac{2}{\\sqrt{\\pi}}\\int_0^\\eta e^{-\\zeta^2}d\\zeta,\n", "\\label{diffu:analysis:erf:def} \\tag{3}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "is the *error function*, and $a$ and $b$ are arbitrary constants.\n", "The error function lies in $(-1,1)$, is odd around $\\eta =0$, and\n", "goes relatively quickly to $\\pm 1$:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\begin{align*}\n", "\\lim_{\\eta\\rightarrow -\\infty}\\mbox{erf}(\\eta) &=-1,\\\\ \n", "\\lim_{\\eta\\rightarrow \\infty}\\mbox{erf}(\\eta) &=1,\\\\ \n", "\\mbox{erf}(\\eta) &= -\\mbox{erf}(-\\eta),\\\\ \n", "\\mbox{erf}(0) &=0,\\\\ \n", "\\mbox{erf}(2) &=0.99532227,\\\\ \n", "\\mbox{erf}(3) &=0.99997791\n", "\\thinspace .\n", "\\end{align*}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As $t\\rightarrow 0$, the error function approaches a step function centered\n", "at $x=c$. For a diffusion problem posed on the unit interval $[0,1]$,\n", "we may choose the step at $x=1/2$ (meaning $c=1/2$), $a=-1/2$, $b=1/2$.\n", "Then" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u(x,t) = \\frac{1}{2}\\left(1 -\n", "\\mbox{erf}\\left(\\frac{x-\\frac{1}{2}}{\\sqrt{4\\dfc t}}\\right)\\right) =\n", "\\frac{1}{2}\\mbox{erfc}\\left(\\frac{x-\\frac{1}{2}}{\\sqrt{4\\dfc t}}\\right),\n", "\\label{diffu:analysis:pde1:step:erf:sol} \\tag{4}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where we have introduced the *complementary error function*\n", "$\\mbox{erfc}(\\eta) = 1-\\mbox{erf}(\\eta)$.\n", "The solution ([4](#diffu:analysis:pde1:step:erf:sol))\n", "implies the boundary conditions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u(0,t) = \\frac{1}{2}\\left(1 - \\mbox{erf}\\left(\\frac{-1/2}{\\sqrt{4\\dfc t}}\\right)\\right),\n", "\\label{diffu:analysis:pde1:p1:erf:uL} \\tag{5} \n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation} \n", "u(1,t) = \\frac{1}{2}\\left(1 - \\mbox{erf}\\left(\\frac{1/2}{\\sqrt{4\\dfc t}}\\right)\\right)\n", "\\label{diffu:analysis:pde1:p1:erf:uR} \\tag{6}\n", "\\thinspace .\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For small enough $t$, $u(0,t)\\approx 1$ and $u(1,t)\\approx 0$, but as\n", "$t\\rightarrow\\infty$, $u(x,t)\\rightarrow 1/2$ on $[0,1]$.\n", "\n", "### Solution for a Gaussian pulse\n", "\n", "The standard diffusion equation $u_t = \\dfc u_{xx}$ admits a\n", "Gaussian function as solution:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u(x,t) = \\frac{1}{\\sqrt{4\\pi\\dfc t}} \\exp{\\left({-\\frac{(x-c)^2}{4\\dfc t}}\\right)}\n", "\\label{diffu:pde1:sol:Gaussian} \\tag{7}\n", "\\thinspace .\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At $t=0$ this is a Dirac delta function, so for computational\n", "purposes one must start to view the solution at some time $t=t_\\epsilon>0$.\n", "Replacing $t$ by $t_\\epsilon +t$ in ([7](#diffu:pde1:sol:Gaussian))\n", "makes it easy to operate with a (new) $t$ that starts at $t=0$\n", "with an initial condition with a finite width.\n", "The important feature of ([7](#diffu:pde1:sol:Gaussian)) is that\n", "the standard deviation $\\sigma$ of a sharp initial Gaussian pulse\n", "increases in time according to $\\sigma = \\sqrt{2\\dfc t}$, making\n", "the pulse diffuse and flatten out.\n", "\n", "\n", "\n", "\n", "### Solution for a sine component\n", "\n", "Also, ([1](#diffu:pde1:eq)) admits a solution of the form" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u(x,t) = Qe^{-at}\\sin\\left( kx\\right)\n", "\\label{diffu:pde1:sol1} \\tag{8}\n", "\\thinspace .\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The parameters $Q$ and $k$ can be freely chosen, while\n", "inserting ([8](#diffu:pde1:sol1)) in ([1](#diffu:pde1:eq)) gives the constraint" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "a = -\\dfc k^2\n", "\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A very important feature is that the initial shape $I(x)=Q\\sin\\left( kx\\right)$\n", "undergoes a damping $\\exp{(-\\dfc k^2t)}$, meaning that\n", "rapid oscillations in space, corresponding to large $k$, are very much\n", "faster dampened than slow oscillations in space, corresponding to small\n", "$k$. This feature leads to a smoothing of the initial condition with time.\n", "(In fact, one can use a few steps of the diffusion equation as\n", "a method for removing noise in signal processing.)\n", "To judge how good a numerical method is, we may look at its ability to\n", "smoothen or dampen the solution in the same way as the PDE does.\n", "\n", "The following example illustrates the damping properties of\n", "([8](#diffu:pde1:sol1)). We consider the specific problem" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\begin{align*}\n", "u_t &= u_{xx},\\quad x\\in (0,1),\\ t\\in (0,T],\\\\ \n", "u(0,t) &= u(1,t) = 0,\\quad t\\in (0,T],\\\\ \n", "u(x,0) & = \\sin (\\pi x) + 0.1\\sin(100\\pi x)\n", "\\thinspace .\n", "\\end{align*}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The initial condition has been chosen such that adding\n", "two solutions like ([8](#diffu:pde1:sol1)) constructs\n", "an analytical solution to the problem:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u(x,t) = e^{-\\pi^2 t}\\sin (\\pi x) + 0.1e^{-\\pi^2 10^4 t}\\sin (100\\pi x)\n", "\\label{diffu:pde1:sol2} \\tag{9}\n", "\\thinspace .\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Figure](#diffu:pde1:fig:damping) illustrates the rapid damping of\n", "rapid oscillations $\\sin (100\\pi x)$ and the very much slower damping of the\n", "slowly varying $\\sin (\\pi x)$ term. After about $t=0.5\\cdot10^{-4}$ the rapid\n", "oscillations do not have a visible amplitude, while we have to wait\n", "until $t\\sim 0.5$ before the amplitude of the long wave $\\sin (\\pi x)$\n", "becomes very small.\n", "\n", "\n", "\n", "
\n", "\n", "

Evolution of the solution of a diffusion problem: initial condition (upper left), 1/100 reduction of the small waves (upper right), 1/10 reduction of the long wave (lower left), and 1/100 reduction of the long wave (lower right).

\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "## Analysis of discrete equations\n", "\n", "A counterpart to ([8](#diffu:pde1:sol1)) is the complex representation\n", "of the same function:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "u(x,t) = Qe^{-at}e^{ikx},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where $i=\\sqrt{-1}$ is the imaginary unit.\n", "We can add such functions, often referred to as wave components,\n", "to make a Fourier representation\n", "of a general solution of the diffusion equation:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u(x,t) \\approx \\sum_{k\\in K} b_k e^{-\\dfc k^2t}e^{ikx},\n", "\\label{diffu:pde1:u:Fourier} \\tag{10}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where $K$ is a set of an infinite number of $k$ values needed to construct\n", "the solution. In practice, however, the series is truncated and\n", "$K$ is a finite set of $k$ values\n", "needed to build a good approximate solution.\n", "Note that ([9](#diffu:pde1:sol2)) is a special case of\n", "([10](#diffu:pde1:u:Fourier)) where $K=\\{\\pi, 100\\pi\\}$, $b_{\\pi}=1$,\n", "and $b_{100\\pi}=0.1$.\n", "\n", "The amplitudes $b_k$ of the individual Fourier waves must be determined\n", "from the initial condition. At $t=0$ we have $u\\approx\\sum_kb_k\\exp{(ikx)}$\n", "and find $K$ and $b_k$ such that" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "I(x) \\approx \\sum_{k\\in K} b_k e^{ikx}\\thinspace .\n", "\\label{_auto1} \\tag{11}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(The relevant formulas for $b_k$ come from Fourier analysis, or\n", "equivalently, a least-squares method for approximating $I(x)$\n", "in a function space with basis $\\exp{(ikx)}$.)\n", "\n", "Much insight about the behavior of numerical methods can be obtained\n", "by investigating how a wave component $\\exp{(-\\dfc k^2\n", "t)}\\exp{(ikx)}$ is treated by the numerical scheme. mathcal{I}_t appears that\n", "such wave components are also solutions of the schemes, but the\n", "damping factor $\\exp{(-\\dfc k^2 t)}$ varies among the schemes. To\n", "ease the forthcoming algebra, we write the damping factor as\n", "$A^n$. The exact amplification factor corresponding to $A$ is $\\Aex =\n", "\\exp{(-\\dfc k^2\\Delta t)}$.\n", "\n", "\n", "## Analysis of the finite difference schemes\n", "
\n", "\n", "We have seen that a general solution of the diffusion equation\n", "can be built as a linear combination of basic components" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "e^{-\\dfc k^2t}e^{ikx} \\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A fundamental question is whether such components are also solutions of\n", "the finite difference schemes. This is indeed the case, but the\n", "amplitude $\\exp{(-\\dfc k^2t)}$ might be modified (which also happens when\n", "solving the ODE counterpart $u'=-\\dfc u$).\n", "We therefore look for numerical solutions of the form" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u^n_q = A^n e^{ikq\\Delta x} = A^ne^{ikx},\n", "\\label{diffu:pde1:analysis:uni} \\tag{12}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where the amplification factor $A$\n", "must be determined by inserting the component into an actual scheme.\n", "Note that $A^n$ means $A$ raised to the power of $n$, $n$ being the\n", "index in the time mesh, while the superscript $n$ in $u^n_q$ just\n", "denotes $u$ at time $t_n$.\n", "\n", "### Stability\n", "\n", "The exact amplification factor is $\\Aex=\\exp{(-\\dfc^2 k^2\\Delta t)}$.\n", "We should therefore require $|A| < 1$ to have a decaying numerical\n", "solution as well. If\n", "$-1\\leq A<0$, $A^n$ will change sign from time level to\n", "time level, and we get stable, non-physical oscillations in the numerical\n", "solutions that are not present in the exact solution.\n", "\n", "\n", "### Accuracy\n", "\n", "To determine how accurately a finite difference scheme treats one\n", "wave component ([12](#diffu:pde1:analysis:uni)), we see that the basic\n", "deviation from the exact solution is reflected in how well\n", "$A^n$ approximates $\\Aex^n$,\n", "or how well $A$ approximates $\\Aex$.\n", "We can plot $\\Aex$ and the various expressions for $A$, and we can\n", "make Taylor expansions of $A/\\Aex$ to see the error more analytically.\n", "\n", "\n", "\n", "\n", "### Truncation error\n", "\n", "As an alternative to examining the accuracy of the damping of a wave\n", "component, we can perform a general truncation error analysis as\n", "explained in \"Truncation error analysis\": \"\"\n", "[[Langtangen_deqbook_trunc]](#Langtangen_deqbook_trunc). Such results are more general, but\n", "less detailed than what we get from the wave component analysis. The\n", "truncation error can almost always be computed and represents the\n", "error in the numerical model when the exact solution is substituted\n", "into the equations. In particular, the truncation error analysis tells\n", "the order of the scheme, which is of fundamental importance when\n", "verifying codes based on empirical estimation of convergence rates.\n", "\n", "## Analysis of the Forward Euler scheme\n", "
\n", "\n", "\n", "\n", "\n", "\n", "The Forward Euler finite difference scheme for $u_t = \\dfc u_{xx}$ can\n", "be written as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "[D_t^+ u = \\dfc D_xD_x u]^n_q\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Inserting a wave component ([12](#diffu:pde1:analysis:uni))\n", "in the scheme demands calculating the terms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "e^{ikq\\Delta x}[D_t^+ A]^n = e^{ikq\\Delta x}A^n\\frac{A-1}{\\Delta t},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "A^nD_xD_x [e^{ikx}]_q = A^n\\left( - e^{ikq\\Delta x}\\frac{4}{\\Delta x^2}\n", "\\sin^2\\left(\\frac{k\\Delta x}{2}\\right)\\right)\n", "\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Inserting these terms in the discrete equation and\n", "dividing by $A^n e^{ikq\\Delta x}$ leads to" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{A-1}{\\Delta t} = -\\dfc \\frac{4}{\\Delta x^2}\\sin^2\\left(\n", "\\frac{k\\Delta x}{2}\\right),\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and consequently" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "A = 1 -4F\\sin^2 p\n", "\\label{_auto2} \\tag{13}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "F = \\frac{\\dfc\\Delta t}{\\Delta x^2}\n", "\\label{_auto3} \\tag{14}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "is the *numerical Fourier number*, and $p=k\\Delta x/2$.\n", "The complete numerical solution is then" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u^n_q = \\left(1 -4F\\sin^2 p\\right)^ne^{ikq\\Delta x}\n", "\\thinspace .\n", "\\label{_auto4} \\tag{15}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Stability\n", "\n", "We easily see that $A\\leq 1$. However, the $A$ can be less than $-1$,\n", "which will lead\n", "to growth of a numerical wave component. The criterion $A\\geq -1$ implies" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "4F\\sin^2 (p/2)\\leq 2\n", "\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The worst case is when $\\sin^2 (p/2)=1$, so a sufficient criterion for\n", "stability is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "F\\leq {\\frac{1}{2}},\n", "\\label{_auto5} \\tag{16}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or expressed as a condition on $\\Delta t$:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "\\Delta t\\leq \\frac{\\Delta x^2}{2\\dfc}\\thinspace .\n", "\\label{_auto6} \\tag{17}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that halving the spatial mesh size, $\\Delta x \\rightarrow {\\frac{1}{2}}\n", "\\Delta x$, requires $\\Delta t$ to be reduced by a factor of $1/4$.\n", "The method hence becomes very expensive for fine spatial meshes.\n", "\n", "\n", "\n", "### Accuracy\n", "\n", "Since $A$ is expressed in terms of $F$ and the parameter we now call\n", "$p=k\\Delta x/2$, we should also express $\\Aex$ by $F$ and $p$. The exponent\n", "in $\\Aex$ is $-\\dfc k^2\\Delta t$, which equals $-F k^2\\Delta x^2=-F4p^2$.\n", "Consequently," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\Aex = \\exp{(-\\dfc k^2\\Delta t)} = \\exp{(-4Fp^2)}\n", "\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All our $A$ expressions as well as $\\Aex$ are now functions of the two\n", "dimensionless parameters $F$ and $p$.\n", "\n", "Computing\n", "the Taylor series expansion of $A/\\Aex$ in terms of $F$\n", "can easily be done with aid of `sympy`:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def A_exact(F, p):\n", " return exp(-4*F*p**2)\n", "\n", "def A_FE(F, p):\n", " return 1 - 4*F*sin(p)**2\n", "\n", "from sympy import *\n", "F, p = symbols('F p')\n", "A_err_FE = A_FE(F, p)/A_exact(F, p)\n", "print A_err_FE.series(F, 0, 6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{A}{\\Aex} = 1 - 4 F \\sin^{2}p + 2F p^{2} - 16F^{2} p^{2} \\sin^{2}p + 8 F^{2} p^{4} + \\cdots\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recalling that $F=\\dfc\\Delta t/\\Delta x^2$, $p=k\\Delta x/2$, and that\n", "$\\sin^2p\\leq 1$, we\n", "realize that the dominating terms in $A/\\Aex$ are at most" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "1 - 4\\dfc \\frac{\\Delta t}{\\Delta x^2} +\n", "\\dfc\\Delta t - 4\\dfc^2\\Delta t^2\n", "+ \\dfc^2 \\Delta t^2\\Delta x^2 + \\cdots\n", "\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Truncation error\n", "\n", "We follow the theory explained in\n", "\"Truncation error analysis\": \"\"\n", "[[Langtangen_deqbook_trunc]](#Langtangen_deqbook_trunc). The recipe is to set up the\n", "scheme in operator notation and use formulas from\n", "\"Overview of leading-order error terms in finite difference formulas\": \"\"\n", "[[Langtangen_deqbook_trunc]](#Langtangen_deqbook_trunc) to derive an expression for\n", "the residual. The details are documented in\n", "\"Linear diffusion equation in 1D\": \"\"\n", "[[Langtangen_deqbook_trunc]](#Langtangen_deqbook_trunc). We end up with a truncation error" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "R^n_i = \\Oof{\\Delta t} + \\Oof{\\Delta x^2}\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Although this is not the true error $\\uex(x_i,t_n) - u^n_i$, it indicates\n", "that the true error is of the form" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "E = C_t\\Delta t + C_x\\Delta x^2\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "for two unknown constants $C_t$ and $C_x$.\n", "\n", "\n", "## Analysis of the Backward Euler scheme\n", "
\n", "\n", "Discretizing $u_t = \\dfc u_{xx}$ by a Backward Euler scheme," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "[D_t^- u = \\dfc D_xD_x u]^n_q,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and inserting a wave component ([12](#diffu:pde1:analysis:uni)),\n", "leads to calculations similar to those arising from the Forward Euler scheme,\n", "but since" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "e^{ikq\\Delta x}[D_t^- A]^n = A^ne^{ikq\\Delta x}\\frac{1 - A^{-1}}{\\Delta t},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we get" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{1-A^{-1}}{\\Delta t} = -\\dfc \\frac{4}{\\Delta x^2}\\sin^2\\left(\n", "\\frac{k\\Delta x}{2}\\right),\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and then" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "A = \\left(1 + 4F\\sin^2p\\right)^{-1}\n", "\\label{diffu:pde1:analysis:BE:A} \\tag{18}\n", "\\thinspace .\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The complete numerical solution can be written" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u^n_q = \\left(1 + 4F\\sin^2 p\\right)^{-n}\n", "e^{ikq\\Delta x} \\thinspace .\n", "\\label{_auto7} \\tag{19}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Stability\n", "\n", "We see from ([18](#diffu:pde1:analysis:BE:A)) that $00$.\n", "\n", "### Truncation error\n", "\n", "The derivation of the truncation error for the Backward Euler scheme is almost\n", "identical to that for the Forward Euler scheme. We end up with" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "R^n_i = \\Oof{\\Delta t} + \\Oof{\\Delta x^2}\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Analysis of the Crank-Nicolson scheme\n", "
\n", "\n", "The Crank-Nicolson scheme can be written as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "[D_t u = \\dfc D_xD_x \\overline{u}^x]^{n+\\frac{1}{2}}_q,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "[D_t u]^{n+\\frac{1}{2}}_q = \\frac{1}{2}\\dfc\\left( [D_xD_x u]^{n}_q +\n", "[D_xD_x u]^{n+1}_q\\right)\n", "\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Inserting ([12](#diffu:pde1:analysis:uni)) in the time derivative approximation\n", "leads to" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "[D_t A^n e^{ikq\\Delta x}]^{n+\\frac{1}{2}} = A^{n+\\frac{1}{2}} e^{ikq\\Delta x}\\frac{A^{\\frac{1}{2}}-A^{-\\frac{1}{2}}}{\\Delta t} = A^ne^{ikq\\Delta x}\\frac{A-1}{\\Delta t}\n", "\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Inserting ([12](#diffu:pde1:analysis:uni)) in the other terms\n", "and dividing by\n", "$A^ne^{ikq\\Delta x}$ gives the relation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{A-1}{\\Delta t} = -\\frac{1}{2}\\dfc\\frac{4}{\\Delta x^2}\n", "\\sin^2\\left(\\frac{k\\Delta x}{2}\\right)\n", "(1 + A),\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and after some more algebra," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "A = \\frac{ 1 - 2F\\sin^2p}{1 + 2F\\sin^2p}\n", "\\thinspace .\n", "\\label{_auto8} \\tag{20}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The exact numerical solution is hence" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u^n_q = \\left(\\frac{ 1 - 2F\\sin^2p}{1 + 2F\\sin^2p}\\right)^ne^{ikq\\Delta x}\n", "\\thinspace .\n", "\\label{_auto9} \\tag{21}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Stability\n", "\n", "The criteria $A>-1$ and $A<1$ are fulfilled for any $\\Delta t >0$.\n", "Therefore, the solution cannot grow, but it will oscillate if\n", "$1-2F\\sin^p < 0$. To avoid such non-physical oscillations, we must demand\n", "$F\\leq\\frac{1}{2}$.\n", "\n", "### Truncation error\n", "\n", "The truncation error is derived in\n", "\"Linear diffusion equation in 1D\": \"\"\n", "[[Langtangen_deqbook_trunc]](#Langtangen_deqbook_trunc):" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "R^{n+\\frac{1}{2}}_i = \\Oof{\\Delta x^2} + \\Oof{\\Delta t^2}\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Analysis of the Leapfrog scheme\n", "
\n", "\n", "\n", "An attractive feature of the Forward Euler scheme is the explicit\n", "time stepping and no need for solving linear systems. However, the\n", "accuracy in time is only $\\Oof{\\Delta t}$. We can get an explicit\n", "*second-order* scheme in time by using the Leapfrog method:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "[D_{2t} u = \\dfc D_xDx u + f]^n_q\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Written out," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "u_q^{n+1} = u_q^{n-1} + \\frac{2\\dfc\\Delta t}{\\Delta x^2}\n", "(u^{n}_{q+1} - 2u^n_q + u^n_{q-1}) + f(x_q,t_n)\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We need some formula for the first step, $u^1_q$, but for that we can use\n", "a Forward Euler step.\n", "\n", "Unfortunately, the Leapfrog scheme is always unstable for the\n", "diffusion equation. To see this, we insert a wave component $A^ne^{ikx}$\n", "and get" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{A - A^{-1}}{\\Delta t} = -\\dfc \\frac{4}{\\Delta x^2}\\sin^2 p,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "A^2 + 4F \\sin^2 p\\, A - 1 = 0,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which has roots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "A = -2F\\sin^2 p \\pm \\sqrt{4F^2\\sin^4 p + 1}\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Both roots have $|A|>1$ so the amplitude always grows, which is not in\n", "accordance with the physics of the problem.\n", "However, for a PDE with a first-order derivative in space, instead of\n", "a second-order one, the Leapfrog scheme performs very well.\n", "\n", "## Summary of accuracy of amplification factors\n", "\n", "We can plot the various amplification factors against $p=k\\Delta x/2$\n", "for different choices of the $F$ parameter. Figures\n", "[diffu:pde1:fig:A:err:C20](#diffu:pde1:fig:A:err:C20), [diffu:pde1:fig:A:err:C0.5](#diffu:pde1:fig:A:err:C0.5), and\n", "[diffu:pde1:fig:A:err:C0.1](#diffu:pde1:fig:A:err:C0.1) show how long and small waves are\n", "damped by the various schemes compared to the exact damping. As long\n", "as all schemes are stable, the amplification factor is positive,\n", "except for Crank-Nicolson when $F>0.5$.\n", "\n", "\n", "\n", "
\n", "\n", "

Amplification factors for large time steps.

\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "

Amplification factors for time steps around the Forward Euler stability limit.

\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "

Amplification factors for small time steps.

\n", "\n", "\n", "\n", "\n", "\n", "The effect of negative amplification factors is that $A^n$ changes\n", "sign from one time level to the next, thereby giving rise to\n", "oscillations in time in an animation of the solution. We see from\n", "[Figure](#diffu:pde1:fig:A:err:C20) that for $F=20$, waves with\n", "$p\\geq \\pi/4$ undergo a damping close to $-1$, which means that the\n", "amplitude does not decay and that the wave component jumps up and down\n", "(flips amplitude) in time. For $F=2$ we have a damping of a factor of\n", "0.5 from one time level to the next, which is very much smaller than\n", "the exact damping. Short waves will therefore fail to be effectively\n", "dampened. These waves will manifest themselves as high frequency\n", "oscillatory noise in the solution.\n", "\n", "A value $p=\\pi/4$ corresponds to four mesh points per wave length of\n", "$e^{ikx}$, while $p=\\pi/2$ implies only two points per wave length,\n", "which is the smallest number of points we can have to represent the\n", "wave on the mesh.\n", "\n", "To demonstrate the oscillatory behavior of the Crank-Nicolson scheme,\n", "we choose an initial condition that leads to short waves with\n", "significant amplitude. A discontinuous $I(x)$ will in particular serve\n", "this purpose: Figures [diffu:pde1:CN:fig:F=3](#diffu:pde1:CN:fig:F=3) and\n", "[diffu:pde1:CN:fig:F=10](#diffu:pde1:CN:fig:F=10) correspond to $F=3$ and $F=10$,\n", "respectively, and we see how short waves pollute the overall solution.\n", "\n", "## Analysis of the 2D diffusion equation\n", "
\n", "\n", "Diffusion in several dimensions is treated later, but it is appropriate to\n", "include the analysis here. We first consider the 2D diffusion equation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "u_{t} = \\dfc(u_{xx} + u_{yy}),\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which has Fourier component solutions of the form" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "u(x,y,t) = Ae^{-\\dfc k^2t}e^{i(k_x x + k_yy)},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and the schemes have discrete versions of this Fourier component:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "u^{n}_{q,r} = A\\xi^{n}e^{i(k_x q\\Delta x + k_y r\\Delta y)}\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The Forward Euler scheme\n", "\n", "For the Forward Euler discretization," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "[D_t^+u = \\dfc(D_xD_x u + D_yD_y u)]_{q,r}^n,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we get" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{\\xi - 1}{\\Delta t}\n", "=\n", "-\\dfc\\frac{4}{\\Delta x^2}\\sin^2\\left(\\frac{k_x\\Delta x}{2}\\right) -\n", "\\dfc\\frac{4}{\\Delta y^2}\\sin^2\\left(\\frac{k_y\\Delta y}{2}\\right)\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Introducing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "p_x = \\frac{k_x\\Delta x}{2},\\quad p_y = \\frac{k_y\\Delta y}{2},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we can write the equation for $\\xi$ more compactly as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{\\xi - 1}{\\Delta t}\n", "=\n", "-\\dfc\\frac{4}{\\Delta x^2}\\sin^2 p_x -\n", "\\dfc\\frac{4}{\\Delta y^2}\\sin^2 p_y,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and solve for $\\xi$:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "\\xi = 1 - 4F_x\\sin^2 p_x - 4F_y\\sin^2 p_y\\thinspace .\n", "\\label{diffu:2D:analysis:xi} \\tag{22}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The complete numerical solution for a wave component is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u^{n}_{q,r} = A(1 - 4F_x\\sin^2 p_x - 4F_y\\sin^2 p_y)^n\n", "e^{i(k_xq\\Delta x + k_yr\\Delta y)}\\thinspace .\n", "\\label{diffu:2D:analysis:FE:numexact} \\tag{23}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For stability we demand $-1\\leq\\xi\\leq 1$, and $-1\\leq\\xi$ is the\n", "critical limit, since clearly $\\xi \\leq 1$, and the worst case\n", "happens when the sines are at their maximum. The stability criterion\n", "becomes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "F_x + F_y \\leq \\frac{1}{2}\\thinspace .\n", "\\label{diffu:2D:analysis:FE:stab} \\tag{24}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the special, yet common, case $\\Delta x=\\Delta y=h$, the\n", "stability criterion can be written as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\Delta t \\leq \\frac{h^2}{2d\\dfc},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where $d$ is the number of space dimensions: $d=1,2,3$.\n", "\n", "### The Backward Euler scheme\n", "\n", "The Backward Euler method," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "[D_t^-u = \\dfc(D_xD_x u + D_yD_y u)]_{q,r}^n,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "results in" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "1 - \\xi^{-1} = - 4F_x \\sin^2 p_x - 4F_y \\sin^2 p_y,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\xi = (1 + 4F_x \\sin^2 p_x + 4F_y \\sin^2 p_y)^{-1},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which is always in $(0,1]$. The solution for a wave component becomes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u^{n}_{q,r} = A(1 + 4F_x\\sin^2 p_x + 4F_y\\sin^2 p_y)^{-n}\n", "e^{i(k_xq\\Delta x + k_yr\\Delta y)}\\thinspace .\n", "\\label{diffu:2D:analysis:BN:numexact} \\tag{25}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The Crank-Nicolson scheme\n", "\n", "With a Crank-Nicolson discretization," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "[D_tu]^{n+\\frac{1}{2}}_{q,r} =\n", "\\frac{1}{2} [\\dfc(D_xD_x u + D_yD_y u)]_{q,r}^{n+1} +\n", "\\frac{1}{2} [\\dfc(D_xD_x u + D_yD_y u)]_{q,r}^n,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we have, after some algebra," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\xi = \\frac{1 - 2(F_x\\sin^2 p_x + F_x\\sin^2p_y)}{1 + 2(F_x\\sin^2 p_x + F_x\\sin^2p_y)}\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The fraction on the right-hand side is always less than 1, so stability\n", "in the sense of non-growing wave components is guaranteed for all\n", "physical and numerical parameters. However,\n", "the fraction can become negative and result in non-physical\n", "oscillations. This phenomenon happens when" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "F_x\\sin^2 p_x + F_x\\sin^2p_y > \\frac{1}{2}\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A criterion against non-physical oscillations is therefore" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "F_x + F_y \\leq \\frac{1}{2},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which is the same limit as the stability criterion for the Forward Euler\n", "scheme.\n", "\n", "The exact discrete solution is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "u^{n}_{q,r} = A\n", "\\left(\n", "\\frac{1 - 2(F_x\\sin^2 p_x + F_x\\sin^2p_y)}{1 + 2(F_x\\sin^2 p_x + F_x\\sin^2p_y)}\n", "\\right)^n\n", "e^{i(k_xq\\Delta x + k_yr\\Delta y)}\\thinspace .\n", "\\label{diffu:2D:analysis:CN:numexact} \\tag{26}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Explanation of numerical artifacts\n", "\n", "\n", "The behavior of the solution generated by Forward Euler discretization in time (and centered\n", "differences in space) is summarized at the end of\n", "the section [diffu:pde1:FE:experiments](#diffu:pde1:FE:experiments). Can we, from the analysis\n", "above, explain the behavior?\n", "\n", "We may start by looking at [Figure](#diffu:pde1:FE:fig:F=0.51)\n", "where $F=0.51$. The figure shows that the solution is unstable and\n", "grows in time. The stability limit for such growth is $F=0.5$ and\n", "since the $F$ in this simulation is slightly larger, growth is\n", "unavoidable.\n", "\n", "[Figure](#diffu:pde1:FE:fig:F=0.5) has unexpected features:\n", "we would expect the solution of the diffusion equation to be\n", "smooth, but the graphs in [Figure](#diffu:pde1:FE:fig:F=0.5)\n", "contain non-smooth noise. Turning to [Figure](#diffu:pde1:FE:fig:gauss:F=0.5), which has a quite similar\n", "initial condition, we see that the curves are indeed smooth.\n", "The problem with the results in [Figure](#diffu:pde1:FE:fig:F=0.5)\n", "is that the initial condition is discontinuous. To represent it, we\n", "need a significant amplitude on the shortest waves in the mesh.\n", "However, for $F=0.5$, the shortest wave ($p=\\pi/2$) gives\n", "the amplitude in the numerical solution as $(1-4F)^n$, which oscillates\n", "between negative and positive values at subsequent time levels\n", "for $F>\\frac{1}{4}$. Since the shortest waves have visible amplitudes in\n", "the solution profile, the oscillations becomes visible. The\n", "smooth initial condition in [Figure](#diffu:pde1:FE:fig:gauss:F=0.5),\n", "on the other hand, leads to very small amplitudes of the shortest waves.\n", "That these waves then oscillate in a non-physical way for\n", "$F=0.5$ is not a visible effect. The oscillations\n", "in time in the amplitude $(1-4F)^n$ disappear for $F\\leq\\frac{1}{4}$,\n", "and that is why also the discontinuous initial condition always leads to\n", "smooth solutions in [Figure](#diffu:pde1:FE:fig:F=0.25), where\n", "$F=\\frac{1}{4}$.\n", "\n", "Turning the attention to the Backward Euler scheme and the experiments\n", "in [Figure](#diffu:pde1:BE:fig:F=0.5), we see that even the discontinuous\n", "initial condition gives smooth solutions for $F=0.5$ (and in fact all other\n", "$F$ values). From the exact expression of the numerical amplitude,\n", "$(1 + 4F\\sin^2p)^{-1}$, we realize that this factor can never flip between\n", "positive and negative values, and no instabilities can occur. The conclusion\n", "is that the Backward Euler scheme always produces smooth solutions.\n", "Also, the Backward Euler scheme guarantees that the solution cannot grow\n", "in time (unless we add a source term to the PDE, but that is meant to\n", "represent a physically relevant growth).\n", "\n", "Finally, we have some small, strange artifacts when simulating the\n", "development of the initial plug profile with the Crank-Nicolson scheme,\n", "see [Figure](#diffu:pde1:CN:fig:F=10), where $F=3$.\n", "The Crank-Nicolson scheme cannot give growing amplitudes, but it may\n", "give oscillating amplitudes in time. The critical factor is\n", "$1 - 2F\\sin^2p$, which for the shortest waves ($p=\\pi/2$) indicates\n", "a stability limit $F=0.5$. With the discontinuous initial condition, we have\n", "enough amplitude on the shortest waves so their wrong behavior is visible,\n", "and this is what we see as small instabilities in\n", "[Figure](#diffu:pde1:CN:fig:F=10). The only remedy is to lower the $F$ value.\n", "\n", "\n", "\n", "\n", "# Exercises\n", "\n", "\n", "\n", "\n", "\n", "## Exercise 1: Explore symmetry in a 1D problem\n", "
\n", "\n", "This exercise simulates the exact solution ([7](#diffu:pde1:sol:Gaussian)).\n", "Suppose for simplicity that $c=0$.\n", "\n", "\n", "**a)**\n", "Formulate an initial-boundary value problem that has\n", "([7](#diffu:pde1:sol:Gaussian)) as solution in the domain $[-L,L]$.\n", "Use the exact solution ([7](#diffu:pde1:sol:Gaussian)) as Dirichlet\n", "condition at the boundaries.\n", "Simulate the diffusion of the Gaussian peak. Observe that the\n", "solution is symmetric around $x=0$.\n", "\n", "**b)**\n", "Show from ([7](#diffu:pde1:sol:Gaussian)) that $u_x(c,t)=0$.\n", "Since the solution is symmetric around $x=c=0$, we can solve the\n", "numerical problem in frac{1}{2} of the domain, using a *symmetry boundary condition*\n", "$u_x=0$ at $x=0$. Set up the\n", "initial-boundary value problem in this case. Simulate the\n", "diffusion problem in $[0,L]$ and compare with the solution in a).\n", "\n", "\n", "\n", "**Solution.**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\begin{align*}\n", "u_t &= \\dfc u_xx,\\\\ \n", "u_x(0,t) &= 0,\\\\ \n", "u(L,t)& =\\frac{1}{\\sqrt{4\\pi\\dfc t}} \\exp{\\left({-\\frac{x^2}{4\\dfc t}}\\right)}\\thinspace .\n", "\\end{align*}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "Filename: `diffu_symmetric_gaussian`.\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "## Exercise 2: Investigate approximation errors from a $u_x=0$ boundary condition\n", "
\n", "\n", "We consider the problem solved in [Exercise 1: Explore symmetry in a 1D problem](#diffu:exer:1D:gaussian:symmetric)\n", "part b). The boundary condition $u_x(0,t)=0$ can be implemented in\n", "two ways: 1) by a standard symmetric finite difference $[D_{2x}u]_i^n=0$,\n", "or 2) by a one-sided difference $[D^+u=0]^n_i=0$.\n", "Investigate the effect of these two conditions on the\n", "convergence rate in space.\n", "\n", "\n", "\n", "**Hint.**\n", "If you use a Forward Euler scheme, choose a discretization parameter\n", "$h=\\Delta t = \\Delta x^2$ and assume the error goes like $E\\sim h^r$.\n", "The error in the scheme is $\\Oof{\\Delta t,\\Delta x^2}$ so one should\n", "expect that the estimated $r$ approaches 1. The question is if\n", "a one-sided difference approximation to $u_x(0,t)=0$ destroys this\n", "convergence rate.\n", "\n", "\n", "Filename: `diffu_onesided_fd`.\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "## Exercise 3: Experiment with open boundary conditions in 1D\n", "
\n", "\n", "We address diffusion of a Gaussian function\n", "as in [Exercise 1: Explore symmetry in a 1D problem](#diffu:exer:1D:gaussian:symmetric),\n", "in the domain $[0,L]$,\n", "but now we shall explore different types of boundary\n", "conditions on $x=L$. In real-life problems we do not know\n", "the exact solution on $x=L$ and must use something simpler.\n", "\n", "\n", "**a)**\n", "Imagine that we want to solve the problem numerically on\n", "$[0,L]$, with a symmetry boundary condition $u_x=0$ at $x=0$,\n", "but we do not know the exact solution and cannot of that\n", "reason assign a correct Dirichlet condition at $x=L$.\n", "One idea is to simply set $u(L,t)=0$ since this will be an\n", "accurate approximation before the diffused pulse reaches $x=L$\n", "and even thereafter it might be a satisfactory condition if the exact $u$ has\n", "a small value.\n", "Let $\\uex$ be the exact solution and let $u$ be the solution\n", "of $u_t=\\dfc u_{xx}$ with an initial Gaussian pulse and\n", "the boundary conditions $u_x(0,t)=u(L,t)=0$. Derive a diffusion\n", "problem for the error $e=\\uex - u$. Solve this problem\n", "numerically using an exact Dirichlet condition at $x=L$.\n", "Animate the evolution of the error and make a curve plot of\n", "the error measure" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "E(t)=\\sqrt{\\frac{\\int_0^L e^2dx}{\\int_0^L udx}}\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Is this a suitable error measure for the present problem?\n", "\n", "**b)**\n", "Instead of using $u(L,t)=0$ as approximate boundary condition for\n", "letting the diffused Gaussian pulse move out of our finite domain,\n", "one may try $u_x(L,t)=0$ since the solution for large $t$ is\n", "quite flat. Argue that this condition gives a completely wrong\n", "asymptotic solution as $t\\rightarrow 0$. To do this,\n", "integrate the diffusion equation from $0$ to $L$, integrate\n", "$u_{xx}$ by parts (or use Gauss' divergence theorem in 1D) to\n", "arrive at the important property" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{d}{dt}\\int_{0}^L u(x,t)dx = 0,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "implying that $\\int_0^Ludx$ must be constant in time, and therefore" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\int_{0}^L u(x,t)dx = \\int_{0}^LI(x)dx\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The integral of the initial pulse is 1.\n", "\n", "**c)**\n", "Another idea for an artificial boundary condition at $x=L$\n", "is to use a cooling law" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "-\\dfc u_x = q(u - u_S),\n", "\\label{diffu:pde1:Gaussian:xL:cooling} \\tag{27}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where $q$ is an unknown heat transfer coefficient and $u_S$ is\n", "the surrounding temperature in the medium outside of $[0,L]$.\n", "(Note that arguing that $u_S$ is approximately $u(L,t)$ gives\n", "the $u_x=0$ condition from the previous subexercise that is\n", "qualitatively wrong for large $t$.)\n", "Develop a diffusion problem for the error in the solution using\n", "([27](#diffu:pde1:Gaussian:xL:cooling)) as boundary condition.\n", "Assume one can take $u_S=0$ \"outside the domain\" since\n", "$\\uex\\rightarrow 0$ as $x\\rightarrow\\infty$.\n", "Find a function $q=q(t)$ such that the exact solution\n", "obeys the condition ([27](#diffu:pde1:Gaussian:xL:cooling)).\n", "Test some constant values of $q$ and animate how the corresponding\n", "error function behaves. Also compute $E(t)$ curves as defined above.\n", "\n", "\n", "Filename: `diffu_open_BC`.\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "## Exercise 4: Simulate a diffused Gaussian peak in 2D/3D\n", "\n", "\n", "**a)**\n", "Generalize ([7](#diffu:pde1:sol:Gaussian)) to multi dimensions by\n", "assuming that one-dimensional solutions can be multiplied to solve\n", "$u_t = \\dfc\\nabla^2 u$. Set $c=0$ such that the peak of\n", "the Gaussian is at the origin.\n", "\n", "**b)**\n", "One can from the exact solution show\n", "that $u_x=0$ on $x=0$, $u_y=0$ on $y=0$, and $u_z=0$ on $z=0$.\n", "The approximately correct condition $u=0$ can be set\n", "on the remaining boundaries (say $x=L$, $y=L$, $z=L$), cf. [Exercise 3: Experiment with open boundary conditions in 1D](#diffu:exer:1D:openBC).\n", "Simulate a 2D case and make an animation of the diffused Gaussian peak.\n", "\n", "**c)**\n", "The formulation in b) makes use of symmetry of the solution such that we\n", "can solve the problem in the first quadrant (2D) or octant (3D) only.\n", "To check that the symmetry assumption is correct, formulate the problem\n", "without symmetry in a domain $[-L,L]\\times [L,L]$ in 2D. Use $u=0$ as\n", "approximately correct boundary condition. Simulate the same case as\n", "in b), but in a four times as large domain. Make an animation and compare\n", "it with the one in b).\n", "\n", "Filename: `diffu_symmetric_gaussian_2D`.\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "## Exercise 5: Examine stability of a diffusion model with a source term\n", "
\n", "\n", "Consider a diffusion equation with a linear $u$ term:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "u_t = \\dfc u_{xx} + \\beta u\\thinspace .\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**a)**\n", "Derive in detail the Forward Euler, Backward Euler,\n", "and Crank-Nicolson schemes for this type of diffusion model.\n", "Thereafter, formulate a $\\theta$-rule to summarize the three schemes.\n", "\n", "**b)**\n", "Assume a solution like ([8](#diffu:pde1:sol1)) and find the relation\n", "between $a$, $k$, $\\dfc$, and $\\beta$.\n", "\n", "\n", "\n", "**Hint.**\n", "Insert ([8](#diffu:pde1:sol1)) in the PDE problem.\n", "\n", "\n", "\n", "**c)**\n", "Calculate the stability of the Forward Euler scheme. Design\n", "numerical experiments to confirm the results.\n", "\n", "\n", "\n", "**Hint.**\n", "Insert the discrete counterpart to ([8](#diffu:pde1:sol1)) in the\n", "numerical scheme. Run experiments at the stability limit and slightly above.\n", "\n", "\n", "\n", "**d)**\n", "Repeat c) for the Backward Euler scheme.\n", "\n", "**e)**\n", "Repeat c) for the Crank-Nicolson scheme.\n", "\n", "**f)**\n", "How does the extra term $bu$ impact the accuracy of the three schemes?\n", "\n", "\n", "\n", "**Hint.**\n", "For analysis of the accuracy,\n", "compare the numerical and exact amplification factors, in\n", "graphs and/or by Taylor series expansion.\n", "\n", "\n", "\n", "Filename: `diffu_stability_uterm`.\n", "\n", "" ] } ], "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }