Rceattle C++ template
The CEATTLE likelihood: src/TMB/ceattle.cpp and its headers
Loading...
Searching...
No Matches
predation.hpp
Go to the documentation of this file.
1#ifndef PREDATION_HPP
2#define PREDATION_HPP
3
4
5/**
6 * @brief Transforms predator-prey preference parameters into vulnerability.
7 *
8 * Multinomial logistic transform: each predator's prey preferences and its vulnerability
9 * to "other food" sum to 1. Parametric suitability only (suitMode > 0). Adopted from
10 * Trijoulet et al. 2020
11 * (https://github.com/vtrijoulet/Multisp_model_JAE/blob/master/MS_SSM.cpp).
12 *
13 * @param vulnerability Bounded predator-prey vulnerability [nspp, nspp], updated.
14 * @param vulnerability_other Remaining vulnerability for "other food" [nspp], updated.
15 * @param nspp Number of species.
16 * @param suitMode Suitability/preference mode per species.
17 * @param log_phi Estimated log-preference parameters [nspp, nspp].
18 */
19template <class Type>
20void calculate_vulnerability(matrix<Type> &vulnerability,
21 vector<Type> &vulnerability_other,
22 int nspp,
23 const vector<int> &suitMode,
24 matrix<Type> &log_phi) {
25
26 vulnerability.setZero();
27 vulnerability_other.setZero();
28
29 for(int rsp = 0; rsp < nspp; rsp++) {
30 if(suitMode(rsp) > 0) {
31
32 Type sum_phi = 0;
33
34 for(int ksp = 0; ksp < nspp; ksp++) {
35 sum_phi += exp(log_phi(rsp, ksp));
36 }
37
38 for(int ksp = 0; ksp < nspp; ksp++) {
39 vulnerability(rsp, ksp) = exp(log_phi(rsp, ksp)) / (Type(1.0) + sum_phi);
40 }
41
42 vulnerability_other(rsp) = Type(1.0) - vulnerability.row(rsp).sum();
43 }
44 }
45}
46
47/**
48 * @brief Parametric suitability (Gamma, Lognormal, Normal) from size ratios.
49 *
50 * Evaluates the predator-to-prey size ratio against a density scaled to a peak of 1.
51 * Adapted from MS_SSM.cpp of Trijoulet et al. (2020), vtrijoulet/Multisp_model_JAE.
52 * `suitMode` sets both the density and the size metric:
53 * - Gamma: suitMode 1 (length-based) or 2 (weight-based)
54 * - Lognormal: suitMode 3 (length-based) or 4 (weight-based)
55 * - Normal: suitMode 5 (length-based) or 6 (weight-based)
56 * The length-based modes 1, 3 and 5 are NOT YET AVAILABLE: they need an active growth
57 * model, and data_check() blocks them until that validation is added.
58 *
59 * @param suitability Size-based suitability, updated
60 * [nspp*max_sex, nspp*max_sex, max_age, max_age, nyrs].
61 * @param suit_other Baseline vulnerability to "other food", updated;
62 * [nspp, max_sex, max_age, nyrs] -- species NOT collapsed,
63 * unlike suitability.
64 * @param nspp Number of species.
65 * @param nyrs Years, hindcast plus projection.
66 * @param nsex Sexes modelled per species: 1 = combined, 2 = both.
67 * @param nages Age bins per species.
68 * @param suitMode Parametric suitability mode per predator species.
69 * @param length_hat Lengths-at-age [nspp*2+n_flt, max_sex, max_age, nyrs].
70 * @param weight_hat Weights-at-age [nspp*2+n_flt, max_sex, max_age, nyrs].
71 * @param vulnerability Bounded predator-prey vulnerability [nspp, nspp].
72 * @param vulnerability_other Vulnerability preference for "other food" [nspp].
73 * @param gam_a Shape/mean parameter for predator size-selectivity [nspp].
74 * @param gam_b Scale/sd parameter for predator size-selectivity [nspp].
75 */
76template <class Type>
77void calculate_parametric_suitability(array<Type> &suitability,
78 array<Type> &suit_other,
79 int nspp, int nyrs,
80 const vector<int> &nsex,
81 const vector<int> &nages,
82 const vector<int> &suitMode,
83 array<Type> &length_hat,
84 array<Type> &weight_hat,
85 matrix<Type> &vulnerability,
86 const vector<Type> &vulnerability_other,
87 const vector<Type> &gam_a,
88 const vector<Type> &gam_b) {
89
90 for(int rsp = 0; rsp < nspp; rsp++) {
91 int smode = suitMode(rsp);
92 int wt_idx_rsp = 2 * rsp;
93
94 // Parametric suitability modes (1 through 6)
95 if(smode >= 1 && smode <= 6) {
96 for(int r_sex = 0; r_sex < nsex(rsp); r_sex++) {
97 int r_idx = rsp + (nspp * r_sex);
98
99 for(int r_age = 0; r_age < nages(rsp); r_age++) {
100 for(int ksp = 0; ksp < nspp; ksp++) {
101 int wt_idx_ksp = 2 * ksp;
102
103 for(int k_sex = 0; k_sex < nsex(ksp); k_sex++) {
104 int k_idx = ksp + (nspp * k_sex);
105
106 for(int k_age = 0; k_age < nages(ksp); k_age++) {
107 for(int yr = 0; yr < nyrs; yr++) {
108
109 suit_other(rsp, r_sex, r_age, yr) = vulnerability_other(rsp);
110 Type size_ratio = 0.0;
111
112 // Row 2 * species (wt_idx_*) holds population length (cm) and weight (kg).
113 // Modes 1-4 take the log size ratio, 5-6 the raw one; a ratio <= 0 leaves suitability 0.
114 if(smode == 1 || smode == 3) {
115 size_ratio = log(length_hat(wt_idx_rsp, r_sex, r_age, yr) / length_hat(wt_idx_ksp, k_sex, k_age, yr));
116 } else if (smode == 2 || smode == 4) {
117 size_ratio = log(weight_hat(wt_idx_rsp, r_sex, r_age, yr) / weight_hat(wt_idx_ksp, k_sex, k_age, yr));
118 } else if (smode == 5) {
119 size_ratio = length_hat(wt_idx_rsp, r_sex, r_age, yr) / length_hat(wt_idx_ksp, k_sex, k_age, yr);
120 } else if (smode == 6) {
121 size_ratio = weight_hat(wt_idx_rsp, r_sex, r_age, yr) / weight_hat(wt_idx_ksp, k_sex, k_age, yr);
122 }
123
124 if(size_ratio > 0) {
125 if(smode == 1 || smode == 2) {
126 // Divided by the density at the mode, (gam_a-1)*gam_b.
127 suitability(r_idx, k_idx, r_age, k_age, yr) = vulnerability(rsp, ksp) * dgamma(size_ratio, gam_a(rsp), gam_b(rsp)) /
128 dgamma((gam_a(rsp)-1) * gam_b(rsp), gam_a(rsp), gam_b(rsp));
129 } else if (smode >= 3 && smode <= 6) {
130 // Divided by the density at the mean, gam_a.
131 suitability(r_idx, k_idx, r_age, k_age, yr) = vulnerability(rsp, ksp) * dnorm(size_ratio, gam_a(rsp), gam_b(rsp)) /
132 dnorm(gam_a(rsp), gam_a(rsp), gam_b(rsp));
133 }
134 }
135 }
136 }
137 }
138 }
139 }
140 }
141 }
142 }
143}
144
145
146/**
147 * @brief MSVPA/empirical suitability, from stomach content and prey biomass.
148 *
149 * For predators with suitMode = 0. stom_div_bio is the observed diet proportion divided
150 * by prey biomass (numbers in thousands x weight in kg = mt; the prey term is squared
151 * under msmMode = 2, Type III). That ratio is normalized over the modeled prey and "other
152 * food", averaged over suit_styr..suit_endyr, and held constant across all years.
153 *
154 * A prey age whose average numbers approach zero with a positive diet proportion sends
155 * stom_div_bio, and so the prey term, to infinity: that one cell takes the whole
156 * normalized share, "other food" vanishes, available food collapses with it, so M2
157 * diverges. The other-food term is floored at a fixed fraction of the prey term, bounding
158 * suit_other below at floor/(1 + floor), leaving sum(suitability) + suit_other = 1 exact.
159 * .warn_suitability_floor() reports when it binds.
160 *
161 * @param stom_div_bio Ratio of diet proportion to biomass, updated;
162 * [nspp*max_sex, nspp*max_sex, max_age, max_age, nyrs].
163 * @param suitability Final calculated suitability, updated.
164 * @param suit_other Remaining suitability for "other food", updated.
165 * @param nspp Number of species.
166 * @param nyrs Years, hindcast plus projection.
167 * @param suit_styr Start year index of the suitability window, per predator.
168 * @param suit_endyr End year index of the suitability window, per predator.
169 * @param nyrs_suit Years in the suitability window, per predator.
170 * @param msmMode Predation mode; 2 (Type III) squares the prey numbers.
171 * @param nsex Sexes modelled per species: 1 = combined, 2 = both.
172 * @param nages Age bins per species.
173 * @param suitMode Suitability mode per predator species.
174 * @param diet_prop Observed diet proportions from organize_diet_obs().
175 * @param avgN_at_age Average numbers-at-age (thousands of fish).
176 * @param weight_hat Estimated weights-at-age (kg).
177 * @param other_food_diet_prop Residual diet proportion for "other food", per mt of it.
178 */
179template <class Type>
180void calculate_msvpa_suitability(array<Type> &stom_div_bio,
181 array<Type> &suitability,
182 array<Type> &suit_other,
183 int nspp, int nyrs,
184 const vector<int> &suit_styr, const vector<int> &suit_endyr, const vector<int> &nyrs_suit,
185 int msmMode,
186 const vector<int> &nsex,
187 const vector<int> &nages,
188 const vector<int> &suitMode,
189 array<Type> &diet_prop, // const
190 array<Type> &avgN_at_age,// const
191 array<Type> &weight_hat,// const
192 array<Type> &other_food_diet_prop) {// const
193
194 // Working sums per predator species/sex/age/year: stom_div_bio over prey,
195 // and diet proportion over prey.
196 int max_sex = imax(nsex);
197 int max_age = imax(nages);
198 array<Type> suma_suit(nspp, max_sex, max_age, nyrs);
199 suma_suit.setZero();
200
201 array<Type> diet_prop_sum(nspp, max_sex, max_age, nyrs);
202 diet_prop_sum.setZero();
203
204 stom_div_bio.setZero();
205
206
207 for(int rsp = 0; rsp < nspp; rsp++) {
208
209 if(suitMode(rsp) == 0) { // MSVPA based suitability
210
211 // Pass 1: diet proportion divided by prey biomass, and its per-predator-age sums.
212 for(int yr = 0; yr < nyrs; yr++) {
213 for(int ksp = 0; ksp < nspp; ksp++) {
214 int wt_idx_ksp = 2 * ksp;
215 for(int k_sex = 0; k_sex < nsex(ksp); k_sex++) {
216 int k_idx = ksp + (nspp * k_sex);
217
218 for(int k_age = 0; k_age < nages(ksp); k_age++) {
219
220 // prey_N is thousands of fish and prey_wt kg, so the product is mt.
221 // Type III MSVPA (msmMode 2) squares prey_N: prey_denom is not a biomass.
222 Type prey_wt = weight_hat(wt_idx_ksp, k_sex, k_age, yr);
223 Type prey_N = avgN_at_age(ksp, k_sex, k_age, yr);
224 if (msmMode == 2) { prey_N *= prey_N; }
225
226 Type prey_denom = prey_wt * prey_N;
227
228 // The tape fixes this branch when TMB records it. NaN passes the test, so
229 // it reaches suitability rather than dropping this prey age from the fit.
230 if (!(prey_denom <= Type(0.0))) {
231 Type inv_prey_denom = Type(1.0) / prey_denom;
232
233 for(int r_sex = 0; r_sex < nsex(rsp); r_sex++) {
234 int r_idx = rsp + (nspp * r_sex);
235 for(int r_age = 0; r_age < nages(rsp); r_age++) {
236
237 Type d_prop = diet_prop(r_idx, k_idx, r_age, k_age, yr);
238
239 if (d_prop > Type(0.0)) {
240 Type sdb = d_prop * inv_prey_denom;
241 stom_div_bio(r_idx, k_idx, r_age, k_age, yr) = sdb;
242 suma_suit(rsp, r_sex, r_age, yr) += sdb;
243 diet_prop_sum(rsp, r_sex, r_age, yr) += d_prop;
244 }
245 }
246 }
247 }
248 }
249 }
250 }
251 }
252
253 // Pass 2: normalize over the modeled prey plus "other food", then average the
254 // standardized ratio over the suitability window.
255 for(int r_sex = 0; r_sex < nsex(rsp); r_sex++) {
256 int r_idx = rsp + (nspp * r_sex);
257 for(int r_age = 0; r_age < nages(rsp); r_age++) {
258
259 vector<Type> inv_pred_denom(nyrs);
260 for(int yr = suit_styr(rsp); yr <= suit_endyr(rsp); yr++) {
261 Type prey_term = suma_suit(rsp, r_sex, r_age, yr);
262 // Other food is held at >= 1e-6 x the modelled-prey term, a unitless
263 // ratio, so suit_other cannot reach 0 when a prey age's biomass does.
264 Type other_term = max2(other_food_diet_prop(rsp, r_sex, r_age, yr),
265 Type(1.0e-6) * prey_term);
266 Type denom = prey_term + other_term;
267 // `diet_prop_sum` gates whether this predator age has diet data; `denom` is
268 // what we divide by and can reach 0 or go negative on its own, so floor it.
269 if (diet_prop_sum(rsp, r_sex, r_age, yr) > Type(0.0)) {
270 inv_pred_denom(yr) = Type(1.0) / max2(denom, Type(1.0e-10));
271 } else {
272 inv_pred_denom(yr) = Type(0.0);
273 }
274 }
275
276 Type total_suit_prey = Type(0.0); // Total suitability assigned to modeled prey
277
278 for(int ksp = 0; ksp < nspp; ksp++) {
279 for(int k_sex = 0; k_sex < nsex(ksp); k_sex++) {
280 int k_idx = ksp + (nspp * k_sex);
281 for(int k_age = 0; k_age < nages(ksp); k_age++) {
282
283 Type suit_sum_yrs = Type(0.0);
284
285 for(int yr = suit_styr(rsp); yr <= suit_endyr(rsp); yr++) {
286 suit_sum_yrs += stom_div_bio(r_idx, k_idx, r_age, k_age, yr) * inv_pred_denom(yr);
287 }
288
289 Type final_suit = suit_sum_yrs / Type(nyrs_suit(rsp));
290 total_suit_prey += final_suit;
291
292 for(int yr = 0; yr < nyrs; yr++) {
293 suitability(r_idx, k_idx, r_age, k_age, yr) = final_suit;
294 }
295 }
296 }
297 }
298
299 // "Other food" takes whatever suitability the modeled prey leave.
300 Type final_suit_other = Type(1.0) - total_suit_prey;
301 for(int yr = 0; yr < nyrs; yr++) {
302 suit_other(rsp, r_sex, r_age, yr) = final_suit_other;
303 }
304 }
305 }
306 }
307 }
308}
309
310
311/**
312 * @brief Available food, predation mortality (M2), and consumed biomass (MSVPA).
313 *
314 * Available food is suitability-weighted prey biomass plus "other food": prey numbers
315 * (thousands of fish) x weight (kg) = mt, and other_food is mt. Under msmMode = 2
316 * (Type III) the prey term is squared, so the denominator is not a biomass there. M2
317 * divides each predator's consumption (avgN x ration) by that denominator, so a LARGER
318 * available-food denominator gives LESS predation mortality. The dB0 (F = 0) and dBF
319 * (F = Ftarget) arrays repeat the calculation for the dynamic reference points.
320 * Outputs the objective reads only in some configurations are filled only when their fill_
321 * flag is set.
322 *
323 * @param avail_food Available food (mt) [nspp, max_sex, max_age, nyrs], updated.
324 * @param avail_food_dB0 Available food at F=0, updated.
325 * @param avail_food_dBF Available food at F=Ftarget, updated.
326 * @param M2_at_age Predation mortality (yr^-1), updated;
327 * [nspp, max_sex, max_age, nyrs].
328 * @param M2_at_age_dB0 Predation mortality at F=0, updated.
329 * @param M2_at_age_dBF Predation mortality at F=Ftarget, updated.
330 * @param M2_prop A predator age's contribution to prey M2, not a share of it.
331 * @param B_eaten Prey biomass eaten by a specific predator age (mt), updated.
332 * @param B_eaten_as_prey Prey biomass eaten by all predators (mt), updated.
333 * @param diet_prop_hat Predicted diet proportions, updated.
334 * @param other_diet_prop_hat Predicted proportion of "other food" in diet, updated.
335 * @param nspp Number of species.
336 * @param nyrs Years, hindcast plus projection.
337 * @param nsex Sexes modelled per species: 1 = combined, 2 = both.
338 * @param nages Age bins per species.
339 * @param msmMode Predation mode (1 = Type II MSVPA, Holsman et al. 2016;
340 * 2 = Type III MSVPA, which squares the prey term).
341 * @param avgN_at_age Average numbers-at-age (thousands of fish).
342 * @param avgN_at_age_dB0 Average numbers-at-age at F=0.
343 * @param avgN_at_age_dBF Average numbers-at-age at F=Ftarget.
344 * @param suitability Calculated suitability.
345 * @param suit_other Suitability for "other food".
346 * @param weight_hat Estimated weights-at-age (kg).
347 * @param ration Individual consumption (kg/predator/yr); consumption_at_age.
348 * @param other_food "Other food" biomass by predator (mt).
349 * @param fill_report_only Fill M2_prop, B_eaten and B_eaten_as_prey (REPORT()ed) and
350 * other_diet_prop_hat (unread); otherwise they stay 0.
351 * @param fill_diet Fill diet_prop_hat, which section 11 turns into the predicted
352 * stomach content, diet_hat; otherwise it stays 0.
353 * @param fill_dynamic_B0 Repeat the prey sums at F = 0 and F = Ftarget; otherwise M2_at_age_dB0
354 * and _dBF stay 0 and avail_food_dB0 and _dBF hold only other food.
355 */
356template <class Type>
357void calculate_msvpa_predation(array<Type> &avail_food,
358 array<Type> &avail_food_dB0,
359 array<Type> &avail_food_dBF,
360 array<Type> &M2_at_age,
361 array<Type> &M2_at_age_dB0,
362 array<Type> &M2_at_age_dBF,
363 array<Type> &M2_prop,
364 array<Type> &B_eaten,
365 array<Type> &B_eaten_as_prey,
366 array<Type> &diet_prop_hat,
367 array<Type> &other_diet_prop_hat,
368 int nspp, int nyrs,
369 const vector<int> &nsex,
370 const vector<int> &nages,
371 int msmMode,
372 array<Type> &avgN_at_age, // const
373 array<Type> &avgN_at_age_dB0, // const
374 array<Type> &avgN_at_age_dBF, // const
375 array<Type> &suitability, // const
376 array<Type> &suit_other, // const
377 array<Type> &weight_hat, // const
378 array<Type> &ration,
379 const vector<Type> &other_food,
380 bool fill_report_only,
381 bool fill_diet,
382 bool fill_dynamic_B0) {
383
384 // Zero all outputs on every pass this runs: pair outputs and other_diet_prop_hat
385 // are written only where available food > 0 (or NaN) and their fill_ flag is set.
386 avail_food.setZero();
387 avail_food_dB0.setZero();
388 avail_food_dBF.setZero();
389 M2_at_age.setZero();
390 M2_at_age_dB0.setZero();
391 M2_at_age_dBF.setZero();
392 M2_prop.setZero();
393 B_eaten.setZero();
394 B_eaten_as_prey.setZero();
395 diet_prop_hat.setZero();
396 other_diet_prop_hat.setZero();
397
398 // Predators and prey are the same (species, sex, age) cells, listed species, then sex,
399 // then age: the order every sum below adds in.
400 int ncell = 0;
401 for(int sp = 0; sp < nspp; sp++) ncell += nsex(sp) * nages(sp);
402 vector<int> cell_sp(ncell), cell_sex(ncell), cell_age(ncell), cell_sp_sex(ncell);
403 int cell = 0;
404 for(int sp = 0; sp < nspp; sp++) {
405 for(int sex = 0; sex < nsex(sp); sex++) {
406 for(int age = 0; age < nages(sp); age++) {
407 cell_sp(cell) = sp;
408 cell_sex(cell) = sex;
409 cell_age(cell) = age;
410 cell_sp_sex(cell) = sp + (nspp * sex); // indexes suitability and the pair outputs
411 cell++;
412 }
413 }
414 }
415
416 // Sums are kept in locals and prey quantities copied out once a year, so the pair loops
417 // make few array(i, j, ...) calls: each one allocates an index vector.
418 vector<Type> avail(ncell), avail_dB0(ncell), avail_dBF(ncell);
419 vector<Type> prey_N_cell(ncell), prey_wt_cell(ncell); // thousands of fish, kg
420 vector<Type> M2(ncell), M2_dB0(ncell), M2_dBF(ncell), eaten_as_prey(ncell);
421
422 // Pass 1 loops prey outside predators and pass 2 the reverse, as two separate loops over
423 // years: any other order changes the gradient's last bits, which can move a fit.
424
425 // 1. Available food: suitability-weighted prey biomass (mt), then "other food" (mt),
426 // prey outside the model, added once per predator age and year.
427 for(int yr = 0; yr < nyrs; yr++) {
428 avail.setZero();
429 avail_dB0.setZero();
430 avail_dBF.setZero();
431 for(int k_cell = 0; k_cell < ncell; k_cell++) {
432 int ksp = cell_sp(k_cell), k_sex = cell_sex(k_cell), k_age = cell_age(k_cell);
433 int k_idx = cell_sp_sex(k_cell);
434
435 // msmMode 2 (Type III) squares the prey numbers; "other food" is never squared.
436 Type prey_N = avgN_at_age(ksp, k_sex, k_age, yr);
437 Type prey_N_adj = (msmMode == 1) ? prey_N : (prey_N * prey_N);
438
439 Type prey_N_dB0 = avgN_at_age_dB0(ksp, k_sex, k_age, yr);
440 Type prey_N_adj_dB0 = (msmMode == 1) ? prey_N_dB0 : (prey_N_dB0 * prey_N_dB0);
441
442 Type prey_N_dBF = avgN_at_age_dBF(ksp, k_sex, k_age, yr);
443 Type prey_N_adj_dBF = (msmMode == 1) ? prey_N_dBF : (prey_N_dBF * prey_N_dBF);
444
445 Type prey_wt = weight_hat(2 * ksp, k_sex, k_age, yr);
446
447 Type bio_avail = prey_N_adj * prey_wt;
448 Type bio_avail_dB0 = prey_N_adj_dB0 * prey_wt; // dynamic B0, F = 0
449 Type bio_avail_dBF = prey_N_adj_dBF * prey_wt; // dynamic BF, F = Ftarget
450
451 for(int r_cell = 0; r_cell < ncell; r_cell++) {
452 Type suit = suitability(cell_sp_sex(r_cell), k_idx, cell_age(r_cell), k_age, yr);
453 avail(r_cell) += suit * bio_avail;
454 if(fill_dynamic_B0) {
455 avail_dB0(r_cell) += suit * bio_avail_dB0;
456 avail_dBF(r_cell) += suit * bio_avail_dBF;
457 }
458 }
459 }
460
461 for(int r_cell = 0; r_cell < ncell; r_cell++) {
462 int rsp = cell_sp(r_cell), r_sex = cell_sex(r_cell), r_age = cell_age(r_cell);
463 Type other_f = other_food(rsp) * suit_other(rsp, r_sex, r_age, yr);
464 avail_food(rsp, r_sex, r_age, yr) = avail(r_cell) + other_f;
465 avail_food_dB0(rsp, r_sex, r_age, yr) = avail_dB0(r_cell) + other_f;
466 avail_food_dBF(rsp, r_sex, r_age, yr) = avail_dBF(r_cell) + other_f;
467 }
468 }
469
470 // 2. Predation mortality (M2) and the prey biomass eaten.
471 for(int yr = 0; yr < nyrs; yr++) {
472 for(int k_cell = 0; k_cell < ncell; k_cell++) {
473 int ksp = cell_sp(k_cell), k_sex = cell_sex(k_cell), k_age = cell_age(k_cell);
474 prey_N_cell(k_cell) = avgN_at_age(ksp, k_sex, k_age, yr);
475 prey_wt_cell(k_cell) = weight_hat(2 * ksp, k_sex, k_age, yr);
476 }
477
478 M2.setZero();
479 M2_dB0.setZero();
480 M2_dBF.setZero();
481 eaten_as_prey.setZero();
482 for(int r_cell = 0; r_cell < ncell; r_cell++) {
483 int rsp = cell_sp(r_cell), r_sex = cell_sex(r_cell), r_age = cell_age(r_cell);
484 int r_idx = cell_sp_sex(r_cell);
485
486 Type avail_r = avail_food(rsp, r_sex, r_age, yr);
487
488 // The tape fixes these branches when TMB records it. NaN available food passes
489 // them, so it reaches M2 rather than dropping this predator age.
490 if(!(avail_r <= Type(0.0))) {
491 // avgN (thousands of fish) x ration (kg/predator/yr) is mt consumed, over
492 // available food in mt.
493 Type inv_avail = Type(1.0) / avail_r;
494 Type pred_rat = ration(rsp, r_sex, r_age, yr);
495 Type pred_mort_scalar = avgN_at_age(rsp, r_sex, r_age, yr) * pred_rat * inv_avail;
496
497 Type pred_mort_scalar_dB0 = Type(0.0);
498 Type avail_r_dB0 = avail_food_dB0(rsp, r_sex, r_age, yr);
499 if(!(avail_r_dB0 <= Type(0.0))) {
500 pred_mort_scalar_dB0 = (avgN_at_age_dB0(rsp, r_sex, r_age, yr) * pred_rat) / avail_r_dB0;
501 }
502
503 Type pred_mort_scalar_dBF = Type(0.0);
504 Type avail_r_dBF = avail_food_dBF(rsp, r_sex, r_age, yr);
505 if(!(avail_r_dBF <= Type(0.0))) {
506 pred_mort_scalar_dBF = (avgN_at_age_dBF(rsp, r_sex, r_age, yr) * pred_rat) / avail_r_dBF;
507 }
508
509 if(fill_report_only) {
510 other_diet_prop_hat(rsp, r_sex, r_age, yr) = other_food(rsp) * suit_other(rsp, r_sex, r_age, yr) * inv_avail;
511 }
512
513 for(int k_cell = 0; k_cell < ncell; k_cell++) {
514 int k_idx = cell_sp_sex(k_cell), k_age = cell_age(k_cell);
515 Type suit = suitability(r_idx, k_idx, r_age, k_age, yr);
516
517 Type M2_frac = pred_mort_scalar * suit;
518 M2(k_cell) += M2_frac;
519
520 if(fill_report_only) {
521 M2_prop(r_idx, k_idx, r_age, k_age, yr) = M2_frac;
522 Type eaten = prey_N_cell(k_cell) * prey_wt_cell(k_cell) * M2_frac;
523 B_eaten(r_idx, k_idx, r_age, k_age, yr) = eaten;
524 eaten_as_prey(k_cell) += eaten;
525 }
526
527 // Prey biomass (prey_N * prey_wt, mt) stays per predator-prey pair: computed once per
528 // prey it changes the diet gradient's last bits (suitMode > 0), which can move a fit.
529 if(fill_diet) {
530 diet_prop_hat(r_idx, k_idx, r_age, k_age, yr) = prey_N_cell(k_cell) * prey_wt_cell(k_cell) * suit * inv_avail;
531 }
532
533 if(fill_dynamic_B0) {
534 M2_dB0(k_cell) += pred_mort_scalar_dB0 * suit;
535 M2_dBF(k_cell) += pred_mort_scalar_dBF * suit;
536 }
537 }
538 }
539 }
540
541 for(int k_cell = 0; k_cell < ncell; k_cell++) {
542 int ksp = cell_sp(k_cell), k_sex = cell_sex(k_cell), k_age = cell_age(k_cell);
543 M2_at_age(ksp, k_sex, k_age, yr) = M2(k_cell);
544 M2_at_age_dB0(ksp, k_sex, k_age, yr) = M2_dB0(k_cell);
545 M2_at_age_dBF(ksp, k_sex, k_age, yr) = M2_dBF(k_cell);
546 B_eaten_as_prey(ksp, k_sex, k_age, yr) = eaten_as_prey(k_cell);
547 }
548 }
549}
550
551
552
553#endif
Type imax(const vector< Type > &x)
Type max2(Type x, Type y)
void calculate_msvpa_predation(array< Type > &avail_food, array< Type > &avail_food_dB0, array< Type > &avail_food_dBF, array< Type > &M2_at_age, array< Type > &M2_at_age_dB0, array< Type > &M2_at_age_dBF, array< Type > &M2_prop, array< Type > &B_eaten, array< Type > &B_eaten_as_prey, array< Type > &diet_prop_hat, array< Type > &other_diet_prop_hat, int nspp, int nyrs, const vector< int > &nsex, const vector< int > &nages, int msmMode, array< Type > &avgN_at_age, array< Type > &avgN_at_age_dB0, array< Type > &avgN_at_age_dBF, array< Type > &suitability, array< Type > &suit_other, array< Type > &weight_hat, array< Type > &ration, const vector< Type > &other_food, bool fill_report_only, bool fill_diet, bool fill_dynamic_B0)
Available food, predation mortality (M2), and consumed biomass (MSVPA).
void calculate_parametric_suitability(array< Type > &suitability, array< Type > &suit_other, int nspp, int nyrs, const vector< int > &nsex, const vector< int > &nages, const vector< int > &suitMode, array< Type > &length_hat, array< Type > &weight_hat, matrix< Type > &vulnerability, const vector< Type > &vulnerability_other, const vector< Type > &gam_a, const vector< Type > &gam_b)
Parametric suitability (Gamma, Lognormal, Normal) from size ratios.
Definition predation.hpp:77
void calculate_vulnerability(matrix< Type > &vulnerability, vector< Type > &vulnerability_other, int nspp, const vector< int > &suitMode, matrix< Type > &log_phi)
Transforms predator-prey preference parameters into vulnerability.
Definition predation.hpp:20
void calculate_msvpa_suitability(array< Type > &stom_div_bio, array< Type > &suitability, array< Type > &suit_other, int nspp, int nyrs, const vector< int > &suit_styr, const vector< int > &suit_endyr, const vector< int > &nyrs_suit, int msmMode, const vector< int > &nsex, const vector< int > &nages, const vector< int > &suitMode, array< Type > &diet_prop, array< Type > &avgN_at_age, array< Type > &weight_hat, array< Type > &other_food_diet_prop)
MSVPA/empirical suitability, from stomach content and prey biomass.