From e4db4faeda5fb0bc6d75831c8ff31440853dffb6 Mon Sep 17 00:00:00 2001 From: cfranken Date: Fri, 6 Nov 2026 18:36:15 -0800 Subject: [PATCH] Fix shared leaf-area quadrature for projection and scattering --- Project.toml | 2 +- docs/src/pages/tutorials/bilambertian.jl | 9 +++ src/canopy_scattering/bilambertian_fourier.jl | 25 ++------ src/canopy_scattering/projection_geometry.jl | 64 +++++++++---------- src/canopy_scattering/specular.jl | 2 +- src/canopy_scattering/z_matrices.jl | 6 +- src/canopy_structure/components.jl | 23 ++++--- src/foursail/foursail.jl | 7 +- test/runtests.jl | 2 + test/test_CanopyOptics.jl | 10 +-- test/test_leaf_quadrature.jl | 34 ++++++++++ 11 files changed, 108 insertions(+), 76 deletions(-) create mode 100644 test/test_leaf_quadrature.jl diff --git a/Project.toml b/Project.toml index 9fe4c8a..b3717ad 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "CanopyOptics" uuid = "a18e34a6-5dbe-4f38-a44b-e5141852e7a7" authors = ["cfranken "] -version = "0.2.0" +version = "0.2.1" [deps] CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba" diff --git a/docs/src/pages/tutorials/bilambertian.jl b/docs/src/pages/tutorials/bilambertian.jl index fbda25a..d166c77 100644 --- a/docs/src/pages/tutorials/bilambertian.jl +++ b/docs/src/pages/tutorials/bilambertian.jl @@ -24,6 +24,11 @@ quadrature = CanopyOptics.CanopyQuadrature(n_leaf = 64) # moments in one call. The array layout is `Z[i_out, j_in, m+1]` for the # common `0:m_max` range. m_max = 8 +# Use the same leaf quadrature for extinction and scattering. Both integrate +# the same normalized leaf-area measure, including for PDFs with endpoint peaks. +# Do not normalize the outgoing Z columns to force energy closure: refine the +# outgoing angular grid to check its remaining integration error. +G_leaf = CanopyOptics.G(μ, LD; quadrature) Z⁺⁺, Z⁻⁺ = CanopyOptics.compute_Z_matrices(leaf, μ, LD, 0:m_max; quadrature) size(Z⁺⁺) @@ -41,6 +46,10 @@ maximum(abs.(Z⁺⁺₂ .- Z⁺⁺[:, :, 3])) Z⁺⁺₀, Z⁻⁺₀ = CanopyOptics.compute_Z_matrices(leaf, μ, LD, 0; quadrature) maximum(abs.(Z⁻⁺₀ .- Z⁻⁺[:, :, 1])) +# G now defaults to `CanopyQuadrature().n_leaf`, matching scattering. The legacy +# `nLeg` keyword remains an explicit resolution override for standalone callers; +# when coupling transport, passing the same `quadrature` is preferred. + # ## Changing the leaf-angle distribution # Leaf-angle distributions can be supplied directly as distributions on diff --git a/src/canopy_scattering/bilambertian_fourier.jl b/src/canopy_scattering/bilambertian_fourier.jl index eda071a..738d29d 100644 --- a/src/canopy_scattering/bilambertian_fourier.jl +++ b/src/canopy_scattering/bilambertian_fourier.jl @@ -26,12 +26,12 @@ function compute_Γ(mod::BiLambertianCanopyScattering, n_leaf = quadrature.n_leaf n_azimuth = quadrature.n_azimuth #μ_l, w = gauleg(nQuad,0.0,1.0); - θₗ, w = gauleg(n_leaf,FT(0),FT(π/2)); + leaf_quad = _leaf_inclination_quadrature(LD, n_leaf, FT) + θₗ = leaf_quad.θ μ_l = cos.(θₗ) # Quadrature points in the azimuth (this has to go over 2π): ϕ, w_azi = gauleg(n_azimuth,FT(0),FT(2π)); - Fᵢ = pdf.(LD.LD,2θₗ/π) * LD.scaling # Create leaf angles in μ and ϕ Ω_l = [dirVector_μ(a,b) for a in μ_l, b in ϕ]; @@ -44,8 +44,8 @@ function compute_Γ(mod::BiLambertianCanopyScattering, iNeg = (integrand-abs.(integrand))./2 # Eq 39 in Shultis and Myneni, double integration here - Γ⁻ = -1/2π * (Fᵢ .* w)' * (iNeg * w_azi) - Γ⁺ = 1/2π * (Fᵢ .* w)' * (iPos * w_azi) + Γ⁻ = -1/2π * leaf_quad.w_measure' * (iNeg * w_azi) + Γ⁺ = 1/2π * leaf_quad.w_measure' * (iPos * w_azi) # Eq 38 in Shultis and Myneni return R * Γ⁻ + T * Γ⁺ end @@ -127,21 +127,6 @@ function _normalise_Z!(Z⁺⁺::AbstractArray{FT,3}, return Z⁺⁺, Z⁻⁺ end -""" - _leaf_inclination_quadrature(LD, nQuad, FT) - -Return leaf-inclination quadrature nodes and measure weights. - -The returned `w_measure` is already the Gauss weight multiplied by the leaf -angle distribution density and scaling factor. Do not multiply by `pdf(LD, ...)` -again at the call site. -""" -function _leaf_inclination_quadrature(LD::AbstractLeafDistribution, nQuad::Int, ::Type{FT}) where {FT} - θₗ, wθ = gauleg(nQuad, FT(0), FT(π / 2)) - Fₗ = FT.(pdf.(LD.LD, 2θₗ / FT(π))) .* FT(LD.scaling) - return (θ = θₗ, w_measure = wθ .* Fₗ) -end - """ _one_sided_projection_moments!(P, N, μ::FT, μ_L::FT, m_max::Int) -> (P, N) _one_sided_projection_moments(μ::FT, μ_L::FT, m_max::Int) -> (P, N) @@ -350,7 +335,7 @@ function compute_Z_matrices_aniso_analytic(mod::BiLambertianCanopyScattering, leaf_quad = _leaf_inclination_quadrature(LD, q.n_leaf, FT) θₗ = leaf_quad.θ w_measure = leaf_quad.w_measure - G = ZFT.(vec(CanopyOptics.G(μ_vec, LD))) + G = ZFT.(vec(CanopyOptics.G(μ_vec, LD; quadrature=q))) Pꜜ = zeros(ZFT, nμ, nm) Nꜜ = zeros(ZFT, nμ, nm) diff --git a/src/canopy_scattering/projection_geometry.jl b/src/canopy_scattering/projection_geometry.jl index 1dde4f7..08ff56d 100644 --- a/src/canopy_scattering/projection_geometry.jl +++ b/src/canopy_scattering/projection_geometry.jl @@ -54,41 +54,41 @@ end """ - $(FUNCTIONNAME)(μ::Array{FT}, LD::AbstractLeafDistribution; nLeg=20) + _leaf_inclination_quadrature(LD, n_leaf, FT) -Returns the integrated projection of leaf area in the direction of μ, assumes azimuthally uniform distribution and a LD distribution for leaf polar angle θ. -This function is often referred to as the function O(B) (Goudriaan 1977) or G(Ζ) (Ross 1975,1981), see Bonan modeling book, eqs. 14.21-14.26. +Leaf-inclination nodes and normalized leaf-area measure. G and the scattering +kernel must integrate the same discrete PDF, including its normalization. +""" +function _leaf_inclination_quadrature(LD::AbstractLeafDistribution, n_leaf::Int, ::Type{FT}) where {FT} + θ, w = gauleg(n_leaf, zero(FT), FT(π) / FT(2)) + density = FT.(pdf.(LD.LD, FT(2) .* θ ./ FT(π))) .* FT(LD.scaling) + w_measure = w .* density + mass = sum(w_measure) + isfinite(mass) && mass > zero(FT) || + throw(ArgumentError("leaf-inclination quadrature must have finite positive mass")) + w_measure ./= mass + return (; θ, w_measure) +end -# Arguments -- `μ` an array of cos(θ) (directions [0,1]) -- `LD` an `AbstractLeafDistribution` type struct, includes a leaf distribution function -- `nLeg` an optional parameter for the number of legendre polynomials to integrate over the leaf distribution (default=20) - -# Examples -```julia-repl -julia> μ,w = CanopyOptics.gauleg(10,0.0,1.0); # Create 10 quadrature points in μ -julia> LD = CanopyOptics.spherical_leaves() # Create a default spherical leaf distribution -julia> G = CanopyOptics.G(μ, LD) # Compute G(μ) -10-element Vector{Float64}: - 0.5002522783000879 - 0.5002715115149204 - 0.5003537989277846 - 0.5004432798701134 - 0.5005134448870893 - 0.5003026448466977 - 0.4999186257540982 - 0.4994511190721635 - 0.49907252201082375 - 0.49936166823681594 -``` """ -function G(μ::AbstractArray{FT}, LD::AbstractLeafDistribution; nLeg=40) where FT - θₗ, w = gauleg(nLeg, FT(0), FT(π/2)) - Fᵢ = FT.(pdf.(LD.LD, FT(2) .* θₗ ./ FT(π))) .* FT(LD.scaling) - Fᵢ = Fᵢ / (w' * Fᵢ) # normalize leaf angle distribution - θ = acos.(abs.(μ)) - G = (w .* Fᵢ)' * A.(θ', θₗ) - return FT.(G') + G(μ, LD; quadrature=CanopyQuadrature(), nLeg=nothing) + +Ross projected leaf area in directions `μ`, for an azimuthally uniform leaf +inclination distribution `LD`. Uses `quadrature.n_leaf` and the same normalized +leaf-area measure as the analytic scattering kernel. Pass the same `quadrature` +to G and `compute_Z_matrices` when assembling transport coefficients. + +`nLeg` remains a compatibility override for the number of leaf-inclination nodes. +With neither keyword, G now uses the common `CanopyQuadrature()` default instead +of an independent 40-node default. Only `n_leaf` affects this azimuth-averaged G. +""" +function G(μ::AbstractArray{FT}, LD::AbstractLeafDistribution; + quadrature::CanopyQuadrature=CanopyQuadrature(), nLeg=nothing) where FT + n_leaf = nLeg === nothing ? quadrature.n_leaf : nLeg + leaf_quad = _leaf_inclination_quadrature(LD, n_leaf, FT) + θ = acos.(abs.(μ)) + projected = leaf_quad.w_measure' * A.(θ', leaf_quad.θ) + return FT.(projected') end """ diff --git a/src/canopy_scattering/specular.jl b/src/canopy_scattering/specular.jl index 17b92c4..34e285e 100644 --- a/src/canopy_scattering/specular.jl +++ b/src/canopy_scattering/specular.jl @@ -121,7 +121,7 @@ function compute_Z_matrices(mod::SpecularCanopyScattering, # Quadrature points in the azimuth: ϕ, w_azi = gauleg(q.n_azimuth, FT(0), FT(2π)) - G_in = ZFT.(vec(G(μ, LD))) + G_in = ZFT.(vec(G(μ, LD; quadrature=q))) if n == 1 𝐙⁺⁺ = zeros(ZFT, length(μ), length(μ)) diff --git a/src/canopy_scattering/z_matrices.jl b/src/canopy_scattering/z_matrices.jl index d7090cc..017f725 100644 --- a/src/canopy_scattering/z_matrices.jl +++ b/src/canopy_scattering/z_matrices.jl @@ -257,10 +257,10 @@ function compute_Z_matrices(mod::CompositeCanopyScattering, quadrature = q, npol = npol) end -function _component_G_weights(components::Tuple, μ::AbstractVector) +function _component_G_weights(components::Tuple, μ::AbstractVector, quadrature::CanopyQuadrature) # Each G_c is already integrated over that component's inclination PDF; # only area-index weighting remains here. - Gs = map(component -> vec(G(μ, component.LAD)), components) + Gs = map(component -> vec(G(μ, component.LAD; quadrature)), components) FTG = eltype(Gs[1]) for i in eachindex(components) FTG = promote_type(FTG, eltype(Gs[i]), typeof(components[i].area_index)) @@ -326,7 +326,7 @@ function _compute_mixed_Z(canopy::MixedCanopy, μ::AbstractVector, m; n = _validate_npol(npol) q = _resolve_quadrature(quadrature, nQuad) components = canopy.components - Gs, G_total = _component_G_weights(components, μ) + Gs, G_total = _component_G_weights(components, μ, q) first_component = components[1] Zpp₁, Zmp₁ = compute_Z_matrices(first_component.scatterer, μ, diff --git a/src/canopy_structure/components.jl b/src/canopy_structure/components.jl index fb7363a..b364a8d 100644 --- a/src/canopy_structure/components.jl +++ b/src/canopy_structure/components.jl @@ -93,18 +93,19 @@ end MixedCanopy(components::Tuple) = MixedCanopy(components...) """ - component_G(component, μ; clumped = false) + component_G(component, μ; clumped = false, quadrature = CanopyQuadrature()) Projected area for one [`CanopyComponent`](@ref). Set `clumped = true` to apply the component's clumping model. """ -function component_G(component::CanopyComponent, μ::AbstractVector; clumped::Bool = false) - G_raw = vec(G(μ, component.LAD)) +function component_G(component::CanopyComponent, μ::AbstractVector; clumped::Bool = false, + quadrature::CanopyQuadrature = CanopyQuadrature()) + G_raw = vec(G(μ, component.LAD; quadrature)) return clumped ? effective_G(component.clumping, G_raw, μ) : G_raw end """ - bulk_G(canopy, μ; clumped = true) + bulk_G(canopy, μ; clumped = true, quadrature = CanopyQuadrature()) Area-index-weighted canopy projected area: @@ -114,17 +115,18 @@ G_{bulk}(μ) = \\sum_c AI_c G_c(μ), or `\\sum_c AI_c Ω_c(μ)G_c(μ)` when `clumped = true`. """ -function bulk_G(canopy::MixedCanopy, μ::AbstractVector; clumped::Bool = true) - first_G = component_G(canopy.components[1], μ; clumped = clumped) +function bulk_G(canopy::MixedCanopy, μ::AbstractVector; clumped::Bool = true, + quadrature::CanopyQuadrature = CanopyQuadrature()) + first_G = component_G(canopy.components[1], μ; clumped = clumped, quadrature) FT = promote_type(eltype(first_G), typeof(canopy.components[1].area_index)) for component in Iterators.drop(canopy.components, 1) - Gc = component_G(component, μ; clumped = clumped) + Gc = component_G(component, μ; clumped = clumped, quadrature) FT = promote_type(FT, eltype(Gc), typeof(component.area_index)) end total = zeros(FT, length(μ)) for component in canopy.components - Gc = component_G(component, μ; clumped = clumped) + Gc = component_G(component, μ; clumped = clumped, quadrature) @inbounds for i in eachindex(total) total[i] += FT(component.area_index) * FT(Gc[i]) end @@ -132,8 +134,9 @@ function bulk_G(canopy::MixedCanopy, μ::AbstractVector; clumped::Bool = true) return total end -G(μ::AbstractArray, canopy::MixedCanopy; clumped::Bool = false) = - bulk_G(canopy, μ; clumped = clumped) +G(μ::AbstractArray, canopy::MixedCanopy; clumped::Bool = false, + quadrature::CanopyQuadrature = CanopyQuadrature()) = + bulk_G(canopy, μ; clumped = clumped, quadrature) ##### ##### Convenience constructors for the canonical tree-canopy populations diff --git a/src/foursail/foursail.jl b/src/foursail/foursail.jl index 9d5a32a..3ff7bb8 100644 --- a/src/foursail/foursail.jl +++ b/src/foursail/foursail.jl @@ -116,12 +116,11 @@ function FourSAILGeometry(LAD::AbstractLeafDistribution; ks = zero(FT); ko = zero(FT); bf = zero(FT) sob = zero(FT); sof = zero(FT) - θl, wθ = gauleg(quadrature.n_leaf, zero(FT), FT(π / 2)) - lidf = FT.(pdf.(LAD.LD, FT(2) .* θl ./ FT(π))) .* FT(LAD.scaling) - lidf ./= sum(wθ .* lidf) + leaf_quad = _leaf_inclination_quadrature(LAD, quadrature.n_leaf, FT) + θl = leaf_quad.θ for i in eachindex(θl) - weight = wθ[i] * lidf[i] + weight = leaf_quad.w_measure[i] χs, χo, frho, ftau = _foursail_volscatt(μs, μo, dϕ, θl[i]) ks += weight * χs / μs ko += weight * χo / μo diff --git a/test/runtests.jl b/test/runtests.jl index 9f04fb4..c9d7e78 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,3 +3,5 @@ using ForwardDiff using Test include("test_CanopyOptics.jl") + +include("test_leaf_quadrature.jl") diff --git a/test/test_CanopyOptics.jl b/test/test_CanopyOptics.jl index 59616ef..9f1d177 100644 --- a/test/test_CanopyOptics.jl +++ b/test/test_CanopyOptics.jl @@ -19,7 +19,7 @@ _relerr(A, B) = norm(A .- B) / max(norm(B), eps(Float64)) G = CanopyOptics.G(μ, LD) @test eltype(G) == FT @test CanopyOptics.G(-μ, LD) ≈ G - # The default G quadrature (nLeg=40) is accurate to about + # The common default leaf quadrature is accurate to about # 1.3e-3 for this 10-point μ grid. tol = FT == Float32 ? FT(2e-3) : FT(1.5e-3) @test all(abs.(G .- FT(0.5)) .< tol) @@ -429,11 +429,11 @@ _relerr(A, B) = norm(A .- B) / max(norm(B), eps(Float64)) clumping = CanopyOptics.ConstantClumping(Ω = 0.5)) mixed = CanopyOptics.MixedCanopy(leaf_component, wood_component) - G_leaf = vec(CanopyOptics.G(μ, leaf_LD)) - G_wood = vec(CanopyOptics.G(μ, wood_LD)) - @test CanopyOptics.bulk_G(mixed, μ; clumped = false) ≈ + G_leaf = vec(CanopyOptics.G(μ, leaf_LD; quadrature)) + G_wood = vec(CanopyOptics.G(μ, wood_LD; quadrature)) + @test CanopyOptics.bulk_G(mixed, μ; clumped = false, quadrature) ≈ 2.0 .* G_leaf .+ G_wood - @test CanopyOptics.bulk_G(mixed, μ; clumped = true) ≈ + @test CanopyOptics.bulk_G(mixed, μ; clumped = true, quadrature) ≈ 2.0 .* G_leaf .+ 0.5 .* G_wood Zpp_mix, Zmp_mix = CanopyOptics.compute_Z_matrices( diff --git a/test/test_leaf_quadrature.jl b/test/test_leaf_quadrature.jl new file mode 100644 index 0000000..511ee0e --- /dev/null +++ b/test/test_leaf_quadrature.jl @@ -0,0 +1,34 @@ +using CanopyOptics, Test, LinearAlgebra + +@testset "Shared leaf-area quadrature" begin + for FT in (Float32, Float64) + μ, wμ = CanopyOptics.gauleg(128, zero(FT), one(FT)) + # The outgoing-stream integration is separate from the leaf quadrature. + tolerance = FT === Float32 ? FT(5e-5) : FT(5e-6) + for (α, β) in ((FT(2), FT(2)), (FT(0.4), FT(0.7))) + lad = beta_leaves(α, β) + for n_leaf in (8, 24, 128) + q = CanopyQuadrature(n_leaf=n_leaf) + leaf = CanopyOptics._leaf_inclination_quadrature(lad,n_leaf,FT) + @test eltype(leaf.w_measure) === FT + @test sum(leaf.w_measure) ≈ one(FT) + @test all(>=(zero(FT)),leaf.w_measure) + @test CanopyOptics.G(μ,lad;quadrature=q) ≈ CanopyOptics.G(μ,lad;nLeg=n_leaf) + # Conservatively scattering leaves must not create/absorb photons + # because the inclination quadrature misses some PDF mass. + scattering = BiLambertianCanopyScattering(R=FT(0.4),T=FT(0.6)) + Zpp,Zmp = CanopyOptics.compute_Z_matrices(scattering,μ,lad,0;quadrature=q) + columns = vec(transpose(wμ)*(Zpp+Zmp))/FT(2) + @test maximum(abs.(columns.-one(FT))) < tolerance + @test eltype(Zpp) === FT + end + end + # Default and explicit quadrature must describe the same leaf population. + lad = beta_leaves(FT(2),FT(2)) + @test CanopyOptics.G(μ,lad) == CanopyOptics.G(μ,lad;quadrature=CanopyQuadrature()) + # One orientation PDF normalizes both the 4SAIL projection and analytic Z. + q = CanopyQuadrature(n_leaf=24) + geom = FourSAILGeometry(lad;sza_deg=FT(40),vza_deg=zero(FT),raa_deg=zero(FT),quadrature=q) + @test geom.ks*geom.μs ≈ only(CanopyOptics.G(FT[geom.μs],lad;quadrature=q)) + end +end