./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
189 / 189
100.0%
Branches:
123 / 142
86.6%
Functions:
25 / 25
100.0%

src/layers/craters/degradation/diffusion.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/craters/degradation/diffusion.h"
4
5 #include "terraingen/layers/craters/crater_fields.h"
6 #include "terraingen/noise/hash.h"
7 #include "terraingen/util/bits.h"
8 #include "util/checks.h"
9
10 #include <algorithm>
11 #include <atomic>
12 #include <cmath>
13 #include <cstdint>
14 #include <limits>
15 #include <stdexcept>
16 #include <utility>
17
18 namespace terraingen {
19
20 namespace {
21
22 /// A number no other diffused profile has, so a thread's last table is never another's.
23 232 std::uint64_t NextInstance() {
24 232 static std::atomic<std::uint64_t> next{1};
25 232 return next.fetch_add(1);
26 }
27
28 constexpr double kFirstAge = 1e-5;
29 constexpr double kLastAge = 0.5;
30 constexpr std::size_t kAgeSteps = 160;
31 constexpr double kReferenceDiameter = 1000.0;
32
33 /// The tabulated diffusion ages: 0, then geometric from `kFirstAge` to `kLastAge`.
34 110768 const std::vector<double>& TableAges() {
35 92 static const std::vector<double> ages = [] {
36 92 std::vector<double> values{0.0};
37 92 const double ratio =
38 std::pow(kLastAge / kFirstAge, 1.0 / static_cast<double>(kAgeSteps - 1));
39
2/2
✓ Branch 0 taken 14720 times.
✓ Branch 1 taken 92 times.
14812 for (std::size_t k = 0; k < kAgeSteps; ++k) {
40 29440 values.push_back(k + 1 == kAgeSteps
41
3/3
✓ Branch 0 taken 14628 times.
✓ Branch 1 taken 92 times.
✓ Branch 3 taken 14720 times.
29348 ? kLastAge
42 14628 : kFirstAge * std::pow(ratio, static_cast<double>(k)));
43 }
44 92 return values;
45
4/5
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 110676 times.
✓ Branch 3 taken 92 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 92 times.
110768 }();
46 110768 return ages;
47 }
48
49 /// `TableAges` in single precision, where the crater kernel reads them.
50 9600326 CraterData KernelAges() {
51 56 static const std::vector<float> ages = [] {
52
1/1
✓ Branch 1 taken 56 times.
56 std::vector<float> values;
53
3/3
✓ Branch 1 taken 56 times.
✓ Branch 3 taken 9016 times.
✓ Branch 4 taken 56 times.
9072 for (const double age : TableAges()) {
54
1/1
✓ Branch 1 taken 9016 times.
9016 values.push_back(static_cast<float>(age));
55 }
56 56 return values;
57
4/5
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 9600270 times.
✓ Branch 3 taken 56 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 56 times.
9600326 }();
58 9600326 CraterData data;
59 9600326 data.values = ages.data();
60 9600326 return data;
61 }
62
63 /// A tridiagonal system lower[i] x[i-1] + diag[i] x[i] + upper[i] x[i+1] = rhs[i] for rows i < n,
64 /// with x[n] given.
65 struct Tridiagonal {
66 std::vector<double> lower;
67 std::vector<double> diag;
68 std::vector<double> upper;
69 };
70
71 /// Solves `system` by the Thomas algorithm: `x` holds the right-hand side of rows 0 to n - 1 and
72 /// the fixed x[n], and is replaced by the solution. `c` and `d` are scratch of n values.
73 903204 void SolveTridiagonal(const Tridiagonal& system, std::vector<double>& x, std::vector<double>& c,
74 std::vector<double>& d) {
75 903204 const std::size_t n = system.diag.size();
76 903204 c[0] = system.upper[0] / system.diag[0];
77 903204 d[0] = x[0] / system.diag[0];
78
2/2
✓ Branch 0 taken 541019196 times.
✓ Branch 1 taken 903204 times.
541922400 for (std::size_t i = 1; i < n; ++i) {
79 541019196 const double m = system.diag[i] - system.lower[i] * c[i - 1];
80 541019196 c[i] = system.upper[i] / m;
81 541019196 d[i] = (x[i] - system.lower[i] * d[i - 1]) / m;
82 }
83
2/2
✓ Branch 0 taken 541922400 times.
✓ Branch 1 taken 903204 times.
542825604 for (std::size_t i = n; i-- > 0;) {
84 541922400 x[i] = d[i] - c[i] * x[i + 1];
85 }
86 903204 }
87
88 /// Implicit steps of radial diffusion on `n` cells `dx` apart with the outer edge held at 0.
89 class RadialDiffusion {
90 public:
91 384 RadialDiffusion(std::size_t n, double dx)
92
2/2
✓ Branch 2 taken 384 times.
✓ Branch 5 taken 385 times.
384 : m_n(n), m_below(n + 1, 0.0), m_above(n + 1, 0.0),
93
4/4
✓ Branch 1 taken 385 times.
✓ Branch 4 taken 384 times.
✓ Branch 7 taken 385 times.
✓ Branch 10 taken 385 times.
384 m_system{std::vector<double>(n), std::vector<double>(n), std::vector<double>(n)}, m_c(n),
94
1/1
✓ Branch 1 taken 385 times.
385 m_d(n) {
95
2/2
✓ Branch 0 taken 227021 times.
✓ Branch 1 taken 385 times.
227406 for (std::size_t i = 1; i < n; ++i) {
96 227021 const double x = static_cast<double>(i) * dx;
97 227021 m_below[i] = (x - 0.5 * dx) / (x * dx * dx);
98 227021 m_above[i] = (x + 0.5 * dx) / (x * dx * dx);
99 }
100 385 m_above[0] = 4.0 / (dx * dx);
101 385 }
102
103 /// Diffuses heights `h` (n + 1 values) over `dt`.
104 903204 void Step(std::vector<double>& h, double dt) {
105
2/2
✓ Branch 0 taken 541922400 times.
✓ Branch 1 taken 903204 times.
542825604 for (std::size_t i = 0; i < m_n; ++i) {
106 541922400 m_system.lower[i] = -dt * m_below[i];
107 541922400 m_system.diag[i] = 1.0 + dt * (m_below[i] + m_above[i]);
108 541922400 m_system.upper[i] = -dt * m_above[i];
109 }
110 903204 h[m_n] = 0.0;
111 903204 SolveTridiagonal(m_system, h, m_c, m_d);
112 903217 }
113
114 private:
115 std::size_t m_n;
116 std::vector<double> m_below;
117 std::vector<double> m_above;
118 Tridiagonal m_system;
119 std::vector<double> m_c;
120 std::vector<double> m_d;
121 };
122
123 /// Appends heights `h` (cells `dx` diameters apart) to `heights` and returns their morphometry:
124 /// the rim the highest from 0.25 diameters out, the floor the lowest within 0.5.
125 61966 CraterMorphometry AppendHeights(const std::vector<double>& h, double dx,
126 std::vector<float>& heights) {
127 61966 double rim = -std::numeric_limits<double>::infinity();
128 61966 double floor = std::numeric_limits<double>::infinity();
129
2/2
✓ Branch 0 taken 36425986 times.
✓ Branch 1 taken 61922 times.
36487908 for (std::size_t i = 0; i < h.size(); ++i) {
130 36425986 const double x = static_cast<double>(i) * dx;
131
2/2
✓ Branch 0 taken 30432944 times.
✓ Branch 1 taken 5993042 times.
36425986 if (x >= 0.25) {
132 30432944 rim = std::fmax(rim, h[i]);
133 }
134
2/2
✓ Branch 0 taken 12296314 times.
✓ Branch 1 taken 24129672 times.
36425986 if (x <= 0.5) {
135 12296314 floor = std::fmin(floor, h[i]);
136 }
137 36425986 heights.push_back(static_cast<float>(h[i]));
138 }
139 61922 return {rim - floor, rim};
140 }
141
142 } // namespace
143
144 247448 double PowerLawDiffusivity::Diffusivity(double diameter) const {
145 247448 return coefficient * std::pow(std::fmax(diameter, minDiameter) / 1000.0, exponent);
146 }
147
148 247442 double PowerLawDiffusivity::operator()(double diameter, double ageGyr) const {
149 247442 return Diffusivity(diameter) * ageGyr * 1000.0;
150 }
151
152 5 void ValidatePowerLawDiffusivity(const PowerLawDiffusivity& law) {
153 5 CheckRanges(law);
154 2 }
155
156 54084 std::size_t DiffusedProfile::KeyHash::operator()(const Key& key) const {
157 54084 return HashWords({BitCast<std::uint64_t>(key.depthRatio),
158 54084 BitCast<std::uint64_t>(key.rimHeightRatio),
159 54084 BitCast<std::uint64_t>(key.diameter)});
160 }
161
162 232 DiffusedProfile::DiffusedProfile(std::shared_ptr<const CraterProfile> fresh, double reachRadii,
163 232 std::size_t cells, std::size_t cacheTables)
164 232 : m_fresh(std::move(fresh)), m_reachRadii(reachRadii), m_cells(cells),
165 232 m_instance(NextInstance()) {
166
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 231 times.
232 if (!m_fresh) {
167
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A diffused profile needs a fresh profile");
168 }
169
4/5
✓ Branch 0 taken 231 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 231 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 229 times.
231 if (!(std::isfinite(m_reachRadii) && m_reachRadii >= m_fresh->ReachRadii())) {
170 2 throw std::invalid_argument(
171
1/1
✓ Branch 2 taken 2 times.
2 "A diffused profile must reach at least as far as the fresh one");
172 }
173
3/4
✓ Branch 0 taken 228 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 228 times.
229 if (m_cells < 100 || cacheTables == 0) {
174
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("A diffused profile needs at least 100 cells and a cache");
175 }
176
1/1
✓ Branch 1 taken 228 times.
228 m_tables = std::make_shared<LruCache<Key, Table, KeyHash>>(
177
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 228 times.
228 cacheTables, [](const Table&) { return std::size_t{1}; });
178 228 m_kernel.diffused = 1u;
179 228 m_kernel.tableExtent = static_cast<float>(0.5 * m_reachRadii);
180 228 m_kernel.tableCells = static_cast<std::uint32_t>(m_cells);
181 228 m_kernel.agesOffset = 0u;
182
1/1
✓ Branch 1 taken 228 times.
228 m_kernel.ageCount = static_cast<std::uint32_t>(TableAges().size());
183
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 time.
232 }
184
185 36340 const std::vector<double>& DiffusedProfile::Ages() const {
186 36340 return TableAges();
187 }
188
189 384 DiffusedProfile::Table DiffusedProfile::Solve(const CraterMorphometry& fresh,
190 double diameter) const {
191 384 const std::size_t n = m_cells;
192 384 const double extent = 0.5 * m_reachRadii;
193 384 const double dx = extent / static_cast<double>(n);
194 384 const CraterState initial{fresh, fresh, 0.0};
195 384 std::vector<double> h(n + 1, 0.0);
196
2/2
✓ Branch 0 taken 229316 times.
✓ Branch 1 taken 385 times.
229701 for (std::size_t i = 0; i < n; ++i) {
197 229316 h[i] =
198
1/1
✓ Branch 1 taken 229316 times.
229316 m_fresh->Height(static_cast<double>(i) * dx * diameter, diameter, initial) / diameter;
199 }
200
1/1
✓ Branch 1 taken 385 times.
385 RadialDiffusion diffusion(n, dx);
201
1/1
✓ Branch 1 taken 385 times.
385 Table table;
202
1/1
✓ Branch 1 taken 385 times.
385 const std::vector<double>& ages = TableAges();
203
1/1
✓ Branch 1 taken 385 times.
385 table.heights.reserve(ages.size() * (n + 1));
204 385 double tau = 0.0;
205
2/2
✓ Branch 0 taken 61971 times.
✓ Branch 1 taken 385 times.
62356 for (const double target : ages) {
206
2/2
✓ Branch 0 taken 903234 times.
✓ Branch 1 taken 61952 times.
965186 while (tau < target) {
207 903234 const double dt = std::fmin(target - tau, std::fmax(dx * dx, 0.002 * tau));
208 903234 diffusion.Step(h, dt);
209 903215 tau += dt;
210 }
211
1/1
✓ Branch 1 taken 61929 times.
123923 table.morphometry.push_back(AppendHeights(h, dx, table.heights));
212 }
213 385 return table;
214 385 }
215
216 std::shared_ptr<const DiffusedProfile::Table>
217 9745004 DiffusedProfile::CachedTable(const CraterMorphometry& fresh, double diameter) const {
218
2/2
✓ Branch 1 taken 8305521 times.
✓ Branch 2 taken 1439537 times.
9745004 const double reference = m_fresh->IsComplex(diameter) ? diameter : kReferenceDiameter;
219 9745058 const Key key{fresh.depthRatio, fresh.rimHeightRatio, reference};
220 9745058 struct Last {
221 std::uint64_t instance = 0;
222 Key key{};
223 std::shared_ptr<const Table> table;
224 };
225
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 9744964 times.
9745058 thread_local Last last;
226
2/2
✓ Branch 0 taken 9744636 times.
✓ Branch 1 taken 422 times.
9745058 if (last.instance == m_instance && last.key == key) {
227
1/2
✓ Branch 0 taken 9718031 times.
✗ Branch 1 not taken.
9718031 return last.table;
228 }
229
2/3
✓ Branch 2 taken 27048 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 27048 times.
54457 last.table = m_tables->Get(key, [&] { return Solve(fresh, reference); });
230 27048 last.instance = m_instance;
231 27048 last.key = key;
232
1/2
✓ Branch 0 taken 27048 times.
✗ Branch 1 not taken.
9772334 return last.table;
233 }
234
235 std::shared_ptr<const std::vector<float>>
236 71038 DiffusedProfile::HeightTable(const CraterMorphometry& fresh, double diameter) const {
237 71038 const std::shared_ptr<const Table> table = CachedTable(fresh, diameter);
238
2/4
✓ Branch 0 taken 71042 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 71045 times.
✗ Branch 3 not taken.
142087 return {table, &table->heights};
239 71042 }
240
241 12392035 double DiffusedProfile::Height(double distance, double diameter, const CraterState& state) const {
242
2/2
✓ Branch 0 taken 2847742 times.
✓ Branch 1 taken 9544293 times.
12392035 if (!(state.diffusion > 0.0)) {
243 2847742 return m_fresh->Height(distance, diameter, {state.fresh, state.fresh, 0.0});
244 }
245 9544293 const float x = static_cast<float>(distance) / static_cast<float>(diameter);
246
2/2
✓ Branch 0 taken 9526892 times.
✓ Branch 1 taken 17401 times.
9544293 if (x >= m_kernel.tableExtent) {
247 return 0.0;
248 }
249 9526892 const auto table = CachedTable(state.fresh, diameter);
250 9526892 CraterData heights;
251
1/1
✓ Branch 1 taken 9526892 times.
9526892 heights.values = table->heights.data();
252 9526892 return diameter *
253 9526892 static_cast<double>(KernelDiffusedValue(m_kernel, KernelAges(), heights, 0u, x,
254
2/3
✓ Branch 1 taken 9526892 times.
✓ Branch 3 taken 9526892 times.
✗ Branch 4 not taken.
19053784 static_cast<float>(state.diffusion)));
255 9526892 }
256
257 321323 CraterMorphometry DiffusedProfile::Morphometry(const CraterMorphometry& fresh, double diameter,
258 double tau) const {
259
2/2
✓ Branch 0 taken 247797 times.
✓ Branch 1 taken 73526 times.
321323 if (!(tau > 0.0)) {
260 247797 return fresh;
261 }
262 73526 const auto table = CachedTable(fresh, diameter);
263
1/1
✓ Branch 1 taken 73546 times.
73568 const KernelAgeRow age = KernelAgeRowAt(m_kernel, KernelAges(), static_cast<float>(tau));
264
2/2
✓ Branch 0 taken 73418 times.
✓ Branch 1 taken 140 times.
73558 CraterMorphometry result = table->morphometry[age.row];
265
2/2
✓ Branch 0 taken 73418 times.
✓ Branch 1 taken 140 times.
73558 if (age.weight > 0.0f) {
266 73418 const auto weight = static_cast<double>(age.weight);
267 73418 const CraterMorphometry& next = table->morphometry[age.row + 1];
268 73418 result.depthRatio += weight * (next.depthRatio - result.depthRatio);
269 73418 result.rimHeightRatio += weight * (next.rimHeightRatio - result.rimHeightRatio);
270 }
271
1/2
✓ Branch 0 taken 73565 times.
✗ Branch 1 not taken.
73558 return result;
272 73569 }
273
274 73666 double DiffusedProfile::AgeAtDepthRatio(const CraterMorphometry& fresh, double diameter,
275 double depthRatio) const {
276
1/2
✓ Branch 1 taken 73784 times.
✗ Branch 2 not taken.
73666 const std::vector<CraterMorphometry>& rows = CachedTable(fresh, diameter)->morphometry;
277 73786 const std::vector<double>& ages = TableAges();
278
2/2
✓ Branch 0 taken 73791 times.
✓ Branch 1 taken 1 time.
73792 if (rows.front().depthRatio <= depthRatio) {
279 return 0.0;
280 }
281
2/2
✓ Branch 0 taken 8544910 times.
✓ Branch 1 taken 1 time.
8544911 for (std::size_t r = 1; r < rows.size(); ++r) {
282
2/2
✓ Branch 0 taken 73790 times.
✓ Branch 1 taken 8471120 times.
8544910 const double above = rows[r - 1].depthRatio;
283 8544910 const double below = rows[r].depthRatio;
284
2/2
✓ Branch 0 taken 73790 times.
✓ Branch 1 taken 8471120 times.
8544910 if (below <= depthRatio) {
285 73790 const double weight = (above - depthRatio) / (above - below);
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73790 times.
73790 return r == 1 ? weight * ages[1]
287 73790 : ages[r - 1] * std::pow(ages[r] / ages[r - 1], weight);
288 }
289 }
290 1 return ages.back();
291 }
292
293 219 DiffusionDegradation::DiffusionDegradation(FreshMorphometry fresh, DiffusionAgeModel ages,
294 219 std::shared_ptr<const DiffusedProfile> profile)
295
5/6
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 219 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 218 times.
✓ Branch 5 taken 1 time.
656 : m_fresh(std::move(fresh)), m_ages(std::move(ages)), m_profile(std::move(profile)) {
296
5/6
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 218 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 217 times.
219 if (!m_fresh || !m_ages || !m_profile) {
297 2 throw std::invalid_argument(
298
1/1
✓ Branch 2 taken 2 times.
2 "Diffusion degradation needs fresh morphometry, diffusion ages and a diffused profile");
299 }
300
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
220 }
301
302 320418 CraterState DiffusionDegradation::State(const Crater& crater) const {
303 320418 return WornState(crater, 1.0);
304 }
305
306 321003 CraterState DiffusionDegradation::WornState(const Crater& crater, double rate) const {
307 321003 RequireDiameter(crater.diameter);
308
1/2
✓ Branch 0 taken 321025 times.
✗ Branch 1 not taken.
321025 if (!IsNonNegative(crater.ageGyr)) {
309
1/1
✓ Branch 2 taken 1 time.
1 throw std::domain_error("Crater age must be finite and not negative");
310 }
311 321024 const double kappaT = rate * m_ages(crater.diameter, crater.ageGyr);
312
1/2
✓ Branch 0 taken 321032 times.
✗ Branch 1 not taken.
321032 if (!IsNonNegative(kappaT)) {
313
1/1
✓ Branch 2 taken 1 time.
1 throw std::domain_error("A crater's diffusion age must be finite and not negative");
314 }
315 321031 const CraterMorphometry fresh = m_fresh(crater.diameter);
316 321011 const double tau = kappaT / (crater.diameter * crater.diameter);
317 321011 return {fresh, m_profile->Morphometry(fresh, crater.diameter, tau), tau};
318 }
319
320 } // namespace terraingen
321