./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
27 / 27
100.0%
Branches:
22 / 22
100.0%
Functions:
7 / 7
100.0%

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
4/4
✓ Branch 0 taken 467 times.
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 467 times.
✓ Branch 4 taken 1 time.
468 m_production(std::move(production)), m_ageGyr(ageGyr) {
16
4/4
✓ Branch 0 taken 467 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 465 times.
468 if (!m_production || m_production->Kind() != SfdKind::ProductionRate) {
17
1/1
✓ Branch 2 taken 3 times.
3 throw std::invalid_argument("A production surface needs a production rate distribution");
18 }
19
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 463 times.
465 if (!chronology) {
20
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("A production surface needs a chronology");
21 }
22
1/1
✓ Branch 1 taken 461 times.
463 SetAllowExtrapolation(m_production->AllowsExtrapolation());
23
2/2
✓ Branch 1 taken 461 times.
✓ Branch 4 taken 460 times.
463 m_scale = chronology(m_ageGyr) / m_production->Cumulative(1000.0);
24
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 time.
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
4/4
✓ Branch 0 taken 7318347 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 7318339 times.
7318353 if (!(ageGyr >= 0.0 && ageGyr <= chronology.maxAgeGyr)) {
48
1/1
✓ Branch 2 taken 14 times.
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