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

Port wgtfacq_c to numpy #394

Merged
merged 10 commits into from
Mar 5, 2024
Merged
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, 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_wgtfacq_c(
jonasjucker marked this conversation as resolved.
Show resolved Hide resolved
jonasjucker marked this conversation as resolved.
Show resolved Hide resolved
z_ifc: np.array,
nlev: int,
) -> np.array:
"""
Compute weighting factor for quadratic interpolation to surface.

Args:
z_ifc: Field[CellDim, KDim] (half levels), geometric height at the vertical interface of cells.
nlev: int, last level
Returns:
Field[CellDim, KDim] (full levels)
"""
wgtfacq_c = np.zeros((z_ifc.shape[0], nlev))
z1 = 0.5 * (z_ifc[:, nlev - 1] - z_ifc[:, nlev])
z2 = 0.5 * (z_ifc[:, nlev - 1] + z_ifc[:, nlev - 2]) - z_ifc[:, nlev]
z3 = 0.5 * (z_ifc[:, nlev - 2] + z_ifc[:, nlev - 3]) - z_ifc[:, nlev]

wgtfacq_c[:, nlev - 3] = z1 * z2 / (z2 - z3) / (z1 - z3)
wgtfacq_c[:, nlev - 2] = (z1 - wgtfacq_c[:, nlev - 3] * (z1 - z3)) / (z1 - z2)
wgtfacq_c[:, nlev - 1] = 1.0 - (wgtfacq_c[:, nlev - 2] + wgtfacq_c[:, nlev - 3])

return wgtfacq_c
14 changes: 1 addition & 13 deletions model/common/tests/metric_tests/test_compute_wgtfac_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,12 @@
from icon4py.model.atmosphere.dycore.state_utils.utils import _allocate_indices
from icon4py.model.common.dimension import CellDim, KDim
from icon4py.model.common.metrics.stencils.compute_wgtfac_c import compute_wgtfac_c
from icon4py.model.common.test_utils.datatest_fixtures import ( # noqa: F401 # import fixtures from test_utils package
data_provider,
datapath,
download_ser_data,
experiment,
grid_savepoint,
icon_grid,
interpolation_savepoint,
metrics_savepoint,
processor_props,
ranked_data_path,
)
from icon4py.model.common.test_utils.helpers import dallclose, zero_field
from icon4py.model.common.type_alias import wpfloat


@pytest.mark.datatest
def test_compute_wgtfac_c(icon_grid, metrics_savepoint): # noqa: F811 # fixture
def test_compute_wgtfac_c(icon_grid, metrics_savepoint): # fixture
wgtfac_c = zero_field(icon_grid, CellDim, KDim, dtype=wpfloat, extend={KDim: 1})
wgtfac_c_ref = metrics_savepoint.wgtfac_c()
z_ifc = metrics_savepoint.z_ifc()
Expand Down
43 changes: 43 additions & 0 deletions model/common/tests/metric_tests/test_compute_wgtfacq_c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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
# 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 pytest

from icon4py.model.common.metrics.stencils.compute_wgtfacq_c import compute_wgtfacq_c
from icon4py.model.common.test_utils.helpers import dallclose


@pytest.mark.datatest
def test_compute_wgtfacq_c(icon_grid, metrics_savepoint): # fixture
wgtfacq_c_dsl = metrics_savepoint.wgtfacq_c_dsl()
z_ifc = metrics_savepoint.z_ifc()

vertical_end = icon_grid.num_levels

wgtfacq_c = compute_wgtfacq_c(
z_ifc.asnumpy(),
vertical_end,
)
assert dallclose(wgtfacq_c, wgtfacq_c_dsl.asnumpy())
Loading