Skip to content

Commit 54e1964

Browse files
committed
Clean up examples
hg hash: a3fc57f974d248125c0dd7b478f99bd3be536d30
1 parent 2e518aa commit 54e1964

File tree

12 files changed

+86
-209
lines changed

12 files changed

+86
-209
lines changed

examples/bvp/1d_lane_emden/lane_emden.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252

5353
# Setup problem
5454
problem = de.NLBVP(domain, variables=['f', 'fx', 'R'], ncc_cutoff=ncc_cutoff)
55-
problem.meta[:]['x']['dirichlet'] = True
5655
problem.parameters['n'] = n
5756
problem.add_equation("x*dx(fx) + 2*fx = -x*(R**2)*(f**n)", tau=False)
5857
problem.add_equation("fx - dx(f) = 0")

examples/evp/1d_rayleigh_benard/rayleigh_benard.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
# 2D Boussinesq hydrodynamics, with no-slip boundary conditions
3535
# Use substitutions for x and t derivatives
3636
problem = de.EVP(domain, variables=['p','b','u','w','bz','uz','wz'], eigenvalue='omega')
37-
problem.meta[:]['z']['dirichlet'] = True
3837
problem.parameters['P'] = (Rayleigh * Prandtl)**(-1/2)
3938
problem.parameters['R'] = (Rayleigh / Prandtl)**(-1/2)
4039
problem.parameters['F'] = F = 1

examples/evp/1d_waves_on_string/waves_on_a_string.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
# Problem
3131
problem = de.EVP(domain, variables=['u', 'u_x'],eigenvalue='l')
32-
problem.meta[:]['x']['dirichlet'] = True
3332
problem.add_equation("l*u + dx(u_x) = 0")
3433
problem.add_equation("u_x - dx(u) = 0")
3534
problem.add_bc("left(u) = 0")

examples/ivp/1d_kdv_burgers/kdv_burgers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
ux.differentiate(0, out=uxx)
4646

4747
# Store data for final plot
48-
u.set_scales(1, keep_data=True)
48+
u.set_scales(1)
4949
u_list = [np.copy(u['g'])]
5050
t_list = [solver.sim_time]
5151

@@ -54,7 +54,7 @@
5454
while solver.ok:
5555
solver.step(dt)
5656
if solver.iteration % 20 == 0:
57-
u.set_scales(1, keep_data=True)
57+
u.set_scales(1)
5858
u_list.append(np.copy(u['g']))
5959
t_list.append(solver.sim_time)
6060
if solver.iteration % 100 == 0:
Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""boundary_forced_waves.py
2-
1+
"""
32
Solve the 2D linear wave equation forced on the right y-boundary by an
43
x and t-dependent forcing function:
54
@@ -19,6 +18,7 @@
1918
with n = 10, Lf = 1, and freq = 10.
2019
2120
"""
21+
2222
import time
2323
import numpy as np
2424
import dedalus.public as de
@@ -27,81 +27,78 @@
2727
import logging
2828
logger = logging.getLogger(__name__)
2929

30+
31+
# Parameters
3032
nx = 100
3133
ny = 100
32-
c = 1.
34+
c = 1.0
35+
dt = 0.01
3336

34-
x = de.Fourier('x',nx,interval=[0.,2*np.pi])
35-
y = de.Chebyshev('y',ny,interval=[0.,2*np.pi])
36-
domain = de.Domain([x,y],grid_dtype='float')
37+
# Bases and domain
38+
x_basis = de.Fourier('x', nx, interval=(0, 2*np.pi))
39+
y_basis = de.Chebyshev('y', ny, interval=(0, 2*np.pi))
40+
domain = de.Domain([x_basis, y_basis], grid_dtype='float')
3741

3842
def BoundaryForcing(*args):
39-
"""this function applies its arguments and returns the forcing
40-
41-
"""
43+
"""This function applies its arguments and returns the forcing"""
4244
t = args[0].value # this is a scalar; we use .value to get its value
4345
x = args[1].data # this is an array; we use .data to get its values
4446
ampl = args[2].value
4547
freq = args[3].value
46-
4748
return ampl*cos_bump(x)*np.cos(t*freq)
4849

