FIMS  v0.10.0
Loading...
Searching...
No Matches
derived.hpp
Go to the documentation of this file.
1
12#ifndef POPULATION_DYNAMICS_AGE_TO_LENGTH_CONVERSION_DERIVED_HPP
13#define POPULATION_DYNAMICS_AGE_TO_LENGTH_CONVERSION_DERIVED_HPP
14
15#include <algorithm>
16#include <cstddef>
17#include <memory>
18#include <stdexcept>
19
20#include "common/fims_math.hpp"
21#include "base.hpp"
26
27namespace fims_popdy {
28
34template <typename Type>
36 std::weak_ptr<Fleet<Type>> fleet_;
37 std::weak_ptr<SizeDistributionProviderBase<Type>>
39 std::shared_ptr<GrowthDerivedObservationBase<Type>>
59
64
72 virtual bool IsActive() const override {
73 std::shared_ptr<Fleet<Type>> fleet_ptr = fleet_.lock();
74 std::shared_ptr<SizeDistributionProviderBase<Type>> size_provider_ptr =
75 size_provider_.lock();
76
77 return fleet_ptr != nullptr && size_provider_ptr != nullptr &&
78 growth_observation_ != nullptr &&
79 growth_observation_->SupportsAgeToLengthConversionDerived() &&
80 fleet_ptr->n_ages > 0 && fleet_ptr->n_lengths > 0 &&
81 fleet_ptr->lengths.size() == fleet_ptr->n_lengths &&
82 fleet_ptr->length_bin_edges.size() == fleet_ptr->n_lengths + 1 &&
84 fleet_ptr->length_bin_edges);
85 }
86
101 virtual bool PrepareForCurrentState() override {
102 if (!this->IsActive()) {
103 return false;
104 }
105
106 std::shared_ptr<SizeDistributionProviderBase<Type>> size_provider_ptr =
108 if (size_provider_ptr == nullptr) {
109 return false;
110 }
111
113 if (growth_products == nullptr) {
114 growth_observation_->PrepareGrowthProducts();
116 }
117
118 if (growth_products == nullptr || growth_products->n_sexes != 1) {
119 return false;
120 }
121
122 size_provider_ptr->PrepareSizeProducts();
123 return true;
124 }
125
136 size_t year, size_t age, fims::Vector<Type>& out_row) const override {
138 std::shared_ptr<Fleet<Type>> fleet_ptr = fleet_.lock();
139 return fleet_ptr != nullptr && out_row.size() == fleet_ptr->n_lengths;
140 }
141
142 protected:
151 std::size_t age_index) const {
152 std::shared_ptr<SizeDistributionProviderBase<Type>> size_provider_ptr =
154
155 if (size_provider_ptr == nullptr) {
156 return fims::Vector<Type>();
157 }
158
159 const SizeGrid* population_size_grid = size_provider_ptr->TryGetSizeGrid();
160 if (population_size_grid == nullptr ||
161 !population_size_grid->IsConsistent() ||
162 population_size_grid->n_bins == 0) {
163 return fims::Vector<Type>();
164 }
165
167
168 for (std::size_t size_bin_index = 0;
172 }
173
174 return prob_size_row;
175 }
176
188 const Type& minimum_bin_prob = static_cast<Type>(1e-12)) const {
189 if (row.size() != expected_size) {
190 return false;
191 }
192
193 Type row_sum = static_cast<Type>(0.0);
194 for (std::size_t i = 0; i < row.size(); ++i) {
196 row_sum += row[i];
197 }
198
199 for (std::size_t i = 0; i < row.size(); ++i) {
200 row[i] /= row_sum;
201 }
202
203 return true;
204 }
205
214 std::size_t year_index, std::size_t age_index) const {
215 std::shared_ptr<Fleet<Type>> fleet_ptr = fleet_.lock();
216 std::shared_ptr<SizeDistributionProviderBase<Type>> size_provider_ptr =
218
219 if (fleet_ptr == nullptr || size_provider_ptr == nullptr) {
220 return fims::Vector<Type>();
221 }
222
223 const SizeGrid* population_size_grid = size_provider_ptr->TryGetSizeGrid();
224 if (population_size_grid == nullptr ||
225 !population_size_grid->IsConsistent() ||
226 population_size_grid->n_bins == 0) {
227 return fims::Vector<Type>();
228 }
229
230 if (fleet_ptr->n_years == 0 || fleet_ptr->n_lengths == 0 ||
231 fleet_ptr->lengths.size() != fleet_ptr->n_lengths ||
232 fleet_ptr->length_bin_edges.size() != fleet_ptr->n_lengths + 1 ||
234 fleet_ptr->length_bin_edges) ||
235 year_index >= fleet_ptr->n_years || age_index >= fleet_ptr->n_ages) {
236 return fims::Vector<Type>();
237 }
238
241 if (population_prob_size.size() != population_size_grid->n_bins) {
242 return fims::Vector<Type>();
243 }
244
247 population_size_grid->edges, fleet_ptr->length_bin_edges);
248
252
255
257 throw std::runtime_error(
258 "AgeToLengthConversionDerived produced an invalid mapped fleet "
259 "probability row");
260 }
261
262 return fleet_row;
263 }
264
269 std::shared_ptr<SizeDistributionProviderBase<Type>> TryGetSizeProvider()
270 const {
271 return size_provider_.lock();
272 }
273
279 if (growth_observation_ == nullptr ||
280 !growth_observation_->SupportsAgeToLengthConversionDerived()) {
281 return nullptr;
282 }
283
284 return growth_observation_->TryGetPreparedGrowthProducts();
285 }
286};
287
288} // namespace fims_popdy
289
290#endif /* POPULATION_DYNAMICS_AGE_TO_LENGTH_CONVERSION_DERIVED_HPP */
Declares the AgeToLengthConversionBase class which is the base class for all age-length key functors.
Definition fims_vector.hpp:27
Generic capability interface for growth models that can feed the growth-derived age-to-length convers...
Definition growth_model_adapter.hpp:26
Base interface for objects that provide population-level size distributions.
Definition size_distribution_provider_base.hpp:30
A collection of mathematical functions used in FIMS.
Declare the fleet functor class which is the base class for all fleet functors.
adapts product-style growth models to the GrowthBase interface used by caa.
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
Defines helpers for mapping population biological size bins to fleet observation bins.
Defines the SizeDistributionProviderBase interface for prepared population-level size products.
Base interface for age-length key implementations.
Definition base.hpp:26
Growth-derived age-to-length conversion implementation that reads prepared population age-to-size pro...
Definition derived.hpp:35
virtual bool PrepareForCurrentState() override
Ensures the linked growth products and population size-provider products are prepared for the current...
Definition derived.hpp:101
fims::Vector< Type > BuildPopulationProbSizeRow(std::size_t year_index, std::size_t age_index) const
Read one prepared age-to-size row from the linked population size provider.
Definition derived.hpp:150
std::weak_ptr< SizeDistributionProviderBase< Type > > size_provider_
Definition derived.hpp:38
std::shared_ptr< GrowthDerivedObservationBase< Type > > growth_observation_
Definition derived.hpp:40
virtual bool IsActive() const override
Returns whether this growth-derived age-to-length conversion is structurally active.
Definition derived.hpp:72
virtual bool BuildAgeToLengthConversionRow(size_t year, size_t age, fims::Vector< Type > &out_row) const override
Builds the normalized age-to-length conversion row for a given year and age.
Definition derived.hpp:135
AgeToLengthConversionDerived(const std::shared_ptr< Fleet< Type > > &fleet_, const std::shared_ptr< GrowthDerivedObservationBase< Type > > &growth_observation_, const std::shared_ptr< SizeDistributionProviderBase< Type > > &size_provider_)
Constructor.
Definition derived.hpp:50
virtual ~AgeToLengthConversionDerived()
Destructor.
Definition derived.hpp:63
bool TryFinalizeMappedProbabilityRow(fims::Vector< Type > &row, std::size_t expected_size, const Type &minimum_bin_prob=static_cast< Type >(1e-12)) const
Normalize one mapped fleet probability row.
Definition derived.hpp:186
const GrowthProducts< Type > * TryGetGrowthProducts() const
Try to get prepared growth products from the linked growth object.
Definition derived.hpp:278
std::weak_ptr< Fleet< Type > > fleet_
Definition derived.hpp:36
std::shared_ptr< SizeDistributionProviderBase< Type > > TryGetSizeProvider() const
Try to get the linked population size provider.
Definition derived.hpp:269
fims::Vector< Type > BuildMappedFleetAgeToLengthConversionRow(std::size_t year_index, std::size_t age_index) const
Build one fleet age-to-length conversion row by mapping a population age-to-size row onto fleet obser...
Definition derived.hpp:213
Base class for all fleets.
Definition fleet.hpp:26
Growth "products" in a consistent (year, age, sex) space.
Definition growth_products.hpp:26
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 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
Population-level biological size grid.
Definition size_grid.hpp:26