Skip to content

Commit a987dd0

Browse files
authored
Merge branch 'tcclevenger/eamxx/fpe_shoc_fix' (PR E3SM-Project#7368)
Fixes FPE introduced in E3SM-Project#7188 from temporary values being computed which attempt to take sqrt(some negative value). These values weren't being used, so not an actual bug. [BFB]
2 parents 9e68ba7 + a9ac492 commit a987dd0

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

components/eamxx/src/physics/shoc/impl/shoc_compute_shoc_mix_shoc_length_impl.hpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,39 @@ ::compute_shoc_mix_shoc_length(
2525
const Int nlev_pack = ekat::npack<Spack>(nlev);
2626
const auto maxlen = scream::shoc::Constants<Scalar>::maxlen;
2727
const auto vk = C::Karman;
28-
28+
2929
// Eddy turnover timescale
3030
const Scalar tscale = 400;
3131

3232
Kokkos::parallel_for(Kokkos::TeamVectorRange(team, nlev_pack), [&] (const Int& k) {
3333
const Spack tkes = ekat::sqrt(tke(k));
3434
const Spack brunt2 = ekat::max(0, brunt(k));
3535

36-
if (shoc_1p5tke){
37-
38-
// If 1.5 TKE closure then set length scale to vertical grid spacing for
39-
// cells with unstable brunt vaisalla frequency. Otherwise, overwrite the length
40-
// scale in stable cells with the new definition.
36+
if (shoc_1p5tke){
37+
// If 1.5 TKE closure then set length scale to vertical grid spacing for
38+
// cells with unstable brunt vaisalla frequency. Otherwise, overwrite the length
39+
// scale in stable cells with the new definition.
4140

42-
// Search for stable cells
43-
const auto stable_mask = brunt(k) > 0;
41+
// Search for stable cells
42+
const auto stable_mask = brunt(k) > 0;
4443

45-
// Define length scale for stable cells
46-
const auto length_tmp = ekat::sqrt(0.76*tk(k)/0.1/ekat::sqrt(brunt(k) + 1.e-10));
47-
// Limit the stability corrected length scale between 0.1*dz and dz
48-
const auto limited_len = ekat::min(dz_zt(k),ekat::max(0.1*dz_zt(k),length_tmp));
44+
// To avoid FPE when calculating sqrt(brunt), set brunt_tmp=0 in the case brunt<1.
45+
Spack brunt_tmp(stable_mask, brunt(k));
4946

50-
// Set length scale to vertical grid if unstable, otherwise the stability adjusted value.
51-
shoc_mix(k).set(stable_mask, limited_len, dz_zt(k));
47+
// Define length scale for stable cells
48+
const auto length_tmp = ekat::sqrt(0.76*tk(k)/0.1/ekat::sqrt(brunt_tmp + 1.e-10));
49+
// Limit the stability corrected length scale between 0.1*dz and dz
50+
const auto limited_len = ekat::min(dz_zt(k),ekat::max(0.1*dz_zt(k),length_tmp));
5251

53-
}else{
54-
shoc_mix(k) = ekat::min(maxlen,
55-
sp(2.8284)*(ekat::sqrt(1/((1/(tscale*tkes*vk*zt_grid(k)))
56-
+ (1/(tscale*tkes*l_inf))
57-
+ sp(0.01)*(brunt2/tke(k)))))/length_fac);
52+
// Set length scale to vertical grid if unstable, otherwise the stability adjusted value.
53+
shoc_mix(k).set(stable_mask, limited_len, dz_zt(k));
54+
} else{
55+
shoc_mix(k) = ekat::min(maxlen,
56+
sp(2.8284)*(ekat::sqrt(1/((1/(tscale*tkes*vk*zt_grid(k)))
57+
+ (1/(tscale*tkes*l_inf))
58+
+ sp(0.01)*(brunt2/tke(k)))))/length_fac);
5859

59-
}
60+
}
6061
});
6162
}
6263

0 commit comments

Comments
 (0)