FIMS  v0.10.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>
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;
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 the expected age x length size
73 return fleet_ptr->n_ages > 0 && fleet_ptr->n_lengths > 0 &&
74 fleet_ptr->age_to_length_conversion.size() ==
75 (fleet_ptr->n_ages * fleet_ptr->n_lengths);
76 }
77
83 virtual bool PrepareForCurrentState() override { return this->IsActive(); }
84
93 size_t year, size_t age, fims::Vector<Type>& out_row) const override {
94 // The current fixed matrix path does not vary by year, but year stays
95 // in the interface so all age-to-length conversion types share the same
96 // method signature.
97 (void)year;
98
99 // Safely access the linked fleet.
100 std::shared_ptr<Fleet<Type>> fleet_ptr = fleet.lock();
101
102 // Stop if:
103 // - the fleet no longer exists
104 // - dimensions are invalid
105 // - the requested age is out of range
106 // - the fixed matrix does not have the expected size
107 if (fleet_ptr == nullptr || fleet_ptr->n_ages == 0 ||
108 fleet_ptr->n_lengths == 0 || age >= fleet_ptr->n_ages ||
109 fleet_ptr->age_to_length_conversion.size() !=
110 (fleet_ptr->n_ages * fleet_ptr->n_lengths)) {
111 return false;
112 }
113
114 // Resize the output row so it has one entry per fleet length bin.
115 out_row.resize(fleet_ptr->n_lengths);
116
117 // Compute the starting position for this age in the flattened
118 // age x length matrix.
119 const size_t row_offset = age * fleet_ptr->n_lengths;
120
121 // Copy the fixed age-to-length probabilities for this age into out_row.
122 for (size_t l = 0; l < fleet_ptr->n_lengths; ++l) {
123 out_row[l] = fleet_ptr->age_to_length_conversion[row_offset + l];
124 }
125
126 // Report success.
127 return true;
128 }
129};
130
131} // namespace fims_popdy
132
133#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
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
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
Base interface for age-length key implementations.
Definition base.hpp:26
Fixed age-to-length conversion implementation using a fleet's stored age-to-length conversion matrix.
Definition fixed.hpp:35
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:92
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:83
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