Skip to content

Commit 1df7cce

Browse files
committed
Using safe_denominator to prevent division by zero.
1 parent 636ab95 commit 1df7cce

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/mam4xx/hetfrz.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,14 +1020,18 @@ void calculate_coated_fraction(
10201020

10211021
const Real n_so4_monolayers_dust = 1.0;
10221022
const Real dr_so4_monolayers_dust = n_so4_monolayers_dust * 4.76e-10;
1023+
// NOTE: To prevent division by zero, we use the routine safe_denominator
1024+
// that returns a factor (1/value). In this case, value is coat_ratio2.
10231025
Real coat_ratio2 =
10241026
haero::max(6.0 * dr_so4_monolayers_dust * vol_core[0], 0.0);
1025-
dstcoat[0] = coat_ratio1 / coat_ratio2;
1027+
Real mult_coat_ratio2 = FloatingPoint<Real>::safe_denominator(coat_ratio2);
1028+
dstcoat[0] = coat_ratio1 * mult_coat_ratio2;
10261029

10271030
// dust_a1
10281031
coat_ratio1 = vol_shell[1] * (r_dust_a1 * 2.0) * fac_volsfc_dust_a1;
10291032
coat_ratio2 = haero::max(6.0 * dr_so4_monolayers_dust * vol_core[1], 0.0);
1030-
dstcoat[1] = coat_ratio1 / coat_ratio2;
1033+
mult_coat_ratio2 = FloatingPoint<Real>::safe_denominator(coat_ratio2);
1034+
dstcoat[1] = coat_ratio1 * mult_coat_ratio2;
10311035

10321036
// dust_a3
10331037
vol_shell[2] = so4mc / (specdens_so4 * air_density) +
@@ -1038,7 +1042,8 @@ void calculate_coated_fraction(
10381042
vol_core[2] = dmc / (specdens_dst * air_density);
10391043
coat_ratio1 = vol_shell[2] * (r_dust_a3 * 2.0) * fac_volsfc_dust_a3;
10401044
coat_ratio2 = haero::max(6.0 * dr_so4_monolayers_dust * vol_core[2], 0.0);
1041-
dstcoat[2] = coat_ratio1 / coat_ratio2;
1045+
mult_coat_ratio2 = FloatingPoint<Real>::safe_denominator(coat_ratio2);
1046+
dstcoat[2] = coat_ratio1 * mult_coat_ratio2;
10421047

10431048
for (int ispec = 0; ispec < Hetfrz::hetfrz_aer_nspec; ++ispec) {
10441049
dstcoat[ispec] = utils::min_max_bound(0.001, 1.0, dstcoat[ispec]);

0 commit comments

Comments
 (0)