Skip to content

Commit

Permalink
Tracer advection stencils zero copy (#106)
Browse files Browse the repository at this point in the history
(feat)  
- stencil to set field to zero for Field[[CellDim], float] and Field[[CellDim, KDim], float]
  • Loading branch information
halungge authored Oct 19, 2022
1 parent a40fe69 commit 24a5120
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
28 changes: 28 additions & 0 deletions advection/src/icon4py/advection/set_zero_c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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

from functional.common import Field
from functional.ffront.decorator import field_operator, program
from functional.ffront.fbuiltins import broadcast

from icon4py.common.dimension import CellDim


@field_operator
def _set_zero_c() -> Field[[CellDim], float]:
return broadcast(0.0, (CellDim, ))


@program
def set_zero_c(field: Field[[CellDim], float]):
_set_zero_c(out=field)
28 changes: 28 additions & 0 deletions advection/src/icon4py/advection/set_zero_c_k.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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

from functional.common import Field
from functional.ffront.decorator import field_operator, program
from functional.ffront.fbuiltins import broadcast

from icon4py.common.dimension import CellDim, KDim


@field_operator
def _set_zero_c_k() -> Field[[CellDim, KDim], float]:
return broadcast(0.0, (CellDim, KDim))


@program
def set_zero_c_k(field: Field[[CellDim, KDim], float]):
_set_zero_c_k(out=field)
27 changes: 27 additions & 0 deletions advection/tests/test_set_zero_c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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

from icon4py.advection.set_zero_c import set_zero_c
from icon4py.common.dimension import CellDim
from icon4py.testutils.simple_mesh import SimpleMesh
from icon4py.testutils.utils import random_field, zero_field


def test_set_zero_cell_k():
mesh = SimpleMesh()
field = random_field(mesh, CellDim)

set_zero_c(field, offset_provider={})
assert np.allclose(field, zero_field(mesh, CellDim))
27 changes: 27 additions & 0 deletions advection/tests/test_set_zero_c_k.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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

from icon4py.advection.set_zero_c_k import set_zero_c_k
from icon4py.common.dimension import CellDim, KDim
from icon4py.testutils.simple_mesh import SimpleMesh
from icon4py.testutils.utils import random_field, zero_field


def test_set_zero_c_k():
mesh = SimpleMesh()
field = random_field(mesh, CellDim, KDim)

set_zero_c_k(field, offset_provider={})
assert np.allclose(field, zero_field(mesh, CellDim, KDim))

0 comments on commit 24a5120

Please sign in to comment.