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 | 98439 | if (!(std::isfinite(range.min) && range.min >= 0.0 && range.max > range.min) || | |
| 20 | 98436 | std::isnan(range.max)) { | |
| 21 | 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 | 7327173 | if (!(IsPositive(minDiameter) && maxDiameter > minDiameter) || std::isnan(maxDiameter)) { | |
| 27 | 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 | 1448396 | if (!(bracket.fa > 0.0)) { | |
| 40 | ✗ | return std::exp(low); | |
| 41 | } | ||
| 42 | 1448396 | if (!(bracket.fb < 0.0)) { | |
| 43 | ✗ | return std::exp(high); | |
| 44 | } | ||
| 45 | 1448396 | double u = low; | |
| 46 | 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 | 5788441 | if (std::isnan(g)) { | |
| 51 | break; | ||
| 52 | } | ||
| 53 | 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 | 23648438 | if (!IsPositive(diameter)) { | |
| 72 | 4 | throw std::domain_error("Diameter must be positive and finite"); | |
| 73 | } | ||
| 74 | 23648434 | if (!m_extrapolate && !m_range.Contains(diameter)) { | |
| 75 | 48 | throw std::domain_error("Diameter " + std::to_string(diameter) + | |
| 76 | 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 | 1448547 | double high = std::isinf(maxDiameter) ? low + 1.0 : std::log(maxDiameter); | |
| 102 | 1448566 | while (std::isinf(maxDiameter) && CumulativeAt(std::exp(high)) > target) { | |
| 103 | 19 | high += 1.0; | |
| 104 | } | ||
| 105 | 1448632 | if (target > 0.0) { | |
| 106 | 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 | 3663698 | if (std::isinf(maxDiameter)) { | |
| 122 | 40182 | if (!m_extrapolate && !std::isinf(m_range.max)) { | |
| 123 | 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 | 3185215 | if (!(count > 0.0)) { | |
| 135 | 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 | 6370559 | return std::clamp(InvertCumulative(target, minDiameter, maxDiameter), minDiameter, maxDiameter); | |
| 140 | } | ||
| 141 | |||
| 142 | } // namespace terraingen | ||
| 143 |