FIMS  v0.11.0
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 <algorithm>
12#include <cmath>
13#include <set>
14#include <regex>
15#include <stdexcept>
16
19
20/* Dictionary block for shared parameter snippet documentations.
21 * Referenced in function docs via @snippet{doc} this snippet_id.
22 [param_population]
23 @param population Shared pointer to the population object.
24 [param_population]
25 [param_i_age_year]
26 @param i_age_year Dimension folded index for age and year.
27 [param_i_age_year]
28 [param_year]
29 @param year Year index.
30 [param_year]
31 [param_age]
32 @param age Age index.
33 [param_age]
34 [param_i_agem1_yearm1]
35 @param i_agem1_yearm1 Dimension folded index for age-1 and year-1.
36 [param_i_agem1_yearm1]
37 [param_i_dev]
38 @param i_dev Index to log_recruit_dev of vector length n_years-1.
39 [param_i_dev]
40 [param_other]
41 @param other The other CatchAtAge object to copy from.
42 [param_other]
43 */
44
45namespace fims_popdy {
46
47template <typename Type>
57class CatchAtAge : public FisheryModelBase<Type> {
58 public:
63 std::string name_m;
64
69 typedef typename std::map<std::string, fims::Vector<Type>>::iterator
71
76 typedef typename std::map<uint32_t,
77 std::map<std::string, fims::Vector<Type>>>::iterator
79
83 typedef
84 typename std::map<uint32_t,
85 std::map<std::string, fims::Vector<size_t>>>::iterator
91 typedef typename std::map<uint32_t,
92 std::map<std::string, fims::Vector<Type>>>::iterator
94
99 typedef
100 typename std::map<uint32_t,
101 std::map<std::string, fims::Vector<size_t>>>::iterator
103
108 typedef typename std::map<uint32_t,
109 std::shared_ptr<fims_popdy::Fleet<Type>>>::iterator
115 typedef
116 typename std::map<std::string, fims::Vector<Type>>::iterator dq_iterator;
122 std::map<std::string, fims::Vector<fims::Vector<Type>>> report_vectors;
131
132 public:
133 std::vector<Type> ages;
139 std::stringstream ss;
140 ss << "caa_" << this->GetId() << "_";
141 this->name_m = ss.str();
142 this->model_type_m = "caa";
143 }
144
150 CatchAtAge(const CatchAtAge &other)
151 : FisheryModelBase<Type>(other),
152 name_m(other.name_m),
153 ages(other.ages),
156 this->model_type_m = "caa";
157 }
158
163 virtual ~CatchAtAge() {}
164
169 virtual void Initialize() {
170 for (size_t p = 0; p < this->populations.size(); p++) {
171 if (this->populations[p]->proportion_female.size() == 0) {
172 this->populations[p]->proportion_female.resize(1);
173 this->populations[p]->proportion_female[0] = static_cast<Type>(0.5);
174 }
175
176 this->populations[p]->M.resize(this->populations[p]->n_years *
177 this->populations[p]->n_ages);
178
179 this->populations[p]->f_multiplier.resize(this->populations[p]->n_years);
180
181 this->populations[p]->spawning_biomass_ratio.resize(
182 (this->populations[p]->n_years + 1));
183 }
184
185 for (fleet_iterator fit = this->fleets.begin(); fit != this->fleets.end();
186 ++fit) {
187 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
188
189 if (fleet->log_q.size() == 0) {
190 fleet->log_q.resize(1);
191 fleet->log_q[0] = static_cast<Type>(0.0);
192 }
193 fleet->q.resize(fleet->log_q.size());
194 fleet->Fmort.resize(fleet->n_years);
195 }
196 }
197
202 virtual void Prepare() {
203 for (size_t p = 0; p < this->populations.size(); p++) {
204 std::shared_ptr<fims_popdy::Population<Type>> &population =
205 this->populations[p];
206
207 auto &derived_quantities =
208 this->GetPopulationDerivedQuantities(population->GetId());
209
210 // Reset the derived quantities for the population
211 for (auto &kv : derived_quantities) {
212 this->ResetVector(kv.second);
213 }
214
215 // Transformation Section
216 for (size_t age = 0; age < population->n_ages; age++) {
217 for (size_t year = 0; year < population->n_years; year++) {
218 size_t i_age_year = age * population->n_years + year;
219 population->M[i_age_year] =
220 fims_math::exp(population->log_M[i_age_year]);
221 }
222 }
223
224 for (size_t year = 0; year < population->n_years; year++) {
225 population->f_multiplier[year] =
226 fims_math::exp(population->log_f_multiplier[year]);
227 }
228 }
229
230 for (fleet_iterator fit = this->fleets.begin(); fit != this->fleets.end();
231 ++fit) {
232 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
233 auto &derived_quantities =
234 this->GetFleetDerivedQuantities(fleet->GetId());
235
236 for (auto &kv : derived_quantities) {
237 this->ResetVector(kv.second);
238 }
239
240 // Transformation Section
241 for (size_t i = 0; i < fleet->log_q.size(); i++) {
242 fleet->q[i] = fims_math::exp(fleet->log_q[i]);
243 }
244
245 for (size_t year = 0; year < fleet->n_years; year++) {
246 fleet->Fmort[year] = fims_math::exp(fleet->log_Fmort[year]);
247 }
248 }
249 }
250
253 void AddPopulation(uint32_t id) { this->population_ids.insert(id); }
254
258 std::set<uint32_t> &GetPopulationIds() { return this->population_ids; }
259
265 std::vector<std::shared_ptr<fims_popdy::Population<Type>>> &GetPopulations() {
266 return this->populations;
267 }
268
282 std::shared_ptr<fims_popdy::Population<Type>> &population,
283 size_t i_age_year, size_t age) {
284 std::map<std::string, fims::Vector<Type>> &dq_ =
285 this->GetPopulationDerivedQuantities(population->GetId());
286
287 dq_["numbers_at_age"][i_age_year] =
288 fims_math::exp(population->log_init_naa[age]);
289 }
290
314 std::shared_ptr<fims_popdy::Population<Type>> &population,
315 size_t i_age_year, size_t i_agem1_yearm1, size_t age) {
316 // using Z from previous age/year
317
318 std::map<std::string, fims::Vector<Type>> &dq_ =
319 this->GetPopulationDerivedQuantities(population->GetId());
320
321 dq_["numbers_at_age"][i_age_year] =
322 dq_["numbers_at_age"][i_agem1_yearm1] *
323 (fims_math::exp(-dq_["mortality_Z"][i_agem1_yearm1]));
324
325 // Plus group calculation
326 if (age == (population->n_ages - 1)) {
327 dq_["numbers_at_age"][i_age_year] =
328 dq_["numbers_at_age"][i_age_year] +
329 dq_["numbers_at_age"][i_agem1_yearm1 + 1] *
330 (fims_math::exp(-dq_["mortality_Z"][i_agem1_yearm1 + 1]));
331 }
332 }
333
357 std::shared_ptr<fims_popdy::Population<Type>> &population,
358 size_t i_age_year, size_t i_agem1_yearm1, size_t age) {
359 std::map<std::string, fims::Vector<Type>> &dq_ =
360 this->GetPopulationDerivedQuantities(population->GetId());
361
362 // using M from previous age/year
363 dq_["unfished_numbers_at_age"][i_age_year] =
364 dq_["unfished_numbers_at_age"][i_agem1_yearm1] *
365 (fims_math::exp(-population->M[i_agem1_yearm1]));
366
367 // Plus group calculation
368 if (age == (population->n_ages - 1)) {
369 dq_["unfished_numbers_at_age"][i_age_year] =
370 dq_["unfished_numbers_at_age"][i_age_year] +
371 dq_["unfished_numbers_at_age"][i_agem1_yearm1 + 1] *
372 (fims_math::exp(-population->M[i_agem1_yearm1 + 1]));
373 }
374 }
375
401 std::shared_ptr<fims_popdy::Population<Type>> &population,
402 size_t i_age_year, size_t age) {
403 std::map<std::string, fims::Vector<Type>> &dq_ =
404 this->GetPopulationDerivedQuantities(population->GetId());
405
406 // In year 0, the previous age is the previous element of the same year
407 size_t i_agem1 = i_age_year - 1;
408 dq_["unfished_numbers_at_age"][i_age_year] =
409 dq_["unfished_numbers_at_age"][i_agem1] *
410 fims_math::exp(-population->M[i_agem1]);
411
412 if (age == (population->n_ages - 1)) {
413 dq_["unfished_numbers_at_age"][i_age_year] =
414 dq_["unfished_numbers_at_age"][i_age_year] /
415 (1.0 - fims_math::exp(-population->M[i_age_year]));
416 }
417 }
418
451 std::shared_ptr<fims_popdy::Population<Type>> &population,
452 size_t i_age_year, size_t year, size_t age) {
453 std::map<std::string, fims::Vector<Type>> &dq_ =
454 this->GetPopulationDerivedQuantities(population->GetId());
455
456 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
457 // evaluate is a member function of the selectivity class
458 Type s = population->fleets[fleet_]->selectivity->evaluate(
459 population->ages[age], year);
460
461 dq_["mortality_F"][i_age_year] +=
462 population->fleets[fleet_]->Fmort[year] *
463 population->f_multiplier[year] * s;
464
465 dq_["sum_selectivity"][i_age_year] += s;
466 }
467 dq_["mortality_M"][i_age_year] = population->M[i_age_year];
468
469 dq_["mortality_Z"][i_age_year] =
470 population->M[i_age_year] + dq_["mortality_F"][i_age_year];
471 }
472
488 std::shared_ptr<fims_popdy::Population<Type>> &population,
489 size_t i_age_year, size_t year, size_t age) {
490 std::map<std::string, fims::Vector<Type>> &dq_ =
491 this->GetPopulationDerivedQuantities(population->GetId());
492
493 dq_["biomass"][year] += dq_["numbers_at_age"][i_age_year] *
494 PopulationMeanWeightAA(population, year, age);
495 }
496
512 std::shared_ptr<fims_popdy::Population<Type>> &population,
513 size_t i_age_year, size_t year, size_t age) {
514 std::map<std::string, fims::Vector<Type>> &dq_ =
515 this->GetPopulationDerivedQuantities(population->GetId());
516
517 dq_["unfished_biomass"][year] +=
518 dq_["unfished_numbers_at_age"][i_age_year] *
519 PopulationMeanWeightAA(population, year, age);
520 }
521
539 std::shared_ptr<fims_popdy::Population<Type>> &population,
540 size_t i_age_year, size_t year, size_t age) {
541 std::map<std::string, fims::Vector<Type>> &dq_ =
542 this->GetPopulationDerivedQuantities(population->GetId());
543
544 dq_["spawning_biomass"][year] +=
545 population->proportion_female.get_force_scalar(age) *
546 dq_["numbers_at_age"][i_age_year] *
547 dq_["proportion_mature_at_age"][i_age_year] *
548 PopulationMeanWeightAA(population, year, age);
549 }
550
567 std::shared_ptr<fims_popdy::Population<Type>> &population,
568 size_t i_age_year, size_t year, size_t age) {
569 std::map<std::string, fims::Vector<Type>> &dq_ =
570 this->GetPopulationDerivedQuantities(population->GetId());
571
572 dq_["unfished_spawning_biomass"][year] +=
573 population->proportion_female.get_force_scalar(age) *
574 dq_["unfished_numbers_at_age"][i_age_year] *
575 dq_["proportion_mature_at_age"][i_age_year] *
576 PopulationMeanWeightAA(population, year, age);
577 }
578
595 std::shared_ptr<fims_popdy::Population<Type>> &population, size_t year) {
596 std::map<std::string, fims::Vector<Type>> &dq_ =
597 this->GetPopulationDerivedQuantities(population->GetId());
598 population->spawning_biomass_ratio[year] =
599 dq_["spawning_biomass"][year] / dq_["unfished_spawning_biomass"][0];
600 }
601
627 std::shared_ptr<fims_popdy::Population<Type>> &population) {
628 std::map<std::string, fims::Vector<Type>> &dq_ =
629 this->GetPopulationDerivedQuantities(population->GetId());
630
631 std::vector<Type> numbers_spr(population->n_ages, 1.0);
632 Type phi_0 = 0.0;
633 phi_0 += numbers_spr[0] *
634 population->proportion_female.get_force_scalar(0) *
635 dq_["proportion_mature_at_age"][0] *
636 PopulationMeanWeightAA(population, 0, 0);
637 for (size_t a = 1; a < (population->n_ages - 1); a++) {
638 numbers_spr[a] =
639 numbers_spr[a - 1] * fims_math::exp(-population->M[a - 1]);
640 phi_0 += numbers_spr[a] *
641 population->proportion_female.get_force_scalar(a) *
642 dq_["proportion_mature_at_age"][a] *
643 PopulationMeanWeightAA(population, 0, a);
644 }
645
646 numbers_spr[population->n_ages - 1] =
647 (numbers_spr[population->n_ages - 2] *
648 fims_math::exp(-population->M[population->n_ages - 2])) /
649 (1 - fims_math::exp(-population->M[population->n_ages - 1]));
650 phi_0 +=
651 numbers_spr[population->n_ages - 1] *
652 population->proportion_female.get_force_scalar(population->n_ages - 1) *
653 dq_["proportion_mature_at_age"][population->n_ages - 1] *
654 PopulationMeanWeightAA(population, 0, population->n_ages - 1);
655
656 return phi_0;
657 }
658
683 std::shared_ptr<fims_popdy::Population<Type>> &population,
684 size_t i_age_year, size_t year, size_t i_dev) {
685 std::map<std::string, fims::Vector<Type>> &dq_ =
686 this->GetPopulationDerivedQuantities(population->GetId());
687
688 Type phi_0 = CalculateSBPR0(population);
689
690 if (i_dev == population->n_years) {
691 dq_["numbers_at_age"][i_age_year] =
692 population->recruitment->evaluate_mean(
693 dq_["spawning_biomass"][year - 1], phi_0);
694 /*the final year of the time series has no data to inform recruitment
695 devs, so this value is set to the mean recruitment.*/
696 } else {
697 // Why are we using evaluate_mean, how come a virtual function was
698 // changed? AMH: there are now two virtual functions: evaluate_mean and
699 // evaluate_process (see below)
700 population->recruitment->log_expected_recruitment[year - 1] =
701 fims_math::log(population->recruitment->evaluate_mean(
702 dq_["spawning_biomass"][year - 1], phi_0));
703
704 dq_["numbers_at_age"][i_age_year] = fims_math::exp(
705 population->recruitment->process->evaluate_process(year - 1));
706 }
707
708 dq_["expected_recruitment"][year] = dq_["numbers_at_age"][i_age_year];
709 }
710
726 std::shared_ptr<fims_popdy::Population<Type>> &population,
727 size_t i_age_year, size_t year, size_t age) {
728 std::map<std::string, fims::Vector<Type>> &dq_ =
729 this->GetPopulationDerivedQuantities(population->GetId());
730
731 // Year n_years is the start-of-year state after the last data year, so it
732 // reuses that year's maturity.
733 dq_["proportion_mature_at_age"][i_age_year] =
734 population->maturity->evaluate(
735 population->ages[age], (std::min)(year, population->n_years - 1));
736 }
737
753 void CalculateCatch(std::shared_ptr<fims_popdy::Population<Type>> &population,
754 size_t year, size_t age) {
755 std::map<std::string, fims::Vector<Type>> &pdq_ =
756 this->GetPopulationDerivedQuantities(population->GetId());
757
758 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
759 std::map<std::string, fims::Vector<Type>> &fdq_ =
760 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
761 size_t i_age_year = year * population->n_ages + age;
762
763 pdq_["total_catch_weight"][year] +=
764 fdq_["catch_weight_at_age"][i_age_year];
765
766 fdq_["catch_weight"][year] += fdq_["catch_weight_at_age"][i_age_year];
767
768 pdq_["total_catch_numbers"][year] +=
769 fdq_["catch_numbers_at_age"][i_age_year];
770
771 fdq_["catch_numbers"][year] += fdq_["catch_numbers_at_age"][i_age_year];
772 }
773 }
774
795 std::shared_ptr<fims_popdy::Population<Type>> &population, size_t year,
796 size_t age) {
797 int i_age_year = year * population->n_ages + age;
798
799 if (population->growth == nullptr) {
800 throw std::runtime_error(
801 "Population growth pointer was null while resolving landings "
802 "weight-at-age.");
803 }
804
805 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
806 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet =
807 population->fleets[fleet_];
808 std::map<std::string, fims::Vector<Type>> &fdq_ =
809 this->GetFleetDerivedQuantities(fleet->GetId());
810
811 Type mean_weight_at_age;
812 if (fleet->weight_at_age.size() > 0) {
813 mean_weight_at_age = fleet->weight_at_age[i_age_year];
814 } else {
815 mean_weight_at_age = PopulationMeanWeightAA(population, year, age);
816 }
817
818 fdq_["catch_weight_at_age"][i_age_year] =
819 fdq_["catch_numbers_at_age"][i_age_year] * mean_weight_at_age;
820 }
821 }
822
841 std::shared_ptr<fims_popdy::Population<Type>> &population,
842 size_t i_age_year, size_t year, size_t age) {
843 std::map<std::string, fims::Vector<Type>> &pdq_ =
844 this->GetPopulationDerivedQuantities(population->GetId());
845
846 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
847 std::map<std::string, fims::Vector<Type>> &fdq_ =
848 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
849
850 // Baranov Catch Equation
851 fdq_["catch_numbers_at_age"][i_age_year] +=
852 (population->fleets[fleet_]->Fmort[year] *
853 population->f_multiplier[year] *
854 population->fleets[fleet_]->selectivity->evaluate(
855 population->ages[age], year)) /
856 pdq_["mortality_Z"][i_age_year] * pdq_["numbers_at_age"][i_age_year] *
857 (1 - fims_math::exp(-(pdq_["mortality_Z"][i_age_year])));
858 }
859 }
860
878 void CalculateIndex(std::shared_ptr<fims_popdy::Population<Type>> &population,
879 size_t i_age_year, size_t year, size_t age) {
880 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
881 std::map<std::string, fims::Vector<Type>> &fdq_ =
882 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
883
884 fdq_["index_weight"][year] += fdq_["index_weight_at_age"][i_age_year];
885
886 fdq_["index_numbers"][year] += fdq_["index_numbers_at_age"][i_age_year];
887 }
888 }
889
910 std::shared_ptr<fims_popdy::Population<Type>> &population,
911 size_t i_age_year, size_t year, size_t age) {
912 std::map<std::string, fims::Vector<Type>> &pdq_ =
913 this->GetPopulationDerivedQuantities(population->GetId());
914
915 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
916 std::map<std::string, fims::Vector<Type>> &fdq_ =
917 this->GetFleetDerivedQuantities(population->fleets[fleet_]->GetId());
918
919 fdq_["index_numbers_at_age"][i_age_year] +=
920 (population->fleets[fleet_]->q.get_force_scalar(year) *
921 population->fleets[fleet_]->selectivity->evaluate(
922 population->ages[age], year)) *
923 pdq_["numbers_at_age"][i_age_year];
924 }
925 }
926
946 std::shared_ptr<fims_popdy::Population<Type>> &population, size_t year,
947 size_t age) {
948 int i_age_year = year * population->n_ages + age;
949
950 if (population->growth == nullptr) {
951 throw std::runtime_error(
952 "Population growth pointer was null while resolving index "
953 "weight-at-age.");
954 }
955
956 for (size_t fleet_ = 0; fleet_ < population->n_fleets; fleet_++) {
957 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet =
958 population->fleets[fleet_];
959 std::map<std::string, fims::Vector<Type>> &fdq_ =
960 this->GetFleetDerivedQuantities(fleet->GetId());
961
962 Type mean_weight_at_age;
963 if (fleet->weight_at_age.size() > 0) {
964 mean_weight_at_age = fleet->weight_at_age[i_age_year];
965 } else {
966 mean_weight_at_age = PopulationMeanWeightAA(population, year, age);
967 }
968
969 fdq_["index_weight_at_age"][i_age_year] =
970 fdq_["index_numbers_at_age"][i_age_year] * mean_weight_at_age;
971 }
972 }
973
978 fleet_iterator fit;
979 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
980 std::map<std::string, fims::Vector<Type>> &fdq_ =
981 this->GetFleetDerivedQuantities((*fit).second->GetId());
982
983 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
984 for (size_t y = 0; y < fleet->n_years; y++) {
985 Type sum = static_cast<Type>(0.0);
986 Type sum_obs = static_cast<Type>(0.0);
987 // robust_add is a small value to add to expected composition
988 // proportions at age to stabilize likelihood calculations
989 // when the expected proportions are close to zero.
990 // Type robust_add = static_cast<Type>(0.0); // zeroed out before
991 // testing 0.0001; sum robust is used to calculate the total sum of
992 // robust additions to ensure that proportions sum to 1. Type robust_sum
993 // = static_cast<Type>(1.0);
994
995 for (size_t a = 0; a < fleet->n_ages; a++) {
996 size_t i_age_year = y * fleet->n_ages + a;
997 // Here we have a check to determine if the age comp
998 // should be calculated from the retained catch or
999 // the total population. These values are slightly different.
1000 // In the future this will have more impact as we implement
1001 // timing rather than everything occurring at the start of
1002 // the year.
1003 if (fleet->fleet_observed_catch_data_id_m == -999) {
1004 fdq_["agecomp_expected"][i_age_year] =
1005 fdq_["index_numbers_at_age"][i_age_year];
1006 } else {
1007 fdq_["agecomp_expected"][i_age_year] =
1008 fdq_["catch_numbers_at_age"][i_age_year];
1009 }
1010 sum += fdq_["agecomp_expected"][i_age_year];
1011 // robust_sum -= robust_add;
1012
1013 // This sums over the observed age composition data so that
1014 // the expected age composition can be rescaled to match the
1015 // total number observed. The check for na values should not
1016 // be needed as individual years should not have missing data.
1017 // This is need to be re-explored if/when we modify FIMS to
1018 // allow for composition bins that do not match the population
1019 // bins.
1020 if (fleet->fleet_observed_agecomp_data_id_m != -999) {
1021 if (fleet->observed_agecomp_data->at(i_age_year) !=
1022 fleet->observed_agecomp_data->na_value) {
1023 sum_obs += fleet->observed_agecomp_data->at(i_age_year);
1024 }
1025 }
1026 }
1027 for (size_t a = 0; a < fleet->n_ages; a++) {
1028 size_t i_age_year = y * fleet->n_ages + a;
1029 fdq_["agecomp_proportion"][i_age_year] =
1030 fdq_["agecomp_expected"][i_age_year] / sum;
1031 // robust_add + robust_sum * this->agecomp_expected[i_age_year] / sum;
1032
1033 if (fleet->fleet_observed_agecomp_data_id_m != -999) {
1034 fdq_["agecomp_expected"][i_age_year] =
1035 fdq_["agecomp_proportion"][i_age_year] * sum_obs;
1036 }
1037 }
1038 }
1039 }
1040 }
1041
1042 // --- Growth-derived WAA helpers ---
1043 //
1044 // These helpers compute and cache fleet-level and population-level
1045 // weight-at-age values that use growth-derived age-to-length conversion rows
1046 // when available.
1047
1070 &growth_observation,
1071 const std::shared_ptr<fims_popdy::Fleet<Type>> &fleet,
1072 const fims::Vector<Type> &age_to_length_conversion_row) {
1073 Type mean_weight = static_cast<Type>(0.0);
1074 for (size_t l = 0; l < fleet->n_lengths; ++l) {
1075 const double bin_midpoint =
1076 0.5 * (fleet->length_bin_edges[l] + fleet->length_bin_edges[l + 1]);
1077 mean_weight += age_to_length_conversion_row[l] *
1078 growth_observation->EvaluateWeightAtLength(bin_midpoint);
1079 }
1080 return mean_weight;
1081 }
1082
1097 &age_to_length_conversion_derived,
1098 const std::shared_ptr<fims_popdy::Fleet<Type>> &fleet, size_t year,
1099 size_t age, fims::Vector<Type> &age_to_length_conversion_row) {
1100 if (age_to_length_conversion_derived == nullptr ||
1101 !age_to_length_conversion_derived->IsActive()) {
1102 throw std::runtime_error(
1103 "Growth-derived age-to-length conversion was unavailable while "
1104 "building fleet age-to-length probabilities.");
1105 }
1106
1107 if (!age_to_length_conversion_derived->BuildAgeToLengthConversionRow(
1108 year, age, age_to_length_conversion_row)) {
1109 std::stringstream ss;
1110 ss << "Failed to build growth-derived age-to-length conversion row for "
1111 "fleet id "
1112 << fleet->GetId() << ", year " << year << ", age " << age << ".";
1113 FIMS_ERROR_LOG(ss.str());
1114 throw std::runtime_error(ss.str());
1115 }
1116 }
1117
1136 &age_to_length_conversion_derived,
1137 const std::shared_ptr<fims_popdy::Fleet<Type>> &fleet, size_t year,
1138 size_t age) {
1139 fims::Vector<Type> age_to_length_conversion_row;
1141 age_to_length_conversion_derived, fleet, year, age,
1142 age_to_length_conversion_row);
1143
1145 age_to_length_conversion_derived->growth_observation_, fleet,
1146 age_to_length_conversion_row);
1147 }
1148
1159 for (size_t p = 0; p < this->populations.size(); ++p) {
1161 this->populations[p]);
1162 }
1163 }
1164
1174 for (size_t p = 0; p < this->populations.size(); ++p) {
1175 std::shared_ptr<fims_popdy::Population<Type>> &population =
1176 this->populations[p];
1177
1178 if (population == nullptr || population->growth == nullptr) {
1179 continue;
1180 }
1181
1182 std::shared_ptr<fims_popdy::GrowthDerivedObservationBase<Type>>
1183 growth_observation = std::dynamic_pointer_cast<
1185 population->growth);
1186
1187 if (growth_observation != nullptr) {
1188 growth_observation->PrepareGrowthProducts();
1189
1190 // Prepared on every evaluation because nothing marks the size
1191 // products stale when growth parameters change.
1192 if (population->size_distribution_provider != nullptr) {
1193 population->size_distribution_provider->PrepareSizeProducts();
1194 }
1195 }
1196 }
1197 }
1198
1215 const std::shared_ptr<fims_popdy::Fleet<Type>> &fleet, size_t year,
1216 size_t age) {
1217 if (fleet == nullptr) {
1218 throw std::runtime_error(
1219 "Fleet pointer was null while resolving growth-derived fleet mean "
1220 "weight-at-age.");
1221 }
1222
1223 std::shared_ptr<fims_popdy::AgeToLengthConversionDerived<Type>>
1224 age_to_length_conversion_derived_model = std::dynamic_pointer_cast<
1226 fleet->age_to_length_conversion_model);
1227
1228 if (age_to_length_conversion_derived_model != nullptr &&
1229 age_to_length_conversion_derived_model->IsActive()) {
1231 age_to_length_conversion_derived_model, fleet, year, age);
1232 }
1233
1234 std::stringstream ss;
1235 ss << "Failed to resolve growth-derived fleet mean weight-at-age for fleet "
1236 "id "
1237 << fleet->GetId() << ", year " << year << ", age " << age << ".";
1238 FIMS_ERROR_LOG(ss.str());
1239 throw std::runtime_error(ss.str());
1240 }
1241
1257 const std::shared_ptr<fims_popdy::Population<Type>> &population,
1259 &growth_observation,
1260 size_t year, size_t age) {
1261 if (population == nullptr || growth_observation == nullptr) {
1262 throw std::runtime_error(
1263 "Population or growth-derived observation pointer was null while "
1264 "resolving biological mean weight-at-age.");
1265 }
1266
1267 const GrowthProducts<Type> *gp =
1268 growth_observation->TryGetPreparedGrowthProducts();
1269
1270 if (gp == nullptr) {
1271 growth_observation->PrepareGrowthProducts();
1272 gp = growth_observation->TryGetPreparedGrowthProducts();
1273 }
1274
1275 if (gp == nullptr) {
1276 throw std::runtime_error(
1277 "Growth products were unavailable while resolving biological mean "
1278 "weight-at-age.");
1279 }
1280
1281 if (gp->n_years == 0 || gp->n_ages == 0) {
1282 throw std::runtime_error(
1283 "Prepared growth products were empty while resolving biological mean "
1284 "weight-at-age.");
1285 }
1286
1287 if (gp->n_sexes != 1) {
1288 throw std::runtime_error(
1289 "Biological mean weight-at-age currently requires one prepared sex.");
1290 }
1291
1292 const size_t y = (std::min)(year, gp->n_years - 1);
1293 const size_t a = (std::min)(age, gp->n_ages - 1);
1294 return gp->MeanWAA(y, a, 0);
1295 }
1296
1315 const std::shared_ptr<fims_popdy::Population<Type>> &population,
1316 size_t year, size_t age) {
1317 if (population == nullptr) {
1318 throw std::runtime_error(
1319 "Population pointer was null while resolving population mean "
1320 "weight-at-age.");
1321 }
1322
1323 if (population->growth == nullptr) {
1324 throw std::runtime_error(
1325 "Population growth pointer was null while resolving population mean "
1326 "weight-at-age.");
1327 }
1328
1329 std::shared_ptr<fims_popdy::GrowthDerivedObservationBase<Type>>
1330 growth_observation = std::dynamic_pointer_cast<
1332
1333 if (growth_observation == nullptr) {
1334 return population->growth->evaluate(year, population->ages[age]);
1335 }
1336
1337 if (population->size_distribution_provider != nullptr) {
1338 return population->size_distribution_provider->MeanWeightAtAge(
1339 (std::min)(year, population->n_years - 1), age);
1340 }
1341
1343 population, growth_observation, year, age);
1344 }
1345
1350 fleet_iterator fit;
1351 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
1352 std::map<std::string, fims::Vector<Type>> &fdq_ =
1353 this->GetFleetDerivedQuantities((*fit).second->GetId());
1354
1355 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
1356
1357 if (!fleet->requires_age_length_mapping || fleet->n_lengths == 0) {
1358 continue;
1359 }
1360
1361 if (fleet->age_to_length_conversion_model == nullptr ||
1362 !fleet->age_to_length_conversion_model->IsActive()) {
1363 std::stringstream ss;
1364 ss << "Fleet id " << fleet->GetId()
1365 << "no usable age-to-length conversion path";
1366 FIMS_ERROR_LOG(ss.str());
1367 throw std::runtime_error(ss.str());
1368 }
1369
1370 for (size_t y = 0; y < fleet->n_years; y++) {
1371 Type sum = static_cast<Type>(0.0);
1372 Type sum_obs = static_cast<Type>(0.0);
1373 // robust_add is a small value to add to expected composition
1374 // proportions at age to stabilize likelihood calculations
1375 // when the expected proportions are close to zero.
1376 // Type robust_add = static_cast<Type>(0.0); // 0.0001; zeroed out
1377 // before testing sum robust is used to calculate the total sum of
1378 // robust additions to ensure that proportions sum to 1. Type
1379 // robust_sum = static_cast<Type>(1.0);
1380 for (size_t a = 0; a < fleet->n_ages; a++) {
1381 size_t i_age_year = y * fleet->n_ages + a;
1382 fims::Vector<Type> age_to_length_conversion_row;
1383 if (!fleet->age_to_length_conversion_model
1384 ->BuildAgeToLengthConversionRow(
1385 y, a, age_to_length_conversion_row)) {
1386 std::stringstream ss;
1387 ss << "Failed to build age-to-length conversion row for fleet id "
1388 << fleet->GetId() << ", year " << y << ", age " << a << ".";
1389 FIMS_ERROR_LOG(ss.str());
1390 throw std::runtime_error(ss.str());
1391 }
1392
1393 for (size_t l = 0; l < fleet->n_lengths; l++) {
1394 size_t i_length_year = y * fleet->n_lengths + l;
1395 const Type age_to_length_prob = age_to_length_conversion_row[l];
1396 // Use agecomp_proportion rather than agecomp_expected, which is
1397 // rescaled to the observed age-composition sample size and is
1398 // therefore 0 in years without age-composition data. The scale
1399 // cancels when the length composition is normalized below.
1400 fdq_["lengthcomp_expected"][i_length_year] +=
1401 fdq_["agecomp_proportion"][i_age_year] * age_to_length_prob;
1402
1403 fdq_["catch_numbers_at_length"][i_length_year] +=
1404 fdq_["catch_numbers_at_age"][i_age_year] * age_to_length_prob;
1405
1406 fdq_["index_numbers_at_length"][i_length_year] +=
1407 fdq_["index_numbers_at_age"][i_age_year] * age_to_length_prob;
1408 }
1409 }
1410
1411 for (size_t l = 0; l < fleet->n_lengths; l++) {
1412 size_t i_length_year = y * fleet->n_lengths + l;
1413 sum += fdq_["lengthcomp_expected"][i_length_year];
1414 // robust_sum -= robust_add;
1415
1416 if (fleet->fleet_observed_lengthcomp_data_id_m != -999) {
1417 if (fleet->observed_lengthcomp_data->at(i_length_year) !=
1418 fleet->observed_lengthcomp_data->na_value) {
1419 sum_obs += fleet->observed_lengthcomp_data->at(i_length_year);
1420 }
1421 }
1422 }
1423 for (size_t l = 0; l < fleet->n_lengths; l++) {
1424 size_t i_length_year = y * fleet->n_lengths + l;
1425 fdq_["lengthcomp_proportion"][i_length_year] =
1426 fdq_["lengthcomp_expected"][i_length_year] / sum;
1427 // robust_add + robust_sum *
1428 // this->lengthcomp_expected[i_length_year] / sum;
1429 if (fleet->fleet_observed_lengthcomp_data_id_m != -999) {
1430 fdq_["lengthcomp_expected"][i_length_year] =
1431 fdq_["lengthcomp_proportion"][i_length_year] * sum_obs;
1432 }
1433 }
1434 }
1435 }
1436 }
1437
1447 for (fleet_iterator fit = this->fleets.begin(); fit != this->fleets.end();
1448 ++fit) {
1449 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
1450 if (fleet->ageing_error.size() == 0) {
1451 continue;
1452 }
1453 std::map<std::string, fims::Vector<Type>> &fdq_ =
1454 this->GetFleetDerivedQuantities(fleet->GetId());
1455
1456 fims::Vector<Type> proportion_at_true_age(fleet->n_ages);
1457 fims::Vector<Type> expected_at_true_age(fleet->n_ages);
1458 for (size_t y = 0; y < fleet->n_years; y++) {
1459 for (size_t a = 0; a < fleet->n_ages; a++) {
1460 size_t i_age_year = y * fleet->n_ages + a;
1461 proportion_at_true_age[a] = fdq_["agecomp_proportion"][i_age_year];
1462 expected_at_true_age[a] = fdq_["agecomp_expected"][i_age_year];
1463 }
1464 fims::Vector<Type> proportion_at_observed_age =
1465 fims_math::apply_ageing_error(proportion_at_true_age,
1466 fleet->ageing_error, y);
1467 fims::Vector<Type> expected_at_observed_age =
1468 fims_math::apply_ageing_error(expected_at_true_age,
1469 fleet->ageing_error, y);
1470 for (size_t a = 0; a < fleet->n_ages; a++) {
1471 size_t i_age_year = y * fleet->n_ages + a;
1472 fdq_["agecomp_proportion"][i_age_year] =
1473 proportion_at_observed_age[a];
1474 fdq_["agecomp_expected"][i_age_year] = expected_at_observed_age[a];
1475 }
1476 }
1477 }
1478 }
1479
1484 fleet_iterator fit;
1485 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
1486 std::map<std::string, fims::Vector<Type>> &fdq_ =
1487 this->GetFleetDerivedQuantities((*fit).second->GetId());
1488 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
1489
1490 for (size_t i = 0; i < fdq_["index_numbers"].size(); i++) {
1491 if (fleet->observed_index_units == "number") {
1492 fdq_["index_expected"][i] = fdq_["index_numbers"][i];
1493 } else {
1494 fdq_["index_expected"][i] = fdq_["index_weight"][i];
1495 }
1496 fdq_["log_index_expected"][i] = log(fdq_["index_expected"][i]);
1497 }
1498 }
1499 }
1500
1505 fleet_iterator fit;
1506 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
1507 std::map<std::string, fims::Vector<Type>> &fdq_ =
1508 this->GetFleetDerivedQuantities((*fit).second->GetId());
1509 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
1510
1511 for (size_t i = 0; i < fdq_["catch_weight"].size(); i++) {
1512 if (fleet->observed_catch_units == "number") {
1513 fdq_["catch_expected"][i] = fdq_["catch_numbers"][i];
1514 } else {
1515 fdq_["catch_expected"][i] = fdq_["catch_weight"][i];
1516 }
1517 fdq_["log_catch_expected"][i] = log(fdq_["catch_expected"][i]);
1518 }
1519 }
1520 }
1521
1522 virtual void Evaluate() {
1523 /*
1524 Sets derived vectors to zero
1525 Performs parameters transformations
1526 Sets recruitment deviations to mean 0.
1527 */
1528 Prepare();
1531 /*
1532 start at year=0, age=0;
1533 here year 0 is the estimated initial population structure and age 0 are
1534 recruits loops start at zero with if statements inside to specify unique
1535 code for initial structure and recruitment 0 loops. Could also have started
1536 loops at 1 with initial structure and recruitment setup outside the loops.
1537
1538 year loop is extended to <= n_years because SB is calculated as the start
1539 of the year value and by extending one extra year we get estimates of the
1540 population structure at the end of the final year. An alternative approach
1541 would be to keep initial numbers at age in it's own vector and each year to
1542 include the population structure at the end of the year. This is likely a
1543 null point given that we are planning to modify to an event/stanza based
1544 structure in later milestones which will eliminate this confusion by
1545 explicitly referencing the exact date (or period of averaging) at which any
1546 calculation or output is being made.
1547 */
1548 for (size_t p = 0; p < this->populations.size(); p++) {
1549 std::shared_ptr<fims_popdy::Population<Type>> &population =
1550 this->populations[p];
1551 std::map<std::string, fims::Vector<Type>> &pdq_ =
1552 this->GetPopulationDerivedQuantities(population->GetId());
1553 // CAAPopulationProxy<Type>& population = this->populations_proxies[p];
1554
1555 for (size_t y = 0; y <= population->n_years; y++) {
1556 for (size_t a = 0; a < population->n_ages; a++) {
1557 /*
1558 index naming defines the dimensional folding structure
1559 i.e. i_age_year is referencing folding over years and ages.
1560 */
1561 size_t i_age_year = y * population->n_ages + a;
1562 /*
1563 Mortality rates are not estimated in the final year which is
1564 used to show expected population structure at the end of the model
1565 period. This is because biomass in year i represents biomass at the
1566 start of the year. Should we add complexity to track more values such
1567 as start, mid, and end biomass in all years where, start biomass=end
1568 biomass of the previous year? Referenced above, this is probably not
1569 worth exploring as later milestone changes will eliminate this
1570 confusion.
1571 */
1572 if (y < population->n_years) {
1573 /*
1574 First thing we need is total mortality aggregated across all fleets
1575 to inform the subsequent catch and change in numbers at age
1576 calculations. This is only calculated for years < n_years as these
1577 are the model estimated years with data. The year loop extends to
1578 y=n_years so that population numbers at age and SB can be
1579 calculated at the end of the last year of the model
1580 */
1581 CalculateMortality(population, i_age_year, y, a);
1582 }
1583 CalculateMaturityAA(population, i_age_year, y, a);
1584 /* if statements needed because some quantities are only needed
1585 for the first year and/or age, so these steps are included here.
1586 */
1587 if (y == 0) {
1588 // Initial numbers at age is a user input or estimated parameter
1589 // vector.
1590 CalculateInitialNumbersAA(population, i_age_year, a);
1591
1592 if (a == 0) {
1593 /*
1594 Expected recruitment in year 0 is numbers at age 0 in year 0.
1595 */
1596 pdq_["expected_recruitment"][y] =
1597 pdq_["numbers_at_age"][i_age_year];
1598 pdq_["unfished_numbers_at_age"][i_age_year] =
1599 fims_math::exp(population->recruitment->log_rzero[0]);
1600 } else {
1601 CalculateUnfishedInitialNumbersAA(population, i_age_year, a);
1602 }
1603
1604 } else {
1605 if (a == 0) {
1606 // Set the nrecruits for age a=0 year y (use pointers instead of
1607 // functional returns)
1608 CalculateRecruitment(population, i_age_year, y, y);
1609 pdq_["unfished_numbers_at_age"][i_age_year] =
1610 fims_math::exp(population->recruitment->log_rzero[0]);
1611 } else {
1612 size_t i_agem1_yearm1 = (y - 1) * population->n_ages + (a - 1);
1613 CalculateNumbersAA(population, i_age_year, i_agem1_yearm1, a);
1614 CalculateUnfishedNumbersAA(population, i_age_year, i_agem1_yearm1,
1615 a);
1616 }
1617 }
1618
1619 /*
1620 Fished and unfished biomass vectors are summing biomass at
1621 age across ages.
1622 */
1623
1624 CalculateBiomass(population, i_age_year, y, a);
1625
1626 CalculateUnfishedBiomass(population, i_age_year, y, a);
1627
1628 /*
1629 Fished and unfished spawning biomass vectors are summing biomass at
1630 age across ages to allow calculation of recruitment in the next
1631 year.
1632 */
1633
1634 CalculateSpawningBiomass(population, i_age_year, y, a);
1635
1636 CalculateUnfishedSpawningBiomass(population, i_age_year, y, a);
1637
1638 /*
1639 Here composition, total catch, and index values are calculated for all
1640 years with reference data. They are not calculated for y=n_years as
1641 there is this is just to get final population structure at the end of
1642 the terminal year.
1643 */
1644 if (y < population->n_years) {
1645 CalculateCatchNumbersAA(population, i_age_year, y, a);
1646 CalculateCatchWeightAA(population, y, a);
1647 CalculateCatch(population, y, a);
1648
1649 CalculateIndexNumbersAA(population, i_age_year, y, a);
1650 CalculateIndexWeightAA(population, y, a);
1651 CalculateIndex(population, i_age_year, y, a);
1652 }
1653 }
1654 /* Calculate spawning biomass depletion ratio */
1655 CalculateSpawningBiomassRatio(population, y);
1656 }
1657 }
1663 }
1664
1668 virtual void Report() {
1669 int n_fleets = this->fleets.size();
1670 int n_pops = this->populations.size();
1671#ifdef TMB_MODEL
1672 if (this->do_reporting == true) {
1674 report_vectors.clear();
1675 // std::shared_ptr<UncertaintyReportInfoMap>
1676 // population_uncertainty_report_info_map =
1677 // this->GetPopulationUncertaintyReportInfoMap();
1678
1679 // std::shared_ptr<UncertaintyReportInfoMap>
1680 // fleet_uncertainty_report_info_map =
1681 // this->GetFleetUncertaintyReportInfoMap();
1682
1683 // initialize population vectors
1684 vector<vector<Type>> biomass_p(n_pops);
1685 vector<vector<Type>> expected_recruitment_p(n_pops);
1686 vector<vector<Type>> mortality_F_p(n_pops);
1687 vector<vector<Type>> mortality_M_p(n_pops);
1688 vector<vector<Type>> mortality_Z_p(n_pops);
1689 vector<vector<Type>> numbers_at_age_p(n_pops);
1690 vector<vector<Type>> proportion_mature_at_age_p(n_pops);
1691 vector<vector<Type>> spawning_biomass_p(n_pops);
1692 vector<vector<Type>> sum_selectivity_p(n_pops);
1693 vector<vector<Type>> total_catch_numbers_p(n_pops);
1694 vector<vector<Type>> total_catch_weight_p(n_pops);
1695 vector<vector<Type>> unfished_biomass_p(n_pops);
1696 vector<vector<Type>> unfished_numbers_at_age_p(n_pops);
1697 vector<vector<Type>> unfished_spawning_biomass_p(n_pops);
1698 vector<vector<Type>> spawning_biomass_ratio_p(n_pops);
1699 vector<vector<Type>> growth_mean_LAA_p(n_pops);
1700 vector<vector<Type>> growth_sd_LAA_p(n_pops);
1701 vector<vector<Type>> growth_mean_WAA_p(n_pops);
1702
1703 // initialize fleet vectors
1704 vector<vector<Type>> agecomp_expected_f(n_fleets);
1705 vector<vector<Type>> agecomp_proportion_f(n_fleets);
1706 vector<vector<Type>> age_to_length_conversion_f(n_fleets);
1707 vector<vector<Type>> age_to_length_conversion_derived_f(n_fleets);
1708 vector<vector<Type>> age_to_length_conversion_derived_used_f(n_fleets);
1709 vector<vector<Type>> growth_derived_mean_WAA_f(n_fleets);
1710 vector<vector<Type>> index_expected_f(n_fleets);
1711 vector<vector<Type>> index_numbers_f(n_fleets);
1712 vector<vector<Type>> index_numbers_at_age_f(n_fleets);
1713 vector<vector<Type>> index_numbers_at_length_f(n_fleets);
1714 vector<vector<Type>> index_weight_f(n_fleets);
1715 vector<vector<Type>> index_weight_at_age_f(n_fleets);
1716 vector<vector<Type>> catch_expected_f(n_fleets);
1717 vector<vector<Type>> catch_numbers_f(n_fleets);
1718 vector<vector<Type>> catch_numbers_at_age_f(n_fleets);
1719 vector<vector<Type>> catch_numbers_at_length_f(n_fleets);
1720 vector<vector<Type>> catch_weight_f(n_fleets);
1721 vector<vector<Type>> catch_weight_at_age_f(n_fleets);
1722 vector<vector<Type>> lengthcomp_expected_f(n_fleets);
1723 vector<vector<Type>> lengthcomp_proportion_f(n_fleets);
1724 vector<vector<Type>> log_index_expected_f(n_fleets);
1725 vector<vector<Type>> log_catch_expected_f(n_fleets);
1726
1727 // initiate population index for structuring report out objects
1728 int pop_idx = 0;
1729 for (size_t p = 0; p < this->populations.size(); p++) {
1730 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1732 biomass_p(pop_idx) = derived_quantities["biomass"].to_tmb();
1733 expected_recruitment_p(pop_idx) =
1734 derived_quantities["expected_recruitment"].to_tmb();
1735 mortality_F_p(pop_idx) = derived_quantities["mortality_F"].to_tmb();
1736 mortality_M_p(pop_idx) = derived_quantities["mortality_M"].to_tmb();
1737 mortality_Z_p(pop_idx) = derived_quantities["mortality_Z"].to_tmb();
1738 numbers_at_age_p(pop_idx) =
1739 derived_quantities["numbers_at_age"].to_tmb();
1740 proportion_mature_at_age_p(pop_idx) =
1741 derived_quantities["proportion_mature_at_age"].to_tmb();
1742 spawning_biomass_p(pop_idx) =
1743 derived_quantities["spawning_biomass"].to_tmb();
1744 sum_selectivity_p(pop_idx) =
1745 derived_quantities["sum_selectivity"].to_tmb();
1746 total_catch_numbers_p(pop_idx) =
1747 derived_quantities["total_catch_numbers"].to_tmb();
1748 total_catch_weight_p(pop_idx) =
1749 derived_quantities["total_catch_weight"].to_tmb();
1750 unfished_biomass_p(pop_idx) =
1751 derived_quantities["unfished_biomass"].to_tmb();
1752 unfished_numbers_at_age_p(pop_idx) =
1753 derived_quantities["unfished_numbers_at_age"].to_tmb();
1754 unfished_spawning_biomass_p(pop_idx) =
1755 derived_quantities["unfished_spawning_biomass"].to_tmb();
1756 spawning_biomass_ratio_p(pop_idx) =
1757 this->populations[pop_idx]->spawning_biomass_ratio.to_tmb();
1758
1760 growth_observation = std::dynamic_pointer_cast<
1762 this->populations[p]->growth)) {
1763 const auto *gp = growth_observation->TryGetPreparedGrowthProducts();
1764 if (gp == nullptr) {
1765 throw std::runtime_error(
1766 "Growth products were not prepared before report generation.");
1767 }
1768
1769 const std::size_t n = gp->Size();
1770 vector<Type> mean_laa(n);
1771 vector<Type> sd_laa(n);
1772 vector<Type> mean_waa(n);
1773 for (std::size_t i = 0; i < n; ++i) {
1774 mean_laa(i) = gp->mean_LAA[i];
1775 sd_laa(i) = gp->sd_LAA[i];
1776 const std::size_t year = i / this->populations[p]->n_ages;
1777 const std::size_t age = i % this->populations[p]->n_ages;
1778 mean_waa(i) =
1779 PopulationMeanWeightAA(this->populations[p], year, age);
1780 }
1781 growth_mean_LAA_p(pop_idx) = mean_laa;
1782 growth_sd_LAA_p(pop_idx) = sd_laa;
1783 growth_mean_WAA_p(pop_idx) = mean_waa;
1784 }
1785
1786 pop_idx += 1;
1787 }
1788
1789 // initiate fleet index for structuring report out objects
1790 int fleet_idx = 0;
1791 fleet_iterator fit;
1792 for (fit = this->fleets.begin(); fit != this->fleets.end(); ++fit) {
1793 std::shared_ptr<fims_popdy::Fleet<Type>> &fleet = (*fit).second;
1794 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1795 this->GetFleetDerivedQuantities(fleet->GetId());
1796
1797 agecomp_expected_f(fleet_idx) =
1798 derived_quantities["agecomp_expected"].to_tmb();
1799 agecomp_proportion_f(fleet_idx) =
1800 derived_quantities["agecomp_proportion"].to_tmb();
1801 fims::Vector<Type> age_to_length_conversion_derived;
1802 fims::Vector<Type> age_to_length_conversion_derived_used;
1803 fims::Vector<Type> growth_derived_mean_WAA;
1804 bool using_age_to_length_conversion_derived = false;
1805 if (isDouble<Type>::value) {
1806 age_to_length_conversion_derived_used.resize(1);
1807 age_to_length_conversion_derived_used[0] = static_cast<Type>(0.0);
1808
1809 std::shared_ptr<fims_popdy::AgeToLengthConversionDerived<Type>>
1810 age_to_length_conversion_derived_model =
1811 std::dynamic_pointer_cast<
1813 fleet->age_to_length_conversion_model);
1814
1815 if (age_to_length_conversion_derived_model != nullptr &&
1816 age_to_length_conversion_derived_model->IsActive()) {
1817 using_age_to_length_conversion_derived = true;
1818 age_to_length_conversion_derived_used[0] = static_cast<Type>(1.0);
1819 growth_derived_mean_WAA.resize(fleet->n_years * fleet->n_ages);
1820
1821 for (size_t y = 0; y < fleet->n_years; ++y) {
1822 for (size_t a = 0; a < fleet->n_ages; ++a) {
1823 const size_t i_age_year = y * fleet->n_ages + a;
1824
1825 if (this->report_age_to_length_conversion_derived_tensor) {
1826 if (age_to_length_conversion_derived.size() == 0) {
1827 age_to_length_conversion_derived.resize(
1828 fleet->n_years * fleet->n_ages * fleet->n_lengths);
1829 }
1830
1831 fims::Vector<Type> age_to_length_conversion_row;
1833 age_to_length_conversion_derived_model, fleet, y, a,
1834 age_to_length_conversion_row);
1835
1836 growth_derived_mean_WAA[i_age_year] =
1838 age_to_length_conversion_derived_model
1839 ->growth_observation_,
1840 fleet, age_to_length_conversion_row);
1841
1842 for (size_t l = 0; l < fleet->n_lengths; ++l) {
1843 const size_t i_length_age_year =
1844 y * (fleet->n_ages * fleet->n_lengths) +
1845 a * fleet->n_lengths + l;
1846 age_to_length_conversion_derived[i_length_age_year] =
1847 age_to_length_conversion_row[l];
1848 }
1849 } else {
1850 growth_derived_mean_WAA[i_age_year] =
1851 GrowthDerivedFleetMeanWeightAA(fleet, y, a);
1852 }
1853 }
1854 }
1855 }
1856 }
1857
1858 age_to_length_conversion_derived_f(fleet_idx) =
1859 age_to_length_conversion_derived.to_tmb();
1860 age_to_length_conversion_derived_used_f(fleet_idx) =
1861 age_to_length_conversion_derived_used.to_tmb();
1862 growth_derived_mean_WAA_f(fleet_idx) = growth_derived_mean_WAA.to_tmb();
1863 if (!using_age_to_length_conversion_derived) {
1864 age_to_length_conversion_f(fleet_idx) =
1865 fleet->age_to_length_conversion.to_tmb();
1866 }
1867 index_expected_f(fleet_idx) =
1868 derived_quantities["index_expected"].to_tmb();
1869 index_numbers_f(fleet_idx) =
1870 derived_quantities["index_numbers"].to_tmb();
1871 index_numbers_at_age_f(fleet_idx) =
1872 derived_quantities["index_numbers_at_age"].to_tmb();
1873 index_numbers_at_length_f(fleet_idx) =
1874 derived_quantities["index_numbers_at_length"].to_tmb();
1875 index_weight_f(fleet_idx) = derived_quantities["index_weight"].to_tmb();
1876 index_weight_at_age_f(fleet_idx) =
1877 derived_quantities["index_weight_at_age"].to_tmb();
1878 catch_expected_f(fleet_idx) =
1879 derived_quantities["catch_expected"].to_tmb();
1880 catch_numbers_f(fleet_idx) =
1881 derived_quantities["catch_numbers"].to_tmb();
1882 catch_numbers_at_age_f(fleet_idx) =
1883 derived_quantities["catch_numbers_at_age"].to_tmb();
1884 catch_numbers_at_length_f(fleet_idx) =
1885 derived_quantities["catch_numbers_at_length"].to_tmb();
1886 catch_weight_f(fleet_idx) = derived_quantities["catch_weight"].to_tmb();
1887 catch_weight_at_age_f(fleet_idx) =
1888 derived_quantities["catch_weight_at_age"].to_tmb();
1889 // length_comp_expected_f(fleet_idx) =
1890 // derived_quantities["length_comp_expected"];
1891 // length_comp_proportion_f(fleet_idx) =
1892 // derived_quantities["length_comp_proportion"];
1893 lengthcomp_expected_f(fleet_idx) =
1894 derived_quantities["lengthcomp_expected"].to_tmb();
1895 lengthcomp_proportion_f(fleet_idx) =
1896 derived_quantities["lengthcomp_proportion"].to_tmb();
1897 log_index_expected_f(fleet_idx) =
1898 derived_quantities["log_index_expected"].to_tmb();
1899 log_catch_expected_f(fleet_idx) =
1900 derived_quantities["log_catch_expected"].to_tmb();
1901 fleet_idx += 1;
1902 }
1903
1904 vector<Type> biomass = ADREPORTvector(biomass_p);
1905 vector<Type> expected_recruitment =
1906 ADREPORTvector(expected_recruitment_p);
1907 vector<Type> mortality_F = ADREPORTvector(mortality_F_p);
1908 vector<Type> mortality_M = ADREPORTvector(mortality_M_p);
1909 vector<Type> mortality_Z = ADREPORTvector(mortality_Z_p);
1910 vector<Type> numbers_at_age = ADREPORTvector(numbers_at_age_p);
1911 vector<Type> proportion_mature_at_age =
1912 ADREPORTvector(proportion_mature_at_age_p);
1913 vector<Type> spawning_biomass = ADREPORTvector(spawning_biomass_p);
1914 vector<Type> sum_selectivity = ADREPORTvector(sum_selectivity_p);
1915 vector<Type> total_catch_numbers = ADREPORTvector(total_catch_numbers_p);
1916 vector<Type> total_catch_weight = ADREPORTvector(total_catch_weight_p);
1917 vector<Type> unfished_biomass = ADREPORTvector(unfished_biomass_p);
1918 vector<Type> unfished_numbers_at_age =
1919 ADREPORTvector(unfished_numbers_at_age_p);
1920 vector<Type> unfished_spawning_biomass =
1921 ADREPORTvector(unfished_spawning_biomass_p);
1922 vector<Type> spawning_biomass_ratio =
1923 ADREPORTvector(spawning_biomass_ratio_p);
1924
1925 vector<Type> agecomp_expected = ADREPORTvector(agecomp_expected_f);
1926 vector<Type> agecomp_proportion = ADREPORTvector(agecomp_proportion_f);
1927 vector<Type> index_expected = ADREPORTvector(index_expected_f);
1928 vector<Type> index_numbers = ADREPORTvector(index_numbers_f);
1929 vector<Type> index_numbers_at_age =
1930 ADREPORTvector(index_numbers_at_age_f);
1931 vector<Type> index_numbers_at_length =
1932 ADREPORTvector(index_numbers_at_length_f);
1933 vector<Type> index_weight = ADREPORTvector(index_weight_f);
1934 vector<Type> index_weight_at_age = ADREPORTvector(index_weight_at_age_f);
1935 vector<Type> catch_expected = ADREPORTvector(catch_expected_f);
1936 vector<Type> catch_numbers = ADREPORTvector(catch_numbers_f);
1937 vector<Type> catch_numbers_at_age =
1938 ADREPORTvector(catch_numbers_at_age_f);
1939 vector<Type> catch_numbers_at_length =
1940 ADREPORTvector(catch_numbers_at_length_f);
1941 vector<Type> catch_weight = ADREPORTvector(catch_weight_f);
1942 vector<Type> catch_weight_at_age = ADREPORTvector(catch_weight_at_age_f);
1943 // vector<Type> length_comp_expected =
1944 // ADREPORTvector(length_comp_expected_f); vector<Type>
1945 // length_comp_proportion = ADREPORTvector(length_comp_proportion_f);
1946 vector<Type> lengthcomp_expected = ADREPORTvector(lengthcomp_expected_f);
1947 vector<Type> lengthcomp_proportion =
1948 ADREPORTvector(lengthcomp_proportion_f);
1949 vector<Type> log_index_expected = ADREPORTvector(log_index_expected_f);
1950 vector<Type> log_catch_expected = ADREPORTvector(log_catch_expected_f);
1951 // populations
1952 // report
1953 FIMS_REPORT_F_("biomass", biomass_p, this->of);
1954 FIMS_REPORT_F_("expected_recruitment", expected_recruitment_p, this->of);
1955 FIMS_REPORT_F_("mortality_F", mortality_F_p, this->of);
1956 FIMS_REPORT_F_("mortality_M", mortality_M_p, this->of);
1957 FIMS_REPORT_F_("mortality_Z", mortality_Z_p, this->of);
1958 FIMS_REPORT_F_("numbers_at_age", numbers_at_age_p, this->of);
1959 FIMS_REPORT_F_("proportion_mature_at_age", proportion_mature_at_age_p,
1960 this->of);
1961 FIMS_REPORT_F_("spawning_biomass", spawning_biomass_p, this->of);
1962 FIMS_REPORT_F_("sum_selectivity", sum_selectivity_p, this->of);
1963 FIMS_REPORT_F_("total_catch_numbers", total_catch_numbers_p, this->of);
1964 FIMS_REPORT_F_("total_catch_weight", total_catch_weight_p, this->of);
1965 FIMS_REPORT_F_("unfished_biomass", unfished_biomass_p, this->of);
1966 FIMS_REPORT_F_("unfished_numbers_at_age", unfished_numbers_at_age_p,
1967 this->of);
1968 FIMS_REPORT_F_("unfished_spawning_biomass", unfished_spawning_biomass_p,
1969 this->of);
1970 FIMS_REPORT_F_("spawning_biomass_ratio", spawning_biomass_ratio_p,
1971 this->of);
1972 FIMS_REPORT_F_("growth_mean_LAA", growth_mean_LAA_p, this->of);
1973 FIMS_REPORT_F_("growth_sd_LAA", growth_sd_LAA_p, this->of);
1974 FIMS_REPORT_F_("growth_mean_WAA", growth_mean_WAA_p, this->of);
1975
1976 // adreport
1977 ADREPORT_F(biomass, this->of);
1978 ADREPORT_F(expected_recruitment, this->of);
1979 ADREPORT_F(mortality_F, this->of);
1980 ADREPORT_F(mortality_M, this->of);
1981 ADREPORT_F(mortality_Z, this->of);
1982 ADREPORT_F(numbers_at_age, this->of);
1983 ADREPORT_F(proportion_mature_at_age, this->of);
1984 ADREPORT_F(spawning_biomass, this->of);
1985 ADREPORT_F(sum_selectivity, this->of);
1986 ADREPORT_F(total_catch_numbers, this->of);
1987 ADREPORT_F(total_catch_weight, this->of);
1988 ADREPORT_F(unfished_biomass, this->of);
1989 ADREPORT_F(unfished_numbers_at_age, this->of);
1990 ADREPORT_F(unfished_spawning_biomass, this->of);
1991 ADREPORT_F(spawning_biomass_ratio, this->of);
1992
1993 // fleets
1994 // report
1995 FIMS_REPORT_F_("agecomp_expected", agecomp_expected_f, this->of);
1996 FIMS_REPORT_F_("agecomp_proportion", agecomp_proportion_f, this->of);
1997 FIMS_REPORT_F_("age_to_length_conversion", age_to_length_conversion_f,
1998 this->of);
1999 FIMS_REPORT_F_("age_to_length_conversion_derived",
2000 age_to_length_conversion_derived_f, this->of);
2001 FIMS_REPORT_F_("age_to_length_conversion_derived_used",
2002 age_to_length_conversion_derived_used_f, this->of);
2003 FIMS_REPORT_F_("growth_derived_mean_WAA", growth_derived_mean_WAA_f,
2004 this->of);
2005 FIMS_REPORT_F_("index_expected", index_expected_f, this->of);
2006 FIMS_REPORT_F_("index_numbers", index_numbers_f, this->of);
2007 FIMS_REPORT_F_("index_numbers_at_age", index_numbers_at_age_f, this->of);
2008 FIMS_REPORT_F_("index_numbers_at_length", index_numbers_at_length_f,
2009 this->of);
2010 FIMS_REPORT_F_("index_weight", index_weight_f, this->of);
2011 FIMS_REPORT_F_("index_weight_at_age", index_weight_at_age_f, this->of);
2012 FIMS_REPORT_F_("catch_expected", catch_expected_f, this->of);
2013 FIMS_REPORT_F_("catch_numbers", catch_numbers_f, this->of);
2014 FIMS_REPORT_F_("catch_numbers_at_age", catch_numbers_at_age_f, this->of);
2015 FIMS_REPORT_F_("catch_numbers_at_length", catch_numbers_at_length_f,
2016 this->of);
2017 FIMS_REPORT_F_("catch_weight", catch_weight_f, this->of);
2018 FIMS_REPORT_F_("catch_weight_at_age", catch_weight_at_age_f, this->of);
2019 FIMS_REPORT_F_("lengthcomp_expected", lengthcomp_expected_f, this->of);
2020 FIMS_REPORT_F_("lengthcomp_proportion", lengthcomp_proportion_f,
2021 this->of);
2022 FIMS_REPORT_F_("log_index_expected", log_index_expected_f, this->of);
2023 FIMS_REPORT_F_("log_catch_expected", log_catch_expected_f, this->of);
2024 // adreport
2025 ADREPORT_F(agecomp_expected, this->of);
2026 ADREPORT_F(agecomp_proportion, this->of);
2027 ADREPORT_F(index_expected, this->of);
2028 ADREPORT_F(index_numbers, this->of);
2029 ADREPORT_F(index_numbers_at_age, this->of);
2030 ADREPORT_F(index_numbers_at_length, this->of);
2031 ADREPORT_F(index_weight, this->of);
2032 ADREPORT_F(index_weight_at_age, this->of);
2033 ADREPORT_F(catch_expected, this->of);
2034 ADREPORT_F(catch_numbers, this->of);
2035 ADREPORT_F(catch_numbers_at_age, this->of);
2036 ADREPORT_F(catch_numbers_at_length, this->of);
2037 ADREPORT_F(catch_weight, this->of);
2038 ADREPORT_F(catch_weight_at_age, this->of);
2039 ADREPORT_F(lengthcomp_expected, this->of);
2040 ADREPORT_F(lengthcomp_proportion, this->of);
2041 ADREPORT_F(log_index_expected, this->of);
2042 ADREPORT_F(log_catch_expected, this->of);
2043 std::stringstream var_name;
2044 typename std::map<std::string, fims::Vector<fims::Vector<Type>>>::iterator
2045 rvit;
2046 for (rvit = report_vectors.begin(); rvit != report_vectors.end();
2047 ++rvit) {
2048 auto &x = rvit->second;
2049
2050 int outer_dim = x.size();
2051 int dim = 0;
2052 for (int i = 0; i < outer_dim; i++) {
2053 dim += x[i].size();
2054 }
2055 vector<Type> res(dim);
2056 int idx = 0;
2057 for (int i = 0; i < outer_dim; i++) {
2058 int inner_dim = x[i].size();
2059 for (int j = 0; j < inner_dim; j++) {
2060 res(idx) = x[i][j];
2061 idx += 1;
2062 }
2063 }
2064 this->of->reportvector.push(res, rvit->first.c_str());
2065 }
2066 }
2067#endif
2068 }
2069};
2070
2071} // namespace fims_popdy
2072
2073#endif
Definition fims_vector.hpp:27
void resize(size_t s)
Changes the number of elements stored.
Definition fims_vector.hpp:401
std::vector< Type > to_tmb() const
Convert fims::Vector to TMB vector type.
Definition fims_vector.hpp:494
size_type size() const
Returns the number of elements.
Definition fims_vector.hpp:299
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:594
void PreparePopulationGrowthProducts()
Prepares cached population growth products for all populations whose growth objects expose the growth...
Definition catch_at_age.hpp:1173
std::map< std::string, fims::Vector< Type > >::iterator dq_iterator
Iterate through derived quantities.
Definition catch_at_age.hpp:116
void evaluate_catch()
Definition catch_at_age.hpp:1504
std::map< std::string, fims::Vector< Type > >::iterator derived_quantities_iterator
Iterate the derived quantities.
Definition catch_at_age.hpp:70
Type CalculateSBPR0(std::shared_ptr< fims_popdy::Population< Type > > &population)
Calculates equilibrium spawning biomass per recruit.
Definition catch_at_age.hpp:626
virtual void Initialize()
Definition catch_at_age.hpp:169
std::vector< Type > ages
Definition catch_at_age.hpp:133
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:356
void EnsureAllFleetAgeToLengthConversion()
Ensure all fleets linked to this model have a usable age-to-length conversion.
Definition catch_at_age.hpp:1158
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:487
void CalculateCatchWeightAA(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t year, size_t age)
Calculates weight at age of the catch for a given fleet from a population.
Definition catch_at_age.hpp:794
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:122
Type MeanWeightFromAgeToLengthConversionRow(const std::shared_ptr< fims_popdy::GrowthDerivedObservationBase< Type > > &growth_observation, const std::shared_ptr< fims_popdy::Fleet< Type > > &fleet, const fims::Vector< Type > &age_to_length_conversion_row)
Compute expected fleet-specific weight-at-age from a normalized age-to-length conversion row and flee...
Definition catch_at_age.hpp:1068
Type BiologicalMeanWeightFromPreparedGrowthProducts(const std::shared_ptr< fims_popdy::Population< Type > > &population, const std::shared_ptr< fims_popdy::GrowthDerivedObservationBase< Type > > &growth_observation, size_t year, size_t age)
Read biological mean weight-at-age directly from prepared growth products.
Definition catch_at_age.hpp:1256
std::set< uint32_t > & GetPopulationIds()
Get the population ids of the model.
Definition catch_at_age.hpp:258
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:102
std::map< uint32_t, std::shared_ptr< fims_popdy::Fleet< Type > > >::iterator fleet_iterator
Iterate through fleets.
Definition catch_at_age.hpp:110
void CalculateCatch(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t year, size_t age)
Calculates total catch (catch) by fleet and population for a given year by aggregating age-specific c...
Definition catch_at_age.hpp:753
Type GrowthDerivedFleetMeanWeightAA(const std::shared_ptr< fims_popdy::Fleet< Type > > &fleet, size_t year, size_t age)
Compute fleet-specific expected weight-at-age from the growth-derived age-to-length conversion path.
Definition catch_at_age.hpp:1214
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:945
void evaluate_age_comp()
Definition catch_at_age.hpp:977
void evaluate_length_comp()
Definition catch_at_age.hpp:1349
virtual ~CatchAtAge()
Destroy the Catch At Age object.
Definition catch_at_age.hpp:163
CatchAtAge(const CatchAtAge &other)
Copy constructor for the CatchAtAge class.
Definition catch_at_age.hpp:150
bool report_age_to_length_conversion_derived_tensor
Controls whether reporting materializes the full derived age-to-length tensor for fleets using the gr...
Definition catch_at_age.hpp:130
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:538
CatchAtAge()
Definition catch_at_age.hpp:138
void evaluate_ageing_error()
Apply each fleet's ageing error to its expected age composition.
Definition catch_at_age.hpp:1446
Type MeanWeightFromAgeToLengthConversionDerived(const std::shared_ptr< fims_popdy::AgeToLengthConversionDerived< Type > > &age_to_length_conversion_derived, const std::shared_ptr< fims_popdy::Fleet< Type > > &fleet, size_t year, size_t age)
Compute fleet-specific mean weight-at-age from one growth-derived age-to-length conversion row.
Definition catch_at_age.hpp:1134
virtual void Evaluate()
Evaluate the model.
Definition catch_at_age.hpp:1522
std::string name_m
The name of the model.
Definition catch_at_age.hpp:63
void CalculateMaturityAA(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t year, size_t age)
Calculates maturity at age, in proportion, for a population.
Definition catch_at_age.hpp:725
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:566
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:878
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:511
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:78
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:93
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:313
void CalculateUnfishedInitialNumbersAA(std::shared_ptr< fims_popdy::Population< Type > > &population, size_t i_age_year, size_t age)
Calculates unfished numbers at age in the first year.
Definition catch_at_age.hpp:400
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:682
void CalculateCatchNumbersAA(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 catch for a given fleet from a population, year and age.
Definition catch_at_age.hpp:840
virtual void Report()
Definition catch_at_age.hpp:1668
void AddPopulation(uint32_t id)
Definition catch_at_age.hpp:253
std::vector< std::shared_ptr< fims_popdy::Population< Type > > > & GetPopulations()
Definition catch_at_age.hpp:265
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:86
Type PopulationMeanWeightAA(const std::shared_ptr< fims_popdy::Population< Type > > &population, size_t year, size_t age)
Calculates population-level mean weight-at-age.
Definition catch_at_age.hpp:1314
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:450
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:909
void BuildAgeToLengthConversionDerivedRowOrThrow(const std::shared_ptr< fims_popdy::AgeToLengthConversionDerived< Type > > &age_to_length_conversion_derived, const std::shared_ptr< fims_popdy::Fleet< Type > > &fleet, size_t year, size_t age, fims::Vector< Type > &age_to_length_conversion_row)
Build one fleet-specific growth-derived age-to-length conversion row or fail clearly.
Definition catch_at_age.hpp:1095
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:281
virtual void Prepare()
Definition catch_at_age.hpp:202
void evaluate_index()
Definition catch_at_age.hpp:1483
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
FisheryModelBase()
Construct a new Fishery Model Base object.
Definition fishery_model_base.hpp:161
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
Generic capability interface for growth models that can feed the growth-derived age-to-length convers...
Definition growth_model_adapter.hpp:26
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:664
fims::Vector< Type > apply_ageing_error(const fims::Vector< Type > &proportion_at_true_age, const fims::Vector< Type > &ageing_error, size_t year=0)
Converts values at true age, proportions or expected numbers, to values at observed age using an agei...
Definition fims_math.hpp:406
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:83
The population dynamics of FIMS.
Definition catch_at_age.hpp:45
void EnsurePopulationFleetAgeToLengthConversion(const std::shared_ptr< Population< Type > > &population)
Ensure all fleets linked to a population have a usable age-to-length conversion.
Definition runtime.hpp:184
Shared runtime helpers for constructing and validating fleet age-to-length conversions.
uint32_t GetId() const
Getter that returns the unique id for parameters in the model.
Definition model_object.hpp:70
Growth-derived age-to-length conversion implementation that reads prepared population age-to-size pro...
Definition derived.hpp:35
Base class for all fleets.
Definition fleet.hpp:26
Growth "products" in a consistent (year, age, sex) space.
Definition growth_products.hpp:26
std::size_t n_years
Definition growth_products.hpp:27
Type & MeanWAA(std::size_t y, std::size_t a, std::size_t s)
Access mean weight-at-age by reference.
Definition growth_products.hpp:112
std::size_t n_sexes
Definition growth_products.hpp:29
std::size_t n_ages
Definition growth_products.hpp:28
Population class. Contains subpopulations that are divided into generic partitions (e....
Definition population.hpp:27