./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
19 / 19
100.0%
Branches:
18 / 24
75.0%
Functions:
3 / 3, 3 excl
100.0%

include/terraingen/layers/craters/degradation/diffusion.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Crater degradation by topographic diffusion: `DiffusedProfile`, `DiffusionDegradation`.
5 #pragma once
6
7 #include "terraingen/layers/craters/degradation/degradation_model.h"
8 #include "terraingen/layers/craters/profiles/profile.h"
9 #include "terraingen/util/lru_cache.h"
10
11 #include <cstddef>
12 #include <functional>
13 #include <memory>
14 #include <vector>
15
16 namespace terraingen {
17
18 /// A crater's diffusion age kappa t in m^2 from its diameter (m) and age (Gyr), such as a
19 /// `PowerLawDiffusivity`; must be thread-safe.
20 using DiffusionAgeModel = std::function<double(double diameter, double ageGyr)>;
21
22 /// A diffusivity kappa = `coefficient` (D / 1 km)^`exponent` m^2/Myr, held at its value at
23 /// `minDiameter` below it; usable as a `DiffusionAgeModel`.
24 2 struct PowerLawDiffusivity {
25 double coefficient = 1.0; ///< kappa of a 1 km crater, in m^2/Myr; positive.
26 double exponent = 0.0; ///< Power of the diameter; 0 for a constant diffusivity.
27 double minDiameter = 0.0; ///< Diameter below which kappa is held (m); non-negative.
28
29 /// kappa in m^2/Myr of craters of `diameter` meters.
30 double Diffusivity(double diameter) const;
31 /// The diffusion age kappa t in m^2 of a crater of `diameter` meters and `ageGyr` Gyr.
32 double operator()(double diameter, double ageGyr) const;
33
34 /// Memberwise equality.
35 bool operator==(const PowerLawDiffusivity& other) const {
36 return coefficient == other.coefficient && exponent == other.exponent &&
37 minDiameter == other.minDiameter;
38 }
39 bool operator!=(const PowerLawDiffusivity& other) const {
40 return !(*this == other);
41 }
42 };
43
44 /// Checks a power-law diffusivity.
45 /// @throws std::invalid_argument for a field outside its range (`CheckRanges`).
46 void ValidatePowerLawDiffusivity(const PowerLawDiffusivity& law);
47
48 /// A fresh crater profile worn by topographic diffusion, tabulated in the diffusion age
49 /// tau = kappa t / D^2 from 1e-5 to 0.5; older craters keep the last shape.
50 class DiffusedProfile final : public CraterProfile {
51 public:
52 /// @param reachRadii Radius of the solution domain, in crater radii; at least `fresh`'s reach.
53 /// @param cells Radial cells of the solver; at least 100.
54 /// @param cacheTables Most tables kept; positive.
55 /// @throws std::invalid_argument for a null `fresh`, `reachRadii` below the fresh profile's
56 /// reach, fewer than 100 cells, or no cache.
57 explicit DiffusedProfile(std::shared_ptr<const CraterProfile> fresh, double reachRadii = 3.0,
58 std::size_t cells = 600, std::size_t cacheTables = 64);
59
60 /// The fresh profile at diffusion age 0, the tabulated diffused shape after it; reads `fresh`
61 /// and `diffusion` of `state`.
62 double Height(double distance, double diameter, const CraterState& state) const override;
63
64 /// `reachRadii`.
65 237405 double ReachRadii() const override {
66
1/1
✓ Branch 1 taken 1 time.
237405 return m_reachRadii;
67 }
68
69 /// As the fresh profile says.
70 23208 bool IsComplex(double diameter) const override {
71
1/1
✓ Branch 1 taken 1 time.
23208 return m_fresh->IsComplex(diameter);
72 }
73
74 /// The morphometry of a crater of `diameter` meters diffused to age `tau` (kappa t / D^2): rim
75 /// crest height (highest point beyond half a radius) and depth below it (to the lowest point
76 /// within a radius), over the diameter.
77 CraterMorphometry Morphometry(const CraterMorphometry& fresh, double diameter,
78 double tau) const;
79
80 /// The diffusion age tau (kappa t / D^2) at which a crater of `fresh` morphometry and
81 /// `diameter` meters wears to `depthRatio` depth over diameter: 0 when it is that shallow
82 /// fresh, the last age tabulated when it is never worn that far.
83 double AgeAtDepthRatio(const CraterMorphometry& fresh, double diameter,
84 double depthRatio) const;
85
86 /// The diffusion ages tau tabulated, from 0, increasing.
87 const std::vector<double>& Ages() const;
88
89 /// The fresh profile.
90 36653 const CraterProfile& Fresh() const {
91 36653 return *m_fresh;
92 }
93
94 /// The profile's tables as the crater kernel reads them, with the ages first in its data.
95 const CraterProfileKernel& Kernel() const {
96 return m_kernel;
97 }
98
99 /// The heights over the diameter a crater of `fresh` morphometry and `diameter` meters is read
100 /// from: a row per age of `Ages`, `Cells` + 1 heights per row out to half the reach in
101 /// diameters.
102 std::shared_ptr<const std::vector<float>> HeightTable(const CraterMorphometry& fresh,
103 double diameter) const;
104
105 private:
106 struct Key {
107 double depthRatio;
108 double rimHeightRatio;
109 double diameter;
110 9771348 bool operator==(const Key& other) const {
111
6/8
✓ Branch 0 taken 26711 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 26712 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9718062 times.
✓ Branch 5 taken 26574 times.
✓ Branch 6 taken 9718064 times.
✗ Branch 7 not taken.
9771348 return depthRatio == other.depthRatio && rimHeightRatio == other.rimHeightRatio &&
112
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 26712 times.
✓ Branch 2 taken 9718031 times.
✓ Branch 3 taken 33 times.
9744776 diameter == other.diameter;
113 }
114 bool operator!=(const Key& other) const {
115 return !(*this == other);
116 }
117 };
118 struct KeyHash {
119 std::size_t operator()(const Key& key) const;
120 };
121
2/2
✓ Branch 5 taken 385 times.
✓ Branch 11 taken 385 times.
1133 struct Table {
122 std::vector<float> heights; ///< Row per age, `m_cells + 1` heights over D per row.
123 std::vector<CraterMorphometry> morphometry;
124 };
125
126 std::shared_ptr<const Table> CachedTable(const CraterMorphometry& fresh, double diameter) const;
127 Table Solve(const CraterMorphometry& fresh, double diameter) const;
128
129 std::shared_ptr<const CraterProfile> m_fresh;
130 double m_reachRadii;
131 std::size_t m_cells;
132 std::shared_ptr<LruCache<Key, Table, KeyHash>> m_tables;
133 std::uint64_t m_instance; ///< Unique to this profile, keying each thread's last table.
134 CraterProfileKernel m_kernel;
135 };
136
137 /// Craters worn by topographic diffusion, shaped with its `DiffusedProfile`.
138 class DiffusionDegradation final : public DegradationModel {
139 public:
140 /// @param ages Each crater's diffusion age kappa t.
141 /// @param profile Gives the current morphometry; the layer's profile.
142 /// @throws std::invalid_argument for an empty `fresh` or `ages`, or a null `profile`.
143 DiffusionDegradation(FreshMorphometry fresh, DiffusionAgeModel ages,
144 std::shared_ptr<const DiffusedProfile> profile);
145
146 /// The crater's fresh morphometry, current morphometry from the profile, and diffusion age.
147 /// @throws std::domain_error for a diameter that is not positive and finite, a negative or
148 /// non-finite age, or a diffusion age that is negative or not finite.
149 CraterState State(const Crater& crater) const override;
150
151 /// The state with the diffusion age kappa t `rate` times larger, as diffusion `rate` times as
152 /// fast would give.
153 CraterState WornState(const Crater& crater, double rate) const override;
154
155 /// The diffused profile the craters are shaped with.
156 177 std::shared_ptr<const CraterProfile> RequiredProfile() const override {
157
5/8
<unknown function 65>:
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 time.
terraingen::DiffusionDegradation::RequiredProfile() const:
✓ Branch 0 taken 175 times.
✗ Branch 1 not taken.
179 return m_profile;
158 }
159
160 private:
161 FreshMorphometry m_fresh;
162 DiffusionAgeModel m_ages;
163 std::shared_ptr<const DiffusedProfile> m_profile;
164 };
165
166 } // namespace terraingen
167