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 70 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
188 changes: 188 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 @@ -32,6 +32,7 @@

from icon4py.model.common.dimension import (
C2E,
C2E2C,
E2C,
CellDim,
E2CDim,
Expand Down Expand Up @@ -564,6 +565,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 +1056,156 @@ def compute_hmask_dd3d(
out=hmask_dd3d,
domain={EdgeDim: (horizontal_start, horizontal_end)},
)


@field_operator
def _compute_mask_hdiff() -> Field[[CellDim, KDim], bool]:
return broadcast(True, (CellDim, KDim))


@program
def compute_mask_hdiff(
mask_hdiff: Field[[CellDim, KDim], bool],
nfarabullini marked this conversation as resolved.
Show resolved Hide resolved
horizontal_start: int32,
horizontal_end: int32,
vertical_start: int32,
vertical_end: int32,
):
"""
Compute mask_hdiff.

See mo_vertical_grid.f90.

Args:
mask_hdiff: output
horizontal_start: horizontal start index
horizontal_end: horizontal end index
vertical_start: vertical start index
vertical_end: vertical end index
"""
_compute_mask_hdiff(
out=mask_hdiff,
domain={CellDim: (horizontal_start, horizontal_end), KDim: (vertical_start, vertical_end)},
)


@field_operator
def _compute_z_maxslp_avg(
maxslp: Field[[CellDim, KDim], wpfloat],
c_bln_avg_0: Field[[CellDim], wpfloat],
c_bln_avg_1: Field[[CellDim], wpfloat],
c_bln_avg_2: Field[[CellDim], wpfloat],
c_bln_avg_3: Field[[CellDim], wpfloat],
) -> Field[[CellDim, KDim], wpfloat]:
z_maxslp_avg = (
nfarabullini marked this conversation as resolved.
Show resolved Hide resolved
maxslp * c_bln_avg_0
+ maxslp(C2E2C[0]) * c_bln_avg_1
+ maxslp(C2E2C[1]) * c_bln_avg_2
+ maxslp(C2E2C[2]) * c_bln_avg_3
)
return z_maxslp_avg


@field_operator
def _compute_z_maxhgtd_avg(
nfarabullini marked this conversation as resolved.
Show resolved Hide resolved
maxhgtd: Field[[CellDim, KDim], wpfloat],
c_bln_avg_0: Field[[CellDim], wpfloat],
c_bln_avg_1: Field[[CellDim], wpfloat],
c_bln_avg_2: Field[[CellDim], wpfloat],
c_bln_avg_3: Field[[CellDim], wpfloat],
) -> Field[[CellDim, KDim], wpfloat]:
z_maxhgtd_avg = (
maxhgtd * c_bln_avg_0
+ maxhgtd(C2E2C[0]) * c_bln_avg_1
+ maxhgtd(C2E2C[1]) * c_bln_avg_2
+ maxhgtd(C2E2C[2]) * c_bln_avg_3
)
return z_maxhgtd_avg


@program
def compute_z_maxslp_avg_z_maxhgtd_avg(
nfarabullini marked this conversation as resolved.
Show resolved Hide resolved
maxslp: Field[[CellDim, KDim], wpfloat],
maxhgtd: Field[[CellDim, KDim], wpfloat],
c_bln_avg_0: Field[[CellDim], wpfloat],
c_bln_avg_1: Field[[CellDim], wpfloat],
c_bln_avg_2: Field[[CellDim], wpfloat],
c_bln_avg_3: Field[[CellDim], 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_0: Interpolation field 0th K element
c_bln_avg_1: Interpolation field 1st K element
c_bln_avg_2: Interpolation field 2nd K element
c_bln_avg_3: Interpolation field 3rd K element
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_z_maxslp_avg(
maxslp=maxslp,
c_bln_avg_0=c_bln_avg_0,
c_bln_avg_1=c_bln_avg_1,
c_bln_avg_2=c_bln_avg_2,
c_bln_avg_3=c_bln_avg_3,
out=z_maxslp_avg,
domain={CellDim: (horizontal_start, horizontal_end), KDim: (vertical_start, vertical_end)},
)

_compute_z_maxhgtd_avg(
maxhgtd=maxhgtd,
c_bln_avg_0=c_bln_avg_0,
c_bln_avg_1=c_bln_avg_1,
c_bln_avg_2=c_bln_avg_2,
c_bln_avg_3=c_bln_avg_3,
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)}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# 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_i_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]:
i_indlist = [0] * n_cells
nfarabullini marked this conversation as resolved.
Show resolved Hide resolved
i_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
i_indlist[ji] = jc

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

return i_indlist, i_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,
k_start: list,
k_end: list,
i_indlist: list,
i_listdim: int,
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,
nlev: int,
) -> np.array:
for ji in range(i_listdim):
jc = i_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
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,9 @@ def construct_cell_geometry(self) -> CellParams:


class InterpolationSavepoint(IconSavepoint):
def c_bln_avg(self):
return self._get_field("c_bln_avg", CellDim, KDim)
halungge marked this conversation as resolved.
Show resolved Hide resolved

def c_intp(self):
return self._get_field("c_intp", VertexDim, V2CDim)

Expand Down Expand Up @@ -692,6 +695,7 @@ def geopot(self):

def _read_and_reorder_sparse_field(self, name: str, sparse_size=3):
ser_input = np.squeeze(self.serializer.read(name, self.savepoint))[:, :, :]
ser_input = self._reduce_to_dim_size(ser_input, (CellDim, C2E2CDim, KDim))
if ser_input.shape[1] != sparse_size:
ser_input = np.moveaxis(ser_input, 1, -1)

Expand Down
Loading
Loading