File size: 6,602 Bytes
2983a9c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #!/usr/bin/env python3
"""Hand-worked cases for Stand Inventory. Run: python3 tests/test_stand_inventory.py"""
import math, os, sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from envs.stand_inventory import gen as G
def close(a, b, tol=1e-3): assert abs(a - b) <= tol, (a, b)
# ---------------------------------------------------------------- plots
# Basal area of a 30 cm tree: pi * 0.15^2 = pi * 0.0225 = 0.070686 m2.
close(G.basal_area_m2(30), 0.070686, 1e-6)
# Radius 10 m: area = 314.159 m2, expansion factor = 10000 / 314.159 = 31.831.
close(G.expansion_factor(10.0), 31.831)
# Case 1: two 20 cm trees on a 10 m plot. g = pi * 0.1^2 = 0.0314159 each, sum 0.0628319.
# stems/ha = 2 * 31.831 = 63.662; BA/ha = 0.0628319 * 31.831 = 2.000; QMD = 20 (all trees equal).
n, stems, ba, q = G.plot_metrics([20, 20], 10.0)
assert n == 2; close(stems, 63.662); close(ba, 2.0); close(q, 20.0)
# Case 2: a 100 m2 plot (radius sqrt(100 / pi) = 5.641896 m), expansion factor 100. Trees 10, 20, 30 cm:
# g = 0.007854 + 0.031416 + 0.070686 = 0.109956 m2 -> 10.9956 m2/ha; 300 stems/ha;
# QMD = sqrt((100 + 400 + 900) / 3) = sqrt(466.667) = 21.602 cm.
n, stems, ba, q = G.plot_metrics([10, 20, 30], 5.641896)
assert n == 3; close(stems, 300.0, 1e-2); close(ba, 10.9956); close(q, 21.602)
# Case 3: radius 7.98 m: area = pi * 63.6804 = 200.058 m2, factor 49.9855. Four 25 cm trees:
# g = pi * 0.125^2 = 0.0490874 each, sum 0.19635 -> 9.8146 m2/ha; 4 * 49.9855 = 199.94 stems/ha; QMD 25.
n, stems, ba, q = G.plot_metrics([25, 25, 25, 25], 7.98)
close(stems, 199.94, 1e-2); close(ba, 9.8146); close(q, 25.0)
# QMD from stand values: 800 stems/ha and 25.1327 m2/ha -> mean tree g = 0.0314159 m2 -> 20 cm.
close(G.qmd_cm(25.1327, 800), 20.0)
# ---------------------------------------------------------------- volume
# Case 1: v = 0.00005 * 20^2 * 16^1 = 0.00005 * 400 * 16 = 0.32 m3.
close(G.tree_volume(20, 16, 0.00005, 2, 1), 0.32, 1e-9)
# Case 2: v = 0.0001 * 30^2 * 25^0.5 = 0.0001 * 900 * 5 = 0.45 m3.
close(G.tree_volume(30, 25, 0.0001, 2, 0.5), 0.45, 1e-9)
# Case 3 (fractional exponents): 0.0001 * 10^1.9 * 10^1.1 = 0.0001 * 10^3 = 0.1 m3.
close(G.tree_volume(10, 10, 0.0001, 1.9, 1.1), 0.1, 1e-9)
# Height curve: h = 1.3 + 20^2 / (2 + 0.15 * 20)^2 = 1.3 + 400 / 25 = 17.3 m.
close(G.curve_height(20, 2, 0.15), 17.3, 1e-9)
# h = 1.3 + 30^2 / (1.5 + 0.2 * 30)^2 = 1.3 + 900 / 56.25 = 17.3 m.
close(G.curve_height(30, 1.5, 0.2), 17.3, 1e-9)
# h = 1.3 + 10^2 / (3 + 0.2 * 10)^2 = 1.3 + 100 / 25 = 5.3 m.
close(G.curve_height(10, 3, 0.2), 5.3, 1e-9)
# Form-factor rule: g(40 cm) = pi * 0.2^2 = 0.125664; v = 0.125664 * 20 * 0.5 = 1.256637 m3.
close(G.form_factor_volume(40, 20, 0.5), 1.256637, 1e-6)
# Lorey's height: trees (10 cm, 10 m) and (20 cm, 20 m) have basal areas in ratio 1 : 4 -> (10 + 80) / 5 = 18 m.
close(G.lorey_height([(10, 10), (20, 20)]), 18.0, 1e-9)
# Equal diameters give the plain mean: (12 + 18) / 2 = 15 m.
close(G.lorey_height([(25, 12), (25, 18)]), 15.0, 1e-9)
# ---------------------------------------------------------------- thinning
# Case 1: trees 10, 20, 30, 40 cm on 1000 m2 (factor 10).
# g = 0.007854, 0.031416, 0.070686, 0.125664; total 0.235619 -> 2.35619 m2/ha.
# Target 2.0: remove the 10 cm tree -> 2.27765 (> 2.0), remove the 20 cm tree -> 1.96350 (<= 2.0): stop.
assert G.thin_from_below([(1, 10, True, False), (2, 20, True, False), (3, 30, True, False), (4, 40, True, False)], 1000, 2.0) == [1, 2]
# Case 2 (tie-break): three 20 cm trees numbered 7, 3, 5 and a 30 cm tree on 1 ha (factor 1).
# total = 3 * 0.031416 + 0.070686 = 0.164934. Target 0.11: remove tree 3 -> 0.133518, tree 5 -> 0.102102: stop. Tree 7 stays.
assert G.thin_from_below([(7, 20, True, False), (3, 20, True, False), (5, 20, True, False), (9, 30, True, False)], 10000, 0.11) == [3, 5]
# Case 3 (retention and damage) on 1 ha: tree 1 (10 cm) retained, tree 3 (40 cm) damaged.
# g: 10 -> 0.007854, 12 -> 0.011310, 40 -> 0.125664, 30 -> 0.070686, 35 -> 0.096211; total 0.311725.
# Damaged tree 3 goes first -> 0.186061 (> 0.17). Tree 1 is retained. Tree 2 -> 0.174751 (> 0.17). Tree 4 -> 0.104065: stop.
assert G.thin_from_below([(1, 10, False, False), (2, 12, True, False), (3, 40, True, True), (4, 30, True, False), (5, 35, True, False)], 10000, 0.17) == [3, 2, 4]
# Case 4: already at the target (0.311725 <= 1.0), nothing is removed.
assert G.thin_from_below([(1, 10, True, False), (2, 12, True, False), (3, 40, True, False), (4, 30, True, False), (5, 35, True, False)], 10000, 1.0) == []
# Case 5 (tie-break again): two 20 cm trees on 1 ha, total 0.062832; target 0.04 -> one removal (0.031416).
assert G.thin_from_below([(2, 20, True, False), (1, 20, True, False)], 10000, 0.04) == [1]
# Quadratic mean of 10, 20, 30 = sqrt(1400 / 3) = 21.602; of 3, 4 = sqrt(12.5) = 3.5355.
close(G.quadratic_mean([10, 20, 30]), 21.602); close(G.quadratic_mean([3, 4]), 3.5355)
# ---------------------------------------------------------------- growth
# Case 1: 200 -> 260 m3/ha in 10 years, nothing cut: PAI = 60 / 10 = 6.0; age 65: MAI = 260 / 65 = 4.0.
close(G.periodic_annual_increment(200, 260, 0, 10), 6.0, 1e-9); close(G.mean_annual_increment(260, 0, 65), 4.0, 1e-9)
# Case 2: same volumes with 30 m3/ha thinned in the period: PAI = (60 + 30) / 10 = 9.0.
# 40 m3/ha removed in the stand's life, age 60: MAI = (260 + 40) / 60 = 5.0.
close(G.periodic_annual_increment(200, 260, 30, 10), 9.0, 1e-9); close(G.mean_annual_increment(260, 40, 60), 5.0, 1e-9)
# Case 3: standing volume fell from 300 to 280 over 5 years but 45 was cut: PAI = (-20 + 45) / 5 = 5.0.
close(G.periodic_annual_increment(300, 280, 45, 5), 5.0, 1e-9)
# Pressler: 200 -> 300 in 10 years: p = 200 / 10 * 100 / 500 = 4.0 % per year.
close(G.pressler_percent(200, 300, 10), 4.0, 1e-9)
# Pressler: 150 -> 170 in 5 years: p = 200 / 5 * 20 / 320 = 2.5 %.
close(G.pressler_percent(150, 170, 5), 2.5, 1e-9)
# Years to target: 100 at 10 % to 121 = 100 * 1.1^2 -> exactly 2 years.
close(G.years_to_target(100, 10.0, 121), 2.0, 1e-9)
# 300 at 4 % to 400: ln(1.33333) / ln(1.04) = 0.287682 / 0.0392207 = 7.335 -> 8 whole years (1.04^7 = 1.3159 < 1.3333 < 1.04^8 = 1.3686).
t = G.years_to_target(300, 4.0, 400)
close(t, 7.335); assert math.ceil(t) == 8 and 300 * 1.04 ** 7 < 400 <= 300 * 1.04 ** 8
# ---------------------------------------------------------------- generator sanity
for d in (1, 2, 3):
for fam in G.FAMILIES:
t = G.generate(3, d, fam)
assert t.family == fam and 6 <= len(t.checks) <= 45, (fam, d, len(t.checks))
print("test_stand_inventory: all passed")
|