-
Notifications
You must be signed in to change notification settings - Fork 16
/
SS_popdyn.tpl
2252 lines (2124 loc) · 91.1 KB
/
SS_popdyn.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SS_Label_file #12. **SS_popdyn.tpl**
// SS_Label_file # * <u>setup_recdevs()</u>
// SS_Label_file # * <u>get_initial_conditions()</u> // does virgin and initial year by calling <u>Do_Equil_Calc()</u> with F=0, then F=init_F
// SS_Label_file # * <u>get_time_series()</u> // loops the years, calling biology, selectivity and spawn-recr functions as needed
// SS_Label_file # * <u>Do_Equil_Calc()</u> // does per-recruit calculations and returns SSB/R and Y/R
// SS_Label_file #
FUNCTION void setup_recdevs()
{
// SS_Label_Info_7.1 #Set up recruitment bias_adjustment vector
sigmaR = SR_parm(N_SRparm(SR_fxn) + 1);
two_sigmaRsq = 2.0 * sigmaR * sigmaR;
half_sigmaRsq = 0.5 * sigmaR * sigmaR;
biasadj.initialize();
if (SR_fxn == 4 || do_recdev == 0)
{
// keep all at 0.0 if not using SR fxn
}
// else if (mceval_phase() || initial_params::mc_phase==1 || recdev_adj(5)<0.0)
else if (mceval_phase() || initial_params::mc_phase == 1)
{
// biasadj=1.0;
biasadj = recdev_doit; // sets to 1.0 for the years or initial ages with estimated recruitments
}
else
{
if (recdev_do_early > 0 && recdev_options(2) >= 0) // do logic on basis of recdev_options(2), which is read, not recdev_PH which can be reset to a neg. value
{
for (i = recdev_early_start; i <= recdev_early_end; i++)
{
if (i >= styr - nages)
biasadj(i) = biasadj_full(i);
}
}
if (do_recdev > 0 && recdev_PH_rd >= 0)
{
for (i = recdev_start; i <= recdev_end; i++)
{
if (i >= styr - nages)
biasadj(i) = biasadj_full(i);
}
}
if (Do_Forecast > 0 && recdev_options(3) >= 0)
{
for (i = recdev_end + 1; i <= YrMax; i++)
{
biasadj(i) = biasadj_full(i);
}
}
if (recdev_read > 0)
{
for (j = 1; j <= recdev_read; j++)
{
y = recdev_input(j, 1);
if (y >= recdev_first && y <= YrMax)
biasadj(y) = biasadj_full(y);
}
}
}
sd_offset_rec = sum(biasadj) * sd_offset;
// SS_Label_Info_7.2 #Copy recdev parm vectors into full time series vector
if (recdev_do_early > 0)
{
recdev(recdev_early_start, recdev_early_end) = recdev_early(recdev_early_start, recdev_early_end);
}
if (do_recdev == 1)
{
recdev(recdev_start, recdev_end) = recdev1(recdev_start, recdev_end);
}
else if (do_recdev >= 2)
{
recdev(recdev_start, recdev_end) = recdev2(recdev_start, recdev_end);
}
if (Do_Forecast > 0 && do_recdev > 0)
recdev(recdev_end + 1, YrMax) = Fcast_recruitments(recdev_end + 1, YrMax); // only needed here for reporting
} // end setup for recdevs
FUNCTION void get_initial_conditions()
{
//*********************************************************************
/* SS_Label_Function_23 #get_initial_conditions */
natage.initialize();
catch_fleet.initialize();
annual_catch.initialize();
annual_F.initialize();
Recr.initialize();
save_gparm = 0; // index for saving time-varying changes to biology quantities
if (SzFreq_Nmeth > 0)
SzFreq_exp.initialize();
// SS_Label_Info_23.1 #call biology and selectivity functions for the initial year
// SS_Label_Info_23.1.1 #These rate are calculated once in PRELIMINARY_CALCS_SECTION, so only recalculate if active according to MG_active
y = styr;
yz = styr;
t_base = styr - 1;
recr_dist_unf.initialize();
// Create time varying parameters
// following call is to routine that does this for all timevary parameters
// that are then copied over to replace the base parameter for MG, SRR, Q, Selex, or Tag as needed
make_timevaryparm(); // this fills array parm_timevary for all years; densitydependence must be done year-by-year later
if (MG_active(0) > 0 || save_for_report > 0)
{
get_MGsetup(y);
}
#ifdef DO_ONCE
if (do_once == 1)
echoinput << " MGsetup OK " << endl;
#endif
if (MG_active(2) > 0)
get_growth1(); // seasonal effects and CV
#ifdef DO_ONCE
if (do_once == 1)
echoinput << " growth1 OK" << endl;
#endif
if (MG_active(2) > 0 || do_once == 1)
{
ALK_subseas_update = 1; // to indicate that all ALKs need calculation
get_growth2(y);
t = styr - 1;
for (s = 1; s <= nseas; s++)
{
t++;
for (subseas = 1; subseas <= N_subseas; subseas++) // do all subseasons in first year
{
get_growth3(y, t, s, subseas); // in case needed for Lorenzen M
Make_AgeLength_Key(s, subseas);
}
}
// SS_Label_Info_16.2.4.3 #propagate Ave_Size from early years forward until first year that has time-vary growth
k = styr + 1;
do
{
for (s = 1; s <= nseas; s++)
{
t = styr + (k - styr) * nseas + s - 1;
Ave_Size(t, 1) = Ave_Size(t - nseas, 1);
} // end season loop
k++;
} while (timevary_MG(k, 2) == 0 && k <= YrMax);
if (k <= YrMax)
{
t = styr + (k - styr) * nseas;
Ave_Size(t, 1) = Ave_Size(t - nseas, 1); // prep for time-vary next yr
}
}
if (MG_active(3) > 0)
get_wtlen(); // stores values for all seasons
get_mat_fec(); // does just spawn season and subseason using ALK calculated just above
if (Hermaphro_Option != 0)
get_Hermaphro();
if (do_once>0 || MG_active(1) > 0)
{
get_natmort(); // gets base M (e.g. M1) by season and stores it in natM(t,0). Later, pred_M2 is added by area
for (s = 1; s <= nseas; s++)
{
natM(t_base - 2 * nseas + s) = natM(t_base + s); // copy to virgin
natM(t_base - nseas + s) = natM(t_base + s); // then to init_conditions year
}
}
#ifdef DO_ONCE
if (do_once == 1)
echoinput << "natmort OK" << endl;
#endif
if (MG_active(4) > 0)
get_recr_distribution();
if (y >= Bmark_Yr(7) && y <= Bmark_Yr(8))
{
for (gp = 1; gp <= N_GP; gp++)
for (p = 1; p <= pop; p++)
for (settle = 1; settle <= N_settle_timings; settle++)
if (recr_dist_pattern(gp, settle, p) > 0)
{
recr_dist_unf(gp, settle, p) += recr_dist(y, gp, settle, p);
if (gender == 2)
recr_dist_unf(gp + N_GP, settle, p) += recr_dist(y, gp + N_GP, settle, p);
}
}
if (MG_active(5) > 0)
get_migration();
#ifdef DO_ONCE
if (do_once == 1)
{
echoinput << "migr OK" << endl;
}
#endif
if (MG_active(7) > 0)
{
get_catch_mult(y, catch_mult_pointer);
for (j = styr + 1; j <= YrMax; j++)
{
catch_mult(j) = catch_mult(y);
}
}
if (Use_AgeKeyZero > 0)
{
if (MG_active(6) > 0)
get_age_age(Use_AgeKeyZero, AgeKey_StartAge, AgeKey_Linear1, AgeKey_Linear2); // call function to get the age_age key
if (save_for_report == 1 && store_agekey_add > 0)
{
save_agekey_count = N_ageerr + 1; // first blank key after the used keys
age_age(save_agekey_count) = age_age(Use_AgeKeyZero);
age_err(save_agekey_count) = age_err(Use_AgeKeyZero);
}
#ifdef DO_ONCE
if (do_once == 1)
{
echoinput << "age_err key recalc in " << y << endl;
}
#endif
}
if (save_for_report > 0)
{
get_saveGparm();
}
// SS_Label_Info_23.2 #Calculate selectivity in the initial year
get_selectivity();
#ifdef DO_ONCE
if (do_once == 1)
{
echoinput << "selectivity OK" << endl;
echoinput << "Calculate ALK" << endl;
}
#endif
// SS_Label_Info_23.3 #Loop seasons and subseasons
t = styr - 1;
for (s = 1; s <= nseas; s++)
{
t++;
if (WTage_rd > 0)
{
for (g = 1; g <= gmorph; g++)
if (use_morph(g) > 0)
{
Wt_Age_beg(s, g) = Wt_Age_t(t, 0, g);
Wt_Age_mid(s, g) = Wt_Age_t(t, -1, g);
if (s == spawn_seas)
fec(g) = Wt_Age_t(t, -2, g);
}
}
else if (MG_active(2) > 0 || MG_active(3) > 0 || save_for_report > 0 || do_once == 1)
{
// Make_Fecundity();
if (s == spawn_seas && spawn_seas == 1)
get_mat_fec();
for (g = 1; g <= gmorph; g++)
if (use_morph(g) > 0)
{
subseas = 1;
ALK_idx = (s - 1) * N_subseas + subseas;
Wt_Age_beg(s, g) = (ALK(ALK_idx, g) * wt_len(s, GP(g))); // wt-at-age at beginning of period
subseas = mid_subseas;
ALK_idx = (s - 1) * N_subseas + subseas;
Wt_Age_mid(s, g) = ALK(ALK_idx, g) * wt_len(s, GP(g)); // use for fisheries with no size selectivity
}
}
Wt_Age_t(t, 0) = Wt_Age_beg(s);
for (g = 1; g <= gmorph; g++)
if (use_morph(g) > 0)
{
// SS_Label_Info_23.3.3 #for each platoon, combine size_at_age distribution with length selectivity and weight-at-length to get combined selectivity vectors
Make_FishSelex();
}
// SS_Label_Info_23.3.4 #add predator M2 to M1 to update seasonal, areal natM in styr and calc surv for use in Pope's
if(N_pred>0)
{
// rebase natM to M1, which is stored in the p=0 section of array
for(p = 1; p <= pop; p++)
{
natM(t, p) = natM(t,0);
}
// calc M2
for (f1 = 1; f1 <= N_pred; f1++)
{
f = predator(f1);
pred_M2(f1, t) = mgp_adj(predparm_pointer(f1)); // base with no seasonal effect
if (nseas > 1)
pred_M2(f1, t) *= mgp_adj(predparm_pointer(f1) + s);
pred_M2(f1, t-nseas) = pred_M2(f1, t);
pred_M2(f1, t-nseas-nseas) = pred_M2(f1, t);
p = fleet_area(f); // area this predator occurs in
// a new array for indexing g and gpi could simplify below
// for (gp = 1; gp <= N_GP * gender * N_settle_timings; gp++)
// add each M2 to get total M
for (gp = 1; gp <= N_GP * gender; gp++)
{
g = g_Start(gp); // base platoon
for (settle = 1; settle <= N_settle_timings; settle++)
{
g += N_platoon;
int gpi = GP3(g); // GP*gender*settlement
natM(t, p,gpi) += pred_M2(f1, t) * sel_num(s, f, g);
if (do_once == 1 && p == 1)
echoinput << "init " << y << " s " << s << " t " << t << " area " << 0 << " gp " << gpi << " M1: " << natM(t,0, gpi) << endl;
if (do_once == 1)
echoinput << "init " << y << " s " << s << " t " << t << " area " << p << " gp " << gpi << " M1+M2: " << natM(t, p, gpi) << endl;
}
}
}
natM(t-nseas) = natM(t); //for initial equilibrium
natM(t-nseas-nseas) = natM(t); // for virgin
}
for(p = 1; p <= pop; p++)
{
int s1 = (p - 1) * nseas + s;
surv1(s1) = mfexp( - natM(t,p) * seasdur_half(s));
surv2(s1) = square(surv1(s1));
}
} // end season (s) loop in biology, mortality and selectivity calcs in initial year
#ifdef DO_ONCE
if (do_once == 1)
echoinput << "Begin calculating virgin age struc " << endl;
#endif
// SS_Label_Info_23.4 #calculate unfished (virgin) numbers-at-age
eq_yr = styr - 2;
bio_yr = styr;
Fishon = 0;
virg_fec = fec;
Recr.initialize(); // will store recruitment by area
for (int i = 1; i <= N_SRparm2; i++)
{
SR_parm_byyr(eq_yr, i) = SR_parm(i);
SR_parm_virg(i) = SR_parm(i);
SR_parm_work(i) = SR_parm(i);
}
// SPAWN-RECR: get expected recruitment globally or by area
if (recr_dist_area == 1 || pop == 1) // do global spawn_recruitment calculations
{
Recr_virgin = mfexp(SR_parm(1));
equ_Recr = Recr_virgin;
exp_rec(eq_yr, 1) = Recr_virgin; // expected Recr from s-r parms
exp_rec(eq_yr, 2) = Recr_virgin;
exp_rec(eq_yr, 3) = Recr_virgin;
exp_rec(eq_yr, 4) = Recr_virgin;
Do_Equil_Calc(equ_Recr); // call function to do equilibrium calculation
SSB_virgin = SSB_equil;
SPR_virgin = SSB_equil / Recr_virgin; // spawners per recruit
if(Do_Benchmark==0)
{
Mgmt_quant(1)=SSB_virgin;
SSB_unf=SSB_virgin;
Recr_unf=Recr_virgin;
Mgmt_quant(2)=totbio; // from equil calcs
Mgmt_quant(3)=smrybio; // from equil calcs
Mgmt_quant(4)=Recr_virgin;
}
Smry_Table(styr - 2, 1) = totbio; // from equil calcs
Smry_Table(styr - 2, 2) = smrybio; // from equil calcs
Smry_Table(styr - 2, 3) = smrynum; // from equil calcs
SSB_pop_gp(eq_yr) = SSB_equil_pop_gp; // dimensions of pop x N_GP
if (Hermaphro_Option != 0)
MaleSPB(eq_yr) = MaleSSB_equil_pop_gp;
SSB_yr(eq_yr) = SSB_equil;
SR_parm_byyr(eq_yr, N_SRparm2 + 1) = SSB_equil;
SR_parm_virg(N_SRparm2 + 1) = SSB_equil;
SR_parm_work(N_SRparm2 + 1) = SSB_equil;
t = styr - 2 * nseas - 1;
for (s = 1; s <= nseas; s++)
for (p = 1; p <= pop; p++)
{
for (g = 1; g <= gmorph; g++)
{
if (use_morph(g) > 0)
{
natage(t + s, p, g)(0, nages) = equ_numbers(s, p, g)(0, nages);
Z_rate(t + s, p, g)(0, nages) = equ_Z(s, p, g)(0, nages);
}
}
}
if (save_for_report > 0)
{
SSB_B_yr(eq_yr).initialize();
SSB_N_yr(eq_yr).initialize();
for (s = 1; s <= nseas; s++)
{
for (p = 1; p <= pop; p++)
for (g = 1; g <= gmorph; g++)
if (use_morph(g) > 0)
{
if (s == spawn_seas && sx(g) == 1)
{
SSB_B_yr(eq_yr) += make_mature_bio(GP4(g)) * natage(t + s, p, g);
SSB_N_yr(eq_yr) += make_mature_numbers(GP4(g)) * natage(t + s, p, g);
}
Save_PopAge(t + s, p, g) = natage(t + s, p, g);
Save_PopAge(t + s, p + pop, g) = elem_prod(natage(t + s, p, g), mfexp(-Z_rate(t + s, p, g) * 0.5 * seasdur(s)));
if (Settle_seas(settle_g(g)) == s)
Recr(p, t + 1 + Settle_seas_offset(settle_g(g))) += equ_Recr * recr_dist(y, GP(g), settle_g(g), p) * platoon_distr(GP2(g));
Save_PopBio(t + s, p, g) = elem_prod(natage(t + s, p, g), Wt_Age_beg(s, g));
Save_PopBio(t + s, p + pop, g) = elem_prod(Save_PopAge(t + s, p + pop, g), Wt_Age_mid(s, g));
}
for (int ff = 1; ff <= N_pred; ff++)
{
f = predator(ff);
for (g = 1; g <= 6; g++)
{
catch_fleet(t + s, f, g) = equ_catch_fleet(g, s, f);
}
for (g = 1; g <= gmorph; g++)
{
catage(t + s, f, g) = equ_catage(s, f, g);
}
}
}
}
}
else // area-specific spawn-recruitment
{
}
// SS_Label_Info_23.5 #Calculate equilibrium using initial F
#ifdef DO_ONCE
if (do_once == 1)
echoinput << "Begin calculating initial age structure" << endl;
#endif
eq_yr = styr - 1;
bio_yr = styr;
if (fishery_on_off == 1)
{
Fishon = 1;
}
else
{
Fishon = 0;
}
for (f = 1; f <= N_SRparm2; f++)
{
if (SR_parm_timevary(f) == 0)
{
// no change to SR_parm_work
}
else
{
SR_parm_work(f) = parm_timevary(SR_parm_timevary(f), eq_yr);
}
SR_parm_byyr(eq_yr, f) = SR_parm_work(f);
}
for (s = 1; s <= nseas; s++)
{
t = styr - nseas - 1 + s;
for (int ff = 1; ff <= N_catchfleets(0); ff++)
{
f = fish_fleet_area(0, ff);
if (init_F_loc(s, f) > 0)
{
Hrate(f, t) = init_F(init_F_loc(s, f));
}
}
}
// for the initial equilibrium, R0 and steepness will remain same as for virgin, but a regime shift is allowed
// change with 3.30.12 to allow R0 to change according to a timevary effect
// exp_rec(eq_yr,1)=Recr_virgin;
// R1_exp=Recr_virgin;
R1_exp = mfexp(SR_parm_work(1));
exp_rec(eq_yr, 1) = R1_exp;
// SS_Label_Info_23.5.1 #Apply adjustments to the recruitment level
// SPAWN-RECR: adjust recruitment for the initial equilibrium
regime_change = 1.0;
if (SR_parm_timevary(N_SRparm2 - 1) > 0) // timevary regime exists
{
regime_change = mfexp(SR_parm_work(N_SRparm2 - 1));
}
if (init_equ_steepness == 0) // Adjustments do not include spawner-recruitment steepness
{
// R1=Recr_virgin*regime_change;
R1 = R1_exp * regime_change;
exp_rec(eq_yr, 2) = R1;
exp_rec(eq_yr, 3) = R1;
exp_rec(eq_yr, 4) = R1;
equ_Recr = R1; // equ_Recr is used inside of Do_Equil_Calc
Do_Equil_Calc(equ_Recr);
CrashPen += Equ_penalty;
}
else
{
// SS_Label_Info_23.5.1.2 #Adjustments include spawner-recruitment function
// do initial equilibrium with R1 based on offset from spawner-recruitment curve, using same approach as the benchmark calculations
// first get SPR for this init_F
// SPAWN-RECR: calc initial equilibrium pop, SPB, Recruitment
// equ_Recr=Recr_virgin;
equ_Recr = R1_exp * regime_change;
Do_Equil_Calc(equ_Recr);
CrashPen += Equ_penalty;
SPR_temp = SSB_equil / equ_Recr; // spawners per recruit at initial F
// get equilibrium SSB and recruitment from SPR_temp, Recr_virgin and virgin steepness
Equ_SpawnRecr_Result = Equil_Spawn_Recr_Fxn(SR_parm(2), SR_parm(3), SSB_virgin, Recr_virgin, SPR_temp); // returns 2 element vector containing equilibrium biomass and recruitment at this SPR
R1_exp = Equ_SpawnRecr_Result(2); // set the expected recruitment equal to this equilibrium
exp_rec(eq_yr, 1) = R1_exp;
equ_Recr = R1_exp * regime_change;
exp_rec(eq_yr, 2) = equ_Recr;
exp_rec(eq_yr, 3) = equ_Recr;
exp_rec(eq_yr, 4) = equ_Recr;
R1 = equ_Recr;
Do_Equil_Calc(equ_Recr); // calculated SSB_equil
CrashPen += Equ_penalty;
}
Smry_Table(styr - 1, 1) = totbio; // from equil calcs
Smry_Table(styr - 1, 2) = smrybio; // from equil calcs
Smry_Table(styr - 1, 3) = smrynum; // from equil calcs
SSB_pop_gp(eq_yr) = SSB_equil_pop_gp; // dimensions of pop x N_GP
if (Hermaphro_Option != 0)
MaleSPB(eq_yr) = MaleSSB_equil_pop_gp;
SSB_yr(eq_yr) = SSB_equil;
SR_parm_byyr(eq_yr, N_SRparm2 + 1) = SSB_equil;
SR_parm_work(N_SRparm2 + 1) = SSB_equil;
SSB_yr(styr) = SSB_equil;
env_data(styr - 1, -1) = 0.0;
env_data(styr - 1, -2) = 0.0;
env_data(styr - 1, -3) = 0.0;
env_data(styr - 1, -4) = 0.0;
/*
// save est_equ_catch which has units (biomass vs numbers) according to that fleet; used in objfun
for (s = 1; s <= nseas; s++)
for (int ff = 1; ff <= N_catchfleets(0); ff++)
{
f = fish_fleet_area(0, ff);
if (catchunits(f) == 1)
{
est_equ_catch(s, f) = equ_catch_fleet(2, s, f);
}
else
{
est_equ_catch(s, f) = equ_catch_fleet(5, s, f);
}
}
*/
// if (save_for_report > 0)
{
for (s = 1; s <= nseas; s++)
{
t = styr - nseas - 1 + s;
for (int ff = 1; ff <= N_catchfleets(0); ff++)
{
f = fish_fleet_area(0, ff);
for (g = 1; g <= 6; g++)
{
catch_fleet(t, f, g) = equ_catch_fleet(g, s, f);
annual_catch(styr - 1, g) += equ_catch_fleet(g, s, f);
}
for (g = 1; g <= gmorph; g++)
{
catage(t, f, g) = equ_catage(s, f, g);
}
}
for (int ff = 1; ff <= N_pred; ff++)
{
f = predator(ff);
for (g = 1; g <= 6; g++)
{
catch_fleet(t, f, g) = equ_catch_fleet(g, s, f);
}
for (g = 1; g <= gmorph; g++)
{
catage(t, f, g) = equ_catage(s, f, g);
}
}
}
for (k = 1; k <= 3; k++)
{
Smry_Table(styr - 1, k + 3) = annual_catch(styr - 1, k);
}
}
for (s = 1; s <= nseas; s++)
{
t = styr - nseas - 1 + s;
a = styr - 1 + s;
for (p = 1; p <= pop; p++)
for (g = 1; g <= gmorph; g++)
{
natage(t, p, g)(0, nages) = equ_numbers(s, p, g)(0, nages);
natage(a, p, g)(0, nages) = equ_numbers(s, p, g)(0, nages);
Z_rate(t, p, g) = equ_Z(s, p, g);
}
}
if (save_for_report > 0)
{
t = styr - nseas - 1;
SSB_B_yr(eq_yr).initialize();
SSB_N_yr(eq_yr).initialize();
for (s = 1; s <= nseas; s++)
for (p = 1; p <= pop; p++)
for (g = 1; g <= gmorph; g++)
if (use_morph(g) > 0)
{
if (s == spawn_seas && sx(g) == 1)
{
SSB_B_yr(eq_yr) += make_mature_bio(GP4(g)) * natage(t + s, p, g);
SSB_N_yr(eq_yr) += make_mature_numbers(GP4(g)) * natage(t + s, p, g);
}
Save_PopAge(t + s, p, g) = natage(t + s, p, g);
Save_PopAge(t + s, p + pop, g) = elem_prod(natage(t + s, p, g), mfexp(-Z_rate(t + s, p, g) * 0.5 * seasdur(s)));
Save_PopBio(t + s, p, g) = elem_prod(natage(t + s, p, g), Wt_Age_beg(s, g));
Save_PopBio(t + s, p + pop, g) = elem_prod(Save_PopAge(t + s, p + pop, g), Wt_Age_mid(s, g));
if (Settle_seas(settle_g(g)) == s)
Recr(p, t + 1 + Settle_seas_offset(settle_g(g))) += equ_Recr * recr_dist(y, GP(g), settle_g(g), p) * platoon_distr(GP2(g));
}
}
if (docheckup == 1)
echoinput << " init equil age comp for styr " << styr << endl
<< natage(styr) << endl
<< endl;
// if recrdevs start before styr, then use them to adjust the initial agecomp
// apply a fraction of the bias adjustment, so bias adjustment gets less linearly as proceed back in time
if (recdev_first < styr)
{
if (do_recdev <= 2 && SR_fxn != 4)
{
for (p = 1; p <= pop; p++)
for (g = 1; g <= gmorph; g++)
for (a = styr - recdev_first; a >= 1; a--)
{
j = styr - a;
natage(styr, p, g, a) *= mfexp(recdev(j) - biasadj(j) * half_sigmaRsq);
}
}
else
{
for (p = 1; p <= pop; p++)
for (g = 1; g <= gmorph; g++)
for (a = styr - recdev_first; a >= 1; a--)
{
j = styr - a;
natage(styr, p, g, a) *= mfexp(recdev(j));
}
}
}
SSB_pop_gp(styr) = SSB_pop_gp(styr - 1); // placeholder in case not calculated early in styr
// note: the above keeps SSB_pop_gp(styr) = SSB_equil. It does not adjust for initial agecomp, but probably should
} // end initial_conditions
//*********************************************************************
FUNCTION void get_time_series()
{
/* SS_Label_Function_24 get_time_series */
dvariable crashtemp;
dvariable crashtemp1;
dvariable interim_tot_catch;
dvariable Z_adjuster;
dvariable R0_use;
dvariable SSB_use;
if (Do_Morphcomp > 0)
Morphcomp_exp.initialize();
// SS_Label_Info_24.0 #Retrieve spawning biomass and recruitment from the initial equilibrium
// SPAWN-RECR: begin of time series, retrieve last spbio and recruitment
SSB_current = SSB_yr(styr); // need these initial assignments in case recruitment distribution occurs before spawnbio&recruits
if (recdev_doit(styr - 1) > 0)
{
Recruits = R1 * mfexp(recdev(styr - 1) - biasadj(styr - 1) * half_sigmaRsq);
}
else
{
Recruits = R1;
}
// SS_Label_Info_24.1 #Loop the years
for (y = styr; y <= endyr; y++)
{
yz = y;
if (STD_Yr_Reverse_F(y) > 0)
F_std(STD_Yr_Reverse_F(y)) = 0.0;
t_base = styr + (y - styr) * nseas - 1;
for (f = 1; f <= N_SRparm2; f++)
{
if (SR_parm_timevary(f) == 0)
{
// no change to SR_parm_work
}
else
{
SR_parm_work(f) = parm_timevary(SR_parm_timevary(f), y);
}
SR_parm_byyr(y, f) = SR_parm_work(f);
}
// SS_Label_Info_24.1.1 #store begin of year quantities for use in density-dependent processes
{
env_data(y, -1) = log(SSB_current / SSB_yr(styr - 1));
if (recdev_doit(y) > 0)
{
env_data(y, -2) = recdev(y);
} // store so can do density-dependence
else
{ // should be 0.0
}
t = t_base + 1; // first season
s = 1;
if (WTage_rd > 0)
{
Wt_Age_beg(s) = Wt_Age_t(t, 0);
Wt_Age_mid(s) = Wt_Age_t(t, -1);
}
else if (timevary_MG(y, 2) > 0 || timevary_MG(y, 3) > 0 || save_for_report == 1)
{
get_growth3(y, t, 1, 1); // before season loop, used for summary biomass
ALK_subseas_update(1) = 1; // do 1st subseas of 1st season; ADD THIS LINE for 3.30.17
Make_AgeLength_Key(s, 1); // this will give wt_age_beg before any time-varying parameter changes for this year
ALK_idx = (s - 1) * N_subseas + 1;
for (g = 1; g <= gmorph; g++)
if (use_morph(g) > 0)
{
Wt_Age_beg(s, g) = (ALK(ALK_idx, g) * wt_len(s, GP(g))); // wt-at-age at beginning of period
}
}
smrybio = 0.0;
smrynum = 0.0;
// do not do totbio here because new recruits have not yet occurred
for (g = 1; g <= gmorph; g++)
if (use_morph(g) > 0)
{
for (p = 1; p <= pop; p++)
{
smrybio += natage(t, p, g)(Smry_Age, nages) * Wt_Age_beg(1, g)(Smry_Age, nages); // calc before recruitment and time-vary biology applied
smrynum += sum(natage(t, p, g)(Smry_Age, nages)); //sums to accumulate across platoons and settlements
}
}
env_data(y, -3) = log(smrybio / Smry_Table(styr - 1, 2));
env_data(y, -4) = log(smrynum / Smry_Table(styr - 1, 3));
Smry_Table(y, 2) = smrybio; // gets used as demoninator for some F_std options
Smry_Table(y, 3) = smrynum;
}
// SS_Label_Info_24.1.1 #skip biology updating if y=styr because already done
if (y > styr)
{
if (do_densitydependent == 1)
make_densitydependent_parm(y); // call to adjust for density dependence
// SS_Label_Info_24.1.1 #Update the time varying biology factors if necessary
if (timevary_MG(y, 0) > 0 || save_for_report > 0)
get_MGsetup(y);
if (timevary_MG(y, 2) > 0)
{
ALK_subseas_update = 1; // indicate that all ALKs will need re-estimation
get_growth2(y); // propagates growth to each season this year and to begin next year
get_growth3(y, t, 1, 1); // cleans up the linear growth range for begin of this year
}
if (timevary_MG(y, 3) > 0)
{
get_wtlen(); // stores values for all seasons
// note that get_mat_fec() will get called in the season loop because it may need the ALK for a later season
// but Maunder's M in get_natmort() may use the fecundity vector, so would be using the most recently calculated Problem??
if (Hermaphro_Option != 0)
get_Hermaphro();
}
if (timevary_MG(y, 1) > 0)
{
get_natmort();
}
else
{
for (s = 1; s <= nseas; s++)
{
natM(t_base + s) = natM(t_base - nseas + s);
} // set M equal to last year's;
// does all areas (p), but if there are predators, then add of pred_M2 occurs in season loop below
}
if (timevary_MG(y, 4) > 0)
get_recr_distribution();
if (y >= Bmark_Yr(7) && y <= Bmark_Yr(8))
{
for (gp = 1; gp <= N_GP; gp++)
for (p = 1; p <= pop; p++)
for (settle = 1; settle <= N_settle_timings; settle++)
if (recr_dist_pattern(gp, settle, p) > 0)
{
recr_dist_unf(gp, settle, p) += recr_dist(y, gp, settle, p);
if (gender == 2)
recr_dist_unf(gp + N_GP, settle, p) += recr_dist(y, gp + N_GP, settle, p);
}
}
if (timevary_MG(y, 5) > 0)
get_migration();
if (timevary_MG(y, 7) > 0)
{
get_catch_mult(y, catch_mult_pointer);
}
if (Use_AgeKeyZero > 0)
{
if (timevary_MG(y, 6) > 0)
{
get_age_age(Use_AgeKeyZero, AgeKey_StartAge, AgeKey_Linear1, AgeKey_Linear2); // call function to get the age_age key
if (save_for_report == 1 && store_agekey_add > 0)
{
save_agekey_count++; // next blank key after the used keys
age_age(save_agekey_count) = age_age(Use_AgeKeyZero);
age_err(save_agekey_count) = age_err(Use_AgeKeyZero);
}
#ifdef DO_ONCE
if (do_once == 1)
echoinput << " ageerr_key recalc in " << y << endl;
#endif
}
}
if (save_for_report > 0)
{
if (timevary_MG(y, 1) > 0 || timevary_MG(y, 2) > 0 || timevary_MG(y, 3) > 0)
{
get_saveGparm();
}
}
}
// SS_Label_Info_24.2 #Loop the seasons
for (s = 1; s <= nseas; s++)
{
if (docheckup == 1)
echoinput << endl
<< "************************************" << endl
<< " year, seas " << y << " " << s << endl;
// SS_Label_Info_24.1.2 #Call selectivity, which does its own internal check for time-varying changes
// note that Make_Fish_selex is called later after the ALK's have been updated
if (s == 1 && y > styr)
get_selectivity();
t = t_base + s;
// SS_Label_Info_24.2.1 #Update the age-length key and the fishery selectivity for this season
// if(timevary_MG(y,2)>0 || timevary_MG(y,3)>0 || save_for_report==1 || WTage_rd>0)
if (timevary_MG(y, 2) > 0 || save_for_report == 1)
{
get_growth3(y, t, s, 1); // first subseas of season=s
Make_AgeLength_Key(s, 1);
get_growth3(y, t, s, mid_subseas); // for midseason
Make_AgeLength_Key(s, mid_subseas);
// SPAWN-RECR: call Make_Fecundity in time series
if (s == spawn_seas)
{
if (spawn_subseas != 1 && spawn_subseas != mid_subseas)
{
subseas = spawn_subseas;
get_growth3(y, t, s, subseas);
Make_AgeLength_Key(s, subseas); // spawn subseas
}
}
}
if (WTage_rd > 0)
{
Wt_Age_beg(s) = Wt_Age_t(t, 0);
Wt_Age_mid(s) = Wt_Age_t(t, -1);
if (s == spawn_seas)
{
fec = Wt_Age_t(t, -2);
}
}
else if (timevary_MG(y, 2) > 0 || timevary_MG(y, 3) > 0 || save_for_report > 0 || do_once == 1)
{
if (s == spawn_seas)
get_mat_fec();
// Make_Fecundity();
ALK_idx = (s - 1) * N_subseas + 1; // subseas=1
int ALK_idx2 = (s - 1) * N_subseas + mid_subseas;
for (g = 1; g <= gmorph; g++)
if (use_morph(g) > 0)
{
Wt_Age_beg(s, g) = (ALK(ALK_idx, g) * wt_len(s, GP(g))); // wt-at-age at beginning of period
Wt_Age_mid(s, g) = ALK(ALK_idx2, g) * wt_len(s, GP(g)); // use for fisheries with no size selectivity
}
}
Wt_Age_t(t, 0) = Wt_Age_beg(s);
if (y > styr) // because styr is done as part of initial conditions
{
for (g = 1; g <= gmorph; g++)
if (use_morph(g) > 0)
{
Make_FishSelex();
}
// rebase natM to M1
for(p = 1; p <= pop; p++)
{
natM(t, p) = natM(t,0);
}
// SS_Label_Info_24.x.x #add predator M2 inside the yr,seas loop
if(N_pred>0)
{
// add pred_M2 by area
for (f1 = 1; f1 <= N_pred; f1++)
{
f = predator(f1);
pred_M2(f1, t) = mgp_adj(predparm_pointer(f1)); // base with no seasonal effect
if (nseas > 1)
pred_M2(f1, t) *= mgp_adj(predparm_pointer(f1) + s);
p = fleet_area(f); // area this predator occurs in
// a new array for indexing g and gpi could simplify below
// for (gp = 1; gp <= N_GP * gender * N_settle_timings; gp++)
for (gp = 1; gp <= N_GP * gender; gp++)
{
g = g_Start(gp); // base platoon
for (settle = 1; settle <= N_settle_timings; settle++)
{
g += N_platoon;
int gpi = GP3(g); // GP*gender*settlement
natM(t, p,gpi) += pred_M2(f1, t) * sel_num(s, f, g);
if (do_once == 1 && p == 1)
echoinput << y << " s " << s << " t " << t << " area " << 0 << " gp " << gpi << " M1: " << natM(t,0, gpi) << endl;
if (do_once == 1)
echoinput << y << " s " << s << " t " << t << " area " << p << " gp " << gpi << " M1+M2: " << natM(t, p, gpi) << endl;
}
}
}
}
for(p = 1; p <= pop; p++)
{
int s1 = (p - 1) * nseas + s;
surv1(s1) = mfexp(-natM(t,p) * seasdur_half(s));
surv2(s1) = square(surv1(s1));
}
}
// SS_Label_Info_24.2.2 #Compute spawning biomass if this is spawning season so recruits could occur later this season
// SPAWN-RECR: calc SPB in time series if spawning is at beginning of the season
if (s == spawn_seas && spawn_time_seas < 0.0001) // compute spawning biomass if spawning at beginning of season so recruits could occur later this season
{
SSB_pop_gp(y).initialize();
SSB_B_yr(y).initialize();
SSB_N_yr(y).initialize();
Smry_Table(y, 15) = 0.0;
for (p = 1; p <= pop; p++)
{
for (g = 1; g <= gmorph; g++)
if (sx(g) == 1 && use_morph(g) > 0) // female
{
SSB_pop_gp(y, p, GP4(g)) += fracfemale_mult * fec(g) * natage(t, p, g); // accumulates SSB by area and by growthpattern
SSB_B_yr(y) += fracfemale_mult * make_mature_bio(GP4(g)) * natage(t, p, g);
SSB_N_yr(y) += fracfemale_mult * make_mature_numbers(GP4(g)) * natage(t, p, g);
Smry_Table(y, 15) += fracfemale_mult * natage(t, p, g) * elem_prod(fec(g), r_ages); // for mean age of female spawners = GenTime
// SSB_pop_gp(y,p,GP4(g)) += fec(g)*natage(t,p,g); // accumulates SSB by area and by growthpattern
// SSB_B_yr(y) += make_mature_bio(GP4(g))*natage(t,p,g);
// SSB_N_yr(y) += make_mature_numbers(GP4(g))*natage(t,p,g);
}
}
SSB_current = sum(SSB_pop_gp(y));
SSB_yr(y) = SSB_current;
if (Hermaphro_Option != 0) // get male biomass
{
MaleSPB(y).initialize();
for (p = 1; p <= pop; p++)
{
for (g = 1; g <= gmorph; g++)
if (sx(g) == 2 && use_morph(g) > 0) // male; all assumed to be mature
{
MaleSPB(y, p, GP4(g)) += Wt_Age_t(t, 0, g) * natage(t, p, g); // accumulates SSB by area and by growthpattern
}
}
if (Hermaphro_maleSPB > 0.0) // add MaleSPB to female SSB
{
SSB_current += Hermaphro_maleSPB * sum(MaleSPB(y));
SSB_yr(y) = SSB_current;
}
}
// SS_Label_Info_24.2.3 #Get the total recruitment produced by this spawning biomass
// SPAWN-RECR: calc recruitment in time series; need to make this area-specific