field moduleο
- class wakis.field.Field(Nx, Ny, Nz, dtype=float, use_ones=False, use_gpu=False)[source]ο
Bases:
objectClass to handle 3D vector fields stored in a flattened 1D array. Uses lexicographic numbering:
n = 1 + (i-1) + (j-1)*Nx + (k-1)*Nx*Ny len(n) = Nx*Ny*Nz
- Parameters
Nx (int) β Number of grid points in the x direction.
Ny (int) β Number of grid points in the y direction.
Nz (int) β Number of grid points in the z direction.
dtype (type, optional) β Data type of the field array. Default is float.
use_ones (bool, optional) β If True, initialize the field array with ones. Otherwise, zeros. Default is False.
use_gpu (bool, optional) β If True, use CuPy for GPU arrays. Default is False.
- Nx, Ny, Nz
Grid dimensions.
- Type
int
- Nο
Total number of grid points (Nx * Ny * Nz).
- Type
int
- dtypeο
Data type of the field array.
- Type
type
- on_gpuο
Whether the field is stored on GPU.
- Type
bool
- xpο
Numpy or cupy module, depending on use_gpu.
- Type
module
- arrayο
Flattened 1D field array of shape (N*3,).
- Type
ndarray
- compute_ijk(n)[source]ο
Compute (i, j, k) indices from a lexico-graphic index.
- Parameters
n (int) β Lexico-graphic index.
- Returns
i, j, k β 3D indices corresponding to the lexico-graphic index.
- Return type
int
- copy()[source]ο
Return a deep copy of the Field object.
- Returns
Deep copy of the field.
- Return type
- property field_xο
Return the x-component of the field as a 1D array.
- property field_yο
Return the y-component of the field as a 1D array.
- property field_zο
Return the z-component of the field as a 1D array.
- from_matrix(mat, key)[source]ο
Set the specified component from a 3D matrix.
- Parameters
mat (ndarray) β 3D array of shape (Nx, Ny, Nz).
key (int or str) β Component to set: 0 or βxβ, 1 or βyβ, 2 or βzβ.
- fromarray(array)[source]ο
Set the field array from a flattened array.
- Parameters
array (ndarray) β Flattened field array of shape (N*3,).
- get_abs(as_matrix=True)[source]ο
Compute the magnitude of the field.
- Parameters
as_matrix (bool, optional) β If True, return as a 3D matrix. If False, return as a 1D array.
- Returns
abs_field β Magnitude of the field.
- Return type
ndarray
- inspect(plane='YZ', cmap='bwr', dpi=100, figsize=[8, 6], x=None, y=None, z=None, off_screen=False, handles=False, **kwargs)[source]ο
Visualize 2D slices of the field components using matplotlib.
- Parameters
plane ({'XY', 'XZ', 'YZ'}, optional) β Plane to visualize. Default is βYZβ.
cmap (str, optional) β Colormap for the plot. Default is βbwrβ.
dpi (int, optional) β Figure DPI. Default is 100.
figsize (list, optional) β Figure size. Default is [8, 6].
x (int, slice, or None, optional) β Custom slice indices. If all are not None, use as custom slice.
y (int, slice, or None, optional) β Custom slice indices. If all are not None, use as custom slice.
z (int, slice, or None, optional) β Custom slice indices. If all are not None, use as custom slice.
off_screen (bool, optional) β If True, display the plot off-screen. Default is False.
handles (bool, optional) β If True, return (fig, axs) instead of showing. Default is False.
**kwargs β Additional keyword arguments for imshow.
- Returns
fig, axs (tuple, optional) β Returned if handles=True.
None β Otherwise.
- inspect3D(field='all', backend='pyista', grid=None, xmax=None, ymax=None, zmax=None, bounding_box=True, show_grid=True, cmap='viridis', dpi=100, off_screen=False, handles=False)[source]ο
Visualize 3D field data on the structured grid using either Matplotlib (voxel rendering) or PyVista (interactive clipping and slicing).
This method provides two complementary visualization backends: - Matplotlib: static voxel plots of the field components (x, y, z)
or all combined, useful for quick inspection, but memory intensive.
PyVista: interactive 3D visualization with sliders to dynamically clip the volume along X, Y, and Z, and optional wireframe slices.
- Parameters
field ({'x', 'y', 'z', 'all'}, optional) β Which field component(s) to visualize. Default is βallβ.
backend ({'matplotlib', 'pyvista'}, optional) β Visualization backend to use. Default is βpyvistaβ.
grid (object, optional) β Structured grid object to use for visualization. If None, a grid is constructed from the solverβs internal dimensions.
xmax (int or float, optional) β Maximum extents in each direction for visualization. Defaults to the full grid dimensions if not specified.
ymax (int or float, optional) β Maximum extents in each direction for visualization. Defaults to the full grid dimensions if not specified.
zmax (int or float, optional) β Maximum extents in each direction for visualization. Defaults to the full grid dimensions if not specified.
bounding_box (bool, optional) β If True, draw a wireframe bounding box of the simulation domain (only used in PyVista backend). Default is True.
show_grid (bool, optional) β If True, show wireframe slice planes of the grid during interactive visualization (PyVista backend). Default is True.
cmap (str, optional) β Colormap to apply to the scalar field. Default is βviridisβ.
dpi (int, optional) β Resolution of Matplotlib figures (only for Matplotlib backend). Default is 100.
show (bool, optional) β Whether to display the figure/plot immediately. If False in PyVista, exports to field.html instead. Default is True.
handles (bool, optional) β If True, return figure/axes (Matplotlib) or the Plotter object (PyVista) for further customization instead of showing directly. Default is False.
- Returns
fig, axs (tuple, optional) β Returned when backend=βmatplotlibβ and handles=True.
pl (pyvista.Plotter, optional) β Returned when backend=βpyvistaβ and handles=True.
Notes
The PyVista backend provides interactive sliders to clip the volume along each axis independently and inspect internal structures of the 3D field.
The Matplotlib backend provides a quick static voxel rendering but is limited in interactivity and scalability.