From b09862ea9576da8d278177e76eeaac859043bb80 Mon Sep 17 00:00:00 2001 From: wwieder Date: Mon, 23 Nov 2026 05:41:19 -0700 Subject: [PATCH 1/3] Resolve MEGAN compound hash collisions (thujone_b/cubebene_b) bld_hash_table_indices overwrote colliding hash slots, so thujone_b got cubebene_b's emission factors, class and molecular weight. Use linear probing with stored-name comparison, endrun on duplicates or a full table, and allocate hash_table_indices from 0 (gen_hashkey can return 0). Fixes #4345 Co-Authored-By: Claude Opus 5.5 --- src/biogeochem/MEGANFactorsMod.F90 | 73 ++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 8 deletions(-) diff --git a/src/biogeochem/MEGANFactorsMod.F90 b/src/biogeochem/MEGANFactorsMod.F90 index 661bfbdde2..05b2830004 100644 --- a/src/biogeochem/MEGANFactorsMod.F90 +++ b/src/biogeochem/MEGANFactorsMod.F90 @@ -63,12 +63,11 @@ subroutine megan_factors_get( comp_name, factors, class_n, molecwght ) real(r8), intent(out) :: molecwght ! molecular weight of the compound of intrest ! ! LOCAL VARS: - integer :: hashkey, ndx + integer :: ndx character(len=120) :: errmes !----------------------------------------------------------------------- - hashkey = gen_hashkey(comp_name) - ndx = hash_table_indices(hashkey) + ndx = get_table_index(comp_name) if (ndx<1) then errmes = 'megan_factors_get: '//trim(comp_name)//' compound not found in MEGAN table' @@ -114,7 +113,7 @@ subroutine megan_factors_init( filename ) real(r8),allocatable :: class_factors(:) allocate(comp_factors_table(150)) - allocate(hash_table_indices(tbl_hash_sz)) + allocate(hash_table_indices(0:tbl_hash_sz-1)) ! gen_hashkey returns values on [0, tbl_hash_sz-1] call getfil(filename, locfn, 0) @@ -215,18 +214,74 @@ subroutine megan_factors_init( filename ) subroutine bld_hash_table_indices( names ) character(len=*),intent(in) :: names(:) - integer :: n, i, hashkey + integer :: n, i, hashkey, nprobe hash_table_indices(:) = 0 n = size(names) do i=1,n + ! Resolve hash collisions by linear probing to the next free slot + ! (e.g., 'thujone_b' and 'cubebene_b' have the same hash key) hashkey = gen_hashkey(names(i)) + nprobe = 0 + do while (hash_table_indices(hashkey) /= 0) + if (names_match(names(hash_table_indices(hashkey)), names(i))) then + call endrun(msg='MEGAN factors file has duplicate compound name: '//trim(names(i))//' '// & + errMsg(sourcefile, __LINE__)) + end if + nprobe = nprobe + 1 + if (nprobe >= tbl_hash_sz) then + call endrun(msg='MEGAN hash table is full: '//errMsg(sourcefile, __LINE__)) + end if + hashkey = iand(hashkey+1, tbl_hash_sz-1) + end do hash_table_indices(hashkey) = i enddo endsubroutine bld_hash_table_indices + !----------------------------------------------------------------------- + !----------------------------------------------------------------------- + integer function get_table_index( name ) + ! Return the index in comp_names / comp_factors_table of compound name, + ! or 0 if it is not found. Follows the linear probing used in + ! bld_hash_table_indices and checks the stored name, so that compounds + ! with colliding hash keys are distinguished. + character(len=*), intent(in) :: name + + integer :: hashkey, ndx, nprobe + + get_table_index = 0 + hashkey = gen_hashkey(name) + do nprobe = 1, tbl_hash_sz + ndx = hash_table_indices(hashkey) + if (ndx == 0) return + if (names_match(comp_names(ndx), name)) then + get_table_index = ndx + return + end if + hashkey = iand(hashkey+1, tbl_hash_sz-1) + end do + + end function get_table_index + + !----------------------------------------------------------------------- + !----------------------------------------------------------------------- + logical function names_match( name1, name2 ) + ! Compare two compound names, ignoring trailing blanks and anything + ! after a null character (netCDF char padding) + character(len=*), intent(in) :: name1, name2 + + integer :: n1, n2 + + n1 = index(name1, achar(0)) - 1 + if (n1 < 0) n1 = len(name1) + n2 = index(name2, achar(0)) - 1 + if (n2 < 0) n2 = len(name2) + names_match = (trim(name1(1:n1)) == trim(name2(1:n2))) + + end function names_match + !----------------------------------------------------------------------- !----------------------------------------------------------------------- subroutine enter_hash_data( name, data, class_n, molec_wght ) @@ -235,13 +290,15 @@ subroutine enter_hash_data( name, data, class_n, molec_wght ) integer, intent(in) :: class_n real(r8), intent(in) :: molec_wght - integer :: hashkey, ndx + integer :: ndx integer :: nfactors - hashkey = gen_hashkey(name) nfactors = size(data) - ndx = hash_table_indices(hashkey) + ndx = get_table_index(name) + if (ndx < 1) then + call endrun(msg='MEGAN compound not found in hash table: '//trim(name)//' '//errMsg(sourcefile, __LINE__)) + end if allocate (comp_factors_table(ndx)%eff(nfactors)) From 3ab4810fecf5a3e9337918a62174254a87d13e45 Mon Sep 17 00:00:00 2001 From: wwieder Date: Mon, 23 Nov 2026 05:41:19 -0700 Subject: [PATCH 2/3] MEGAN gamma_P: use 24-h mean shaded PAR in Cp (Guenther et al. 2006 eq. 7) The shaded-leaf Cp used instantaneous par_sha_in; par24_sha_in was passed but never used. Fixes #4346 Co-Authored-By: Claude Opus 5.5 --- src/biogeochem/VOCEmissionMod.F90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/biogeochem/VOCEmissionMod.F90 b/src/biogeochem/VOCEmissionMod.F90 index 71de1e3c0e..5ae615eae5 100644 --- a/src/biogeochem/VOCEmissionMod.F90 +++ b/src/biogeochem/VOCEmissionMod.F90 @@ -862,7 +862,7 @@ function get_gamma_P(par_sun_in, par24_sun_in, par240_sun_in, par_sha_in, par24_ gamma_p_LDF = fsun_in * ( cp * alpha * par_sun_in * (1._r8 + alpha*alpha*par_sun_in*par_sun_in)**(-0.5_r8) ) ! SHADE: alpha = ca1 - ca2 * log(par240_sha_in) - cp = ca3 * exp(ca2 * (par_sha_in-par0_shade))*par240_sha_in**(0.6_r8) + cp = ca3 * exp(ca2 * (par24_sha_in-par0_shade))*par240_sha_in**(0.6_r8) gamma_p_LDF = gamma_p_LDF + (1._r8-fsun_in) * (cp*alpha*par_sha_in*(1._r8 + alpha*alpha*par_sha_in*par_sha_in)**(-0.5_r8)) else ! With fixed alpha and cp (from MEGAN User's Guide): From 0008127e4fd4512c5718ae7d1670c7f404435d9c Mon Sep 17 00:00:00 2001 From: wwieder Date: Mon, 23 Nov 2026 05:41:20 -0700 Subject: [PATCH 3/3] MEGAN gamma_A: bound previous-LAI estimate at zero During rapid leaf-out 2*elai240 - elai < 0, giving fnew > 1 and fmat < 0, negative isoprene gamma_A and inflated gamma_A for other classes. Fixes #4347 Co-Authored-By: Claude Opus 5.5 --- src/biogeochem/VOCEmissionMod.F90 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/biogeochem/VOCEmissionMod.F90 b/src/biogeochem/VOCEmissionMod.F90 index 5ae615eae5..2d6d1a33db 100644 --- a/src/biogeochem/VOCEmissionMod.F90 +++ b/src/biogeochem/VOCEmissionMod.F90 @@ -1056,6 +1056,10 @@ function get_gamma_A(ivt_in, elai240_in, elai_in, nclass_in) if ( (elai240_in > 0.0_r8) .and. (elai240_in < 1.e30_r8) )then elai_prev = 2._r8*elai240_in-elai_in ! have accumulated average lai over last 10 days + ! During rapid leaf-out elai can exceed 2*elai240, which would give a negative + ! previous LAI estimate and leaf-age fractions outside [0,1] (fnew > 1, fmat < 0). + ! A negative previous LAI is not physical; bound it at zero (all leaves new). + elai_prev = max(0._r8, elai_prev) if (elai_prev == elai_in) then fnew = 0.0_r8 fgro = 0.0_r8