./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
71 / 80
88.8%
Branches:
47 / 58
81.0%
Functions:
12 / 13
92.3%

src/distribution/size_frequency.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/distribution/size_frequency.h"
4
5 #include "util/checks.h"
6 #include "util/numerics.h"
7
8 #include <algorithm>
9 #include <cmath>
10 #include <optional>
11 #include <stdexcept>
12 #include <string>
13
14 namespace terraingen {
15
16 namespace {
17
18 98439 void CheckRange(const DiameterRange& range) {
19
5/6
✓ Branch 0 taken 98439 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98438 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 98436 times.
✓ Branch 5 taken 2 times.
98439 if (!(std::isfinite(range.min) && range.min >= 0.0 && range.max > range.min) ||
20
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98436 times.
98436 std::isnan(range.max)) {
21
1/1
✓ Branch 2 taken 3 times.
3 throw std::invalid_argument("Diameter range must satisfy 0 <= min < max");
22 }
23 98436 }
24
25 3663587 void CheckInterval(double minDiameter, double maxDiameter) {
26
3/4
✓ Branch 0 taken 3663587 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3663583 times.
✓ Branch 3 taken 3 times.
7327173 if (!(IsPositive(minDiameter) && maxDiameter > minDiameter) || std::isnan(maxDiameter)) {
27
1/1
✓ Branch 2 taken 4 times.
4 throw std::invalid_argument("Diameter interval must satisfy 0 < minDiameter < maxDiameter");
28 }
29 3663583 }
30
31 /// The diameter in the bracket [`low`, `high`] of log diameters where N = `target` > 0, by Illinois
32 /// steps on log N; unset when they do not converge, with the bracket narrowed.
33 template <typename Cumulative>
34 1448640 std::optional<double> InvertBySecant(const Cumulative& cumulative, double target, double& low,
35 double& high) {
36 1448640 const double logTarget = std::log(target);
37 18811772 const auto excess = [&](double u) { return std::log(cumulative(std::exp(u))) - logTarget; };
38 1448640 IllinoisBracket bracket{low, high, excess(low), excess(high)};
39
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1448396 times.
1448396 if (!(bracket.fa > 0.0)) {
40 ✗ return std::exp(low);
41 }
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1448396 times.
1448396 if (!(bracket.fb < 0.0)) {
43 ✗ return std::exp(high);
44 }
45 1448396 double u = low;
46
1/2
✓ Branch 0 taken 5788291 times.
✗ Branch 1 not taken.
5788291 for (int i = 0; i < 100; ++i) {
47 5788291 const double previous = u;
48 5788291 u = bracket.Next();
49 5788291 const double g = excess(u);
50
1/2
✓ Branch 0 taken 5788441 times.
✗ Branch 1 not taken.
5788441 if (std::isnan(g)) {
51 break;
52 }
53
4/4
✓ Branch 0 taken 4793024 times.
✓ Branch 1 taken 995417 times.
✓ Branch 2 taken 453096 times.
✓ Branch 3 taken 4339928 times.
5788441 if (g == 0.0 || std::fabs(u - previous) <= 1e-14 * std::fmax(1.0, std::fabs(u))) {
54 1448513 return std::exp(u);
55 }
56 4339928 bracket.Update(u, g);
57 }
58 ✗ low = bracket.a;
59 ✗ high = bracket.b;
60 ✗ return std::nullopt;
61 }
62
63 } // namespace
64
65 98439 SizeFrequencyDistribution::SizeFrequencyDistribution(SfdKind kind, DiameterRange range)
66 98439 : m_kind(kind), m_range(range) {
67 98439 CheckRange(m_range);
68 98436 }
69
70 23648438 void SizeFrequencyDistribution::CheckDiameter(double diameter) const {
71
2/2
✓ Branch 0 taken 23648436 times.
✓ Branch 1 taken 2 times.
23648438 if (!IsPositive(diameter)) {
72
1/1
✓ Branch 2 taken 4 times.
4 throw std::domain_error("Diameter must be positive and finite");
73 }
74
4/4
✓ Branch 0 taken 23530510 times.
✓ Branch 1 taken 117924 times.
✓ Branch 2 taken 23530492 times.
✓ Branch 3 taken 18 times.
23648434 if (!m_extrapolate && !m_range.Contains(diameter)) {
75
1/1
✓ Branch 1 taken 24 times.
48 throw std::domain_error("Diameter " + std::to_string(diameter) +
76
2/2
✓ Branch 2 taken 24 times.
✓ Branch 5 taken 24 times.
72 " m is outside the distribution's valid range");
77 }
78 23648410 }
79
80 11123926 double SizeFrequencyDistribution::Cumulative(double diameter) const {
81 11123926 CheckDiameter(diameter);
82 11123977 return CumulativeAt(diameter);
83 }
84
85 12529022 double SizeFrequencyDistribution::Incremental(double diameter) const {
86 12529022 CheckDiameter(diameter);
87 12537199 return IncrementalAt(diameter);
88 }
89
90 2 double SizeFrequencyDistribution::IncrementalAt(double diameter) const {
91 2 constexpr double kStep = 1e-5;
92 2 const double low = diameter * std::exp(-kStep);
93 2 const double high = diameter * std::exp(kStep);
94 2 return (CumulativeAt(low) - CumulativeAt(high)) / (high - low);
95 }
96
97 1448547 double SizeFrequencyDistribution::InvertCumulative(double target, double minDiameter,
98 double maxDiameter) const {
99 8681296 const auto cumulative = [this](double diameter) { return CumulativeAt(diameter); };
100 1448547 double low = std::log(minDiameter);
101
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1448527 times.
1448547 double high = std::isinf(maxDiameter) ? low + 1.0 : std::log(maxDiameter);
102
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1448612 times.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 20 times.
1448566 while (std::isinf(maxDiameter) && CumulativeAt(std::exp(high)) > target) {
103 19 high += 1.0;
104 }
105
1/2
✓ Branch 0 taken 1448632 times.
✗ Branch 1 not taken.
1448632 if (target > 0.0) {
106
1/2
✓ Branch 1 taken 1448429 times.
✗ Branch 2 not taken.
1448632 if (const std::optional<double> found = InvertBySecant(cumulative, target, low, high)) {
107 1448429 return *found;
108 }
109 }
110 ✗ const double tolerance = 1e-15 * std::fmax(1.0, std::fabs(high));
111 ✗ const BisectionInterval found = Bisect(
112 ✗ low, high, 200, tolerance, [&](double u) { return !(CumulativeAt(std::exp(u)) > target); });
113 ✗ return std::exp(0.5 * (found.low + found.high));
114 }
115
116 3663570 double SizeFrequencyDistribution::ExpectedCount(double area, double minDiameter,
117 double maxDiameter) const {
118 3663570 CheckInterval(minDiameter, maxDiameter);
119 3663736 RequireNonNegative(area, "Area");
120 3663698 double upper = 0.0;
121
2/2
✓ Branch 0 taken 40182 times.
✓ Branch 1 taken 3623516 times.
3663698 if (std::isinf(maxDiameter)) {
122
4/4
✓ Branch 0 taken 40180 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 40179 times.
40182 if (!m_extrapolate && !std::isinf(m_range.max)) {
123
1/1
✓ Branch 2 taken 1 time.
1 throw std::domain_error("An infinite maxDiameter needs an unbounded valid range");
124 }
125 } else {
126 3623516 upper = Cumulative(maxDiameter);
127 }
128 3663509 return area * (Cumulative(minDiameter) - upper);
129 }
130
131 3185132 double SizeFrequencyDistribution::SampleDiameter(RandomStream& random, double minDiameter,
132 double maxDiameter) const {
133 3185132 const double count = ExpectedCount(1.0, minDiameter, maxDiameter);
134
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3185213 times.
3185215 if (!(count > 0.0)) {
135
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("The distribution has no mass in the diameter interval");
136 }
137 3185213 const double top = Cumulative(minDiameter);
138 3185178 const double target = top - random.NextDouble() * count;
139
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3185160 times.
6370559 return std::clamp(InvertCumulative(target, minDiameter, maxDiameter), minDiameter, maxDiameter);
140 }
141
142 } // namespace terraingen
143