FIMS  v0.9.2
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] +=
438 dq_["numbers_at_age"][i_age_year] *
439 population->growth->evaluate(year, population->ages[age]);
440 }
441
458 size_t i_age_year, size_t year, size_t age) {
459 std::map<std::string, fims::Vector<Type>> &dq_ =
461
462 dq_["unfished_biomass"][year] +=
463 dq_["unfished_numbers_at_age"][i_age_year] *
464 population->growth->evaluate(year, population->ages[age]);
465 }
466
485 size_t i_age_year, size_t year, size_t age) {
486 std::map<std::string, fims::Vector<Type>> &dq_ =
488
489 dq_["spawning_biomass"][year] +=
490 population->proportion_female[age] * dq_["numbers_at_age"][i_age_year] *
491 dq_["proportion_mature_at_age"][i_age_year] *
492 population->growth->evaluate(year, population->ages[age]);
493 }
494
512 size_t i_age_year, size_t year, size_t age) {
513 std::map<std::string, fims::Vector<Type>> &dq_ =
515
516 dq_["unfished_spawning_biomass"][year] +=
517 population->proportion_female[age] *
518 dq_["unfished_numbers_at_age"][i_age_year] *
519 dq_["proportion_mature_at_age"][i_age_year] *
520 population->growth->evaluate(year, population->ages[age]);
521 }
522
539 std::shared_ptr<fims_popdy::Population<Type>> &population, size_t year) {
540 std::map<std::string, fims::Vector<Type>> &dq_ =
542 population->spawning_biomass_ratio[year] =
543 dq_["spawning_biomass"][year] / dq_["unfished_spawning_biomass"][0];
544 }
545
570 std::shared_ptr<fims_popdy::Population<Type>> &population) {
571 std::map<std::string, fims::Vector<Type>> &dq_ =
573
574 std::vector<Type> numbers_spr(population->n_ages, 1.0);
575 Type phi_0 = 0.0;
576 phi_0 += numbers_spr[0] * population->proportion_female[0] *
577 dq_["proportion_mature_at_age"][0] *
578 population->growth->evaluate(0, population->ages[0]);
579 for (size_t a = 1; a < (population->n_ages - 1); a++) {
580 numbers_spr[a] = numbers_spr[a - 1] * fims_math::exp(-population->M[a]);
581 phi_0 += numbers_spr[a] * population->proportion_female[a] *
582 dq_["proportion_mature_at_age"][a] *
583 population->growth->evaluate(0, population->ages[a]);
584 }
585
586 numbers_spr[population->n_ages - 1] =
587 (numbers_spr[population->n_ages - 2] *
588 fims_math::exp(-population->M[population->n_ages - 2])) /
589 (1 - fims_math::exp(-population->M[population->n_ages - 1]));
590 phi_0 += 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(
594 0, population->ages[population->n_ages - 1]);
595
596 return phi_0;
597 }
598
624 size_t i_age_year, size_t year, size_t i_dev) {
625 std::map<std::string, fims::Vector<Type>> &dq_ =
627
629
630 if (i_dev == population->n_years) {
631 dq_["numbers_at_age"][i_age_year] =
632 population->recruitment->evaluate_mean(
633 dq_["spawning_biomass"][year - 1], phi0);
634 /*the final year of the time series has no data to inform recruitment
635 devs, so this value is set to the mean recruitment.*/
636 } else {
637 // Why are we using evaluate_mean, how come a virtual function was
638 // changed? AMH: there are now two virtual functions: evaluate_mean and
639 // evaluate_process (see below)
640 population->recruitment->log_expected_recruitment[year - 1] =
641 fims_math::log(population->recruitment->evaluate_mean(
642 dq_["spawning_biomass"][year - 1], phi0));
643
644 dq_["numbers_at_age"][i_age_year] = fims_math::exp(
645 population->recruitment->process->evaluate_process(year - 1));
646 }
647
648 dq_["expected_recruitment"][year] = dq_["numbers_at_age"][i_age_year];
649 }
650
666 size_t i_age_year, size_t age) {
667 std::map<std::string, fims::Vector<Type>> &dq_ =
669
670 dq_["proportion_mature_at_age"][i_age_year] =
671 population->maturity->evaluate(population->ages[age]);
672 }
673
690 std::shared_ptr<fims_popdy::Population<Type>> &population, size_t year,
691 size_t age) {
692 std::map<std::string, fims::Vector<Type>> &pdq_ =
694
695 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
696 std::map<std::string, fims::Vector<Type>> &fdq_ =
697 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
698 size_t i_age_year = year * population->n_ages + age;
699
700 pdq_["total_landings_weight"][year] +=
701 fdq_["landings_weight_at_age"][i_age_year];
702
703 fdq_["landings_weight"][year] +=
704 fdq_["landings_weight_at_age"][i_age_year];
705
706 pdq_["total_landings_numbers"][year] +=
707 fdq_["landings_numbers_at_age"][i_age_year];
708
709 fdq_["landings_numbers"][year] +=
710 fdq_["landings_numbers_at_age"][i_age_year];
711 }
712 }
713
730 std::shared_ptr<fims_popdy::Population<Type>> &population, size_t year,
731 size_t age) {
732 int i_age_year = year * population->n_ages + age;
733 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
734 std::map<std::string, fims::Vector<Type>> &fdq_ =
735 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
736
737 fdq_["landings_weight_at_age"][i_age_year] =
738 fdq_["landings_numbers_at_age"][i_age_year] *
739 population->growth->evaluate(year, population->ages[age]);
740 }
741 }
742
762 size_t i_age_year, size_t year, size_t age) {
763 std::map<std::string, fims::Vector<Type>> &pdq_ =
765
766 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
767 std::map<std::string, fims::Vector<Type>> &fdq_ =
768 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
769
770 // Baranov Catch Equation
771 fdq_["landings_numbers_at_age"][i_age_year] +=
772 (population->fleets[fleet_]->Fmort[year] *
773 population->f_multiplier[year] *
774 population->fleets[fleet_]->selectivity->evaluate(
775 population->ages[age], year)) /
776 pdq_["mortality_Z"][i_age_year] * pdq_["numbers_at_age"][i_age_year] *
777 (1 - fims_math::exp(-(pdq_["mortality_Z"][i_age_year])));
778 }
779 }
780
799 size_t i_age_year, size_t year, size_t age) {
800 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
801 std::map<std::string, fims::Vector<Type>> &fdq_ =
802 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
803
804 fdq_["index_weight"][year] += fdq_["index_weight_at_age"][i_age_year];
805
806 fdq_["index_numbers"][year] += fdq_["index_numbers_at_age"][i_age_year];
807 }
808 }
809
831 size_t i_age_year, size_t year, size_t age) {
832 std::map<std::string, fims::Vector<Type>> &pdq_ =
834
835 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
836 std::map<std::string, fims::Vector<Type>> &fdq_ =
837 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
838
839 fdq_["index_numbers_at_age"][i_age_year] +=
840 (population->fleets[fleet_]->q.get_force_scalar(year) *
841 population->fleets[fleet_]->selectivity->evaluate(
842 population->ages[age], year)) *
843 pdq_["numbers_at_age"][i_age_year];
844 }
845 }
846
862 std::shared_ptr<fims_popdy::Population<Type>> &population, size_t year,
863 size_t age) {
864 int i_age_year = year * population->n_ages + age;
865 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
866 std::map<std::string, fims::Vector<Type>> &fdq_ =
867 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
868
869 fdq_["index_weight_at_age"][i_age_year] =
870 fdq_["index_numbers_at_age"][i_age_year] *
871 population->growth->evaluate(year, population->ages[age]);
872 }
873 }
874
880 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
881 std::map<std::string, fims::Vector<Type>> &fdq_ =
882 this->GetFleetDerivedQuantities((*fit).second->GetId());
883
884 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
885 for (size_t y = 0; y < fleet->n_years; y++) {
886 Type sum = static_cast<Type>(0.0);
887 Type sum_obs = static_cast<Type>(0.0);
888 // robust_add is a small value to add to expected composition
889 // proportions at age to stabilize likelihood calculations
890 // when the expected proportions are close to zero.
891 // Type robust_add = static_cast<Type>(0.0); // zeroed out before
892 // testing 0.0001; sum robust is used to calculate the total sum of
893 // robust additions to ensure that proportions sum to 1. Type robust_sum
894 // = static_cast<Type>(1.0);
895
896 for (size_t a = 0; a < fleet->n_ages; a++) {
897 size_t i_age_year = y * fleet->n_ages + a;
898 // Here we have a check to determine if the age comp
899 // should be calculated from the retained landings or
900 // the total population. These values are slightly different.
901 // In the future this will have more impact as we implement
902 // timing rather than everything occurring at the start of
903 // the year.
904 if (fleet->fleet_observed_landings_data_id_m == -999) {
905 fdq_["agecomp_expected"][i_age_year] =
906 fdq_["index_numbers_at_age"][i_age_year];
907 } else {
908 fdq_["agecomp_expected"][i_age_year] =
909 fdq_["landings_numbers_at_age"][i_age_year];
910 }
911 sum += fdq_["agecomp_expected"][i_age_year];
912 // robust_sum -= robust_add;
913
914 // This sums over the observed age composition data so that
915 // the expected age composition can be rescaled to match the
916 // total number observed. The check for na values should not
917 // be needed as individual years should not have missing data.
918 // This is need to be re-explored if/when we modify FIMS to
919 // allow for composition bins that do not match the population
920 // bins.
921 if (fleet->fleet_observed_agecomp_data_id_m != -999) {
922 if (fleet->observed_agecomp_data->at(i_age_year) !=
923 fleet->observed_agecomp_data->na_value) {
924 sum_obs += fleet->observed_agecomp_data->at(i_age_year);
925 }
926 }
927 }
928 for (size_t a = 0; a < fleet->n_ages; a++) {
929 size_t i_age_year = y * fleet->n_ages + a;
930 fdq_["agecomp_proportion"][i_age_year] =
931 fdq_["agecomp_expected"][i_age_year] / sum;
932 // robust_add + robust_sum * this->agecomp_expected[i_age_year] / sum;
933
934 if (fleet->fleet_observed_agecomp_data_id_m != -999) {
935 fdq_["agecomp_expected"][i_age_year] =
936 fdq_["agecomp_proportion"][i_age_year] * sum_obs;
937 }
938 }
939 }
940 }
941 }
942
948 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
949 std::map<std::string, fims::Vector<Type>> &fdq_ =
950 this->GetFleetDerivedQuantities((*fit).second->GetId());
951
952 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
953
954 if (fleet->n_lengths > 0) {
955 for (size_t y = 0; y < fleet->n_years; y++) {
956 Type sum = static_cast<Type>(0.0);
957 Type sum_obs = static_cast<Type>(0.0);
958 // robust_add is a small value to add to expected composition
959 // proportions at age to stabilize likelihood calculations
960 // when the expected proportions are close to zero.
961 // Type robust_add = static_cast<Type>(0.0); // 0.0001; zeroed out
962 // before testing sum robust is used to calculate the total sum of
963 // robust additions to ensure that proportions sum to 1. Type
964 // robust_sum = static_cast<Type>(1.0);
965 for (size_t l = 0; l < fleet->n_lengths; l++) {
966 size_t i_length_year = y * fleet->n_lengths + l;
967 for (size_t a = 0; a < fleet->n_ages; a++) {
968 size_t i_age_year = y * fleet->n_ages + a;
969 size_t i_length_age = a * fleet->n_lengths + l;
970 fdq_["lengthcomp_expected"][i_length_year] +=
971 fdq_["agecomp_expected"][i_age_year] *
972 fleet->age_to_length_conversion[i_length_age];
973
974 fdq_["landings_numbers_at_length"][i_length_year] +=
975 fdq_["landings_numbers_at_age"][i_age_year] *
976 fleet->age_to_length_conversion[i_length_age];
977
978 fdq_["index_numbers_at_length"][i_length_year] +=
979 fdq_["index_numbers_at_age"][i_age_year] *
980 fleet->age_to_length_conversion[i_length_age];
981 }
982
983 sum += fdq_["lengthcomp_expected"][i_length_year];
984 // robust_sum -= robust_add;
985
986 if (fleet->fleet_observed_lengthcomp_data_id_m != -999) {
987 if (fleet->observed_lengthcomp_data->at(i_length_year) !=
988 fleet->observed_lengthcomp_data->na_value) {
989 sum_obs += fleet->observed_lengthcomp_data->at(i_length_year);
990 }
991 }
992 }
993 for (size_t l = 0; l < fleet->n_lengths; l++) {
994 size_t i_length_year = y * fleet->n_lengths + l;
995 fdq_["lengthcomp_proportion"][i_length_year] =
996 fdq_["lengthcomp_expected"][i_length_year] / sum;
997 // robust_add + robust_sum *
998 // this->lengthcomp_expected[i_length_year] / sum;
999 if (fleet->fleet_observed_lengthcomp_data_id_m != -999) {
1000 fdq_["lengthcomp_expected"][i_length_year] =
1001 fdq_["lengthcomp_proportion"][i_length_year] * sum_obs;
1002 }
1003 }
1004 }
1005 }
1006 }
1007 }
1008
1014 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
1015 std::map<std::string, fims::Vector<Type>> &fdq_ =
1016 this->GetFleetDerivedQuantities((*fit).second->GetId());
1017 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
1018
1019 for (size_t i = 0; i < fdq_["index_numbers"].size(); i++) {
1020 if (fleet->observed_index_units == "number") {
1021 fdq_["index_expected"][i] = fdq_["index_numbers"][i];
1022 } else {
1023 fdq_["index_expected"][i] = fdq_["index_weight"][i];
1024 }
1025 fdq_["log_index_expected"][i] = log(fdq_["index_expected"][i]);
1026 }
1027 }
1028 }
1029
1035 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
1036 std::map<std::string, fims::Vector<Type>> &fdq_ =
1037 this->GetFleetDerivedQuantities((*fit).second->GetId());
1038 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
1039
1040 for (size_t i = 0; i < fdq_["landings_weight"].size(); i++) {
1041 if (fleet->observed_landings_units == "number") {
1042 fdq_["landings_expected"][i] = fdq_["landings_numbers"][i];
1043 } else {
1044 fdq_["landings_expected"][i] = fdq_["landings_weight"][i];
1045 }
1046 fdq_["log_landings_expected"][i] = log(fdq_["landings_expected"][i]);
1047 }
1048 }
1049 }
1050
1051 virtual void Evaluate() {
1052 /*
1053 Sets derived vectors to zero
1054 Performs parameters transformations
1055 Sets recruitment deviations to mean 0.
1056 */
1057 Prepare();
1058 /*
1059 start at year=0, age=0;
1060 here year 0 is the estimated initial population structure and age 0 are
1061 recruits loops start at zero with if statements inside to specify unique
1062 code for initial structure and recruitment 0 loops. Could also have started
1063 loops at 1 with initial structure and recruitment setup outside the loops.
1064
1065 year loop is extended to <= n_years because SSB is calculated as the start
1066 of the year value and by extending one extra year we get estimates of the
1067 population structure at the end of the final year. An alternative approach
1068 would be to keep initial numbers at age in it's own vector and each year to
1069 include the population structure at the end of the year. This is likely a
1070 null point given that we are planning to modify to an event/stanza based
1071 structure in later milestones which will eliminate this confusion by
1072 explicitly referencing the exact date (or period of averaging) at which any
1073 calculation or output is being made.
1074 */
1075 for (size_t p = 0; p < this->populations.size(); p++) {
1076 std::shared_ptr<fims_popdy::Population<Type>> &population =
1077 this->populations[p];
1078 std::map<std::string, fims::Vector<Type>> &pdq_ =
1079 this->GetPopulationDerivedQuantities(population->GetId());
1080 // CAAPopulationProxy<Type>& population = this->populations_proxies[p];
1081
1082 for (size_t y = 0; y <= population->n_years; y++) {
1083 for (size_t a = 0; a < population->n_ages; a++) {
1084 /*
1085 index naming defines the dimensional folding structure
1086 i.e. i_age_year is referencing folding over years and ages.
1087 */
1088 size_t i_age_year = y * population->n_ages + a;
1089 /*
1090 Mortality rates are not estimated in the final year which is
1091 used to show expected population structure at the end of the model
1092 period. This is because biomass in year i represents biomass at the
1093 start of the year. Should we add complexity to track more values such
1094 as start, mid, and end biomass in all years where, start biomass=end
1095 biomass of the previous year? Referenced above, this is probably not
1096 worth exploring as later milestone changes will eliminate this
1097 confusion.
1098 */
1099 if (y < population->n_years) {
1100 /*
1101 First thing we need is total mortality aggregated across all fleets
1102 to inform the subsequent catch and change in numbers at age
1103 calculations. This is only calculated for years < n_years as these
1104 are the model estimated years with data. The year loop extends to
1105 y=n_years so that population numbers at age and SSB can be
1106 calculated at the end of the last year of the model
1107 */
1109 }
1111 /* if statements needed because some quantities are only needed
1112 for the first year and/or age, so these steps are included here.
1113 */
1114 if (y == 0) {
1115 // Initial numbers at age is a user input or estimated parameter
1116 // vector.
1118
1119 if (a == 0) {
1120 /*
1121 Expected recruitment in year 0 is numbers at age 0 in year 0.
1122 */
1123 pdq_["expected_recruitment"][y] =
1124 pdq_["numbers_at_age"][i_age_year];
1125 pdq_["unfished_numbers_at_age"][i_age_year] =
1126 fims_math::exp(population->recruitment->log_rzero[0]);
1127 } else {
1129 }
1130
1131 } else {
1132 if (a == 0) {
1133 // Set the nrecruits for age a=0 year y (use pointers instead of
1134 // functional returns) assuming fecundity = 1 and 50:50 sex ratio
1136 pdq_["unfished_numbers_at_age"][i_age_year] =
1137 fims_math::exp(population->recruitment->log_rzero[0]);
1138 } else {
1139 size_t i_agem1_yearm1 = (y - 1) * population->n_ages + (a - 1);
1142 a);
1143 }
1144 }
1145
1146 /*
1147 Fished and unfished biomass vectors are summing biomass at
1148 age across ages.
1149 */
1150
1152
1154
1155 /*
1156 Fished and unfished spawning biomass vectors are summing biomass at
1157 age across ages to allow calculation of recruitment in the next
1158 year.
1159 */
1160
1162
1164
1165 /*
1166 Here composition, total catch, and index values are calculated for all
1167 years with reference data. They are not calculated for y=n_years as
1168 there is this is just to get final population structure at the end of
1169 the terminal year.
1170 */
1171 if (y < population->n_years) {
1175
1179 }
1180 }
1181 /* Calculate spawning biomass depletion ratio */
1183 }
1184 }
1189 }
1194 virtual void Report() {
1195 int n_fleets = this->fleets.size();
1196 int n_pops = this->populations.size();
1197#ifdef TMB_MODEL
1198 if (this->do_reporting == true) {
1199 report_vectors.clear();
1200 // initialize population vectors
1216
1217 // initialize fleet vectors
1237
1238 // initiate population index for structuring report out objects
1239 int pop_idx = 0;
1240 for (size_t p = 0; p < this->populations.size(); p++) {
1241 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1243 biomass_p(pop_idx) = derived_quantities["biomass"].to_tmb();
1245 derived_quantities["expected_recruitment"].to_tmb();
1246 mortality_F_p(pop_idx) = derived_quantities["mortality_F"].to_tmb();
1247 mortality_M_p(pop_idx) = derived_quantities["mortality_M"].to_tmb();
1248 mortality_Z_p(pop_idx) = derived_quantities["mortality_Z"].to_tmb();
1250 derived_quantities["numbers_at_age"].to_tmb();
1252 derived_quantities["proportion_mature_at_age"].to_tmb();
1254 derived_quantities["spawning_biomass"].to_tmb();
1256 derived_quantities["sum_selectivity"].to_tmb();
1258 derived_quantities["total_landings_numbers"].to_tmb();
1260 derived_quantities["total_landings_weight"].to_tmb();
1262 derived_quantities["unfished_biomass"].to_tmb();
1264 derived_quantities["unfished_numbers_at_age"].to_tmb();
1266 derived_quantities["unfished_spawning_biomass"].to_tmb();
1268 this->populations[pop_idx]->spawning_biomass_ratio.to_tmb();
1269
1270 pop_idx += 1;
1271 }
1272
1273 // initiate fleet index for structuring report out objects
1274 int fleet_idx = 0;
1276 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
1277 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
1278 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1279 this->GetFleetDerivedQuantities(fleet->GetId());
1280
1282 derived_quantities["agecomp_expected"].to_tmb();
1284 derived_quantities["agecomp_proportion"].to_tmb();
1285 catch_index_f(fleet_idx) = derived_quantities["catch_index"].to_tmb();
1287 derived_quantities["index_expected"].to_tmb();
1289 derived_quantities["index_numbers"].to_tmb();
1291 derived_quantities["index_numbers_at_age"].to_tmb();
1293 derived_quantities["index_numbers_at_length"].to_tmb();
1294 index_weight_f(fleet_idx) = derived_quantities["index_weight"].to_tmb();
1296 derived_quantities["index_weight_at_age"].to_tmb();
1298 derived_quantities["landings_expected"].to_tmb();
1300 derived_quantities["landings_numbers"].to_tmb();
1302 derived_quantities["landings_numbers_at_age"].to_tmb();
1304 derived_quantities["landings_numbers_at_length"].to_tmb();
1306 derived_quantities["landings_weight"].to_tmb();
1308 derived_quantities["landings_weight_at_age"].to_tmb();
1309 // length_comp_expected_f(fleet_idx) =
1310 // derived_quantities["length_comp_expected"];
1311 // length_comp_proportion_f(fleet_idx) =
1312 // derived_quantities["length_comp_proportion"];
1314 derived_quantities["lengthcomp_expected"].to_tmb();
1316 derived_quantities["lengthcomp_proportion"].to_tmb();
1318 derived_quantities["log_index_expected"].to_tmb();
1320 derived_quantities["log_landings_expected"].to_tmb();
1321 fleet_idx += 1;
1322 }
1323
1324 vector<Type> biomass = ADREPORTvector(biomass_p);
1325 vector<Type> expected_recruitment =
1331 vector<Type> proportion_mature_at_age =
1335 vector<Type> total_landings_numbers =
1337 vector<Type> total_landings_weight =
1340 vector<Type> unfished_numbers_at_age =
1342 vector<Type> unfished_spawning_biomass =
1344 vector<Type> spawning_biomass_ratio =
1346
1347 vector<Type> agecomp_expected = ADREPORTvector(agecomp_expected_f);
1348 vector<Type> agecomp_proportion = ADREPORTvector(agecomp_proportion_f);
1352 vector<Type> index_numbers_at_age =
1354 vector<Type> index_numbers_at_length =
1360 vector<Type> landings_numbers_at_age =
1362 vector<Type> landings_numbers_at_length =
1365 vector<Type> landings_weight_at_age =
1367 // vector<Type> length_comp_expected =
1368 // ADREPORTvector(length_comp_expected_f); vector<Type>
1369 // length_comp_proportion = ADREPORTvector(length_comp_proportion_f);
1370 vector<Type> lengthcomp_expected = ADREPORTvector(lengthcomp_expected_f);
1371 vector<Type> lengthcomp_proportion =
1373 vector<Type> log_index_expected = ADREPORTvector(log_index_expected_f);
1374 vector<Type> log_landings_expected =
1376 // populations
1377 // report
1378 FIMS_REPORT_F_("biomass", biomass_p, this->of);
1379 FIMS_REPORT_F_("expected_recruitment", expected_recruitment_p, this->of);
1380 FIMS_REPORT_F_("mortality_F", mortality_F_p, this->of);
1381 FIMS_REPORT_F_("mortality_M", mortality_M_p, this->of);
1382 FIMS_REPORT_F_("mortality_Z", mortality_Z_p, this->of);
1383 FIMS_REPORT_F_("numbers_at_age", numbers_at_age_p, this->of);
1384 FIMS_REPORT_F_("proportion_mature_at_age", proportion_mature_at_age_p,
1385 this->of);
1386 FIMS_REPORT_F_("spawning_biomass", spawning_biomass_p, this->of);
1387 FIMS_REPORT_F_("sum_selectivity", sum_selectivity_p, this->of);
1388 FIMS_REPORT_F_("total_landings_numbers", total_landings_numbers_p,
1389 this->of);
1390 FIMS_REPORT_F_("total_landings_weight", total_landings_weight_p,
1391 this->of);
1392 FIMS_REPORT_F_("unfished_biomass", unfished_biomass_p, this->of);
1393 FIMS_REPORT_F_("unfished_numbers_at_age", unfished_numbers_at_age_p,
1394 this->of);
1395 FIMS_REPORT_F_("unfished_spawning_biomass", unfished_spawning_biomass_p,
1396 this->of);
1397 FIMS_REPORT_F_("spawning_biomass_ratio", spawning_biomass_ratio_p,
1398 this->of);
1399
1400 // adreport
1401 ADREPORT_F(biomass, this->of);
1402 ADREPORT_F(expected_recruitment, this->of);
1403 ADREPORT_F(mortality_F, this->of);
1404 ADREPORT_F(mortality_M, this->of);
1405 ADREPORT_F(mortality_Z, this->of);
1406 ADREPORT_F(numbers_at_age, this->of);
1407 ADREPORT_F(proportion_mature_at_age, this->of);
1408 ADREPORT_F(spawning_biomass, this->of);
1409 ADREPORT_F(sum_selectivity, this->of);
1410 ADREPORT_F(total_landings_numbers, this->of);
1411 ADREPORT_F(total_landings_weight, this->of);
1412 ADREPORT_F(unfished_biomass, this->of);
1413 ADREPORT_F(unfished_numbers_at_age, this->of);
1414 ADREPORT_F(unfished_spawning_biomass, this->of);
1415 ADREPORT_F(spawning_biomass_ratio, this->of);
1416
1417 // fleets
1418 // report
1419 FIMS_REPORT_F_("agecomp_expected", agecomp_expected_f, this->of);
1420 FIMS_REPORT_F_("agecomp_proportion", agecomp_proportion_f, this->of);
1421 FIMS_REPORT_F_("catch_index", catch_index_f, this->of);
1422 FIMS_REPORT_F_("index_expected", index_expected_f, this->of);
1423 FIMS_REPORT_F_("index_numbers", index_numbers_f, this->of);
1424 FIMS_REPORT_F_("index_numbers_at_age", index_numbers_at_age_f, this->of);
1425 FIMS_REPORT_F_("index_numbers_at_length", index_numbers_at_length_f,
1426 this->of);
1427 FIMS_REPORT_F_("index_weight", index_weight_f, this->of);
1428 FIMS_REPORT_F_("index_weight_at_age", index_weight_at_age_f, this->of);
1429 FIMS_REPORT_F_("landings_expected", landings_expected_f, this->of);
1430 FIMS_REPORT_F_("landings_numbers", landings_numbers_f, this->of);
1431 FIMS_REPORT_F_("landings_numbers_at_age", landings_numbers_at_age_f,
1432 this->of);
1433 FIMS_REPORT_F_("landings_numbers_at_length", landings_numbers_at_length_f,
1434 this->of);
1435 FIMS_REPORT_F_("landings_weight", landings_weight_f, this->of);
1436 FIMS_REPORT_F_("landings_weight_at_age", landings_weight_at_age_f,
1437 this->of);
1438 FIMS_REPORT_F_("lengthcomp_expected", lengthcomp_expected_f, this->of);
1439 FIMS_REPORT_F_("lengthcomp_proportion", lengthcomp_proportion_f,
1440 this->of);
1441 FIMS_REPORT_F_("log_index_expected", log_index_expected_f, this->of);
1442 FIMS_REPORT_F_("log_landings_expected", log_landings_expected_f,
1443 this->of);
1444 // adreport
1445 ADREPORT_F(agecomp_expected, this->of);
1446 ADREPORT_F(agecomp_proportion, this->of);
1447 ADREPORT_F(catch_index, this->of);
1448 ADREPORT_F(index_expected, this->of);
1449 ADREPORT_F(index_numbers, this->of);
1450 ADREPORT_F(index_numbers_at_age, this->of);
1451 ADREPORT_F(index_numbers_at_length, this->of);
1452 ADREPORT_F(index_weight, this->of);
1453 ADREPORT_F(index_weight_at_age, this->of);
1454 ADREPORT_F(landings_expected, this->of);
1455 ADREPORT_F(landings_numbers, this->of);
1456 ADREPORT_F(landings_numbers_at_age, this->of);
1457 ADREPORT_F(landings_numbers_at_length, this->of);
1458 ADREPORT_F(landings_weight, this->of);
1459 ADREPORT_F(landings_weight_at_age, this->of);
1460 ADREPORT_F(lengthcomp_expected, this->of);
1461 ADREPORT_F(lengthcomp_proportion, this->of);
1462 ADREPORT_F(log_index_expected, this->of);
1463 ADREPORT_F(log_landings_expected, this->of);
1464 std::stringstream var_name;
1465 typename std::map<std::string, fims::Vector<fims::Vector<Type>>>::iterator
1466 rvit;
1467 for (rvit = report_vectors.begin(); rvit != report_vectors.end();
1468 ++rvit) {
1469 auto &x = rvit->second;
1470
1471 int outer_dim = x.size();
1472 int dim = 0;
1473 for (int i = 0; i < outer_dim; i++) {
1474 dim += x[i].size();
1475 }
1476 vector<Type> res(dim);
1477 int idx = 0;
1478 for (int i = 0; i < outer_dim; i++) {
1479 int inner_dim = x[i].size();
1480 for (int j = 0; j < inner_dim; j++) {
1481 res(idx) = x[i][j];
1482 idx += 1;
1483 }
1484 }
1485 this->of->reportvector.push(res, rvit->first.c_str());
1486 }
1487 }
1488#endif
1489 }
1490};
1491
1492} // namespace fims_popdy
1493
1494#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:538
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:569
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:861
void evaluate_age_comp()
Definition catch_at_age.hpp:878
void evaluate_length_comp()
Definition catch_at_age.hpp:946
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:483
CatchAtAge()
Definition catch_at_age.hpp:126
virtual void Evaluate()
Evaluate the model.
Definition catch_at_age.hpp:1051
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:510
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:798
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:456
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:664
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:760
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:622
void evaluate_landings()
Definition catch_at_age.hpp:1033
virtual void Report()
Definition catch_at_age.hpp:1194
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:729
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:689
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:829
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:1012
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:79
The population dynamics of FIMS.
Definition catch_at_age.hpp:41
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:235
Population class. Contains subpopulations that are divided into generic partitions (e....
Definition population.hpp:25