magoptlib.sh_gpu

Functions

normalization_gpu(l, m)

Compute the normalization constant for real spherical harmonics on the GPU.

derivative_plm_gpu(l, m, theta[, eps])

Compute the derivative of the associated Legendre polynomial P_l^m(cosθ) with respect to θ.

real_sh_basis_gpu(l, m, theta, phi, a, r)

Compute the three parts of the real spherical‐harmonic

compute_sh_matrix_gpu(l_vals, m_vals, phi, theta, a, r)

Construct the three SH basis matrices.

cart2sph_gpu(x, y, z[, eps])

Convert Cartesian coordinates to spherical coordinates on the GPU.

sph2cart_gpu(theta, phi)

Generate local spherical-to-Cartesian transformation matrices for given angles.

get_orientations_gpu(delta)

Module Contents

magoptlib.sh_gpu.normalization_gpu(l, m)

Compute the normalization constant for real spherical harmonics on the GPU.

N_{lm} = sqrt((2l+1)/(4π) * (l-|m|)!/(l+|m|)!)

Parameters:
  • l (int) – Degree of the spherical harmonic.

  • m (int) – Order of the spherical harmonic.

Returns:

The normalization constant as a CuPy scalar.

Return type:

cp.ndarray (scalar)

magoptlib.sh_gpu.derivative_plm_gpu(l, m, theta, eps=1e-10)

Compute the derivative of the associated Legendre polynomial P_l^m(cosθ) with respect to θ.

Uses the recurrence:

dP/dθ = -sinθ * dP/dx, where x = cosθ

Parameters:
  • l (int) – Degree of the Legendre polynomial.

  • m (int) – Order of the Legendre polynomial.

  • theta (cupy.ndarray) – Polar angles (radians).

  • eps (float, optional) – Tolerance for detecting singularities at poles (default is 1e-10).

Returns:

The derivative d/dθ of the associated Legendre function evaluated at theta.

Return type:

cupy.ndarray

magoptlib.sh_gpu.real_sh_basis_gpu(l, m, theta, phi, a, r)

Compute the three parts of the real spherical‐harmonic expansion for degree l, order m, at angles theta,phi and radii a (source), r (interest point).

Parameters:
  • l (int) – Degree of the SH.

  • m (int) – Order of the SH.

  • theta (cupy.ndarray) – Polar angles (radians).

  • phi (cupy.ndarray) – Azimuthal angles (radians).

  • a (float or cupy.ndarray) – Reference radius for normalization.

  • r (cupy.ndarray) – Radius at each evaluation point.

Returns:

  • radial_part (cupy.ndarray) – radial basis times (a/r)^(l+2)

  • theta_part (cupy.ndarray) – d/dθ basis times (a/r)^(l+2)

  • phi_part (cupy.ndarray) – (1/sinθ)·(d/dφ) basis times (a/r)^(l+2)

magoptlib.sh_gpu.compute_sh_matrix_gpu(l_vals, m_vals, phi, theta, a, r)

Construct the three SH basis matrices.

Parameters:
  • l_vals (np.ndarray) – Degrees of the SH basis functions.

  • m_vals (np.ndarray) – Orders of the SH basis functions.

  • phi (cupy.ndarray) – Azimuthal angles (radians).

  • theta (cupy.ndarray) – Polar angles (radians).

  • a (float or cupy.ndarray) – Reference radius.

  • r (cupy.ndarray) – Actual radius at each point.

Returns:

  • Zp (cupy.ndarray) – Radial SH basis matrix, shape (Np, Nm).

  • Xp (cupy.ndarray) – Theta SH basis matrix, shape (Np, Nm).

  • Yp (cupy.ndarray) – Phi SH basis matrix, shape (Np, Nm).

magoptlib.sh_gpu.cart2sph_gpu(x, y, z, eps=1e-12)

Convert Cartesian coordinates to spherical coordinates on the GPU.

Parameters:
  • x (cupy.ndarray) – Cartesian coordinates.

  • y (cupy.ndarray) – Cartesian coordinates.

  • z (cupy.ndarray) – Cartesian coordinates.

  • eps (float, optional) – Tolerance to avoid division by zero.

Returns:

  • r (cupy.ndarray) – Radius.

  • theta (cupy.ndarray) – Polar angle (radians).

  • phi (cupy.ndarray) – Azimuthal angle (radians).

magoptlib.sh_gpu.sph2cart_gpu(theta, phi)

Generate local spherical-to-Cartesian transformation matrices for given angles.

Parameters:
  • theta (cupy.ndarray) – Polar angles (radians).

  • phi (cupy.ndarray) – Azimuthal angles (radians).

Returns:

M – Transformation matrices of shape (N, 3, 3).

Return type:

cupy.ndarray

magoptlib.sh_gpu.get_orientations_gpu(delta)

” Construct orientation matrices for a Halbach ring based on angles delta.

Each orientation is defined as Rz(delta) * Ry(90°), rotating the local coordinate system.

Parameters:

delta (cupy.ndarray) – Array of angles (radians) defining each magnet’s rotation about z.

Returns:

  • orientations (cupy.ndarray) – Orientation matrices of shape (d, 3, 3), where d is the number of magnets.

  • Rz[i] = – [ c, -s, 0] [ s, c, 0] [ 0, 0, 1]