Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metrics fields factory #626

Open
wants to merge 179 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
179 commits
Select commit Hold shift + click to select a range
51bd673
WIP
halungge Jun 27, 2024
2270522
add backend to metric_fields stencils
halungge Jun 27, 2024
0bf8d18
ugly version that works for gtfn programs
halungge Jun 27, 2024
b78b24f
use operator.add instead of lambda
halungge Jul 1, 2024
faa931a
merge main
halungge Aug 8, 2024
5836a32
reduce dependencies, move ProgramFieldProvider out of Factory
halungge Aug 8, 2024
6f3e6c6
rename fields
halungge Aug 9, 2024
6d99a17
Merge branch 'main' into prototype_fields_factory
halungge Aug 14, 2024
21c744b
move factory.py to states package
halungge Aug 15, 2024
bf7dc7e
remove duplicated computation of wgtfacq_c_dsl
halungge Aug 15, 2024
d07fef2
fix type annotations for arrays
halungge Aug 15, 2024
8bb63f6
add type annotations to compute_vwind_impl_wgt.py
halungge Aug 15, 2024
a9b0b54
FieldProvider for numpy functions (WIP I)
halungge Aug 16, 2024
dc809e8
merge main
halungge Aug 16, 2024
ffb4661
first version for numpy functions
halungge Aug 16, 2024
9f042b1
fix: move _unallocated to ProgramFieldProvider
halungge Aug 20, 2024
809f060
move joint functionality into FieldProvider
halungge Aug 20, 2024
bcd65b5
- switch to device dependent import in compute_wgtfacq.py
halungge Aug 20, 2024
52a837d
add type annotation to connectivity
halungge Aug 21, 2024
72e742b
handle numpy field with connectivity
halungge Aug 21, 2024
d93c570
merge main
halungge Aug 27, 2024
fba0891
add type to get_processor_properties argument
halungge Aug 28, 2024
c2c250a
add c_lin_e metadata
halungge Aug 28, 2024
04645e0
start_index, end_index abstraction for vertical (WIP)
halungge Aug 28, 2024
306b761
basic sample of factory.
halungge Aug 28, 2024
f5d03f9
intial implementation for metrics fields factory
nfarabullini Aug 28, 2024
717fa5f
some more metrics fields factory
nfarabullini Aug 29, 2024
cec01f9
fix with_allocator function
halungge Aug 29, 2024
28c0be3
Merge branch 'prototype_fields_factory' of https://github.com/C2SM/ic…
nfarabullini Aug 29, 2024
45bff38
some more metrics fields factory
nfarabullini Aug 29, 2024
bb2500b
update with upstrean
nfarabullini Sep 3, 2024
57bf95d
update with upstrean
nfarabullini Sep 3, 2024
966abbd
update with upstrean
nfarabullini Sep 3, 2024
a417ba8
update with prototype_fields_factory
nfarabullini Sep 3, 2024
aa2c402
ran pre-commit and made fixes
nfarabullini Sep 3, 2024
9acd188
other fixes
nfarabullini Sep 3, 2024
afe3f47
small edit
nfarabullini Sep 3, 2024
ee11821
Merge branch 'prototype_fields_factory' into metrics_fields_factory
nfarabullini Sep 3, 2024
64640e1
small fixes
nfarabullini Sep 3, 2024
cd50cc2
Merge branch 'main' into metrics_fields_factory
nfarabullini Sep 3, 2024
b95e8bf
Merge branch 'metrics_fields_factory' of https://github.com/C2SM/icon…
nfarabullini Sep 4, 2024
07841d9
more metrics_fields
nfarabullini Sep 5, 2024
df8ba00
Merge branch 'main' into prototype_fields_factory
halungge Sep 5, 2024
8f8d8de
using domains for the compute domain in factory
halungge Sep 5, 2024
eb58de0
Merge branch 'prototype_fields_factory' of https://github.com/C2SM/ic…
nfarabullini Sep 6, 2024
d8bcebd
Merge branch 'prototype_fields_factory' into metrics_fields_factory
nfarabullini Sep 6, 2024
dc7928e
Merge branch 'metrics_fields_factory' of https://github.com/C2SM/icon…
nfarabullini Sep 6, 2024
a0b0876
Update model/common/src/icon4py/model/common/metrics/metrics_factory.py
nfarabullini Sep 6, 2024
fcc293f
Merge branch 'metrics_fields_factory' of https://github.com/C2SM/icon…
nfarabullini Sep 6, 2024
85bdff4
edits following review
nfarabullini Sep 6, 2024
dba0dc2
added TODOs for future edits
nfarabullini Sep 6, 2024
70552d7
further factory fields implementation
nfarabullini Sep 9, 2024
0256d27
implementations in metadata file and cleanup
nfarabullini Sep 9, 2024
059dcaf
small edit
nfarabullini Sep 9, 2024
e1ec531
add docstring to Providers
halungge Sep 10, 2024
cac4c2f
partial fixes
nfarabullini Sep 11, 2024
20db7e5
Merge branch 'prototype_fields_factory' of https://github.com/C2SM/ic…
nfarabullini Sep 11, 2024
db0dbd6
further fixes and implementations
nfarabullini Sep 12, 2024
62c21ae
separate vertical and horizontal connectivities
halungge Sep 13, 2024
f98f8dc
pre-commit
halungge Sep 13, 2024
110bec6
Merge branch 'main' into prototype_fields_factory
halungge Sep 17, 2024
438f455
vwind_impl_wgt edits
nfarabullini Sep 17, 2024
c96444f
additional changes
nfarabullini Sep 18, 2024
ac6fca8
Merge branch 'prototype_fields_factory' of https://github.com/C2SM/ic…
nfarabullini Sep 18, 2024
792b8f6
fixes and cleanup
nfarabullini Sep 19, 2024
db4c0fb
additional cleanup
nfarabullini Sep 19, 2024
5687a27
Merge branch 'main' into metrics_fields_factory
nfarabullini Sep 19, 2024
82ac3e6
ran pre-commit
nfarabullini Sep 19, 2024
8b5bc49
additional edits from np to xp
nfarabullini Sep 19, 2024
0804ecc
small edit
nfarabullini Sep 19, 2024
8cdcea6
merge main
halungge Sep 19, 2024
3030cd1
update with upstream
nfarabullini Sep 19, 2024
f9f4e7d
installed tach
nfarabullini Sep 19, 2024
88a94b4
Merge branch 'main' of https://github.com/C2SM/icon4py into metrics_f…
nfarabullini Sep 23, 2024
a8e7c9f
fixed dims import
nfarabullini Sep 23, 2024
f111573
fixed small edit
nfarabullini Sep 23, 2024
506bcbd
some more fixes
nfarabullini Sep 23, 2024
659f5d9
some more fixes
nfarabullini Sep 24, 2024
1e4a20f
Merge branch 'main' into prototype_fields_factory
halungge Sep 24, 2024
ce9f559
Merge branch 'prototype_fields_factory' into metrics_fields_factory
nfarabullini Sep 24, 2024
65c5683
minor edits
nfarabullini Sep 24, 2024
251df09
move constants from cf_utils.py to metadata.py
halungge Sep 24, 2024
5fb868c
small edit
nfarabullini Sep 24, 2024
053f646
Merge branch 'metrics_fields_factory' of https://github.com/C2SM/icon…
nfarabullini Sep 24, 2024
e179a37
Merge branch 'main' of https://github.com/C2SM/icon4py into metrics_f…
nfarabullini Sep 26, 2024
ecdd514
some edits following merge
nfarabullini Sep 26, 2024
b7e51fb
small edit
nfarabullini Sep 26, 2024
8d16766
small edit to test
nfarabullini Sep 27, 2024
8d441ed
small edit to test
nfarabullini Sep 27, 2024
e697cc8
Merge branch 'main' into prototype_fields_factory
halungge Sep 27, 2024
8c7b782
Merge branch 'main' into prototype_fields_factory
halungge Oct 1, 2024
f1d2333
Merge branch 'prototype_fields_factory' of https://github.com/C2SM/ic…
nfarabullini Oct 1, 2024
e393419
small fix in test_factory
nfarabullini Oct 1, 2024
e417fd1
add types for metadata attributes
halungge Oct 2, 2024
75bda6d
fix int32 issues (ad hoc fix)
halungge Oct 2, 2024
f978d72
rename providers, fixes in FieldProvider Protocol
halungge Oct 2, 2024
e635e3d
add FieldSource Protocol
halungge Oct 3, 2024
fef2ced
fix doc strings, pre-commit
halungge Oct 3, 2024
1d7abf5
Merge branch 'main' into prototype_fields_factory
halungge Oct 4, 2024
21c529a
merge prototype_fields_factory
halungge Oct 4, 2024
02cce48
add documentation
halungge Oct 4, 2024
9fa73d4
Merge branch 'prototype_fields_factory' of https://github.com/C2SM/ic…
nfarabullini Oct 7, 2024
21169b5
edits following review
nfarabullini Oct 7, 2024
88e6baf
fixes to tests
nfarabullini Oct 7, 2024
39d2ee8
fixes to tests
nfarabullini Oct 7, 2024
598d453
added missing offset
nfarabullini Oct 7, 2024
bfd5fe9
small tests edits
nfarabullini Oct 8, 2024
a3a4774
removed z_ for maxslp_avg and maxhgtd_avg
nfarabullini Oct 8, 2024
60e5de7
removed if statements for cpu backend
nfarabullini Oct 8, 2024
086d6b8
removed experiment params and string check
nfarabullini Oct 8, 2024
9f8385b
small edit to hmask_dd3d
nfarabullini Oct 9, 2024
70063a7
move FieldSource protocol
halungge Oct 10, 2024
19ceae8
add return type to FieldSource.get(...)
halungge Oct 11, 2024
ae937d4
Split factory argument in FieldProvider to several protocols
halungge Oct 11, 2024
c46f6a4
Merge branch 'prototype_fields_factory' into metrics_fields_factory
nfarabullini Oct 21, 2024
7ab27fa
update with upstream
nfarabullini Oct 21, 2024
257fe31
Merge branch 'metrics_fields_factory' of https://github.com/C2SM/icon…
nfarabullini Oct 21, 2024
745a43a
edit following merge with upstream
nfarabullini Oct 22, 2024
310612a
fix imports in geometry.py
halungge Nov 14, 2024
4ddfc19
setting up empty factory
halungge Nov 14, 2024
f56134e
add xfail for stencils that need embedded backend
halungge Nov 14, 2024
dfd321c
register first fields (WIP)
halungge Nov 14, 2024
3a00e64
Merge branch 'main' into interpolation_fields_factory
halungge Nov 19, 2024
8788bbb
add first field to interpolation factory,
halungge Nov 19, 2024
f1aab49
FieldOperator provider WIP
halungge Nov 21, 2024
f7423e0
simplify field_source protocol
halungge Nov 25, 2024
cb1565f
pre-commit
halungge Nov 25, 2024
c9a76e7
xfail test for in that must be run on embedded
halungge Nov 25, 2024
7cc9ea5
add composite source
halungge Nov 26, 2024
6e05d08
read cell normal orientation
halungge Nov 26, 2024
e327e47
add geofac_rot, geofac_div, and missing geometry fields (not computed)
halungge Nov 26, 2024
042e8e5
add convenience functions for
halungge Nov 26, 2024
91742e3
Merge branch 'import_array_ns_in_fieldallocation' into interpolation_…
halungge Nov 26, 2024
3ce3657
merge main
halungge Nov 28, 2024
29a4be2
update with upstream and resolved conflicts
nfarabullini Nov 29, 2024
27d16d3
small fix
nfarabullini Nov 29, 2024
54abef8
merge main
halungge Nov 29, 2024
2df1e38
add c_lin_e computation (numpy) to interpolation_factory.py
halungge Dec 2, 2024
0539bc6
Merge branch 'main' into metrics_fields_factory
nfarabullini Dec 2, 2024
12ebc30
register geofac_n2s, workaround composite source
halungge Dec 2, 2024
8e065b1
make domain simple sequence of dims in NumpyFieldProvider
halungge Dec 2, 2024
32029da
register geofac_grdiv (WIP)
halungge Dec 3, 2024
00f4756
improve and fix doc string in factory
halungge Dec 3, 2024
b15ee4d
Merge branch 'main' of https://github.com/C2SM/icon4py into metrics_f…
nfarabullini Dec 3, 2024
b5cb369
Merge branch 'metrics_fields_factory' of https://github.com/C2SM/icon…
nfarabullini Dec 3, 2024
e3c3e4c
merge with upstream
nfarabullini Dec 3, 2024
db9fc0d
fix import in grid_test/utils.py
halungge Dec 3, 2024
d5b0c28
Merge branch 'main' into interpolation_fields_factory
halungge Dec 3, 2024
f6c32a4
fix import of grid_file constant
halungge Dec 3, 2024
d16d759
add geofac_grg
halungge Dec 3, 2024
db7c6b9
merge main
halungge Dec 4, 2024
86e768d
Update model/common/src/icon4py/model/common/states/factory.py
halungge Dec 5, 2024
1db9096
Update model/common/src/icon4py/model/common/states/factory.py
halungge Dec 5, 2024
bf2f1b2
review fixes
halungge Dec 5, 2024
5550832
partial metrics_field refactoring
nfarabullini Dec 5, 2024
a2c5a37
fix tests:
halungge Dec 5, 2024
13cbfda
check union type annotation
halungge Dec 6, 2024
fc4caf3
further edits
nfarabullini Dec 9, 2024
78e5a0a
Merge branch 'interpolation_fields_factory' into metrics_fields_factory
nfarabullini Dec 9, 2024
d775e92
further edits
nfarabullini Dec 9, 2024
bd6ef18
further edits
nfarabullini Dec 11, 2024
a85b340
small fix
nfarabullini Dec 11, 2024
5514374
merge with upstream
nfarabullini Dec 12, 2024
ae69a62
small edits
nfarabullini Dec 12, 2024
e4ac5c5
further edits
nfarabullini Dec 12, 2024
0c27ab7
further edits
nfarabullini Dec 12, 2024
e8ed26e
edits post-precommit
nfarabullini Dec 12, 2024
2e4bc95
small edit
nfarabullini Dec 12, 2024
bc78a76
small edit
nfarabullini Dec 12, 2024
a9d6d39
small cleanup
nfarabullini Dec 12, 2024
6100550
small cleanup
nfarabullini Dec 13, 2024
a71c8dc
Merge branch 'main' into metrics_fields_factory
nfarabullini Jan 7, 2025
307bbb1
Merge branch 'main' into metrics_fields_factory
nfarabullini Jan 7, 2025
02ab3de
edits following merge with upstream
nfarabullini Jan 7, 2025
3be39af
further edits
nfarabullini Jan 7, 2025
dde8b4d
Merge branch 'main' into metrics_fields_factory
nfarabullini Jan 9, 2025
096af0e
further edits
nfarabullini Jan 9, 2025
19b212a
Merge branch 'main' into metrics_fields_factory
nfarabullini Jan 13, 2025
c33ffef
added MetricdCOnfig class
nfarabullini Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,6 @@ def time_step(
f"running timestep: dtime = {dtime}, initial_timestep = {at_initial_timestep}, first_substep = {at_first_substep}, last_substep = {at_last_substep}, prep_adv = {lprep_adv}"
)

