./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
18 / 18
100.0%
Branches:
23 / 46
50.0%
Functions:
3 / 3, 4 excl
100.0%

include/terraingen/distribution/power_law.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Power-law size-frequency distributions and their segments.
5 #pragma once
6
7 #include "terraingen/distribution/size_frequency.h"
8
9 #include <cmath>
10
11 namespace terraingen {
12
13 /// A power law N(>=D) = `coefficient` * D^(-`exponent`) from `start` meters, alone or as
14 /// one segment of a `PiecewisePowerLawSfd`.
15 18 struct PowerLawSegment {
16 double start = 0.0; ///< First diameter, meters.
17 double coefficient = 1.0; ///< N(>=1 m), per m^2 (or per m^2 per Gyr); positive.
18 double exponent = 2.0; ///< Cumulative slope; positive.
19
20 /// N(>=D) = coefficient * D^(-exponent), with D = `diameter` in meters.
21 524850 double Cumulative(double diameter) const {
22
4/4
✓ Branch 1 taken 79452 times.
✓ Branch 2 taken 898 times.
✓ Branch 3 taken 5475 times.
✓ Branch 4 taken 77459 times.
15148189 return coefficient * std::pow(diameter, -exponent);
23 }
24
25 /// -dN/dD = coefficient * exponent * D^(-exponent - 1), with D in meters.
26 1062038 double Incremental(double diameter) const {
27 1062038 return coefficient * exponent * std::pow(diameter, -exponent - 1.0);
28 }
29
30 /// The diameter in meters where N(>=D) is a positive `target`.
31 1654982 double Invert(double target) const {
32 1654982 return std::pow(target / coefficient, -1.0 / exponent);
33 }
34
35 /// Memberwise equality.
36 bool operator==(const PowerLawSegment& other) const {
37 return start == other.start && coefficient == other.coefficient &&
38 exponent == other.exponent;
39 }
40 bool operator!=(const PowerLawSegment& other) const {
41 return !(*this == other);
42 }
43 };
44
45 /// N(>=D) = `coefficient` * D^(-`exponent`), with D in meters and N per m^2 (or per m^2 per
46 /// Gyr).
47
19/42
✗ Branch 0 not taken.
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✓ Branch 1 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 2 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 3 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 3 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✗ Branch 4 not taken.
✗ Branch 4 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 1 time.
✗ Branch 8 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 3 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 time.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 19 taken 1 time.
✓ Branch 23 taken 1 time.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✓ Branch 30 taken 1 time.
✓ Branch 35 taken 1 time.
3273 class PowerLawSfd final : public SizeFrequencyDistribution {
48 public:
49 /// The power law with the given coefficient and exponent.
50 /// @param coefficient N(>=1 m), per m^2 (or per m^2 per Gyr).
51 /// @param range The valid range, in meters.
52 /// @throws std::invalid_argument unless `coefficient` and `exponent` are positive and finite
53 /// and the range is valid.
54 PowerLawSfd(double coefficient, double exponent, SfdKind kind = SfdKind::Density,
55 DiameterRange range = {});
56
57 /// N(>=1 m), per m^2 (or per m^2 per Gyr).
58 2 double Coefficient() const {
59 2 return m_law.coefficient;
60 }
61
62 /// The cumulative slope.
63 2 double Exponent() const {
64 2 return m_law.exponent;
65 }
66
67 protected:
68 double CumulativeAt(double diameter) const override;
69 double IncrementalAt(double diameter) const override;
70 double InvertCumulative(double target, double minDiameter, double maxDiameter) const override;
71
72 private:
73 PowerLawSegment m_law;
74 };
75
76 /// Converts a power-law coefficient published for D in km and N per km^2 to D in meters and
77 /// N per m^2.
78 double PowerLawCoefficientFromKilometers(double coefficient, double exponent);
79
80 } // namespace terraingen
81