|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# SUPG Advection Example: Dispersion and Phase Speed Diagrams" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": null, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "# dependencies\n", |
| 17 | + "using Pkg\n", |
| 18 | + "Pkg.activate(\".\")\n", |
| 19 | + "Pkg.instantiate()\n", |
| 20 | + "Pkg.develop(path=\"../..\")\n", |
| 21 | + "using Plots\n", |
| 22 | + "using LFAToolkit\n", |
| 23 | + "using LinearAlgebra " |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "# setup\n", |
| 33 | + "p = 2\n", |
| 34 | + "q = p\n", |
| 35 | + "dimension = 1\n", |
| 36 | + "mapping = nothing\n", |
| 37 | + "collocate = false\n", |
| 38 | + "basis = TensorH1LagrangeBasis(p, q, 1, 1, collocatedquadrature = collocate, mapping = mapping)\n", |
| 39 | + "\n", |
| 40 | + "mesh = []\n", |
| 41 | + "if dimension == 1\n", |
| 42 | + " mesh = Mesh1D(1.0)\n", |
| 43 | + "elseif dimension == 2\n", |
| 44 | + " mesh = Mesh2D(1.0, 1.0)\n", |
| 45 | + "end\n", |
| 46 | + "\n", |
| 47 | + "# advective speed\n", |
| 48 | + "c = 1.0\n", |
| 49 | + "# Tau scaling for SUPG, 0 returns Galerkin method\n", |
| 50 | + "τ = 0.5 / (p - 1)" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": null, |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [], |
| 58 | + "source": [ |
| 59 | + "# SUPG advection weakform \n", |
| 60 | + "function supgadvectionweakform(U::Matrix{Float64}, w::Array{Float64})\n", |
| 61 | + " u = U[1, :]\n", |
| 62 | + " du = U[2, :]\n", |
| 63 | + " dv = (c * u - c * τ * (c * du)) * w[1]\n", |
| 64 | + " return [dv]\n", |
| 65 | + "end\n", |
| 66 | + "\n", |
| 67 | + "# SUPG advection operator\n", |
| 68 | + "inputs = [\n", |
| 69 | + " OperatorField(\n", |
| 70 | + " basis,\n", |
| 71 | + " [EvaluationMode.interpolation, EvaluationMode.gradient],\n", |
| 72 | + " \"advected field\",\n", |
| 73 | + " ),\n", |
| 74 | + " OperatorField(basis, [EvaluationMode.quadratureweights], \"quadrature weights\"),\n", |
| 75 | + "]\n", |
| 76 | + "outputs = [OperatorField(basis, [EvaluationMode.gradient])]\n", |
| 77 | + "supgadvection = Operator(supgadvectionweakform, mesh, inputs, outputs)\n", |
| 78 | + "\n", |
| 79 | + "# SUPG mass weakform and mass operator\n", |
| 80 | + "function supgmassweakform(udot::Array{Float64}, w::Array{Float64})\n", |
| 81 | + " v = udot * w[1]\n", |
| 82 | + " dv = c * τ * udot * w[1]\n", |
| 83 | + " return ([v; dv],)\n", |
| 84 | + "end\n", |
| 85 | + "supgmass = Operator(\n", |
| 86 | + " supgmassweakform,\n", |
| 87 | + " mesh,\n", |
| 88 | + " [\n", |
| 89 | + " OperatorField(basis, [EvaluationMode.interpolation], \"u_t\"),\n", |
| 90 | + " OperatorField(basis, [EvaluationMode.quadratureweights], \"quadrature weights\"),\n", |
| 91 | + " ],\n", |
| 92 | + " [OperatorField(basis, [EvaluationMode.interpolation, EvaluationMode.gradient])],\n", |
| 93 | + ")" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "code", |
| 98 | + "execution_count": null, |
| 99 | + "metadata": {}, |
| 100 | + "outputs": [], |
| 101 | + "source": [ |
| 102 | + "# compute full operator symbols\n", |
| 103 | + "numbersteps = 100\n", |
| 104 | + "maxeigenvalue = 0\n", |
| 105 | + "θ_min = 0\n", |
| 106 | + "θ_max = (p-1) * π\n", |
| 107 | + "θ_range = LinRange(θ_min, θ_max, numbersteps)\n", |
| 108 | + "\n", |
| 109 | + "# compute and plot dispersion and phase speed\n", |
| 110 | + "# -- 1D --\n", |
| 111 | + "function advection_supg_symbol(θ_range)\n", |
| 112 | + " A = computesymbols(supgadvection, [θ_range]) * 2 # transform from reference to physical on dx=1 grid\n", |
| 113 | + " M = computesymbols(supgmass, [θ_range]) # mass matrix\n", |
| 114 | + " return sort(imag.(eigvals(-M \\ A)))\n", |
| 115 | + "end\n", |
| 116 | + "\n", |
| 117 | + "eigenvalues = hcat(advection_supg_symbol.(θ_range)...)'\n", |
| 118 | + "\n", |
| 119 | + "plot1 = plot(θ_range / π, eigenvalues ./ π, linewidth = 3) # Dispersion diagram\n", |
| 120 | + " plot!(\n", |
| 121 | + " identity,\n", |
| 122 | + " xlabel = \"θ/π\",\n", |
| 123 | + " ylabel = \"Eigenvalues\",\n", |
| 124 | + " label = \"exact\",\n", |
| 125 | + " legend = :none,\n", |
| 126 | + " color = :black,\n", |
| 127 | + " title = \"Dispersion relation, p=$p, collocate=$collocate, τ=$τ\",\n", |
| 128 | + ")\n", |
| 129 | + "plot2 = plot(θ_range / π, eigenvalues ./ θ_range, linewidth = 3) # Phase speed diagram\n", |
| 130 | + " plot!(\n", |
| 131 | + " one,\n", |
| 132 | + " xlabel = \"θ/π\",\n", |
| 133 | + " ylabel = \"Eigvalues/θ\",\n", |
| 134 | + " legend = :none,\n", |
| 135 | + " color = :black,\n", |
| 136 | + " title = \"Phase speed, p=$p, collocate=$collocate, τ=$τ\",\n", |
| 137 | + ")\n", |
| 138 | + "plot!(plot1, plot2, layout = (1, 2), size = (1050, 450))" |
| 139 | + ] |
| 140 | + } |
| 141 | + ], |
| 142 | + "metadata": { |
| 143 | + "kernelspec": { |
| 144 | + "display_name": "Julia 1.7.0", |
| 145 | + "language": "julia", |
| 146 | + "name": "julia-1.7" |
| 147 | + }, |
| 148 | + "language_info": { |
| 149 | + "file_extension": ".jl", |
| 150 | + "mimetype": "application/julia", |
| 151 | + "name": "julia", |
| 152 | + "version": "1.7.0" |
| 153 | + }, |
| 154 | + "orig_nbformat": 4 |
| 155 | + }, |
| 156 | + "nbformat": 4, |
| 157 | + "nbformat_minor": 2 |
| 158 | +} |
0 commit comments