fluidsim.operators.spatial_average3d

Spatial radial and azimuthal average in 3D (fluidsim.operator.spatial_average3d)

Provides:

class fluidsim.operators.spatial_average3d.SpatialAverage(oper, dr=1.0, drh=1.0, dz=1.0, shift_origin=True)[source]

Bases: object

Compute spatial average on a field.

Parameters:
operOperator

Operator object containing the mesh information (X, Y, Z coordinates). The mesh is assumed uniform with the same grid spacing dx=dy=dz in all directions, but the domain lengths Lx, Ly, Lz (and therefore the number of grid points Nx, Ny, Nz) can differ.

drfloat, optional

Radial bin size factor (default: 2.0)

drhfloat, optional

Azimuthal rho bin size factor (default: 2.0)

dzfloat, optional

Azimuthal z bin size factor (default: 1.0)

shift_originbool, optional

If True, shift origin to domain center (default: True)

Notes

The continuous form of the radial average of field \(f\) over a sphere \(\Omega\) of radius \(r\) with is:

\[\langle f \rangle_{\Omega}^{continuous}(r) = 1/(4\pi) \int f(r,\theta,\phi) sin(\phi) \mathop{d\theta} \mathop{d\phi}.\]

Its discrete form in a 3D domain of uniform cubic cells of sizes \(\mathop{dx} \times \mathop{dy} \times \mathop{dz}\) is approximated at radius \(r\) as:

\[\langle f \rangle_{\Omega}^{discrete}(r) = \frac{\text{Sum}_f(r)}{\text{Weight}_f(r)},\]

with

\[\begin{split}\text{Sum}_f(r) = \sum_{\substack{i,j,k \\ r \leq r_i < r + \Delta r \\ r - \Delta r \leq r_{i-1} < r}} f_{i,j,k}(r_i,\theta_j,\phi_k)\left(1 - \frac{r_i - r}{\Delta r}\right) + f_{i-1,j,k}(r_{i-1},\theta_j,\phi_k)\frac{r_{i-1} - r}{\Delta r},\end{split}\]

and

\[\begin{split}\text{Weight}_f(r) = \sum_{\substack{i,j,k \\ r \leq r_i < r + \Delta r \\ r - \Delta r \leq r_{i-1} < r}} \mathbf{1}_{i,j,k}(r_i,\theta_j,\phi_k) \left(1 - \frac{r_i - r}{\Delta r}\right) + \mathbf{1}_{i-1,j,k}(r_{i-1},\theta_j,\phi_k)\frac{r_{i-1} - r}{\Delta r}.\end{split}\]

Where \(\mathbf{1}\) is a 3D field of values \(1\) and \(\Delta r = \mathop{dr}\text{min}(\mathop{dx}, \mathop{dy}, \mathop{dz})\) is the radius step (worth \(\mathop{dx} = \mathop{dy} = \mathop{dz}\) by default).

The continuous form of the azimuthal average of field \(f\) over circles of radius \(\rho\) at each height \(z\) is:

\[\langle f \rangle_{\theta}(\rho, z) = 1/(2\pi) \int f(\rho,\theta,z) \mathop{d\theta}.\]

Its discrete form in a 3D domain of uniform cubic cells is approximated as:

\[\langle f \rangle_{\theta}^{discrete}(\rho, z) = \frac{\text{Sum}_f(\rho, z)}{\text{Weight}_f(\rho, z)},\]

with

\[\begin{split}\text{Sum}_f(\rho, z) = \sum_{\substack{i,j,k \\ z \leq z_k < z + \Delta z \\ \rho \leq \rho_i < \rho + \Delta \rho \\ \rho - \Delta \rho \leq \rho_{i-1} < \rho}} f_{i,j,k}(\rho_i,\theta_j,z_k)\left(1 - \frac{\rho_i - \rho}{\Delta \rho}\right) + f_{i-1,j,k}(\rho_{i-1},\theta_j,z_k)\frac{\rho_{i-1} - \rho}{\Delta \rho},\end{split}\]

and

\[\begin{split}\text{Weight}_f(\rho, z) = \sum_{\substack{i,j,k \\ z \leq z_k < z + \Delta z \\ \rho \leq r_i < \rho + \Delta \rho \\ \rho - \Delta \rho \leq \rho_{i-1} < \rho}} \mathbf{1}_{i,j,k}(\rho_i,\theta_j,z_k) \left(1 - \frac{\rho_i - \rho}{\Delta \rho}\right) + \mathbf{1}_{i-1,j,k}(\rho_{i-1},\theta_j,z_k)\frac{\rho_{i-1} - \rho}{\Delta \rho}.\end{split}\]