4950
def cos_bump(x, Lx=2*np.pi, n=10):
50-
"""a simple, smooth bump function.
51-
52-
"""
51+
"""A simple, smooth bump function."""
5352
return (((1-np.cos(2*np.pi/Lx * x))/2)**n)
5453

55-
5654
def Forcing(*args, domain=domain, F=BoundaryForcing):
57-
"""This function takes arguments *args, a function F, and a domain and
55+
"""
56+
This function takes arguments *args, a function F, and a domain and
5857
returns a Dedalus GeneralFunction that can be applied.
59-
6058
"""
6159
return de.operators.GeneralFunction(domain, layout='g', func=F, args=args)
6260

63-
# now we make it parseable, so the symbol BF can be used in equations
61+
# Now we make it parseable, so the symbol BF can be used in equations
6462
# and the parser will know what it is.
6563
de.operators.parseables['BF'] = Forcing
6664

67-
waves = de.IVP(domain,variables=['f','f_t','f_y'])
65+
# Problem
66+
waves = de.IVP(domain, variables=['f','f_t','f_y'])
6867
waves.parameters['csq'] = c**2
6968
waves.parameters['ampl'] = 1.
7069
waves.parameters['freq'] = 10.
7170
waves.add_equation("dt(f_t) - csq*(dx(dx(f)) + dy(f_y)) = 0")
7271
waves.add_equation("f_t - dt(f) = 0")
7372
waves.add_equation("f_y - dy(f) = 0")
74-
7573
# note that we apply the right() operator to the forcing
7674
waves.add_bc("left(f) = 0")
7775
waves.add_bc("right(f) = right(BF(t,x,ampl,freq))")
7876

77+
# Solver
7978
solver = waves.build_solver(de.timesteppers.RK443)
80-
8179
solver.stop_sim_time = 10.
82-
solver.stop_wall_time = 1000.
83-
solver.stop_iteration = np.inf
84-
dt = 0.01
8580

86-
# analysis
81+
# Analysis
8782
analysis_tasks = []
8883
check = solver.evaluator.add_file_handler('checkpoints', iter=10, max_writes=200)
8984
check.add_system(solver.state)
9085
analysis_tasks.append(check)
9186

87+
# Main loop
9288
start_time = time.time()
9389
while solver.ok:
9490
solver.step(dt)
9591
if (solver.iteration-1) % 10 == 0:
9692
logger.info("Time step {}".format(solver.iteration))
9793
end_time = time.time()
94+
9895
# Print statistics
9996
logger.info('Total time: %f' %(end_time-start_time))
10097
logger.info('Iterations: %i' %solver.iteration)
10198
logger.info('Average timestep: %f' %(solver.sim_time/solver.iteration))
10299

100+
# Merge output
103101
logger.info('beginning join operation')
104-
105102
for task in analysis_tasks:
106103
logger.info(task.base_path)
107104
post.merge_analysis(task.base_path)

examples/ivp/2d_rayleigh_benard/merge.py

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

examples/ivp/2d_rayleigh_benard/plot_2d_series.py renamed to examples/ivp/2d_rayleigh_benard/plot_slices.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Plot planes from joint analysis files.
33
44
Usage:
5-
plot_2d_series.py <files>... [--output=<dir>]
5+
plot_slices.py <files>... [--output=<dir>]
66
77
Options:
88
--output=<dir> Output directory [default: ./frames]

examples/ivp/2d_rayleigh_benard/rayleigh_benard.py

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,34 @@
55
conditions. The equations are scaled in units of the buoyancy time (Fr = 1).
66
77
This script can be ran serially or in parallel, and uses the built-in analysis
8-
framework to save data snapshots in HDF5 files. The `merge.py` script in this
9-
folder can be used to merge distributed analysis sets from parallel runs,
10-
and the `plot_2d_series.py` script can be used to plot the snapshots.
8+
framework to save data snapshots in HDF5 files. The `merge_procs` command can
9+
be used to merge distributed analysis sets from parallel runs, and the
10+
`plot_slices.py` script can be used to plot the snapshots.
1111
1212
To run, merge, and plot using 4 processes, for instance, you could use:
1313
$ mpiexec -n 4 python3 rayleigh_benard.py
14-
$ mpiexec -n 4 python3 merge.py snapshots
15-
$ mpiexec -n 4 python3 plot_2d_series.py snapshots/*.h5
14+
$ mpiexec -n 4 python3 -m dedalus merge_procs snapshots
15+
$ mpiexec -n 4 python3 plot_slices.py snapshots/*.h5
1616
17-
The simulation should take a few process-minutes to run.
17+
This script can restart the simulation from the last save of the original
18+
output to extend the integration. This requires that the output files from
19+
the original simulation are merged, and the last is symlinked or copied to
20+
`restart.h5`.
21+
22+
To run the original example and the restart, you could use:
23+
$ mpiexec -n 4 python3 rayleigh_benard.py
24+
$ mpiexec -n 4 python3 -m dedalus merge_procs snapshots
25+
$ ln -s snapshots/snapshots_s2.h5 restart.h5
26+
$ mpiexec -n 4 python3 rayleigh_benard.py
27+
28+
The simulations should take a few process-minutes to run.
1829
1930
"""
2031

