FIMS  v0.8.1
Loading...
Searching...
No Matches
catch_at_age.hpp
Go to the documentation of this file.
1
8#ifndef FIMS_MODELS_CATCH_AT_AGE_HPP
9#define FIMS_MODELS_CATCH_AT_AGE_HPP
10
11#include <set>
12#include <regex>
13
15
16/* Dictionary block for shared parameter snippet documentations.
17 * Referenced in function docs via @snippet{doc} this snippet_id.
18 [param_population]
19 @param population Shared pointer to the population object.
20 [param_population]
21 [param_i_age_year]
22 @param i_age_year Dimension folded index for age and year.
23 [param_i_age_year]
24 [param_year]
25 @param year Year index.
26 [param_year]
27 [param_age]
28 @param age Age index.
29 [param_age]
30 [param_i_agem1_yearm1]
31 @param i_agem1_yearm1 Dimension folded index for age-1 and year-1.
32 [param_i_agem1_yearm1]
33 [param_i_dev]
34 @param i_dev Index to log_recruit_dev of vector length n_years-1.
35 [param_i_dev]
36 [param_other]
37 @param other The other CatchAtAge object to copy from.
38 [param_other]
39 */
40
41namespace fims_popdy {
42
43template <typename Type>
53class CatchAtAge : public FisheryModelBase<Type> {
54 public:
59 std::string name_m;
60
65 typedef typename std::map<std::string, fims::Vector<Type>>::iterator
67
72 typedef typename std::map<uint32_t,
73 std::map<std::string, fims::Vector<Type>>>::iterator
75
79 typedef
80 typename std::map<uint32_t,
81 std::map<std::string, fims::Vector<size_t>>>::iterator
87 typedef typename std::map<uint32_t,
88 std::map<std::string, fims::Vector<Type>>>::iterator
90
95 typedef
96 typename std::map<uint32_t,
97 std::map<std::string, fims::Vector<size_t>>>::iterator
99
104 typedef typename std::map<uint32_t,
105 std::shared_ptr<fims_popdy::Fleet<Type>>>::iterator
111 typedef
112 typename std::map<std::string, fims::Vector<Type>>::iterator dq_iterator;
118 std::map<std::string, fims::Vector<fims::Vector<Type>>> report_vectors;
119
120 public:
121 std::vector<Type> ages;
127 std::stringstream ss;
128 ss << "caa_" << this->GetId() << "_";
129 this->name_m = ss.str();
130 this->model_type_m = "caa";
131 }
132
142
147 virtual ~CatchAtAge() {}
148
153 virtual void Initialize() {
154 for (size_t p = 0; p < this->populations.size(); p++) {
155 this->populations[p]->proportion_female.resize(
156 this->populations[p]->n_ages);
157
158 this->populations[p]->M.resize(this->populations[p]->n_years *
159 this->populations[p]->n_ages);
160
161 this->populations[p]->f_multiplier.resize(this->populations[p]->n_years);
162
163 this->populations[p]->spawning_biomass_ratio.resize(
164 (this->populations[p]->n_years + 1));
165 }
166
167 for (fleet_iterator fit = this->fleets.begin(); fit != this->fleets.end();
168 ++fit) {
169 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
170
171 if (fleet->log_q.size() == 0) {
172 fleet->log_q.resize(1);
173 fleet->log_q[0] = static_cast<Type>(0.0);
174 }
175 fleet->q.resize(fleet->log_q.size());
176 fleet->Fmort.resize(fleet->n_years);
177 }
178 }
179
184 virtual void Prepare() {
185 for (size_t p = 0; p < this->populations.size(); p++) {
186 std::shared_ptr<fims_popdy::Population<Type>> &population =
187 this->populations[p];
188
189 auto &derived_quantities =
190 this->GetPopulationDerivedQuantities(population->GetId());
191
192 // Reset the derived quantities for the population
193 for (auto &kv : derived_quantities) {
194 this->ResetVector(kv.second);
195 }
196
197 // Prepare proportion_female
198 for (size_t age = 0; age < population->n_ages; age++) {
199 population->proportion_female[age] = 0.5;
200 }
201
202 // Transformation Section
203 for (size_t age = 0; age < population->n_ages; age++) {
204 for (size_t year = 0; year < population->n_years; year++) {
205 size_t i_age_year = age * population->n_years + year;
207 fims_math::exp(population->log_M[i_age_year]);
208 }
209 }
210
211 for (size_t year = 0; year < population->n_years; year++) {
212 population->f_multiplier[year] =
213 fims_math::exp(population->log_f_multiplier[year]);
214 }
215 }
216
217 for (fleet_iterator fit = this->fleets.begin(); fit != this->fleets.end();
218 ++fit) {
219 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
220 auto &derived_quantities =
221 this->GetFleetDerivedQuantities(fleet->GetId());
222
223 for (auto &kv : derived_quantities) {
224 this->ResetVector(kv.second);
225 }
226
227 // Transformation Section
228 for (size_t i = 0; i < fleet->log_q.size(); i++) {
229 fleet->q[i] = fims_math::exp(fleet->log_q[i]);
230 }
231
232 for (size_t year = 0; year < fleet->n_years; year++) {
233 fleet->Fmort[year] = fims_math::exp(fleet->log_Fmort[year]);
234 }
235 }
236 }
240 void AddPopulation(uint32_t id) { this->population_ids.insert(id); }
241
245 std::set<uint32_t> &GetPopulationIds() { return this->population_ids; }
246
252 std::vector<std::shared_ptr<fims_popdy::Population<Type>>> &GetPopulations() {
253 return this->populations;
254 }
255
270 size_t i_age_year, size_t age) {
271 std::map<std::string, fims::Vector<Type>> &dq_ =
273
274 dq_["numbers_at_age"][i_age_year] =
275 fims_math::exp(population->log_init_naa[age]);
276 }
277
302 size_t i_age_year, size_t i_agem1_yearm1, size_t age) {
303 // using Z from previous age/year
304
305 std::map<std::string, fims::Vector<Type>> &dq_ =
307
308 dq_["numbers_at_age"][i_age_year] =
309 dq_["numbers_at_age"][i_agem1_yearm1] *
310 (fims_math::exp(-dq_["mortality_Z"][i_agem1_yearm1]));
311
312 // Plus group calculation
313 if (age == (population->n_ages - 1)) {
314 dq_["numbers_at_age"][i_age_year] =
315 dq_["numbers_at_age"][i_age_year] +
316 dq_["numbers_at_age"][i_agem1_yearm1 + 1] *
317 (fims_math::exp(-dq_["mortality_Z"][i_agem1_yearm1 + 1]));
318 }
319 }
320
345 size_t i_age_year, size_t i_agem1_yearm1, size_t age) {
346 std::map<std::string, fims::Vector<Type>> &dq_ =
348
349 // using M from previous age/year
350 dq_["unfished_numbers_at_age"][i_age_year] =
351 dq_["unfished_numbers_at_age"][i_agem1_yearm1] *
352 (fims_math::exp(-population->M[i_agem1_yearm1]));
353
354 // Plus group calculation
355 if (age == (population->n_ages - 1)) {
356 dq_["unfished_numbers_at_age"][i_age_year] =
357 dq_["unfished_numbers_at_age"][i_age_year] +
358 dq_["unfished_numbers_at_age"][i_agem1_yearm1 + 1] *
359 (fims_math::exp(-population->M[i_agem1_yearm1 + 1]));
360 }
361 }
362
396 size_t i_age_year, size_t year, size_t age) {
397 std::map<std::string, fims::Vector<Type>> &dq_ =
399
400 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
401 // evaluate is a member function of the selectivity class
402 Type s = population->fleets[fleet_]->selectivity->evaluate(
403 population->ages[age], year);
404
405 dq_["mortality_F"][i_age_year] +=
406 population->fleets[fleet_]->Fmort[year] *
407 population->f_multiplier[year] * s;
408
409 dq_["sum_selectivity"][i_age_year] += s;
410 }
411 dq_["mortality_M"][i_age_year] = population->M[i_age_year];
412
413 dq_["mortality_Z"][i_age_year] =
414 population->M[i_age_year] + dq_["mortality_F"][i_age_year];
415 }
416
433 size_t i_age_year, size_t year, size_t age) {
434 std::map<std::string, fims::Vector<Type>> &dq_ =
436
437 dq_["biomass"][year] += dq_["numbers_at_age"][i_age_year] *
438 population->growth->evaluate(year, population->ages[age]);
439 }
440
457 size_t i_age_year, size_t year, size_t age) {
458 std::map<std::string, fims::Vector<Type>> &dq_ =
460
461 dq_["unfished_biomass"][year] +=
462 dq_["unfished_numbers_at_age"][i_age_year] *
463 population->growth->evaluate(year,population->ages[age]);
464 }
465
484 size_t i_age_year, size_t year, size_t age) {
485 std::map<std::string, fims::Vector<Type>> &dq_ =
487
488 dq_["spawning_biomass"][year] +=
489 population->proportion_female[age] * dq_["numbers_at_age"][i_age_year] *
490 dq_["proportion_mature_at_age"][i_age_year] *
491 population->growth->evaluate(year,population->ages[age]);
492 }
493
511 size_t i_age_year, size_t year, size_t age) {
512 std::map<std::string, fims::Vector<Type>> &dq_ =
514
515 dq_["unfished_spawning_biomass"][year] +=
516 population->proportion_female[age] *
517 dq_["unfished_numbers_at_age"][i_age_year] *
518 dq_["proportion_mature_at_age"][i_age_year] *
519 population->growth->evaluate(year,population->ages[age]);
520 }
521
538 std::shared_ptr<fims_popdy::Population<Type>> &population, size_t year) {
539 std::map<std::string, fims::Vector<Type>> &dq_ =
541 population->spawning_biomass_ratio[year] =
542 dq_["spawning_biomass"][year] / dq_["unfished_spawning_biomass"][0];
543 }
544
569 std::shared_ptr<fims_popdy::Population<Type>> &population) {
570 std::map<std::string, fims::Vector<Type>> &dq_ =
572
573 std::vector<Type> numbers_spr(population->n_ages, 1.0);
574 Type phi_0 = 0.0;
575 phi_0 += numbers_spr[0] * population->proportion_female[0] *
576 dq_["proportion_mature_at_age"][0] *
577 population->growth->evaluate(0,population->ages[0]);
578 for (size_t a = 1; a < (population->n_ages - 1); a++) {
579 numbers_spr[a] = numbers_spr[a - 1] * fims_math::exp(-population->M[a]);
580 phi_0 += numbers_spr[a] * population->proportion_female[a] *
581 dq_["proportion_mature_at_age"][a] *
582 population->growth->evaluate(0,population->ages[a]);
583 }
584
585 numbers_spr[population->n_ages - 1] =
586 (numbers_spr[population->n_ages - 2] *
587 fims_math::exp(-population->M[population->n_ages - 2])) /
588 (1 - fims_math::exp(-population->M[population->n_ages - 1]));
589 phi_0 +=
590 numbers_spr[population->n_ages - 1] *
591 population->proportion_female[population->n_ages - 1] *
592 dq_["proportion_mature_at_age"][population->n_ages - 1] *
593 population->growth->evaluate(0,population->ages[population->n_ages - 1]);
594
595 return phi_0;
596 }
597
623 size_t i_age_year, size_t year, size_t i_dev) {
624 std::map<std::string, fims::Vector<Type>> &dq_ =
626
628
629 if (i_dev == population->n_years) {
630 dq_["numbers_at_age"][i_age_year] =
631 population->recruitment->evaluate_mean(
632 dq_["spawning_biomass"][year - 1], phi0);
633 /*the final year of the time series has no data to inform recruitment
634 devs, so this value is set to the mean recruitment.*/
635 } else {
636 // Why are we using evaluate_mean, how come a virtual function was
637 // changed? AMH: there are now two virtual functions: evaluate_mean and
638 // evaluate_process (see below)
639 population->recruitment->log_expected_recruitment[year - 1] =
640 fims_math::log(population->recruitment->evaluate_mean(
641 dq_["spawning_biomass"][year - 1], phi0));
642
643 dq_["numbers_at_age"][i_age_year] = fims_math::exp(
644 population->recruitment->process->evaluate_process(year - 1));
645 }
646
647 dq_["expected_recruitment"][year] = dq_["numbers_at_age"][i_age_year];
648 }
649
665 size_t i_age_year, size_t age) {
666 std::map<std::string, fims::Vector<Type>> &dq_ =
668
669 dq_["proportion_mature_at_age"][i_age_year] =
670 population->maturity->evaluate(population->ages[age]);
671 }
672
689 std::shared_ptr<fims_popdy::Population<Type>> &population, size_t year,
690 size_t age) {
691 std::map<std::string, fims::Vector<Type>> &pdq_ =
693
694 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
695 std::map<std::string, fims::Vector<Type>> &fdq_ =
696 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
697 size_t i_age_year = year * population->n_ages + age;
698
699 pdq_["total_landings_weight"][year] +=
700 fdq_["landings_weight_at_age"][i_age_year];
701
702 fdq_["landings_weight"][year] +=
703 fdq_["landings_weight_at_age"][i_age_year];
704
705 pdq_["total_landings_numbers"][year] +=
706 fdq_["landings_numbers_at_age"][i_age_year];
707
708 fdq_["landings_numbers"][year] +=
709 fdq_["landings_numbers_at_age"][i_age_year];
710 }
711 }
712
729 std::shared_ptr<fims_popdy::Population<Type>> &population, size_t year,
730 size_t age) {
731 int i_age_year = year * population->n_ages + age;
732 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
733 std::map<std::string, fims::Vector<Type>> &fdq_ =
734 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
735
736 fdq_["landings_weight_at_age"][i_age_year] =
737 fdq_["landings_numbers_at_age"][i_age_year] *
738 population->growth->evaluate(year,population->ages[age]);
739 }
740 }
741
761 size_t i_age_year, size_t year, size_t age) {
762 std::map<std::string, fims::Vector<Type>> &pdq_ =
764
765 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
766 std::map<std::string, fims::Vector<Type>> &fdq_ =
767 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
768
769 // Baranov Catch Equation
770 fdq_["landings_numbers_at_age"][i_age_year] +=
771 (population->fleets[fleet_]->Fmort[year] *
772 population->f_multiplier[year] *
773 population->fleets[fleet_]->selectivity->evaluate(
774 population->ages[age],year)) /
775 pdq_["mortality_Z"][i_age_year] * pdq_["numbers_at_age"][i_age_year] *
776 (1 - fims_math::exp(-(pdq_["mortality_Z"][i_age_year])));
777 }
778 }
779
798 size_t i_age_year, size_t year, size_t age) {
799 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
800 std::map<std::string, fims::Vector<Type>> &fdq_ =
801 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
802
803 fdq_["index_weight"][year] += fdq_["index_weight_at_age"][i_age_year];
804
805 fdq_["index_numbers"][year] += fdq_["index_numbers_at_age"][i_age_year];
806 }
807 }
808
830 size_t i_age_year, size_t year, size_t age) {
831 std::map<std::string, fims::Vector<Type>> &pdq_ =
833
834 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
835 std::map<std::string, fims::Vector<Type>> &fdq_ =
836 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
837
838 fdq_["index_numbers_at_age"][i_age_year] +=
839 (population->fleets[fleet_]->q.get_force_scalar(year) *
840 population->fleets[fleet_]->selectivity->evaluate(
841 population->ages[age],year)) *
842 pdq_["numbers_at_age"][i_age_year];
843 }
844 }
845
861 std::shared_ptr<fims_popdy::Population<Type>> &population, size_t year,
862 size_t age) {
863 int i_age_year = year * population->n_ages + age;
864 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
865 std::map<std::string, fims::Vector<Type>> &fdq_ =
866 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
867
868 fdq_["index_weight_at_age"][i_age_year] =
869 fdq_["index_numbers_at_age"][i_age_year] *
870 population->growth->evaluate(year,population->ages[age]);
871 }
872 }
873
879 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
880 std::map<std::string, fims::Vector<Type>> &fdq_ =
881 this->GetFleetDerivedQuantities((*fit).second->GetId());
882
883 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
884 for (size_t y = 0; y < fleet->n_years; y++) {
885 Type sum = static_cast<Type>(0.0);
886 Type sum_obs = static_cast<Type>(0.0);
887 // robust_add is a small value to add to expected composition
888 // proportions at age to stabilize likelihood calculations
889 // when the expected proportions are close to zero.
890 // Type robust_add = static_cast<Type>(0.0); // zeroed out before
891 // testing 0.0001; sum robust is used to calculate the total sum of
892 // robust additions to ensure that proportions sum to 1. Type robust_sum
893 // = static_cast<Type>(1.0);
894
895 for (size_t a = 0; a < fleet->n_ages; a++) {
896 size_t i_age_year = y * fleet->n_ages + a;
897 // Here we have a check to determine if the age comp
898 // should be calculated from the retained landings or
899 // the total population. These values are slightly different.
900 // In the future this will have more impact as we implement
901 // timing rather than everything occurring at the start of
902 // the year.
903 if (fleet->fleet_observed_landings_data_id_m == -999) {
904 fdq_["agecomp_expected"][i_age_year] =
905 fdq_["index_numbers_at_age"][i_age_year];
906 } else {
907 fdq_["agecomp_expected"][i_age_year] =
908 fdq_["landings_numbers_at_age"][i_age_year];
909 }
910 sum += fdq_["agecomp_expected"][i_age_year];
911 // robust_sum -= robust_add;
912
913 // This sums over the observed age composition data so that
914 // the expected age composition can be rescaled to match the
915 // total number observed. The check for na values should not
916 // be needed as individual years should not have missing data.
917 // This is need to be re-explored if/when we modify FIMS to
918 // allow for composition bins that do not match the population
919 // bins.
920 if (fleet->fleet_observed_agecomp_data_id_m != -999) {
921 if (fleet->observed_agecomp_data->at(i_age_year) !=
922 fleet->observed_agecomp_data->na_value) {
923 sum_obs += fleet->observed_agecomp_data->at(i_age_year);
924 }
925 }
926 }
927 for (size_t a = 0; a < fleet->n_ages; a++) {
928 size_t i_age_year = y * fleet->n_ages + a;
929 fdq_["agecomp_proportion"][i_age_year] =
930 fdq_["agecomp_expected"][i_age_year] / sum;
931 // robust_add + robust_sum * this->agecomp_expected[i_age_year] / sum;
932
933 if (fleet->fleet_observed_agecomp_data_id_m != -999) {
934 fdq_["agecomp_expected"][i_age_year] =
935 fdq_["agecomp_proportion"][i_age_year] * sum_obs;
936 }
937 }
938 }
939 }
940 }
941
947 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
948 std::map<std::string, fims::Vector<Type>> &fdq_ =
949 this->GetFleetDerivedQuantities((*fit).second->GetId());
950
951 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
952
953 if (fleet->n_lengths > 0) {
954 for (size_t y = 0; y < fleet->n_years; y++) {
955 Type sum = static_cast<Type>(0.0);
956 Type sum_obs = static_cast<Type>(0.0);
957 // robust_add is a small value to add to expected composition
958 // proportions at age to stabilize likelihood calculations
959 // when the expected proportions are close to zero.
960 // Type robust_add = static_cast<Type>(0.0); // 0.0001; zeroed out
961 // before testing sum robust is used to calculate the total sum of
962 // robust additions to ensure that proportions sum to 1. Type
963 // robust_sum = static_cast<Type>(1.0);
964 for (size_t l = 0; l < fleet->n_lengths; l++) {
965 size_t i_length_year = y * fleet->n_lengths + l;
966 for (size_t a = 0; a < fleet->n_ages; a++) {
967 size_t i_age_year = y * fleet->n_ages + a;
968 size_t i_length_age = a * fleet->n_lengths + l;
969 fdq_["lengthcomp_expected"][i_length_year] +=
970 fdq_["agecomp_expected"][i_age_year] *
971 fleet->age_to_length_conversion[i_length_age];
972
973 fdq_["landings_numbers_at_length"][i_length_year] +=
974 fdq_["landings_numbers_at_age"][i_age_year] *
975 fleet->age_to_length_conversion[i_length_age];
976
977 fdq_["index_numbers_at_length"][i_length_year] +=
978 fdq_["index_numbers_at_age"][i_age_year] *
979 fleet->age_to_length_conversion[i_length_age];
980 }
981
982 sum += fdq_["lengthcomp_expected"][i_length_year];
983 // robust_sum -= robust_add;
984
985 if (fleet->fleet_observed_lengthcomp_data_id_m != -999) {
986 if (fleet->observed_lengthcomp_data->at(i_length_year) !=
987 fleet->observed_lengthcomp_data->na_value) {
988 sum_obs += fleet->observed_lengthcomp_data->at(i_length_year);
989 }
990 }
991 }
992 for (size_t l = 0; l < fleet->n_lengths; l++) {
993 size_t i_length_year = y * fleet->n_lengths + l;
994 fdq_["lengthcomp_proportion"][i_length_year] =
995 fdq_["lengthcomp_expected"][i_length_year] / sum;
996 // robust_add + robust_sum *
997 // this->lengthcomp_expected[i_length_year] / sum;
998 if (fleet->fleet_observed_lengthcomp_data_id_m != -999) {
999 fdq_["lengthcomp_expected"][i_length_year] =
1000 fdq_["lengthcomp_proportion"][i_length_year] * sum_obs;
1001 }
1002 }
1003 }
1004 }
1005 }
1006 }
1007
1013 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
1014 std::map<std::string, fims::Vector<Type>> &fdq_ =
1015 this->GetFleetDerivedQuantities((*fit).second->GetId());
1016 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
1017
1018 for (size_t i = 0; i < fdq_["index_numbers"].size(); i++) {
1019 if (fleet->observed_index_units == "number") {
1020 fdq_["index_expected"][i] = fdq_["index_numbers"][i];
1021 } else {
1022 fdq_["index_expected"][i] = fdq_["index_weight"][i];
1023 }
1024 fdq_["log_index_expected"][i] = log(fdq_["index_expected"][i]);
1025 }
1026 }
1027 }
1028
1034 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
1035 std::map<std::string, fims::Vector<Type>> &fdq_ =
1036 this->GetFleetDerivedQuantities((*fit).second->GetId());
1037 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
1038
1039 for (size_t i = 0; i < fdq_["landings_weight"].size(); i++) {
1040 if (fleet->observed_landings_units == "number") {
1041 fdq_["landings_expected"][i] = fdq_["landings_numbers"][i];
1042 } else {
1043 fdq_["landings_expected"][i] = fdq_["landings_weight"][i];
1044 }
1045 fdq_["log_landings_expected"][i] = log(fdq_["landings_expected"][i]);
1046 }
1047 }
1048 }
1049
1050 virtual void Evaluate() {
1051 /*
1052 Sets derived vectors to zero
1053 Performs parameters transformations
1054 Sets recruitment deviations to mean 0.
1055 */
1056 Prepare();
1057 /*
1058 start at year=0, age=0;
1059 here year 0 is the estimated initial population structure and age 0 are
1060 recruits loops start at zero with if statements inside to specify unique
1061 code for initial structure and recruitment 0 loops. Could also have started
1062 loops at 1 with initial structure and recruitment setup outside the loops.
1063
1064 year loop is extended to <= n_years because SSB is calculated as the start
1065 of the year value and by extending one extra year we get estimates of the
1066 population structure at the end of the final year. An alternative approach
1067 would be to keep initial numbers at age in it's own vector and each year to
1068 include the population structure at the end of the year. This is likely a
1069 null point given that we are planning to modify to an event/stanza based
1070 structure in later milestones which will eliminate this confusion by
1071 explicitly referencing the exact date (or period of averaging) at which any
1072 calculation or output is being made.
1073 */
1074 for (size_t p = 0; p < this->populations.size(); p++) {
1075 std::shared_ptr<fims_popdy::Population<Type>> &population =
1076 this->populations[p];
1077 std::map<std::string, fims::Vector<Type>> &pdq_ =
1078 this->GetPopulationDerivedQuantities(population->GetId());
1079 // CAAPopulationProxy<Type>& population = this->populations_proxies[p];
1080
1081 for (size_t y = 0; y <= population->n_years; y++) {
1082 for (size_t a = 0; a < population->n_ages; a++) {
1083 /*
1084 index naming defines the dimensional folding structure
1085 i.e. i_age_year is referencing folding over years and ages.
1086 */
1087 size_t i_age_year = y * population->n_ages + a;
1088 /*
1089 Mortality rates are not estimated in the final year which is
1090 used to show expected population structure at the end of the model
1091 period. This is because biomass in year i represents biomass at the
1092 start of the year. Should we add complexity to track more values such
1093 as start, mid, and end biomass in all years where, start biomass=end
1094 biomass of the previous year? Referenced above, this is probably not
1095 worth exploring as later milestone changes will eliminate this
1096 confusion.
1097 */
1098 if (y < population->n_years) {
1099 /*
1100 First thing we need is total mortality aggregated across all fleets
1101 to inform the subsequent catch and change in numbers at age
1102 calculations. This is only calculated for years < n_years as these
1103 are the model estimated years with data. The year loop extends to
1104 y=n_years so that population numbers at age and SSB can be
1105 calculated at the end of the last year of the model
1106 */
1108 }
1110 /* if statements needed because some quantities are only needed
1111 for the first year and/or age, so these steps are included here.
1112 */
1113 if (y == 0) {
1114 // Initial numbers at age is a user input or estimated parameter
1115 // vector.
1117
1118 if (a == 0) {
1119 /*
1120 Expected recruitment in year 0 is numbers at age 0 in year 0.
1121 */
1122 pdq_["expected_recruitment"][y] =
1123 pdq_["numbers_at_age"][i_age_year];
1124 pdq_["unfished_numbers_at_age"][i_age_year] =
1125 fims_math::exp(population->recruitment->log_rzero[0]);
1126 } else {
1128 }
1129
1130 } else {
1131 if (a == 0) {
1132 // Set the nrecruits for age a=0 year y (use pointers instead of
1133 // functional returns) assuming fecundity = 1 and 50:50 sex ratio
1135 pdq_["unfished_numbers_at_age"][i_age_year] =
1136 fims_math::exp(population->recruitment->log_rzero[0]);
1137 } else {
1138 size_t i_agem1_yearm1 = (y - 1) * population->n_ages + (a - 1);
1141 a);
1142 }
1143 }
1144
1145 /*
1146 Fished and unfished biomass vectors are summing biomass at
1147 age across ages.
1148 */
1149
1151
1153
1154 /*
1155 Fished and unfished spawning biomass vectors are summing biomass at
1156 age across ages to allow calculation of recruitment in the next
1157 year.
1158 */
1159
1161
1163
1164 /*
1165 Here composition, total catch, and index values are calculated for all
1166 years with reference data. They are not calculated for y=n_years as
1167 there is this is just to get final population structure at the end of
1168 the terminal year.
1169 */
1170 if (y < population->n_years) {
1174
1178 }
1179 }
1180 /* Calculate spawning biomass depletion ratio */
1182 }
1183 }
1188 }
1193 virtual void Report() {
1194 int n_fleets = this->fleets.size();
1195 int n_pops = this->populations.size();
1196#ifdef TMB_MODEL
1197 if (this->do_reporting == true) {
1198 report_vectors.clear();
1199 // initialize population vectors
1215
1216 // initialize fleet vectors
1236
1237 // initiate population index for structuring report out objects
1238 int pop_idx = 0;
1239 for (size_t p = 0; p < this->populations.size(); p++) {
1240 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1242 biomass_p(pop_idx) = derived_quantities["biomass"].to_tmb();
1244 derived_quantities["expected_recruitment"].to_tmb();
1245 mortality_F_p(pop_idx) = derived_quantities["mortality_F"].to_tmb();
1246 mortality_M_p(pop_idx) = derived_quantities["mortality_M"].to_tmb();
1247 mortality_Z_p(pop_idx) = derived_quantities["mortality_Z"].to_tmb();
1249 derived_quantities["numbers_at_age"].to_tmb();
1251 derived_quantities["proportion_mature_at_age"].to_tmb();
1253 derived_quantities["spawning_biomass"].to_tmb();
1255 derived_quantities["sum_selectivity"].to_tmb();
1257 derived_quantities["total_landings_numbers"].to_tmb();
1259 derived_quantities["total_landings_weight"].to_tmb();
1261 derived_quantities["unfished_biomass"].to_tmb();
1263 derived_quantities["unfished_numbers_at_age"].to_tmb();
1265 derived_quantities["unfished_spawning_biomass"].to_tmb();
1267 this->populations[pop_idx]->spawning_biomass_ratio.to_tmb();
1268
1269 pop_idx += 1;
1270 }
1271
1272 // initiate fleet index for structuring report out objects
1273 int fleet_idx = 0;
1275 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
1276 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
1277 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1278 this->GetFleetDerivedQuantities(fleet->GetId());
1279
1281 derived_quantities["agecomp_expected"].to_tmb();
1283 derived_quantities["agecomp_proportion"].to_tmb();
1284 catch_index_f(fleet_idx) = derived_quantities["catch_index"].to_tmb();
1286 derived_quantities["index_expected"].to_tmb();
1288 derived_quantities["index_numbers"].to_tmb();
1290 derived_quantities["index_numbers_at_age"].to_tmb();
1292 derived_quantities["index_numbers_at_length"].to_tmb();
1293 index_weight_f(fleet_idx) = derived_quantities["index_weight"].to_tmb();
1295 derived_quantities["index_weight_at_age"].to_tmb();
1297 derived_quantities["landings_expected"].to_tmb();
1299 derived_quantities["landings_numbers"].to_tmb();
1301 derived_quantities["landings_numbers_at_age"].to_tmb();
1303 derived_quantities["landings_numbers_at_length"].to_tmb();
1305 derived_quantities["landings_weight"].to_tmb();
1307 derived_quantities["landings_weight_at_age"].to_tmb();
1308 // length_comp_expected_f(fleet_idx) =
1309 // derived_quantities["length_comp_expected"];
1310 // length_comp_proportion_f(fleet_idx) =
1311 // derived_quantities["length_comp_proportion"];
1313 derived_quantities["lengthcomp_expected"].to_tmb();
1315 derived_quantities["lengthcomp_proportion"].to_tmb();
1317 derived_quantities["log_index_expected"].to_tmb();
1319 derived_quantities["log_landings_expected"].to_tmb();
1320 fleet_idx += 1;
1321 }
1322
1323 vector<Type> biomass = ADREPORTvector(biomass_p);
1324 vector<Type> expected_recruitment =
1330 vector<Type> proportion_mature_at_age =
1334 vector<Type> total_landings_numbers =
1336 vector<Type> total_landings_weight =
1339 vector<Type> unfished_numbers_at_age =
1341 vector<Type> unfished_spawning_biomass =
1343 vector<Type> spawning_biomass_ratio =
1345
1346 vector<Type> agecomp_expected = ADREPORTvector(agecomp_expected_f);
1347 vector<Type> agecomp_proportion = ADREPORTvector(agecomp_proportion_f);
1351 vector<Type> index_numbers_at_age =
1353 vector<Type> index_numbers_at_length =
1359 vector<Type> landings_numbers_at_age =
1361 vector<Type> landings_numbers_at_length =
1364 vector<Type> landings_weight_at_age =
1366 // vector<Type> length_comp_expected =
1367 // ADREPORTvector(length_comp_expected_f); vector<Type>
1368 // length_comp_proportion = ADREPORTvector(length_comp_proportion_f);
1369 vector<Type> lengthcomp_expected = ADREPORTvector(lengthcomp_expected_f);
1370 vector<Type> lengthcomp_proportion =
1372 vector<Type> log_index_expected = ADREPORTvector(log_index_expected_f);
1373 vector<Type> log_landings_expected =
1375 // populations
1376 // report
1377 FIMS_REPORT_F_("biomass", biomass_p, this->of);
1378 FIMS_REPORT_F_("expected_recruitment", expected_recruitment_p, this->of);
1379 FIMS_REPORT_F_("mortality_F", mortality_F_p, this->of);
1380 FIMS_REPORT_F_("mortality_M", mortality_M_p, this->of);
1381 FIMS_REPORT_F_("mortality_Z", mortality_Z_p, this->of);
1382 FIMS_REPORT_F_("numbers_at_age", numbers_at_age_p, this->of);
1383 FIMS_REPORT_F_("proportion_mature_at_age", proportion_mature_at_age_p,
1384 this->of);
1385 FIMS_REPORT_F_("spawning_biomass", spawning_biomass_p, this->of);
1386 FIMS_REPORT_F_("sum_selectivity", sum_selectivity_p, this->of);
1387 FIMS_REPORT_F_("total_landings_numbers", total_landings_numbers_p,
1388 this->of);
1389 FIMS_REPORT_F_("total_landings_weight", total_landings_weight_p,
1390 this->of);
1391 FIMS_REPORT_F_("unfished_biomass", unfished_biomass_p, this->of);
1392 FIMS_REPORT_F_("unfished_numbers_at_age", unfished_numbers_at_age_p,
1393 this->of);
1394 FIMS_REPORT_F_("unfished_spawning_biomass", unfished_spawning_biomass_p,
1395 this->of);
1396 FIMS_REPORT_F_("spawning_biomass_ratio", spawning_biomass_ratio_p,
1397 this->of);
1398
1399 // adreport
1400 ADREPORT_F(biomass, this->of);
1401 ADREPORT_F(expected_recruitment, this->of);
1402 ADREPORT_F(mortality_F, this->of);
1403 ADREPORT_F(mortality_M, this->of);
1404 ADREPORT_F(mortality_Z, this->of);
1405 ADREPORT_F(numbers_at_age, this->of);
1406 ADREPORT_F(proportion_mature_at_age, this->of);
1407 ADREPORT_F(spawning_biomass, this->of);
1408 ADREPORT_F(sum_selectivity, this->of);
1409 ADREPORT_F(total_landings_numbers, this->of);
1410 ADREPORT_F(total_landings_weight, this->of);
1411 ADREPORT_F(unfished_biomass, this->of);
1412 ADREPORT_F(unfished_numbers_at_age, this->of);
1413 ADREPORT_F(unfished_spawning_biomass, this->of);
1414 ADREPORT_F(spawning_biomass_ratio, this->of);
1415
1416 // fleets
1417 // report
1418 FIMS_REPORT_F_("agecomp_expected", agecomp_expected_f, this->of);
1419 FIMS_REPORT_F_("agecomp_proportion", agecomp_proportion_f, this->of);
1420 FIMS_REPORT_F_("catch_index", catch_index_f, this->of);
1421 FIMS_REPORT_F_("index_expected", index_expected_f, this->of);
1422 FIMS_REPORT_F_("index_numbers", index_numbers_f, this->of);
1423 FIMS_REPORT_F_("index_numbers_at_age", index_numbers_at_age_f, this->of);
1424 FIMS_REPORT_F_("index_numbers_at_length", index_numbers_at_length_f,
1425 this->of);
1426 FIMS_REPORT_F_("index_weight", index_weight_f, this->of);
1427 FIMS_REPORT_F_("index_weight_at_age", index_weight_at_age_f, this->of);
1428 FIMS_REPORT_F_("landings_expected", landings_expected_f, this->of);
1429 FIMS_REPORT_F_("landings_numbers", landings_numbers_f, this->of);
1430 FIMS_REPORT_F_("landings_numbers_at_age", landings_numbers_at_age_f,
1431 this->of);
1432 FIMS_REPORT_F_("landings_numbers_at_length", landings_numbers_at_length_f,
1433 this->of);
1434 FIMS_REPORT_F_("landings_weight", landings_weight_f, this->of);
1435 FIMS_REPORT_F_("landings_weight_at_age", landings_weight_at_age_f,
1436 this->of);
1437 FIMS_REPORT_F_("lengthcomp_expected", lengthcomp_expected_f, this->of);
1438 FIMS_REPORT_F_("lengthcomp_proportion", lengthcomp_proportion_f,
1439 this->of);
1440 FIMS_REPORT_F_("log_index_expected", log_index_expected_f, this->of);
1441 FIMS_REPORT_F_("log_landings_expected", log_landings_expected_f,
1442 this->of);
1443 // adreport
1444 ADREPORT_F(agecomp_expected, this->of);
1445 ADREPORT_F(agecomp_proportion, this->of);
1446 ADREPORT_F(catch_index, this->of);
1447 ADREPORT_F(index_expected, this->of);
1448 ADREPORT_F(index_numbers, this->of);
1449 ADREPORT_F(index_numbers_at_age, this->of);
1450 ADREPORT_F(index_numbers_at_length, this->of);
1451 ADREPORT_F(index_weight, this->of);
1452 ADREPORT_F(index_weight_at_age, this->of);
1453 ADREPORT_F(landings_expected, this->of);
1454 ADREPORT_F(landings_numbers, this->of);
1455 ADREPORT_F(landings_numbers_at_age, this->of);
1456 ADREPORT_F(landings_numbers_at_length, this->of);
1457 ADREPORT_F(landings_weight, this->of);
1458 ADREPORT_F(landings_weight_at_age, this->of);
1459 ADREPORT_F(lengthcomp_expected, this->of);
1460 ADREPORT_F(lengthcomp_proportion, this->of);
1461 ADREPORT_F(log_index_expected, this->of);
1462 ADREPORT_F(log_landings_expected, this->of);
1463 std::stringstream var_name;
1464 typename std::map<std::string, fims::Vector<fims::Vector<Type>>>::iterator
1465 rvit;
1466 for (rvit = report_vectors.begin(); rvit != report_vectors.end();
1467 ++rvit) {
1468 auto &x = rvit->second;
1469
1470 int outer_dim = x.size();
1471 int dim = 0;
1472 for (int i = 0; i < outer_dim; i++) {
1473 dim += x[i].size();
1474 }
1475 vector<Type> res(dim);
1476 int idx = 0;
1477 for (int i = 0; i < outer_dim; i++) {
1478 int inner_dim = x[i].size();
1479 for (int j = 0; j < inner_dim; j++) {
1480 res(idx) = x[i][j];
1481 idx += 1;
1482 }
1483 }
1484 this->of->reportvector.push(res, rvit->first.c_str());
1485 }
1486 }
1487#endif
1488 }
1489};
1490
1491} // namespace fims_popdy
1492
1493#endif
CatchAtAge is a class containing a catch-at-age model, which is just one of many potential fishery mo...
Definition catch_at_age.hpp:53
void CalculateSpawningBiomassRatio(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t year)
Calculate the spawning biomass ratio for a population and year.
Definition catch_at_age.hpp:537
std::map< std::string, fims::Vector< Type > >::iterator dq_iterator
Iterate through derived quantities.
Definition catch_at_age.hpp:112
std::map< std::string, fims::Vector< Type > >::iterator derived_quantities_iterator
Iterate the derived quantities.
Definition catch_at_age.hpp:66
Type CalculateSBPR0(std::shared_ptr< fims_popdy::Population< Type > > &population)
Calculates equilibrium spawning biomass per recruit.
Definition catch_at_age.hpp:568
virtual void Initialize()
Definition catch_at_age.hpp:153
std::vector< Type > ages
Definition catch_at_age.hpp:121
void CalculateUnfishedNumbersAA(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t i_agem1_yearm1, size_t age)
Calculates unfished numbers at age at year and age specific indices.
Definition catch_at_age.hpp:343
void CalculateBiomass(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t year, size_t age)
Calculates biomass for a population.
Definition catch_at_age.hpp:431
std::map< std::string, fims::Vector< fims::Vector< Type > > > report_vectors
A map of report vectors for the object. used to populate the report_vectors map in for submodule para...
Definition catch_at_age.hpp:118
std::set< uint32_t > & GetPopulationIds()
Get the population ids of the model.
Definition catch_at_age.hpp:245
std::map< uint32_t, std::map< std::string, fims::Vector< size_t > > >::iterator population_derived_quantities_dims_iterator
Used to iterate through population-based derived quantities dimensions.
Definition catch_at_age.hpp:98
std::map< uint32_t, std::shared_ptr< fims_popdy::Fleet< Type > > >::iterator fleet_iterator
Iterate through fleets.
Definition catch_at_age.hpp:106
void CalculateIndexWeightAA(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t year, size_t age)
Calculates biomass of fish for the index for a given fleet from a population.
Definition catch_at_age.hpp:860
void evaluate_age_comp()
Definition catch_at_age.hpp:877
void evaluate_length_comp()
Definition catch_at_age.hpp:945
virtual ~CatchAtAge()
Destroy the Catch At Age object.
Definition catch_at_age.hpp:147
CatchAtAge(const CatchAtAge &other)
Copy constructor for the CatchAtAge class.
Definition catch_at_age.hpp:138
void CalculateSpawningBiomass(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t year, size_t age)
Calculates spawning biomass for a population.
Definition catch_at_age.hpp:482
CatchAtAge()
Definition catch_at_age.hpp:126
virtual void Evaluate()
Evaluate the model.
Definition catch_at_age.hpp:1050
std::string name_m
The name of the model.
Definition catch_at_age.hpp:59
void CalculateUnfishedSpawningBiomass(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t year, size_t age)
Calculated unfished spawning biomass for a population.
Definition catch_at_age.hpp:509
void CalculateIndex(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t year, size_t age)
Calculates the index for a fleet from a population.
Definition catch_at_age.hpp:797
void CalculateUnfishedBiomass(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t year, size_t age)
Calculates the unfished biomass for a population.
Definition catch_at_age.hpp:455
std::map< uint32_t, std::map< std::string, fims::Vector< Type > > >::iterator fleet_derived_quantities_iterator
Used to iterate through fleet-based derived quantities.
Definition catch_at_age.hpp:74
void CalculateMaturityAA(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t age)
Calculates maturity at age, in proportion, for a population.
Definition catch_at_age.hpp:663
std::map< uint32_t, std::map< std::string, fims::Vector< Type > > >::iterator population_derived_quantities_iterator
Used to iterate through population-based derived quantities.
Definition catch_at_age.hpp:89
void CalculateNumbersAA(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t i_agem1_yearm1, size_t age)
Calculates numbers at age for a population.
Definition catch_at_age.hpp:300
void CalculateLandingsNumbersAA(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t year, size_t age)
Calculates numbers of fish for the landings for a given fleet from a population, year and age.
Definition catch_at_age.hpp:759
void CalculateRecruitment(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t year, size_t i_dev)
Calculates expected recruitment for a population.
Definition catch_at_age.hpp:621
void evaluate_landings()
Definition catch_at_age.hpp:1032
virtual void Report()
Definition catch_at_age.hpp:1193
void AddPopulation(uint32_t id)
Definition catch_at_age.hpp:240
void CalculateLandingsWeightAA(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t year, size_t age)
Calculates weight at age of the landings for a given fleet from a population.
Definition catch_at_age.hpp:728
std::vector< std::shared_ptr< fims_popdy::Population< Type > > > & GetPopulations()
Definition catch_at_age.hpp:252
std::map< uint32_t, std::map< std::string, fims::Vector< size_t > > >::iterator fleet_derived_quantities_dims_iterator
Used to iterate through fleet-based derived quantities dimensions.
Definition catch_at_age.hpp:82
void CalculateLandings(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t year, size_t age)
Calculates total catch (landings) by fleet and population for a given year by aggregating age-specifi...
Definition catch_at_age.hpp:688
void CalculateMortality(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t year, size_t age)
Calculates total mortality for a population.
Definition catch_at_age.hpp:394
void CalculateIndexNumbersAA(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t year, size_t age)
Calculates the numbers for the index for a fleet from a population.
Definition catch_at_age.hpp:828
void CalculateInitialNumbersAA(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t age)
Calculates initial numbers at age for index and age.
Definition catch_at_age.hpp:268
virtual void Prepare()
Definition catch_at_age.hpp:184
void evaluate_index()
Definition catch_at_age.hpp:1011
FisheryModelBase is a base class for fishery models in FIMS.
Definition fishery_model_base.hpp:77
uint32_t GetId()
Get the Id object.
Definition fishery_model_base.hpp:382
std::map< uint32_t, std::shared_ptr< fims_popdy::Fleet< Type > > > fleets
A map of fleets in the fishery model, indexed by fleet id. Unique instances to eliminate duplicate in...
Definition fishery_model_base.hpp:107
std::vector< std::shared_ptr< fims_popdy::Population< Type > > > populations
A vector of populations in the fishery model.
Definition fishery_model_base.hpp:101
std::string model_type_m
A string specifying the model type.
Definition fishery_model_base.hpp:91
DerivedQuantitiesMap & GetFleetDerivedQuantities()
Get the fleet derived quantities.
Definition fishery_model_base.hpp:213
std::set< uint32_t > population_ids
Unique identifier for the fishery model.
Definition fishery_model_base.hpp:96
DerivedQuantitiesMap & GetPopulationDerivedQuantities()
Get the population derived quantities.
Definition fishery_model_base.hpp:222
virtual void ResetVector(fims::Vector< Type > &v, Type value=0.0)
Reset a vector from start to end with a value.
Definition fishery_model_base.hpp:361
Defines the base class for all fishery models within the FIMS framework.
#define ADREPORT_F(name, F)
TMB macro that reports variables and uncertainties.
Definition interface.hpp:78
The population dynamics of FIMS.
Definition catch_at_age.hpp:41
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:239
Population class. Contains subpopulations that are divided into generic partitions (e....
Definition population.hpp:25