9#ifndef FIMS_POPULATION_DYNAMICS_GROWTH_DERIVED_SIZE_PROVIDER_HPP
10#define FIMS_POPULATION_DYNAMICS_GROWTH_DERIVED_SIZE_PROVIDER_HPP
16#include "../growth/growth_model_adapter.hpp"
19#include "../../common/def.hpp"
20#include "../../common/fims_math.hpp"
34template <
typename Type>
46 : growth_observation_(growth) {}
49 return population_size_grid_;
54 size_products_prepared_ =
false;
55 plus_group_warning_emitted_ =
false;
59 std::size_t n_ages)
override {
62 size_products_prepared_ =
false;
63 plus_group_warning_emitted_ =
false;
67 if (!population_size_grid_) {
68 throw std::runtime_error(
69 "GrowthDerivedSizeProvider requires a population biological size "
74 throw std::runtime_error(
75 "GrowthDerivedSizeProvider requires a consistent population "
76 "biological size grid");
79 if (population_size_grid_->
n_bins == 0) {
80 throw std::runtime_error(
81 "GrowthDerivedSizeProvider requires a population biological size grid"
82 " with at least one bin");
85 if (n_years_ == 0 || n_ages_ == 0) {
86 throw std::runtime_error(
87 "GrowthDerivedSizeProvider requires nonzero population dimensions");
94 throw std::runtime_error(
95 "GrowthDerivedSizeProvider growth products do not match "
96 "configured population dimensions");
100 throw std::runtime_error(
101 "GrowthDerivedSizeProvider currently requires pooled growth "
102 "products with n_sexes == 1");
105 size_products_.ResizeProbSizeOnly(n_years_, n_ages_,
106 population_size_grid_->
n_bins);
149 "Growth-derived size preparation placed more than 0.01 "
150 "probability in the terminal biological plus-group bin for at "
151 "least one year-age row. First occurrence was year index " +
154 ". The population biological size grid may need to be widened.");
155 plus_group_warning_emitted_ =
true;
158 size_products_prepared_ =
true;
162 size_products_prepared_ =
false;
177 if (!size_products_prepared_) {
178 throw std::runtime_error(
179 "GrowthDerivedSizeProvider requires prepared size products");
191 growth_observation_ = growth;
192 size_products_prepared_ =
false;
193 plus_group_warning_emitted_ =
false;
200 if (!population_size_grid_ || population_size_grid_->
n_bins == 0) {
201 throw std::runtime_error(
202 "GrowthDerivedSizeProvider requires a population biological size grid"
203 " before computing ProbSize");
209 static_cast<Type>(1
e-8));
211 if (population_size_grid_->
n_bins == 1) {
212 return static_cast<Type>(1.0);
222 population_size_grid_->
edges[population_size_grid_->
n_bins - 1]);
237 if (!growth_observation_) {
238 throw std::runtime_error(
239 "GrowthDerivedSizeProvider requires a linked growth object");
243 growth_observation_->TryGetPreparedGrowthProducts();
246 throw std::runtime_error(
247 "GrowthDerivedSizeProvider requires prepared growth products");
253 std::shared_ptr<GrowthDerivedObservationBase<Type>> growth_observation_;
254 const SizeGrid* population_size_grid_ =
nullptr;
255 std::size_t n_years_ = 0;
256 std::size_t n_ages_ = 0;
257 bool size_products_prepared_ =
false;
258 bool plus_group_warning_emitted_ =
false;
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
Size provider that converts growth outputs to population size products.
Definition growth_derived_size_provider.hpp:35
void PrepareSizeProducts() override
Prepare size products for the current model state.
Definition growth_derived_size_provider.hpp:66
void SetPopulationDimensions(std::size_t n_years, std::size_t n_ages) override
Configure the provider dimensions for prepared size products.
Definition growth_derived_size_provider.hpp:58
void SetPopulationSizeGrid(const SizeGrid *population_size_grid) override
Configure the provider to use the canonical population size grid.
Definition growth_derived_size_provider.hpp:52
const Type & MeanLAA(std::size_t year_index, std::size_t age_index) const override
Read prepared mean length-at-age.
Definition growth_derived_size_provider.hpp:165
void SetGrowth(std::shared_ptr< GrowthDerivedObservationBase< Type > > growth)
Replace the Growth-derived observation object used by this provider.
Definition growth_derived_size_provider.hpp:190
GrowthDerivedSizeProvider(std::shared_ptr< GrowthDerivedObservationBase< Type > > growth)
Construct a provider linked to a Growth-derived observation object.
Definition growth_derived_size_provider.hpp:44
const SizeGrid * TryGetSizeGrid() const override
Return the biological size grid used by this provider.
Definition growth_derived_size_provider.hpp:48
const Type & SdLAA(std::size_t year_index, std::size_t age_index) const override
Read prepared spread of length-at-age.
Definition growth_derived_size_provider.hpp:170
const Type & ProbSize(std::size_t year_index, std::size_t age_index, std::size_t size_bin_index) const override
Read prepared age-to-size probability on the population grid.
Definition growth_derived_size_provider.hpp:175
void InvalidatePreparedSizeProducts() override
Invalidate any prepared size state held by this provider.
Definition growth_derived_size_provider.hpp:161
Base interface for objects that provide population-level size distributions.
Definition size_distribution_provider_base.hpp:30
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:648
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 the SizeDistributionProviderBase interface for prepared population-level size products.
Defines the SizeProducts container for cached population-level size outputs on the biological size gr...
Growth "products" in a consistent (year, age, sex) space.
Definition growth_products.hpp:26
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