# # TODO: abishekg7 move this to tests
if self.p_test_run:
self._init_test_fields(
self.intermediate_fields.z_rho_e,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
EdgeField: TypeAlias = Field[Dims[dims.EdgeDim], T]
VertexField: TypeAlias = Field[Dims[dims.VertexDim], T]
KField: TypeAlias = Field[Dims[dims.KDim], T]
KHalfField: TypeAlias = Field[Dims[dims.KHalfDim], T]

CellKField: TypeAlias = Field[Dims[dims.CellDim, dims.KDim], T]
EdgeKField: TypeAlias = Field[Dims[dims.EdgeDim, dims.KDim], T]
Expand Down
1 change: 1 addition & 0 deletions model/common/src/icon4py/model/common/grid/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def __init__(
{
# TODO (@magdalena) rescaled by grid_length_rescale_factor (mo_grid_tools.f90)
attrs.EDGE_CELL_DISTANCE: extra_fields[gm.GeometryName.EDGE_CELL_DISTANCE],
attrs.EDGE_VERTEX_DISTANCE: extra_fields[gm.GeometryName.EDGE_VERTEX_DISTANCE],
attrs.CELL_AREA: extra_fields[gm.GeometryName.CELL_AREA],
attrs.DUAL_AREA: extra_fields[gm.GeometryName.DUAL_AREA],
attrs.TANGENT_ORIENTATION: extra_fields[gm.GeometryName.TANGENT_ORIENTATION],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
EDGE_AREA: Final[str] = "edge_area"
DUAL_AREA: Final[str] = "dual_area"
EDGE_CELL_DISTANCE: Final[str] = "edge_midpoint_to_cell_center_distance"
EDGE_VERTEX_DISTANCE: Final[str] = "edge_midpoint_to_vertex_distance"
TANGENT_ORIENTATION: Final[str] = "edge_orientation"
CELL_NORMAL_ORIENTATION: Final[str] = "orientation_of_normal_to_cell_edges"
VERTEX_EDGE_ORIENTATION: Final[str] = "orientation_of_edges_around_vertex"
Expand Down Expand Up @@ -126,6 +127,14 @@
icon_var_name="t_grid_edges%edge_cell_length",
dtype=ta.wpfloat,
),
EDGE_VERTEX_DISTANCE: dict(
standard_name=EDGE_VERTEX_DISTANCE,
long_name="distances between edge midpoint and adjacent vertices",
units="m",
dims=(dims.EdgeDim, dims.E2VDim),
icon_var_name="t_grid_edges%edge_vert_length",
dtype=ta.wpfloat,
),
DUAL_EDGE_LENGTH: dict(
standard_name=DUAL_EDGE_LENGTH,
long_name="length of the dual edge",
Expand Down
5 changes: 5 additions & 0 deletions model/common/src/icon4py/model/common/grid/grid_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class GeometryName(FieldName):
EDGE_ORIENTATION_ON_VERTEX = "edge_orientation"
# TODO (@halungge) compute from coordinates
EDGE_CELL_DISTANCE = "edge_cell_distance"
EDGE_VERTEX_DISTANCE = "edge_vert_distance"


class CoordinateName(FieldName):
Expand Down Expand Up @@ -467,6 +468,10 @@ def _read_geometry_fields(self, backend: Optional[gtx_backend.Backend]):
self._reader.variable(GeometryName.EDGE_CELL_DISTANCE, transpose=True),
),
# TODO (@halungge) recompute from coordinates? field in gridfile contains NaN on boundary edges
GeometryName.EDGE_VERTEX_DISTANCE.value: gtx.as_field(
(dims.EdgeDim, dims.E2VDim),
self._reader.variable(GeometryName.EDGE_VERTEX_DISTANCE, transpose=True),
),
GeometryName.TANGENT_ORIENTATION.value: gtx.as_field(
(dims.EdgeDim,),
self._reader.variable(GeometryName.TANGENT_ORIENTATION),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
GEOFAC_GRDIV: Final[str] = "geometrical_factor_for_gradient_of_divergence"
GEOFAC_GRG_X: Final[str] = "geometrical_factor_for_green_gauss_gradient_x"
GEOFAC_GRG_Y: Final[str] = "geometrical_factor_for_green_gauss_gradient_y"
CELL_AW_VERTS: Final[str] = "geometrical_factor_for_cells_aw_verts"

attrs: dict[str, model.FieldMetaData] = {
C_LIN_E: dict(
Expand Down Expand Up @@ -86,4 +87,12 @@
icon_var_name="geofac_grg",
dtype=ta.wpfloat,
),
CELL_AW_VERTS: dict(
standard_name=CELL_AW_VERTS,
long_name="geometrical factor for cells_aw_verts",
units="",
dims=(dims.VertexDim, dims.V2CDim),
icon_var_name="cells_aw_verts",
dtype=ta.wpfloat,
),
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

cell_domain = h_grid.domain(dims.CellDim)
edge_domain = h_grid.domain(dims.EdgeDim)
vertex_domain = h_grid.domain(dims.VertexDim)


class InterpolationFieldsFactory(factory.FieldSource, factory.GridProvider):
Expand Down Expand Up @@ -181,9 +182,31 @@ def _register_computed_fields(self):
)
},
)

self.register_provider(geofac_grg)

cells_aw_verts = factory.NumpyFieldsProvider(
func=functools.partial(interpolation_fields.compute_cells_aw_verts),
fields=(attrs.CELL_AW_VERTS,),
domain=(dims.VertexDim, dims.V2CDim),
deps={
"dual_area": geometry_attrs.DUAL_AREA,
"edge_vert_length": geometry_attrs.EDGE_VERTEX_DISTANCE,
"edge_cell_length": geometry_attrs.EDGE_CELL_DISTANCE,
},
connectivities={
"v2e": dims.V2EDim,
"e2v": dims.E2VDim,
"v2c": dims.V2CDim,
"e2c": dims.E2CDim,
},
params={
"horizontal_start": self.grid.start_index(
vertex_domain(h_grid.Zone.LATERAL_BOUNDARY_LEVEL_2)
)
},
)
self.register_provider(cells_aw_verts)

@property
def metadata(self) -> dict[str, model.FieldMetaData]:
return self._attrs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ def compute_cells_aw_verts(

Args:
dual_area: numpy array, representing a gtx.Field[gtx.Dims[VertexDim], ta.wpfloat]
edge_vert_length: \\ numpy array, representing a gtx.Field[gtx.Dims[EdgeDim, E2CDim], ta.wpfloat]
edge_vert_length: \\ numpy array, representing a gtx.Field[gtx.Dims[EdgeDim, E2VDim], ta.wpfloat]
edge_cell_length: //
owner_mask: numpy array, representing a gtx.Field[gtx.Dims[VertexDim], bool]
v2e: numpy array, representing a gtx.Field[gtx.Dims[VertexDim, V2EDim], gtx.int32]
Expand All @@ -849,14 +849,18 @@ def compute_cells_aw_verts(
cells_aw_verts[jv, :] = 0.0
for je in range(v2e.shape[1]):
# INVALID_INDEX
if je > gm.GridFile.INVALID_INDEX and (je > 0 and v2e[jv, je] == v2e[jv, je - 1]):
if v2e[jv, je] == gm.GridFile.INVALID_INDEX or (
je > 0 and v2e[jv, je] == v2e[jv, je - 1]
):
continue
ile = v2e[jv, je]
idx_ve = 0 if e2v[ile, 0] == jv else 1
cell_offset_idx_0 = e2c[ile, 0]
cell_offset_idx_1 = e2c[ile, 1]
for jc in range(v2e.shape[1]):
if jc > gm.GridFile.INVALID_INDEX and (jc > 0 and v2c[jv, jc] == v2c[jv, jc - 1]):
if v2c[jv, jc] == gm.GridFile.INVALID_INDEX or (
jc > 0 and v2c[jv, jc] == v2c[jv, jc - 1]
):
continue
if cell_offset_idx_0 == v2c[jv, jc]:
cells_aw_verts[jv, jc] = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@


def compute_coeff_gradekin(
edge_cell_length: np.array,
inv_dual_edge_length: np.array,
horizontal_start: float,
horizontal_end: float,
) -> np.array:
edge_cell_length: data_alloc.NDArray,
inv_dual_edge_length: data_alloc.NDArray,
horizontal_start: int,
horizontal_end: int,
):
"""
Compute coefficients for improved calculation of kinetic energy gradient

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
from icon4py.model.common.utils import data_allocation as data_alloc


def compute_max_nbhgt_np(
c2e2c: data_alloc.NDArray, z_mc: data_alloc.NDArray, nlev: int
) -> data_alloc.NDArray:
z_mc_nlev = z_mc[:, nlev - 1]
max_nbhgt_0_1 = np.maximum(z_mc_nlev[c2e2c[:, 0]], z_mc_nlev[c2e2c[:, 1]])
max_nbhgt = np.maximum(max_nbhgt_0_1, z_mc_nlev[c2e2c[:, 2]])
return max_nbhgt


def _compute_nbidx(
k_range: range,
z_mc: data_alloc.NDArray,
Expand Down Expand Up @@ -62,8 +71,8 @@ def _compute_z_vintcoeff(
def _compute_ls_params(
k_start: list,
k_end: list,
z_maxslp_avg: data_alloc.NDArray,
z_maxhgtd_avg: data_alloc.NDArray,
maxslp_avg: data_alloc.NDArray,
maxhgtd_avg: data_alloc.NDArray,
c_owner_mask: data_alloc.NDArray,
thslp_zdiffu: float,
thhgtd_zdiffu: float,
Expand All @@ -78,8 +87,7 @@ def _compute_ls_params(

for jc in range(cell_nudging, n_cells):
if (
z_maxslp_avg[jc, nlev - 1] >= thslp_zdiffu
or z_maxhgtd_avg[jc, nlev - 1] >= thhgtd_zdiffu
maxslp_avg[jc, nlev - 1] >= thslp_zdiffu or maxhgtd_avg[jc, nlev - 1] >= thhgtd_zdiffu
) and c_owner_mask[jc]:
ji += 1
indlist[ji] = jc
Expand All @@ -96,8 +104,8 @@ def _compute_ls_params(
def _compute_k_start_end(
z_mc: data_alloc.NDArray,
max_nbhgt: data_alloc.NDArray,
z_maxslp_avg: data_alloc.NDArray,
z_maxhgtd_avg: data_alloc.NDArray,
maxslp_avg: data_alloc.NDArray,
maxhgtd_avg: data_alloc.NDArray,
c_owner_mask: data_alloc.NDArray,
thslp_zdiffu: float,
thhgtd_zdiffu: float,
Expand All @@ -109,16 +117,15 @@ def _compute_k_start_end(
k_end = [None] * n_cells
for jc in range(cell_nudging, n_cells):
if (
z_maxslp_avg[jc, nlev - 1] >= thslp_zdiffu
or z_maxhgtd_avg[jc, nlev - 1] >= thhgtd_zdiffu
maxslp_avg[jc, nlev - 1] >= thslp_zdiffu or maxhgtd_avg[jc, nlev - 1] >= thhgtd_zdiffu
) and c_owner_mask[jc]:
for jk in reversed(range(nlev)):
if z_mc[jc, jk] >= max_nbhgt[jc]:
k_end[jc] = jk + 1
break

for jk in range(nlev):
if z_maxslp_avg[jc, jk] >= thslp_zdiffu or z_maxhgtd_avg[jc, jk] >= thhgtd_zdiffu:
if maxslp_avg[jc, jk] >= thslp_zdiffu or maxhgtd_avg[jc, jk] >= thhgtd_zdiffu:
k_start[jc] = jk
break

Expand All @@ -129,29 +136,31 @@ def _compute_k_start_end(


def compute_diffusion_metrics(
c2e2c: data_alloc.NDArray,
z_mc: data_alloc.NDArray,
z_mc_off: data_alloc.NDArray,
max_nbhgt: data_alloc.NDArray,
c_owner_mask: data_alloc.NDArray,
nbidx: data_alloc.NDArray,
z_vintcoeff: data_alloc.NDArray,
z_maxslp_avg: data_alloc.NDArray,
z_maxhgtd_avg: data_alloc.NDArray,
mask_hdiff: data_alloc.NDArray,
zd_diffcoef_dsl: data_alloc.NDArray,
zd_intcoef_dsl: data_alloc.NDArray,
zd_vertoffset_dsl: data_alloc.NDArray,
maxslp_avg: data_alloc.NDArray,
maxhgtd_avg: data_alloc.NDArray,
thslp_zdiffu: float,
thhgtd_zdiffu: float,
n_c2e2c: int,
cell_nudging: int,
n_cells: int,
nlev: int,
) -> tuple[data_alloc.NDArray, data_alloc.NDArray, data_alloc.NDArray, data_alloc.NDArray]:
z_mc_off = z_mc[c2e2c]
nbidx = np.ones(shape=(n_cells, n_c2e2c, nlev), dtype=int)
z_vintcoeff = np.zeros(shape=(n_cells, n_c2e2c, nlev))
mask_hdiff = np.zeros(shape=(n_cells, nlev), dtype=bool)
zd_vertoffset_dsl = np.zeros(shape=(n_cells, n_c2e2c, nlev))
zd_intcoef_dsl = np.zeros(shape=(n_cells, n_c2e2c, nlev))
zd_diffcoef_dsl = np.zeros(shape=(n_cells, nlev))
k_start, k_end = _compute_k_start_end(
z_mc=z_mc,
max_nbhgt=max_nbhgt,
z_maxslp_avg=z_maxslp_avg,
z_maxhgtd_avg=z_maxhgtd_avg,
maxslp_avg=maxslp_avg,
maxhgtd_avg=maxhgtd_avg,
c_owner_mask=c_owner_mask,
thslp_zdiffu=thslp_zdiffu,
thhgtd_zdiffu=thhgtd_zdiffu,
Expand All @@ -163,8 +172,8 @@ def compute_diffusion_metrics(
indlist, listreduce, ji = _compute_ls_params(
k_start=k_start,
k_end=k_end,
z_maxslp_avg=z_maxslp_avg,
z_maxhgtd_avg=z_maxhgtd_avg,
maxslp_avg=maxslp_avg,
maxhgtd_avg=maxhgtd_avg,
c_owner_mask=c_owner_mask,
thslp_zdiffu=thslp_zdiffu,
thhgtd_zdiffu=thhgtd_zdiffu,
Expand All @@ -191,10 +200,18 @@ def compute_diffusion_metrics(
zd_diffcoef_dsl_var = np.maximum(
0.0,
np.maximum(
np.sqrt(np.maximum(0.0, z_maxslp_avg[jc, k_range] - thslp_zdiffu)) / 250.0,
2.0e-4 * np.sqrt(np.maximum(0.0, z_maxhgtd_avg[jc, k_range] - thhgtd_zdiffu)),
np.sqrt(np.maximum(0.0, maxslp_avg[jc, k_range] - thslp_zdiffu)) / 250.0,
2.0e-4 * np.sqrt(np.maximum(0.0, maxhgtd_avg[jc, k_range] - thhgtd_zdiffu)),
),
)
zd_diffcoef_dsl[jc, k_range] = np.minimum(0.002, zd_diffcoef_dsl_var)

# flatten first two dims:
zd_intcoef_dsl = zd_intcoef_dsl.reshape(
(zd_intcoef_dsl.shape[0] * zd_intcoef_dsl.shape[1],) + zd_intcoef_dsl.shape[2:]
)
zd_vertoffset_dsl = zd_vertoffset_dsl.reshape(
(zd_vertoffset_dsl.shape[0] * zd_vertoffset_dsl.shape[1],) + zd_vertoffset_dsl.shape[2:]
)

return mask_hdiff, zd_diffcoef_dsl, zd_intcoef_dsl, zd_vertoffset_dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ICON4Py - ICON inspired code in Python and GT4Py
#
# Copyright (c) 2022-2024, ETH Zurich and MeteoSwiss
# All rights reserved.
#
# Please, refer to the LICENSE file in the root directory.
# SPDX-License-Identifier: BSD-3-Clause

import numpy as np

from icon4py.model.common.utils import data_allocation as data_alloc


def compute_flat_idx_max(
e2c: data_alloc.NDArray,
z_mc: data_alloc.NDArray,
c_lin_e: data_alloc.NDArray,
z_ifc: data_alloc.NDArray,
k_lev: data_alloc.NDArray,
horizontal_lower: int,
horizontal_upper: int,
) -> data_alloc.NDArray:
z_me = np.sum(z_mc[e2c] * np.expand_dims(c_lin_e, axis=-1), axis=1)
z_ifc_e_0 = z_ifc[e2c[:, 0]]
z_ifc_e_k_0 = np.roll(z_ifc_e_0, -1, axis=1)
z_ifc_e_1 = z_ifc[e2c[:, 1]]
z_ifc_e_k_1 = np.roll(z_ifc_e_1, -1, axis=1)
flat_idx = np.zeros_like(z_me)
for je in range(horizontal_lower, horizontal_upper):
for jk in range(k_lev.shape[0] - 1):
if (
(z_me[je, jk] <= z_ifc_e_0[je, jk])
and (z_me[je, jk] >= z_ifc_e_k_0[je, jk])
and (z_me[je, jk] <= z_ifc_e_1[je, jk])
and (z_me[je, jk] >= z_ifc_e_k_1[je, jk])
):
flat_idx[je, jk] = k_lev[jk]
flat_idx_max = np.amax(flat_idx, axis=1)
return flat_idx_max.astype(np.int32)
Loading
Loading