Skip to content

Commit 75e6b1c

Browse files
committed
Allow changing scales during field getitem/setitem
1 parent 4bd724d commit 75e6b1c

File tree

6 files changed

+71
-39
lines changed

6 files changed

+71
-39
lines changed

dedalus/core/field.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -575,13 +575,23 @@ def __init__(self, dist, bases=None, name=None, tensorsig=None, dtype=None):
575575
# Add weak reference to distributor
576576
dist.fields.add(self)
577577

578-
def __getitem__(self, layout):
579-
"""Return data viewed in specified layout."""
578+
def __getitem__(self, key):
579+
"""Return data viewed in specified layout and scales."""
580+
if isinstance(key, tuple):
581+
layout, scales = key
582+
self.change_scales(scales)
583+
else:
584+
layout = key
580585
self.change_layout(layout)
581586
return self.data
582587

583-
def __setitem__(self, layout, data):
584-
"""Set data viewed in a specified layout."""
588+
def __setitem__(self, key, data):
589+
"""Set data viewed in a specified layout and scales."""
590+
if isinstance(key, tuple):
591+
layout, scales = key
592+
self.preset_scales(scales)
593+
else:
594+
layout = key
585595
layout = self.dist.get_layout_object(layout)
586596
self.preset_layout(layout)
587597
copyto(self.data, data)

