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
57 Type length_at_age(const Type& age) const {
58 if (reference_age_for_length_young > Type(0.0) &&
60 // For early ages, avoid back-extrapolating the curved growth equation.
61 // This no-seasons ramp goes from length 0 at age 0 to length young at
62 // age young; seasonal growth may need a different transition in the
63 // future.
64 const Type age_nonnegative = age <= Type(0.0) ? Type(0.0) : age;
65 return mean_length_young * age_nonnegative /
67 }
68
69 const Type denom =
70 Type(1.0) -
73 // AD-safe floor to avoid divide-by-zero/NaN when denominator is tiny.
74 const Type denom_safe =
75 fims_math::ad_max(fims_math::ad_fabs(denom), static_cast<Type>(1e-8));
76 const Type numer =
77 Type(1.0) - fims_math::exp(-growth_coefficient *
79 return mean_length_young +
80 (mean_length_old - mean_length_young) * numer / denom_safe;
81 }
82
88 Type log_length_at_age(const Type& age) const {
89 const Type length = length_at_age(age);
90 const Type length_safe = fims_math::ad_max(length, static_cast<Type>(1e-8));
91 return fims_math::log(length_safe);
92 }
93
105 void log_length_at_age_gradient(const Type& age,
106 Type& d_log_laa_d_length_young,
107 Type& d_log_laa_d_length_old,
108 Type& d_log_laa_d_k) const {
109 if (reference_age_for_length_young > Type(0.0) &&
111 const Type age_nonnegative = age <= Type(0.0) ? Type(0.0) : age;
112 const Type ratio = age_nonnegative / reference_age_for_length_young;
113 const Type mean_length = mean_length_young * ratio;
114 const Type mean_length_safe =
115 fims_math::ad_max(mean_length, static_cast<Type>(1e-8));
116
117 d_log_laa_d_length_young = ratio / mean_length_safe;
118 d_log_laa_d_length_old = Type(0.0);
119 d_log_laa_d_k = Type(0.0);
120 return;
121 }
122
123 const Type age_delta_1 = age - reference_age_for_length_young;
124 const Type age_delta_2 =
126
127 const Type exp_num = fims_math::exp(-growth_coefficient * age_delta_1);
128 const Type exp_den = fims_math::exp(-growth_coefficient * age_delta_2);
129
130 const Type numer = Type(1.0) - exp_num;
131 const Type denom = Type(1.0) - exp_den;
132 const Type denom_safe =
133 fims_math::ad_max(fims_math::ad_fabs(denom), static_cast<Type>(1e-8));
134
135 const Type ratio = numer / denom_safe;
136 const Type delta_length = mean_length_old - mean_length_young;
137 const Type mean_length = mean_length_young + delta_length * ratio;
138 const Type mean_length_safe =
139 fims_math::ad_max(mean_length, static_cast<Type>(1e-8));
140
141 const Type d_length_d_l1 = Type(1.0) - ratio;
142 const Type d_length_d_l2 = ratio;
143
144 const Type d_numer_d_k = age_delta_1 * exp_num;
145 const Type d_denom_d_k = age_delta_2 * exp_den;
146 const Type d_ratio_d_k = (d_numer_d_k * denom_safe - numer * d_denom_d_k) /
147 (denom_safe * denom_safe);
148 const Type d_length_d_k = delta_length * d_ratio_d_k;
149
150 d_log_laa_d_length_young = d_length_d_l1 / mean_length_safe;
151 d_log_laa_d_length_old = d_length_d_l2 / mean_length_safe;
152 d_log_laa_d_k = d_length_d_k / mean_length_safe;
153 }
154
169 Type& d_log_laa_d_log_length_young,
170 Type& d_log_laa_d_log_length_old,
171 Type& d_log_laa_d_log_k) const {
172 Type d_log_laa_d_length_young = Type(0.0);
173 Type d_log_laa_d_length_old = Type(0.0);
174 Type d_log_laa_d_k = Type(0.0);
175
176 log_length_at_age_gradient(age, d_log_laa_d_length_young,
177 d_log_laa_d_length_old, d_log_laa_d_k);
178
179 d_log_laa_d_log_length_young = d_log_laa_d_length_young * mean_length_young;
180 d_log_laa_d_log_length_old = d_log_laa_d_length_old * mean_length_old;
181 d_log_laa_d_log_k = d_log_laa_d_k * growth_coefficient;
182 }
183
189 Type weight_at_age(const Type& age) const {
190 Type length = length_at_age(age);
191 return length_weight_a * fims_math::pow(length, length_weight_b);
192 }
193
194 virtual const Type evaluate(int year, const double& a) override {
195 (void)year;
196 return weight_at_age(Type(a));
197 }
198};
199} // namespace fims_popdy
200
201#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:380
const Type ad_fabs(const Type &x, Type C=1e-5)
Definition fims_math.hpp:333
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:57
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:189
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 the log-scale FIMS VonBertalanffySchn...
Definition vonb_schnute.hpp:168
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:88
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:105
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
virtual const Type evaluate(int year, const double &a) override
Calculates the growth at the independent variable value.
Definition vonb_schnute.hpp:194
Type mean_length_old
Expected length at the second reference age.
Definition vonb_schnute.hpp:36