2132
import numpy as np
2233
from mpi4py import MPI
2334
import time
35+
import pathlib
2436

2537
from dedalus import public as de
2638
from dedalus.extras import flow_tools
@@ -64,34 +76,46 @@
6476
solver = problem.build_solver(de.timesteppers.RK222)
6577
logger.info('Solver built')
6678

67-
# Initial conditions
68-
x = domain.grid(0)
69-
z = domain.grid(1)
70-
b = solver.state['b']
71-
bz = solver.state['bz']
72-
73-
# Random perturbations, initialized globally for same results in parallel
74-
gshape = domain.dist.grid_layout.global_shape(scales=1)
75-
slices = domain.dist.grid_layout.slices(scales=1)
76-
rand = np.random.RandomState(seed=42)
77-
noise = rand.standard_normal(gshape)[slices]
78-
79-
# Linear background + perturbations damped at walls
80-
zb, zt = z_basis.interval
81-
pert = 1e-3 * noise * (zt - z) * (z - zb)
82-
b['g'] = F * pert
83-
b.differentiate('z', out=bz)
84-
85-
# Initial timestep
86-
dt = 0.125
79+
# Initial conditions or restart
80+
if not pathlib.Path('restart.h5').exists():
81+
82+
# Initial conditions
83+
x = domain.grid(0)
84+
z = domain.grid(1)
85+
b = solver.state['b']
86+
bz = solver.state['bz']
87+
88+
# Random perturbations, initialized globally for same results in parallel
89+
gshape = domain.dist.grid_layout.global_shape(scales=1)
90+
slices = domain.dist.grid_layout.slices(scales=1)
91+
rand = np.random.RandomState(seed=42)
92+
noise = rand.standard_normal(gshape)[slices]
93+
94+
# Linear background + perturbations damped at walls
95+
zb, zt = z_basis.interval
96+
pert = 1e-3 * noise * (zt - z) * (z - zb)
97+
b['g'] = F * pert
98+
b.differentiate('z', out=bz)
99+
100+
# Timestepping and output
101+
dt = 0.125
102+
stop_sim_time = 25
103+
fh_mode = 'overwrite'
104+
105+
else:
106+
# Restart
107+
write, last_dt = solver.load_state('restart.h5', -1)
108+
109+
# Timestepping and output
110+
dt = last_dt
111+
stop_sim_time = 50
112+
fh_mode = 'append'
87113

88114
# Integration parameters
89-
solver.stop_sim_time = 25
90-
solver.stop_wall_time = 30 * 60.
91-
solver.stop_iteration = np.inf
115+
solver.stop_sim_time = stop_sim_time
92116

93117
# Analysis
94-
snapshots = solver.evaluator.add_file_handler('snapshots', sim_dt=0.25, max_writes=50)
118+
snapshots = solver.evaluator.add_file_handler('snapshots', sim_dt=0.25, max_writes=50, mode=fh_mode)
95119
snapshots.add_system(solver.state)
96120

97121
# CFL

0 commit comments

Comments
 (0)