78 array<Type> &suit_other,
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) {
90 for(
int rsp = 0; rsp < nspp; rsp++) {
91 int smode = suitMode(rsp);
92 int wt_idx_rsp = 2 * rsp;
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);
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;
103 for(
int k_sex = 0; k_sex < nsex(ksp); k_sex++) {
104 int k_idx = ksp + (nspp * k_sex);
106 for(
int k_age = 0; k_age < nages(ksp); k_age++) {
107 for(
int yr = 0; yr < nyrs; yr++) {
109 suit_other(rsp, r_sex, r_age, yr) = vulnerability_other(rsp);
110 Type size_ratio = 0.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);
125 if(smode == 1 || smode == 2) {
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) {
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));
181 array<Type> &suitability,
182 array<Type> &suit_other,
184 const vector<int> &suit_styr,
const vector<int> &suit_endyr,
const vector<int> &nyrs_suit,
186 const vector<int> &nsex,
187 const vector<int> &nages,
188 const vector<int> &suitMode,
189 array<Type> &diet_prop,
190 array<Type> &avgN_at_age,
191 array<Type> &weight_hat,
192 array<Type> &other_food_diet_prop) {
196 int max_sex =
imax(nsex);
197 int max_age =
imax(nages);
198 array<Type> suma_suit(nspp, max_sex, max_age, nyrs);
201 array<Type> diet_prop_sum(nspp, max_sex, max_age, nyrs);
202 diet_prop_sum.setZero();
204 stom_div_bio.setZero();
207 for(
int rsp = 0; rsp < nspp; rsp++) {
209 if(suitMode(rsp) == 0) {
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);
218 for(
int k_age = 0; k_age < nages(ksp); k_age++) {
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; }
226 Type prey_denom = prey_wt * prey_N;
230 if (!(prey_denom <= Type(0.0))) {
231 Type inv_prey_denom = Type(1.0) / prey_denom;
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++) {
237 Type d_prop = diet_prop(r_idx, k_idx, r_age, k_age, yr);
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;
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++) {
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);
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;
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));
272 inv_pred_denom(yr) = Type(0.0);
276 Type total_suit_prey = Type(0.0);
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++) {
283 Type suit_sum_yrs = Type(0.0);
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);
289 Type final_suit = suit_sum_yrs / Type(nyrs_suit(rsp));
290 total_suit_prey += final_suit;
292 for(
int yr = 0; yr < nyrs; yr++) {
293 suitability(r_idx, k_idx, r_age, k_age, yr) = final_suit;
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;
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,
369 const vector<int> &nsex,
370 const vector<int> &nages,
372 array<Type> &avgN_at_age,
373 array<Type> &avgN_at_age_dB0,
374 array<Type> &avgN_at_age_dBF,
375 array<Type> &suitability,
376 array<Type> &suit_other,
377 array<Type> &weight_hat,
379 const vector<Type> &other_food,
380 bool fill_report_only,
382 bool fill_dynamic_B0) {
386 avail_food.setZero();
387 avail_food_dB0.setZero();
388 avail_food_dBF.setZero();
390 M2_at_age_dB0.setZero();
391 M2_at_age_dBF.setZero();
394 B_eaten_as_prey.setZero();
395 diet_prop_hat.setZero();
396 other_diet_prop_hat.setZero();
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);
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++) {
408 cell_sex(cell) = sex;
409 cell_age(cell) = age;
410 cell_sp_sex(cell) = sp + (nspp * sex);
418 vector<Type> avail(ncell), avail_dB0(ncell), avail_dBF(ncell);
419 vector<Type> prey_N_cell(ncell), prey_wt_cell(ncell);
420 vector<Type> M2(ncell), M2_dB0(ncell), M2_dBF(ncell), eaten_as_prey(ncell);
427 for(
int yr = 0; yr < nyrs; yr++) {
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);
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);
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);
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);
445 Type prey_wt = weight_hat(2 * ksp, k_sex, k_age, yr);
447 Type bio_avail = prey_N_adj * prey_wt;
448 Type bio_avail_dB0 = prey_N_adj_dB0 * prey_wt;
449 Type bio_avail_dBF = prey_N_adj_dBF * prey_wt;
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;
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;
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);
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);
486 Type avail_r = avail_food(rsp, r_sex, r_age, yr);
490 if(!(avail_r <= Type(0.0))) {
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;
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;
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;
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;
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);
517 Type M2_frac = pred_mort_scalar * suit;
518 M2(k_cell) += M2_frac;
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;
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;
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;
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);
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).