Advection of a Gaussian in an interval

Advection of a Gaussian in an interval#

\[\begin{split} \mathbb{S} \begin{cases} \Omega = [0, L_x] \\ u_0(x)=\exp\left(-\frac{(x - x_0)^2}{\sigma^2}\right) \\ u_{\text{I}}(x=0) = 1 \\ \textbf{a}(t)=(1 + \epsilon\sqrt{t})\textbf{e}_x \\ \end{cases} \end{split}\]
import numpy as np
from lucifex.mesh import interval_mesh
from lucifex.fem import Constant
from lucifex.fdm import (
    FiniteDifference, FiniteDifferenceArgwise, FunctionSeries, ConstantSeries, 
    BE, FE, CN, finite_difference_order, advective_timestep,
)
from lucifex.solver import ibvp, evaluation , BoundaryConditions
from lucifex.sim import Simulation, run
from lucifex.viz import plot_line, save_figure
from lucifex.utils import nested_dict
from lucifex.pde.advection import advection


def velocity(
    t: Constant | float,
    eps: float,
) -> tuple[float]:
    return (1.0 + eps * np.sqrt(float(t)), )


def create_simulation(
    Lx: float,
    Nx: int,
    dt: float,
    D_adv: FiniteDifference | FiniteDifferenceArgwise,
    x0: float,
    sigma: float,
    eps: float,
) -> Simulation:
    order = finite_difference_order(D_adv)
    mesh = interval_mesh(Lx, Nx)
    t = ConstantSeries(mesh, name='t', ics=0.0)
    dt = Constant(mesh, dt, name='dt')
    a = ConstantSeries(mesh, name='a', order=order, shape=(1,), store=1, ics=velocity(0.0, eps))
    a_solver = evaluation(a, velocity, future=True)(t[1], eps)
    u = FunctionSeries((mesh, 'P', 1), name='u', order=order, store=1)
    u_ics = lambda x: np.exp(-(x[0] - x0)**2 / sigma**2)
    u_bcs = BoundaryConditions(('dirichlet', lambda x: x[0], 0.0))
    u_solver = ibvp(advection, u_ics, u_bcs)(u, dt, a, D_adv)
    solvers = [a_solver, u_solver]
    return Simulation(solvers, t, dt)


Lx = 1.0
Nx = 250
x0 = 0.2 * Lx
sigma = 0.001 ** 0.5
eps = 0.01

dt_opts = (0.005, 0.001)
D_adv_opts = (FE, BE, FE @ BE, CN)
simulations = nested_dict((float, FiniteDifference, Simulation))

for dt in dt_opts:
    for D_adv in D_adv_opts:
        simulations[dt][D_adv] = create_simulation(Lx, Nx, dt, D_adv, x0, sigma, eps)

t_stop = 1.0
n_stop = 200
for dt in dt_opts:
    for D_adv in D_adv_opts:
        run(simulations[dt][D_adv], n_stop, t_stop) 
time_slice = slice(0, None, 10)

for dt in dt_opts:
    h = Lx/ Nx
    dt_advective = advective_timestep(velocity(0.0, eps), h)
    courant = dt / dt_advective
    for D_adv in D_adv_opts:
        u = simulations[dt][D_adv]['u']
        legend_labels=(min(u.time_series[time_slice]), max(u.time_series[time_slice]))
        tex = str(D_adv).replace('◦', '\circ ')
        title = f"$\mathcal{{D}}_{{\mathbf{{a}}, u}}=\mathrm{{{tex}}}$\n $C_{{\mathbf{{a}}}}={courant}$"
        fig, ax = plot_line(
            u.series[time_slice], 
            legend_labels, '$t$', cyc='jet', x_label='$x$', y_label='$u$', title=title)
        save_figure(f'{D_adv}', thumbnail=(D_adv is CN))(fig)

a = simulations[dt_opts[0]][D_adv_opts[0]]['a']
a_scalar = [i[0] for i in a.value_series]
fig, ax = plot_line((a.time_series, a_scalar), x_label='$t$', y_label=f'${a.name}(t)$')
save_figure('a(t)')(fig)
../../_images/77a535ecf457a02f7807e0be905bd0811f5eaaa5b5c3e952e225d675f3064de5.png ../../_images/09c95e8e8b9666a2a9bdbaa68e991206c556e09ee2afecd5c9c81c4cfbe7530a.png ../../_images/9ecd0e754266d654a67bdb672c043a0c472a1a09fff0869ca0b56bee8e52640e.png ../../_images/5c9923d7245bedcb6e460f050a5cfaef225c18a276171d0b27dcf8e275e83d24.png ../../_images/27212b31384b891b9d9b81f976bec2e39d40fa8f66d5299cc9e4ca0161627eb6.png ../../_images/9db0a25b8879990cbc2eabcd1374fbd1dd34c7f44238b15b6b06f6f9e1efe49f.png ../../_images/52be9a3b50a9c0941205d941a57ce3267146d65b28a44bae357da619ef9e0b3a.png ../../_images/5d89ebceef3ac64ca45943e264423e6a3a14e45a36d66add178b93cb7a00898a.png ../../_images/89615b15b7dbd035cf2e0b50693a42eb2ff7f2e38b967be3287a07485b3ef110.png
The Kernel crashed while executing code in the current cell or a previous cell. 

Please review the code in the cell(s) to identify a possible cause of the failure. 

Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. 

View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.