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 | 459 | : m_productionRate(std::move(productionRate)), m_equilibrium(std::move(equilibrium)), | |
| 32 | 459 | m_ageGyr(ageGyr), m_chronology(std::move(chronology)) { | |
| 33 | 459 | if (!m_productionRate || m_productionRate->Kind() != SfdKind::ProductionRate) { | |
| 34 | 2 | throw std::invalid_argument("Steady-state ages need a production rate distribution"); | |
| 35 | } | ||
| 36 | 457 | if (!m_equilibrium || m_equilibrium->Kind() != SfdKind::Density) { | |
| 37 | 2 | throw std::invalid_argument("Steady-state ages need an equilibrium density"); | |
| 38 | } | ||
| 39 | 455 | if (!m_chronology) { | |
| 40 | 1 | throw std::invalid_argument("Steady-state ages need a chronology"); | |
| 41 | } | ||
| 42 | 454 | m_chronology(m_ageGyr); | |
| 43 | 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 | 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 | 270 | std::vector<std::shared_ptr<const SizeFrequencyDistribution>>{ | |
| 64 | 630 | std::make_shared<ProductionSurface>(production, ageGyr, chronology), equilibrium}); | |
| 65 | 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 |