-
Notifications
You must be signed in to change notification settings - Fork 0
/
advection.py
99 lines (82 loc) · 2.61 KB
/
advection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 20 15:30:22 2022
@author: cpf5546
"""
import numpy as np
import dedalus.public as d3
import matplotlib.pyplot as plt
from dedalus_sdc import ERK4
from dedalus_sdc import SpectralDeferredCorrectionIMEX
# Bases and field
coords = d3.CartesianCoordinates('x')
dist = d3.Distributor(coords, dtype=np.float64)
xbasis = d3.RealFourier(coords['x'], size=16, bounds=(0, 2*np.pi))
u = dist.Field(name='u', bases=xbasis)
# Initial solution
x = xbasis.local_grid()
listK = [0, 1, 2]
u0 = np.sum([np.cos(k*x) for k in listK], axis=0)
np.copyto(u['g'], u0)
plt.figure('Initial solution')
plt.plot(u['g'], label='Real space')
plt.plot(u['c'], 'o', label='Coefficient space')
plt.legend()
plt.grid()
# Problem
dx = lambda f: d3.Differentiate(f, coords['x'])
problem = d3.IVP([u], namespace=locals())
problem.add_equation("dt(u) + dx(u) = 0")
# Prepare plots
orderPlot = {'RK111': 1,
'RK222': 2,
'RK443': 3,
'ERK4': 4}
plt.figure('Error')
SpectralDeferredCorrectionIMEX.setParameters(
M=3, quadType='RADAU-RIGHT', nodeDistr='LEGENDRE',
implSweep='OPT-QmQd-0', explSweep='PIC', initSweep='COPY',
forceProl=True)
for timeStepper in [d3.RK111, ERK4, 1, 2]:
# For integer number, use SDC with given number of sweeps
useSDC = False
nSweep = None
if isinstance(timeStepper, int):
# Using SDC with a given number of sweeps
nSweep = timeStepper
timeStepper = SpectralDeferredCorrectionIMEX
timeStepper.nSweep = nSweep
useSDC = True
# Build solver
solver = problem.build_solver(timeStepper)
solver.stop_sim_time = 2*np.pi
name = timeStepper.__name__
# Function to run the simulation with one given time step
def getErr(nStep):
np.copyto(u['g'], u0)
solver.sim_time = 0
dt = 2*np.pi/nStep
for _ in range(nStep):
solver.step(dt)
err = np.linalg.norm(u0-u['g'], ord=np.inf)
return dt, err
# Run all simulations
listNumStep = [2**(i+2) for i in range(11)]
dt, err = np.array([getErr(n) for n in listNumStep]).T
# Plot error VS time step
lbl = f'SDC, nSweep={nSweep}' if useSDC else name
sym = '^-' if useSDC else 'o-'
plt.loglog(dt, err, sym, label=lbl)
# Eventually plot order curve
if name in orderPlot:
order = orderPlot[name]
c = err[-1]/dt[-1]**order * 2
plt.plot(dt, c*dt**order, '--', color='gray')
plt.xlabel(r'$\Delta{t}$')
plt.ylabel(r'error ($L_{inf}$)')
plt.ylim(1e-9, 1e2)
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.savefig("adv_accuracy.pd")