include/terraingen/distribution/size_frequency.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief The size-frequency distribution base class, its kinds and diameter ranges. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/random/stream.h" | ||
| 8 | |||
| 9 | #include <limits> | ||
| 10 | #include <memory> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | namespace terraingen { | ||
| 14 | |||
| 15 | /// What a size-frequency distribution's values measure. | ||
| 16 | enum class SfdKind { | ||
| 17 | Density, ///< Cumulative count per m^2 (an equilibrium or present-day population). | ||
| 18 | ProductionRate, ///< Cumulative count per m^2 per Gyr (a production function). | ||
| 19 | }; | ||
| 20 | |||
| 21 | /// Diameters in meters where a distribution is defined: (`min`, `max`], or with `min` = 0 all | ||
| 22 | /// positive diameters up to `max`. | ||
| 23 | struct DiameterRange { | ||
| 24 | double min = 0.0; ///< Lower bound in meters, >= 0. | ||
| 25 | double max = std::numeric_limits<double>::infinity(); ///< Upper bound in meters, > `min`. | ||
| 26 | |||
| 27 | /// Whether `diameter` (meters) is positive and within [`min`, `max`]. | ||
| 28 | 23530510 | bool Contains(double diameter) const { | |
| 29 | 23530510 | return diameter > 0.0 && diameter >= min && diameter <= max; | |
| 30 | } | ||
| 31 | }; | ||
| 32 | |||
| 33 | /// A cumulative size-frequency distribution N(>=D) of craters or boulders. | ||
| 34 | /// | ||
| 35 | /// Diameters are in meters and counts per m^2 (or per m^2 per Gyr; see `Kind`). Distributions | ||
| 36 | /// decrease monotonically. Evaluating outside `ValidRange` throws unless extrapolation is allowed. | ||
| 37 | 829 | class SizeFrequencyDistribution { | |
| 38 | public: | ||
| 39 | 96477 | virtual ~SizeFrequencyDistribution() = default; | |
| 40 | |||
| 41 | /// What the values measure. | ||
| 42 | 273721 | SfdKind Kind() const { | |
| 43 | 270828 | return m_kind; | |
| 44 | } | ||
| 45 | |||
| 46 | /// The diameters in meters where the distribution may be evaluated without extrapolation. | ||
| 47 | 1248 | const DiameterRange& ValidRange() const { | |
| 48 | 1251 | return m_range; | |
| 49 | } | ||
| 50 | |||
| 51 | /// Whether evaluation outside `ValidRange` is allowed. | ||
| 52 | 2643 | bool AllowsExtrapolation() const { | |
| 53 | 2643 | return m_extrapolate; | |
| 54 | } | ||
| 55 | |||
| 56 | /// Permits or forbids evaluation at positive diameters outside `ValidRange`. | ||
| 57 | 502 | void SetAllowExtrapolation(bool allow) { | |
| 58 | 502 | m_extrapolate = allow; | |
| 59 | 13 | } | |
| 60 | |||
| 61 | /// N(>=D), the count per m^2 (or per m^2 per Gyr) of features at least `diameter` meters | ||
| 62 | /// across. | ||
| 63 | /// @throws std::domain_error if `diameter` is not positive and finite, or is outside the valid | ||
| 64 | /// range without extrapolation. | ||
| 65 | double Cumulative(double diameter) const; | ||
| 66 | |||
| 67 | /// -dN/dD at `diameter` meters, per m^2 per meter (or per m^2 per Gyr per meter). | ||
| 68 | /// @throws std::domain_error under the same conditions as `Cumulative`. | ||
| 69 | double Incremental(double diameter) const; | ||
| 70 | |||
| 71 | /// Expected number of features with diameter in [`minDiameter`, `maxDiameter`) meters over | ||
| 72 | /// `area`. | ||
| 73 | /// @param area Area in m^2 (or m^2 Gyr for a production rate), finite and >= 0. | ||
| 74 | /// @param maxDiameter Largest diameter in meters; may be infinite. | ||
| 75 | /// @throws std::invalid_argument unless 0 < `minDiameter` < `maxDiameter` and `area` >= 0 is | ||
| 76 | /// finite. | ||
| 77 | /// @throws std::domain_error for a diameter outside the valid range without extrapolation. | ||
| 78 | double ExpectedCount(double area, double minDiameter, double maxDiameter) const; | ||
| 79 | |||
| 80 | /// Draws a diameter in meters in [`minDiameter`, `maxDiameter`) by inverting N(>=D); | ||
| 81 | /// `maxDiameter` may be infinite. | ||
| 82 | /// @throws std::invalid_argument unless 0 < `minDiameter` < `maxDiameter`, or if N(minDiameter) | ||
| 83 | /// = N(maxDiameter). | ||
| 84 | /// @throws std::domain_error under the same conditions as `ExpectedCount`. | ||
| 85 | double SampleDiameter(RandomStream& random, double minDiameter, double maxDiameter) const; | ||
| 86 | |||
| 87 | protected: | ||
| 88 | /// Sets the kind and valid range. | ||
| 89 | /// @throws std::invalid_argument unless 0 <= `range.min` < `range.max`. | ||
| 90 | SizeFrequencyDistribution(SfdKind kind, DiameterRange range); | ||
| 91 | |||
| 92 | /// N(>=D) at a positive `diameter` in meters, without range checks. | ||
| 93 | virtual double CumulativeAt(double diameter) const = 0; | ||
| 94 | |||
| 95 | /// -dN/dD without range checks; defaults to a central difference in log diameter. | ||
| 96 | virtual double IncrementalAt(double diameter) const; | ||
| 97 | |||
| 98 | /// Diameter D in meters in [`minDiameter`, `maxDiameter`] with N(>=D) = `target`; defaults to | ||
| 99 | /// bisection in log diameter. | ||
| 100 | /// @param target Cumulative count with N(maxDiameter) <= `target` <= N(minDiameter). | ||
| 101 | /// @param maxDiameter Largest diameter in meters; may be infinite. | ||
| 102 | virtual double InvertCumulative(double target, double minDiameter, double maxDiameter) const; | ||
| 103 | |||
| 104 | /// Unchecked `CumulativeAt` of another distribution, for distributions built from others. | ||
| 105 | 23207691 | static double UncheckedCumulative(const SizeFrequencyDistribution& distribution, | |
| 106 | double diameter) { | ||
| 107 | 23206954 | return distribution.CumulativeAt(diameter); | |
| 108 | } | ||
| 109 | |||
| 110 | /// Unchecked `IncrementalAt` of another distribution. | ||
| 111 | 1336745 | static double UncheckedIncremental(const SizeFrequencyDistribution& distribution, | |
| 112 | double diameter) { | ||
| 113 | 1336745 | return distribution.IncrementalAt(diameter); | |
| 114 | } | ||
| 115 | |||
| 116 | /// Unchecked `InvertCumulative` of another distribution. | ||
| 117 | 140985 | static double UncheckedInverse(const SizeFrequencyDistribution& distribution, double target, | |
| 118 | double minDiameter, double maxDiameter) { | ||
| 119 | 140985 | return distribution.InvertCumulative(target, minDiameter, maxDiameter); | |
| 120 | } | ||
| 121 | |||
| 122 | private: | ||
| 123 | void CheckDiameter(double diameter) const; | ||
| 124 | |||
| 125 | SfdKind m_kind; | ||
| 126 | DiameterRange m_range; | ||
| 127 | bool m_extrapolate = false; | ||
| 128 | }; | ||
| 129 | |||
| 130 | } // namespace terraingen | ||
| 131 |