DedalusProject / dedalus

A flexible framework for solving PDEs with modern spectral methods.
http://dedalus-project.org/
GNU General Public License v3.0
513 stars 121 forks source link

Documentation example not working #163

Closed Joel-Dahne closed 3 years ago

Joel-Dahne commented 3 years ago

I recently installed Dedalus and tried to run some of the code from Tutorial 2: Fields and Operators but it didn't work as written there. More precisely I tried to run the code

import numpy as np
import matplotlib.pyplot as plt
from dedalus import public as de
from dedalus.extras.plot_tools import plot_bot_2d
figkw = {'figsize':(6,4), 'dpi':100}

xbasis = de.Fourier('x', 64, interval=(-np.pi, np.pi), dealias=3/2)
ybasis = de.Chebyshev('y', 64, interval=(-1, 1), dealias=3/2)
domain = de.Domain([xbasis, ybasis], grid_dtype=np.float64)
f = de.Field(domain, name='f')

x, y = domain.grids(scales=1)
f.set_scales(1)
f['g'] = np.exp((1-y**2)*np.cos(x+np.cos(x)*y**2)) * (1 + 0.05*np.cos(10*(x+2*y)))

# Plot grid values
plot_bot_2d(f, figkw=figkw, title="f['g']");

but got an error from f = de.Field(domain, name='f') saying

AttributeError: module 'dedalus.public' has no attribute 'Field'

After searching around a bit in the source code for Dedalus I came up with the solution

import numpy as np
import matplotlib.pyplot as plt
import dedalus
from dedalus import public as de
from dedalus.extras.plot_tools import plot_bot_2d
figkw = {'figsize':(6,4), 'dpi':100}

xbasis = de.Fourier('x', 64, interval=(-np.pi, np.pi), dealias=3/2)
ybasis = de.Chebyshev('y', 64, interval=(-1, 1), dealias=3/2)
domain = de.Domain([xbasis, ybasis], grid_dtype=np.float64)
f = dedalus.core.domain.Field(domain, name='f')

x, y = domain.grids(scales=1)
f.set_scales(1)
f['g'] = np.exp((1-y**2)*np.cos(x+np.cos(x)*y**2)) * (1 + 0.05*np.cos(10*(x+2*y)))

# Plot grid values
plot_bot_2d(f, figkw=figkw, title="f['g']");

which worked for me.

Is the documentation for an older version of Dedalus or did I miss something?

kburns commented 3 years ago

This error comes up if you try to run the latest examples (from master) using an older version of the code (e.g. the latest pip release). Everything should execute correctly if you use the same version of the code and examples.

Joel-Dahne commented 3 years ago

Oh I see, I missed the version switch in the documentation! Thank you!