docs/notebooks/dedalus_tutorial_2.ipynb

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
"name": "stderr",
213213
"output_type": "stream",
214214
"text": [
215-
"/var/folders/gl/8q1_pm2s1490lvyfvm_8yby80000gn/T/ipykernel_4766/1812536267.py:4: RuntimeWarning: divide by zero encountered in log10\n",
215+
"/var/folders/gl/8q1_pm2s1490lvyfvm_8yby80000gn/T/ipykernel_26772/1812536267.py:4: RuntimeWarning: divide by zero encountered in log10\n",
216216
" log_mag = lambda xmesh, ymesh, data: (xmesh, ymesh, np.log10(np.abs(data)))\n"
217217
]
218218
},
@@ -351,6 +351,32 @@
351351
"plot_bot_2d(f, title=\"f['g']\", figkw=figkw);"
352352
]
353353
},
354+
{
355+
"cell_type": "markdown",
356+
"metadata": {},
357+
"source": [
358+
"Scale factors can also be provided as second arguments when setting or retrieving field data via the geitem/setitem interface:"
359+
]
360+
},
361+
{
362+
"cell_type": "code",
363+
"execution_count": 10,
364+
"metadata": {},
365+
"outputs": [
366+
{
367+
"name": "stdout",
368+
"output_type": "stream",
369+
"text": [
370+
"(64, 64)\n",
371+
"(128, 128)\n"
372+
]
373+
}
374+
],
375+
"source": [
376+
"print(f['g', 1].shape)\n",
377+
"print(f['g', 2].shape)"
378+
]
379+
},
354380
{
355381
"cell_type": "markdown",
356382
"metadata": {},
@@ -378,7 +404,7 @@
378404
},
379405
{
380406
"cell_type": "code",
381-
"execution_count": 10,
407+
"execution_count": 11,
382408
"metadata": {},
383409
"outputs": [
384410
{
@@ -405,7 +431,7 @@
405431
},
406432
{
407433
"cell_type": "code",
408-
"execution_count": 11,
434+
"execution_count": 12,
409435
"metadata": {},
410436
"outputs": [
411437
{
@@ -448,7 +474,7 @@
448474
},
449475
{
450476
"cell_type": "code",
451-
"execution_count": 12,
477+
"execution_count": 13,
452478
"metadata": {},
453479
"outputs": [
454480
{
@@ -473,7 +499,7 @@
473499
},
474500
{
475501
"cell_type": "code",
476-
"execution_count": 13,
502+
"execution_count": 14,
477503
"metadata": {},
478504
"outputs": [
479505
{
@@ -506,7 +532,7 @@
506532
},
507533
{
508534
"cell_type": "code",
509-
"execution_count": 14,
535+
"execution_count": 15,
510536
"metadata": {},
511537
"outputs": [
512538
{
@@ -550,7 +576,7 @@
550576
},
551577
{
552578
"cell_type": "code",
553-
"execution_count": 15,
579+
"execution_count": 16,
554580
"metadata": {},
555581
"outputs": [
556582
{
@@ -581,7 +607,7 @@
581607
},
582608
{
583609
"cell_type": "code",
584-
"execution_count": 16,
610+
"execution_count": 17,
585611
"metadata": {},
586612
"outputs": [
587613
{
@@ -625,7 +651,7 @@
625651
},
626652
{
627653
"cell_type": "code",
628-
"execution_count": 17,
654+
"execution_count": 18,
629655
"metadata": {},
630656
"outputs": [],
631657
"source": [
@@ -649,7 +675,7 @@
649675
},
650676
{
651677
"cell_type": "code",
652-
"execution_count": 18,
678+
"execution_count": 19,
653679
"metadata": {},
654680
"outputs": [
655681
{
@@ -694,7 +720,7 @@
694720
},
695721
{
696722
"cell_type": "code",
697-
"execution_count": 19,
723+
"execution_count": 20,
698724
"metadata": {},
699725
"outputs": [],
700726
"source": [
@@ -718,7 +744,7 @@
718744
},
719745
{
720746
"cell_type": "code",
721-
"execution_count": 20,
747+
"execution_count": 21,
722748
"metadata": {},
723749
"outputs": [
724750
{
@@ -757,7 +783,7 @@
757783
},
758784
{
759785
"cell_type": "code",
760-
"execution_count": 21,
786+
"execution_count": 22,
761787
"metadata": {},
762788
"outputs": [
763789
{

docs/notebooks/dedalus_tutorial_3.ipynb

Lines changed: 14 additions & 12 deletions
Large diffs are not rendered by default.

examples/ivp_1d_kdv_burgers/kdv_burgers.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,14 @@
5454
solver.stop_sim_time = stop_sim_time
5555

5656
# Main loop
57-
u.change_scales(1)
58-
u_list = [np.copy(u['g'])]
57+
u_list = [u['g',1].copy()]
5958
t_list = [solver.sim_time]
6059
while solver.proceed:
6160
solver.step(timestep)
6261
if solver.iteration % 100 == 0:
6362
logger.info('Iteration=%i, Time=%e, dt=%e' %(solver.iteration, solver.sim_time, timestep))
6463
if solver.iteration % 25 == 0:
65-
u.change_scales(1)
66-
u_list.append(np.copy(u['g']))
64+
u_list.append(u['g',1].copy())
6765
t_list.append(solver.sim_time)
6866

6967
# Plot

examples/ivp_2d_rayleigh_benard/rayleigh_benard.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
flow.add_property(np.sqrt(u@u)/nu, name='Re')
104104

105105
# Main loop
106-
startup_iter = 10
107106
try:
108107
logger.info('Starting main loop')
109108
while solver.proceed:
@@ -117,4 +116,3 @@
117116
raise
118117
finally:
119118
solver.log_stats()
120-

examples/nlbvp_ball_lane_emden/lane_emden.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,15 @@
7979
# Solver
8080
solver = problem.build_solver(ncc_cutoff=ncc_cutoff)
8181
pert_norm = np.inf
82-
f.change_scales(dealias)
83-
steps = [f['g'].ravel().copy()]
82+
steps = [f['g',1].copy().ravel()]
8483
while pert_norm > tolerance:
8584
solver.newton_iteration()
8685
pert_norm = sum(pert.allreduce_data_norm('c', 2) for pert in solver.perturbations)
8786
logger.info(f'Perturbation norm: {pert_norm:.3e}')
8887
f0 = f(r=0).evaluate().allgather_data('g')[0,0,0]
8988
Ri = f0**((n-1)/2)
9089
logger.info(f'R iterate: {Ri}')
91-
steps.append(f['g'].ravel().copy())
90+
steps.append(f['g',1].copy().ravel())
9291

9392
# Compare to reference solutions from Boyd
9493
R_ref = {0.0: np.sqrt(6),
@@ -110,7 +109,6 @@
110109

111110
# Plot solution
112111
plt.figure(figsize=(6, 4))
113-
_, _, r = dist.local_grids(ball, scales=(dealias,dealias,dealias))
114112
alpha = np.linspace(0.2, 1, len(steps))
115113
color = ('C0',) * (len(steps)-1) + ('C1',)
116114
for i, step in enumerate(steps):

0 commit comments

Comments
 (0)