12#ifndef POPULATION_DYNAMICS_AGE_TO_LENGTH_CONVERSION_DERIVED_HPP
13#define POPULATION_DYNAMICS_AGE_TO_LENGTH_CONVERSION_DERIVED_HPP
34template <
typename Type>
37 std::weak_ptr<SizeDistributionProviderBase<Type>>
39 std::shared_ptr<GrowthDerivedObservationBase<Type>>
55 : AgeToLengthConversionBase<Type>(),
73 std::shared_ptr<Fleet<Type>> fleet_ptr =
fleet_.lock();
74 std::shared_ptr<SizeDistributionProviderBase<Type>> size_provider_ptr =
77 return fleet_ptr !=
nullptr && size_provider_ptr !=
nullptr &&
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);
106 std::shared_ptr<SizeDistributionProviderBase<Type>> size_provider_ptr =
108 if (size_provider_ptr ==
nullptr) {
113 if (growth_products ==
nullptr) {
118 if (growth_products ==
nullptr || growth_products->
n_sexes != 1) {
122 size_provider_ptr->PrepareSizeProducts();
138 std::shared_ptr<Fleet<Type>> fleet_ptr =
fleet_.lock();
139 return fleet_ptr !=
nullptr && out_row.
size() == fleet_ptr->n_lengths;
151 std::size_t age_index)
const {
152 std::shared_ptr<SizeDistributionProviderBase<Type>> size_provider_ptr =
155 if (size_provider_ptr ==
nullptr) {
159 const SizeGrid* population_size_grid = size_provider_ptr->TryGetSizeGrid();
160 if (population_size_grid ==
nullptr ||
162 population_size_grid->
n_bins == 0) {
168 for (std::size_t size_bin_index = 0;
169 size_bin_index < population_size_grid->
n_bins; ++size_bin_index) {
170 prob_size_row[size_bin_index] =
171 size_provider_ptr->ProbSize(year_index, age_index, size_bin_index);
174 return prob_size_row;
188 const Type& minimum_bin_prob =
static_cast<Type
>(1e-12))
const {
189 if (row.
size() != expected_size) {
193 Type row_sum =
static_cast<Type
>(0.0);
194 for (std::size_t i = 0; i < row.
size(); ++i) {
195 row[i] += minimum_bin_prob;
199 for (std::size_t i = 0; i < row.
size(); ++i) {
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 =
219 if (fleet_ptr ==
nullptr || size_provider_ptr ==
nullptr) {
223 const SizeGrid* population_size_grid = size_provider_ptr->TryGetSizeGrid();
224 if (population_size_grid ==
nullptr ||
226 population_size_grid->
n_bins == 0) {
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) {
241 if (population_prob_size.
size() != population_size_grid->
n_bins) {
247 population_size_grid->
edges, fleet_ptr->length_bin_edges);
251 mapping_fleet_edges);
257 throw std::runtime_error(
258 "AgeToLengthConversionDerived produced an invalid mapped fleet "
Declares the AgeToLengthConversionBase class which is the base class for all age-length key functors.
Definition fims_vector.hpp:27
size_type size() const
Returns the number of elements.
Definition fims_vector.hpp:299
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
Defines helpers for mapping population biological size bins to fleet observation bins.
Defines the SizeDistributionProviderBase interface for prepared population-level size products.
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
std::size_t n_sexes
Definition growth_products.hpp:29
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
bool IsConsistent() const
Return whether the grid dimensions and geometry are internally consistent.
Definition size_grid.hpp:80
fims::Vector< double > edges
Definition size_grid.hpp:28
std::size_t n_bins
Definition size_grid.hpp:27