FIMS  v0.11.0
Loading...
Searching...
No Matches
fixed.hpp
Go to the documentation of this file.
1
12#ifndef POPULATION_DYNAMICS_AGE_TO_LENGTH_CONVERSION_FIXED_HPP
13#define POPULATION_DYNAMICS_AGE_TO_LENGTH_CONVERSION_FIXED_HPP
14
15// Needed for size_t.
16#include <cstddef>
17
18// Needed for shared_ptr and weak_ptr.
19#include <memory>
20
21// Base age-to-length conversion interface.
22#include "base.hpp"
23
24// Fleet definition, since this age-to-length conversion reads the fleet's fixed
25// age-to-length conversion matrix.
27
28namespace fims_popdy {
29
34template <typename Type>
35struct AgeToLengthConversionFixed : public AgeToLengthConversionBase<Type> {
36 // Non-owning link back to the fleet. weak_ptr avoids creating an
37 // ownership cycle if Fleet later owns an AgeToLengthConversionBase pointer.
38 std::weak_ptr<Fleet<Type>> fleet;
39
46 : AgeToLengthConversionBase<Type>(), fleet(fleet) {}
47
52
58 virtual bool IsActive() const override {
59 // Convert the weak_ptr into a temporary shared_ptr so we can safely
60 // access the fleet if it still exists.
61 std::shared_ptr<Fleet<Type>> fleet_ptr = fleet.lock();
62
63 // If the fleet no longer exists, this age-to-length conversion cannot be
64 // used.
65 if (fleet_ptr == nullptr) {
66 return false;
67 }
68
69 // The fixed age-to-length conversion is active only if:
70 // - the fleet has at least one age
71 // - the fleet has at least one length bin
72 // - the stored fixed matrix has one age x length table, or one per year
73 return fleet_ptr->n_ages > 0 && fleet_ptr->n_lengths > 0 &&
74 HasValidSize(*fleet_ptr);
75 }
76
84 static bool HasValidSize(const Fleet<Type>& fleet_ref) {
85 const size_t table_size = fleet_ref.n_ages * fleet_ref.n_lengths;
86 const size_t matrix_size = fleet_ref.age_to_length_conversion.size();
87 return matrix_size == table_size ||
88 matrix_size == fleet_ref.n_years * table_size;
89 }
91
97 virtual bool PrepareForCurrentState() override { return this->IsActive(); }
98
107 size_t year, size_t age, fims::Vector<Type>& out_row) const override {
108 // Safely access the linked fleet.
109 std::shared_ptr<Fleet<Type>> fleet_ptr = fleet.lock();
110
111 // Stop if:
112 // - the fleet no longer exists
113 // - dimensions are invalid
114 // - the requested age or year is out of range
115 // - the fixed matrix does not have the expected size
116 if (fleet_ptr == nullptr || fleet_ptr->n_ages == 0 ||
117 fleet_ptr->n_lengths == 0 || age >= fleet_ptr->n_ages ||
118 !HasValidSize(*fleet_ptr)) {
119 return false;
120 }
121 const size_t table_size = fleet_ptr->n_ages * fleet_ptr->n_lengths;
122 const bool has_table_per_year =
123 fleet_ptr->age_to_length_conversion.size() != table_size;
124 if (has_table_per_year && year >= fleet_ptr->n_years) {
125 return false;
126 }
127
128 // Resize the output row so it has one entry per fleet length bin.
129 out_row.resize(fleet_ptr->n_lengths);
130
131 // Compute the starting position for this age in the flattened
132 // year x age x length matrix. A single table is shared by every year.
133 const size_t year_offset = has_table_per_year ? year * table_size : 0;
134 const size_t row_offset = year_offset + age * fleet_ptr->n_lengths;
135
136 // Copy the fixed age-to-length probabilities for this age into out_row.
137 for (size_t l = 0; l < fleet_ptr->n_lengths; ++l) {
138 out_row[l] = fleet_ptr->age_to_length_conversion[row_offset + l];
139 }
140
141 // Report success.
142 return true;
143 }
144};
145
146} // namespace fims_popdy
147
148#endif /* POPULATION_DYNAMICS_AGE_TO_LENGTH_CONVERSION_FIXED_HPP */
Declares the AgeToLengthConversionBase class which is the base class for all age-length key functors.
Definition fims_vector.hpp:27
void resize(size_t s)
Changes the number of elements stored.
Definition fims_vector.hpp:401
Declare the fleet functor class which is the base class for all fleet functors.
The population dynamics of FIMS.
Definition catch_at_age.hpp:45
AgeToLengthConversionFixed(const std::shared_ptr< Fleet< Type > > &fleet)
Constructor.
Definition fixed.hpp:45
std::weak_ptr< Fleet< Type > > fleet
Definition fixed.hpp:38
virtual bool BuildAgeToLengthConversionRow(size_t year, size_t age, fims::Vector< Type > &out_row) const override
Builds the fixed age-to-length conversion row for a given age.
Definition fixed.hpp:106
virtual ~AgeToLengthConversionFixed()
Destructor.
Definition fixed.hpp:51
virtual bool PrepareForCurrentState() override
Prepare the fixed-matrix age-to-length conversion for the current model state.
Definition fixed.hpp:97
virtual bool IsActive() const override
Returns whether this fixed-matrix age-to-length conversion is active and usable.
Definition fixed.hpp:58
Base class for all fleets.
Definition fleet.hpp:26
size_t n_years
Definition fleet.hpp:28
size_t n_ages
Definition fleet.hpp:29
fims::Vector< Type > age_to_length_conversion
Definition fleet.hpp:83
size_t n_lengths
Definition fleet.hpp:30