Mondaic

salvus.mesh.data_structures.salvus_mesh.algorithms.fields

salvus.mesh.data_structures.salvus_mesh.algorithms.fields salvus mesh data_structures salvus_mesh algorithms fields

Helper functions for field handling of mesh blocks.

Functions

choose_int_dtype()

def choose_int_dtype(
    arr: npt.NDArray, validate_whole: bool = False
) -> np.dtype: ...

Choose the smallest integer dtype that can represent the array.

Prefer unsigned dtypes for arrays that are non-negative (connectivity, global point ids, etc.) to further reduce storage. Falls back to signed types when negative values are present.

If an array of size zero is given, np.int32 is returned.

Parameters
  • arr npt.NDArray — The array to choose the dtype for.
  • validate_whole bool — If True, validate that all values in the array are whole numbers (integers). Floats are allowed if they are whole numbers.
Returns np.dtype — The chosen numpy integer dtype.

collapse_constant_nodal_fields()

def collapse_constant_nodal_fields(
    mesh: MeshType, field_name: str | None = None
) -> MeshType: ...

Collapse constant nodal fields to scalars, in-place.

If mesh is a Mesh, operate on all blocks. If mesh is a MeshBlock, operate on that block. If field_name is given, only collapse that field; otherwise, collapse all nodal fields.

Parameters
  • mesh MeshType — The Mesh or MeshBlock to operate on.
  • field_name str | None — If provided, only collapse this nodal field.
Returns MeshType — The same Mesh or MeshBlock, with constant nodal fields collapsed to scalars.

collect_elemental_field_arrays()

def collect_elemental_field_arrays(
    blocks: tuple[MeshBlock, ...],
    field_name: str,
    fill_value: types.scalar_ | None = None,
    output_dtype: np.dtype | None = None,
) -> tuple[list[npt.NDArray], np.dtype]: ...

Collect and prepare per-block elemental field arrays for global assembly.

Parameters
  • blocks tuple[MeshBlock, ...] — List of MeshBlock objects.
  • field_name str — Name of the field to collect.
  • fill_value types.scalar_ | None — Value to fill missing blocks.
  • output_dtype np.dtype | None — Desired numpy dtype for output arrays.
Returns tuple[list[npt.NDArray], np.dtype] — A tuple of (prepared arrays list, output dtype). The prepared arrays list contains one array per block, with missing blocks filled with fill_value and all arrays cast to output_dtype. The output dtype is the common dtype used for all arrays.

collect_nodal_field_arrays()

def collect_nodal_field_arrays(
    blocks: tuple[MeshBlock, ...],
    field_name: str,
    fill_value: types.scalar_ | None = None,
    output_dtype: np.dtype | None = None,
) -> tuple[list[npt.NDArray], np.dtype, npt.NDArray]: ...

Collect and prepare per-block nodal field arrays for global assembly.

Parameters
  • blocks tuple[MeshBlock, ...] — List of MeshBlock objects.
  • field_name str — Name of the field to collect.
  • fill_value types.scalar_ | None — Value to fill missing blocks.
  • output_dtype np.dtype | None — Desired numpy dtype for output arrays.
Returns tuple[list[npt.NDArray], np.dtype, npt.NDArray] — A tuple of (prepared arrays list, output dtype, index mapping (section)). The prepared arrays list contains one array per block, with missing blocks filled with fill_value and all arrays cast to output_dtype. The output dtype is the common dtype used for all arrays. The index mapping (section) is a 1D array of global indices corresponding to each entry.

determine_common_dtype()

def determine_common_dtype(dtypes: list[np.dtype]) -> np.dtype: ...

Determine the common dtype for arrays, handling special cases.

This helper function determines the appropriate common dtype for a collection of arrays, taking into account the default value, potential upcasting needs, and explicit output dtype requirements.

Parameters
  • dtypes list[np.dtype] — List of dtypes from the arrays to be combined.
Returns np.dtype — The common dtype to use for the combined arrays.

expand_scalar_nodal_fields()

def expand_scalar_nodal_fields(
    mesh: MeshType, field_name: str | None = None
) -> MeshType: ...

Expand scalar nodal fields back to arrays, in-place.

This is the reverse operation of collapse_constant_nodal_fields. Any nodal field stored as a scalar will be broadcast to an array of length number_of_points of its block. Existing array-valued nodal fields are left untouched.

Parameters
  • mesh MeshType — The Mesh or MeshBlock to operate on.
  • field_name str | None — If provided, only expand this nodal field; otherwise all scalar nodal fields are expanded.
Returns MeshType — The same Mesh or MeshBlock with scalar nodal fields expanded to arrays.