src/distribution/production_surface.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/distribution/production_surface.h" | ||
| 4 | |||
| 5 | #include <cmath> | ||
| 6 | #include <stdexcept> | ||
| 7 | #include <utility> | ||
| 8 | |||
| 9 | namespace terraingen { | ||
| 10 | |||
| 11 | 468 | ProductionSurface::ProductionSurface(std::shared_ptr<const SizeFrequencyDistribution> production, | |
| 12 | 468 | double ageGyr, const Chronology& chronology) | |
| 13 | : SizeFrequencyDistribution(SfdKind::Density, | ||
| 14 | 467 | production ? production->ValidRange() : DiameterRange{}), | |
| 15 | 468 | m_production(std::move(production)), m_ageGyr(ageGyr) { | |
| 16 | 468 | if (!m_production || m_production->Kind() != SfdKind::ProductionRate) { | |
| 17 | 3 | throw std::invalid_argument("A production surface needs a production rate distribution"); | |
| 18 | } | ||
| 19 | 465 | if (!chronology) { | |
| 20 | 2 | throw std::invalid_argument("A production surface needs a chronology"); | |
| 21 | } | ||
| 22 | 463 | SetAllowExtrapolation(m_production->AllowsExtrapolation()); | |
| 23 | 463 | m_scale = chronology(m_ageGyr) / m_production->Cumulative(1000.0); | |
| 24 | 468 | } | |
| 25 | |||
| 26 | 2642385 | double ProductionSurface::CumulativeAt(double diameter) const { | |
| 27 | 2642385 | return m_scale * UncheckedCumulative(*m_production, diameter); | |
| 28 | } | ||
| 29 | |||
| 30 | 6 | double ProductionSurface::IncrementalAt(double diameter) const { | |
| 31 | 6 | return m_scale * UncheckedIncremental(*m_production, diameter); | |
| 32 | } | ||
| 33 | |||
| 34 | 81298 | double ProductionSurface::InvertCumulative(double target, double minDiameter, | |
| 35 | double maxDiameter) const { | ||
| 36 | 81298 | return UncheckedInverse(*m_production, target / m_scale, minDiameter, maxDiameter); | |
| 37 | } | ||
| 38 | |||
| 39 | namespace detail { | ||
| 40 | |||
| 41 | namespace { | ||
| 42 | |||
| 43 | /// Square meters per square kilometer. | ||
| 44 | constexpr double kSquareKilometer = 1e6; | ||
| 45 | |||
| 46 | 7318353 | void CheckAge(const ExponentialChronology& chronology, double ageGyr) { | |
| 47 | 7318353 | if (!(ageGyr >= 0.0 && ageGyr <= chronology.maxAgeGyr)) { | |
| 48 | 14 | throw std::domain_error(chronology.ageError); | |
| 49 | } | ||
| 50 | 7318339 | } | |
| 51 | |||
| 52 | } // namespace | ||
| 53 | |||
| 54 | 7319116 | double ExponentialChronology::Count(double ageGyr) const { | |
| 55 | 7319116 | CheckAge(*this, ageGyr); | |
| 56 | 7320583 | return (burst * std::expm1(decay * ageGyr) + linear * ageGyr) / kSquareKilometer; | |
| 57 | } | ||
| 58 | |||
| 59 | 31 | double ExponentialChronology::Rate(double ageGyr) const { | |
| 60 | 31 | CheckAge(*this, ageGyr); | |
| 61 | 27 | return (burst * decay * std::exp(decay * ageGyr) + linear) / kSquareKilometer; | |
| 62 | } | ||
| 63 | |||
| 64 | } // namespace detail | ||
| 65 | |||
| 66 | } // namespace terraingen | ||
| 67 |