Skip to content

[WIP] Check for consistency of passing array_ns in functions#1178

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/check-consistency-of-array-ns
Draft

[WIP] Check for consistency of passing array_ns in functions#1178
Copilot wants to merge 5 commits intomainfrom
copilot/check-consistency-of-array-ns

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 30, 2026

Summary

Refactored functions that receive input numpy/cupy arrays to derive array_ns from those inputs using data_alloc.array_namespace() instead of requiring it as a parameter. Functions that only produce arrays (no suitable input arrays) retain the array_ns parameter.

Completed

  • grid/topography.py: compute_nabla2_on_cell, smooth_topography — derive from input arrays
  • initialization/topography.py: jablonowski_williamson — derive from cell_lat
  • math/projection.py: gnomonic_proj — derive from lon_c
  • metrics/compute_coeff_gradekin.py: compute_coeff_gradekin — derive from edge_cell_length
  • grid/geometry_stencils.py: compute_primal_cart_normal — derive from primal_cart_normal_x
  • metrics/compute_diffusion_metrics.py: 4 functions — derive from first NDArray input
  • metrics/compute_weight_factors.py: compute_wgtfacq_c_dsl, compute_wgtfacq_e_dsl
  • metrics/compute_zdiff_gradp.py: compute_zdiff_gradp
  • metrics/metric_fields.py: compute_flat_max_idx, compute_nflat_gradp, compute_exner_w_implicit_weight_parameter
  • grid/base.py: _replace_skip_values, _has_skip_values_in_table
  • grid/grid_refinement.py: compute_domain_bounds, is_unordered_field, convert_to_non_nested_refinement_values, is_limited_area_grid
  • grid/grid_manager.py: _get_derived_connectivities, _construct_diamond_vertices, _determine_center_position, _construct_diamond_edges, _construct_triangle_edges, _construct_butterfly_cells, _patch_with_dummy_lastline, _get_local_connectivities
  • grid/vertical.py: _compute_SLEVE_coordinate_from_vcta_and_topography, _check_and_correct_layer_thickness, _check_flatness_of_flat_level, compute_vertical_coordinate
  • decomposition/definitions.py: Reductions protocol + SingleNodeReductions (min/max/sum/mean)
  • decomposition/halo.py: global_to_local
  • decomposition/mpi_decomposition.py: _reduce, _calc_buffer_size, min, max, sum, mean
  • grid/geometry.py: Updated reductions callers
  • metrics/metrics_factory.py: Updated all callers
  • Updated corresponding test files to remove array_ns arguments

Functions that correctly KEEP array_ns (only output is an array)

  • _get_index_field in grid_manager.py (input is file name, not array)
  • _create_dummy_decomposition_arrays in halo.py (input is size: int)
  • _min_identity, _max_identity, _sum_identity in mpi_decomposition.py (input is dtype)

Remaining (not yet done)

  • interpolation/interpolation_fields.py (~15 functions)
  • interpolation/rbf_interpolation.py (~12 functions)
  • driver/testcases/utils.py, driver/testcases/gauss3d.py
  • standalone_driver/testcases/utils.py, standalone_driver/driver_utils.py
  • tools/py2fgen/wrappers/common.py
  • Update remaining test callers (e.g. test_mpi_decomposition.py)

⌨️ Start Copilot coding agent tasks without leaving your editor — available in VS Code, Visual Studio, JetBrains IDEs and Eclipse.

@jenkins-apn
Copy link
Copy Markdown

Hi there, this is jenkins continuous integration...
Do you want me to verify this patch?

1 similar comment
@jenkins-apn
Copy link
Copy Markdown

Hi there, this is jenkins continuous integration...
Do you want me to verify this patch?

Copilot AI linked an issue Mar 30, 2026 that may be closed by this pull request
Copilot AI and others added 4 commits March 30, 2026 09:57
Remove the array_ns: ModuleType parameter from functions that already
receive NDArray inputs. Instead, derive the array namespace using
data_alloc.array_namespace() from the first input array.

Updated functions:
- grid/topography.py: compute_nabla2_on_cell, smooth_topography
- initialization/topography.py: jablonowski_williamson
- math/projection.py: gnomonic_proj
- metrics/compute_coeff_gradekin.py: compute_coeff_gradekin
- grid/geometry_stencils.py: compute_primal_cart_normal

Also updated all callers and removed now-unused imports.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Co-authored-by: jcanton <5622559+jcanton@users.noreply.github.com>
Remove array_ns: ModuleType = np parameters from functions that already
receive input NDArray arguments. These functions now derive the array
namespace internally using data_alloc.array_namespace() from their first
input array.

Functions modified:
- compute_max_nbhgt_array_ns (derives from c2e2c)
- _compute_k_start_end (derives from z_mc)
- compute_diffusion_mask_and_coef (derives from c2e2c)
- compute_diffusion_intcoef_and_vertoffset (derives from c2e2c)
- compute_wgtfacq_c_dsl (derives from z_ifc)
- compute_wgtfacq_e_dsl (derives from e2c)
- compute_zdiff_gradp (derives from z_mc)
- compute_flat_max_idx (derives from e2c)
- compute_nflat_gradp (derives from flat_idx_max)
- compute_exner_w_implicit_weight_parameter (derives from c2e)

Callers in metrics_factory.py and test files updated accordingly.
Unused imports (ModuleType, numpy) removed where no longer needed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Co-authored-by: jcanton <5622559+jcanton@users.noreply.github.com>
…put NDArrays

Remove array_ns: ModuleType = np parameters from functions that already
receive input NDArray arguments. These functions now derive the array
namespace from their input arrays using data_alloc.array_namespace().

Files modified:
- base.py: _replace_skip_values, _has_skip_values_in_table
- grid_refinement.py: compute_domain_bounds, is_unordered_field,
  convert_to_non_nested_refinement_values, is_limited_area_grid
- grid_manager.py: _get_derived_connectivities, _construct_diamond_vertices,
  _determine_center_position, _construct_diamond_edges,
  _construct_triangle_edges, _construct_butterfly_cells,
  _patch_with_dummy_lastline
- vertical.py: _compute_SLEVE_coordinate_from_vcta_and_topography,
  _check_and_correct_layer_thickness, _check_flatness_of_flat_level,
  compute_vertical_coordinate

All callers updated accordingly. Unused imports removed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Co-authored-by: jcanton <5622559+jcanton@users.noreply.github.com>
…put NDArrays

Remove array_ns: ModuleType = np parameters from functions that can derive
the array namespace from their input NDArray arguments using
data_alloc.array_namespace(). Updated functions:

- definitions.py: Reductions protocol and SingleNodeReductions methods
- halo.py: global_to_local()
- mpi_decomposition.py: _reduce, _calc_buffer_size, min, max, sum, mean
- grid_manager.py: _get_local_connectivities and its caller
- geometry.py: remove functools.partial wrapping for mean reductions
- metric_fields.py: update min_reduction callable signature and call

Functions without input arrays (_create_dummy_decomposition_arrays,
_min_identity, _max_identity, _sum_identity) retain array_ns parameter.

Note: Test files in decomposition/mpi_tests/ still pass array_ns to
reduction methods and need updating separately (MPI tests require
special infrastructure to run).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Co-authored-by: jcanton <5622559+jcanton@users.noreply.github.com>
@havogt
Copy link
Copy Markdown
Contributor

havogt commented Apr 1, 2026

Make sure the following scenario is covered: if you are on gpu, you might want to use cupy, but the starting point might be a numpy array after reading from somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Check for consistency of passing array_ns/backend

4 participants