Skip to content

Commit

Permalink
Don't compute log10(chl) for small chl
Browse files Browse the repository at this point in the history
if chl <= chl_min, we already have log10(chl_min) stored as a parameter.
Avoiding the computation of log10(chl) in those cases prevents the possibility
of a floating point exception, and lets us replace a max() statement with an if
check so it shouldn't affect performance.

This requires adding chl_min to the optics_type (which already has log10chl_min
and log10chl_max).
  • Loading branch information
mnlevy1981 committed Sep 27, 2024
1 parent a3e2f14 commit 5726d94
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/parameterizations/vertical/MOM_opacity.F90
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module MOM_opacity
!! Lookup tables for Ohlmann solar penetration scheme
!! These would naturally exist as private module variables but that is prohibited in MOM6
real :: dlog10chl !< Chl increment within lookup table
real :: chl_min !< Lower bound of Chl in lookup table
real :: log10chl_min !< Lower bound of Chl in lookup table
real :: log10chl_max !< Upper bound of Chl in lookup table
real, allocatable, dimension(:) :: a1_lut,& !< Coefficient for band 1
Expand Down Expand Up @@ -1303,6 +1304,7 @@ subroutine init_ohlmann_table(optics)
call MOM_error(FATAL,"init_ohlmann: Cannot allocate lookup table")
endif

optics%chl_min = chl_tab1a(1)
optics%log10chl_min = log10(chl_tab1a(1))
optics%log10chl_max = log10(chl_tab1a(nval_tab1a))
optics%dlog10chl = (optics%log10chl_max - optics%log10chl_min)/(nval_lut-1)
Expand Down Expand Up @@ -1349,7 +1351,11 @@ function lookup_ohlmann_swpen(chl,optics) result(A)
integer :: n

! Make sure we are in the table
log10chl = max(optics%log10chl_min,min(log10(chl),optics%log10chl_max))
if (chl > optics%chl_min) then
log10chl = min(log10(chl),optics%log10chl_max)
else
log10chl = optics%log10chl_min
endif
! Do a nearest neighbor lookup
n = nint( (log10chl - optics%log10chl_min)/optics%dlog10chl ) + 1

Expand Down

0 comments on commit 5726d94

Please sign in to comment.