remesh_array

Contents

remesh_array#

remesh_array(data, imesh, omesh=None, order='F')[source]#

Shift an array from one mesh stagger to another.

Compares the source mesh imesh against the target mesh omesh axis by axis and applies adjacent-element averaging on every axis that needs to move from half mesh to main mesh. Only the half → main direction is supported; requesting main → half raises ValueError.

This is commonly needed before performing interpolation or arithmetic between quantities on different mesh positions. For example, computing the magnitude of the magnetic field requires \(B_r\), \(B_\theta\), and \(B_\varphi\) on the same mesh: each must be remeshed from its native stagger (0b100, 0b010, 0b001) to a common target before squaring and summing.

If omesh is None, the array is returned unchanged.

Parameters:
datanp.ndarray

Input array on the stagger described by imesh.

imeshMeshCodeType

Source mesh stagger in any form accepted by MeshCodeType.

omeshMeshCodeType | None, optional

Target mesh stagger. None (default) is a no-op. Pass 0 or 'main' to move every half-mesh axis to the main mesh.

orderArrayOrdering, optional

Memory-order convention controlling how mesh-code bits map to numpy axes; see ArrayOrdering. Defaults to 'F' (Fortran / PSI HDF column-major: last numpy axis = \(r\)).

Returns:
outnp.ndarray

Array on the target mesh. Each remeshed axis is reduced by one element via adjacent averaging; axes that already match are left unchanged.

Raises:
ValueError

If any axis in omesh requests half mesh where imesh is already main (upsampling is not supported).

See also

Mesh

Compact representation of a multi-axis mesh stagger code.

MeshCodeType

Accepted forms for mesh stagger specifications.

ArrayOrdering

Memory-order convention for bit-axis mapping.

Examples

Convert a radial magnetic-field array (half-mesh in \(r\), the last numpy axis) to the all-main mesh:

>>> import numpy as np
>>> from psi_io.mesh import remesh_array
>>> br = np.ones((128, 64, 57))   # shape (Nφ, Nθ, Nr); Nr is half-mesh size
>>> br_main = remesh_array(br, imesh=0b100, omesh='main')
>>> br_main.shape
(128, 64, 56)

Remesh a scalar quantity (all-half, 0b111) to all-main:

>>> rho = np.ones((128, 64, 57))
>>> remesh_array(rho, imesh=0b111, omesh='main').shape
(127, 63, 56)

omesh=None is a no-op:

>>> remesh_array(br, imesh=0b100).shape
(128, 64, 57)

No-op when source and target stagger already match:

>>> remesh_array(br, imesh=0b100, omesh=0b100).shape
(128, 64, 57)