shenfun.utilities package¶
Submodules¶
shenfun.utilities.findbasis module¶
- shenfun.utilities.findbasis.get_bc_basis(bcs, family, alpha=None, beta=None, gn=1)[source]¶
Return boundary basis satisfying bcs.
- Parameters:
bcs (dict) – The boundary conditions in dictionary form, see
BoundaryConditions
.family (str) – Choose one of
Chebyshev
Chebyshevu
Legendre
Ultraspherical
Jacobi
Laguerre
alpha, beta (numbers, optional) – The Jacobi parameters, used only for
Jacobi
orUltraspherical
gn (Scaling function for Jacobi polynomials)
- shenfun.utilities.findbasis.get_stencil_matrix(bcs, family, alpha=None, beta=None, gn=1)[source]¶
Return stencil matrix.
Return the narrowest possible stencil matrix satisfying the homogeneous boundary conditions in bcs, and with leading coefficient equal to one.
- Parameters:
bcs (dict) – The boundary conditions in dictionary form, see
BoundaryConditions
.family (str) – Choose one of
Chebyshev
Chebyshevu
Legendre
Ultraspherical
Jacobi
Laguerre
alpha, beta (numbers, optional) – The Jacobi parameters, used only for
Jacobi
orUltraspherical
gn (Scaling function for Jacobi polynomials)
shenfun.utilities.integrators module¶
Module for some integrators.
Integrators are set up to solve equations like
where \(u\) is the solution, \(L\) is a linear operator and \(N(u)\) is the nonlinear part of the right hand side.
There are two kinds of integrators, or time steppers. The first are ment to be subclassed and used one at the time. These are
IRK3: Implicit third order Runge-Kutta
RK4: Runge-Kutta fourth order
ETD: Exponential time differencing Euler method
ETDRK4: Exponential time differencing Runge-Kutta fourth order
See, e.g., H. Montanelli and N. Bootland “Solving periodic semilinear PDEs in 1D, 2D and 3D with exponential integrators”, https://arxiv.org/pdf/1604.08900.pdf
The second kind is ment to be used for systems of equations, one class instance for each equation. These are mainly IMEX Runge Kutta integrators:
IMEXRK3
IMEXRK111
IMEXRK222
IMEXRK443
See, e.g., https://github.com/spectralDNS/shenfun/blob/master/demo/ChannelFlow for an example of use.
Note
RK4, ETD and ETDRK4 can only be used with Fourier function spaces, as they assume all matrices are diagonal.
- class shenfun.utilities.integrators.BackwardEuler(T, L=None, N=None, update=None, **params)[source]¶
Bases:
IntegratorBase
First order backward Euler
- Parameters:
T (TensorProductSpace)
L (function of TrialFunction(T)) – To compute linear part of right hand side
N (function) – To compute nonlinear part of right hand side
update (function) – To be called at the end of a timestep
params (dictionary) – Any relevant keyword arguments
- class shenfun.utilities.integrators.ETD(T, L=None, N=None, update=None, **params)[source]¶
Bases:
IntegratorBase
Exponential time differencing Euler method
H. Montanelli and N. Bootland “Solving periodic semilinear PDEs in 1D, 2D and 3D with exponential integrators”, https://arxiv.org/pdf/1604.08900.pdf
- Parameters:
T (TensorProductSpace)
L (function) – To compute linear part of right hand side
N (function) – To compute nonlinear part of right hand side
update (function) – To be called at the end of a timestep
params (dictionary) – Any relevant keyword arguments
- class shenfun.utilities.integrators.ETDRK4(T, L=None, N=None, update=None, **params)[source]¶
Bases:
IntegratorBase
Exponential time differencing Runge-Kutta 4’th order method
H. Montanelli and N. Bootland “Solving periodic semilinear PDEs in 1D, 2D and 3D with exponential integrators”, https://arxiv.org/pdf/1604.08900.pdf
- Parameters:
T (TensorProductSpace)
L (function) – To compute linear part of right hand side
N (function) – To compute nonlinear part of right hand side
update (function) – To be called at the end of a timestep
params (dictionary) – Any relevant keyword arguments
- class shenfun.utilities.integrators.IMEXRK011(v, u, L, N, dt, solver=None, name='U-equation', latex=None)[source]¶
Bases:
PDEIMEXRK
- class shenfun.utilities.integrators.IMEXRK111(v, u, L, N, dt, solver=None, name='U-equation', latex=None)[source]¶
Bases:
PDEIMEXRK
- class shenfun.utilities.integrators.IMEXRK222(v, u, L, N, dt, solver=None, name='U-equation', latex=None)[source]¶
Bases:
PDEIMEXRK
- class shenfun.utilities.integrators.IMEXRK3(v, u, L, N, dt, solver=None, name='U-equation', latex=None)[source]¶
Bases:
object
Solve partial differential equations of the form
\[\frac{\partial u}{\partial t} = N+Lu, \quad (1)\]where \(N\) is a nonlinear term and \(L\) is a linear operator.
This is a third order accurate implicit Runge-Kutta solver
- Parameters:
v (
TestFunction
)u (
Expr
orFunction
) – Representing \(u\) in (1) If anExpr
, then its basis must be aFunction
TheFunction
will then hold the solution. If \(u\) is aFunction
, then this will hold the solution.L (Linear operator) – Operates on \(u\)
dt (number) – Time step
solver (Linear solver, optional)
name (str, optional)
- class shenfun.utilities.integrators.IMEXRK443(v, u, L, N, dt, solver=None, name='U-equation', latex=None)[source]¶
Bases:
PDEIMEXRK
- class shenfun.utilities.integrators.IRK3(T, L=None, N=None, update=None, **params)[source]¶
Bases:
IntegratorBase
Third order implicit Runge Kutta
- Parameters:
T (TensorProductSpace)
L (function of TrialFunction(T)) – To compute linear part of right hand side
N (function) – To compute nonlinear part of right hand side
update (function) – To be called at the end of a timestep
params (dictionary) – Any relevant keyword arguments
- class shenfun.utilities.integrators.RK4(T, L=None, N=None, update=None, **params)[source]¶
Bases:
IntegratorBase
Regular 4’th order Runge-Kutta integrator
- Parameters:
T (TensorProductSpace)
L (function) – To compute linear part of right hand side
N (function) – To compute nonlinear part of right hand side
update (function) – To be called at the end of a timestep
params (dictionary) – Any relevant keyword arguments
shenfun.utilities.lagrangian_particles module¶
Module contents¶
Module for implementing helper functions.
- class shenfun.utilities.CachedArrayDict[source]¶
Bases:
MutableMapping
Dictionary for caching Numpy arrays (work arrays)
Example
>>> import numpy as np >>> from shenfun.utilities import CachedArrayDict >>> work = CachedArrayDict() >>> a = np.ones((3, 4), dtype=int) >>> w = work[(a, 0, True)] # create work array with shape as a >>> print(w.shape) (3, 4) >>> print(w) [[0 0 0 0] [0 0 0 0] [0 0 0 0]] >>> w2 = work[(a, 1, True)] # Get different(note 1!) array of same shape/dtype
- shenfun.utilities.Lambda(x)¶
Return
\[\Lambda(x) =\]rac{Gamma(x+ rac{1}{2})}{Gamma(x+1)}
x : array of floats
- shenfun.utilities.cross(c, a, b)[source]¶
Cross product c = a x b
- Parameters:
c (Array)
a (Array)
b (Array)
- Returns:
c
- Return type:
- shenfun.utilities.dot(u, v, output_array=None, forward_output=True)[source]¶
Return dot product of u and v
- Parameters:
u (Function or Array)
v (Function or Array)
output_array (Function or Array) – Must be of correct shape
forward_output (bool, optional) – Return the result as a Function if True, and an Array if False.
Note
This function uses the 3/2-rule for dealiasing all spaces if the input arrays are spectral Functions. The returned Function is projected to an orthogonal space, without boundary conditions.
- shenfun.utilities.dx(u, weighted=False)[source]¶
Compute integral of u over domain
\[\int_{\Omega} u dx\]- Parameters:
u (Array) – The Array to integrate
Note
This function assumes a standard reference domain. If the domain is not standard, then modify the result accordingly.
- shenfun.utilities.get_bc_basis(bcs, family, alpha=None, beta=None, gn=1)[source]¶
Return boundary basis satisfying bcs.
- Parameters:
bcs (dict) – The boundary conditions in dictionary form, see
BoundaryConditions
.family (str) – Choose one of
Chebyshev
Chebyshevu
Legendre
Ultraspherical
Jacobi
Laguerre
alpha, beta (numbers, optional) – The Jacobi parameters, used only for
Jacobi
orUltraspherical
gn (Scaling function for Jacobi polynomials)
- shenfun.utilities.get_stencil_matrix(bcs, family, alpha=None, beta=None, gn=1)[source]¶
Return stencil matrix.
Return the narrowest possible stencil matrix satisfying the homogeneous boundary conditions in bcs, and with leading coefficient equal to one.
- Parameters:
bcs (dict) – The boundary conditions in dictionary form, see
BoundaryConditions
.family (str) – Choose one of
Chebyshev
Chebyshevu
Legendre
Ultraspherical
Jacobi
Laguerre
alpha, beta (numbers, optional) – The Jacobi parameters, used only for
Jacobi
orUltraspherical
gn (Scaling function for Jacobi polynomials)
- shenfun.utilities.integrate_sympy(f, d)[source]¶
Exact definite integral using sympy
Try to convert expression f to a polynomial before integrating.
See sympy issue https://github.com/sympy/sympy/pull/18613 to why this is needed. Poly().integrate() is much faster than sympy.integrate() when applicable.
- Parameters:
f (sympy expression)
d (3-tuple) – First item the symbol, next two the lower and upper integration limits
- shenfun.utilities.mayavi_show()[source]¶
Return show function that updates the mayavi figure in the background.
- shenfun.utilities.outer(a, b, c)[source]¶
Return outer product $c_{i,j} = a_i b_j$
- Parameters:
a (Array of shape (N, …))
b (Array of shape (N, …))
c (Array of shape (N*N, …))
The outer product is taken over the first index of a and b,
for all remaining indices.
- shenfun.utilities.quiver3D(u, mesh=None, wrapaxes=(), slices=None, fig=None, kind='quadrature', **kw)[source]¶
- Parameters:
u (Array)
mesh (list of Cartesian meshes [x, y, z], optional)
backend (str, optional) – plotly or mayavi
wrapaxes (sequence of integers, optional) – For domains that wrap around, extend mesh/u by one in given direction
fig (None, optional) – If None create a new figure
slices (None or sequence of slices, optional) – If only part of u should be plotted
kind (str, optional) –
‘quadrature’ - Use quadrature mesh
‘uniform’ - Use uniform mesh
kw (Keyword arguments, optional) – Arguments to mlab.quiver3d, for example ‘scale_factor’, ‘color’ or ‘’mode
- shenfun.utilities.reset_profile(prof)[source]¶
Reset profiler for kernprof
- Parameters:
prof (The profiler)
- shenfun.utilities.scalar_product(v, f, output_array=None, assemble='exact')[source]¶
Return scalar product
\[(v, f)_w\]- Parameters:
v (
TestFunction
)f (Sympy function)
output_array (
Function
)assemble (str, optional) –
‘exact’
‘adaptive’
Note
The computed scalar product is not compensated for a non-standard domain size.
>>> from shenfun import inner, FunctionSpace, TestFunction >>> import sympy as sp >>> C = FunctionSpace(4, 'C') >>> v = TestFunction(C) >>> x = sp.Symbol('x', real=True) >>> f = x**2 >>> inner(v, f, assemble='exact') Function([1.57079633, 0. , 0.78539816, 0. ])
- shenfun.utilities.surf3D(u, mesh=None, backend='plotly', wrapaxes=(), slices=None, fig=None, **kw)[source]¶
Plot surface embedded in 3D
- Parameters:
u (Function or Array)
mesh (list, str or None, optional) –
list of Cartesian meshes [x, y, z]
‘quadrature’ - use quadrature mesh
‘uniform’ - use uniform mesh
backend (str, optional) – plotly or mayavi
wrapaxes (sequence of integers, optional) – For domains that wrap around, extend mesh/u by one in given direction
slices (None or sequence of slices, optional) – If only part of u should be plotted
fig (Figure instance, optional)
kw (Keyword arguments, optional) – Used by plotly Surface. Possibly colorscale.