# 4.2.6. pde.grids.spherical module¶

Spherically-symmetric grids in 2 and 3 dimensions. These are grids that only discretize the radial direction, assuming symmetry with respect to all angles. This choice implies that differential operators might not be applicable to all fields. For instance, the divergence of a vector field on a spherical grid can only be represented as a scalar field on the same grid if the θ-component of the vector field vanishes.

2-dimensional polar grid assuming angular symmetry

The angular symmetry implies that states only depend on the radial coordinate $$r$$, which is discretized uniformly as

$r_i = R_\mathrm{inner} + \left(i + \frac12\right) \Delta r \quad \text{for} \quad i = 0, \ldots, N - 1 \quad \text{with} \quad \Delta r = \frac{R_\mathrm{outer} - R_\mathrm{inner}}{N}$

where $$R_\mathrm{outer}$$ is the outer radius of the grid and $$R_\mathrm{inner}$$ corresponds to a possible inner radius, which is zero by default. The radial direction is discretized by $$N$$ support points.

Parameters
• radius (float or tuple of floats) – radius $$R_\mathrm{outer}$$ in case a simple float is given. If a tuple is supplied it is interpreted as the inner and outer radius, $$(R_\mathrm{inner}, R_\mathrm{outer})$$.

• shape (tuple or int) – A single number setting the number $$N$$ of support points along the radial coordinate

axes: List[str] = ['r']

Names of all axes that are described by the grid

Type

list

axes_symmetric: List[str] = ['phi']

The names of the additional axes that the fields do not depend on, e.g. along which they are constant.

Type

list

cell_volume_data: Sequence[FloatNumerical]

Information about the size of discretization cells

Type

list

coordinate_constraints: List[int] = [0, 1]

axes that not described explicitly

Type

list

dim: int = 2

The spatial dimension in which the grid is embedded

Type

int

point_to_cartesian(points, *, full=False)[source]

convert coordinates of a point to Cartesian coordinates

This function returns points along the y-coordinate, i.e, the x coordinates will be zero.

Parameters
• points (ndarray) – The grid coordinates of the points

• full (bool) – Flag indicating whether angular coordinates are specified

Returns

The Cartesian coordinates of the point

Return type

ndarray

3-dimensional spherical grid assuming spherical symmetry

The symmetry implies that states only depend on the radial coordinate $$r$$, which is discretized as follows:

$r_i = R_\mathrm{inner} + \left(i + \frac12\right) \Delta r \quad \text{for} \quad i = 0, \ldots, N - 1 \quad \text{with} \quad \Delta r = \frac{R_\mathrm{outer} - R_\mathrm{inner}}{N}$

where $$R_\mathrm{outer}$$ is the outer radius of the grid and $$R_\mathrm{inner}$$ corresponds to a possible inner radius, which is zero by default. The radial direction is discretized by $$N$$ support points.

Warning

Not all results of differential operators on vectorial and tensorial fields can be expressed in terms of fields that only depend on the radial coordinate $$r$$. In particular, the gradient of a vector field can only be calculated if the azimuthal component of the vector field vanishes. Similarly, the divergence of a tensor field can only be taken in special situations.

Parameters
• radius (float or tuple of floats) – radius $$R_\mathrm{outer}$$ in case a simple float is given. If a tuple is supplied it is interpreted as the inner and outer radius, $$(R_\mathrm{inner}, R_\mathrm{outer})$$.

• shape (tuple or int) – A single number setting the number $$N$$ of support points along the radial coordinate

axes: List[str] = ['r']

Names of all axes that are described by the grid

Type

list

axes_symmetric: List[str] = ['theta', 'phi']

The names of the additional axes that the fields do not depend on, e.g. along which they are constant.

Type

list

cell_volume_data: Sequence[FloatNumerical]

Information about the size of discretization cells

Type

list

coordinate_constraints: List[int] = [0, 1, 2]

axes that not described explicitly

Type

list

dim: int = 3

The spatial dimension in which the grid is embedded

Type

int

point_to_cartesian(points, *, full=False)[source]

convert coordinates of a point to Cartesian coordinates

This function returns points along the z-coordinate, i.e, the x and y coordinates will be zero.

Parameters
• points (ndarray) – The grid coordinates of the points

• full (bool) – Flag indicating whether angular coordinates are specified

Returns

The Cartesian coordinates of the point

Return type

ndarray

Bases: GridBase

Base class for d-dimensional spherical grids with angular symmetry

The angular symmetry implies that states only depend on the radial coordinate $$r$$, which is discretized uniformly as

$r_i = R_\mathrm{inner} + \left(i + \frac12\right) \Delta r \quad \text{for} \quad i = 0, \ldots, N - 1 \quad \text{with} \quad \Delta r = \frac{R_\mathrm{outer} - R_\mathrm{inner}}{N}$

where $$R_\mathrm{outer}$$ is the outer radius of the grid and $$R_\mathrm{inner}$$ corresponds to a possible inner radius, which is zero by default. The radial direction is discretized by $$N$$ support points.

Parameters
• radius (float or tuple of floats) – radius $$R_\mathrm{outer}$$ in case a simple float is given. If a tuple is supplied it is interpreted as the inner and outer radius, $$(R_\mathrm{inner}, R_\mathrm{outer})$$.

• shape (tuple or int) – A single number setting the number $$N$$ of support points along the radial coordinate

axes: List[str]

