forked from nmfs-ost/ss3-source-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSS_selex.tpl
2082 lines (1989 loc) · 92.2 KB
/
SS_selex.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 #11. **SS_selex.tpl**
// SS_Label_file # * <u>get_selectivity()</u> // does length and age selectivity and retention for all fleets
// SS_Label_file # * <u>make_fish_selex()</u> // for all fleets in a particular season, does the dot product of length distribution with length selectivity and retention vectors to calculate equivalent mean quantities at age for each platoon
FUNCTION void get_selectivity()
{
//*******************************************************************
/* SS_Label_Function_22 #get_selectivity */
// SS_Label_Info_22.01 #define local variables for selectivity
int fs; // = f-Nfleet to allow reference of actual fleet when looping to 2*Nfleet for the age selectivity calculations
int scaling_offset = 0;
dvariable t1;
dvariable t2;
dvariable t3;
dvariable t4;
dvariable Apical_Selex;
dvariable t1min;
dvariable t1max;
dvariable t1power;
dvariable t2min;
dvariable t2max;
dvariable t2power;
dvariable final;
dvariable sel_maxL;
dvariable lastsel;
dvariable lastSelPoint;
dvariable SelPoint;
dvariable finalSelPoint;
dvariable asc;
dvariable dsc;
dvar_vector sp(1, 199); // temporary vector for selex parms
Ip = 0;
// SS_Label_Info_22.2 #Loop all fisheries and surveys twice; first for size selectivity, then for age selectivity
for (f = 1; f <= 2 * Nfleet; f++)
{
fs = f - Nfleet; //index for saving age selex in the fleet arrays
// SS_Label_Info_22.2.1 #recalculate selectivity for any fleets or surveys with time-vary flag set for this year
if (timevary_sel(y, f) == 1 || save_for_report > 0)
{ // recalculate the selex in this year x type
if (N_selparmvec(f) > 0) // type has parms, so look for adjustments
{
for (j = 1; j <= N_selparmvec(f); j++)
{
if (selparm_timevary(Ip + j) == 0)
{
sp(j) = selparm(Ip + j);
}
else // time-varying
{
sp(j) = parm_timevary(selparm_timevary(Ip + j), y);
if (parm_adjust_method == 1 && (save_for_report > 0 || do_once == 1))
{
if (sp(j) > -999 && (sp(j) < selparm_1(Ip + j, 1) || sp(j) > selparm_1(Ip + j, 2)))
{
N_warn++;
warning << N_warn << " "
<< " adjusted selparm out of base parm bounds. Phase: " << current_phase() << "; Inter: " << niter << "; fleet: " << f << "; base_parm#: " << Ip + j << "; y: " << y << "; min: " << selparm_1(Ip + j, 1) << "; max: " << selparm_1(Ip + j, 2) << "; base: " << selparm(Ip + j) << " timevary_val: " << sp(j) << " " << ParmLabel(firstselparm + Ip + j) << endl;
}
}
}
} // end j parm loop
if (docheckup == 1 && f <= Nfleet)
echoinput << " len selex parms for fleet: " << f << " " << endl
<< sp(1, N_selparmvec(f)) << endl;
if (docheckup == 1 && f > Nfleet)
echoinput << " age selex parms for fleet: " << f - Nfleet << " " << endl
<< sp(1, N_selparmvec(f)) << endl;
if (save_for_report > 0 || do_once == 1)
{
for (j = 1; j <= N_selparmvec(f); j++)
save_sp_len(y, f, j) = sp(j);
}
}
if (f <= Nfleet) // do size selectivity, retention, discard mort
{
for (gg = 1; gg <= gender; gg++)
{
if (gg == 1 || (gg == 2 && seltype(f, 3) >= 3))
{
// SS_Label_Info_22.3 #Switch on size selectivity type
switch (seltype(f, 1)) // select the selectivity pattern
{
// SS_Label_Info_22.3.0 #case 0 constant size selectivity
case 0: // *********** constant
{
sel = 1.;
break;
}
// SS_Label_Info_22.3.1 #case 1 logistic size selectivity
case 1:
{
if (seltype(f, 3) < 3 || (gg == 1 && seltype(f, 3) == 3) || (gg == 2 && seltype(f, 3) == 4)) // do the primary gender
{
sel = 1. / (1. + mfexp(neglog19 * (len_bins_m - sp(1)) / sp(2)));
}
else // do the offset gender
{
temp = sp(1) + sp(Maleselparm(f));
temp1 = sp(2) + sp(Maleselparm(f) + 1);
sel = sp(Maleselparm(f) + 2) / (1. + mfexp(neglog19 * (len_bins_m - temp) / temp1));
}
break;
}
// SS_Label_Info_22.3.2 #case 2 size selectivity using double_normal_plateau and lots of bells and whistles; old version of 24 available for back compatibility
case 2:
// SS_Label_Info_22.3.24 #case 24 size selectivity using double_normal_plateau and lots of bells and whistles
case 24:
{
if (seltype(f, 3) < 3 || (gg == 1 && seltype(f, 3) == 3) || (gg == 2 && seltype(f, 3) == 4))
{
peak = sp(1);
upselex = mfexp(sp(3));
downselex = mfexp(sp(4));
final = sp(6);
Apical_Selex = 1.;
}
else
{ // offset male parameters if seltype(f,3)==3, female parameters if seltype(f,3)==4
peak = sp(1) + sp(Maleselparm(f));
upselex = mfexp(sp(3) + sp(Maleselparm(f) + 1));
downselex = mfexp(sp(4) + sp(Maleselparm(f) + 2));
if (sp(6) > -999.)
final = sp(6) + sp(Maleselparm(f) + 3);
Apical_Selex = sp(Maleselparm(f) + 4);
}
if (sp(5) < -1000.)
{
j1 = -1001 - int(value(sp(5))); // selex is nil thru bin j1, so set sp(5) equal to first bin with selex (e.g. -1002 to start selex at bin 2)
sel(1, j1) = 1.0e-06;
}
else
{
j1 = startbin - 1; // start selex at bin equal to min sizecomp databin (=j1+1)
if (sp(5) > -999)
{
point1 = 1.0 / (1.0 + mfexp(-sp(5)));
t1min = mfexp(-(square(len_bins_m(startbin) - peak) / upselex)); // fxn at first bin
}
}
if (sp(6) < -1000.)
{
j2 = -1000 - int(value(sp(6))); // selex is constant beyond this sizebin, so set sp(6) equal to last bin with estimated selex
}
else
{
j2 = nlength;
}
peak2 = peak + binwidth2 + (0.99 * len_bins_m(j2) - peak - binwidth2) / (1. + mfexp(-sp(2)));
if (sp(6) > -999)
{
if (seltype(f, 1) == 24)
{
point2 = Apical_Selex / (1.0 + mfexp(-final)); // The newer way of doing this
}
else
{
point2 = 1.0 / (1.0 + mfexp(-final)); // For back compatibility (old (3.30.18 and earlier) version of pattern 24, now available as 2)
#ifdef DO_ONCE
if (do_once == 1)
{
N_warn++;
warning << N_warn << " "
<< "Note: Selectivity 2 is a back-compatible (SS 3.30.18 and earlier) version of selectivity 24. Recommend using 24." << endl;
}
#endif
}
t2min = mfexp(-(square(len_bins_m(j2) - peak2) / downselex)); // fxn at last bin
}
for (j = j1 + 1; j <= j2; j++)
{
t1 = len_bins_m(j) - peak;
t2 = len_bins_m(j) - peak2;
join1 = 1.0 / (1.0 + mfexp(-(20. * t1 / (1.0 + fabs(t1))))); // note the logit transform on t1 causes range of mfexp to be over -20 to 20
join2 = 1.0 / (1.0 + mfexp(-(20. * t2 / (1.0 + fabs(t2)))));
if (sp(5) > -999)
{
asc = point1 + (Apical_Selex - point1) * (mfexp(-square(t1) / upselex) - t1min) / (1.0 - t1min);
}
else
{
asc = Apical_Selex * mfexp(-square(t1) / upselex);
}
if (sp(6) > -999)
{
dsc = Apical_Selex + (point2 - Apical_Selex) * (mfexp(-square(t2) / downselex) - 1.0) / (t2min - 1.0);
}
else
{
dsc = Apical_Selex * mfexp(-square(t2) / downselex);
}
sel(j) = asc * (1.0 - join1) + join1 * (Apical_Selex * (1.0 - join2) + dsc * join2);
}
if (startbin > 1 && sp(5) >= -1000.)
{
for (j = 1; j <= startbin - 1; j++)
{
sel(j) = square(len_bins_m(j) / len_bins_m(startbin)) * sel(startbin);
}
}
if (j2 < nlength)
{
sel(j2 + 1, nlength) = sel(j2);
}
break;
}
// SS_Label_Info_22.3.3 #case 3 discontinued
case 3:
{
N_warn++;
cout << "EXIT - see warning" << endl;
warning << N_warn << " "
<< "Selectivity pattern 3 discontinued. Use a different pattern." << endl;
exit(1);
break;
}
// SS_Label_Info_22.3.4 #case 4 discontinued; use pattern 30 to get spawning biomass
case 4:
{
N_warn++;
cout << "EXIT - see warning" << endl;
warning << N_warn << " "
<< "Selectivity pattern 4 discontinued; use pattern 0 and special survey units 30 in data file instead." << endl;
exit(1);
break;
}
// SS_Label_Info_22.3.5 #case 5 mirror another fleets size selectivity for specified bin range
// use only the specified bin range using mirror_mask created upon read
// must refer to a lower numbered type (f)
case 5:
{
sel = elem_prod(mirror_mask(f), sel_l(y, seltype(f, 4), 1));
break;
}
// SS_Label_Info_22.3.15 #case 15 mirror another fleets size selectivity for all size bins
case 15:
{
sel = sel_l(y, seltype(f, 4), 1);
break;
}
// SS_Label_Info_22.3.6 #case 6 non-parametric size selex pattern
// #43 non-parametric size selex scaled by average of values at low bin through high bin
case 43:
scaling_offset = 2;
[[fallthrough]];
case 6:
{
lastsel = -10.0; // log(selex) for first bin;
lastSelPoint = len_bins_m(1); // first size
finalSelPoint = value(sp(2 + scaling_offset)); // size beyond which selex is constant
SelPoint = value(sp(1 + scaling_offset)); // first size that will get a parameter. Value will get incremented by step interval (temp1)
z = 3 + scaling_offset; // parameter counter
temp1 = (finalSelPoint - SelPoint) / (seltype(f, 4) - 1.0); // step interval
for (j = 1; j <= nlength; j++)
{
if (len_bins_m(j) < SelPoint)
{
tempvec_l(j) = lastsel + (len_bins_m(j) - lastSelPoint) / (SelPoint - lastSelPoint) * (sp(z) - lastsel);
}
else if (len_bins_m(j) == SelPoint)
{
tempvec_l(j) = sp(z);
lastsel = sp(z);
lastSelPoint = SelPoint;
SelPoint += temp1;
if (SelPoint <= finalSelPoint)
{
z++;
}
else
{
SelPoint = finalSelPoint;
}
}
else if (len_bins_m(j) <= finalSelPoint)
{
lastsel = sp(z);
lastSelPoint = SelPoint;
SelPoint += temp1;
if (SelPoint <= finalSelPoint)
{
z++;
}
else
{
SelPoint = finalSelPoint;
}
tempvec_l(j) = lastsel + (len_bins_m(j) - lastSelPoint) / (SelPoint - lastSelPoint) * (sp(z) - lastsel);
}
else
{
tempvec_l(j) = sp(z);
}
#ifdef DO_ONCE
if (do_once == 1)
echoinput << "selex42 " << j << " " << len_bins_m(j) << " " << SelPoint << " " << tempvec_l(j) << endl;
#endif
}
if (scaling_offset == 0)
{
temp = max(tempvec_l);
}
else
{
int low_bin = int(value(sp(1)));
int high_bin = int(value(sp(2)));
if (low_bin < 1)
{
low_bin = 1;
N_warn++;
warning << N_warn << " "
<< " selex pattern 43; value for low bin is less than 1, so set to 1 " << endl;
}
if (high_bin > nlength)
{
high_bin = nlength;
N_warn++;
warning << N_warn << " "
<< " selex pattern 43; value for high bin is greater than " << nlength << ", so set to " << nlength << " " << endl;
}
if (high_bin < low_bin)
high_bin = low_bin;
if (low_bin > high_bin)
low_bin = high_bin;
sp(1) = low_bin;
sp(2) = high_bin;
temp = mean(tempvec_l(low_bin, high_bin));
scaling_offset = 0; // reset scaling offset
}
sel = mfexp(tempvec_l - temp);
break;
}
// SS_Label_Info_22.3.7 #case 7 discontinued; use pattern 8 for double logistic
case 7:
{
N_warn++;
cout << " EXIT - see warning " << endl;
warning << N_warn << " "
<< "Selectivity pattern 7 discontinued; use pattern 8 instead for double logistic, but recommend pattern 24. " << endl;
exit(1);
break;
}
// SS_Label_Info_22.3.8 #case 8 double logistic with eight parameters
case 8:
// 1=peak, 2=init, 3=infl, 4=slope, 5=final, 6=infl2, 7=slope2 8=binwidth; Mirror=1===const_above_Linf
{
#ifdef DO_ONCE
if (do_once == 1)
{
N_warn++;
warning << N_warn << " "
<< "Selectivity pattern 24 is recommended over pattern 8 because it has fewer parameters." << endl;
}
#endif
t1 = minL + (1. / (1. + mfexp(-sp(3)))) * (sp(1) - minL); // INFL
t1min = 1. / (1. + mfexp(-mfexp(sp(4)) * (minL - t1))) * 0.9999; // asc value at minsize
t1max = 1. / (1. + mfexp(-mfexp(sp(4)) * (sp(1) - t1))) * 1.0001; // asc value at peak
t1power = log(0.5) / log((0.5 - t1min) / (t1max - t1min)); // so the parameter will actual correspond to 50% point
if (seltype(f, 4) == 0)
{
sel_maxL = maxL;
}
else
{
sel_maxL = Ave_Size(styr, 1, 1, nages);
}
t2 = (sp(1) + sp(8)) + (1. / (1. + mfexp(-sp(6)))) * (sel_maxL - (sp(1) + sp(8))); // INFL
t2min = 1. / (1. + mfexp(-mfexp(sp(7)) * (sp(1) + sp(8) - t2))) * 0.9999; // asc value at peak+
t2max = 1. / (1. + mfexp(-mfexp(sp(7)) * (sel_maxL - t2))) * 1.0001; // asc value at maxL
t2power = log(0.5) / log((0.5 - t2min) / (t2max - t2min));
final = 1. / (1. + mfexp(-sp(5)));
for (j = 1; j <= nlength; j++) //calculate the value over length bins
{
join1 = 1. / (1. + mfexp(10. * (len_bins_m(j) - sp(1))));
join2 = 1. / (1. + mfexp(10. * (len_bins_m(j) - (sp(1) + sp(8)))));
join3 = 1. / (1. + mfexp(10. * (len_bins_m(j) - sel_maxL)));
upselex = sp(2) + (1. - sp(2)) * pow(((1. / (1. + mfexp(-mfexp(sp(4)) * (len_bins_m(j) - t1))) - t1min) / (t1max - t1min)), t1power);
downselex = (1. + (final - 1.) * pow(fabs((((1. / (1. + mfexp(-mfexp(sp(7)) * (len_bins_m(j) - t2))) - t2min) / (t2max - t2min)))), t2power));
sel(j) = ((((upselex * join1) + 1.0 * (1.0 - join1)) * join2) + downselex * (1 - join2)) * join3 + final * (1 - join3);
} // end size bin loop
break;
} // end New double logistic
// SS_Label_Info_22.3.9 #case 9 old double logistic with 4 parameters
case 9:
{
k1 = int(value(sp(5)));
if (k1 > 1)
sel(1, k1 - 1) = 0.0;
sel(k1, nlength) = elem_prod((1 / (1 + mfexp(-sp(2) * (len_bins_m(k1, nlength) - sp(1))))),
(1 - 1 / (1 + mfexp(-sp(4) * (len_bins_m(k1, nlength) - (sp(1) * sp(6) + sp(3)))))));
sel += 1.0e-6;
sel /= max(sel);
break;
}
// SS_Label_Info_22.3.11 #case 11 selex=1.0 within a range of lengths
case 11:
{
sel = mirror_mask(f); // (y,f,1)
break;
}
// SS_Label_Info_22.3.21 #case 21 non-parametric size selectivity
/* N points; where the first N parameters is vector of sizes for the line segment ends
and second N parameters is selectivity at that size (no transformations) */
case 21: // *******New non-parametric
{
j = 1;
z = 1;
k = seltype(f, 4); // N points
lastsel = 0.0;
lastSelPoint = 0.0;
#ifdef DO_ONCE
if (do_once == 1)
{
if (sp(k) > len_bins(nlength))
{
N_warn++;
cout << " EXIT - see warning " << endl;
warning << N_warn << " "
<< "Selex21: cannot have max selpoint > max_pop_lenbin" << endl;
exit(1);
}
if (sp(k - 1) > len_bins(nlength - 1))
{
N_warn++;
warning << N_warn << " "
<< "Selex21: should not have selpoint(n-1) > pop_lenbin(nlength-1)" << endl;
}
}
#endif
while (j <= nlength)
{
if (len_bins(j) <= sp(z))
{
sel(j) = lastsel + (len_bins(j) - lastSelPoint) / (sp(z) - lastSelPoint) * (sp(z + k) - lastsel);
j++;
}
else if (z <= k)
{
lastSelPoint = sp(z);
lastsel = sp(z + k);
z++;
}
else // for sizes beyond last point
{
sel(j) = sp(k + k);
j++;
}
}
break;
}
// SS_Label_Info_22.3.22 #case 22 size selectivity using double_normal_plateau (similar to CASAL)
case 22:
{
peak2 = sp(1) + (0.99 * len_bins(nlength) - sp(1)) / (1. + mfexp(-sp(2)));
upselex = mfexp(sp(3));
downselex = mfexp(sp(4));
for (j = 1; j <= nlength; j++)
{
t1 = len_bins_m(j);
if (t1 < sp(1))
{
sel(j) = mfexp(-square(t1 - sp(1)) / upselex);
}
else if (t1 <= peak2)
{
sel(j) = 1.0;
}
else
{
sel(j) = mfexp(-square(t1 - peak2) / downselex);
}
}
break;
}
// SS_Label_Info_22.3.23 #case 23 size selectivity double_normal_plateau where final value can be greater than 1.0
/* cannot be used with Pope's because can cause selex to be >1.0 */
case 23:
{
if (seltype(f, 3) < 3 || (gg == 1 && seltype(f, 3) == 3) || (gg == 2 && seltype(f, 3) == 4))
{
peak = sp(1);
upselex = mfexp(sp(3));
downselex = mfexp(sp(4));
final = sp(6);
}
else
{ // offset male parameters if seltype(f,3)==3
peak = sp(1) + sp(Maleselparm(f));
upselex = mfexp(sp(3) + sp(Maleselparm(f) + 1));
downselex = mfexp(sp(4) + sp(Maleselparm(f) + 2));
if (sp(6) > -999.)
final = sp(6) + sp(Maleselparm(f) + 3);
}
if (sp(5) < -1000.)
{
j1 = -1001 - int(value(sp(5))); // selex is nil thru bin j1, so set sp(5) equal to first bin with selex (e.g. -1002 to start selex at bin 2)
sel(1, j1) = 1.0e-06;
}
else
{
j1 = startbin - 1; // start selex at bin equal to min sizecomp databin (=j1+1)
if (sp(5) > -999)
{
point1 = 1.0 / (1.0 + mfexp(-sp(5)));
t1min = mfexp(-(square(len_bins_m(startbin) - peak) / upselex)); // fxn at first bin
}
}
if (sp(6) < -1000.)
{
j2 = -1000 - int(value(sp(6))); // selex is constant beyond this sizebin, so set sp(6) equal to last bin with estimated selex
}
else
{
j2 = nlength;
}
peak2 = peak + binwidth2 + (0.99 * len_bins_m(j2) - peak - binwidth2) / (1. + mfexp(-sp(2)));
if (sp(6) > -999)
{
point2 = final;
t2min = mfexp(-(square(len_bins_m(j2) - peak2) / downselex)); // fxn at last bin
}
for (j = j1 + 1; j <= j2; j++)
{
t1 = len_bins_m(j) - peak;
t2 = len_bins_m(j) - peak2;
join1 = 1.0 / (1.0 + mfexp(-(20. * t1 / (1.0 + fabs(t1))))); // note the logit transform on t1 causes range of mfexp to be over -20 to 20
join2 = 1.0 / (1.0 + mfexp(-(20. * t2 / (1.0 + fabs(t2)))));
if (sp(5) > -999)
{
asc = point1 + (1.0 - point1) * (mfexp(-square(t1) / upselex) - t1min) / (1.0 - t1min);
}
else
{
asc = mfexp(-square(t1) / upselex);
}
if (sp(6) > -999)
{
dsc = 1.0 + (point2 - 1.0) * (mfexp(-square(t2) / downselex) - 1.0) / (t2min - 1.0);
}
else
{
dsc = mfexp(-square(t2) / downselex);
}
sel(j) = asc * (1.0 - join1) + join1 * (1.0 - join2 + dsc * join2);
}
if (startbin > 1 && sp(5) >= -1000.)
{
for (j = 1; j <= startbin - 1; j++)
{
sel(j) = square(len_bins_m(j) / len_bins_m(startbin)) * sel(startbin);
}
}
if (j2 < nlength)
{
sel(j2 + 1, nlength) = sel(j2);
}
break;
}
// SS_Label_Info_22.3.25 #case 25 size selectivity using exponential-logistic
case 25:
{
peak = len_bins_m(1) + sp(2) * (len_bins_m(nlength) - len_bins_m(1));
for (j = 1; j <= nlength; j++)
{
sel(j) = mfexp(sp(3) * sp(1) * (peak - len_bins_m(j))) / (1.0 - sp(3) * (1.0 - mfexp(sp(1) * (peak - len_bins_m(j)))));
}
break;
}
// SS_Label_Info_22.3.27 #case 27 size selectivity using cubic spline
// #42 size selectivity using cubic spline scaled by average of values at low bin through high bin
/* first N parameters are the spline knots; second N parameters are ln(selex) at the knot */
/* uses max(raw vector) to achieve scale to 1.0 */
case 42:
scaling_offset = 2;
[[fallthrough]];
case 27:
{
int j2;
// define vectors which form the basis for cubic spline selectivity
// IMPORTANT: these vectors might need to be expanded to fit values for multiple fleets
dvector splineX(1, 200);
dvar_vector splineY(1, 200);
splineX.initialize();
splineY.initialize();
k = seltype(f, 4); // n points to include in cubic spline
for (i = 1; i <= k; i++)
{
splineX(i) = value(sp(i + 3 + scaling_offset)); // "value" required to avoid error, but values should be always fixed anyway
splineY(i) = sp(i + 3 + k + scaling_offset);
}
// calculate last size bin before first knot
z = 1;
while (len_bins_m(z) < splineX(1))
{
z++;
}
j1 = z - 1; // last size bin before first knot
// calculate first size bin beyond last knot
z = nlength;
while (len_bins_m(z) > splineX(k))
{
z--;
}
j2 = z + 1; // first size bin beyond last knot
vcubic_spline_function splinefn = vcubic_spline_function(splineX(1, k), splineY(1, k), sp(2 + scaling_offset), sp(3 + scaling_offset));
tempvec_l = splinefn(len_bins_m); // interpolate selectivity at the mid-point of each population size bin
if (scaling_offset == 0)
{
temp = max(tempvec_l(1, j2));
// if spline code on first parameter line is 10, 11, or 12, then
// scale only based on interval between knots (unless first knot is at first bin)
if ((sp(1) >= 10) & (j1 >= 1))
{
temp = max(tempvec_l(j1, j2));
}
}
else
{
int low_bin = int(value(sp(1)));
int high_bin = int(value(sp(2)));
if (low_bin < 1)
{
low_bin = 1;
N_warn++;
warning << N_warn << " "
<< " selex pattern 42; value for low bin is less than 1, so set to 1 " << endl;
}
if (high_bin > nlength)
{
high_bin = nlength;
N_warn++;
warning << N_warn << " "
<< " selex pattern 42; value for high bin is greater than " << nlength << ", so set to " << nlength << " " << endl;
}
if (high_bin < low_bin)
high_bin = low_bin;
if (low_bin > high_bin)
low_bin = high_bin;
sp(1) = low_bin;
sp(2) = high_bin;
temp = mean(tempvec_l(low_bin, high_bin));
scaling_offset = 0; // reset scaling offset
}
tempvec_l -= temp; // rescale to get max of 0.0
tempvec_l(j2 + 1, nlength) = tempvec_l(j2); // set constant above last knot
sel = mfexp(tempvec_l);
// if spline code on first parameter line is 10, 11, or 12, then
// set to zero before the first knot (unless first knot is at first bin)
if ((sp(1) >= 10) & (j1 >= 1))
{
sel(1, j1) = 0; // set to 0 before first knot
}
break;
} // end cubic spline (type 42 or 27)
case 30:
{
N_warn++;
cout << " EXIT - see warning " << endl;
warning << N_warn << " "
<< "Selectivity pattern 30 not valid. Please set up in survey units instead and use pattern 0 for selectivity." << endl;
exit(1);
break;
}
default: // Selectivity pattern not found
{
N_warn++;
cout << " EXIT - see warning " << endl;
warning << N_warn << " "
<< "Length Selectivity Pattern " << seltype(f, 1) << " not valid." << endl;
exit(1);
break;
}
} // end select the selectivity pattern
sel_l(y, f, gg) = sel; // Store size-selex in year*type array
} // end direct calc of selex from parameters
// SS_Label_Info_22.4 #Do male relative to female selex
if (gg == 2) // males exist and am now in the male loop
{
if (seltype(f, 1) == 4)
{
sel_l(y, f, 2) = 0.;
} // set males to zero for spawning biomass
else if (seltype(f, 1) == 5) // set males equal to mirrored males
{
// i=int(value(sp(1))); if(i<=0) i=1;
// j=int(value(sp(2))); if(j<=0) j=nlength;
sel_l(y, f, 2) = elem_prod(mirror_mask(f), sel_l(y, seltype(f, 4), 2));
}
else if (seltype(f, 1) == 15) // set males equal to mirrored males
{
sel_l(y, f, 2) = sel_l(y, seltype(f, 4), 2);
}
else if (seltype(f, 3) == 1 || seltype(f, 3) == 2) // do gender selex as offset
{
// k=seltype_Nparam(seltype(f,1)) + 1;
// if(seltype(f,2)>0) k+=seltype(f,2)*4; // first gender offset parm (skip over the retention parameters
// if(seltype(f,1)==6) k += seltype(f,4); // for non-parametric in which N parm is stored in special column
k = Maleselparm(f);
temp = sp(k);
temp1 = 1.;
switch (seltype(f, 3))
{
case 1:
{ // do males relative to females
for (j = 1; j <= nlength; j++)
{
if (len_bins_m(j) <= temp)
{
sel(j) *= mfexp(sp(k + 1) + (len_bins_m(j) - minL_m) / (temp - minL_m) * (sp(k + 2) - sp(k + 1)));
}
else
{
sel(j) *= mfexp(sp(k + 2) + (len_bins_m(j) - temp) / (maxL - temp) * (sp(k + 3) - sp(k + 2)));
}
// if(sel(j)>temp1) temp1=sel(j);
}
sel_l(y, f, 2) = sel;
tempvec_a(1) = max(sel_l(y, f, 1));
tempvec_a(2) = max(sel_l(y, f, 2));
temp1 = max(tempvec_a(1, 2));
sel_l(y, f) /= temp1;
break;
}
case 2:
{ // do females relative to males
sel_l(y, f, 2) = sel;
for (j = 1; j <= nlength; j++)
{
if (len_bins_m(j) <= temp)
{
sel(j) *= mfexp(sp(k + 1) + (len_bins_m(j) - minL_m) / (temp - minL_m) * (sp(k + 2) - sp(k + 1)));
}
else
{
sel(j) *= mfexp(sp(k + 2) + (len_bins_m(j) - temp) / (maxL - temp) * (sp(k + 3) - sp(k + 2)));
}
// if(sel(j)>temp1) temp1=sel(j);
}
sel_l(y, f, 1) = sel;
tempvec_a(1) = max(sel_l(y, f, 1));
tempvec_a(2) = max(sel_l(y, f, 2));
temp1 = max(tempvec_a(1, 2));
sel_l(y, f) /= temp1;
break;
}
} // end switch
} // end do gender selex as offset from other gender
// SS_Label_Info_22.5 #Calculate size-specific retention and discard mortality
else if (seltype(f, 3) != 3 && seltype(f, 3) != 4) // where the "3" and "4" option do the male offset as direct parameters, rathen than do selex as offset
{
sel_l(y, f, 2) = sel;
}
} // end doing males
if (docheckup == 1)
echoinput << gg << " sel-len" << sel_l(y, f, gg) << endl;
} // end loop of genders
// apply 2D_AR adjustment to sex-specific length selectivity
// TwoD_AR_def: read: 1-fleet, 2-ymin, 3-ymax, 4-amin, 5-amax, 6-sigma_amax, 7-use_rho, 8-age/len, 9-dev_phase; 10-before_yrs, 11=after_yrs,
// calc quantities: 12-N_parm_dev, 13-selparm_location
if (TwoD_AR_use(f) > 0)
{
j = TwoD_AR_use(f); // get index for this fleet's effect
if (TwoD_AR_def[j](8) == 1) // age vs length flag
{
z = TwoD_AR_def[j](12); // index of dev vector used
if (docheckup == 1)
echoinput << "2dar for fleet: " << f << " 2DAR: " << j << " dev: " << z << endl;
if (y == styr && (TwoD_AR_before(j) == 3 || TwoD_AR_after(j) == 3)) // if needed, calculate average dev for each length over the range of years
{
TwoD_AR_ave(j).initialize();
for (int yyy = TwoD_AR_ymin(j); yyy <= TwoD_AR_ymax(j); yyy++)
{
k = (yyy - TwoD_AR_ymin(j)) * (TwoD_AR_amax(j) - TwoD_AR_amin(j) + 1); // index of 1st dev in vector created from year and length index
for (int ll = TwoD_AR_amin(j); ll <= TwoD_AR_amax(j); ll++)
{
k++;
TwoD_AR_ave(j, ll) += parm_dev(z, k); // sum devs for each length
}
}
TwoD_AR_ave(j) /= (TwoD_AR_ymax(j) - TwoD_AR_ymin(j) + 1.0);
#ifdef DO_ONCE
if (do_once == 1)
echoinput << "mean 2D AR dev for fleet: " << f << " means: " << TwoD_AR_ave(j) << endl;
#endif
}
if (y < TwoD_AR_ymin(j)) // early years
{
if (TwoD_AR_before(j) == 0) // apply no devs
{
k = -2;
}
else if (TwoD_AR_before(j) == 1) // use first year devs
{
// k=(TwoD_AR_ymin(j)-TwoD_AR_ymin(j))*(TwoD_AR_amax(j)-TwoD_AR_amin(j)+1); // index of 1st dev in vector created from year and length index
k = 0;
}
else if (TwoD_AR_before(j) == 3) // use mean length-specific devs
{
k = -1;
}
}
else if (y <= TwoD_AR_ymax(j)) // in year range for annual devs
{
k = (y - TwoD_AR_ymin(j)) * (TwoD_AR_amax(j) - TwoD_AR_amin(j) + 1); // index of 1st dev in vector created from year and length index
}
else // later years
{
if (TwoD_AR_after(j) == 0) // apply no devs
{
k = -2;
}
else if (TwoD_AR_after(j) == 1) // use last year devs
{
k = (TwoD_AR_ymax(j) - TwoD_AR_ymin(j)) * (TwoD_AR_amax(j) - TwoD_AR_amin(j) + 1);
} // index of 1st dev in vector created from year and length index
else if (TwoD_AR_after(j) == 3) // use mean length-specific devs
{
k = -1;
}
}
if (docheckup == 1)
echoinput << "K value for first dev to use: " << k << endl;
if (k > -2)
{
a1 = TwoD_AR_def[j](13) - 1; // parameter number-1 for sigmasel
dvariable sigmasel;
for (a = TwoD_AR_amin(j); a <= TwoD_AR_amax(j); a++) // loop lengths, using "a" as index
{
if (a <= TwoD_AR_def[j](6)) // so get sigmasel for this length bin
{
a1++;
sigmasel = selparm(a1);
}
if (k >= 0)
{
k++;
if (docheckup == 1)
echoinput << a << " sigmasel: " << sigmasel << endl;
sel_l(y, f, 1, a) *= mfexp(sigmasel * parm_dev(z, k));
if (gender == 2)
sel_l(y, f, 2, a) *= mfexp(sigmasel * parm_dev(z, k));
}
else // use mean devs
{
sel_l(y, f, 1, a) *= mfexp(sigmasel * TwoD_AR_ave(j, a));
if (gender == 2)
sel_l(y, f, 2, a) *= mfexp(sigmasel * TwoD_AR_ave(j, a));
}
}
}
if (docheckup == 1)
echoinput << "len selex after 2D_AR" << endl
<< sel_l(y, f) << endl;
}
}
// SS_Label_Info_22.5.1 #Calculate discmort
// discmort is the size-specific fraction of discarded fish that die
// discmort2 is size-specific fraction that die from being retained or are dead discard
// = elem_prod(sel,(retain + (1-retain)*discmort)) */
if (seltype(f, 2) == 0) // no discard, all retained
{
retain(y, f) = 1.0;
sel_l_r(y, f) = sel_l(y, f);
discmort(y, f) = 1.0;
discmort2(y, f) = sel_l(y, f);
if (gender == 2)
{
discmort_M = 1.0;
retain_M = 1.0;
}
}
else if (seltype(f, 2) == 3) // none retained; all dead
{
retain(y, f) = 0.0;
discmort(y, f) = 1.0;
sel_l_r(y, f) = 0.0;
discmort2(y, f) = sel_l(y, f);
if (gender == 2)
{
discmort_M = 1.0;
retain_M = 0.0;
}
}
else
{
if (seltype(f, 2) < 0) // mirror
{
k = -seltype(f, 2);
retain(y, f) = retain(y, k);
discmort(y, f) = discmort(y, k);
if (seltype(k, 2) == 1)
{
discmort2(y, f) = sel_l(y, f); // all selected fish are dead; this statement does both genders implicitly
}
else
{
discmort2(y, f, 1) = elem_prod(sel_l(y, f, 1), retain(y, f)(1, nlength) + elem_prod((1. - retain(y, f)(1, nlength)), discmort(y, f)(1, nlength)));
}
}
else
{
k = RetainParm(f);
if (sp(k + 2) == -999.)
{
temp1 = 0.0;
}
else if (sp(k + 2) == 999.)
{
temp1 = 1.0;
}
else
{
temp1 = 1.0 / (1.0 + mfexp(-sp(k + 2)));
}
// where maleoffset is a vector of 2*nbins with 0 in length bins for females and 1 for male bins
retain(y, f) = temp1 / (1. + mfexp(-(len_bins_m2 - (sp(k) + male_offset * sp(k + 3))) / sp(k + 1))); // males are at end of vector, so automatically get done
if (seltype(f, 2) == 4)
{
// allow for dome-shaped retention in 3.30 only
retain(y, f) = elem_prod(retain(y, f), (1. - (1. / (1. + mfexp(-(len_bins_m2 - (sp(k + 4) + male_offset * sp(k + 6))) / sp(k + 5))))));
}
if (docheckup == 1 && y == styr)
{
echoinput << "retention parms start at #: " << k << " infl: " << sp(k) << " slope: " << sp(k + 1) << " male_offset " << sp(k + 3) << " asymptote: " << temp1;
if (seltype(f, 2) == 4)
{
// additional dome-shaped retention parameters
echoinput << " " << sp(k + 4) << " " << sp(k + 5) << " " << sp(k + 6);
}
echoinput << "retention " << retain(y, f) << endl;
}
if (seltype(f, 2) == 1) // all discards are dead
{
discmort(y, f) = 1.0;
discmort2(y, f) = sel_l(y, f); // all selected fish are dead; this statement does both genders implicitly
}
else
{
k += N_ret_parm(seltype(f, 2)); // first discard mortality parm
temp = 1. - sp(k + 2);
temp1 = posfun(temp, 0.0, CrashPen);
discmort(y, f) = (1. - temp1 / (1 + mfexp(-(len_bins_m2 - (sp(k) + male_offset * sp(k + 3))) / sp(k + 1)))); // males are at end of vector, so automatically get done
if (docheckup == 1 && y == styr)
echoinput << "discmort " << discmort(y, f) << endl;
discmort2(y, f, 1) = elem_prod(sel_l(y, f, 1), retain(y, f)(1, nlength) + elem_prod((1. - retain(y, f)(1, nlength)), discmort(y, f)(1, nlength)));
}
}
sel_l_r(y, f, 1) = elem_prod(sel_l(y, f, 1), retain(y, f)(1, nlength));
if (gender == 2)
{
discmort_M.shift(nlength1) = discmort(y, f)(nlength1, nlength2);
retain_M.shift(nlength1) = retain(y, f)(nlength1, nlength2);
sel_l_r(y, f, 2) = elem_prod(sel_l(y, f, 2), retain_M.shift(1));
discmort2(y, f, 2) = elem_prod(sel_l(y, f, 2), retain_M.shift(1) + elem_prod((1. - retain_M.shift(1)), discmort_M.shift(1))); // V3.21f
}
}
if (docheckup == 1 && y == styr)
echoinput << "sel-len-r " << sel_l_r(y, f) << endl;
if (docheckup == 1 && y == styr)
echoinput << " dead " << discmort2(y, f) << endl;