FIMS  v0.10.0
Loading...
Searching...
No Matches
size_bin_mapping.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_POPULATION_DYNAMICS_SIZE_BIN_MAPPING_HPP
10#define FIMS_POPULATION_DYNAMICS_SIZE_BIN_MAPPING_HPP
11
12#include <algorithm>
13#include <cstddef>
14#include <stdexcept>
15
16#include "../size_grid.hpp"
17#include "../../../common/fims_vector.hpp"
18
19namespace fims_popdy {
20
37 static double OverlapWidth(double left_a, double right_a, double left_b,
38 double right_b) {
39 return (std::max)(
40 0.0, (std::min)(right_a, right_b) - (std::max)(left_a, left_b));
41 }
42
50 if (edges.size() < 2) {
51 return false;
52 }
53
54 for (std::size_t i = 0; i + 1 < edges.size(); ++i) {
55 if (!(edges[i + 1] > edges[i])) {
56 return false;
57 }
58 }
59
60 return true;
61 }
62
83 throw std::runtime_error(
84 "SizeBinMapping requires strictly increasing source and "
85 "destination edges");
86 }
87
89 throw std::runtime_error(
90 "SizeBinMapping requires destination edges to cover the full "
91 "source-bin range");
92 }
93
94 const std::size_t n_source_bins = source_edges.size() - 1;
95 const std::size_t n_destination_bins = destination_edges.size() - 1;
96
99
100 for (std::size_t i = 0; i < n_destination_bins; ++i) {
101 weights[i].resize(n_source_bins);
102
103 for (std::size_t j = 0; j < n_source_bins; ++j) {
104 const double source_left = source_edges[j];
105 const double source_right = source_edges[j + 1];
106 const double destination_left = destination_edges[i];
107 const double destination_right = destination_edges[i + 1];
108 const double source_width = source_right - source_left;
109 const double overlap = OverlapWidth(
111
112 weights[i][j] = source_width > 0.0 ? overlap / source_width : 0.0;
113 }
114 }
115
116 return weights;
117 }
118
137
157 throw std::runtime_error(
158 "SizeBinMapping requires strictly increasing source and "
159 "destination edges");
160 }
161
163
164 if (expanded_edges[0] > source_edges[0]) {
166 }
167
168 const std::size_t last_destination_edge = expanded_edges.size() - 1;
169 const std::size_t last_source_edge = source_edges.size() - 1;
170
174 }
175
176 return expanded_edges;
177 }
178
211 template <typename Type>
213 const fims::Vector<fims::Vector<double>>& weights,
216 destination_mass.resize(weights.size());
217
218 for (std::size_t i = 0; i < weights.size(); ++i) {
219 Type total = Type(0.0);
220
221 if (weights[i].size() != source_mass.size()) {
222 throw std::runtime_error(
223 "SizeBinMapping weight rows must match source mass length");
224 }
225
226 for (std::size_t j = 0; j < source_mass.size(); ++j) {
227 total += source_mass[j] * weights[i][j];
228 }
229
231 }
232
233 return destination_mass;
234 }
235};
236
237} // namespace fims_popdy
238
239#endif /* FIMS_POPULATION_DYNAMICS_SIZE_BIN_MAPPING_HPP */
Definition fims_vector.hpp:27
void resize(size_t s)
Changes the number of elements stored.
Definition fims_vector.hpp:401
size_type size() const
Returns the number of elements.
Definition fims_vector.hpp:299
The population dynamics of FIMS.
Definition catch_at_age.hpp:45
std::shared_ptr< AgeToLengthConversionBase< Type > > BuildAgeToLengthConversionFleet(const std::shared_ptr< Population< Type > > &population, const std::shared_ptr< Fleet< Type > > &fleet)
Build the active age-to-length conversion for a fleet from the current population and fleet state.
Definition runtime.hpp:44
Helper for mapping population biological size bins to observed fleet bins.
Definition size_bin_mapping.hpp:28
static bool HasStrictlyIncreasingEdges(const fims::Vector< double > &edges)
Check that a bin-edge vector is usable for geometric mapping.
Definition size_bin_mapping.hpp:49
static fims::Vector< fims::Vector< double > > BuildRebinWeights(const fims::Vector< double > &source_edges, const fims::Vector< double > &destination_edges)
Build overlap-based rebin weights from source bins to destination bins.
Definition size_bin_mapping.hpp:78
static double OverlapWidth(double left_a, double right_a, double left_b, double right_b)
Return the overlap width between two 1-D closed-open intervals.
Definition size_bin_mapping.hpp:37
static bool DestinationCoversSourceRange(const fims::Vector< double > &source_edges, const fims::Vector< double > &destination_edges)
Check that the destination edges fully cover the source-bin range.
Definition size_bin_mapping.hpp:125
static fims::Vector< Type > ApplyRebinWeights(const fims::Vector< fims::Vector< double > > &weights, const fims::Vector< Type > &source_mass)
Apply a source-to-destination bin mapping to a size-mass vector.
Definition size_bin_mapping.hpp:212
static fims::Vector< double > ExpandDestinationEdgesToCoverSourceRange(const fims::Vector< double > &source_edges, const fims::Vector< double > &destination_edges)
Return explicit mapping edges whose support covers the source range while preserving the canonical de...
Definition size_bin_mapping.hpp:152