Skip to content

Commit 9cfeb2f

Browse files
authored
patching with @muellch vel adv bug fixes (#280)
1 parent be34d20 commit 9cfeb2f

File tree

6 files changed

+31
-48
lines changed

6 files changed

+31
-48
lines changed

atm_dyn_iconam/src/icon4py/atm_dyn_iconam/mo_velocity_advection_stencil_14.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@
2121
def _mo_velocity_advection_stencil_14(
2222
ddqz_z_half: Field[[CellDim, KDim], float],
2323
z_w_con_c: Field[[CellDim, KDim], float],
24-
cfl_clipping: Field[[CellDim, KDim], bool],
25-
pre_levelmask: Field[[CellDim, KDim], bool],
26-
vcfl: Field[[CellDim, KDim], float],
2724
cfl_w_limit: float,
2825
dtime: float,
2926
) -> tuple[
30-
Field[[CellDim, KDim], bool],
3127
Field[[CellDim, KDim], bool],
3228
Field[[CellDim, KDim], float],
3329
Field[[CellDim, KDim], float],
@@ -38,8 +34,6 @@ def _mo_velocity_advection_stencil_14(
3834
False,
3935
)
4036

41-
pre_levelmask = where(cfl_clipping, broadcast(True, (CellDim, KDim)), False)
42-
4337
vcfl = where(cfl_clipping, z_w_con_c * dtime / ddqz_z_half, 0.0)
4438

4539
z_w_con_c = where(
@@ -50,26 +44,22 @@ def _mo_velocity_advection_stencil_14(
5044
(cfl_clipping) & (vcfl > 0.85), 0.85 * ddqz_z_half / dtime, z_w_con_c
5145
)
5246

53-
return cfl_clipping, pre_levelmask, vcfl, z_w_con_c
47+
return cfl_clipping, vcfl, z_w_con_c
5448

5549

5650
@program
5751
def mo_velocity_advection_stencil_14(
5852
ddqz_z_half: Field[[CellDim, KDim], float],
5953
z_w_con_c: Field[[CellDim, KDim], float],
6054
cfl_clipping: Field[[CellDim, KDim], bool],
61-
pre_levelmask: Field[[CellDim, KDim], bool],
6255
vcfl: Field[[CellDim, KDim], float],
6356
cfl_w_limit: float,
6457
dtime: float,
6558
):
6659
_mo_velocity_advection_stencil_14(
6760
ddqz_z_half,
6861
z_w_con_c,
69-
cfl_clipping,
70-
pre_levelmask,
71-
vcfl,
7262
cfl_w_limit,
7363
dtime,
74-
out=(cfl_clipping, pre_levelmask, vcfl, z_w_con_c),
64+
out=(cfl_clipping, vcfl, z_w_con_c),
7565
)

atm_dyn_iconam/src/icon4py/atm_dyn_iconam/mo_velocity_advection_stencil_18.py

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

2020
@field_operator
2121
def _mo_velocity_advection_stencil_18(
22-
levelmask: Field[[KDim], bool],
22+
levmask: Field[[KDim], bool],
2323
cfl_clipping: Field[[CellDim, KDim], bool],
2424
owner_mask: Field[[CellDim], bool],
2525
z_w_con_c: Field[[CellDim, KDim], float],
@@ -33,7 +33,7 @@ def _mo_velocity_advection_stencil_18(
3333
dtime: float,
3434
) -> Field[[CellDim, KDim], float]:
3535
difcoef = where(
36-
levelmask & cfl_clipping & owner_mask,
36+
levmask & cfl_clipping & owner_mask,
3737
scalfac_exdiff
3838
* minimum(
3939
0.85 - cfl_w_limit * dtime,
@@ -43,7 +43,7 @@ def _mo_velocity_advection_stencil_18(
4343
)
4444

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

5555
@program
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,

atm_dyn_iconam/src/icon4py/atm_dyn_iconam/mo_velocity_advection_stencil_20.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
CellDim,
3030
E2C2EODim,
3131
E2CDim,
32-
E2VDim,
3332
EdgeDim,
3433
KDim,
3534
Koff,
@@ -74,10 +73,12 @@ def _mo_velocity_advection_stencil_20(
7473
ddt_vn_adv = where(
7574
(levelmask | levelmask(Koff[1])) & (abs(w_con_e) > cfl_w_limit * ddqz_z_full_e),
7675
ddt_vn_adv
77-
+ difcoef * area_edge * neighbor_sum(geofac_grdiv * vn(E2C2EO), axis=E2C2EODim)
78-
+ tangent_orientation
79-
* inv_primal_edge_length
80-
* neighbor_sum(zeta(E2V), axis=E2VDim),
76+
+ difcoef * area_edge * (
77+
neighbor_sum(geofac_grdiv * vn(E2C2EO), axis=E2C2EODim)
78+
+ tangent_orientation
79+
* inv_primal_edge_length
80+
* (zeta(E2V[1]) - zeta(E2V[0]))
81+
),
8182
ddt_vn_adv,
8283
)
8384
return ddt_vn_adv

atm_dyn_iconam/tests/test_mo_velocity_advection_stencil_14.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ def mo_velocity_advection_stencil_14_numpy(
3333
np.ones([num_rows, num_cols]),
3434
np.zeros_like(z_w_con_c),
3535
)
36-
num_rows, num_cols = cfl_clipping.shape
37-
pre_levelmask = np.where(
38-
cfl_clipping == 1.0, np.ones([num_rows, num_cols]), np.zeros_like(cfl_clipping)
39-
)
36+
4037
vcfl = np.where(cfl_clipping == 1.0, z_w_con_c * dtime / ddqz_z_half, 0.0)
4138
z_w_con_c = np.where(
4239
(cfl_clipping == 1.0) & (vcfl < -0.85), -0.85 * ddqz_z_half / dtime, z_w_con_c
@@ -45,7 +42,7 @@ def mo_velocity_advection_stencil_14_numpy(
4542
(cfl_clipping == 1.0) & (vcfl > 0.85), 0.85 * ddqz_z_half / dtime, z_w_con_c
4643
)
4744

48-
return cfl_clipping, pre_levelmask, vcfl, z_w_con_c
45+
return cfl_clipping, vcfl, z_w_con_c
4946

5047

5148
def test_mo_velocity_advection_stencil_14():
@@ -54,16 +51,12 @@ def test_mo_velocity_advection_stencil_14():
5451
ddqz_z_half = random_field(mesh, CellDim, KDim)
5552
z_w_con_c = random_field(mesh, CellDim, KDim)
5653
cfl_clipping = random_mask(mesh, CellDim, KDim, dtype=bool)
57-
pre_levelmask = random_mask(
58-
mesh, CellDim, KDim, dtype=bool
59-
) # TODO should be just a K field
6054
vcfl = zero_field(mesh, CellDim, KDim)
6155
cfl_w_limit = 5.0
6256
dtime = 9.0
6357

6458
(
6559
cfl_clipping_ref,
66-
pre_levelmask_ref,
6760
vcfl_ref,
6861
z_w_con_c_ref,
6962
) = mo_velocity_advection_stencil_14_numpy(
@@ -77,13 +70,11 @@ def test_mo_velocity_advection_stencil_14():
7770
ddqz_z_half,
7871
z_w_con_c,
7972
cfl_clipping,
80-
pre_levelmask,
8173
vcfl,
8274
cfl_w_limit,
8375
dtime,
8476
offset_provider={},
8577
)
8678
assert np.allclose(cfl_clipping, cfl_clipping_ref)
87-
assert np.allclose(pre_levelmask, pre_levelmask_ref)
8879
assert np.allclose(vcfl, vcfl_ref)
8980
assert np.allclose(z_w_con_c, z_w_con_c_ref)

atm_dyn_iconam/tests/test_mo_velocity_advection_stencil_18.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
def mo_velocity_advection_stencil_18_numpy(
2525
c2e2c0: np.array,
26-
levelmask: np.array,
26+
levmask: np.array,
2727
cfl_clipping: np.array,
2828
owner_mask: np.array,
2929
z_w_con_c: np.array,
@@ -36,13 +36,13 @@ def mo_velocity_advection_stencil_18_numpy(
3636
cfl_w_limit: float,
3737
dtime: float,
3838
):
39-
levelmask = np.expand_dims(levelmask, axis=0)
39+
levmask = np.expand_dims(levmask, axis=0)
4040
owner_mask = np.expand_dims(owner_mask, axis=-1)
4141
area = np.expand_dims(area, axis=-1)
4242
geofac_n2s = np.expand_dims(geofac_n2s, axis=-1)
4343

4444
difcoef = np.where(
45-
(levelmask == 1) & (cfl_clipping == 1) & (owner_mask == 1),
45+
(levmask == 1) & (cfl_clipping == 1) & (owner_mask == 1),
4646
scalfac_exdiff
4747
* np.minimum(
4848
0.85 - cfl_w_limit * dtime,
@@ -52,7 +52,7 @@ def mo_velocity_advection_stencil_18_numpy(
5252
)
5353

5454
ddt_w_adv = np.where(
55-
(levelmask == 1) & (cfl_clipping == 1) & (owner_mask == 1),
55+
(levmask == 1) & (cfl_clipping == 1) & (owner_mask == 1),
5656
ddt_w_adv + difcoef * area * np.sum(w[c2e2c0] * geofac_n2s, axis=1),
5757
ddt_w_adv,
5858
)
@@ -63,7 +63,7 @@ def mo_velocity_advection_stencil_18_numpy(
6363
def test_mo_velocity_advection_stencil_18():
6464
mesh = SimpleMesh()
6565

66-
levelmask = random_mask(mesh, KDim)
66+
levmask = random_mask(mesh, KDim)
6767
cfl_clipping = random_mask(mesh, CellDim, KDim)
6868
owner_mask = random_mask(mesh, CellDim)
6969
z_w_con_c = random_field(mesh, CellDim, KDim)
@@ -78,7 +78,7 @@ def test_mo_velocity_advection_stencil_18():
7878

7979
ref = mo_velocity_advection_stencil_18_numpy(
8080
mesh.c2e2cO,
81-
np.asarray(levelmask),
81+
np.asarray(levmask),
8282
np.asarray(cfl_clipping),
8383
np.asarray(owner_mask),
8484
np.asarray(z_w_con_c),
@@ -93,7 +93,7 @@ def test_mo_velocity_advection_stencil_18():
9393
)
9494

9595
mo_velocity_advection_stencil_18(
96-
levelmask,
96+
levmask,
9797
cfl_clipping,
9898
owner_mask,
9999
z_w_con_c,

atm_dyn_iconam/tests/test_mo_velocity_advection_stencil_20.py

Lines changed: 9 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,11 @@ 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 * area_edge * (np.sum(geofac_grdiv * vn[e2c2eO], axis=1)
82+
+ tangent_orientation * inv_primal_edge_length * (zeta[e2v][:, 1] - zeta[e2v][:, 0])),
8283
ddt_vn_adv,
8384
)
8485
return ddt_vn_adv
@@ -87,7 +88,7 @@ def mo_velocity_advection_stencil_20_numpy(
8788
def test_mo_velocity_advection_stencil_20():
8889
mesh = SimpleMesh()
8990

90-
levelmask = random_mask(mesh, KDim)
91+
levelmask = random_mask(mesh, KDim, extend={KDim: 1})
9192
c_lin_e = random_field(mesh, EdgeDim, E2CDim)
9293
z_w_con_c_full = random_field(mesh, CellDim, KDim)
9394
ddqz_z_full_e = random_field(mesh, EdgeDim, KDim)
@@ -144,5 +145,5 @@ def test_mo_velocity_advection_stencil_20():
144145
"E2V": mesh.get_e2v_offset_provider(),
145146
},
146147
)
147-
148+
148149
assert np.allclose(ddt_vn_adv[:, :-1], ddt_vn_adv_ref[:, :-1])

0 commit comments

Comments
 (0)