4.2.7 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.

class PolarSymGrid(radius, shape)[source]

Bases: SphericalSymGridBase

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) – The number \(N\) of support points along the radial coordinate.

c: CoordinatesBase = PolarCoordinates()

Coordinates of the grid

Type:

CoordinatesBase

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

axes that not described explicitly

Type:

list

class SphericalSymGrid(radius, shape)[source]

Bases: SphericalSymGridBase

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) – The number \(N\) of support points along the radial coordinate.

c: CoordinatesBase = SphericalCoordinates()

Coordinates of the grid

Type:

CoordinatesBase

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

axes that not described explicitly

Type:

list

class SphericalSymGridBase(radius, shape)[source]

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) – The number \(N\) of support points along the radial coordinate.

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] | None

Information about the size of discretization cells

Type:

list

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 (tuple[bool]) –

Returns:

represents the region chosen by bounds

Return type:

SphericalGridBase

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

get_image_data(data, *, performance_goal='speed', fill_value=0, masked=True)[source]

return a 2d-image of the data

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

  • performance_goal (str) – Determines the method chosen for interpolation. Possible options are speed and quality.

  • fill_value (float) – The value assigned to invalid positions (those inside the hole or outside the region).

  • masked (bool) – Whether a numpy.ma.MaskedArray is returned for the data instead of the normal ndarray.

Returns:

dict: 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

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

property radius: float | tuple[float, float]

radius of the sphere

Type:

float

property state: dict[str, Any]

the state of the grid

Type:

state

property volume: float

total volume of the grid

Type:

float

volume_from_radius(radius, dim)[source]

Return the volume of a sphere with a given radius

Parameters:
  • radius (float or ndarray) – Radius of the sphere

  • dim (int) – Dimension of the space

Returns:

Volume of the sphere

Return type:

float or ndarray