Rceattle C++ template
The CEATTLE likelihood: src/TMB/ceattle.cpp and its headers
Loading...
Searching...
No Matches
growth.hpp
Go to the documentation of this file.
1#ifndef GROWTH_HPP
2#define GROWTH_HPP
3
4/**
5 * @brief SD of length-at-age (cm) for one age, year and sex.
6 *
7 * The variability term v is \f$e^{sd_0}\f$ at or below `age_L1`, pinned to
8 * \f$e^{sd_1}\f$ for the plus group under `sd_style == 1` (WHAM; also SS3 with
9 * Growth_Age_for_L2 = 999), and otherwise interpolated linearly in length between
10 * \f$(l_1, e^{sd_0})\f$ and \f$(L_\infty, e^{sd_1})\f$. `sd_form` 1 makes v an SD in cm
11 * (SS3 CV_Growth_Pattern 2), 2 a CV, so SD = v * L (SS3 CV_Growth_Pattern 0).
12 */
13template<class Type>
14Type length_sd_at_age(Type current_age, Type age_L1, bool plus_group,
15 int sd_style, int sd_form, Type l1, Type linf, Type len,
16 Type log_sd0, Type log_sd1) {
17 Type sd0 = exp(log_sd0);
18 Type sd1 = exp(log_sd1);
19 Type v;
20 if(current_age <= age_L1) {
21 v = sd0;
22 } else if(plus_group && sd_style == 1) {
23 v = sd1;
24 } else {
25 v = sd0 + (sd1 - sd0) / (linf - l1) * (len - l1);
26 }
27 if(sd_form == 2) v = v * len;
28 return v;
29}
30
31
32/**
33 * @brief Age-length key, weight-at-age and mature weight-at-age for one cell.
34 *
35 * Length is normal with mean `mu` and SD `sd` (cm) on the population length bins
36 * `lengths_pop` (lower edges): the first bin is a minus group, the last a plus group
37 * (SS3's convention).
38 *
39 * Weight-at-age is \f$\sum_l P(l|a) \alpha L_{mid}^\beta\f$ at population-bin midpoints
40 * (kg). With maturity-at-length, mature weight-at-age is the same sum with the logistic
41 * maturity \f$1/(1+e^{-s(L_{mid} - L_{50})})\f$ inside: SS3's fecundity-at-age when
42 * fecundity equals body weight.
43 */
44template<class Type>
45void fill_age_length_key(int wtind, int sp, int sex, int age, int yr,
46 Type mu, Type sd,
47 const vector<int>& nlengths_pop,
48 matrix<Type>& lengths_pop,
49 matrix<Type>& weight_length_pars,
50 const vector<int>& mat_len_use,
51 matrix<Type>& mat_len_pars,
52 array<Type>& growth_matrix,
53 array<Type>& weight_hat,
54 array<Type>& mat_weight_hat) {
55 int np = nlengths_pop(sp);
56 // Kept on the POPULATION bins: readers multiply by selectivity-at-length there, as SS3
57 // does, and sum into data bins afterwards (ceattle.cpp section 2.3c). Aggregating here
58 // would apply one bin-average selectivity across a data bin.
59 for(int lp = 0; lp < np; lp++) growth_matrix(wtind, sex, age, lp, yr) = Type(0.0);
60
61 Type expected_weight = 0.0;
62 Type expected_mat_weight = 0.0;
63 for(int lp = 0; lp < np; lp++) {
64 Type prob;
65 if(np < 2) {
66 // One bin spans the whole length distribution, so it holds all of it.
67 // Tested before the lp == 0 branch, which would read the second edge.
68 prob = Type(1.0);
69 } else if(lp == 0) {
70 prob = pnorm((lengths_pop(sp, 1) - mu) / sd);
71 } else if(lp == (np - 1)) {
72 prob = 1.0 - pnorm((lengths_pop(sp, np - 1) - mu) / sd);
73 } else {
74 prob = pnorm((lengths_pop(sp, lp + 1) - mu) / sd) - pnorm((lengths_pop(sp, lp) - mu) / sd);
75 }
76 growth_matrix(wtind, sex, age, lp, yr) = prob;
77
78 // Bin midpoint (cm): lower edge plus half its own width, the last bin taking the
79 // previous bin's width. A single-bin grid has no neighbour to take a width from,
80 // so it keeps the edge itself -- as selectivity.hpp does.
81 Type lenmid;
82 if(lp < np - 1) {
83 lenmid = (lengths_pop(sp, lp) + lengths_pop(sp, lp + 1)) / Type(2.0);
84 } else if(np < 2) {
85 lenmid = lengths_pop(sp, np - 1);
86 } else {
87 lenmid = lengths_pop(sp, np - 1) + (lengths_pop(sp, np - 1) - lengths_pop(sp, np - 2)) / Type(2.0);
88 }
89 Type wt_len = weight_length_pars(sp, 0) * pow(lenmid, weight_length_pars(sp, 1));
90 expected_weight += prob * wt_len;
91 if(mat_len_use(sp) == 1) {
92 expected_mat_weight += prob * wt_len / (Type(1.0) + exp(-mat_len_pars(sp, 1) * (lenmid - mat_len_pars(sp, 0))));
93 }
94 }
95 weight_hat(wtind, sex, age, yr) = expected_weight;
96 if(mat_len_use(sp) == 1) mat_weight_hat(wtind, sex, age, yr) = expected_mat_weight;
97}
98
99/**
100 * @brief Jan-1 (month 0) mean length, age-length key and weight-at-age for one species.
101 *
102 * Mean length-at-age (cm) is anchored at \f$L(a_{L1}) = l_1\f$:
103 * - von Bertalanffy (`growth_model` 1):
104 * \f$L_a = L_{\infty} + (l_1 - L_{\infty}) \cdot e^{-K(a - a_{L1})}\f$
105 * - Richards (2):
106 * \f$L_a = [L_{\infty}^m + (l_1^m - L_{\infty}^m) \cdot e^{-K(a - a_{L1})}]^{1/m}\f$
107 * - Below the anchor, a linear ramp from `Lmin_sp` at age 0 to \f$l_1\f$ at `age_L1`.
108 * - After year 0 a cohort recursion advances \f$L_{a, y}\f$ from \f$L_{a-1, y-1}\f$ with
109 * the previous year's parameters; where that predecessor is still on the ramp, or at
110 * slot 0, the closed-form anchor at \f$l_1\f$ is used instead.
111 *
112 * Adapted from Giancarlo Correa's mean length-at-age block in src/wham_v0.cpp, WHAM's
113 * growth branch (timjmiller/wham at 16770d4, GPL-3).
114 *
115 * Weight-at-age (kg) is integrated across the length distribution rather than taken at
116 * mean length, which Jensen's inequality would bias:
117 * \f$W_a = \sum_{ln} P(ln | a) \cdot \alpha \cdot L_{mid}^{\beta}\f$.
118 *
119 * @param wtind Weight index slot to write into.
120 * @param sp Species index.
121 * @param minage Minimum modelled age per species; 0 is supported.
122 * @param growth_model Per species, 1 = von Bertalanffy, 2 = Richards.
123 * @param growth_parameters Time-varying growth parameters by species, sex, year and
124 * parameter: K (per year), L1 and Linf (cm), Richards m.
125 * @param growth_log_sd Log-scale variability endpoints: index 0 at L1, index 1 at Linf.
126 * @param weight_length_pars Allometric \f$\alpha\f$ and \f$\beta\f$, kg from cm.
127 * @param length_hat [Output] mean length-at-age (cm).
128 * @param growth_matrix [Output] age-length key on the population length bins.
129 * @param weight_hat [Output] integrated weight-at-age (kg).
130 */
131template<class Type>
133 int wtind,
134 int sp,
135 int nspp,
136 int nyrs,
137 const vector<int>& nsex,
138 const vector<int>& nages,
139 const vector<int>& nlengths,
140 const vector<int>& minage,
141 const vector<Type>& growth_age_L1,
142 const vector<int>& growth_model,
143 const vector<int>& growth_sd_style, // Plus-group SD-at-age: 1 = WHAM pin, 2 = SS3 by length
144 const vector<int>& growth_sd_form, // 1 = SD endpoints in cm, 2 = CV endpoints (SD = CV * L)
145 const vector<int>& growth_plus_length,// Plus-group mean length: 1 = M1-weighted,
146 // 2 = no adjustment, 3/4 = SS3 Linf_decay
147 const vector<Type>& plus_group_decay, // Decay rate (per year) for growth_plus_length == 4
148 matrix<Type>& lengths,
149 const vector<int>& nlengths_pop,
150 matrix<Type>& lengths_pop, // Population length bins, lower edges (cm)
151 array<Type>& growth_parameters,
152 array<Type>& growth_log_sd,
153 matrix<Type>& weight_length_pars,
154 const vector<int>& mat_len_use, // 1 = maturity-at-length for this species
155 matrix<Type>& mat_len_pars, // [sp, 0] = L50 (cm), [sp, 1] = logistic slope (per cm)
156 array<Type>& log_M1, // Base natural mortality at age [nspp, nsex, nages], log scale
157 array<Type> &length_hat, // Modified by reference
158 array<Type> &growth_matrix, // Modified by reference
159 array<Type> &weight_hat, // Modified by reference
160 array<Type> &mat_weight_hat // Modified by reference (maturity-at-length species only)
161) {
162
163 Type b_len, last_linear, current_age;
164
165 // The linear ramp below age_L1 starts from the lowest population length edge (cm) at
166 // age 0 (SS3's len_bins(1)); with no population grid that is the lowest data bin.
167 Type Lmin_sp = lengths_pop(sp, 0);
168 // Anchor age, where mean length is `l1` (SS3's Growth_Age_for_L1). fit_mod() resolves
169 // an unset value to max(0.5, minage[sp]): an SS3-style half-year anchor at minage = 0.
170 Type age_L1 = growth_age_L1(sp);
171
172 // An anchor at age 0 would divide by zero in the ramp slope b_len, so the slope and the
173 // cohort-boundary test below both floor it at 1 year.
174 Type age_L1_safe = age_L1;
175 if (age_L1 <= Type(0)) age_L1_safe = Type(1);
176
177 for(int sex = 0; sex < nsex(sp); sex++) {
178 for(int yr = 0; yr < nyrs; yr++) {
179 for(int age = 0; age < nages(sp); age++) {
180
181 // This sex and year: K (per year), L1 and Linf (cm), Richards m.
182 Type kappa = growth_parameters(sp, sex, yr, 0);
183 Type l1 = growth_parameters(sp, sex, yr, 1);
184 Type linf = growth_parameters(sp, sex, yr, 2);
185 Type m = growth_parameters(sp, sex, yr, 3);
186 // Age (years) at the start of the year: 0-based slot `age` holds the
187 // (age + minage) cohort. This is the SLOT age, not the anchor age_L1 above, and
188 // adding 1 rather than minage reads the wrong age at any minage != 1.
189 current_age = Type(age) + Type(minage(sp));
190
191 // 1. Calculate Mean Length at Age ---
192 switch(growth_model(sp)) {
193 case 1: // Von Bertalanffy
194
195 // Slope from Lmin to L1
196 b_len = (l1 - Lmin_sp) / age_L1_safe; // age_L1_safe == age_L1 unless the anchor is <= 0
197
198 // Linear ramp below the anchor age
199 if((current_age) <= age_L1){
200 length_hat(wtind, sex, age, yr) = Lmin_sp + b_len * (current_age);
201 } else {
202 if(yr == 0) {
203 length_hat(wtind, sex, age, yr) = linf + (l1 - linf) * exp(-kappa * (current_age - age_L1));
204 } else { // Yr > 0
205 // The recursion below reads the same cohort one year younger, so it is
206 // valid only once that slot is on the curve. Where it is still on the ramp
207 // -- or at slot 0, which has no predecessor -- read off the anchor instead.
208 if(current_age - Type(1) < age_L1_safe || age == 0) {
209 last_linear = Lmin_sp + b_len * age_L1_safe;
210 length_hat(wtind, sex, age, yr) = last_linear + (last_linear - linf) * (exp(-kappa * (current_age - age_L1)) - 1.0);
211 } else {
212 // Lag 1-year parameters
213 Type lagkappa = growth_parameters(sp, sex, yr - 1, 0);
214 Type laglinf = growth_parameters(sp, sex, yr - 1, 2);
215 length_hat(wtind, sex, age, yr) = length_hat(wtind, sex, age - 1, yr - 1) + (length_hat(wtind, sex, age - 1, yr - 1) - laglinf) * (exp(-lagkappa) - 1.0);
216 }
217 }
218 }
219 break;
220
221 case 2: // Richards
222
223 // Slope from Lmin to L1
224 b_len = (l1 - Lmin_sp) / age_L1_safe;
225
226 // Linear ramp below the anchor age
227 if((current_age) <= age_L1) {
228 length_hat(wtind, sex, age, yr) = Lmin_sp + b_len * (current_age);
229 } else {
230 if(yr == 0) {
231 length_hat(wtind, sex, age, yr) = pow(pow(linf, m) + (pow(l1, m) - pow(linf, m)) * exp(-kappa * (current_age - age_L1)), 1 / m);
232 } else { // Yr > 0
233 // Same cohort-boundary test as the VBGF branch above.
234 if(current_age - Type(1) < age_L1_safe || age == 0) {
235 last_linear = Lmin_sp + b_len * age_L1_safe;
236 length_hat(wtind, sex, age, yr) = pow(pow(last_linear, m) + (pow(last_linear, m) - pow(linf, m)) * (exp(-kappa * (current_age - age_L1)) - 1.0), 1 / m);
237 } else {
238 // Lag 1-year parameters
239 Type lagkappa = growth_parameters(sp, sex, yr - 1, 0);
240 Type laglinf = growth_parameters(sp, sex, yr - 1, 2);
241 Type lagm = growth_parameters(sp, sex, yr - 1, 3);
242 length_hat(wtind, sex, age, yr) = pow(pow(length_hat(wtind, sex, age - 1, yr - 1), lagm) + (pow(length_hat(wtind, sex, age - 1, yr - 1), lagm) - pow(laglinf, lagm)) * (exp(-lagkappa) - 1.0), 1 / lagm);
243 }
244 }
245 }
246 break;
247
248 case 3: // Non-parametric (Free parameters)
249 error("Non-parametric growth not yet implemented");
250 break;
251
252 default:
253 error("Invalid 'growth_model");
254 } // Growth_model switch
255
256
257 // 2. Plus-Group Mean Length (Oldest Age Only) ---
258 // The plus group holds fish past the oldest age, so its mean length (cm) lies
259 // between L(oldest age) and Linf. growth_plus_length: 1 = weighted by survival at
260 // the oldest age's M1, 2/3/4 = SS3's Linf_decay -998/-999/d (?build_growth).
261 if(growth_model(sp) < 3 && age == (nages(sp) - 1)) {
262 Type current_size = length_hat(wtind, sex, age, yr);
263 Type diff = linf - current_size;
264 int oldest_age = minage(sp) + nages(sp) - 1;
265 if(growth_plus_length(sp) == 1) {
266 Type temp_n = 0, temp_sum = 0, weight_a = 1.0;
267 Type surv = exp(-exp(log_M1(sp, sex, nages(sp) - 1)));
268 for(int a = 0; a <= nages(sp); a++) {
269 temp_sum += weight_a * (current_size + (Type(a) / Type(nages(sp))) * diff);
270 temp_n += weight_a;
271 weight_a *= surv;
272 }
273 length_hat(wtind, sex, age, yr) = temp_sum / temp_n;
274 } else if(growth_plus_length(sp) == 3) {
275 Type temp_n = 0, temp_sum = 0;
276 // Survival weights at a fixed 0.2/yr, the nominal mortality SS3 uses for
277 // Linf_decay = -999 -- not this species' M1, which is what form 1 above uses.
278 for(int a = 0; a <= oldest_age; a++) {
279 Type weight_a = exp(Type(-0.2) * Type(a));
280 temp_sum += weight_a * (current_size + (Type(a) / Type(oldest_age)) * diff);
281 temp_n += weight_a;
282 }
283 length_hat(wtind, sex, age, yr) = temp_sum / temp_n;
284 } else if(growth_plus_length(sp) == 4) {
285 Type size = current_size, temp_sum = current_size, temp_n = 1.0, weight_a = 1.0;
286 // Each further age is one more year along the von Bertalanffy curve, for
287 // either growth family, weighted by exp(-plus_group_decay) per year. Averaged
288 // over 2 x the oldest age, SS3's span, by when the weights are negligible.
289 for(int a = 1; a <= 2 * oldest_age; a++) {
290 weight_a *= exp(-plus_group_decay(sp));
291 size += (linf - size) * (Type(1.0) - exp(-kappa));
292 temp_sum += weight_a * size;
293 temp_n += weight_a;
294 }
295 length_hat(wtind, sex, age, yr) = temp_sum / temp_n;
296 }
297 }
298
299 // 3. SD of length-at-age, then age-length key and weight-at-age ---
300 if(growth_model(sp) < 3) {
301 Type len = length_hat(wtind, sex, age, yr);
302 Type sd = length_sd_at_age(current_age, age_L1, age == (nages(sp) - 1),
303 growth_sd_style(sp), growth_sd_form(sp), l1, linf, len,
304 growth_log_sd(sp, sex, 0), growth_log_sd(sp, sex, 1));
305 fill_age_length_key(wtind, sp, sex, age, yr, len, sd, nlengths_pop,
306 lengths_pop, weight_length_pars,
307 mat_len_use, mat_len_pars, growth_matrix, weight_hat, mat_weight_hat);
308 }
309 } // age
310 } // yr
311 } // sex
312}
313
314
315
316/**
317 * @brief Mean length-at-age, age-length key and weight-at-age `fracyr` into the year.
318 *
319 * Advances the Jan-1 mean length stored at `id_pop` by `fracyr` of growth, for SSB
320 * weight-at-age at the spawning month and each fleet's weight-at-age at its own month:
321 * - von Bertalanffy (`growth_model` 1):
322 * \f$L_a = L_{\infty} + (L_{a, jan1} - L_{\infty}) \cdot e^{-K \cdot fracyr}\f$
323 * - Richards (2):
324 * \f$L_a = [L_{\infty}^m + (L_{a, jan1}^m - L_{\infty}^m) \cdot e^{-K \cdot fracyr}]^{1/m}\f$
325 * - Below the anchor age (`current_age <= age_L1`) the linear ramp applies, as at Jan-1.
326 *
327 * Adapted from Giancarlo Correa's pred_LAA() in WHAM's growth branch (timjmiller/wham at
328 * 16770d4, GPL-3).
329 *
330 * Under `growth_plus_length == 1` the plus group keeps the Jan-1 mean length
331 * `estimate_growth()` set for it; under the SS3 forms (2-4) it grows within the year like
332 * every other age, as SS3 does.
333 *
334 * Weight-at-age (kg) is integrated over the length distribution as in estimate_growth().
335 *
336 * @param wtind Weight index slot to write into.
337 * @param id_pop Weight index holding the Jan-1 lengths the advance starts from.
338 * @param sp Species index.
339 * @param fracyr Fraction of the year (0.0 - 1.0) to advance growth by.
340 * @param growth_parameters,growth_log_sd,weight_length_pars As in estimate_growth().
341 * @param length_hat,growth_matrix,weight_hat [Output] As in estimate_growth().
342 */
343template<class Type>
345 int wtind,
346 int id_pop,
347 int sp,
348 Type fracyr,
349 int nspp,
350 int nyrs,
351 const vector<int>& nsex,
352 const vector<int>& nages,
353 const vector<int>& nlengths,
354 const vector<int>& minage,
355 const vector<Type>& growth_age_L1,
356 const vector<int>& growth_model,
357 const vector<int>& growth_sd_style, // Plus-group SD-at-age: 1 = WHAM pin, 2 = SS3 by length
358 const vector<int>& growth_sd_form, // 1 = SD endpoints in cm, 2 = CV endpoints (SD = CV * L)
359 const vector<int>& growth_plus_length,// 1 = plus group held at Jan-1 length; 2-4 = it grows
360 matrix<Type>& lengths,
361 const vector<int>& nlengths_pop,
362 matrix<Type>& lengths_pop, // Population length bins, lower edges (cm)
363 array<Type>& growth_parameters,
364 array<Type>& growth_log_sd,
365 matrix<Type>& weight_length_pars,
366 const vector<int>& mat_len_use, // 1 = maturity-at-length for this species
367 matrix<Type>& mat_len_pars, // [sp, 0] = L50 (cm), [sp, 1] = logistic slope (per cm)
368 array<Type> &length_hat, // Modified by reference
369 array<Type> &growth_matrix, // Modified by reference
370 array<Type> &weight_hat, // Modified by reference
371 array<Type> &mat_weight_hat // Modified by reference (maturity-at-length species only)
372) {
373
374 Type b_len, current_age, last_linear;
375
376 // Ramp origin (lowest population length edge, cm, SS3's len_bins(1)) and anchor age
377 // (SS3's Growth_Age_for_L1), both as in estimate_growth().
378 Type Lmin_sp = lengths_pop(sp, 0);
379 Type age_L1 = growth_age_L1(sp);
380
381 // Floors the ramp slope's denominator at 1 year when the anchor is at age 0. This
382 // routine advances each slot from its own Jan-1 length, so unlike estimate_growth() it
383 // has no cohort-boundary test to floor.
384 Type age_L1_safe = age_L1;
385 if (age_L1 <= Type(0)) age_L1_safe = Type(1);
386
387 for(int sex = 0; sex < nsex(sp); sex++) {
388 for(int yr = 0; yr < nyrs; yr++) {
389 for(int age = 0; age < nages(sp); age++) {
390
391 // This sex and year: K (per year), L1 and Linf (cm), Richards m.
392 Type kappa = growth_parameters(sp, sex, yr, 0);
393 Type l1 = growth_parameters(sp, sex, yr, 1);
394 Type linf = growth_parameters(sp, sex, yr, 2);
395 Type m = growth_parameters(sp, sex, yr, 3);
396 // As in estimate_growth(): the slot age, not the anchor age_L1, offset by fracyr.
397 // Adding 1 rather than minage reads the wrong age at any minage != 1.
398 current_age = Type(age) + Type(minage(sp)) + fracyr;
399
400 // 1. Calculate Mean Length at Age ---
401 switch(growth_model(sp)) {
402 case 1: // Von Bertalanffy
403
404 // Slope from Lmin to L1
405 b_len = (l1 - Lmin_sp) / age_L1_safe; // age_L1_safe == age_L1 unless the anchor is <= 0
406
407 // Linear ramp below the anchor age
408 if((current_age) <= age_L1){
409 length_hat(wtind, sex, age, yr) = Lmin_sp + b_len * (current_age);
410 // Plus group PINNED at the Jan-1 length estimate_growth() wrote for it, so this
411 // override comes BEFORE the anchor test below, which also matches it when the
412 // whole age range sits under the anchor and would discard that pinned length.
413 // `age + 1.0 == nages(sp)` is a slot ordinal (0-based `age`, `nages` a count).
414 }else if(age + 1.0 == nages(sp) && growth_plus_length(sp) == 1) { // Pinned at Jan-1
415 length_hat(wtind, sex, age, yr) = length_hat(id_pop, sex, age, yr);
416 // The cohort that CROSSED the anchor during the year -- Jan-1 age below age_L1,
417 // mid-year age above it -- is read off the growth curve from the anchor, not
418 // advanced from its ramp length. `last_linear` is identically `l1` here, so the
419 // body is linf + (l1 - linf) * exp(-kappa * (current_age - age_L1)). The test
420 // is the JAN-1 age: `current_age` carries fracyr and the first branch took it.
421 }else if(age + Type(minage(sp)) < age_L1){ // Linear + growth curve mixed
422 last_linear = Lmin_sp + b_len * age_L1;
423 length_hat(wtind, sex, age, yr) = last_linear + (last_linear - linf) * (exp(-kappa * (current_age - age_L1)) - 1.0);
424 }else { // Growth curve
425 length_hat(wtind, sex, age, yr) = length_hat(id_pop, sex, age, yr) + (length_hat(id_pop, sex, age, yr) - linf) * (exp(-kappa * fracyr) - 1.0); // Add fracyr growth
426 }
427 break;
428
429 case 2: // Richards
430
431 // Slope from Lmin to L1
432 b_len = (l1 - Lmin_sp) / age_L1_safe;
433
434 // Same branch order as the VBGF case above, with the Richards curve, so the
435 // plus group is treated identically whichever growth family a species uses.
436 // Linear ramp below the anchor age
437 if((current_age) <= age_L1){
438 length_hat(wtind, sex, age, yr) = Lmin_sp + b_len * (current_age);
439 // Plus group PINNED at its Jan-1 length, ordered before the anchor test for the
440 // reason given in the VBGF branch above.
441 }else if(age + 1.0 == nages(sp) && growth_plus_length(sp) == 1) { // Pinned at Jan-1
442 length_hat(wtind, sex, age, yr) = length_hat(id_pop, sex, age, yr);
443 // As in the VBGF branch, but read off the Richards curve: `last_linear` is
444 // identically `l1` here, so the body is
445 // [linf^m + (l1^m - linf^m) * exp(-kappa * (current_age - age_L1))]^(1/m).
446 }else if(age + Type(minage(sp)) < age_L1){ // Linear + growth curve mixed
447 last_linear = Lmin_sp + b_len * age_L1;
448 length_hat(wtind, sex, age, yr) = pow(pow(last_linear, m) + (pow(last_linear, m) - pow(linf, m)) * (exp(-kappa * (current_age - age_L1)) - 1.0), 1 / m);
449 } else {
450 length_hat(wtind, sex, age, yr) = pow(pow(length_hat(id_pop, sex, age, yr), m) + (pow(length_hat(id_pop, sex, age, yr), m) - pow(linf, m)) * (exp(-kappa * fracyr) - 1.0), 1 / m); // Add fracyr growth
451 }
452 break;
453
454 case 3: // Non-parametric (Free parameters)
455 error("Non-parametric growth not yet implemented");
456 break;
457
458 default:
459 error("Invalid 'growth_model");
460 } // Growth_model switch
461
462 // 2. SD of length-at-age, then age-length key and weight-at-age ---
463 if(growth_model(sp) < 3) {
464 Type len = length_hat(wtind, sex, age, yr);
465 Type sd = length_sd_at_age(current_age, age_L1, age == (nages(sp) - 1),
466 growth_sd_style(sp), growth_sd_form(sp), l1, linf, len,
467 growth_log_sd(sp, sex, 0), growth_log_sd(sp, sex, 1));
468 fill_age_length_key(wtind, sp, sex, age, yr, len, sd, nlengths_pop,
469 lengths_pop, weight_length_pars,
470 mat_len_use, mat_len_pars, growth_matrix, weight_hat, mat_weight_hat);
471 }
472 } // age
473 } // yr
474 } // sex
475}
476
477
478/**
479 * @brief Population, SSB and fleet weight-at-age (kg) for every species and fleet.
480 *
481 * Empirical weight-at-age (`growth_model == 0`) is read straight from `weight_obs`, each
482 * projection year carrying the last hindcast year forward. Under estimated growth
483 * (`growth_model > 0`) population weight comes from estimate_growth() at Jan-1, and SSB
484 * and fleet weight from estimate_growth_within_yr() at the spawning month and at each
485 * fleet's own month. Arguments not listed below pass through to those two, except n_flt,
486 * which this function's own fleet loop consumes.
487 *
488 * @param weight_hat [ref] weight-at-age (wt_index, sex, age, year), kg
489 * @param length_hat [ref] mean length-at-age (cm)
490 * @param growth_matrix [ref] age-length key on the population length bins
491 * @param mat_weight_hat [ref] mature weight-at-age (kg), maturity-at-length species only
492 * @param weight_obs Empirical weight-at-age input (wt_index, sex, age, year), kg
493 * @param growth_model Per species, 0 = empirical, 1 = von Bertalanffy, 2 = Richards
494 * @param nyrs Years, hindcast plus projection.
495 * @param nyrs_hind Years in the hindcast.
496 * @param flt_spp Species index of each fleet
497 * @param flt_month Fleet observation month (0 = not specified); flt_month/12 of a year
498 * @param pop_wt_index Weight-at-age input index for population biomass
499 * @param ssb_wt_index Weight-at-age input index for spawning-stock biomass
500 * @param flt_wt_index Weight-at-age input index per fleet
501 * @param spawn_month Spawning month per species; spawn_month/12 of a year (0 = Jan-1)
502 */
503template <class Type>
505 array<Type> &weight_hat, // Modified by reference
506 array<Type> &length_hat, // Modified by reference
507 array<Type> &growth_matrix,// Modified by reference
508 array<Type> &mat_weight_hat,// Modified by reference; maturity-at-length species only
509 array<Type>& weight_obs,
510 const vector<int>& growth_model,
511 const vector<int>& growth_sd_style,
512 const vector<int>& growth_sd_form,
513 const vector<int>& growth_plus_length,
514 const vector<Type>& plus_group_decay,
515 const int& nspp,
516 const int& nyrs,
517 const int& nyrs_hind,
518 const int& n_flt,
519 const vector<int>& flt_spp,
520 vector<Type> flt_month,
521 const vector<int>& nsex,
522 const vector<int>& minage,
523 const vector<Type>& growth_age_L1,
524 const vector<int>& nages,
525 const vector<int>& nlengths,
526 const vector<int>& pop_wt_index,
527 const vector<int>& ssb_wt_index,
528 const vector<int>& flt_wt_index,
529 vector<Type> spawn_month,
530 matrix<Type>& lengths,
531 const vector<int>& nlengths_pop,
532 matrix<Type>& lengths_pop,
533 array<Type>& growth_parameters,
534 array<Type>& growth_log_sd,
535 matrix<Type> weight_length_pars,
536 const vector<int>& mat_len_use,
537 matrix<Type>& mat_len_pars,
538 array<Type>& log_M1
539) {
540 int yr_ind;
541 int wt_idx_pop;
542 int wt_idx_ssb;
543 int wt_idx_flt;
544
545 // 1. POPULATION WEIGHT-AT-AGE
546 for (int sp = 0; sp < nspp; sp++) {
547
548 wt_idx_pop = 2 * sp;
549 wt_idx_ssb = 2 * sp + 1;
550
551 // -- 1.1. Empirical weight-at-age
552 if (growth_model(sp) == 0) {
553 for (int sex = 0; sex < nsex(sp); sex++) {
554 for (int age = 0; age < nages(sp); age++) {
555 for (int yr = 0; yr < nyrs; yr++) {
556
557 // Projection years reuse the last hindcast year's empirical weight-at-age.
558 yr_ind = (yr < nyrs_hind) ? yr : (nyrs_hind - 1);
559
560 // Biomass weight
561 weight_hat(wt_idx_pop, sex, age, yr) = weight_obs(pop_wt_index(sp), sex, age, yr_ind);
562
563 // SSB weight
564 weight_hat(wt_idx_ssb, sex, age, yr) = weight_obs(ssb_wt_index(sp), sex, age, yr_ind);
565 }
566 }
567 }
568 }
569
570 // -- 1.2. Estimated growth
571 if (growth_model(sp) > 0) {
572 // Biomass weight (beginning of year / month 0)
574 wt_idx_pop,
575 sp,
576 nspp,
577 nyrs,
578 nsex,
579 nages,
580 nlengths,
581 minage,
582 growth_age_L1,
583 growth_model,
584 growth_sd_style,
585 growth_sd_form,
586 growth_plus_length,
587 plus_group_decay,
588 lengths,
589 nlengths_pop,
590 lengths_pop,
591 growth_parameters,
592 growth_log_sd,
593 weight_length_pars,
594 mat_len_use,
595 mat_len_pars,
596 log_M1,
597 length_hat,
598 growth_matrix,
599 weight_hat,
600 mat_weight_hat
601 );
602
603 // SSB weight (at month of spawning)
605 wt_idx_ssb,
606 wt_idx_pop,
607 sp,
608 spawn_month(sp) / Type(12.0),
609 nspp,
610 nyrs,
611 nsex,
612 nages,
613 nlengths,
614 minage,
615 growth_age_L1,
616 growth_model,
617 growth_sd_style,
618 growth_sd_form,
619 growth_plus_length,
620 lengths,
621 nlengths_pop,
622 lengths_pop,
623 growth_parameters,
624 growth_log_sd,
625 weight_length_pars,
626 mat_len_use,
627 mat_len_pars,
628 length_hat,
629 growth_matrix,
630 weight_hat,
631 mat_weight_hat
632 );
633 }
634 }
635
636 // 2. FLEET WEIGHT-AT-AGE
637 for (int flt = 0; flt < n_flt; flt++) {
638 int sp = flt_spp(flt);
639 Type mo = flt_month(flt);
640 wt_idx_pop = 2 * sp;
641 wt_idx_flt = nspp * 2 + flt;
642
643 // -- 2.1. Empirical weight-at-age
644 if (growth_model(sp) == 0) {
645 for (int sex = 0; sex < nsex(sp); sex++) {
646 for (int age = 0; age < nages(sp); age++) {
647 for (int yr = 0; yr < nyrs; yr++) {
648
649 yr_ind = (yr < nyrs_hind) ? yr : (nyrs_hind - 1);
650 weight_hat(wt_idx_flt, sex, age, yr) = weight_obs(flt_wt_index(flt), sex, age, yr_ind);
651 }
652 }
653 }
654 }
655
656 // -- 2.2. Estimated growth
657 if (growth_model(sp) > 0) {
658
660 wt_idx_flt,
661 wt_idx_pop,
662 sp,
663 mo / Type(12.0),
664 nspp,
665 nyrs,
666 nsex,
667 nages,
668 nlengths,
669 minage,
670 growth_age_L1,
671 growth_model,
672 growth_sd_style,
673 growth_sd_form,
674 growth_plus_length,
675 lengths,
676 nlengths_pop,
677 lengths_pop,
678 growth_parameters,
679 growth_log_sd,
680 weight_length_pars,
681 mat_len_use,
682 mat_len_pars,
683 length_hat,
684 growth_matrix,
685 weight_hat,
686 mat_weight_hat
687 );
688 }
689 }
690}
691
692
693
694#endif
695
Type length_sd_at_age(Type current_age, Type age_L1, bool plus_group, int sd_style, int sd_form, Type l1, Type linf, Type len, Type log_sd0, Type log_sd1)
SD of length-at-age (cm) for one age, year and sex.
Definition growth.hpp:14
void calculate_weight(array< Type > &weight_hat, array< Type > &length_hat, array< Type > &growth_matrix, array< Type > &mat_weight_hat, array< Type > &weight_obs, const vector< int > &growth_model, const vector< int > &growth_sd_style, const vector< int > &growth_sd_form, const vector< int > &growth_plus_length, const vector< Type > &plus_group_decay, const int &nspp, const int &nyrs, const int &nyrs_hind, const int &n_flt, const vector< int > &flt_spp, vector< Type > flt_month, const vector< int > &nsex, const vector< int > &minage, const vector< Type > &growth_age_L1, const vector< int > &nages, const vector< int > &nlengths, const vector< int > &pop_wt_index, const vector< int > &ssb_wt_index, const vector< int > &flt_wt_index, vector< Type > spawn_month, matrix< Type > &lengths, const vector< int > &nlengths_pop, matrix< Type > &lengths_pop, array< Type > &growth_parameters, array< Type > &growth_log_sd, matrix< Type > weight_length_pars, const vector< int > &mat_len_use, matrix< Type > &mat_len_pars, array< Type > &log_M1)
Population, SSB and fleet weight-at-age (kg) for every species and fleet.
Definition growth.hpp:504
void estimate_growth(int wtind, int sp, int nspp, int nyrs, const vector< int > &nsex, const vector< int > &nages, const vector< int > &nlengths, const vector< int > &minage, const vector< Type > &growth_age_L1, const vector< int > &growth_model, const vector< int > &growth_sd_style, const vector< int > &growth_sd_form, const vector< int > &growth_plus_length, const vector< Type > &plus_group_decay, matrix< Type > &lengths, const vector< int > &nlengths_pop, matrix< Type > &lengths_pop, array< Type > &growth_parameters, array< Type > &growth_log_sd, matrix< Type > &weight_length_pars, const vector< int > &mat_len_use, matrix< Type > &mat_len_pars, array< Type > &log_M1, array< Type > &length_hat, array< Type > &growth_matrix, array< Type > &weight_hat, array< Type > &mat_weight_hat)
Jan-1 (month 0) mean length, age-length key and weight-at-age for one species.
Definition growth.hpp:132
void fill_age_length_key(int wtind, int sp, int sex, int age, int yr, Type mu, Type sd, const vector< int > &nlengths_pop, matrix< Type > &lengths_pop, matrix< Type > &weight_length_pars, const vector< int > &mat_len_use, matrix< Type > &mat_len_pars, array< Type > &growth_matrix, array< Type > &weight_hat, array< Type > &mat_weight_hat)
Age-length key, weight-at-age and mature weight-at-age for one cell.
Definition growth.hpp:45
void estimate_growth_within_yr(int wtind, int id_pop, int sp, Type fracyr, int nspp, int nyrs, const vector< int > &nsex, const vector< int > &nages, const vector< int > &nlengths, const vector< int > &minage, const vector< Type > &growth_age_L1, const vector< int > &growth_model, const vector< int > &growth_sd_style, const vector< int > &growth_sd_form, const vector< int > &growth_plus_length, matrix< Type > &lengths, const vector< int > &nlengths_pop, matrix< Type > &lengths_pop, array< Type > &growth_parameters, array< Type > &growth_log_sd, matrix< Type > &weight_length_pars, const vector< int > &mat_len_use, matrix< Type > &mat_len_pars, array< Type > &length_hat, array< Type > &growth_matrix, array< Type > &weight_hat, array< Type > &mat_weight_hat)
Mean length-at-age, age-length key and weight-at-age fracyr into the year.
Definition growth.hpp:344