77 array<Type> &suit_other,
79 const vector<int> &nsex,
80 const vector<int> &nages,
81 const vector<int> &suitMode,
82 array<Type> &length_hat,
83 array<Type> &weight_hat,
84 matrix<Type> &vulnerability,
85 const vector<Type> &vulnerability_other,
86 const vector<Type> &gam_a,
87 const vector<Type> &gam_b) {
89 for(
int rsp = 0; rsp < nspp; rsp++) {
90 int smode = suitMode(rsp);
91 int wt_idx_rsp = 2 * rsp;
94 if(smode >= 1 && smode <= 6) {
95 for(
int r_sex = 0; r_sex < nsex(rsp); r_sex++) {
96 int r_idx = rsp + (nspp * r_sex);
98 for(
int r_age = 0; r_age < nages(rsp); r_age++) {
99 for(
int ksp = 0; ksp < nspp; ksp++) {
100 int wt_idx_ksp = 2 * ksp;
102 for(
int k_sex = 0; k_sex < nsex(ksp); k_sex++) {
103 int k_idx = ksp + (nspp * k_sex);
105 for(
int k_age = 0; k_age < nages(ksp); k_age++) {
106 for(
int yr = 0; yr < nyrs; yr++) {
108 suit_other(rsp, r_sex, r_age, yr) = vulnerability_other(rsp);
109 Type size_ratio = 0.0;
114 if(smode == 1 || smode == 3) {
115 size_ratio = log(length_hat(rsp, r_sex, r_age, yr) / length_hat(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(rsp, r_sex, r_age, yr) / length_hat(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;
228 if (prey_denom > Type(0.0)) {
229 Type inv_prey_denom = Type(1.0) / prey_denom;
231 for(
int r_sex = 0; r_sex < nsex(rsp); r_sex++) {
232 int r_idx = rsp + (nspp * r_sex);
233 for(
int r_age = 0; r_age < nages(rsp); r_age++) {
235 suitability(r_idx, k_idx, r_age, k_age, yr) = 0.0;
236 Type d_prop = diet_prop(r_idx, k_idx, r_age, k_age, yr);
238 if (d_prop > Type(0.0)) {
239 Type sdb = d_prop * inv_prey_denom;
240 stom_div_bio(r_idx, k_idx, r_age, k_age, yr) = sdb;
241 suma_suit(rsp, r_sex, r_age, yr) += sdb;
242 diet_prop_sum(rsp, r_sex, r_age, yr) += d_prop;
254 for(
int r_sex = 0; r_sex < nsex(rsp); r_sex++) {
255 int r_idx = rsp + (nspp * r_sex);
256 for(
int r_age = 0; r_age < nages(rsp); r_age++) {
258 vector<Type> inv_pred_denom(nyrs);
259 for(
int yr = suit_styr(rsp); yr <= suit_endyr(rsp); yr++) {
260 Type prey_term = suma_suit(rsp, r_sex, r_age, yr);
263 Type other_term =
max2(other_food_diet_prop(rsp, r_sex, r_age, yr),
264 Type(1.0e-6) * prey_term);
265 Type denom = prey_term + other_term;
268 if (diet_prop_sum(rsp, r_sex, r_age, yr) > Type(0.0)) {
269 inv_pred_denom(yr) = Type(1.0) /
max2(denom, Type(1.0e-10));
271 inv_pred_denom(yr) = Type(0.0);
275 Type total_suit_prey = Type(0.0);
277 for(
int ksp = 0; ksp < nspp; ksp++) {
278 for(
int k_sex = 0; k_sex < nsex(ksp); k_sex++) {
279 int k_idx = ksp + (nspp * k_sex);
280 for(
int k_age = 0; k_age < nages(ksp); k_age++) {
282 Type suit_sum_yrs = Type(0.0);
284 for(
int yr = suit_styr(rsp); yr <= suit_endyr(rsp); yr++) {
285 suit_sum_yrs += stom_div_bio(r_idx, k_idx, r_age, k_age, yr) * inv_pred_denom(yr);
288 Type final_suit = suit_sum_yrs / Type(nyrs_suit(rsp));
289 total_suit_prey += final_suit;
291 for(
int yr = 0; yr < nyrs; yr++) {
292 suitability(r_idx, k_idx, r_age, k_age, yr) = final_suit;
299 Type final_suit_other = Type(1.0) - total_suit_prey;
300 for(
int yr = 0; yr < nyrs; yr++) {
301 suit_other(rsp, r_sex, r_age, yr) = final_suit_other;
350 array<Type> &avail_food_dB0,
351 array<Type> &avail_food_dBF,
352 array<Type> &M2_at_age,
353 array<Type> &M2_at_age_dB0,
354 array<Type> &M2_at_age_dBF,
355 array<Type> &M2_prop,
356 array<Type> &B_eaten,
357 array<Type> &B_eaten_as_prey,
358 array<Type> &diet_prop_hat,
359 array<Type> &other_diet_prop_hat,
361 const vector<int> &nsex,
362 const vector<int> &nages,
364 array<Type> &avgN_at_age,
365 array<Type> &avgN_at_age_dB0,
366 array<Type> &avgN_at_age_dBF,
367 array<Type> &suitability,
368 array<Type> &suit_other,
369 array<Type> &weight_hat,
371 const vector<Type> &other_food) {
375 avail_food.setZero();
376 avail_food_dB0.setZero();
377 avail_food_dBF.setZero();
379 M2_at_age_dB0.setZero();
380 M2_at_age_dBF.setZero();
383 B_eaten_as_prey.setZero();
384 diet_prop_hat.setZero();
385 other_diet_prop_hat.setZero();
388 for(
int yr = 0; yr < nyrs; yr++) {
389 for(
int ksp = 0; ksp < nspp; ksp++) {
390 int wt_idx_ksp = 2 * ksp;
391 for(
int k_sex = 0; k_sex < nsex(ksp); k_sex++) {
392 int k_idx = ksp + (nspp * k_sex);
393 for(
int k_age = 0; k_age < nages(ksp); k_age++) {
396 Type prey_N = avgN_at_age(ksp, k_sex, k_age, yr);
397 Type prey_N_adj = (msmMode == 1) ? prey_N : (prey_N * prey_N);
399 Type prey_N_dB0 = avgN_at_age_dB0(ksp, k_sex, k_age, yr);
400 Type prey_N_adj_dB0 = (msmMode == 1) ? prey_N_dB0 : (prey_N_dB0 * prey_N_dB0);
402 Type prey_N_dBF = avgN_at_age_dBF(ksp, k_sex, k_age, yr);
403 Type prey_N_adj_dBF = (msmMode == 1) ? prey_N_dBF : (prey_N_dBF * prey_N_dBF);
405 Type prey_wt = weight_hat(wt_idx_ksp, k_sex, k_age, yr);
407 Type bio_avail = prey_N_adj * prey_wt;
408 Type bio_avail_dB0 = prey_N_adj_dB0 * prey_wt;
409 Type bio_avail_dBF = prey_N_adj_dBF * prey_wt;
411 for(
int rsp = 0; rsp < nspp; rsp++) {
412 for(
int r_sex = 0; r_sex < nsex(rsp); r_sex++) {
413 int r_idx = rsp + (nspp * r_sex);
414 for(
int r_age = 0; r_age < nages(rsp); r_age++) {
415 Type suit = suitability(r_idx, k_idx, r_age, k_age, yr);
416 avail_food(rsp, r_sex, r_age, yr) += suit * bio_avail;
417 avail_food_dB0(rsp, r_sex, r_age, yr) += suit * bio_avail_dB0;
418 avail_food_dBF(rsp, r_sex, r_age, yr) += suit * bio_avail_dBF;
427 for(
int rsp = 0; rsp < nspp; rsp++) {
428 for(
int r_sex = 0; r_sex < nsex(rsp); r_sex++) {
429 for(
int r_age = 0; r_age < nages(rsp); r_age++) {
430 Type other_f = other_food(rsp) * suit_other(rsp, r_sex, r_age, yr);
431 avail_food(rsp, r_sex, r_age, yr) += other_f;
432 avail_food_dB0(rsp, r_sex, r_age, yr) += other_f;
433 avail_food_dBF(rsp, r_sex, r_age, yr) += other_f;
440 for(
int yr = 0; yr < nyrs; yr++) {
441 for(
int rsp = 0; rsp < nspp; rsp++) {
442 for(
int r_sex = 0; r_sex < nsex(rsp); r_sex++) {
443 int r_idx = rsp + (nspp * r_sex);
444 for(
int r_age = 0; r_age < nages(rsp); r_age++) {
446 Type avail = avail_food(rsp, r_sex, r_age, yr);
448 if(avail > Type(0.0)) {
451 Type inv_avail = Type(1.0) / avail;
452 Type pred_rat = ration(rsp, r_sex, r_age, yr);
453 Type pred_mort_scalar = avgN_at_age(rsp, r_sex, r_age, yr) * pred_rat * inv_avail;
455 Type pred_mort_scalar_dB0 = Type(0.0);
456 Type avail_dB0 = avail_food_dB0(rsp, r_sex, r_age, yr);
457 if(avail_dB0 > Type(0.0)) {
458 pred_mort_scalar_dB0 = (avgN_at_age_dB0(rsp, r_sex, r_age, yr) * pred_rat) / avail_dB0;
461 Type pred_mort_scalar_dBF = Type(0.0);
462 Type avail_dBF = avail_food_dBF(rsp, r_sex, r_age, yr);
463 if(avail_dBF > Type(0.0)) {
464 pred_mort_scalar_dBF = (avgN_at_age_dBF(rsp, r_sex, r_age, yr) * pred_rat) / avail_dBF;
467 other_diet_prop_hat(rsp, r_sex, r_age, yr) = other_food(rsp) * suit_other(rsp, r_sex, r_age, yr) * inv_avail;
469 for(
int ksp = 0; ksp < nspp; ksp++) {
470 int wt_idx_ksp = 2 * ksp;
471 for(
int k_sex = 0; k_sex < nsex(ksp); k_sex++) {
472 int k_idx = ksp + (nspp * k_sex);
473 for(
int k_age = 0; k_age < nages(ksp); k_age++) {
475 Type prey_N = avgN_at_age(ksp, k_sex, k_age, yr);
476 Type prey_wt = weight_hat(wt_idx_ksp, k_sex, k_age, yr);
477 Type suit = suitability(r_idx, k_idx, r_age, k_age, yr);
479 Type M2_frac = pred_mort_scalar * suit;
480 M2_at_age(ksp, k_sex, k_age, yr) += M2_frac;
481 M2_prop(r_idx, k_idx, r_age, k_age, yr) = M2_frac;
483 Type eaten = prey_N * prey_wt * M2_frac;
484 B_eaten(r_idx, k_idx, r_age, k_age, yr) = eaten;
485 B_eaten_as_prey(ksp, k_sex, k_age, yr) += eaten;
487 diet_prop_hat(r_idx, k_idx, r_age, k_age, yr) = prey_N * prey_wt * suit * inv_avail;
489 M2_at_age_dB0(ksp, k_sex, k_age, yr) += pred_mort_scalar_dB0 * suit;
490 M2_at_age_dBF(ksp, k_sex, k_age, yr) += pred_mort_scalar_dBF * suit;
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)
Available food, predation mortality (M2), and consumed biomass (MSVPA).