forked from nmfs-ost/ss3-source-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSS_popdyn.tpl
1925 lines (1798 loc) · 76.5 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
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) recdev(recdev_end+1,YrMax)=Fcast_recruitments(recdev_end+1,YrMax); // only needed here for reporting
// if(mcmc_counter>0) warning<<N_warn<<" "<<mcmc_counter<<" MGparm "<<MGparm<<" SRparm "<<SR_parm<<" recdev "<<recdev2<<" fore_rec "<<Fcast_recruitments<<" selparm "<<selparm<<" q: "<<Q_parm<<endl;
} // 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();
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();
natM_unf.initialize();
surv1_unf.initialize();
surv2_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) cout<<" MG setup OK "<<endl;
#endif
if(MG_active(2)>0) get_growth1(); // seasonal effects and CV
#ifdef DO_ONCE
if(do_once==1) cout<<" growth1 OK"<<endl;
#endif
if(MG_active(2)>0 || MG_active(3)>0 || save_for_report>0)
{
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);
}
}
}
if(MG_active(1)>0) get_natmort();
#ifdef DO_ONCE
if(do_once==1) cout<<" natmort OK"<<endl;
#endif
if(y>=Bmark_Yr(1)&&y<=Bmark_Yr(2))
{
for (s=1;s<=nseas;s++)
for (gp=1;gp<=N_GP*gender*N_settle_timings;gp++)
{
natM_unf(s,gp)+=natM(s,gp); // need nseas to capture differences due to settlement
surv1_unf(s,gp)+=surv1(s,gp); // need nseas to capture differences due to settlement
surv2_unf(s,gp)+=surv2(s,gp); // need nseas to capture differences due to settlement
}
}
if(MG_active(3)>0) get_wtlen();
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) cout<<" 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)
{
cout<<" ageerr_key OK"<<endl;
echoinput<<" ageerr_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) cout<<" selex OK, ready to call ALK and fishselex "<<endl;
#endif
// SS_Label_Info_23.3 #Loop seasons and subseasons
t=styr-1;
for (s=1;s<=nseas;s++)
{
t++;
/* // redundant
if(MG_active(2)>0 || MG_active(3)>0 || save_for_report>0) // initial year; if growth parms are active, get growth
{
for(subseas=1;subseas<=N_subseas;subseas++) // do all subseasons in first year
{
// SS_Label_Info_23.3.1 #calculate mean size-at-age, then size_at_age distribution (ALK)
get_growth3(s, subseas);
Make_AgeLength_Key(s, subseas);
}
}
*/
if(WTage_rd>0)
{
for (g=1;g<=gmorph;g++)
if(use_morph(g)>0)
{
Wt_Age_beg(s,g)=WTage_emp(t,GP3(g),0);
Wt_Age_mid(s,g)=WTage_emp(t,GP3(g),-1);
if(s==spawn_seas) fec(g)=WTage_emp(t,GP3(g),-2);
}
}
else if(MG_active(2)>0 || MG_active(3)>0 || save_for_report>0 || do_once==1)
{
Make_Fecundity();
// if(do_once==1) echoinput<<"Save_fec in initial year: "<<t<<" %% "<<save_sel_fec(t,1,0)<<endl;
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
}
}
Save_Wt_Age(t)=Wt_Age_beg(s);
// if(do_once==1) echoinput<<"Save_Wt_Age in initial year: "<<t<<" %% "<<Save_Wt_Age(t)<<endl;
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();
}
}
#ifdef DO_ONCE
if(do_once==1) cout<<" ready for 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;
Mgmt_quant(3)=smrybio;
Mgmt_quant(4)=Recr_virgin;
}
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);
}
}
}
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);
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));
}
}
}
else // area-specific spawn-recruitment
{
}
// SS_Label_Info_23.5 #Calculate equilibrium using initial F
#ifdef DO_ONCE
if(do_once==1) cout<<" ready for initial age struc "<<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 (f=1;f<=Nfleet;f++)
{
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;
}
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;
for (s=1;s<=nseas;s++)
for (f=1;f<=Nfleet;f++)
{
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 (f=1;f<=Nfleet;f++)
{
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(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_PopBio(t+s,p,g)=elem_prod(natage(t+s,p,g),Wt_Age_beg(s,g));
Save_PopAge(t+s,p,g)=natage(t+s,p,g);
// warning<<N_warn<<" "<<s<<" init "<<t+Settle_seas_offset(settle_g(g))<<endl;
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 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;}
smrybio=0.0;
smrynum=0.0;
for (g=1;g<=gmorph;g++)
if(use_morph(g)>0)
{
for (p=1;p<=pop;p++)
{
smrybio+=natage(styr,p,g)(Smry_Age,nages)*Save_Wt_Age(styr,g)(Smry_Age,nages);
smrynum+=sum(natage(styr,p,g)(Smry_Age,nages)); //sums to accumulate across platoons and settlements
}
}
Smry_Table(styr-1,2)=smrybio;
Smry_Table(styr-1,3)=smrynum;
// 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);
}
{
env_data(y,-1)=log(SSB_current/SSB_yr(styr-1)); // store most recent value for density-dependent effects, NOTE - off by a year if recalc'ed at beginning of season 1
if(recdev_doit(y)>0)
{env_data(y,-2)=recdev(y);} // store so can do density-dependence
else
{ // should be 0.0
}
// warning<<N_warn<<" "<<y<<" "<<SSB_current<<" "<<SSB_yr(styr-1)<<" "<<recdev(y)<<" env: "<<env_data(y)<<endl;
t=t_base+1; // first season
s=1;
if(WTage_rd>0)
{
for (g=1;g<=gmorph;g++)
if(use_morph(g)>0)
{
Wt_Age_beg(s,g)=WTage_emp(t,GP3(g),0);
Wt_Age_mid(s,g)=WTage_emp(t,GP3(g),-1);
}
}
else if(timevary_MG(y,2)>0 || timevary_MG(y,3)>0 || save_for_report==1 || WTage_rd>0)
{
subseas=1; // begin season note that ALK_idx re-calculated inside get_growth3
ALK_idx=(s-1)*N_subseas+subseas; // redundant with calc inside get_growth3 ????
// get_growth3(s, subseas); // not needed because size-at-age already has been propagated to seas 1 subseas 1
Make_AgeLength_Key(s, subseas); // this will give wt_age_beg before any time-varying parameter changes for this year
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
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
}
}
smrybio=0.0;
smrynum=0.0;
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);
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;
Smry_Table(y,3)=smrynum;
}
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);
}
if(timevary_MG(y,1)>0) get_natmort();
if(y>=Bmark_Yr(1)&&y<=Bmark_Yr(2))
{
for (s=1;s<=nseas;s++)
for (gp=1;gp<=N_GP*gender*N_settle_timings;gp++)
{
natM_unf(s,gp)+=natM(s,gp);
surv1_unf(s,gp)+=surv1(s,gp);
surv2_unf(s,gp)+=surv2(s,gp);
}
}
if(timevary_MG(y,3)>0) get_wtlen();
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
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)
{
subseas=1; // begin season note that ALK_idx re-calculated inside get_growth3
get_growth3(y,t,s, subseas);
Make_AgeLength_Key(s, subseas);
subseas=mid_subseas;
get_growth3(y,t,s, subseas);
Make_AgeLength_Key(s, subseas); // for midseason
// 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)
{
for (g=1;g<=gmorph;g++)
if(use_morph(g)>0)
{
Wt_Age_beg(s,g)=WTage_emp(t,GP3(g),0);
Wt_Age_mid(s,g)=WTage_emp(t,GP3(g),-1);
if(s==spawn_seas)
{
fec(g)=WTage_emp(t,GP3(g),-2);
save_sel_fec(t,g,0)= fec(g);
// if(y==endyr) save_sel_fec(t+nseas,g,0)=fec(g);
}
}
}
else if(timevary_MG(y,2)>0 || timevary_MG(y,3)>0 || save_for_report>0 || do_once==1)
{
Make_Fecundity();
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
}
}
Save_Wt_Age(t)=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();}
}
// 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();
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);
// 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)) += Save_Wt_Age(t,g)*natage(t,p,g); // accumulates SSB by area and by growthpattern
}
}
if(Hermaphro_maleSPB==1) // add MaleSPB to female SSB
{
SSB_current+=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
if(SR_parm_timevary(1)==0) // R0 is not time-varying
{R0_use=Recr_virgin; SSB_use=SSB_virgin;}
else
{
R0_use=mfexp(SR_parm_work(1));
equ_Recr=R0_use;
Fishon=0;
eq_yr=y;
bio_yr=y;
Do_Equil_Calc(R0_use); // call function to do equilibrium calculation
if(fishery_on_off==1) {Fishon=1;} else {Fishon=0;}
SSB_use=SSB_equil;
}
Recruits=Spawn_Recr(SSB_use,R0_use,SSB_current); // calls to function Spawn_Recr
apply_recdev(Recruits, R0_use); // apply recruitment deviation
// distribute Recruitment of age 0 fish among the current and future settlements; and among areas and morphs
// use t offset for each birth event: Settlement_offset(settle)
// so the total number of Recruits will be relative to their numbers at the time of the set of settlement_events.
// so need the integer elapsed time (in season count) stored in Birth_offset()
// and need the real elapsed time (in fraction of a year) from the beginning of the season to settlement
// use NatM to calculate the virtual numbers that would have existed at the beginning of the season of the settlement
// need to use natM(t) because natM(t+offset) is not yet known
// also need to store the integer age at settlement
// NOTE: the settlement is added to natage at the beginning of the season in which the settlement occurs,
// so it will be fished and sampled even before its settlement time
// this is a shortcoming that might be dealt with in future.
// For now, users will need to create finer season structure
// NOTE: the distributed recruits are added into natage because more than one settlement can occur in same season
// but each settlement has a unique "g", so maybe additive is not necessary
for (g=1;g<=gmorph;g++)
if(use_morph(g)>0)
{
settle=settle_g(g);
for (p=1;p<=pop;p++)
{
if(y==styr) natage(t+Settle_seas_offset(settle),p,g,Settle_age(settle))=0.0; // to negate the additive code
natage(t+Settle_seas_offset(settle),p,g,Settle_age(settle)) +=
Recruits*recr_dist(y,GP(g),settle,p)*platoon_distr(GP2(g))*
mfexp(natM(s,GP3(g),Settle_age(settle))*Settle_timing_seas(settle));
Recr(p,t+Settle_seas_offset(settle))+=Recruits*recr_dist(y,GP(g),settle,p)*platoon_distr(GP2(g));
// the adjustment for mortality increases recruit value for elapsed time since begin of season because M will then be applied from beginning of season
if(docheckup==1) echoinput<<y<<" Recruits, dist, surv, result"<<Recruits<<" "<<recr_dist(y,GP(g),settle,p)<<" "<<mfexp(natM(s,GP3(g),Settle_age(settle))*Settle_timing_seas(settle))<<" "<<natage(t+Settle_seas_offset(settle),p,g,Settle_age(settle))<<endl;
}
}
}
else
{
// spawning biomass and total recruits will be calculated later so they can use Z
}
// SS_Label_Info_24.3 #Loop the areas
for (p=1;p<=pop;p++)
{
for (g=1;g<=gmorph;g++)
if(use_morph(g)>0)
{
// SS_Label_Info_24.3.1 #Get middle of season numbers-at-age from M only;
Nmid(g) = elem_prod(natage(t,p,g),surv1(s,GP3(g))); // get numbers-at-age(g,a) surviving to middle of time period
if(docheckup==1) echoinput<<p<<" "<<g<<" "<<GP3(g)<<" area & morph "<<endl<<"N-at-age "<<natage(t,p,g)(0,min(6,nages))<<endl
<<"survival "<<surv1(s,GP3(g))(0,min(6,nages))<<endl;
if(save_for_report==1)
{
// SS_Label_Info_24.3.2 #Store some beginning of season quantities
Save_PopLen(t,p,g)=0.0;
Save_PopLen(t,p+pop,g)=0.0; // later put midseason here
Save_PopWt(t,p,g)=0.0;
Save_PopWt(t,p+pop,g)=0.0; // later put midseason here
Save_PopAge(t,p,g)=0.0;
Save_PopAge(t,p+pop,g)=0.0; // later put midseason here
Save_PopBio(t,p,g)=0.0;
Save_PopBio(t,p+pop,g)=0.0; // later put midseason here
for (a=0;a<=nages;a++)
{
Save_PopLen(t,p,g)+=value(natage(t,p,g,a))*value(ALK(s,g,a));
Save_PopWt(t,p,g)+=value(natage(t,p,g,a))*value(elem_prod(ALK(s,g,a),wt_len(s,GP(g))));
Save_PopAge(t,p,g,a)=value(natage(t,p,g,a));
Save_PopBio(t,p,g,a)=value(natage(t,p,g,a))*value(Wt_Age_beg(s,g,a));
} // close age loop
}
}
// SS_Label_Info_24.3.3 #Do fishing mortality using switch(F_method)
catage_tot.initialize();
if(catch_seas_area(t,p,0)==1 && fishery_on_off==1)
{
switch (F_Method_use)
{
// SS_Label_Info_24.3.3.3 #use the hybrid F method
case 3: // hybrid F_method
{
// SS_Label_Info_24.3.3.3.1 #Start by doing a Pope's approximation
for (f=1;f<=Nfleet;f++)
if(fleet_type(f)==1) // do exact catch for this fleet; skipping adjustment for bycatch fleets
{
if (catch_seas_area(t,p,f)==1)
{
vbio.initialize();
for (g=1;g<=gmorph;g++)
if(use_morph(g)>0)
{
if(catchunits(f)==1)
{vbio+=Nmid(g)*sel_al_2(s,g,f);} // retained catch bio
else
{vbio+=Nmid(g)*sel_al_4(s,g,f);} // retained catch numbers
} //close gmorph loop
// SS_Label_Info_24.3.3.3.2 #Apply constraint so that no fleet's initial calculation of harvest rate would exceed 95%
temp = catch_ret_obs(f,t)/(vbio+0.1*catch_ret_obs(f,t)); // Pope's rate robust
join1=1./(1.+mfexp(30.*(temp-0.95))); // steep logistic joiner at harvest rate of 0.95
temp1=join1*temp + (1.-join1)*0.95;
// SS_Label_Info_24.3.3.3.3 #Convert the harvest rate to a starting value for F
Hrate(f,t)=-log(1.-temp1)/seasdur(s); // initial estimate of F (even though labelled as Hrate)
//echoinput<<t<<" "<<vbio<<" "<<catch_ret_obs(f,t)<<" "<<temp<<" "<<Hrate(f,t)<<endl<<
//Nmid(1)<<endl<<sel_al_2(s,1,f)<<endl;
//if(catch_ret_obs(f,t)>0.0) exit(1);
// done with starting values from Pope's approximation
}
else
{
// Hrate(f,t) previously set to zero or set to a parameter value
}
}
// SS_Label_Info_24.3.3.3.4 #Do a specified number of loops to tune up these F values to more closely match the observed catch
for (int tune_F=1;tune_F<=F_Tune-1;tune_F++)
{
// SS_Label_Info_24.3.3.3.5 #add F+M to get Z
for (g=1;g<=gmorph;g++)
if(use_morph(g)>0)
{
Z_rate(t,p,g)=natM(s,GP3(g));
for (f=1;f<=Nfleet;f++) //loop over fishing fleets to get Z
if (catch_seas_area(t,p,f)!=0)
{
Z_rate(t,p,g)+=deadfish(s,g,f)*Hrate(f,t);
}
Zrate2(p,g)=elem_div( (1.-mfexp(-seasdur(s)*Z_rate(t,p,g))), Z_rate(t,p,g));
}
// gradient_structure::set_NO_DERIVATIVES();
// SS_Label_Info_24.3.3.3.6 #Now calc adjustment to Z based on changes to be made to Hrate
// if(tune_F<F_Tune)
{
// now calc adjustment to Z based on changes to be made to Hrate
interim_tot_catch=0.0; // this is the expected total catch that would occur with the current Hrates and Z
for (f=1;f<=Nfleet;f++)
if(fleet_type(f)==1) // skips bycatch fleets
{
if (catch_seas_area(t,p,f)==1)
{
for (g=1;g<=gmorph;g++)
if(use_morph(g)>0)
{
if(catchunits(f)==1)
{
interim_tot_catch+=catch_mult(y,f)*Hrate(f,t)*elem_prod(natage(t,p,g),sel_al_2(s,g,f))*Zrate2(p,g); // biomass basis
}
else
{
interim_tot_catch+=catch_mult(y,f)*Hrate(f,t)*elem_prod(natage(t,p,g),sel_al_4(s,g,f))*Zrate2(p,g); // numbers basis
}
} //close gmorph loop
} // close fishery
}
Z_adjuster = totcatch_byarea(t,p)/(interim_tot_catch+0.0001); // but this totcatch_by_area needs to exclude fisheries with F from param
for (g=1;g<=gmorph;g++)
if(use_morph(g)>0)
{
Z_rate(t,p,g)=natM(s,GP3(g)) + Z_adjuster*(Z_rate(t,p,g)-natM(s,GP3(g))); // need to modify to only do the exact catches
Zrate2(p,g)=elem_div( (1.-mfexp(-seasdur(s)*Z_rate(t,p,g))), Z_rate(t,p,g));
}
for (f=1;f<=Nfleet;f++) //loop over fishing fleets with input catch
if(fleet_type(f)==1)
{
if(catch_seas_area(t,p,f)==1)
{
vbio=0.; // now use this to calc the selected vulnerable biomass (numbers) to each fishery with the adjusted Zrate2
// since catch = N * F*sel * (1-e(-Z))/Z
// so F = catch / (N*sel * (1-e(-Z)) /Z )
for (g=1;g<=gmorph;g++)
if(use_morph(g)>0)
{
if(catchunits(f)==1)
{
vbio+=elem_prod(natage(t,p,g),sel_al_2(s,g,f)) *Zrate2(p,g);
}
else
{
vbio+=elem_prod(natage(t,p,g),sel_al_4(s,g,f)) *Zrate2(p,g);
}
} //close gmorph loop
temp=catch_ret_obs(f,t)/(catch_mult(y,f)*vbio+0.0001); // prototype new F
join1=1./(1.+mfexp(30.*(temp-0.95*max_harvest_rate)));
Hrate(f,t)=join1*temp + (1.-join1)*max_harvest_rate; // new F value for this fleet
} // close fishery
}
}
/*
else
{
// SS_Label_Info_24.3.3.3.7 #Final tuning loop; loop over fleets to apply the iterated F
for (f=1;f<=Nfleet;f++)
if (catch_seas_area(t,p,f)==1)
{
for (g=1;g<=gmorph;g++)
if(use_morph(g)>0)
{
catch_fleet(t,f,1)+=Hrate(f,t)*elem_prod(natage(t,p,g),sel_al_1(s,g,f))*Zrate2(p,g);