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

Diffusion metrics fields #472

Merged
merged 81 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
af43771
further interpolation coefficients
ajocksch Mar 22, 2024
b1a7e5d
progress
ajocksch Mar 26, 2024
c2d201c
progress
ajocksch Mar 28, 2024
89949b4
ddxt_z_full
ajocksch Apr 2, 2024
b05bbee
model/common/src/icon4py/model/common/interpolation/interpolation_fie…
ajocksch Apr 9, 2024
8aac5e2
progress gt4py
ajocksch Apr 9, 2024
9ecd7e3
progress gt4py
ajocksch Apr 10, 2024
bac95e4
progress numpy to gt4py
ajocksch Apr 10, 2024
596ec48
progress ddqz_z_full_e
ajocksch Apr 19, 2024
6bd552c
Merge branch 'main' of github.com:C2SM/icon4py into greenline_interpo…
ajocksch Apr 19, 2024
cb68e6c
partial implementation of vwind_impl_wgt and vwind_expl_wgt
nfarabullini Apr 19, 2024
3d71578
small progress
ajocksch Apr 23, 2024
57ca79f
some implementation for fields
nfarabullini Apr 24, 2024
61ac852
Update model/common/src/icon4py/model/common/interpolation/interpolat…
nfarabullini Apr 24, 2024
c489f8a
applied changes after PR review
nfarabullini Apr 24, 2024
2492205
one more change
nfarabullini Apr 24, 2024
9e92338
Merge branch 'main' of https://github.com/C2SM/icon4py into greenline…
nfarabullini Apr 24, 2024
7412635
fixed pre-commit
nfarabullini Apr 24, 2024
da8d98a
edits following review
nfarabullini Apr 24, 2024
53c5ac6
docstring edit
nfarabullini Apr 24, 2024
ec468eb
small edits
nfarabullini Apr 25, 2024
c3c4427
merge with other branch
nfarabullini Apr 25, 2024
54363af
edits
nfarabullini Apr 26, 2024
8eed019
merge with upstream
nfarabullini Apr 30, 2024
ddfd413
edits
nfarabullini Apr 30, 2024
76d5554
further edits
nfarabullini Apr 30, 2024
308b1e4
small edit
nfarabullini Apr 30, 2024
fc9d8e2
added comment for skipping
nfarabullini Apr 30, 2024
3608bb7
file split
nfarabullini Apr 30, 2024
41ab599
Merge branch 'main' of https://github.com/C2SM/icon4py into metrics_f…
nfarabullini May 2, 2024
80f4eb5
exner_exfac implementation
nfarabullini May 2, 2024
de19fea
edits following review
nfarabullini May 2, 2024
375136c
further edits
nfarabullini May 2, 2024
6819a4f
added missing backend option to some programs
nfarabullini May 2, 2024
675e91f
Merge branch 'metrics_fields_second_batch' of https://github.com/C2SM…
nfarabullini May 2, 2024
b68f9f8
added zdiff_gradp_dsl computation
nfarabullini May 2, 2024
6cda94a
added zdiff_gradp_dsl computation
nfarabullini May 2, 2024
44fd84f
added coeff_gradekin computation
nfarabullini May 2, 2024
0b1dd78
corrections
nfarabullini May 2, 2024
f3bc5b4
vwind_impl_wgt test
nfarabullini May 3, 2024
c4211b9
vwind_impl_wgt prog
nfarabullini May 3, 2024
c96beb8
removed vwind_impl_wgt
nfarabullini May 3, 2024
bea8158
wgtfacq_e_dsl and other edits to wgtfacq_c
nfarabullini May 6, 2024
6431069
pg_edgeidx_dsl, pg_exdist_dsl, wgtfac_e
nfarabullini May 13, 2024
6496b93
cleanup
nfarabullini May 13, 2024
84d31a2
mask_prog_halo_c implementation and zdiff_gradp fix
nfarabullini May 14, 2024
a55265c
ran pre-commit
nfarabullini May 14, 2024
b2736cc
bdy_halo_c and docstrings
nfarabullini May 14, 2024
20a333e
hmask_dd3d implementation
nfarabullini May 14, 2024
495fa22
Merge branch 'metrics_fields_second_batch' into metrics_fields_third_…
nfarabullini May 14, 2024
ac644a7
ran pre-commit
nfarabullini May 14, 2024
4a66dae
Merge branch 'main' into metrics_fields_third_batch
nfarabullini May 14, 2024
c27d39a
partial merge
nfarabullini May 14, 2024
605d084
fixed imports and updates
nfarabullini May 14, 2024
ce0bcde
fixed minor mistake
nfarabullini May 15, 2024
5805b98
update to vwind_impl_wgt
nfarabullini May 23, 2024
f320b77
mask_hdiff implementation
nfarabullini May 23, 2024
455d9a5
zd_diffcoef_dsl implementation and partial for zd_vertoffset_dsl
nfarabullini May 24, 2024
cb990b8
mask_hdiff, zd_diffcoef_dsl, zd_vertoffset_dsl implementation
nfarabullini May 28, 2024
c1a6343
finishes
nfarabullini May 29, 2024
70223e1
merge with upstream
nfarabullini May 29, 2024
ef9f662
cleanup
nfarabullini May 29, 2024
cba8c1a
small edit
nfarabullini May 29, 2024
9bfee5b
small edit
nfarabullini May 30, 2024
ec666dc
small edit
nfarabullini May 30, 2024
a8bfa20
edits
nfarabullini May 30, 2024
87eea72
added docstring as ran pre-commit
nfarabullini May 30, 2024
383b84c
small edit
nfarabullini May 30, 2024
110d03a
small edit
nfarabullini May 30, 2024
3dfa6dd
code cleanup and slim down
nfarabullini May 30, 2024
1be470f
Merge branch 'main' of https://github.com/C2SM/icon4py into diffusion…
nfarabullini May 31, 2024
d9e680c
edits following review and ran pre-commit
nfarabullini May 31, 2024
20b7d73
small edit
nfarabullini May 31, 2024
91bb310
edits following review
nfarabullini Jun 10, 2024
859e7aa
Merge branch 'main' of https://github.com/C2SM/icon4py into diffusion…
nfarabullini Jun 26, 2024
051589d
update with upstream and docstrings for partial nested k-loop field_op
nfarabullini Jun 26, 2024
a015c10
Merge branch 'main' of https://github.com/C2SM/icon4py into diffusion…
nfarabullini Jun 27, 2024
5c76e7c
update with upstream
nfarabullini Jul 10, 2024
df8080f
reverted change
nfarabullini Jul 11, 2024
c1fbb83
removed backend specification
nfarabullini Jul 11, 2024
b980056
Merge branch 'main' into diffusion_metrics_fields
nfarabullini Jul 23, 2024
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
148 changes: 148 additions & 0 deletions model/common/src/icon4py/model/common/metrics/metric_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@
int32,
maximum,
minimum,
neighbor_sum,
program,
scan_operator,
sin,
tanh,
where,
)

