|
1 |
| -# This script tests the output of the ClimaLand TwoStream implementation |
| 1 | +# This script tests the canopy absorbed output of the ClimaLand TwoStream implementation |
2 | 2 | # against the T. Quaife pySellersTwoStream implementation by providing
|
3 | 3 | # the same setups to each model and checking that the outputs are equal.
|
4 | 4 | # The output of the python module for a variety of inputs is given in the
|
5 | 5 | # linked file which then gets read and compared to the Clima version, ensuring
|
6 | 6 | # the FAPAR of each model is within 1/2 of a percentage point.
|
7 | 7 |
|
| 8 | +# It also tests that the sum of absorbed and reflected radiation is 1. |
| 9 | + |
8 | 10 | using Test
|
9 | 11 | using ClimaLand
|
10 | 12 | import ClimaComms
|
11 | 13 | ClimaComms.@import_required_backends
|
12 | 14 | using ClimaLand.Canopy
|
13 | 15 | using DelimitedFiles
|
14 | 16 | using ClimaLand.Domains: Point
|
| 17 | +import ClimaLand.Parameters as LP |
| 18 | +import ClimaParams |
15 | 19 |
|
16 |
| -include("../../Artifacts.jl") |
| 20 | +@testset "Comparison to pySellersTwoStream" begin |
| 21 | + include("../../Artifacts.jl") |
17 | 22 |
|
18 |
| -# Read the test data from the ClimaArtifact |
19 |
| -datapath = twostr_test_data_path() |
| 23 | + # Read the test data from the ClimaArtifact |
| 24 | + datapath = twostr_test_data_path() |
20 | 25 |
|
21 |
| -data = joinpath(datapath, "twostr_test.csv") |
22 |
| -test_set = readdlm(data, ',') |
| 26 | + data = joinpath(datapath, "twostr_test.csv") |
| 27 | + test_set = readdlm(data, ',') |
23 | 28 |
|
24 |
| -# Floating point precision to use |
25 |
| -for FT in (Float32, Float64) |
26 |
| - @testset "Two-Stream Model Correctness, FT = $FT" begin |
27 |
| - # Read the conditions for each setup parameter from the test file |
28 |
| - column_names = test_set[1, :] |
29 |
| - cosθs = FT.(test_set[2:end, column_names .== "mu"]) |
30 |
| - LAI = FT.(test_set[2:end, column_names .== "LAI"]) |
31 |
| - a_soil = FT.(test_set[2:end, column_names .== "a_soil"]) |
32 |
| - n_layers = UInt64.(test_set[2:end, column_names .== "n_layers"]) |
33 |
| - PropDif = FT.(test_set[2:end, column_names .== "prop_diffuse"]) |
| 29 | + # Floating point precision to use |
| 30 | + for FT in (Float32, Float64) |
| 31 | + @testset "Two-Stream Model Correctness, FT = $FT" begin |
| 32 | + # Read the conditions for each setup parameter from the test file |
| 33 | + column_names = test_set[1, :] |
| 34 | + cosθs = FT.(test_set[2:end, column_names .== "mu"]) |
| 35 | + LAI = FT.(test_set[2:end, column_names .== "LAI"]) |
| 36 | + a_soil = FT.(test_set[2:end, column_names .== "a_soil"]) |
| 37 | + n_layers = UInt64.(test_set[2:end, column_names .== "n_layers"]) |
| 38 | + PropDif = FT.(test_set[2:end, column_names .== "prop_diffuse"]) |
34 | 39 |
|
35 |
| - # setup spatially varying params as both float and spatially varying |
36 |
| - domain = Point(; z_sfc = FT(0.0)) |
37 |
| - lds = FT.(test_set[2:end, column_names .== "ld"]) |
38 |
| - lds_field = map(x -> fill(x, domain.space.surface), lds) |
39 |
| - α_PAR_leaf_scalars = FT.(test_set[2:end, column_names .== "rho"]) |
40 |
| - α_PAR_leaf_fields = |
41 |
| - map(x -> fill(x, domain.space.surface), α_PAR_leaf_scalars) |
42 |
| - τ_scalars = FT.(test_set[2:end, column_names .== "tau"]) |
43 |
| - τ_fields = map(x -> fill(x, domain.space.surface), τ_scalars) |
44 |
| - # loop through once with params as floats, then with params as fields |
45 |
| - Ω_cases = (FT(1), fill(FT(1), domain.space.surface)) |
46 |
| - α_PAR_leaf_cases = (α_PAR_leaf_scalars, α_PAR_leaf_fields) |
47 |
| - τ_PAR_leaf_cases = (τ_scalars, τ_fields) |
48 |
| - α_NIR_leaf_cases = (FT(0.4), fill(FT(0.4), domain.space.surface)) |
49 |
| - τ_NIR_leaf_cases = (FT(0.25), fill(FT(0.24), domain.space.surface)) |
50 |
| - lds_cases = (lds, lds_field) |
51 |
| - zipped_params = zip( |
52 |
| - Ω_cases, |
53 |
| - α_PAR_leaf_cases, |
54 |
| - τ_PAR_leaf_cases, |
55 |
| - α_NIR_leaf_cases, |
56 |
| - τ_NIR_leaf_cases, |
57 |
| - lds_cases, |
58 |
| - ) |
59 |
| - for (Ω, α_PAR_leaf, τ_PAR_leaf, α_NIR_leaf, τ_NIR_leaf, lds) in |
60 |
| - zipped_params |
61 |
| - # Read the result for each setup from the Python output |
62 |
| - py_FAPAR = FT.(test_set[2:end, column_names .== "FAPAR"]) |
| 40 | + # setup spatially varying params as both float and spatially varying |
| 41 | + domain = Point(; z_sfc = FT(0.0)) |
| 42 | + lds = FT.(test_set[2:end, column_names .== "ld"]) |
| 43 | + lds_field = map(x -> fill(x, domain.space.surface), lds) |
| 44 | + α_PAR_leaf_scalars = FT.(test_set[2:end, column_names .== "rho"]) |
| 45 | + α_PAR_leaf_fields = |
| 46 | + map(x -> fill(x, domain.space.surface), α_PAR_leaf_scalars) |
| 47 | + τ_scalars = FT.(test_set[2:end, column_names .== "tau"]) |
| 48 | + τ_fields = map(x -> fill(x, domain.space.surface), τ_scalars) |
| 49 | + # loop through once with params as floats, then with params as fields |
| 50 | + Ω_cases = (FT(1), fill(FT(1), domain.space.surface)) |
| 51 | + α_PAR_leaf_cases = (α_PAR_leaf_scalars, α_PAR_leaf_fields) |
| 52 | + τ_PAR_leaf_cases = (τ_scalars, τ_fields) |
| 53 | + α_NIR_leaf_cases = (FT(0.4), fill(FT(0.4), domain.space.surface)) |
| 54 | + τ_NIR_leaf_cases = (FT(0.25), fill(FT(0.24), domain.space.surface)) |
| 55 | + lds_cases = (lds, lds_field) |
| 56 | + zipped_params = zip( |
| 57 | + Ω_cases, |
| 58 | + α_PAR_leaf_cases, |
| 59 | + τ_PAR_leaf_cases, |
| 60 | + α_NIR_leaf_cases, |
| 61 | + τ_NIR_leaf_cases, |
| 62 | + lds_cases, |
| 63 | + ) |
| 64 | + for (Ω, α_PAR_leaf, τ_PAR_leaf, α_NIR_leaf, τ_NIR_leaf, lds) in |
| 65 | + zipped_params |
| 66 | + # Read the result for each setup from the Python output |
| 67 | + py_FAPAR = FT.(test_set[2:end, column_names .== "FAPAR"]) |
63 | 68 |
|
64 |
| - # Python code does not use clumping index, and λ_γ does not impact FAPAR |
65 |
| - # Test over all rows in the stored output from the Python module |
66 |
| - for i in 2:(size(test_set, 1) - 1) |
| 69 | + # Python code does not use clumping index, and λ_γ does not impact FAPAR |
| 70 | + # Test over all rows in the stored output from the Python module |
| 71 | + for i in 2:(size(test_set, 1) - 1) |
67 | 72 |
|
68 |
| - # Set the parameters based on the setup read from the file |
69 |
| - RT_params = TwoStreamParameters( |
70 |
| - FT; |
71 |
| - Ω = Ω, |
72 |
| - G_Function = ConstantGFunction(FT.(lds[i])), |
73 |
| - α_PAR_leaf = α_PAR_leaf[i], |
74 |
| - τ_PAR_leaf = τ_PAR_leaf[i], |
75 |
| - α_NIR_leaf = α_NIR_leaf, |
76 |
| - τ_NIR_leaf = τ_NIR_leaf, |
77 |
| - n_layers = n_layers[i], |
78 |
| - ) |
| 73 | + # Set the parameters based on the setup read from the file |
| 74 | + RT_params = TwoStreamParameters( |
| 75 | + FT; |
| 76 | + Ω = Ω, |
| 77 | + G_Function = ConstantGFunction(FT.(lds[i])), |
| 78 | + α_PAR_leaf = α_PAR_leaf[i], |
| 79 | + τ_PAR_leaf = τ_PAR_leaf[i], |
| 80 | + α_NIR_leaf = α_NIR_leaf, |
| 81 | + τ_NIR_leaf = τ_NIR_leaf, |
| 82 | + n_layers = n_layers[i], |
| 83 | + ) |
79 | 84 |
|
80 |
| - # Initialize the TwoStream model |
81 |
| - RT = TwoStreamModel(RT_params) |
| 85 | + # Initialize the TwoStream model |
| 86 | + RT = TwoStreamModel(RT_params) |
82 | 87 |
|
83 |
| - # Compute the predicted FAPAR using the ClimaLand TwoStream implementation |
84 |
| - G = compute_G(RT_params.G_Function, cosθs) |
85 |
| - K = extinction_coeff.(G, cosθs[i]) |
86 |
| - output = |
87 |
| - canopy_sw_rt_two_stream.( |
88 |
| - G, |
89 |
| - RT_params.Ω, |
90 |
| - RT_params.n_layers, |
91 |
| - RT_params.α_PAR_leaf, |
92 |
| - RT_params.τ_PAR_leaf, |
93 |
| - LAI[i], |
94 |
| - K, |
95 |
| - cosθs[i], |
96 |
| - a_soil[i], |
97 |
| - PropDif[i], |
| 88 | + # Compute the predicted FAPAR using the ClimaLand TwoStream implementation |
| 89 | + G = compute_G(RT_params.G_Function, cosθs) |
| 90 | + K = extinction_coeff.(G, cosθs[i]) |
| 91 | + output = |
| 92 | + canopy_sw_rt_two_stream.( |
| 93 | + G, |
| 94 | + RT_params.Ω, |
| 95 | + RT_params.n_layers, |
| 96 | + RT_params.α_PAR_leaf, |
| 97 | + RT_params.τ_PAR_leaf, |
| 98 | + LAI[i], |
| 99 | + K, |
| 100 | + cosθs[i], |
| 101 | + a_soil[i], |
| 102 | + PropDif[i], |
| 103 | + ) |
| 104 | + FAPAR = output.abs |
| 105 | + # Check that the predictions are app. equivalent to the Python model |
| 106 | + # Create a field of the expect value because isapprox cannot be broadcast |
| 107 | + # over a field of floats. The domain is a point, so it makes no difference |
| 108 | + # to the error when FAPAR is a float |
| 109 | + expected_output = fill(py_FAPAR[i], domain.space.surface) |
| 110 | + @test isapprox( |
| 111 | + 0, |
| 112 | + sum(FAPAR .- expected_output), |
| 113 | + atol = 0.005, |
98 | 114 | )
|
99 |
| - FAPAR = output.abs |
100 |
| - # Check that the predictions are app. equivalent to the Python model |
101 |
| - # Create a field of the expect value because isapprox cannot be broadcast |
102 |
| - # over a field of floats. The domain is a point, so it makes no difference |
103 |
| - # to the error when FAPAR is a float |
104 |
| - expected_output = fill(py_FAPAR[i], domain.space.surface) |
105 |
| - @test isapprox(0, sum(FAPAR .- expected_output), atol = 0.005) |
| 115 | + end |
106 | 116 | end
|
107 | 117 | end
|
108 | 118 | end
|
109 | 119 | end
|
| 120 | + |
| 121 | +@testset "Test physicality" begin |
| 122 | + N = 100000 |
| 123 | + θs = [rand(N - 1) * 2π..., π / 2] |
| 124 | + cosθs = cos.(θs) |
| 125 | + α_leaf = [rand(N - 4)..., 0.0, 0.0, 1.0, 1.0] |
| 126 | + τ_leaf = (1.0 .- α_leaf) .* rand(N) |
| 127 | + α_soil = [rand(N - 6)..., 0.0, 1.0, 0.2, 0.2, 0.2, 0.2] |
| 128 | + G = 0.5 |
| 129 | + K = ClimaLand.Canopy.extinction_coeff.(G, cosθs) |
| 130 | + frac_diff = rand(N) |
| 131 | + n_layers = UInt64(20) |
| 132 | + Ω = rand(N) |
| 133 | + LAI = round.(rand(N)) |
| 134 | + output = |
| 135 | + ClimaLand.Canopy.canopy_sw_rt_two_stream.( |
| 136 | + G, |
| 137 | + Ω, |
| 138 | + n_layers, |
| 139 | + α_leaf, |
| 140 | + τ_leaf, |
| 141 | + LAI, |
| 142 | + K, |
| 143 | + cosθs, |
| 144 | + α_soil, |
| 145 | + frac_diff, |
| 146 | + ) |
| 147 | + expected = zeros(N) |
| 148 | + for i in 1:N |
| 149 | + expected[i] = |
| 150 | + output[i].trans * (1 - α_soil[i]) + output[i].abs + output[i].refl |
| 151 | + end |
| 152 | + @assert all(expected .≈ 1) |
| 153 | +end |
0 commit comments