Names of all axes that are described by the grid

Type

list

boundary_names: Dict[str, Tuple[int, bool]] = {'inner': (0, False), 'outer': (0, True)}

Names of boundaries to select them conveniently

Type

dict

cell_volume_data: Sequence[FloatNumerical]

Information about the size of discretization cells

Type

list

dim: int

The spatial dimension in which the grid is embedded

Type

int

classmethod from_bounds(bounds, shape, periodic)[source]
Parameters
• bounds (tuple) – Give the coordinate range for the radial axis.

• shape (tuple) – The number of support points for the radial axis

• periodic (bool or list) – Not used

Returns

SphericalGridBase representing the region chosen by bounds

Return type

SphericalSymGridBase

classmethod from_state(state)[source]

create a field from a stored state.

Parameters

state (dict) – The state from which the grid is reconstructed.

Return type

SphericalSymGridBase

get_cartesian_grid(mode='valid', num=None)[source]

return a Cartesian grid for this spherical one

Parameters
• mode (str) – Determines how the grid is determined. Setting it to ‘valid’ (or ‘inscribed’) only returns points that are fully resolved in the spherical grid, e.g., the Cartesian grid is inscribed in the sphere. Conversely, ‘full’ (or ‘circumscribed’) returns all data, so the Cartesian grid is circumscribed.

• num (int) – Number of support points along each axis of the returned grid.

Returns

The requested grid

Return type

pde.grids.cartesian.CartesianGrid

return a 2d-image of the data

Parameters
Returns

A dictionary with information about the image, which is convenient for plotting.

Return type

Dict[str, Any]

get_line_data(data, extract='auto')[source]

return a line cut along the radial axis

Parameters
• data (ndarray) – The values at the grid points

• extract (str) – Determines which cut is done through the grid. This parameter is mainly supplied for a consistent interface and has no effect for polar grids.

Returns

A dictionary with information about the line cut, which is convenient for plotting.

Return type

Dict[str, Any]

get_random_point(*, boundary_distance=0, avoid_center=False, coords='cartesian', rng=None)[source]

return a random point within the grid

Note that these points will be uniformly distributed in the volume, implying they are not uniformly distributed on the radial axis.

Parameters
• boundary_distance (float) – The minimal distance this point needs to have from all boundaries.

• avoid_center (bool) – Determines whether the boundary distance should also be kept from the center, i.e., whether points close to the center are returned.

• coords (str) – Determines the coordinate system in which the point is specified. Valid values are cartesian, cell, and grid; see transform().

• rng (Generator) – Random number generator (default: default_rng())

Returns

The coordinates of the point

Return type

ndarray

property has_hole: bool

returns whether the inner radius is larger than zero

iter_mirror_points(point, with_self=False, only_periodic=True)[source]

generates all mirror points corresponding to point

Parameters
• point (ndarray) – the point within the grid

• with_self (bool) – whether to include the point itself

• only_periodic (bool) – whether to only mirror along periodic axes

Returns

A generator yielding the coordinates that correspond to mirrors

Return type

Generator

num_axes: int = 1

Number of axes that are not assumed symmetrically

Type

int

periodic: List[bool] = [False]

Flags that describe which axes are periodic

Type

list

plot(*args, title=None, filename=None, action='auto', ax_style=None, fig_style=None, ax=None, **kwargs)[source]

visualize the spherically symmetric grid in two dimensions

Parameters
• title (str) – Title of the plot. If omitted, the title might be chosen automatically.

• filename (str, optional) – If given, the plot is written to the specified file.

• action (str) – Decides what to do with the final figure. If the argument is set to show, matplotlib.pyplot.show() will be called to show the plot. If the value is none, the figure will be created, but not necessarily shown. The value close closes the figure, after saving it to a file when filename is given. The default value auto implies that the plot is shown if it is not a nested plot call.

• ax_style (dict) – Dictionary with properties that will be changed on the axis after the plot has been drawn by calling matplotlib.pyplot.setp(). A special item i this dictionary is use_offset, which is flag that can be used to control whether offset are shown along the axes of the plot.

• fig_style (dict) – Dictionary with properties that will be changed on the figure after the plot has been drawn by calling matplotlib.pyplot.setp(). For instance, using fig_style={‘dpi’: 200} increases the resolution of the figure.

• ax (matplotlib.axes.Axes) – Figure axes to be used for plotting. The special value “create” creates a new figure, while “reuse” attempts to reuse an existing figure, which is the default.

• **kwargs – Extra arguments are passed on the to the matplotlib plotting routines, e.g., to set the color of the lines

point_from_cartesian(points)[source]

convert points given in Cartesian coordinates to this grid

Parameters

points (ndarray) – Points given in Cartesian coordinates.

Returns

Points given in the coordinates of the grid

Return type

ndarray

polar_coordinates_real(origin=None, *, ret_angle=False, **kwargs)[source]

return spherical coordinates associated with the grid

Parameters
• origin – Place holder variable to comply with the interface

• ret_angle (bool) – Determines whether angles are returned alongside the distance. If False only the distance to the origin is returned for each support point of the grid. If True, the distance and angles are returned. Note that in the case of spherical grids, this angle is zero by convention.

Return type

Union[ndarray, Tuple[ndarray, …]]

Type

float

property state: Dict[str, Any]

the state of the grid

Type

state

property volume: float

total volume of the grid

Type

float

float or ndarray