Where \(\Delta \rho = \mathop{drh}\text{min}(\mathop{dx}, \mathop{dy}, \mathop{dz})\) and \(\Delta z = \widetilde{\mathop{dz}}\text{min}(\mathop{dx}, \mathop{dy}, \mathop{dz})\) (\(\widetilde{\mathop{dz}}\) is an input parameter, ratio of the required step by the step from the grid) the horizontal radius and vertical steps (both worth \(\mathop{dx} = \mathop{dy} = \mathop{dz}\) by default).

_compute_coordinates()[source]

Compute cylindrical and spherical coordinate arrays

Notes

Uses CoordSystem3DConverter to compute \(\rho\) and \(r\).

_prepare_radial_bins()[source]

Prepare bins for radial averaging.

Notes

Creates uniformly spaced bin at \(\Delta r = \mathop{dr}\text{min}(\mathop{dx}, \mathop{dy}, \mathop{dz})\) centers spanning [r_min, r_max] globally.

_prepare_azimuthal_bins()[source]

Prepare bins for azimuthal averaging

Notes

Creates uniformly spaced bin centers for \(\rho\) and \(z\).

_compute_weights()[source]

Compute the total weight (count) in each bin across all processes.

Notes

This computes the normalization factor for averaging. For radial average: counts points in each spherical shell. For azimuthal average: counts points in each \((\rho, z)\) bin.

compute_radial_average(field, return_std=False)[source]

Compute the solid-angle average of a scalar or vector field

Parameters:
fieldarray_like

Scalar field of shape (Nx_loc, Ny_loc, Nz_loc) or vector field of shape (3, Nx_loc, Ny_loc, Nz_loc) in the spherical basis. Each MPI process provides its local slice.

return_stdbool, optional

If True, also return the standard deviation (default: False).

Returns:
r_centersndarray, shape (nr,)

Radial bin centres (same on all processes).

field_avgndarray, shape (nr,) or (3, nr)

Solid-angle average in each bin (same on all processes).

field_stdndarray, same shape as field_avg (only if return_std=True)

Standard deviation in each bin (same on all processes).

_radial_average_scalar(field, return_std=False)[source]

Average over radial bins for a scalar field using loop_spherical.

Parameters:
fieldndarray, shape (Nx_loc, Ny_loc, Nz_loc)

Local field slice on this process.

return_stdbool
Returns:
field_avgndarray, shape (nr,)

Global average across all processes.

field_stdndarray, shape (nr,) — only if return_std is True
compute_azimuthal_average(field, return_std=False)[source]

Compute the azimuthal average of a scalar or vector field

Parameters:
fieldarray_like

Scalar field of shape (Nx_loc, Ny_loc, Nz_loc) or vector field of shape (3, Nx_loc, Ny_loc, Nz_loc) in the cylindrical basis. Each MPI process provides its local slice.

return_stdbool, optional

If True, also return the standard deviation (default: False).

Returns:
rho_centersndarray, shape (nrh,)

Bin centres in rho (same on all processes).

z_centersndarray, shape (nz,)

Bin centres in z (same on all processes).

field_avgndarray, shape (nz, nrh) or (3, nz, nrh)

Azimuthal average in each (rho, z) bin (same on all processes).

field_stdndarray, same shape as field_avg (only if return_std=True)
_azimuthal_average_scalar(field, return_std=False)[source]

Average over \((\rho, z)\) bins for a scalar field using loop_azimuthal_rhrz.

Parameters:
fieldndarray, shape (Nx_loc, Ny_loc, Nz_loc)

Local field slice on this process.

return_stdbool
Returns:
field_avgndarray, shape (nz, nrh)

Global average across all processes.

field_stdndarray, shape (nz, nrh) — only if return_std is True
compute_volume_weights()[source]

Compute the volume weight of each mesh cell

Returns:
Volumesndarray, shape (Nx_loc, Ny_loc, Nz_loc)

Volume weights on this process.

Volumesndarray, shape (Nx_loc, Ny_loc, Nz_loc)

Volume weights on this process.

Notes

For a uniform isotropic mesh \((\mathop{dx} = \mathop{dy} = \mathop{dz} = \mathop{d})\), all cells have the same volume \(\mathop{d}^3\). The grid spacing is read from the operator if available.

Functions

loop_azimuthal_rhrz(field_to_avg, rho_list, ...)

Compute the _z-rh field summed over theta.

loop_spherical(arr_r0r1r2, rs, R2)

Compute the radial field summed over theta and phi.

Classes

SpatialAverage(oper[, dr, drh, dz, shift_origin])

Compute spatial average on a field.