from icon4py.model.common.dimension import (
C2E,
C2E2C,
C2E2CO,
E2C,
C2E2CODim,
CellDim,
E2CDim,
EdgeDim,
Expand Down Expand Up @@ -564,6 +569,40 @@ def _compute_maxslp_maxhgtd(
return z_maxslp, z_maxhgtd


@program
def compute_maxslp_maxhgtd(
ddxn_z_full: Field[[EdgeDim, KDim], wpfloat],
dual_edge_length: Field[[EdgeDim], wpfloat],
z_maxslp: Field[[CellDim, KDim], wpfloat],
z_maxhgtd: Field[[CellDim, KDim], wpfloat],
horizontal_start: int32,
horizontal_end: int32,
vertical_start: int32,
vertical_end: int32,
):
"""
Compute z_maxslp and z_maxhgtd.

See mo_vertical_grid.f90.

Args:
ddxn_z_full: dual_edge_length
dual_edge_length: dual_edge_length
z_maxslp: output
z_maxhgtd: output
horizontal_start: horizontal start index
horizontal_end: horizontal end index
vertical_start: vertical start index
vertical_end: vertical end index
"""
_compute_maxslp_maxhgtd(
ddxn_z_full=ddxn_z_full,
dual_edge_length=dual_edge_length,
out=(z_maxslp, z_maxhgtd),
domain={CellDim: (horizontal_start, horizontal_end), KDim: (vertical_start, vertical_end)},
)


@field_operator
def _compute_exner_exfac(
ddxn_z_full: Field[[EdgeDim, KDim], wpfloat],
Expand Down Expand Up @@ -1021,3 +1060,112 @@ def compute_hmask_dd3d(
out=hmask_dd3d,
domain={EdgeDim: (horizontal_start, horizontal_end)},
)


@field_operator
def _compute_weighted_cell_neighbor_sum(
field: Field[[CellDim, KDim], wpfloat],
c_bln_avg: Field[[CellDim, C2E2CODim], wpfloat],
) -> Field[[CellDim, KDim], wpfloat]:
field_avg = neighbor_sum(field(C2E2CO) * c_bln_avg, axis=C2E2CODim)
return field_avg


@program
def compute_weighted_cell_neighbor_sum(
maxslp: Field[[CellDim, KDim], wpfloat],
maxhgtd: Field[[CellDim, KDim], wpfloat],
c_bln_avg: Field[[CellDim, C2E2CODim], wpfloat],
z_maxslp_avg: Field[[CellDim, KDim], wpfloat],
z_maxhgtd_avg: Field[[CellDim, KDim], wpfloat],
horizontal_start: int32,
horizontal_end: int32,
vertical_start: int32,
vertical_end: int32,
):
"""
Compute z_maxslp_avg and z_maxhgtd_avg.

See mo_vertical_grid.f90.

Args:
maxslp: Max field over ddxn_z_full offset
maxhgtd: Max field over ddxn_z_full offset*dual_edge_length offset
c_bln_avg: Interpolation field
z_maxslp_avg: output
z_maxhgtd_avg: output
horizontal_start: horizontal start index
horizontal_end: horizontal end index
vertical_start: vertical start index
vertical_end: vertical end index
"""

_compute_weighted_cell_neighbor_sum(
field=maxslp,
c_bln_avg=c_bln_avg,
out=z_maxslp_avg,
domain={CellDim: (horizontal_start, horizontal_end), KDim: (vertical_start, vertical_end)},
)

_compute_weighted_cell_neighbor_sum(
field=maxhgtd,
c_bln_avg=c_bln_avg,
out=z_maxhgtd_avg,
domain={CellDim: (horizontal_start, horizontal_end), KDim: (vertical_start, vertical_end)},
)


@field_operator
def _compute_max_nbhgt(
z_mc_nlev: Field[[CellDim], wpfloat],
) -> Field[[CellDim], wpfloat]:
max_nbhgt_0_1 = maximum(z_mc_nlev(C2E2C[0]), z_mc_nlev(C2E2C[1]))
max_nbhgt = maximum(max_nbhgt_0_1, z_mc_nlev(C2E2C[2]))
return max_nbhgt


@program
def compute_max_nbhgt(
z_mc_nlev: Field[[CellDim], wpfloat],
max_nbhgt: Field[[CellDim], wpfloat],
horizontal_start: int32,
horizontal_end: int32,
):
"""
Compute max_nbhgt.

See mo_vertical_grid.f90.

Args:
z_mc_nlev: Last K level of z_mc
max_nbhgt: output
horizontal_start: horizontal start index
horizontal_end: horizontal end index
"""
_compute_max_nbhgt(
z_mc_nlev=z_mc_nlev, out=max_nbhgt, domain={CellDim: (horizontal_start, horizontal_end)}
)


@scan_operator(axis=KDim, forward=True, init=(0, False))
def _compute_param(
param: tuple[int32, bool],
z_me_jk: float,
z_ifc_off: float,
z_ifc_off_koff: float,
lower: int32,
nlev: int32,
) -> tuple[int32, bool]:
param_0, param_1 = param
if param_0 >= lower:
if (param_0 == nlev) | (z_me_jk <= z_ifc_off) & (z_me_jk >= z_ifc_off_koff):
param_1 = True
return param_0 + 1, param_1


@field_operator(grid_type=GridType.UNSTRUCTURED)
def _compute_z_ifc_off_koff(
z_ifc_off: Field[[EdgeDim, KDim], wpfloat],
) -> Field[[EdgeDim, KDim], wpfloat]:
n = z_ifc_off(Koff[1])
return n
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
# ICON4Py - ICON inspired code in Python and GT4Py
#
# Copyright (c) 2022, ETH Zurich and MeteoSwiss
# All rights reserved.
#
# This file is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or any later
# version. See the LICENSE.txt file at the top-level directory of this
# distribution for a copy of the license or check <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later

import numpy as np


def _compute_nbidx(
k_range: range,
z_mc: np.array,
z_mc_off: np.array,
nbidx: np.array,
jc: int,
nlev: int,
) -> np.array:
for ind in range(3):
jk_start = nlev - 1
for jk in reversed(k_range):
for jk1 in reversed(range(jk_start)):
if (
z_mc[jc, jk] <= z_mc_off[jc, ind, jk1]
and z_mc[jc, jk] >= z_mc_off[jc, ind, jk1 + 1]
):
nbidx[jc, ind, jk] = jk1
jk_start = jk1 + 1
break

return nbidx[jc, :, :]


def _compute_z_vintcoeff(
k_range: range,
z_mc: np.array,
z_mc_off: np.array,
z_vintcoeff: np.array,
jc: int,
nlev: int,
) -> np.array:
for ind in range(3):
jk_start = nlev - 1
for jk in reversed(k_range):
for jk1 in reversed(range(jk_start)):
if (
z_mc[jc, jk] <= z_mc_off[jc, ind, jk1]
and z_mc[jc, jk] >= z_mc_off[jc, ind, jk1 + 1]
):
z_vintcoeff[jc, ind, jk] = (z_mc[jc, jk] - z_mc_off[jc, ind, jk1 + 1]) / (
z_mc_off[jc, ind, jk1] - z_mc_off[jc, ind, jk1 + 1]
)
jk_start = jk1 + 1
break

return z_vintcoeff[jc, :, :]


def _compute_ls_params(
k_start: list,
k_end: list,
z_maxslp_avg: np.array,
z_maxhgtd_avg: np.array,
c_owner_mask: np.array,
thslp_zdiffu: float,
thhgtd_zdiffu: float,
cell_nudging: int,
n_cells: int,
nlev: int,
) -> tuple[list, int, int]:
indlist = [0] * n_cells
listreduce = 0
ji = -1
ji_ind = -1

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
) and c_owner_mask[jc]:
ji += 1
indlist[ji] = jc

if all((k_start[jc], k_end[jc])) and k_start[jc] > k_end[jc]:
listreduce += 1
else:
ji_ind += 1
indlist[ji_ind] = jc

return indlist, listreduce, ji


def _compute_k_start_end(
z_mc: np.array,
max_nbhgt: np.array,
z_maxslp_avg: np.array,
z_maxhgtd_avg: np.array,
c_owner_mask: np.array,
thslp_zdiffu: float,
thhgtd_zdiffu: float,
cell_nudging: int,
n_cells: int,
nlev: int,
) -> tuple[list, list]:
k_start = [None] * n_cells
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
) 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:
k_start[jc] = jk
break

if all((k_start[jc], k_end[jc])) and k_start[jc] > k_end[jc]:
k_start[jc] = nlev - 1

return k_start, k_end


def compute_diffusion_metrics(
z_mc: np.array,
z_mc_off: np.array,
max_nbhgt: np.array,
c_owner_mask: np.array,
nbidx: np.array,
z_vintcoeff: np.array,
z_maxslp_avg: np.array,
z_maxhgtd_avg: np.array,
mask_hdiff: np.array,
zd_diffcoef_dsl: np.array,
zd_intcoef_dsl: np.array,
zd_vertoffset_dsl: np.array,
thslp_zdiffu: float,
thhgtd_zdiffu: float,
cell_nudging: int,
n_cells: int,
nlev: int,
) -> tuple[np.array, np.array, np.array, np.array]:
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,
c_owner_mask=c_owner_mask,
thslp_zdiffu=thslp_zdiffu,
thhgtd_zdiffu=thhgtd_zdiffu,
cell_nudging=cell_nudging,
n_cells=n_cells,
nlev=nlev,
)

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,
c_owner_mask=c_owner_mask,
thslp_zdiffu=thslp_zdiffu,
thhgtd_zdiffu=thhgtd_zdiffu,
cell_nudging=cell_nudging,
n_cells=n_cells,
nlev=nlev,
)

listdim = ji - listreduce

for ji in range(listdim):
jc = indlist[ji]
k_range = range(k_start[jc], k_end[jc])
if all((k_range)):
nbidx[jc, :, :] = _compute_nbidx(k_range, z_mc, z_mc_off, nbidx, jc, nlev)
z_vintcoeff[jc, :, :] = _compute_z_vintcoeff(
k_range, z_mc, z_mc_off, z_vintcoeff, jc, nlev
)

zd_intcoef_dsl[jc, :, k_range] = z_vintcoeff[jc, :, k_range]
zd_vertoffset_dsl[jc, :, k_range] = nbidx[jc, :, k_range] - np.transpose([k_range] * 3)
mask_hdiff[jc, k_range] = True

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)),
),
)
zd_diffcoef_dsl[jc, k_range] = np.minimum(0.002, zd_diffcoef_dsl_var)

return mask_hdiff, zd_diffcoef_dsl, zd_intcoef_dsl, zd_vertoffset_dsl
Loading
Loading