FIMS  v0.11.0
Loading...
Searching...
No Matches
vonb_schnute.hpp
Go to the documentation of this file.
1
9#ifndef POPULATION_DYNAMICS_GROWTH_VONB_SCHNUTE_HPP
10#define POPULATION_DYNAMICS_GROWTH_VONB_SCHNUTE_HPP
11
13#include "growth_base.hpp"
14
15namespace fims_popdy {
16
31template <typename Type>
32struct VonBertalanffySchnuteGrowth : public GrowthBase<Type> {
34 Type mean_length_young = Type(0.0);
36 Type mean_length_old = Type(0.0);
38 Type growth_coefficient = Type(0.0);
43
45 Type length_weight_a = Type(0.0);
47 Type length_weight_b = Type(3.0);
48
49 VonBertalanffySchnuteGrowth() : GrowthBase<Type>() {}
51
59 Type growth_fraction(const Type& age, Type* derivative = nullptr) const {
60 const Type elapsed_age = age - reference_age_for_length_young;
61 const Type reference_age_span =
63
64 // Evaluate (1 - exp(-x)) / x and, optionally, its derivative.
65 auto scaled_decay = [](const Type& x, Type* decay_derivative) -> Type {
66 const Type threshold = Type(1e-3);
67 // Keep both expressions safe because AD may evaluate the inactive branch.
68 const Type series_x = fims_math::cond_exp_lt(x, threshold, x, Type(0.0));
69 const Type regular_x = fims_math::cond_exp_lt(x, threshold, Type(1.0), x);
70
71 const Type series =
72 Type(1.0) +
73 series_x * (Type(-0.5) +
74 series_x * (Type(1.0 / 6.0) +
75 series_x * (Type(-1.0 / 24.0) +
76 series_x * Type(1.0 / 120.0))));
77 const Type regular = (Type(1.0) - fims_math::exp(-regular_x)) / regular_x;
78
79 if (decay_derivative != nullptr) {
80 const Type series_derivative =
81 Type(-0.5) + series_x * (Type(1.0 / 3.0) +
82 series_x * (Type(-1.0 / 8.0) +
83 series_x * Type(1.0 / 30.0)));
84 const Type regular_derivative =
85 (fims_math::exp(-regular_x) - regular) / regular_x;
86
87 *decay_derivative = fims_math::cond_exp_lt(
88 x, threshold, series_derivative, regular_derivative);
89 }
90
91 return fims_math::cond_exp_lt(x, threshold, series, regular);
92 };
93
94 Type elapsed_derivative = Type(0.0);
95 Type reference_derivative = Type(0.0);
96 const Type elapsed_decay =
97 scaled_decay(growth_coefficient * elapsed_age,
98 derivative != nullptr ? &elapsed_derivative : nullptr);
99 const Type reference_decay =
100 scaled_decay(growth_coefficient * reference_age_span,
101 derivative != nullptr ? &reference_derivative : nullptr);
102 const Type age_ratio = elapsed_age / reference_age_span;
103 const Type decay_ratio = elapsed_decay / reference_decay;
104
105 if (derivative != nullptr) {
106 *derivative = age_ratio *
107 (elapsed_age * elapsed_derivative -
108 decay_ratio * reference_age_span * reference_derivative) /
109 reference_decay;
110 }
111
112 return age_ratio * decay_ratio;
113 }
114
120 Type length_at_age(const Type& age) const {
121 if (reference_age_for_length_young > Type(0.0) &&
123 // For early ages, avoid back-extrapolating the curved growth equation.
124 // This no-seasons ramp goes from length 0 at age 0 to length young at
125 // age young; seasonal growth may need a different transition in the
126 // future.
127 const Type age_nonnegative = age <= Type(0.0) ? Type(0.0) : age;
128 return mean_length_young * age_nonnegative /
130 }
131
132 return mean_length_young +
134 }
135
141 Type log_length_at_age(const Type& age) const {
142 const Type length = length_at_age(age);
143 const Type length_safe = fims_math::ad_max(length, static_cast<Type>(1e-8));
144 return fims_math::log(length_safe);
145 }
146
158 void log_length_at_age_gradient(const Type& age,
159 Type& d_log_laa_d_length_young,
160 Type& d_log_laa_d_length_old,
161 Type& d_log_laa_d_k) const {
162 if (reference_age_for_length_young > Type(0.0) &&
164 const Type age_nonnegative = age <= Type(0.0) ? Type(0.0) : age;
165 const Type ratio = age_nonnegative / reference_age_for_length_young;
166 const Type mean_length = mean_length_young * ratio;
167 const Type mean_length_safe =
168 fims_math::ad_max(mean_length, static_cast<Type>(1e-8));
169
170 d_log_laa_d_length_young = ratio / mean_length_safe;
171 d_log_laa_d_length_old = Type(0.0);
172 d_log_laa_d_k = Type(0.0);
173 return;
174 }
175
176 Type d_ratio_d_k = Type(0.0);
177 const Type ratio = growth_fraction(age, &d_ratio_d_k);
178 const Type delta_length = mean_length_old - mean_length_young;
179 const Type mean_length = mean_length_young + delta_length * ratio;
180 const Type mean_length_safe =
181 fims_math::ad_max(mean_length, static_cast<Type>(1e-8));
182
183 const Type d_length_d_l1 = Type(1.0) - ratio;
184 const Type d_length_d_l2 = ratio;
185 const Type d_length_d_k = delta_length * d_ratio_d_k;
186 d_log_laa_d_length_young = d_length_d_l1 / mean_length_safe;
187 d_log_laa_d_length_old = d_length_d_l2 / mean_length_safe;
188 d_log_laa_d_k = d_length_d_k / mean_length_safe;
189 }
190
206 Type& d_log_laa_d_log_length_young,
207 Type& d_log_laa_d_log_length_old,
208 Type& d_log_laa_d_log_k) const {
209 Type d_log_laa_d_length_young = Type(0.0);
210 Type d_log_laa_d_length_old = Type(0.0);
211 Type d_log_laa_d_k = Type(0.0);
212
213 log_length_at_age_gradient(age, d_log_laa_d_length_young,
214 d_log_laa_d_length_old, d_log_laa_d_k);
215
216 d_log_laa_d_log_length_young = d_log_laa_d_length_young * mean_length_young;
217 d_log_laa_d_log_length_old = d_log_laa_d_length_old * mean_length_old;
218 d_log_laa_d_log_k = d_log_laa_d_k * growth_coefficient;
219 }
220
226 Type weight_at_age(const Type& age) const {
227 Type length = length_at_age(age);
228 return length_weight_a * fims_math::pow(length, length_weight_b);
229 }
230
231 virtual const Type evaluate(int year, const double& a) override {
232 (void)year;
233 return weight_at_age(Type(a));
234 }
235};
236} // namespace fims_popdy
237
238#endif // POPULATION_DYNAMICS_GROWTH_VONB_SCHNUTE_HPP
A collection of mathematical functions used in FIMS.
const Type ad_max(const Type &a, const Type &b, Type C=1e-5)
Definition fims_math.hpp:410
Declares the GrowthBase class which is the base class for all growth functors.
The population dynamics of FIMS.
Definition catch_at_age.hpp:45
GrowthBase()
Constructor.
Definition growth_base.hpp:34
VonBertalanffySchnute growth functor for length-at-age and weight-at-age.
Definition vonb_schnute.hpp:32
Type mean_length_young
Expected length at the first reference age.
Definition vonb_schnute.hpp:34
Type length_at_age(const Type &age) const
Evaluate mean length at age.
Definition vonb_schnute.hpp:120
Type reference_age_for_length_old
Second reference age for the length parameterization.
Definition vonb_schnute.hpp:42
Type growth_coefficient
Growth coefficient.
Definition vonb_schnute.hpp:38
Type weight_at_age(const Type &age) const
Evaluate mean weight at age via the length-weight relationship.
Definition vonb_schnute.hpp:226
void log_length_at_age_logscale_gradient(const Type &age, Type &d_log_laa_d_log_length_young, Type &d_log_laa_d_log_length_old, Type &d_log_laa_d_log_k) const
Evaluate the gradient of log mean length at age with respect to [log(mean_length_young),...
Definition vonb_schnute.hpp:205
Type length_weight_b
Exponent in the length-weight relationship, W = a * L^b.
Definition vonb_schnute.hpp:47
Type log_length_at_age(const Type &age) const
Evaluate log mean length at age.
Definition vonb_schnute.hpp:141
void log_length_at_age_gradient(const Type &age, Type &d_log_laa_d_length_young, Type &d_log_laa_d_length_old, Type &d_log_laa_d_k) const
Evaluate the gradient of log mean length at age with respect to the current natural-scale FIMS VonBer...
Definition vonb_schnute.hpp:158
Type reference_age_for_length_young
First reference age for the length parameterization.
Definition vonb_schnute.hpp:40
Type length_weight_a
Coefficient in the length-weight relationship, W = a * L^b.
Definition vonb_schnute.hpp:45
Type growth_fraction(const Type &age, Type *derivative=nullptr) const
Growth fraction at or after the younger reference age.
Definition vonb_schnute.hpp:59
virtual const Type evaluate(int year, const double &a) override
Calculates the growth at the independent variable value.
Definition vonb_schnute.hpp:231
Type mean_length_old
Expected length at the second reference age.
Definition vonb_schnute.hpp:36