-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSS_benchfore.tpl
3662 lines (3482 loc) · 140 KB
/
SS_benchfore.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 #14. **SS_benchfore.tpl**
// SS_Label_file # * <u>setup_Benchmark()</u> // calculates average biology and selectivity over specified range of years for use in benchmark
// SS_Label_file # * <u>get_benchmark()</u> // searches for Fspr, Fmsy, etc. conditioned on average biology and selectivity conditions
// SS_Label_file # * <u>get_forecast()</u> // calculates forecast quantities, includes all popdy characteristics of the time series, writes forecast-report.sso
// SS_Label_file #
FUNCTION void setup_Benchmark() // and forecast
{
// SS_Label_Info_7.5 #Get averages from selected years to use in forecasts
if (Do_Forecast > 0)
{
if (Fcast_timevary_Selex == 1)
{
// SS_Label_Info_7.5.1 #Calc average selectivity to use in forecast; store in endyr+1
temp = float(Fcast_Sel_yr2 - Fcast_Sel_yr1 + 1.);
for (gg = 1; gg <= gender; gg++)
for (f = 1; f <= Nfleet; f++)
{
tempvec_l.initialize();
for (y = Fcast_Sel_yr1; y <= Fcast_Sel_yr2; y++)
{
tempvec_l += sel_l(y, f, gg);
}
for (y = endyr + 1; y <= YrMax; y++)
{
sel_l(y, f, gg) = tempvec_l / temp;
}
tempvec_l.initialize();
for (y = Fcast_Sel_yr1; y <= Fcast_Sel_yr2; y++)
{
tempvec_l += sel_l_r(y, f, gg);
}
for (y = endyr + 1; y <= YrMax; y++)
{
sel_l_r(y, f, gg) = tempvec_l / temp;
}
tempvec_l.initialize();
for (y = Fcast_Sel_yr1; y <= Fcast_Sel_yr2; y++)
{
tempvec_l += discmort2(y, f, gg);
}
for (y = endyr + 1; y <= YrMax; y++)
{
discmort2(y, f, gg) = tempvec_l / temp;
}
if (gg == gender) // vectors processed here have males stacked after females in same row
{
exp_l_temp.initialize();
for (y = Fcast_Sel_yr1; y <= Fcast_Sel_yr2; y++)
{
exp_l_temp += retain(y, f);
}
for (y = endyr + 1; y <= YrMax; y++)
{
retain(y, f) = exp_l_temp / temp;
}
exp_l_temp.initialize();
for (y = Fcast_Sel_yr1; y <= Fcast_Sel_yr2; y++)
{
exp_l_temp += discmort(y, f);
}
for (y = endyr + 1; y <= YrMax; y++)
{
discmort(y, f) = exp_l_temp / temp;
}
}
tempvec_a.initialize();
for (y = Fcast_Sel_yr1; y <= Fcast_Sel_yr2; y++)
{
tempvec_a += sel_a(y, f, gg);
}
for (y = endyr + 1; y <= YrMax; y++)
{
sel_a(y, f, gg) = tempvec_a / temp;
}
tempvec_a.initialize();
for (y = Fcast_Sel_yr1; y <= Fcast_Sel_yr2; y++)
{
tempvec_a += discmort2_a(y, f, gg);
}
for (y = endyr + 1; y <= YrMax; y++)
{
discmort2_a(y, f, gg) = tempvec_a / temp;
}
if (seltype(f + Nfleet, 2) > 0) // using age retention
{
tempvec_a.initialize();
for (y = Fcast_Sel_yr1; y <= Fcast_Sel_yr2; y++)
{
tempvec_a += retain_a(y, f, gg);
}
for (y = endyr + 1; y <= YrMax; y++)
{
retain_a(y, f, gg) = tempvec_a / temp;
}
tempvec_a.initialize();
for (y = Fcast_Sel_yr1; y <= Fcast_Sel_yr2; y++)
{
tempvec_a += discmort_a(y, f, gg);
}
for (y = endyr + 1; y <= YrMax; y++)
{
discmort_a(y, f, gg) = tempvec_a / temp;
}
}
}
}
t = styr + (endyr + 1 - styr) * nseas + spawn_seas - 1;
fec = Wt_Age_t(t, -2);
// for (g=1;g<=gmorph;g++)
// if(use_morph(g)>0 && sx(g)==1)
// {
// fec(g)=save_sel_num(t,0,g);
// }
if (Fcast_Loop_Control(3) == 3) // using mean recr_dist from range of years
{
warnstream << "This option (mean recruitment) may be deprecated; same as forecast option(5), averaging parameters, type 4.";
write_message(WARN, 0);
//get average and store in each fcast years
recr_dist_endyr.initialize();
for (y = Fcast_Rec_yr1; y <= Fcast_Rec_yr2; y++)
for (gp = 1; gp <= N_GP * gender; gp++)
{
recr_dist_endyr(gp) += recr_dist(y, gp);
}
recr_dist_endyr /= float(Fcast_Rec_yr2 - Fcast_Rec_yr1 + 1);
for (y = endyr + 1; y <= YrMax; y++)
{
if (timevary_MG(y, 4) > 0)
{
warnstream << "mean recr_dist for forecast is incompatible with timevary recr_dist in forecast yr: " << y << "; user must adjust manually";
write_message(WARN, 0);
}
recr_dist(y) = recr_dist_endyr;
}
}
else // provide placeholder
{
recr_dist_endyr = recr_dist(endyr);
}
// create average of selected MGparms for use in forecast
for (int parm_type = 1; parm_type <= 12; parm_type++)
{
if(Fcast_MGparm_ave(parm_type, 2) == 1) // do averaging of derived factor
{
double ave_styr = Fcast_MGparm_ave(parm_type,3);
double ave_endyr = Fcast_MGparm_ave(parm_type,4);
double N_ave_yrs = ave_endyr - ave_styr + 1.; // get denominator
switch (parm_type)
{
case 1: // 1=Natural mortality (M),
for (int s = 1; s <= nseas; s++)
for (int g = 1; g <= gmorph; g++)
{
int gpi = GP3(g);
for (int p = 0; p <= pop; p++) // question. Perhaps only do this for area 0 as others filled in later in code
{
tempvec_a.initialize();
for (y = ave_styr; y <= ave_endyr; y++)
{
t = styr + (y - styr) * nseas - 1 + s;
tempvec_a += natM(t, p, gpi);
}
tempvec_a /= N_ave_yrs;
for (int y = endyr + 1; y <= YrMax; y++)
{
t = styr + (y - styr) * nseas - 1 + s;
natM(t, p, gpi) = tempvec_a;
}
}
}
break;
case 2: // 2=growth,
tempvec_a.initialize();
warnstream << "Growth params averaging is not implemented, execution continues. " ;
write_message (WARN, 1);
break;
case 3: // 3=wtlen,
tempvec_a.initialize();
warnstream << "Weight/Length params averaging is not implemented, execution continues. " ;
write_message (WARN, 1);
break;
case 4: // 4=recr_dist&femfrac,
//get average and store in each fcast years
recr_dist_endyr.initialize();
for (y = ave_styr; y <= ave_endyr; y++)
for (gp = 1; gp <= N_GP * gender; gp++)
{
recr_dist_endyr(gp) += recr_dist(y, gp);
}
recr_dist_endyr /= N_ave_yrs;
for (y = endyr + 1; y <= YrMax; y++)
{
if (timevary_MG(y, 4) > 0)
{
warnstream << "mean recr_dist for forecast is incompatible with timevary recr_dist in forecast yr: " << y << "; user must adjust manually";
write_message(WARN, 0);
}
recr_dist(y) = recr_dist_endyr;
}
break;
case 5: // 5=migration,
for (j = 1; j <= do_migr2; j++)
{
tempvec_a.initialize();
for (y = ave_styr; y <= ave_endyr; y++)
{
tempvec_a += migrrate(y, j);
}
tempvec_a /= N_ave_yrs;
for (y = endyr + 1; y <= YrMax; y++)
migrrate(y, j) = tempvec_a;
}
break;
case 6: // 6=ageerror,
tempvec_a.initialize();
warnstream << "Age Error params averaging is not implemented, execution continues. " ;
write_message (WARN, 1);
break;
case 7: // 7=catchmult,
tempvec_a.initialize();
warnstream << "Catch mult params averaging is not implemented, execution continues. " ;
write_message (WARN, 1);
break;
case 8: // 8=hermaphroditism, and
tempvec_a.initialize();
warnstream << "Hermaphroditism params averaging is not implemented, execution continues. " ;
write_message (WARN, 1);
break;
case 9: // 9=maturity&fecundity
tempvec_a.initialize();
warnstream << "Maturity & fecundity params averaging is not implemented, execution continues. " ;
write_message (WARN, 1);
break;
case 10: // 9=selectivity
tempvec_a.initialize();
break;
}
}
}
// SS_Label_Info_7.5.2 #Set-up relative F among fleets and seasons for forecast
if (Fcast_RelF_Basis == 1) // set allocation according to range of years
{
temp = 0.0;
Fcast_RelF_Use.initialize();
for (int ff = 1; ff <= N_catchfleets(0); ff++)
{
f = fish_fleet_area(0, ff);
if (fleet_type(f) == 1 || (fleet_type(f) == 2 && bycatch_setup(f, 3) == 1))
{
for (y = Fcast_RelF_yr1; y <= Fcast_RelF_yr2; y++)
for (s = 1; s <= nseas; s++)
{
t = styr + (y - styr) * nseas + s - 1;
Fcast_RelF_Use(s, f) += Hrate(f, t);
}
}
}
temp = sum(Fcast_RelF_Use);
if (temp > 0.0)
{
Fcast_RelF_Use /= temp;
Fcurr_Fmult = temp / float(Fcast_RelF_yr2 - Fcast_RelF_yr1 + 1);
}
else
{
Fcast_RelF_Use(1, 1) = 1.0;
Fcurr_Fmult = 0.0;
}
}
else // Fcast_RelF_Basis==2 so set to values that were read
{
temp = 0.0;
for (f = 1; f <= Nfleet; f++)
for (s = 1; s <= nseas; s++)
{
temp += Fcast_RelF_Input(s, f);
Fcast_RelF_Use(s, f) = Fcast_RelF_Input(s, f);
}
// Fcast_RelF_Use=Fcast_RelF_Input/temp;
Fcast_RelF_Use /= temp;
Fcurr_Fmult = temp;
}
if (N_bycatch > 0)
{
for (f = 1; f <= Nfleet; f++)
for (s = 1; s <= nseas; s++)
{
if (Fcast_RelF_Use(s, f) == 0. && bycatch_setup(f, 3) > 0)
{
Fcast_RelF_Use(s, f) = 1.0e-6;
warnstream << "setting positive forecast relF for bycatch fleet: " << f;
write_message(ADJUST, 0);
}
}
}
if (N_Fcast_Input_Catches > 0)
{
for (f = 1; f <= Nfleet; f++)
for (s = 1; s <= nseas; s++)
{
if (Fcast_RelF_special(s, f) == 1 && Fcast_RelF_Use(s, f) == 0.0)
{
Fcast_RelF_Use(s, f) = 1.0e-6;
warnstream << "setting positive forecast relF for forecast only fleet: " << f;
write_message(ADJUST, 0);
}
}
}
} // end getting quantities for forecasts
// SS_Label_Info_7.5.3 #Calc average selectivity to use in benchmarks; store in styr-3
// Bmark_Yr(1,6)<<" Benchmark years: beg-end bio; beg-end selex; beg-end alloc"<<endl;
if (Do_Benchmark > 0)
{
// if(save_for_report>0 || last_phase() || current_phase()==max_phase || ((sd_phase() || mceval_phase()) && (initial_params::mc_phase==0)))
{
// calc average biology to use in equil; store in styr-3
temp = float(Bmark_Yr(2) - Bmark_Yr(1) + 1.); // get denominator
for (g = 1; g <= gmorph; g++)
if (use_morph(g) > 0)
{
for (s = 0; s <= nseas - 1; s++) // note -1 baked into the loop index
{
tempvec_a.initialize();
for (t = Bmark_t(1); t <= Bmark_t(2); t += nseas)
{
tempvec_a += Ave_Size(t + s, 1, g);
}
Ave_Size(styr - 3 * nseas + s, 1, g) = tempvec_a / temp;
tempvec_a.initialize();
for (t = Bmark_t(1); t <= Bmark_t(2); t += nseas)
{
tempvec_a += Ave_Size(t + s, mid_subseas, g);
}
Ave_Size(styr - 3 * nseas + s, mid_subseas, g) = tempvec_a / temp;
// get mean natM
int gpi=GP3(g);
for (int p = 0; p <= pop; p++)
{
tempvec_a.initialize();
for (t = Bmark_t(1); t <= Bmark_t(2); t += nseas)
{
tempvec_a += natM(t + s, p, gpi);
}
natM(styr - 3 * nseas + s, p, gpi) = tempvec_a / temp;
if(p>0)
{
int s1 = (p - 1)*nseas + s + 1;
surv1(s1, gpi) = mfexp(-natM(styr - 3 * nseas + s, p, gpi) * seasdur_half(s + 1)); // does all the gpi and ages
surv2(s1, gpi) = square(surv1(s1, gpi));
}
}
for (int kk = -2; kk <= 0; kk++) // get mean fecundity and pop body wt
{
tempvec_a.initialize();
for (t = Bmark_t(1); t <= Bmark_t(2); t += nseas)
{
tempvec_a += Wt_Age_t(t + s, kk, g);
}
Wt_Age_t(styr - 3 * nseas + s, kk, g) = tempvec_a / temp;
}
for (f = 1; f <= Nfleet; f++)
{
tempvec_a.initialize();
for (t = Bmark_t(1); t <= Bmark_t(2); t += nseas)
{
tempvec_a += save_sel_num(t + s, f, g);
}
save_sel_num(styr - 3 * nseas + s, f, g) = tempvec_a / temp;
}
}
}
if (pop > 0)
{
if (do_migration > 0)
{
temp = float(Bmark_Yr(8) - Bmark_Yr(7) + 1.); // get denominator
for (j = 1; j <= do_migr2; j++)
{
tempvec_a.initialize();
for (y = Bmark_Yr(7); y <= Bmark_Yr(8); y++)
{
tempvec_a += migrrate(y, j);
}
migrrate(styr - 3, j) = tempvec_a / temp;
}
}
// recr_dist_unf is accumulated while doing the time_series
// then its mean is calculated in Get_Benchmarks and assigned to recr_dist
// the SR_parm_bench is calculated from Bmark_yrs 9-10 in benchmark code using values stored in SR_parm_byyr
}
// calc average selectivity to use in equil; store in styr-3
temp = float(Bmark_Yr(4) - Bmark_Yr(3) + 1.); // get denominator
for (gg = 1; gg <= gender; gg++)
for (f = 1; f <= Nfleet; f++)
{
tempvec_l.initialize();
for (y = Bmark_Yr(3); y <= Bmark_Yr(4); y++)
{
tempvec_l += sel_l(y, f, gg);
}
sel_l(styr - 3, f, gg) = tempvec_l / temp;
tempvec_l.initialize();
for (y = Bmark_Yr(3); y <= Bmark_Yr(4); y++)
{
tempvec_l += sel_l_r(y, f, gg);
}
sel_l_r(styr - 3, f, gg) = tempvec_l / temp;
if (gg == gender)
{
exp_l_temp.initialize(); // use because dimensioned to nlength2
for (y = Bmark_Yr(3); y <= Bmark_Yr(4); y++)
{
exp_l_temp += retain(y, f);
}
retain(styr - 3, f) = exp_l_temp / temp;
exp_l_temp.initialize();
for (y = Bmark_Yr(3); y <= Bmark_Yr(4); y++)
{
exp_l_temp += discmort(y, f);
}
discmort(styr - 3, f) = exp_l_temp / temp;
}
tempvec_l.initialize();
for (y = Bmark_Yr(3); y <= Bmark_Yr(4); y++)
{
tempvec_l += discmort2(y, f, gg);
}
discmort2(styr - 3, f, gg) = tempvec_l / temp;
tempvec_a.initialize();
for (y = Bmark_Yr(3); y <= Bmark_Yr(4); y++)
{
tempvec_a += sel_a(y, f, gg);
}
sel_a(styr - 3, f, gg) = tempvec_a / temp;
tempvec_a.initialize();
for (y = Bmark_Yr(3); y <= Bmark_Yr(4); y++)
{
tempvec_a += discmort2_a(y, f, gg);
}
discmort2_a(styr - 3, f, gg) = tempvec_a / temp;
if (seltype(f + Nfleet, 2) > 0) // using age retention
{
tempvec_a.initialize();
for (y = Bmark_Yr(3); y <= Bmark_Yr(4); y++)
{
tempvec_a += retain_a(y, f, gg);
}
retain_a(styr - 3, f, gg) = tempvec_a / temp;
tempvec_a.initialize();
for (y = Bmark_Yr(3); y <= Bmark_Yr(4); y++)
{
tempvec_a += discmort_a(y, f, gg);
}
discmort_a(styr - 3, f, gg) = tempvec_a / temp;
}
}
// set-up relative F among fleets and seasons
if (Bmark_RelF_Basis == 1) // set allocation according to range of years
{
temp = 0.0;
Bmark_RelF_Use.initialize();
Bmark_HistF.initialize();
for (y = Bmark_Yr(5); y <= Bmark_Yr(6); y++)
for (f = 1; f <= Nfleet; f++)
if (fleet_type(f) == 1 || (fleet_type(f) == 2 && bycatch_setup(f, 3) == 1))
for (s = 1; s <= nseas; s++)
{
t = styr + (y - styr) * nseas + s - 1;
Bmark_HistF(s, f) += Hrate(f, t);
Bmark_RelF_Use(s, f) += Hrate(f, t);
}
Bmark_HistF /= float(Bmark_Yr(6) - Bmark_Yr(5) + 1.); // average F(s,f) across benchmark years
temp = sum(Bmark_RelF_Use);
// note that the relF calculation below is not conditional on whether a fleet is not bycatch and not non-optimized
// Fmult later calculated as multiplier times Bmark_relF_use and will compensate automatically
if (temp > 0.0)
{
Bmark_RelF_Use /= temp;
}
else
{
Bmark_RelF_Use(1, 1) = 1.0;
}
}
else // Bmark_RelF_Basis==2 so set same as forecast
{
Bmark_RelF_Use = Fcast_RelF_Use;
}
} // end being in a phase for these calcs
} // end getting quantities for benchmarks
}
FUNCTION void Get_Benchmarks(const int show_MSY)
{
//********************************************************************
/* SS_Label_FUNCTION 34 Get_Benchmarks(Find Fspr, MSY) */
int jj;
int Nloops;
int Nloops2;
// int bio_t;
int bio_t_base;
dvariable last_F1;
dvariable Closer;
dvariable Vbio1_unfished;
dvariable SPR_unfished;
dvariable Vbio_MSY;
dvariable Vbio1_MSY;
dvariable junk;
dvariable Nmid_c;
dvariable df;
dvariable BestYield;
dvariable BestF1;
dvariable FF;
dvariable dyld;
dvariable dyldp;
dvariable Fmax;
dvariable bestF1;
dvariable bestF2;
dvariable F01_origin;
dvariable F01_second;
dvariable F01_actual;
dvar_vector F1(1, 3);
dvar_vector F2(1, 3);
dvar_vector yld1(1, 3);
dvar_vector Fmult_save(1, 3);
write_bodywt_save = write_bodywt;
write_bodywt = 0;
Nloops2 = 0;
y = styr - 3; // the average biology from specified benchmark years is stored here
yz = y;
bio_yr = y;
eq_yr = y;
t_base = y + (y - styr) * nseas - 1;
bio_t_base = styr + (bio_yr - styr) * nseas - 1;
// set the Hrate for bycatch fleets so not scaled with other fleets
// bycatch_F(f,s) is created here for use in forecast
for (f = 1; f <= Nfleet; f++)
{
if (fleet_type(f) == 2 && bycatch_setup(f, 3) == 2) // set rate to input value
{
for (s = 1; s <= nseas; s++)
{
t = bio_t_base + s;
Hrate(f, t) = bycatch_setup(f, 4);
bycatch_F(f, s) = bycatch_setup(f, 4);
}
}
else if (fleet_type(f) == 2 && bycatch_setup(f, 3) == 3) // set rate to historical mean
{
for (s = 1; s <= nseas; s++)
{
t = bio_t_base + s;
Hrate(f, t) = 0.0;
for (int yy = bycatch_setup(f, 4); yy <= bycatch_setup(f, 5); yy++)
{
Hrate(f, t) += Hrate(f, styr + (yy - styr) * nseas + s - 1);
}
Hrate(f, t) /= (bycatch_setup(f, 5) - bycatch_setup(f, 4) + 1.);
bycatch_F(f, s) = Hrate(f, t);
}
}
else
{
for (s = 1; s <= nseas; s++)
{
t = bio_t_base + s;
Hrate(f, t) = 0.0;
}
}
}
if (show_MSY == 1)
{
report5 << version_info << endl
<< ctime(&start);
report5 << "Bmark_relF(by_fleet_&seas) (excluding non-scaled bycatch fleets)" << endl
<< Bmark_RelF_Use << endl
<< "#" << endl;
report5 << "Bmark_histF(by_fleet_&seas)" << endl
<< Bmark_HistF << endl
<< "#" << endl;
report5 << "Bycatch_F" << endl
<< trans(bycatch_F) << endl
<< "#" << endl;
report5 << "YPR_mask for including catch: " << endl
<< YPR_mask << endl;
}
if (show_MSY == 2)
{
// do not recalc the age-specific vectors
}
else // recalc age specific biology and selectivity. NOTE: not density-dependent!!
{
for (s = 1; s <= nseas; s++)
{
t = styr - 3 * nseas + s - 1;
subseas = 1; // for begin of season
ALK_idx = (s - 1) * N_subseas + subseas;
ALK_subseas_update(ALK_idx) = 1; // new in 3.30.12 force updating
Make_AgeLength_Key(s, subseas); // begin season
subseas = mid_subseas;
ALK_idx = (s - 1) * N_subseas + subseas;
ALK_subseas_update(ALK_idx) = 1; // new in 3.30.12 force updating
Make_AgeLength_Key(s, subseas);
// SPAWN-RECR: call make_fecundity for benchmarks
if (s == spawn_seas)
{
{
fec = Wt_Age_t(styr - 3 * nseas + s - 1, -2);
}
}
Wt_Age_beg(s) = Wt_Age_t(styr - 3 * nseas + s - 1, 0);
}
// following uses the values of sel_l, sel_a, etc. stored in yr=styr-3
for (s = 1; s <= nseas; s++)
for (g = 1; g <= gmorph; g++)
if (use_morph(g) > 0)
{
ALK_idx = (s - 1) * N_subseas + mid_subseas; // for midseason
Make_FishSelex(); // including sel_dead_num
}
if (show_MSY == 1)
{
report5 << version_info << endl
<< ctime(&start);
report5 << "Bmark_relF(by_fleet_&seas) (excluding non-scaled bycatch fleets)" << endl
<< Bmark_RelF_Use << endl
<< "#" << endl;
report5 << "Bmark_histF(by_fleet_&seas)" << endl
<< Bmark_HistF << endl
<< "#" << endl;
report5 << "Bycatch_F" << endl
<< trans(bycatch_F) << endl
<< "#" << endl;
report5 << "YPR_mask for including catch: " << endl
<< YPR_mask << endl;
report5 << "Fecundity: " << fec(1) << endl;
for (f = 1; f <= Nfleet; f++)
{
if (fleet_type(f) <= 2)
{
for (s = 1; s <= nseas; s++)
{
report5 << f << " " << s << " sel_bio: " << sel_bio(s, f, 1) << endl;
report5 << f << " " << s << " sel_dead_bio: " << sel_dead_bio(s, f, 1) << endl;
}
}
}
for (f = 1; f <= Nfleet; f++)
{
if (fleet_type(f) <= 2)
{
for (s = 1; s <= nseas; s++)
report5 << f << " " << s << " sel_num: " << sel_num(s, f, 1) << endl;
}
}
for (f = 1; f <= Nfleet; f++)
{
if (fleet_type(f) <= 2)
{
for (s = 1; s <= nseas; s++)
report5 << f << " " << s << " sel_dead_num: " << sel_dead_num(s, f, 1) << endl;
}
}
}
}
maxpossF.initialize();
for (g = 1; g <= gmorph; g++)
{
for (s = 1; s <= nseas; s++)
{
tempvec_a.initialize();
for (f = 1; f <= Nfleet; f++)
{
tempvec_a += Bmark_RelF_Use(s, f) * sel_dead_num(s, f, g);
}
temp = max(tempvec_a);
if (temp > maxpossF)
maxpossF = temp;
}
}
maxpossF = max_harvest_rate / maxpossF; // applies to any F_method
// SPAWN-RECR: notes regarding virgin vs. benchmark biology usage in spawn-recr
// the spawner-recruitment function has Bzero based on virgin biology, not benchmark biology
// need to deal with possibility that with time-varying biology, the SSB_virgin calculated from virgin conditions will differ from the SSB_virgin used for benchmark conditions
// note that recr_dist(styr-3), updated at end of ss_popdyn.
for (j = 1; j <= N_SRparm2; j++)
{
if (SR_parm_timevary(j) == 0)
{
SR_parm_work(j) = SR_parm(j);
}
else
{
temp = 0.;
for (int y = Bmark_Yr(9); y <= Bmark_Yr(10); y++)
{
temp += SR_parm_byyr(y, j);
}
SR_parm_work(j) = temp / (Bmark_Yr(10) - Bmark_Yr(9) + 1.);
}
}
Fishon = 0;
Recr_unf = mfexp(SR_parm_work(1));
Do_Equil_Calc(Recr_unf);
SSB_unf = SSB_equil;
SR_parm_work(N_SRparm2 + 1) = SSB_unf;
if (show_MSY == 1)
report5 << "SR_parm for benchmark: " << SR_parm_work << endl
<< "for years: " << Bmark_Yr(9) << " " << Bmark_Yr(10) << " SSB_virgin was: " << SSB_virgin << endl;
if (show_MSY == 1)
report5 << "Repro_output_by_age_for_morph_1: " << fec(1) << endl;
Mgmt_quant(1) = SSB_unf;
Mgmt_quant(2) = totbio;
Mgmt_quant(3) = smrybio;
Mgmt_quant(4) = Recr_unf;
// find Fspr SS_Label_710
{
if (show_MSY == 1)
{
report5 << "#" << endl
<< "find_target_SPR" << endl;
report5 << "SPR_is_spawner_potential_ratio=(fishedSSB/R)/(unfishedSSB/R))" << endl;
report5 << "Iter Fmult ann_F SPR tot_catch";
for (p = 1; p <= pop; p++)
for (gp = 1; gp <= N_GP; gp++)
{
report5 << " SSB_Area:" << p << "_GP:" << gp;
}
report5 << endl;
}
Fmult = 0.;
Nloops = 18;
Closer = 1.;
F1(1) = log(1.0e-3);
last_calc = 0.;
Fchange = -4.0;
equ_Recr = 1.0;
Fishon = 0;
dvariable SPR_target100;
SPR_target100 = SPR_target * 100.;
Do_Equil_Calc(equ_Recr);
SPR_unfished = SSB_unf / Recr_unf; // this corresponds to the biology for benchmark average years, not the virgin SSB_virgin
Vbio1_unfished = smrybio; // gets value from equil_calc
if (show_MSY == 1)
{
report5 << "0 0 0 1 0";
for (p = 1; p <= pop; p++)
for (gp = 1; gp <= N_GP; gp++)
{
report5 << " " << SSB_equil_pop_gp(p, gp);
}
report5 << endl;
}
df = 1.e-5;
Fishon = 1;
for (j = 1; j <= Nloops; j++) // loop find Fspr
{
if (fabs(Fchange) <= 0.25)
{
jj = 3;
F1(2) = F1(1) + df * .5;
F1(3) = F1(2) - df;
}
else
{
jj = 1;
}
for (int ii = jj; ii >= 1; ii--)
{
Fmult = 40.00 / (1.0 + mfexp(-F1(ii)));
for (f = 1; f <= Nfleet; f++)
{
if (fleet_type(f) == 1 || (fleet_type(f) == 2 && bycatch_setup(f, 3) == 1))
{
for (int s = 1; s <= nseas; s++)
{
Hrate(f, bio_t_base + s) = Fmult * Bmark_RelF_Use(s, f);
}
}
// else Hrate for bycatch fleets already set
}
Fishon = 1;
Do_Equil_Calc(equ_Recr);
yld1(ii) = 100. * SSB_equil / SPR_unfished; // spawning potential ratio
}
SPR_actual = yld1(1); // spawning potential ratio
if (jj == 3)
{
Closer *= 0.5;
dyld = (yld1(2) - yld1(3)) / df; // First derivative (to find the root of this)
if (dyld != 0.)
{
last_F1 = F1(1);
F1(1) += (SPR_target100 - SPR_actual) / (dyld + 0.001);
F1(1) = (1. - Closer) * F1(1) + Closer * last_F1;
} // averages with last good value to keep from changing too fast
else
{
F1(1) = (F1(1) + last_F1) * 0.5;
} // go halfway back towards previous value
}
else
{
// if((last_calc-SPR_target)*(SPR_actual-SPR_target)<0.0) {Fchange*=-0.5;} // changed sign, so reverse search direction
temp = (last_calc - SPR_target100) * (SPR_actual - SPR_target100) / (sfabs(last_calc - SPR_target100) * sfabs(SPR_actual - SPR_target100)); // values of -1 or 1
temp1 = temp - 1.; // values of -2 or 0
Fchange *= exp(temp1 / 4.) * temp;
F1(1) += Fchange;
last_calc = SPR_actual;
}
if (show_MSY == 1)
{
report5 << j << " " << Fmult << " " << equ_F_std << " " << SPR_actual / 100. << " " << sum(equ_catch_fleet(2));
for (p = 1; p <= pop; p++)
for (gp = 1; gp <= N_GP; gp++)
{
report5 << " " << SSB_equil_pop_gp(p, gp);
}
report5 << endl;
}
} // end search loop
if (show_MSY == 1)
{
if (fabs(SPR_actual - SPR_target100) >= 0.1)
{
warnstream << "poor convergence in Fspr search " << SPR_target << " " << SPR_actual / 100.;
write_message(WARN, 0);
}
if (SPR_actual / SPR_target100 >= 1.01)
{
warnstream << "Fmult = " << Fmult << " cannot get high enough to achieve low SPR target: " << SPR_target << "; SPR achieved is: " << SPR_actual / 100.;
write_message(WARN, 0);
}
report5 << "seas fleet Hrate encB deadB retB encN deadN retN: " << endl;
for (s = 1; s <= nseas; s++)
for (f = 1; f <= Nfleet; f++)
if (fleet_type(f) <= 2)
{
report5 << s << " " << f << " " << Hrate(f, bio_t_base + s);
for (g = 1; g <= 6; g++)
{
report5 << " " << equ_catch_fleet(g, s, f);
}
report5 << endl;
}
}
// SPAWN-RECR: calc equil spawn-recr in YPR; need to make this area-specific
Equ_SpawnRecr_Result = Equil_Spawn_Recr_Fxn(SR_parm_work(2), SR_parm_work(3), SSB_unf, Recr_unf, SSB_equil); // returns 2 element vector containing equilibrium biomass and recruitment at this SPR
Bspr = Equ_SpawnRecr_Result(1);
Bspr_rec = Equ_SpawnRecr_Result(2);
YPR_spr_enc = YPR_enc; // total encountered yield per recruit
YPR_spr_dead = YPR_dead; // total dead yield per recruit
YPR_spr_N_dead = YPR_N_dead;
YPR_spr_ret = YPR_ret;
YPR_spr_cost = Cost;
YPR_spr_revenue = (PricePerF * YPR_val_vec) * Equ_SpawnRecr_Result(2); // vector*vector*scalar
YPR_spr_profit = YPR_spr_revenue - Cost;
SPR_Fmult = Fmult;
if (rundetail > 0 && mceval_counter == 0 && show_MSY == 1)
{
echoinput << "Calculated Fspr " << SPR_Fmult << " " << SPR_actual / 100. << endl;
}
Vbio_spr = totbio;
Vbio1_spr = smrybio;
Mgmt_quant(10) = equ_F_std;
Mgmt_quant(9) = Equ_SpawnRecr_Result(1);
Mgmt_quant(11) = YPR_dead * Equ_SpawnRecr_Result(2);
} // end finding Fspr
if (Do_Benchmark == 2) // Find F0.1
{
equ_Recr = 1.0;
Fishon = 1;
// get slope at origin
// Fmult=0.000001;
Fmult = 0.001;
for (f = 1; f <= Nfleet; f++)
{
if (fleet_type(f) == 1 || (fleet_type(f) == 2 && bycatch_setup(f, 3) == 1))
{
for (int s = 1; s <= nseas; s++)
{
Hrate(f, bio_t_base + s) = Fmult * Bmark_RelF_Use(s, f);
}
}
// else Hrate for bycatch fleets already set
}
Do_Equil_Calc(equ_Recr);
F01_origin = YPR_opt / Fmult;
BTGT_target = 0.1; // now relative to Bmark
Btgttgt = F01_origin * 0.1;
if (show_MSY == 1)
{
report5 << "#" << endl
<< "#Find_F0.1; slope_at_origin_wrt_Fmult: " << F01_origin << " " << YPR_opt << " " << Hrate(1, bio_t_base + 3) << endl;
report5 << "Iter Fmult ann_F SPR YPR YPR_slope YPR_curvature" << endl;
}
Nloops = 20;
Closer = 0.75;
F1(1) = SPR_Fmult * 0.1;
for (j = 1; j <= Nloops; j++) // loop to find F0.1
{
df = 0.01 * F1(1);
F1(2) = F1(1) + df * .5;
F1(3) = F1(2) - df;
for (int ii = 3; ii >= 1; ii--)
{
for (f = 1; f <= Nfleet; f++)
{
if (fleet_type(f) == 1 || (fleet_type(f) == 2 && bycatch_setup(f, 3) == 1))
{
for (int s = 1; s <= nseas; s++)
{
Hrate(f, bio_t_base + s) = F1(ii) * Bmark_RelF_Use(s, f);
}
} // else Hrate for bycatch fleets set above
}
Do_Equil_Calc(equ_Recr);
yld1(ii) = YPR_opt;
}
F01_actual = (yld1(2) - yld1(3)) / (F1(2) - F1(3));
F01_second = ((yld1(2) - yld1(1)) / (F1(2) - F1(1)) - (yld1(1) - yld1(3)) / (F1(1) - F1(3))) / (F1(2) - F1(3));
last_F1 = F1(1);
if (show_MSY == 1)
{
report5 << j << " " << F1(1) << " " << equ_F_std << " " << SSB_equil / SPR_unfished << " " << YPR_opt << " " << F01_actual << " " << F01_second << " last F1 " << last_F1 << " Closer " << Closer << " delta " << (F01_origin * 0.1 - F01_actual) / (F01_second) << endl;
}
F1(1) += (F01_origin * 0.1 - F01_actual) / (F01_second);
F1(1) = (1. - Closer) * F1(1) + Closer * last_F1;
Closer *= 0.75;
} // end search loop
if (show_MSY == 1)
{
if (sfabs(F01_origin * 0.1 - F01_actual) >= 0.001)
{
warnstream << "poor convergence in F0.1 search target= " << F01_origin * 0.1 << " actual= " << F01_actual;
write_message(WARN, 0);