|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "b5e0474c", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Elliptic equation in the general form\n", |
| 9 | + "\n", |
| 10 | + "We consider here, the following general form of an elliptic partial differential equation,\n", |
| 11 | + "\n", |
| 12 | + "$$\n", |
| 13 | + "\\begin{align}\n", |
| 14 | + "- \\nabla \\cdot \\left( A \\nabla u \\right) + \\mathbf{b} \\cdot \\nabla u + c u &= f, \\quad \\Omega \\\\\n", |
| 15 | + "u &= 0, \\quad \\partial \\Omega\n", |
| 16 | + "\\end{align}\n", |
| 17 | + "$$\n", |
| 18 | + "\n", |
| 19 | + "## Variational Formulation\n", |
| 20 | + "\n", |
| 21 | + "An $H^1$-conforming variational formulation of reads\n", |
| 22 | + "\n", |
| 23 | + "$$\n", |
| 24 | + "\\begin{align}\n", |
| 25 | + " \\text{find $u \\in V$ such that} \\quad a(u,v) = l(v) \\quad \\forall v \\in V,\n", |
| 26 | + "\\end{align}\n", |
| 27 | + "$$\n", |
| 28 | + "\n", |
| 29 | + "where \n", |
| 30 | + "\n", |
| 31 | + "- $V \\subset H^1(\\Omega)$, \n", |
| 32 | + "- $a(u,v) := \\int_{\\Omega} \\left( \\left( A \\nabla u \\right) \\cdot \\nabla v + \\left( \\mathbf{b} \\cdot \\nabla u \\right) v + cuv \\right) ~ d\\Omega$,\n", |
| 33 | + "- $l(v) := \\int_{\\Omega} f v ~ d\\Omega$.\n" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "id": "c5c944a1", |
| 39 | + "metadata": {}, |
| 40 | + "source": [ |
| 41 | + "## Formal Model" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "code", |
| 46 | + "execution_count": 10, |
| 47 | + "id": "c2cee2d9", |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "from sympde.expr import BilinearForm, LinearForm, integral\n", |
| 52 | + "from sympde.expr import find, EssentialBC, Norm, SemiNorm\n", |
| 53 | + "from sympde.topology import ScalarFunctionSpace, Square, element_of\n", |
| 54 | + "from sympde.calculus import grad, dot, div\n", |
| 55 | + "from sympde.core import Vector, Matrix\n", |
| 56 | + "\n", |
| 57 | + "from sympy import pi, sin\n", |
| 58 | + "\n", |
| 59 | + "from psydac.api.discretization import discretize\n", |
| 60 | + "\n", |
| 61 | + "domain = Square()\n", |
| 62 | + "\n", |
| 63 | + "V = ScalarFunctionSpace('V', domain)\n", |
| 64 | + "\n", |
| 65 | + "x,y = domain.coordinates\n", |
| 66 | + "\n", |
| 67 | + "u,v = [element_of(V, name=i) for i in ['u', 'v']]\n", |
| 68 | + "\n", |
| 69 | + "c = x*y\n", |
| 70 | + "b = Vector([1e-2, 1e-1], name='b')\n", |
| 71 | + "A = Matrix([[1,1], [0,1]], name='A')\n", |
| 72 | + "\n", |
| 73 | + "# bilinear form\n", |
| 74 | + "expr = dot(grad(v), A * grad(u)) + dot(b, grad(u))*v + c*u*v\n", |
| 75 | + "a = BilinearForm((u,v), integral(domain, expr))" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "markdown", |
| 80 | + "id": "366c2d67", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "### Manifactured solution" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": 11, |
| 89 | + "id": "2b05c79f", |
| 90 | + "metadata": {}, |
| 91 | + "outputs": [], |
| 92 | + "source": [ |
| 93 | + "# the analytical solution and its rhs\n", |
| 94 | + "ue = sin(pi * x) * sin(pi * y)\n", |
| 95 | + "\n", |
| 96 | + "L = lambda u: - div(A*grad(u)) + dot(b,grad(u)) + c*u\n", |
| 97 | + "f = L(ue)" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "markdown", |
| 102 | + "id": "66b50430", |
| 103 | + "metadata": {}, |
| 104 | + "source": [ |
| 105 | + "### Formal Equation" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "code", |
| 110 | + "execution_count": 13, |
| 111 | + "id": "d0d386a2", |
| 112 | + "metadata": {}, |
| 113 | + "outputs": [], |
| 114 | + "source": [ |
| 115 | + "l = LinearForm(v, integral(domain, f*v))\n", |
| 116 | + "\n", |
| 117 | + "# Dirichlet boundary conditions\n", |
| 118 | + "bc = [EssentialBC(u, 0, domain.boundary)]\n", |
| 119 | + "\n", |
| 120 | + "# Variational problem\n", |
| 121 | + "equation = find(u, forall=v, lhs=a(u, v), rhs=l(v), bc=bc)" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "markdown", |
| 126 | + "id": "0614c740", |
| 127 | + "metadata": {}, |
| 128 | + "source": [ |
| 129 | + "## Discretization" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "code", |
| 134 | + "execution_count": 14, |
| 135 | + "id": "96c29a27", |
| 136 | + "metadata": {}, |
| 137 | + "outputs": [], |
| 138 | + "source": [ |
| 139 | + "degree = [2,2]\n", |
| 140 | + "ncells = [8,8]" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": 15, |
| 146 | + "id": "ef6a9709", |
| 147 | + "metadata": {}, |
| 148 | + "outputs": [ |
| 149 | + { |
| 150 | + "name": "stderr", |
| 151 | + "output_type": "stream", |
| 152 | + "text": [ |
| 153 | + "/home/ranania/PYCCEL/IGA-Python/.iga-python/lib/python3.10/site-packages/sympy/matrices/repmatrix.py:90: SymPyDeprecationWarning: \n", |
| 154 | + "\n", |
| 155 | + "non-Expr objects in a Matrix has been deprecated since SymPy 1.9. Use\n", |
| 156 | + "list of lists, TableForm or some other data structure instead. See\n", |
| 157 | + "https://github.com/sympy/sympy/issues/21497 for more info.\n", |
| 158 | + "\n", |
| 159 | + " ).warn()\n" |
| 160 | + ] |
| 161 | + } |
| 162 | + ], |
| 163 | + "source": [ |
| 164 | + "# Create computational domain from topological domain\n", |
| 165 | + "domain_h = discretize(domain, ncells=ncells, comm=None)\n", |
| 166 | + "\n", |
| 167 | + "# Create discrete spline space\n", |
| 168 | + "Vh = discretize(V, domain_h, degree=degree)\n", |
| 169 | + "\n", |
| 170 | + "# Discretize equation\n", |
| 171 | + "equation_h = discretize(equation, domain_h, [Vh, Vh])" |
| 172 | + ] |
| 173 | + }, |
| 174 | + { |
| 175 | + "cell_type": "markdown", |
| 176 | + "id": "c286e572", |
| 177 | + "metadata": {}, |
| 178 | + "source": [ |
| 179 | + "### Solving the PDE" |
| 180 | + ] |
| 181 | + }, |
| 182 | + { |
| 183 | + "cell_type": "code", |
| 184 | + "execution_count": 16, |
| 185 | + "id": "3bafe9f5", |
| 186 | + "metadata": {}, |
| 187 | + "outputs": [], |
| 188 | + "source": [ |
| 189 | + "equation_h.set_solver('gmres', info=False, tol=1e-8)" |
| 190 | + ] |
| 191 | + }, |
| 192 | + { |
| 193 | + "cell_type": "code", |
| 194 | + "execution_count": 17, |
| 195 | + "id": "b88daf50", |
| 196 | + "metadata": {}, |
| 197 | + "outputs": [], |
| 198 | + "source": [ |
| 199 | + "uh = equation_h.solve()" |
| 200 | + ] |
| 201 | + }, |
| 202 | + { |
| 203 | + "cell_type": "markdown", |
| 204 | + "id": "d865a17f", |
| 205 | + "metadata": {}, |
| 206 | + "source": [ |
| 207 | + "## Computing the error norm" |
| 208 | + ] |
| 209 | + }, |
| 210 | + { |
| 211 | + "cell_type": "markdown", |
| 212 | + "id": "be88e26c", |
| 213 | + "metadata": {}, |
| 214 | + "source": [ |
| 215 | + "### Computing the $L^2$ norm" |
| 216 | + ] |
| 217 | + }, |
| 218 | + { |
| 219 | + "cell_type": "code", |
| 220 | + "execution_count": 18, |
| 221 | + "id": "3440e74a", |
| 222 | + "metadata": {}, |
| 223 | + "outputs": [ |
| 224 | + { |
| 225 | + "name": "stdout", |
| 226 | + "output_type": "stream", |
| 227 | + "text": [ |
| 228 | + "0.00021948770944141364\n" |
| 229 | + ] |
| 230 | + } |
| 231 | + ], |
| 232 | + "source": [ |
| 233 | + "u = element_of(V, name='u')\n", |
| 234 | + "\n", |
| 235 | + "# create the formal Norm object\n", |
| 236 | + "l2norm = Norm(u - ue, domain, kind='l2')\n", |
| 237 | + "\n", |
| 238 | + "# discretize the norm\n", |
| 239 | + "l2norm_h = discretize(l2norm, domain_h, Vh)\n", |
| 240 | + "\n", |
| 241 | + "# assemble the norm\n", |
| 242 | + "l2_error = l2norm_h.assemble(u=uh)\n", |
| 243 | + "\n", |
| 244 | + "# print the result\n", |
| 245 | + "print(l2_error)" |
| 246 | + ] |
| 247 | + }, |
| 248 | + { |
| 249 | + "cell_type": "code", |
| 250 | + "execution_count": null, |
| 251 | + "id": "5d7d9180", |
| 252 | + "metadata": {}, |
| 253 | + "outputs": [], |
| 254 | + "source": [] |
| 255 | + } |
| 256 | + ], |
| 257 | + "metadata": { |
| 258 | + "kernelspec": { |
| 259 | + "display_name": ".iga-python", |
| 260 | + "language": "python", |
| 261 | + "name": ".iga-python" |
| 262 | + }, |
| 263 | + "language_info": { |
| 264 | + "codemirror_mode": { |
| 265 | + "name": "ipython", |
| 266 | + "version": 3 |
| 267 | + }, |
| 268 | + "file_extension": ".py", |
| 269 | + "mimetype": "text/x-python", |
| 270 | + "name": "python", |
| 271 | + "nbconvert_exporter": "python", |
| 272 | + "pygments_lexer": "ipython3", |
| 273 | + "version": "3.10.12" |
| 274 | + } |
| 275 | + }, |
| 276 | + "nbformat": 4, |
| 277 | + "nbformat_minor": 5 |
| 278 | +} |
0 commit comments