Skip to content

Commit e930b97

Browse files
committed
TL: first major rework of dedalus playground
1 parent ef774f3 commit e930b97

File tree

12 files changed

+290
-237
lines changed

12 files changed

+290
-237
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#

pySDC/playgrounds/dedalus/advection.py

Lines changed: 0 additions & 95 deletions
This file was deleted.

pySDC/playgrounds/dedalus/demo.py

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Quick demo to solve the 1D advection-diffusion with Dedalus
5+
using the pySDC interface
6+
"""
7+
# Base user imports
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
import dedalus.public as d3
11+
12+
# pySDC imports
13+
from pySDC.playgrounds.dedalus.interface.problem import DedalusProblem
14+
from pySDC.playgrounds.dedalus.interface.sweeper import DedalusSweeperIMEX
15+
from pySDC.implementations.controller_classes.controller_nonMPI import controller_nonMPI
16+
17+
# -----------------------------------------------------------------------------
18+
# User parameters
19+
# -----------------------------------------------------------------------------
20+
listK = [0, 1, 2] # list of initial wavenumber in the solution (amplitude 1)
21+
nu = 1e-2 # viscosity (unitary velocity)
22+
23+
# -- Space discretisation
24+
xEnd = 2*np.pi # domain [0, xEnd]
25+
nX = 16 # number of points in x (periodic domain)
26+
27+
# -- Time integration
28+
nSweeps = 4 # number of SDC iterations (sweeps)
29+
nNodes = 4 # number of SDC quadrature nodes
30+
tEnd = 2*np.pi # final simulation time (starts at 0)
31+
nSteps = 50 # number of time-steps
32+
33+
# -----------------------------------------------------------------------------
34+
# Solver setup
35+
# -----------------------------------------------------------------------------
36+
37+
# -- Dedalus space grid
38+
coords = d3.CartesianCoordinates('x')
39+
dist = d3.Distributor(coords, dtype=np.float64)
40+
xbasis = d3.RealFourier(coords['x'], size=nX, bounds=(0, xEnd))
41+
u = dist.Field(name='u', bases=xbasis)
42+
43+
# -- Initial solution
44+
x = xbasis.local_grid(dist, scale=1)
45+
u0 = np.sum([np.cos(k*x) for k in listK], axis=0)
46+
np.copyto(u['g'], u0)
47+
u0 = u.copy() # store initial field for later
48+
49+
# -- Problem
50+
dx = lambda f: d3.Differentiate(f, coords['x'])
51+
problem = d3.IVP([u], namespace=locals())
52+
problem.add_equation("dt(u) + dx(u) - nu*dx(dx(u)) = 0")
53+
54+
nSweeps = 4
55+
nNodes = 4
56+
tEnd = 2*np.pi
57+
nSteps = 100
58+
dt = tEnd / nSteps
59+
60+
# -- pySDC controller settings
61+
description = {
62+
# Sweeper and its parameters
63+
"sweeper_class": DedalusSweeperIMEX,
64+
"sweeper_params": {
65+
"quad_type": "RADAU-RIGHT",
66+
"num_nodes": nNodes,
67+
"node_type": "LEGENDRE",
68+
"initial_guess": "copy",
69+
"do_coll_update": False,
70+
"QI": "IE",
71+
"QE": "EE",
72+
'skip_residual_computation':
73+
('IT_CHECK', 'IT_DOWN', 'IT_UP', 'IT_FINE', 'IT_COARSE'),
74+
},
75+
# Step parameters
76+
"step_params": {
77+
"maxiter": 1,
78+
},
79+
# Level parameters
80+
"level_params": {
81+
"dt": dt,
82+
"restol": -1,
83+
"nsweeps": nSweeps,
84+
},
85+
"problem_class": DedalusProblem,
86+
"problem_params": {
87+
'problem': problem,
88+
'nNodes': nNodes,
89+
}
90+
}
91+
92+
controller = controller_nonMPI(
93+
num_procs=1, controller_params={'logger_level': 30},
94+
description=description)
95+
96+
prob = controller.MS[0].levels[0].prob
97+
uSol = prob.solver.state
98+
tVals = np.linspace(0, tEnd, nSteps + 1)
99+
100+
for i in range(nSteps):
101+
uSol, _ = controller.run(u0=uSol, t0=tVals[i], Tend=tVals[i + 1])
102+
103+
# -- Plotting solution in real and Fourier space
104+
fig, (ax1, ax2) = plt.subplots(1, 2)
105+
fig.suptitle(rf"Advection-diffusion with $\nu={nu:1.1e}$")
106+
107+
plt.sca(ax1)
108+
plt.title("Real space")
109+
plt.plot(u0['g'], label='$u_0$')
110+
plt.plot(u['g'], label='$u(T)$')
111+
plt.legend()
112+
plt.grid(True)
113+
plt.xlabel("$x$")
114+
plt.ylabel("$u$")
115+
116+
plt.sca(ax2)
117+
plt.title("Coefficient space")
118+
plt.plot(u0['c'], 'o', mfc="none", label='$u_0$')
119+
plt.plot(u['c'], 's', mfc="none", ms=10, label='$u(t)$')
120+
plt.legend()
121+
plt.grid(True)
122+
plt.xlabel(r"$\kappa$")
123+
plt.ylabel("$u$")
124+
125+
fig.set_size_inches(12, 5)
126+
plt.tight_layout()

pySDC/playgrounds/dedalus/burger.py renamed to pySDC/playgrounds/dedalus/demo_interface_burger.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@
1010
import logging
1111
logger = logging.getLogger(__name__)
1212

13-
from problem import DedalusProblem
14-
from sweeper import DedalusSweeperIMEX
13+
from pySDC.playgrounds.dedalus.interface.problem import DedalusProblem
14+
from pySDC.playgrounds.dedalus.interface.sweeper import DedalusSweeperIMEX
1515
from pySDC.implementations.controller_classes.controller_nonMPI import controller_nonMPI
1616

17-
18-
# Parameters
17+
# Space parameters
1918
Lx = 10
20-
Nx = 1024
19+
Nx = 512
2120
a = 1e-4
2221
b = 2e-4
2322
dealias = 3/2
2423
dtype = np.float64
2524

25+
# Time-integration parameters
26+
nSweeps = 4
27+
nNodes = 4
2628
tEnd = 10
27-
nSteps = 10000
28-
29+
nSteps = 5000
30+
timeStep = tEnd / nSteps
2931

3032
# Bases
3133
xcoord = d3.Coordinate('x')
@@ -47,11 +49,6 @@
4749
n = 20
4850
u['g'] = np.log(1 + np.cosh(n)**2/np.cosh(n*(x-0.2*Lx))**2) / (2*n)
4951

50-
# pySDC parameters
51-
dt = tEnd / nSteps
52-
nSweeps = 1
53-
nNodes = 4
54-
5552
description = {
5653
# Sweeper and its parameters
5754
"sweeper_class": DedalusSweeperIMEX,
@@ -61,8 +58,8 @@
6158
"node_type": "LEGENDRE",
6259
"initial_guess": "copy",
6360
"do_coll_update": False,
64-
"QI": "IE",
65-
"QE": "EE",
61+
"QI": "MIN-SR-S",
62+
"QE": "PIC",
6663
'skip_residual_computation':
6764
('IT_CHECK', 'IT_DOWN', 'IT_UP', 'IT_FINE', 'IT_COARSE'),
6865
},
@@ -72,7 +69,7 @@
7269
},
7370
# Level parameters
7471
"level_params": {
75-
"dt": dt,
72+
"dt": timeStep,
7673
"restol": -1,
7774
"nsweeps": nSweeps,
7875
},
@@ -104,7 +101,6 @@
104101
if (i+1) % 100 == 0:
105102
print(f"step {i+1}/{nSteps}")
106103
if (i+1) % 25 == 0:
107-
108104
u.change_scales(1)
109105
u_list.append(np.copy(u['g']))
110106
t_list.append(tVals[i])
@@ -121,4 +117,4 @@
121117
plt.ylabel('t')
122118
plt.title(f'KdV-Burgers, (a,b)=({a},{b})')
123119
plt.tight_layout()
124-
plt.savefig("KdV_Burgers_pySDC.pdf")
120+
plt.savefig("KdV_Burgers_interface.pdf")

0 commit comments

Comments
 (0)