|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "a13bc8d5-3247-4792-931b-cca7574d1e34", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "import numpy as np\n", |
| 11 | + "import matplotlib.pyplot as plt\n", |
| 12 | + "from matplotlib.animation import FuncAnimation\n", |
| 13 | + "from IPython.display import HTML\n", |
| 14 | + "from tqdm.notebook import trange, tqdm\n", |
| 15 | + "import firedrake\n", |
| 16 | + "from firedrake import (\n", |
| 17 | + " Constant, inner, min_value, max_value, jump, avg, grad, dx, ds, dS\n", |
| 18 | + ")\n", |
| 19 | + "import irksome\n", |
| 20 | + "from irksome import Dt" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": null, |
| 26 | + "id": "0b47295b-abd4-43cf-9e4c-618646526567", |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "nx, ny = 32, 32\n", |
| 31 | + "mesh = firedrake.UnitSquareMesh(nx, ny, diagonal=\"crossed\")\n", |
| 32 | + "δ = 1.0 / nx" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": null, |
| 38 | + "id": "b9ac67fe-69cf-4cd4-8b3c-76b1bb7bcf19", |
| 39 | + "metadata": {}, |
| 40 | + "outputs": [], |
| 41 | + "source": [ |
| 42 | + "degree = 0\n", |
| 43 | + "element = firedrake.FiniteElement(\"DG\", \"triangle\", degree)\n", |
| 44 | + "Q = firedrake.FunctionSpace(mesh, element)" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": null, |
| 50 | + "id": "1afae3de-d7b9-49a1-9868-a4768f72d164", |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "u_max = 1.0\n", |
| 55 | + "u = firedrake.Constant((u_max, 0.0))" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": null, |
| 61 | + "id": "36de950f-a4bf-4e0e-9b47-cae2a23fbd51", |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "h_in = Constant(1.0)\n", |
| 66 | + "x = firedrake.SpatialCoordinate(mesh)\n", |
| 67 | + "L = Constant(0.25)\n", |
| 68 | + "expr = firedrake.conditional(x[0] < L, 1.0, 0.0)\n", |
| 69 | + "h_0 = firedrake.Function(Q).project(expr)\n", |
| 70 | + "h = h_0.copy(deepcopy=True)" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "code", |
| 75 | + "execution_count": null, |
| 76 | + "id": "de1cf5af-901c-4a6b-b537-9c070d337edd", |
| 77 | + "metadata": {}, |
| 78 | + "outputs": [], |
| 79 | + "source": [ |
| 80 | + "ϕ = firedrake.TestFunction(Q)\n", |
| 81 | + "F_cells = (Dt(h) * ϕ - inner(h * u, grad(ϕ))) * dx\n", |
| 82 | + "\n", |
| 83 | + "ν = firedrake.FacetNormal(mesh)\n", |
| 84 | + "f = max_value(0, h * inner(u, ν))\n", |
| 85 | + "F_facets = jump(f) * jump(ϕ) * dS\n", |
| 86 | + "\n", |
| 87 | + "F_inflow = h_in * ϕ * min_value(0, inner(u, ν)) * ds\n", |
| 88 | + "F_outflow = h * ϕ * max_value(0, inner(u, ν)) * ds\n", |
| 89 | + "\n", |
| 90 | + "F = F_cells + F_facets + F_inflow + F_outflow" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": null, |
| 96 | + "id": "9a458b42-4fd6-4e55-b0ce-83ebb0e8b99f", |
| 97 | + "metadata": {}, |
| 98 | + "outputs": [], |
| 99 | + "source": [ |
| 100 | + "method = irksome.BackwardEuler()\n", |
| 101 | + "t = Constant(0.0)\n", |
| 102 | + "dt = Constant(0.5 * δ / u_max)\n", |
| 103 | + "\n", |
| 104 | + "params = {\n", |
| 105 | + " \"solver_parameters\": {\n", |
| 106 | + " \"snes_type\": \"newtonls\",\n", |
| 107 | + " \"ksp_type\": \"gmres\",\n", |
| 108 | + " \"pc_type\": \"lu\",\n", |
| 109 | + " \"pc_factor_mat_solver_type\": \"mumps\",\n", |
| 110 | + " },\n", |
| 111 | + "}\n", |
| 112 | + "\n", |
| 113 | + "solver = irksome.TimeStepper(F, method, t, dt, h, **params)" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "code", |
| 118 | + "execution_count": null, |
| 119 | + "id": "c34b4498-b788-4c07-a3f0-5c3a1aa58dba", |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "hs = [h.copy(deepcopy=True)]\n", |
| 124 | + "\n", |
| 125 | + "final_time = 1.0\n", |
| 126 | + "num_steps = int(final_time / float(dt))\n", |
| 127 | + "for step in trange(num_steps):\n", |
| 128 | + " solver.advance()\n", |
| 129 | + " hs.append(h.copy(deepcopy=True))" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "code", |
| 134 | + "execution_count": null, |
| 135 | + "id": "a3a9e4fb-fa7d-4afc-ac4b-06d4e93c66d6", |
| 136 | + "metadata": {}, |
| 137 | + "outputs": [], |
| 138 | + "source": [ |
| 139 | + "%%capture\n", |
| 140 | + "\n", |
| 141 | + "fig, ax = plt.subplots()\n", |
| 142 | + "ax.set_aspect(\"equal\")\n", |
| 143 | + "colors = firedrake.tripcolor(\n", |
| 144 | + " hs[0], axes=ax, num_sample_points=1, shading=\"gouraud\"\n", |
| 145 | + ");\n", |
| 146 | + "\n", |
| 147 | + "fn_plotter = firedrake.FunctionPlotter(mesh, num_sample_points=1)\n", |
| 148 | + "def animate(h):\n", |
| 149 | + " colors.set_array(fn_plotter(h))" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "code", |
| 154 | + "execution_count": null, |
| 155 | + "id": "1816a106-b3e7-4112-ac60-f073ea8d9120", |
| 156 | + "metadata": {}, |
| 157 | + "outputs": [], |
| 158 | + "source": [ |
| 159 | + "animation = FuncAnimation(fig, animate, tqdm(hs), interval=1e3/10)" |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "code", |
| 164 | + "execution_count": null, |
| 165 | + "id": "446a0a61-0164-4367-b425-437b8f1bcabe", |
| 166 | + "metadata": {}, |
| 167 | + "outputs": [], |
| 168 | + "source": [ |
| 169 | + "HTML(animation.to_html5_video())" |
| 170 | + ] |
| 171 | + } |
| 172 | + ], |
| 173 | + "metadata": { |
| 174 | + "kernelspec": { |
| 175 | + "display_name": "firedrake", |
| 176 | + "language": "python", |
| 177 | + "name": "firedrake" |
| 178 | + }, |
| 179 | + "language_info": { |
| 180 | + "codemirror_mode": { |
| 181 | + "name": "ipython", |
| 182 | + "version": 3 |
| 183 | + }, |
| 184 | + "file_extension": ".py", |
| 185 | + "mimetype": "text/x-python", |
| 186 | + "name": "python", |
| 187 | + "nbconvert_exporter": "python", |
| 188 | + "pygments_lexer": "ipython3", |
| 189 | + "version": "3.11.9" |
| 190 | + } |
| 191 | + }, |
| 192 | + "nbformat": 4, |
| 193 | + "nbformat_minor": 5 |
| 194 | +} |
0 commit comments