Skip to content

Commit f929579

Browse files
authored
Remove pre_levelmask from vel. advection and fix bugs (#258)
1 parent 841b772 commit f929579

File tree

6 files changed

+36
-46
lines changed

6 files changed

+36
-46
lines changed

model/atmosphere/dycore/src/icon4py/model/atmosphere/dycore/mo_velocity_advection_stencil_14.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@
2222
def _mo_velocity_advection_stencil_14(
2323
ddqz_z_half: Field[[CellDim, KDim], float],
2424
z_w_con_c: Field[[CellDim, KDim], float],
25-
cfl_clipping: Field[[CellDim, KDim], bool],
26-
pre_levelmask: Field[[CellDim, KDim], bool],
27-
vcfl: Field[[CellDim, KDim], float],
2825
cfl_w_limit: float,
2926
dtime: float,
3027
) -> tuple[
31-
Field[[CellDim, KDim], bool],
3228
Field[[CellDim, KDim], bool],
3329
Field[[CellDim, KDim], float],
3430
Field[[CellDim, KDim], float],
@@ -39,34 +35,30 @@ def _mo_velocity_advection_stencil_14(
3935
False,
4036
)
4137

42-
pre_levelmask = where(cfl_clipping, broadcast(True, (CellDim, KDim)), False)
43-
38+
# TOOO: As soon as reductions of arbitrar dimensions are possible in gt4py, this stencil
39+
# should reduce the vertical cfl to a scalar and the levmask to a per level boolean field (see Fortran dycore).
4440
vcfl = where(cfl_clipping, z_w_con_c * dtime / ddqz_z_half, 0.0)
4541

4642
z_w_con_c = where((cfl_clipping) & (vcfl < -0.85), -0.85 * ddqz_z_half / dtime, z_w_con_c)
4743

4844
z_w_con_c = where((cfl_clipping) & (vcfl > 0.85), 0.85 * ddqz_z_half / dtime, z_w_con_c)
4945

50-
return cfl_clipping, pre_levelmask, vcfl, z_w_con_c
46+
return cfl_clipping, vcfl, z_w_con_c
5147

5248

5349
@program(grid_type=GridType.UNSTRUCTURED)
5450
def mo_velocity_advection_stencil_14(
5551
ddqz_z_half: Field[[CellDim, KDim], float],
5652
z_w_con_c: Field[[CellDim, KDim], float],
5753
cfl_clipping: Field[[CellDim, KDim], bool],
58-
pre_levelmask: Field[[CellDim, KDim], bool],
5954
vcfl: Field[[CellDim, KDim], float],
6055
cfl_w_limit: float,
6156
dtime: float,
6257
):
6358
_mo_velocity_advection_stencil_14(
6459
ddqz_z_half,
6560
z_w_con_c,
66-
cfl_clipping,
67-
pre_levelmask,
68-
vcfl,
6961
cfl_w_limit,
7062
dtime,
71-
out=(cfl_clipping, pre_levelmask, vcfl, z_w_con_c),
63+
out=(cfl_clipping, vcfl, z_w_con_c),
7264
)

model/atmosphere/dycore/src/icon4py/model/atmosphere/dycore/mo_velocity_advection_stencil_18.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
@field_operator
2222
def _mo_velocity_advection_stencil_18(
23-
levelmask: Field[[KDim], bool],
23+
levmask: Field[[KDim], bool],
2424
cfl_clipping: Field[[CellDim, KDim], bool],
2525
owner_mask: Field[[CellDim], bool],
2626
z_w_con_c: Field[[CellDim, KDim], float],
@@ -34,7 +34,7 @@ def _mo_velocity_advection_stencil_18(
3434
dtime: float,
3535
) -> Field[[CellDim, KDim], float]:
3636
difcoef = where(
37-
levelmask & cfl_clipping & owner_mask,
37+
levmask & cfl_clipping & owner_mask,
3838
scalfac_exdiff
3939
* minimum(
4040
0.85 - cfl_w_limit * dtime,
@@ -44,7 +44,7 @@ def _mo_velocity_advection_stencil_18(
4444
)
4545

4646
ddt_w_adv = where(
47-
levelmask & cfl_clipping & owner_mask,
47+
levmask & cfl_clipping & owner_mask,
4848
ddt_w_adv + difcoef * area * neighbor_sum(w(C2E2CO) * geofac_n2s, axis=C2E2CODim),
4949
ddt_w_adv,
5050
)
@@ -54,7 +54,7 @@ def _mo_velocity_advection_stencil_18(
5454

5555
@program(grid_type=GridType.UNSTRUCTURED)
5656
def mo_velocity_advection_stencil_18(
57-
levelmask: Field[[KDim], bool],
57+
levmask: Field[[KDim], bool],
5858
cfl_clipping: Field[[CellDim, KDim], bool],
5959
owner_mask: Field[[CellDim], bool],
6060
z_w_con_c: Field[[CellDim, KDim], float],
@@ -68,7 +68,7 @@ def mo_velocity_advection_stencil_18(
6868
dtime: float,
6969
):
7070
_mo_velocity_advection_stencil_18(
71-
levelmask,
71+
levmask,
7272
cfl_clipping,
7373
owner_mask,
7474
z_w_con_c,

model/atmosphere/dycore/src/icon4py/model/atmosphere/dycore/mo_velocity_advection_stencil_20.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
CellDim,
3131
E2C2EODim,
3232
E2CDim,
33-
E2VDim,
3433
EdgeDim,
3534
KDim,
3635
Koff,
@@ -75,8 +74,12 @@ def _mo_velocity_advection_stencil_20(
7574
ddt_vn_adv = where(
7675
(levelmask | levelmask(Koff[1])) & (abs(w_con_e) > cfl_w_limit * ddqz_z_full_e),
7776
ddt_vn_adv
78-
+ difcoef * area_edge * neighbor_sum(geofac_grdiv * vn(E2C2EO), axis=E2C2EODim)
79-
+ tangent_orientation * inv_primal_edge_length * neighbor_sum(zeta(E2V), axis=E2VDim),
77+
+ difcoef
78+
* area_edge
79+
* (
80+
neighbor_sum(geofac_grdiv * vn(E2C2EO), axis=E2C2EODim)
81+
+ tangent_orientation * inv_primal_edge_length * (zeta(E2V[1]) - zeta(E2V[0]))
82+
),
8083
ddt_vn_adv,
8184
)
8285
return ddt_vn_adv
@@ -114,5 +117,5 @@ def mo_velocity_advection_stencil_20(
114117
cfl_w_limit,
115118
scalfac_exdiff,
116119
d_time,
117-
out=ddt_vn_adv[:, :-1],
120+
out=(ddt_vn_adv),
118121
)

model/atmosphere/dycore/tests/test_mo_velocity_advection_stencil_14.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
class TestMoVelocityAdvectionStencil14(StencilTest):
3030
PROGRAM = mo_velocity_advection_stencil_14
31-
OUTPUTS = ("cfl_clipping", "pre_levelmask", "vcfl", "z_w_con_c")
31+
OUTPUTS = ("cfl_clipping", "vcfl", "z_w_con_c")
3232

3333
@staticmethod
3434
def reference(
@@ -41,11 +41,6 @@ def reference(
4141
np.zeros_like(z_w_con_c),
4242
)
4343
num_rows, num_cols = cfl_clipping.shape
44-
pre_levelmask = np.where(
45-
cfl_clipping == 1.0,
46-
np.ones([num_rows, num_cols]),
47-
np.zeros_like(cfl_clipping),
48-
)
4944
vcfl = np.where(cfl_clipping == 1.0, z_w_con_c * dtime / ddqz_z_half, 0.0)
5045
z_w_con_c = np.where(
5146
(cfl_clipping == 1.0) & (vcfl < -0.85),
@@ -58,7 +53,6 @@ def reference(
5853

5954
return dict(
6055
cfl_clipping=cfl_clipping,
61-
pre_levelmask=pre_levelmask,
6256
vcfl=vcfl,
6357
z_w_con_c=z_w_con_c,
6458
)
@@ -68,9 +62,6 @@ def input_data(self, mesh):
6862
ddqz_z_half = random_field(mesh, CellDim, KDim)
6963
z_w_con_c = random_field(mesh, CellDim, KDim)
7064
cfl_clipping = random_mask(mesh, CellDim, KDim, dtype=bool)
71-
pre_levelmask = random_mask(
72-
mesh, CellDim, KDim, dtype=bool
73-
) # TODO should be just a K field
7465
vcfl = zero_field(mesh, CellDim, KDim)
7566
cfl_w_limit = 5.0
7667
dtime = 9.0
@@ -79,7 +70,6 @@ def input_data(self, mesh):
7970
ddqz_z_half=ddqz_z_half,
8071
z_w_con_c=z_w_con_c,
8172
cfl_clipping=cfl_clipping,
82-
pre_levelmask=pre_levelmask,
8373
vcfl=vcfl,
8474
cfl_w_limit=cfl_w_limit,
8575
dtime=dtime,

model/atmosphere/dycore/tests/test_mo_velocity_advection_stencil_18.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TestMoVelocityAdvectionStencil18(StencilTest):
3232
@staticmethod
3333
def reference(
3434
mesh,
35-
levelmask: np.array,
35+
levmask: np.array,
3636
cfl_clipping: np.array,
3737
owner_mask: np.array,
3838
z_w_con_c: np.array,
@@ -46,13 +46,13 @@ def reference(
4646
dtime: float,
4747
**kwargs,
4848
):
49-
levelmask = np.expand_dims(levelmask, axis=0)
49+
levmask = np.expand_dims(levmask, axis=0)
5050
owner_mask = np.expand_dims(owner_mask, axis=-1)
5151
area = np.expand_dims(area, axis=-1)
5252
geofac_n2s = np.expand_dims(geofac_n2s, axis=-1)
5353

5454
difcoef = np.where(
55-
(levelmask == 1) & (cfl_clipping == 1) & (owner_mask == 1),
55+
(levmask == 1) & (cfl_clipping == 1) & (owner_mask == 1),
5656
scalfac_exdiff
5757
* np.minimum(
5858
0.85 - cfl_w_limit * dtime,
@@ -62,7 +62,7 @@ def reference(
6262
)
6363

6464
ddt_w_adv = np.where(
65-
(levelmask == 1) & (cfl_clipping == 1) & (owner_mask == 1),
65+
(levmask == 1) & (cfl_clipping == 1) & (owner_mask == 1),
6666
ddt_w_adv + difcoef * area * np.sum(w[mesh.c2e2cO] * geofac_n2s, axis=1),
6767
ddt_w_adv,
6868
)
@@ -71,7 +71,7 @@ def reference(
7171

7272
@pytest.fixture
7373
def input_data(self, mesh):
74-
levelmask = random_mask(mesh, KDim)
74+
levmask = random_mask(mesh, KDim)
7575
cfl_clipping = random_mask(mesh, CellDim, KDim)
7676
owner_mask = random_mask(mesh, CellDim)
7777
z_w_con_c = random_field(mesh, CellDim, KDim)
@@ -85,7 +85,7 @@ def input_data(self, mesh):
8585
dtime = 2.0
8686

8787
return dict(
88-
levelmask=levelmask,
88+
levmask=levmask,
8989
cfl_clipping=cfl_clipping,
9090
owner_mask=owner_mask,
9191
z_w_con_c=z_w_con_c,

model/atmosphere/dycore/tests/test_mo_velocity_advection_stencil_20.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def mo_velocity_advection_stencil_20_numpy(
5050
w_con_e = np.zeros_like(vn)
5151
difcoef = np.zeros_like(vn)
5252

53-
levelmask_offset_1 = np.roll(levelmask, shift=-1, axis=0)
53+
levelmask_offset_0 = levelmask[:-1]
54+
levelmask_offset_1 = levelmask[1:]
5455

5556
c_lin_e = np.expand_dims(c_lin_e, axis=-1)
5657
geofac_grdiv = np.expand_dims(geofac_grdiv, axis=-1)
@@ -59,12 +60,12 @@ def mo_velocity_advection_stencil_20_numpy(
5960
inv_primal_edge_length = np.expand_dims(inv_primal_edge_length, axis=-1)
6061

6162
w_con_e = np.where(
62-
(levelmask == 1) | (levelmask_offset_1 == 1),
63+
(levelmask_offset_0) | (levelmask_offset_1),
6364
np.sum(c_lin_e * z_w_con_c_full[e2c], axis=1),
6465
w_con_e,
6566
)
6667
difcoef = np.where(
67-
((levelmask == 1) | (levelmask_offset_1 == 1))
68+
((levelmask_offset_0) | (levelmask_offset_1))
6869
& (np.abs(w_con_e) > cfl_w_limit * ddqz_z_full_e),
6970
scalfac_exdiff
7071
* np.minimum(
@@ -74,11 +75,15 @@ def mo_velocity_advection_stencil_20_numpy(
7475
difcoef,
7576
)
7677
ddt_vn_adv = np.where(
77-
((levelmask == 1) | (levelmask_offset_1 == 1))
78+
((levelmask_offset_0) | (levelmask_offset_1))
7879
& (np.abs(w_con_e) > cfl_w_limit * ddqz_z_full_e),
7980
ddt_vn_adv
80-
+ difcoef * area_edge * np.sum(geofac_grdiv * vn[e2c2eO], axis=1)
81-
+ tangent_orientation * inv_primal_edge_length * np.sum(zeta[e2v], axis=1),
81+
+ difcoef
82+
* area_edge
83+
* (
84+
np.sum(geofac_grdiv * vn[e2c2eO], axis=1)
85+
+ tangent_orientation * inv_primal_edge_length * (zeta[e2v][:, 1] - zeta[e2v][:, 0])
86+
),
8287
ddt_vn_adv,
8388
)
8489
return ddt_vn_adv
@@ -87,7 +92,7 @@ def mo_velocity_advection_stencil_20_numpy(
8792
def test_mo_velocity_advection_stencil_20():
8893
mesh = SimpleMesh()
8994

90-
levelmask = random_mask(mesh, KDim)
95+
levelmask = random_mask(mesh, KDim, extend={KDim: 1})
9196
c_lin_e = random_field(mesh, EdgeDim, E2CDim)
9297
z_w_con_c_full = random_field(mesh, CellDim, KDim)
9398
ddqz_z_full_e = random_field(mesh, EdgeDim, KDim)
@@ -145,4 +150,4 @@ def test_mo_velocity_advection_stencil_20():
145150
},
146151
)
147152

148-
assert np.allclose(ddt_vn_adv[:, :-1], ddt_vn_adv_ref[:, :-1])
153+
assert np.allclose(ddt_vn_adv, ddt_vn_adv_ref)

0 commit comments

Comments
 (0)