coordinates.mixins.coords.CoordinateOperationsMixin.coordinate_meshgrid#
- CoordinateOperationsMixin.coordinate_meshgrid(*coordinate_arrays, axes: Sequence[str] | None = None, copy: bool = True, sparse: bool = False)[source]#
Construct a coordinate-aligned meshgrid from 1D coordinate arrays.
This method returns a tuple of N-D coordinate arrays aligned with the coordinate system’s canonical axis order, given a subset of 1D coordinate arrays. It reorders the inputs to match the canonical axis order and wraps NumPy’s
numpy.meshgrid()
.- Parameters:
coordinate_arrays (
array_like
) – 1-D arrays representing the coordinates of a grid.axes (
list
ofstr
, optional) – The axes present in the providedcoordinate_arrays
. IfNone
(default), then the firstlen(coordinate_arrays)
axes of the coordinate system will be used. Otherwise, the provided axes will be used to re-order the coordinates into canonical order before creating the mesh.sparse (
bool
, optional) –If True the shape of the returned coordinate array for dimension i is reduced from
(N1, ..., Ni, ... Nn)
to(1, ..., 1, Ni, 1, ..., 1)
. These sparse coordinate grids are intended to be use with Broadcasting. When all coordinates are used in an expression, broadcasting still leads to a fully-dimensional result array.Default is False.
copy (
bool
, optional) – If False, a view into the original arrays are returned in order to conserve memory. Default is True. Please note thatsparse=False, copy=False
will likely return non-contiguous arrays. Furthermore, more than one element of a broadcast array may refer to a single memory location. If you need to write to the arrays, make copies first.
- Returns:
A tuple of N-D arrays suitable for evaluating vectorized fields over a structured grid.
- Return type:
tuple
ofnp.ndarray
- Raises:
ValueError – If axes are invalid or their count does not match coordinate_arrays.
Examples
Create a grid in spherical coordinates composed of \(r\) and \(\theta\) coordinates.
>>> from pymetric.coordinates import SphericalCoordinateSystem >>> from pymetric.utilities.logging import pg_log >>> pg_log.level = 50 >>> cs = SphericalCoordinateSystem() >>> r = np.linspace(0, 1, 10) >>> theta = np.linspace(0, np.pi, 20) >>> rgrid, thetagrid = cs.coordinate_meshgrid(r, theta, axes=["r", "theta"]) >>> rgrid.shape, thetagrid.shape ((10, 20), (10, 20))
Axes may also be supplied out of order
>>> cs = SphericalCoordinateSystem() >>> r = np.linspace(0, 1, 10) >>> theta = np.linspace(0, np.pi, 20) >>> rgrid, thetagrid = cs.coordinate_meshgrid(theta,r, axes=["theta","r"]) >>> rgrid.shape, thetagrid.shape ((10, 20), (10, 20))
If an invalid axis is specified, then an error occurs.
>>> cs = SphericalCoordinateSystem() >>> r = np.linspace(0, 1, 10) >>> theta = np.linspace(0, np.pi, 20) >>> rgrid, thetagrid = cs.coordinate_meshgrid(theta,r, axes=["theta","psi"]) ValueError: Unknown axis/axes ['psi'] – valid axes are ['r', 'theta', 'phi']