|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Script investigating IMEX stability for SDC methods for 3D advection-diffusion |
| 5 | +""" |
| 6 | +import numpy as np |
| 7 | +import matplotlib.pyplot as plt |
| 8 | + |
| 9 | +from qmat.qcoeff.collocation import Collocation |
| 10 | +from qmat.qdelta import QDELTA_GENERATORS |
| 11 | + |
| 12 | +from qmat.solvers.dahlquist import DahlquistIMEX |
| 13 | + |
| 14 | +# ----------------------------------------------------------------------------- |
| 15 | +# Script parameters |
| 16 | +# ----------------------------------------------------------------------------- |
| 17 | +# -- SDC |
| 18 | +nNodes = 4 |
| 19 | +nSweeps = 4 |
| 20 | +stepUpdate = False |
| 21 | +implSweep = "LU" |
| 22 | +explSweep = "FE" |
| 23 | +nodeType="LEGENDRE" |
| 24 | +quadType="RADAU-RIGHT" |
| 25 | + |
| 26 | +# -- space discretization |
| 27 | +dim = 3 |
| 28 | +advOrder = 4 |
| 29 | +diffOrder = 4 |
| 30 | + |
| 31 | +# -- plot options |
| 32 | +cflAdvMax = 10 |
| 33 | +nSamplesCFL = 301 |
| 34 | +nWavenumbers = 16 |
| 35 | + |
| 36 | +# ----------------------------------------------------------------------------- |
| 37 | +# Script execution |
| 38 | +# ----------------------------------------------------------------------------- |
| 39 | +ADV_COEFFS = { |
| 40 | + 1: ([0, 1, -1], 1), |
| 41 | + 2: ([1, 0, -1], 2), |
| 42 | + 3: ([0, 2, 3, -6, 1], 6), |
| 43 | + 4: ([-1, 8, 0, -8, 1], 12), |
| 44 | + 5: ([0, -3, 30, 20, -60, 15, -2], 60), |
| 45 | + 6: ([1, -9, 45, 0, -45, 9, -1], 60) |
| 46 | + } |
| 47 | + |
| 48 | +DIFF_COEFFS = { |
| 49 | + 2: ([1, -2, 1], 1), |
| 50 | + 4: ([-1, 16, -30, 16, -1], 12), |
| 51 | + 6: ([2, -27, 270, -490, 270, -27, 2], 180), |
| 52 | + } |
| 53 | + |
| 54 | +if advOrder == np.inf: |
| 55 | + def zAdv(theta): |
| 56 | + return 1j*theta |
| 57 | +else: |
| 58 | + def zAdv(theta): |
| 59 | + coeffs, div = ADV_COEFFS[advOrder] |
| 60 | + s = len(coeffs) |
| 61 | + exponents = range((s//2), -(s//2)-1, -1) |
| 62 | + symbol = sum(c*np.exp(1j*theta*e) for c, e in zip(coeffs, exponents)) |
| 63 | + symbol /= div |
| 64 | + return symbol |
| 65 | + |
| 66 | + |
| 67 | +if diffOrder == np.inf: |
| 68 | + def zDiff(theta): |
| 69 | + return -theta**2 |
| 70 | +else: |
| 71 | + def zDiff(theta): |
| 72 | + coeffs, div = DIFF_COEFFS[diffOrder] |
| 73 | + s = len(coeffs) |
| 74 | + exponents = range((s//2), -(s//2)-1, -1) |
| 75 | + symbol = sum(c*np.exp(1j*theta*e) for c, e in zip(coeffs, exponents)) |
| 76 | + symbol /= div |
| 77 | + return symbol |
| 78 | + |
| 79 | + |
| 80 | +theta = np.linspace(0, np.pi, num=nWavenumbers) |
| 81 | +cflAdv = np.linspace(0, cflAdvMax, num=nSamplesCFL) |
| 82 | +cflRatio = np.logspace(-2, 1, num=nSamplesCFL) |
| 83 | + |
| 84 | +zE = -cflAdv[:, None, None]*zAdv(theta)[None, None, :] |
| 85 | +zI = cflRatio[None, :, None]*cflAdv[:, None, None]*zDiff(theta)[None, None, :] |
| 86 | +zI = (1+0j)*zI |
| 87 | + |
| 88 | +zE *= dim |
| 89 | +zI *= dim |
| 90 | + |
| 91 | +print(f"Computing one SDC time-step on {zI.size} points ...") |
| 92 | +problem = DahlquistIMEX(zI, zE) |
| 93 | + |
| 94 | +coll = Collocation(nNodes=nNodes, nodeType=nodeType, quadType=quadType) |
| 95 | + |
| 96 | +genI = QDELTA_GENERATORS[implSweep](qGen=coll) |
| 97 | +genE = QDELTA_GENERATORS[explSweep](qGen=coll) |
| 98 | + |
| 99 | +sweeps = [k+1 for k in range(nSweeps)] |
| 100 | + |
| 101 | +uNum = problem.solveSDC( |
| 102 | + coll.Q, coll.weights if stepUpdate else None, |
| 103 | + genI.genCoeffs(k=sweeps), genE.genCoeffs(k=sweeps), |
| 104 | + nSweeps=nSweeps) |
| 105 | +print(" -- done !") |
| 106 | + |
| 107 | + |
| 108 | +u1 = uNum[-1] |
| 109 | +stab = np.abs(u1).max(axis=-1) |
| 110 | +stab = np.clip(stab, 0, 1.2) # clip to ignore unstable area |
| 111 | +# error = np.abs(u1 - np.exp(zI+zE)) |
| 112 | + |
| 113 | + |
| 114 | +plt.figure("imex stability") |
| 115 | +plt.clf() |
| 116 | + |
| 117 | +coords = (cflRatio, cflAdv) |
| 118 | +plt.contourf(*coords, stab, levels = [0.8, 1, 1.2], cmap="coolwarm") |
| 119 | +plt.colorbar() |
| 120 | +plt.contour(*coords, stab, levels=[1], colors="black") |
| 121 | +# plt.contour(*coords, error, levels=[1], colors="red", linestyles=":") |
| 122 | +# plt.contour(*coords, error, levels=[1e-1], colors="orange", linestyles="-.") |
| 123 | +# plt.contour(*coords, error, levels=[1e-2], colors="gray", linestyles="--") |
| 124 | +plt.grid(True) |
| 125 | +plt.xscale('log') |
| 126 | +plt.ylabel(r"$\sigma_{adv}$", fontsize=12) |
| 127 | +plt.xlabel(r"$\frac{\sigma_{diff}}{\sigma_{adv}}$", fontsize=18) |
| 128 | +plt.tight_layout() |
| 129 | +plt.savefig("imexStabilityAdvDiff.pdf") |
0 commit comments