./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
31 / 31
100.0%
Branches:
29 / 37
78.4%
Functions:
7 / 7
100.0%

src/layers/craters/ages/steady_state.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/craters/ages/steady_state.h"
4
5 #include "terraingen/distribution/minimum.h"
6 #include "util/numerics.h"
7
8 #include <cmath>
9 #include <stdexcept>
10 #include <utility>
11 #include <vector>
12
13 namespace terraingen {
14
15 namespace {
16
17 /// An age in [0, `maxAgeGyr`] drawn from the formation rate of `chronology`, by bisection on
18 /// N1(T).
19 375037 double SampleFormationAge(RandomStream& random, const Chronology& chronology, double maxAgeGyr) {
20 375037 const double target = random.NextDouble() * chronology(maxAgeGyr);
21 375170 const BisectionInterval age = BisectRising(0.0, maxAgeGyr, 100, 1e-15 * maxAgeGyr, target,
22 6860829 [&](double t) { return chronology(t); });
23 374133 return 0.5 * (age.low + age.high);
24 }
25
26 } // namespace
27
28 459 SteadyStateAges::SteadyStateAges(std::shared_ptr<const SizeFrequencyDistribution> productionRate,
29 std::shared_ptr<const SizeFrequencyDistribution> equilibrium,
30 459 double ageGyr, Chronology chronology)
31
2/2
✓ Branch 0 taken 458 times.
✓ Branch 1 taken 1 time.
459 : m_productionRate(std::move(productionRate)), m_equilibrium(std::move(equilibrium)),
32
2/2
✓ Branch 0 taken 458 times.
✓ Branch 1 taken 1 time.
459 m_ageGyr(ageGyr), m_chronology(std::move(chronology)) {
33
4/4
✓ Branch 0 taken 458 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 457 times.
459 if (!m_productionRate || m_productionRate->Kind() != SfdKind::ProductionRate) {
34
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Steady-state ages need a production rate distribution");
35 }
36
4/4
✓ Branch 0 taken 456 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 455 times.
✓ Branch 3 taken 1 time.
457 if (!m_equilibrium || m_equilibrium->Kind() != SfdKind::Density) {
37
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Steady-state ages need an equilibrium density");
38 }
39
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 454 times.
455 if (!m_chronology) {
40
1/1
✓ Branch 2 taken 1 time.
1 throw std::invalid_argument("Steady-state ages need a chronology");
41 }
42
1/1
✓ Branch 1 taken 451 times.
454 m_chronology(m_ageGyr);
43
4/4
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 1 time.
466 }
44
45 456476 double SteadyStateAges::EquilibriumLifetimeGyr(double diameter) const {
46 456476 return m_equilibrium->Incremental(diameter) / m_productionRate->Incremental(diameter);
47 }
48
49 382803 double SteadyStateAges::LifetimeAt(double diameter) const {
50 382803 return std::fmin(EquilibriumLifetimeGyr(diameter), m_ageGyr);
51 }
52
53 375084 double SteadyStateAges::SampleAgeAt(RandomStream& random, double /*diameter*/,
54 double lifetimeGyr) const {
55
1/2
✓ Branch 0 taken 375147 times.
✗ Branch 1 not taken.
375084 if (lifetimeGyr == 0.0) {
56 return 0.0;
57 }
58 375147 return SampleFormationAge(random, m_chronology, lifetimeGyr);
59 }
60
61 90 std::shared_ptr<const SizeFrequencyDistribution> EquilibriumSurface::Density() const {
62 90 return std::make_shared<MinimumSfd>(
63
2/4
✓ Branch 1 taken 180 times.
✓ Branch 2 taken 90 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
270 std::vector<std::shared_ptr<const SizeFrequencyDistribution>>{
64
2/6
✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 180 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
630 std::make_shared<ProductionSurface>(production, ageGyr, chronology), equilibrium});
65
2/3
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 90 times.
180 }
66
67 92 std::shared_ptr<const SteadyStateAges> EquilibriumSurface::Ages() const {
68 92 return std::make_shared<SteadyStateAges>(production, equilibrium, ageGyr, chronology);
69 }
70
71 } // namespace terraingen
72