9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MODELS_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MODELS_HPP
15#include "../../../models/fisheries_models.hpp"
17#include "../../../utilities/fims_json.hpp"
61 static std::map<uint32_t, std::shared_ptr<FisheryModelInterfaceBase>>
69 this->population_ids = std::make_shared<std::set<uint32_t>>();
102 return "std::string to_json() not yet implemented.";
118 std::shared_ptr<fims_info::Information<double>> info0 =
121 Rcpp::NumericVector p;
123 for (
size_t i = 0; i < info0->fixed_effects_parameters.size(); i++) {
124 p.push_back(*info0->fixed_effects_parameters[i]);
138 std::shared_ptr<fims_info::Information<double>> d0 =
141 Rcpp::NumericVector p;
143 for (
size_t i = 0; i < d0->random_effects_parameters.size(); i++) {
144 p.push_back(*d0->random_effects_parameters[i]);
156 double sum(
const std::valarray<double> &v) {
158 for (
size_t i = 0; i < v.size(); i++) {
170 double sum(
const std::vector<double> &v) {
172 for (
size_t i = 0; i < v.size(); i++) {
185 double min(
const std::valarray<double> &v) {
186 double min_value = v[0];
187 for (
size_t i = 1; i < v.size(); i++) {
188 if (v[i] < min_value) {
203 std::valarray<double>
fabs(
const std::valarray<double> &v) {
204 std::valarray<double> result(v.size());
205 for (
size_t i = 0; i < v.size(); i++) {
206 result[i] = std::fabs(v[i]);
222 std::shared_ptr<CatchAtAgeInterface> caa =
223 std::make_shared<CatchAtAgeInterface>(*
this);
242 std::map<uint32_t, std::shared_ptr<PopulationInterfaceBase>>::iterator pit;
245 std::shared_ptr<PopulationInterfaceBase> &pop = (*pit).second;
246 pop->initialize_catch_at_age.set(
true);
262 std::shared_ptr<fims_info::Information<double>> info =
267 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
268 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
270 model_ptr->do_reporting = report;
284 std::shared_ptr<fims_info::Information<double>> info =
289 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
290 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
292 return model_ptr->do_reporting;
307 std::shared_ptr<fims_info::Information<double>> info =
312 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
313 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
315 model_ptr->report_age_to_length_conversion_derived_tensor = report;
334 std::stringstream ss;
336 typename std::map<uint32_t,
337 std::shared_ptr<PopulationInterfaceBase>>::iterator
340 population_interface->
get_id());
343 fims::to_string(population_interface->
get_id()) +
344 " not found in live objects.");
348 std::shared_ptr<PopulationInterface> population_interface_ptr =
349 std::dynamic_pointer_cast<PopulationInterface>((*pi_it).second);
351 std::shared_ptr<fims_info::Information<double>> info =
356 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
357 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
362 pit = info->populations.find(population_interface->
get_id());
364 if (pit != info->populations.end()) {
365 std::shared_ptr<fims_popdy::Population<double>> &pop = (*pit).second;
368 ss <<
" \"module_name\": \"Population\",\n";
369 ss <<
" \"population\": \""
372 ss <<
" \"module_id\": " << population_interface->
id <<
",\n";
373 ss <<
" \"recruitment_id\": " << population_interface->
recruitment_id
375 ss <<
" \"growth_id\": " << population_interface->
growth_id <<
",\n";
376 ss <<
" \"maturity_id\": " << population_interface->
maturity_id <<
",\n";
381 std::stringstream age;
382 age << std::fixed << std::setprecision(10);
383 for (
int i = 0; i < population_interface->
n_ages.
get(); i++) {
384 if (i > 0) age <<
", ";
387 ss <<
" \"age\": [" << age.str() <<
"],\n";
389 ss <<
" \"parameters\": [\n";
390 for (
size_t i = 0; i < pop->log_M.size(); i++) {
391 population_interface_ptr->log_M[i].final_value_m = pop->log_M[i];
394 ss <<
"{\n \"name\": \"log_M\",\n";
395 ss <<
" \"id\":" << population_interface->
log_M.
id_m <<
",\n";
396 ss <<
" \"type\": \"vector\",\n";
397 ss <<
" \"dimensionality\": {\n";
398 ss <<
" \"header\": [" <<
"\"n_years\", \"n_ages\"" <<
"],\n";
399 ss <<
" \"dimensions\": [" << population_interface->
n_years.
get() <<
", "
400 << population_interface->
n_ages.
get() <<
"]\n},\n";
401 ss <<
" \"values\": " << population_interface->
log_M <<
"\n\n";
404 for (
size_t i = 0; i < pop->log_f_multiplier.size(); i++) {
405 population_interface_ptr->log_f_multiplier[i].final_value_m =
406 pop->log_f_multiplier[i];
409 ss <<
"{\n \"name\": \"log_f_multiplier\",\n";
411 ss <<
" \"type\": \"vector\",\n";
412 ss <<
" \"dimensionality\": {\n";
413 ss <<
" \"header\": [" <<
"\"n_years\"" <<
"],\n";
414 ss <<
" \"dimensions\": [" << population_interface->
n_years.
get()
419 for (
size_t i = 0; i < pop->spawning_biomass_ratio.size(); i++) {
420 population_interface_ptr->spawning_biomass_ratio[i].final_value_m =
421 pop->spawning_biomass_ratio[i];
424 ss <<
"{\n \"name\": \"spawning_biomass_ratio\",\n";
427 ss <<
" \"type\": \"vector\",\n";
428 ss <<
" \"dimensionality\": {\n";
429 ss <<
" \"header\": [" <<
"\"n_years\"" <<
"],\n";
430 ss <<
" \"dimensions\": [" << (population_interface->
n_years.
get() + 1)
436 for (
size_t i = 0; i < pop->log_init_naa.size(); i++) {
437 population_interface_ptr->log_init_naa[i].final_value_m =
438 pop->log_init_naa[i];
440 ss <<
" {\n\"name\": \"log_init_naa\",\n";
442 ss <<
" \"type\": \"vector\",\n";
443 ss <<
" \"dimensionality\": {\n";
444 ss <<
" \"header\": [" <<
"\"n_ages\"" <<
"],\n";
445 ss <<
" \"dimensions\": [" << population_interface->
n_ages.
get()
448 ss <<
" \"values\":" << population_interface->
log_init_naa <<
"\n";
453 population_interface_ptr->proportion_female[i].final_value_m =
454 pop->proportion_female.get_force_scalar(i);
456 ss <<
" {\n\"name\": \"proportion_female\",\n";
459 ss <<
" \"type\": \"vector\",\n";
460 ss <<
" \"dimensionality\": {\n";
461 ss <<
" \"header\": [" <<
"\"n_ages\"" <<
"],\n";
462 ss <<
" \"dimensions\": ["
468 ss <<
" \"derived_quantities\": [\n";
470 std::map<std::string, fims::Vector<double>> dqs =
471 model_ptr->GetPopulationDerivedQuantities(
472 population_interface->
get_id());
474 std::map<std::string, fims_popdy::DimensionInfo> dim_info =
475 model_ptr->GetPopulationDimensionInfo(population_interface->
get_id());
480 ss <<
" \"name\": \"Population\",\n";
482 ss <<
" \"type\": \"population\",\n";
483 ss <<
" \"tag\": \"" << population_interface->
get_id()
484 <<
" not found in Information.\",\n";
485 ss <<
" \"id\": " << population_interface->
get_id() <<
",\n";
486 ss <<
" \"recruitment_id\": " << population_interface->
recruitment_id
488 ss <<
" \"growth_id\": " << population_interface->
growth_id <<
",\n";
489 ss <<
" \"maturity_id\": " << population_interface->
maturity_id <<
",\n";
490 ss <<
" \"derived_quantities\": []}\n";
504 std::stringstream ss;
506 std::stringstream dim_entry;
508 switch (dim_info.
ndims) {
510 dim_entry <<
"\"dimensionality\": {\n";
511 dim_entry <<
" \"header\": [\"" << dim_info.
dim_names[0] <<
"\"],\n";
512 dim_entry <<
" \"dimensions\": [";
513 for (
size_t i = 0; i < dim_info.
dims.
size(); ++i) {
514 if (i > 0) dim_entry <<
", ";
515 dim_entry << dim_info.
dims[i];
521 dim_entry <<
"\"dimensionality\": {\n";
522 dim_entry <<
" \"header\": [\"" << dim_info.
dim_names[0] <<
"\", \""
524 dim_entry <<
" \"dimensions\": [";
525 for (
size_t i = 0; i < dim_info.
dims.
size(); ++i) {
526 if (i > 0) dim_entry <<
", ";
527 dim_entry << dim_info.
dims[i];
533 dim_entry <<
"\"dimensionality\": {\n";
534 dim_entry <<
" \"header\": [\"" << dim_info.
dim_names[0] <<
"\", \""
537 dim_entry <<
" \"dimensions\": [";
538 for (
size_t i = 0; i < dim_info.
dims.
size(); ++i) {
539 if (i > 0) dim_entry <<
", ";
540 dim_entry << dim_info.
dims[i];
546 dim_entry <<
"\"dimensionality\": {\n";
547 dim_entry <<
" \"header\": [],\n";
548 dim_entry <<
" \"dimensions\": []\n";
555 ss <<
"\"name\":\"" << (*it).first <<
"\",\n";
556 ss << dim_entry.str() <<
",\n";
558 ss << std::fixed << std::setprecision(10);
562 for (
size_t i = 0; i < dq.
size() - 1; i++) {
580 std::map<std::string, fims_popdy::DimensionInfo> &dim_info) {
581 std::stringstream ss;
587 std::map<std::string, fims_popdy::DimensionInfo>::iterator dim_info_it;
588 std::map<std::string, fims::Vector<double>>::iterator it;
589 typename std::map<std::string, fims::Vector<double>>::iterator
590 second_to_last = dqs.end();
594 for (; it != second_to_last; ++it) {
595 dim_info_it = dim_info.find(it->first);
599 dim_info_it = dim_info.find(second_to_last->first);
600 if (dim_info_it != dim_info.end()) {
614 std::stringstream ss;
616 if (!fleet_interface) {
618 "Fleet pointer is null; cannot get id. Not found in live objects.");
622 std::shared_ptr<fims_info::Information<double>> info =
627 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
628 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
633 fit = info->fleets.find(fleet_interface->
get_id());
635 if (fit != info->fleets.end()) {
636 std::shared_ptr<fims_popdy::Fleet<double>> &fleet = (*fit).second;
639 ss <<
" \"module_name\": \"Fleet\",\n";
640 ss <<
" \"fleet\": \""
643 ss <<
" \"module_id\": " << fleet_interface->
id <<
",\n";
646 ss <<
" \"selectivity_id\": "
648 ss <<
" \"n_ages\": " << fleet_interface->
n_ages.
get() <<
",\n";
649 ss <<
" \"n_years\": " << fleet_interface->
n_years.
get() <<
",\n";
650 ss <<
" \"n_lengths\": " << fleet_interface->
n_lengths.
get() <<
",\n";
654 std::stringstream length;
655 length << std::fixed << std::setprecision(10);
656 for (
int i = 0; i < fleet_interface->
n_lengths.
get(); i++) {
657 if (i > 0) length <<
", ";
660 ss <<
" \"length\": [" << length.str() <<
"],\n";
661 ss <<
"\"data_ids\" : [\n";
664 ss <<
"{\"lengthcomp\": "
671 ss <<
"\"parameters\": [\n";
673 for (
size_t i = 0; i < fleet_interface->
log_Fmort.
size(); i++) {
674 fleet_interface->
log_Fmort[i].final_value_m = fleet->log_Fmort[i];
677 ss <<
" \"name\": \"log_Fmort\",\n";
678 ss <<
" \"id\":" << fleet_interface->
log_Fmort.
id_m <<
",\n";
679 ss <<
" \"type\": \"vector\",\n";
680 ss <<
" \"dimensionality\": {\n";
681 ss <<
" \"header\": [\"" <<
"n_years" <<
"\"],\n";
682 ss <<
" \"dimensions\": [" << fleet_interface->
n_years.
get()
684 ss <<
" \"values\": " << fleet_interface->
log_Fmort <<
"},\n";
687 for (
size_t i = 0; i < fleet->log_q.size(); i++) {
688 fleet_interface->
log_q[i].final_value_m = fleet->log_q[i];
690 ss <<
" \"name\": \"log_q\",\n";
691 ss <<
" \"id\":" << fleet_interface->
log_q.
id_m <<
",\n";
692 ss <<
" \"type\": \"vector\",\n";
693 ss <<
" \"dimensionality\": {\n";
694 ss <<
" \"header\": [\"" <<
"na" <<
"\"],\n";
695 ss <<
" \"dimensions\": [" << fleet->log_q.size() <<
"]\n},\n";
697 ss <<
" \"values\": " << fleet_interface->
log_q <<
"}\n";
699 ss <<
"], \"derived_quantities\": [";
701 std::map<std::string, fims::Vector<double>> dqs =
702 model_ptr->GetFleetDerivedQuantities(fleet_interface->
get_id());
703 std::map<std::string, fims_popdy::DimensionInfo> dim_info =
704 model_ptr->GetFleetDimensionInfo(fleet_interface->
get_id());
708 ss <<
" \"name\": \"Fleet\",\n";
709 ss <<
" \"type\": \"fleet\",\n";
710 ss <<
" \"tag\": \"" << fleet_interface->
get_id()
711 <<
" not found in Information.\",\n";
712 ss <<
" \"derived_quantities\": []}\n";
721 std::set<uint32_t> recruitment_ids;
722 std::set<uint32_t> growth_ids;
723 std::set<uint32_t> maturity_ids;
724 std::set<uint32_t> selectivity_ids;
725 std::set<uint32_t> fleet_ids;
727 typename std::set<uint32_t>::iterator module_id_it;
728 typename std::set<uint32_t>::iterator pit;
729 typename std::set<uint32_t>::iterator fids;
732 std::shared_ptr<PopulationInterface> population_interface =
733 std::dynamic_pointer_cast<PopulationInterface>(
735 if (population_interface) {
736 recruitment_ids.insert(population_interface->recruitment_id.get());
737 growth_ids.insert(population_interface->growth_id.get());
738 maturity_ids.insert(population_interface->maturity_id.get());
740 for (fids = population_interface->fleet_ids->begin();
741 fids != population_interface->fleet_ids->end(); fids++) {
742 fleet_ids.insert(*fids);
747 for (fids = fleet_ids.begin(); fids != fleet_ids.end(); fids++) {
748 std::shared_ptr<FleetInterface> fleet_interface =
749 std::dynamic_pointer_cast<FleetInterface>(
751 if (fleet_interface) {
752 selectivity_ids.insert(fleet_interface->GetSelectivityID());
756 std::shared_ptr<fims_info::Information<double>> info =
759 std::shared_ptr<fims_popdy::CatchAtAge<double>> model =
760 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
761 info->models_map[this->
get_id()]);
763 std::shared_ptr<fims_model::Model<double>> model_internal =
767 model->do_reporting =
false;
770 double value = model_internal->Evaluate();
772 std::stringstream ss;
777 ss <<
" \"name\": \"CatchAtAge\",\n";
778 ss <<
" \"type\": \"model\",\n";
779 ss <<
" \"estimation_framework\": ";
781 ss <<
"\"Template_Model_Builder (TMB)\",";
785 ss <<
" \"id\": " << this->
get_id() <<
",\n";
786 ss <<
" \"objective_function_value\": " <<
sanitize_val(value) <<
",\n";
787 ss <<
"\"growth\":[\n";
788 for (module_id_it = growth_ids.begin(); module_id_it != growth_ids.end();
790 std::shared_ptr<GrowthInterfaceBase> growth_interface =
793 if (growth_interface != NULL) {
794 growth_interface->finalize();
795 ss << growth_interface->to_json();
796 if (std::next(module_id_it) != growth_ids.end()) {
804 ss <<
"\"recruitment\": [\n";
805 for (module_id_it = recruitment_ids.begin();
806 module_id_it != recruitment_ids.end(); module_id_it++) {
807 std::shared_ptr<RecruitmentInterfaceBase> recruitment_interface =
809 if (recruitment_interface) {
810 recruitment_interface->finalize();
811 ss << recruitment_interface->to_json();
812 if (std::next(module_id_it) != recruitment_ids.end()) {
819 ss <<
"\"maturity\": [\n";
820 for (module_id_it = maturity_ids.begin();
821 module_id_it != maturity_ids.end(); module_id_it++) {
822 std::shared_ptr<MaturityInterfaceBase> maturity_interface =
824 if (maturity_interface) {
825 maturity_interface->finalize();
826 ss << maturity_interface->to_json();
827 if (std::next(module_id_it) != maturity_ids.end()) {
834 ss <<
"\"selectivity\": [\n";
835 for (module_id_it = selectivity_ids.begin();
836 module_id_it != selectivity_ids.end(); module_id_it++) {
837 std::shared_ptr<SelectivityInterfaceBase> selectivity_interface =
839 if (selectivity_interface) {
840 selectivity_interface->finalize();
841 ss << selectivity_interface->to_json();
842 if (std::next(module_id_it) != selectivity_ids.end()) {
849 ss <<
" \"population_ids\": [";
858 ss <<
" \"fleet_ids\": [";
860 for (fids = fleet_ids.begin(); fids != fleet_ids.end(); fids++) {
862 if (std::next(fids) != fleet_ids.end()) {
867 ss <<
"\"populations\": [\n";
868 typename std::set<uint32_t>::iterator pop_it;
869 typename std::set<uint32_t>::iterator pop_end_it;
871 typename std::set<uint32_t>::iterator pop_second_to_last_it;
873 pop_second_to_last_it = std::prev(pop_end_it);
875 pop_second_to_last_it = pop_end_it;
878 pop_it != pop_second_to_last_it; pop_it++) {
879 std::shared_ptr<PopulationInterface> population_interface =
880 std::dynamic_pointer_cast<PopulationInterface>(
882 if (population_interface) {
883 std::set<uint32_t>::iterator fids;
884 for (fids = population_interface->fleet_ids->begin();
885 fids != population_interface->fleet_ids->end(); fids++) {
886 fleet_ids.insert(*fids);
888 population_interface->finalize();
892 " not found in live objects.");
897 std::shared_ptr<PopulationInterface> population_interface =
898 std::dynamic_pointer_cast<PopulationInterface>(
900 if (population_interface) {
901 std::set<uint32_t>::iterator fids;
902 for (fids = population_interface->fleet_ids->begin();
903 fids != population_interface->fleet_ids->end(); fids++) {
904 fleet_ids.insert(*fids);
909 " not found in live objects.");
915 ss <<
"\"fleets\": [\n";
917 typename std::set<uint32_t>::iterator fleet_it;
918 typename std::set<uint32_t>::iterator fleet_end_it;
919 fleet_end_it = fleet_ids.end();
920 typename std::set<uint32_t>::iterator fleet_second_to_last_it;
922 if (fleet_end_it != fleet_ids.begin()) {
923 fleet_second_to_last_it = std::prev(fleet_end_it);
925 for (fleet_it = fleet_ids.begin(); fleet_it != fleet_second_to_last_it;
927 std::shared_ptr<FleetInterface> fleet_interface =
928 std::dynamic_pointer_cast<FleetInterface>(
930 if (fleet_interface) {
931 fleet_interface->finalize();
935 " not found in live objects.");
939 std::shared_ptr<FleetInterface> fleet_interface =
940 std::dynamic_pointer_cast<FleetInterface>(
942 if (fleet_interface) {
946 " not found in live objects.");
952 ss <<
"\"density_components\": [\n";
955 uint32_t, std::shared_ptr<DistributionsInterfaceBase>>::iterator dit;
958 std::shared_ptr<DistributionsInterfaceBase> dist_interface =
960 if (dist_interface) {
961 dist_interface->finalize();
962 ss << dist_interface->to_json();
969 ss <<
"\"data\": [\n";
970 typename std::map<uint32_t, std::shared_ptr<DataInterfaceBase>>::iterator
974 std::shared_ptr<DataInterfaceBase> data_interface = (*d_it).second;
975 if (data_interface) {
976 data_interface->finalize();
977 ss << data_interface->to_json();
986 model->do_reporting =
true;
993 template <
typename Type>
994 bool add_to_fims_tmb_internal() {
995 std::shared_ptr<fims_info::Information<Type>> info =
998 std::shared_ptr<fims_popdy::CatchAtAge<Type>> model =
999 std::make_shared<fims_popdy::CatchAtAge<Type>>();
1005 model->AddPopulation((*it));
1008 std::set<uint32_t> fleet_ids;
1009 typedef typename std::set<uint32_t>::iterator fleet_ids_iterator;
1012 info->models_map[this->
get_id()] = model;
1016 auto population_interface_it =
1018 if (population_interface_it ==
1021 " not found in live objects.");
1025 std::shared_ptr<PopulationInterface> population_interface =
1026 std::dynamic_pointer_cast<PopulationInterface>(
1027 population_interface_it->second);
1028 if (!population_interface) {
1030 " not found in live objects.");
1034 model->InitializePopulationDerivedQuantities(population_interface->id);
1035 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1036 model->GetPopulationDerivedQuantities(population_interface->id);
1038 std::map<std::string, fims_popdy::DimensionInfo>
1039 &derived_quantities_dim_info =
1040 model->GetPopulationDimensionInfo(population_interface->id);
1042 std::stringstream ss;
1044 derived_quantities[
"total_catch_weight"] =
1047 derived_quantities_dim_info[
"total_catch_weight"] =
1049 "total_catch_weight",
1052 info->variable_map[population_interface->total_catch_weight.id_m] =
1053 &derived_quantities[
"total_catch_weight"];
1055 derived_quantities[
"total_catch_numbers"] =
1058 derived_quantities_dim_info[
"total_catch_numbers"] =
1060 "total_catch_numbers",
1063 info->variable_map[population_interface->total_catch_numbers.id_m] =
1064 &derived_quantities[
"total_catch_numbers"];
1066 derived_quantities[
"mortality_F"] =
1068 population_interface->n_ages.get());
1072 population_interface->n_ages.get()},
1074 info->variable_map[population_interface->mortality_F.id_m] =
1075 &derived_quantities[
"mortality_F"];
1077 derived_quantities[
"mortality_M"] =
1079 population_interface->n_ages.get());
1083 population_interface->n_ages.get()},
1085 info->variable_map[population_interface->mortality_M.id_m] =
1086 &derived_quantities[
"mortality_M"];
1088 derived_quantities[
"mortality_Z"] =
1090 population_interface->n_ages.get());
1094 population_interface->n_ages.get()},
1096 info->variable_map[population_interface->mortality_Z.id_m] =
1097 &derived_quantities[
"mortality_Z"];
1099 derived_quantities[
"numbers_at_age"] =
1101 population_interface->n_ages.get());
1105 population_interface->n_ages.get()},
1107 info->variable_map[population_interface->numbers_at_age.id_m] =
1108 &derived_quantities[
"numbers_at_age"];
1110 derived_quantities[
"unfished_numbers_at_age"] =
1112 population_interface->n_ages.get());
1113 derived_quantities_dim_info[
"unfished_numbers_at_age"] =
1115 "unfished_numbers_at_age",
1117 population_interface->n_ages.get()},
1119 info->variable_map[population_interface->unfished_numbers_at_age.id_m] =
1120 &derived_quantities[
"unfished_numbers_at_age"];
1122 derived_quantities[
"biomass"] =
1128 info->variable_map[population_interface->biomass.id_m] =
1129 &derived_quantities[
"biomass"];
1131 derived_quantities[
"spawning_biomass"] =
1133 derived_quantities_dim_info[
"spawning_biomass"] =
1138 info->variable_map[population_interface->spawning_biomass.id_m] =
1139 &derived_quantities[
"spawning_biomass"];
1141 derived_quantities[
"unfished_biomass"] =
1143 derived_quantities_dim_info[
"unfished_biomass"] =
1148 info->variable_map[population_interface->unfished_biomass.id_m] =
1149 &derived_quantities[
"unfished_biomass"];
1151 derived_quantities[
"unfished_spawning_biomass"] =
1153 derived_quantities_dim_info[
"unfished_spawning_biomass"] =
1155 "unfished_spawning_biomass",
1158 info->variable_map[population_interface->unfished_spawning_biomass.id_m] =
1159 &derived_quantities[
"unfished_spawning_biomass"];
1161 derived_quantities[
"proportion_mature_at_age"] =
1163 population_interface->n_ages.get());
1164 derived_quantities_dim_info[
"proportion_mature_at_age"] =
1166 "proportion_mature_at_age",
1168 population_interface->n_ages.get()},
1170 info->variable_map[population_interface->proportion_mature_at_age.id_m] =
1171 &derived_quantities[
"proportion_mature_at_age"];
1173 derived_quantities[
"expected_recruitment"] =
1175 derived_quantities_dim_info[
"expected_recruitment"] =
1177 "expected_recruitment",
1180 info->variable_map[population_interface->expected_recruitment.id_m] =
1181 &derived_quantities[
"expected_recruitment"];
1183 derived_quantities[
"sum_selectivity"] =
1185 population_interface->n_ages.get());
1186 derived_quantities_dim_info[
"sum_selectivity"] =
1190 population_interface->n_ages.get()},
1192 info->variable_map[population_interface->sum_selectivity.id_m] =
1193 &derived_quantities[
"sum_selectivity"];
1197 for (fleet_ids_iterator fit = population_interface->fleet_ids->begin();
1198 fit != population_interface->fleet_ids->end(); ++fit) {
1199 fleet_ids.insert(*fit);
1203 for (fleet_ids_iterator it = fleet_ids.begin(); it != fleet_ids.end();
1208 " not found in live objects.");
1212 std::shared_ptr<FleetInterface> fleet_interface =
1213 std::dynamic_pointer_cast<FleetInterface>(fleet_interface_it->second);
1214 if (!fleet_interface) {
1216 " not found in live objects.");
1219 model->InitializeFleetDerivedQuantities(fleet_interface->id);
1220 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1221 model->GetFleetDerivedQuantities(fleet_interface->id);
1223 std::map<std::string, fims_popdy::DimensionInfo>
1224 &derived_quantities_dim_info =
1225 model->GetFleetDimensionInfo(fleet_interface->id);
1230 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1231 derived_quantities_dim_info[
"catch_numbers_at_age"] =
1233 "catch_numbers_at_age",
1235 fleet_interface->n_ages.get()},
1237 info->variable_map[fleet_interface->catch_numbers_at_age.id_m] =
1238 &derived_quantities[
"catch_numbers_at_age"];
1241 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1242 derived_quantities_dim_info[
"catch_weight_at_age"] =
1244 "catch_weight_at_age",
1246 fleet_interface->n_ages.get()},
1248 info->variable_map[fleet_interface->catch_weight_at_age.id_m] =
1249 &derived_quantities[
"catch_weight_at_age"];
1252 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1253 derived_quantities_dim_info[
"catch_numbers_at_length"] =
1255 "catch_numbers_at_length",
1257 fleet_interface->n_lengths.get()},
1259 info->variable_map[fleet_interface->catch_numbers_at_length.id_m] =
1260 &derived_quantities[
"catch_numbers_at_length"];
1262 derived_quantities[
"catch_weight"] =
1267 info->variable_map[fleet_interface->catch_weight.id_m] =
1268 &derived_quantities[
"catch_weight"];
1270 derived_quantities[
"catch_numbers"] =
1275 info->variable_map[fleet_interface->catch_numbers.id_m] =
1276 &derived_quantities[
"catch_numbers"];
1278 derived_quantities[
"catch_expected"] =
1283 info->variable_map[fleet_interface->catch_expected.id_m] =
1284 &derived_quantities[
"catch_expected"];
1286 derived_quantities[
"log_catch_expected"] =
1288 derived_quantities_dim_info[
"log_catch_expected"] =
1290 "log_catch_expected",
1293 info->variable_map[fleet_interface->log_catch_expected.id_m] =
1294 &derived_quantities[
"log_catch_expected"];
1297 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1298 derived_quantities_dim_info[
"agecomp_proportion"] =
1300 "agecomp_proportion",
1302 fleet_interface->n_ages.get()},
1304 info->variable_map[fleet_interface->agecomp_proportion.id_m] =
1305 &derived_quantities[
"agecomp_proportion"];
1308 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1309 derived_quantities_dim_info[
"lengthcomp_proportion"] =
1311 "lengthcomp_proportion",
1313 fleet_interface->n_lengths.get()},
1315 info->variable_map[fleet_interface->lengthcomp_proportion.id_m] =
1316 &derived_quantities[
"lengthcomp_proportion"];
1320 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1321 derived_quantities_dim_info[
"index_numbers_at_age"] =
1323 "index_numbers_at_age",
1325 fleet_interface->n_ages.get()},
1327 info->variable_map[fleet_interface->index_numbers_at_age.id_m] =
1328 &derived_quantities[
"index_numbers_at_age"];
1331 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1332 derived_quantities_dim_info[
"index_weight_at_age"] =
1334 "index_weight_at_age",
1336 fleet_interface->n_ages.get()},
1338 info->variable_map[fleet_interface->index_weight_at_age.id_m] =
1339 &derived_quantities[
"index_weight_at_age"];
1342 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1343 derived_quantities_dim_info[
"index_numbers_at_length"] =
1345 "index_numbers_at_length",
1347 fleet_interface->n_lengths.get()},
1349 info->variable_map[fleet_interface->index_numbers_at_length.id_m] =
1350 &derived_quantities[
"index_numbers_at_length"];
1352 derived_quantities[
"index_weight"] =
1357 info->variable_map[fleet_interface->index_weight.id_m] =
1358 &derived_quantities[
"index_weight"];
1360 derived_quantities[
"index_numbers"] =
1365 info->variable_map[fleet_interface->index_numbers.id_m] =
1366 &derived_quantities[
"index_numbers"];
1368 derived_quantities[
"index_expected"] =
1373 info->variable_map[fleet_interface->index_expected.id_m] =
1374 &derived_quantities[
"index_expected"];
1376 derived_quantities[
"log_index_expected"] =
1378 derived_quantities_dim_info[
"log_index_expected"] =
1380 "log_index_expected",
1383 info->variable_map[fleet_interface->log_index_expected.id_m] =
1384 &derived_quantities[
"log_index_expected"];
1387 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1388 derived_quantities_dim_info[
"agecomp_expected"] =
1392 (fleet_interface->n_ages.get())},
1394 info->variable_map[fleet_interface->agecomp_expected.id_m] =
1395 &derived_quantities[
"agecomp_expected"];
1398 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1399 derived_quantities_dim_info[
"lengthcomp_expected"] =
1401 "lengthcomp_expected",
1403 (fleet_interface->n_lengths.get())},
1405 info->variable_map[fleet_interface->lengthcomp_expected.id_m] =
1406 &derived_quantities[
"lengthcomp_expected"];
1413 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
1414 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
The CatchAtAgeInterface class is used to interface with the CatchAtAge model. It inherits from the Fi...
Definition rcpp_models.hpp:216
virtual std::string to_json()
Serialize the fishery model to a JSON string.
Definition rcpp_models.hpp:720
void AddPopulation(uint32_t id)
Definition rcpp_models.hpp:239
std::string derived_quantity_to_json(std::map< std::string, fims::Vector< double > >::iterator it, const fims_popdy::DimensionInfo &dim_info)
Definition rcpp_models.hpp:501
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_models.hpp:328
CatchAtAgeInterface()
The constructor.
Definition rcpp_models.hpp:221
void DoReporting(bool report)
Enable or disable reporting for the CatchAtAge model.
Definition rcpp_models.hpp:260
void ReportAgeToLengthConversionDerivedTensor(bool report)
Turn reporting of the Growth-derived age-to-length tensor on or off.
Definition rcpp_models.hpp:305
virtual uint32_t get_id()
Method to get this id.
Definition rcpp_models.hpp:323
std::string fleet_to_json(FleetInterface *fleet_interface)
Method to convert a fleet to a JSON string.
Definition rcpp_models.hpp:613
std::string derived_quantities_component_to_json(std::map< std::string, fims::Vector< double > > &dqs, std::map< std::string, fims_popdy::DimensionInfo > &dim_info)
Send the fleet-based derived quantities to the json file.
Definition rcpp_models.hpp:578
std::string population_to_json(PopulationInterface *population_interface)
Method to convert a population to a JSON string.
Definition rcpp_models.hpp:333
CatchAtAgeInterface(const CatchAtAgeInterface &other)
Construct a new Catch At Age Interface object.
Definition rcpp_models.hpp:233
bool IsReporting()
Check if reporting is enabled for the CatchAtAge model.
Definition rcpp_models.hpp:282
static std::map< uint32_t, std::shared_ptr< DataInterfaceBase > > live_objects
The map associating the IDs of DataInterfaceBase to the objects. This is a live object,...
Definition rcpp_data.hpp:43
static std::map< uint32_t, std::shared_ptr< DistributionsInterfaceBase > > live_objects
The map associating the ID of the DistributionsInterfaceBase to the DistributionsInterfaceBase object...
Definition rcpp_distribution.hpp:72
Base class for all interface objects.
Definition rcpp_interface_base.hpp:655
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:665
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:670
The FisheryModelInterfaceBase class is the base class for all fishery models in the FIMS Rcpp interfa...
Definition rcpp_models.hpp:36
double min(const std::valarray< double > &v)
Minimum method to calculate the minimum of an array or vector of doubles.
Definition rcpp_models.hpp:185
FisheryModelInterfaceBase(const FisheryModelInterfaceBase &other)
Construct a new Data Interface Base object.
Definition rcpp_models.hpp:80
static uint32_t id_g
The static id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:51
virtual uint32_t get_id()=0
Get the ID for the child fleet interface objects to inherit.
FisheryModelInterfaceBase()
The constructor.
Definition rcpp_models.hpp:67
std::set< uint32_t >::iterator population_id_iterator
Iterator for population ids.
Definition rcpp_models.hpp:45
uint32_t id
The local id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:55
std::valarray< double > fabs(const std::valarray< double > &v)
A function to compute the absolute value of a value array of floating-point values....
Definition rcpp_models.hpp:203
virtual std::string to_json()
Serialize the fishery model to a JSON string.
Definition rcpp_models.hpp:101
static std::map< uint32_t, std::shared_ptr< FisheryModelInterfaceBase > > live_objects
The map associating the IDs of FleetInterfaceBase to the objects. This is a live object,...
Definition rcpp_models.hpp:62
double sum(const std::valarray< double > &v)
Sum method to calculate the sum of an array or vector of doubles.
Definition rcpp_models.hpp:156
Rcpp::NumericVector get_random_parameters_vector()
Get the vector of random effect parameters for the model.
Definition rcpp_models.hpp:137
Rcpp::NumericVector get_fixed_parameters_vector()
Get the vector of fixed effect parameters for the model.
Definition rcpp_models.hpp:117
std::shared_ptr< std::set< uint32_t > > population_ids
The set of population ids that this fishery model operates on.
Definition rcpp_models.hpp:41
virtual ~FisheryModelInterfaceBase()
The destructor.
Definition rcpp_models.hpp:86
double sum(const std::vector< double > &v)
Sum method for a vector of doubles.
Definition rcpp_models.hpp:170
uint32_t id
The local id of the FleetInterfaceBase object.
Definition rcpp_fleet.hpp:28
static std::map< uint32_t, std::shared_ptr< FleetInterfaceBase > > live_objects
The map associating the IDs of FleetInterfaceBase to the objects. This is a live object,...
Definition rcpp_fleet.hpp:34
The Rcpp interface for Fleet to instantiate from R: fleet <- methods::new(Fleet)
Definition rcpp_fleet.hpp:68
SharedInt n_years
The number of years in the fleet data.
Definition rcpp_fleet.hpp:124
uint32_t GetSelectivityID()
Get the unique ID for the selectivity object.
Definition rcpp_fleet.hpp:374
int GetObservedIndexDataID()
Get the unique id for the observed index data object.
Definition rcpp_fleet.hpp:408
int GetObservedCatchDataID()
Get the unique id for the observed catch data object.
Definition rcpp_fleet.hpp:415
int GetObservedAgeCompDataID()
Get the unique ID for the observed age-composition data object.
Definition rcpp_fleet.hpp:379
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_fleet.hpp:300
int GetObservedLengthCompDataID()
Get the unique ID for the observed length-composition data object.
Definition rcpp_fleet.hpp:387
SharedString name
The name of the fleet.
Definition rcpp_fleet.hpp:108
SharedInt n_ages
The number of age bins in the fleet data.
Definition rcpp_fleet.hpp:112
VariableVector log_q
The natural log of the index of abundance scaling parameter for this fleet.
Definition rcpp_fleet.hpp:139
VariableVector log_Fmort
The vector of the natural log of fishing mortality rates for this fleet.
Definition rcpp_fleet.hpp:144
SharedInt n_lengths
The number of length bins in the fleet data.
Definition rcpp_fleet.hpp:116
RealVector lengths
Fleet-specific observation-bin centers.
Definition rcpp_fleet.hpp:120
static std::map< uint32_t, std::shared_ptr< GrowthInterfaceBase > > live_objects
The map associating the IDs of GrowthInterfaceBase to the objects. This is a live object,...
Definition rcpp_growth.hpp:39
static std::map< uint32_t, std::shared_ptr< MaturityInterfaceBase > > live_objects
The map associating the IDs of MaturityInterfaceBase to the objects. This is a live object,...
Definition rcpp_maturity.hpp:35
uint32_t id
The local id of the PopulationInterfaceBase object.
Definition rcpp_population.hpp:28
static std::map< uint32_t, std::shared_ptr< PopulationInterfaceBase > > live_objects
The map associating the IDs of PopulationInterfaceBase to the objects. This is a live object,...
Definition rcpp_population.hpp:35
Rcpp interface for a new Population to instantiate from R: population <- methods::new(population)
Definition rcpp_population.hpp:80
VariableVector proportion_female
Proportion of females in the population.
Definition rcpp_population.hpp:146
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_population.hpp:290
VariableVector log_init_naa
The natural log of the initial numbers at age.
Definition rcpp_population.hpp:137
SharedString name
The name for the population.
Definition rcpp_population.hpp:155
SharedInt n_ages
The number of age bins.
Definition rcpp_population.hpp:85
SharedInt maturity_id
The ID of the maturity module.
Definition rcpp_population.hpp:109
VariableVector log_M
The natural log of the natural mortality for each year.
Definition rcpp_population.hpp:125
SharedInt n_years
The number of years.
Definition rcpp_population.hpp:101
VariableVector log_f_multiplier
Log of the population annual fishing mortality multiplier.
Definition rcpp_population.hpp:133
RealVector ages
Ages that are modeled in the population, the length of this vector should equal "n_ages".
Definition rcpp_population.hpp:151
VariableVector spawning_biomass_ratio
The population spawning biomass ratio for each year.
Definition rcpp_population.hpp:129
SharedInt growth_id
The ID of the growth module.
Definition rcpp_population.hpp:113
SharedInt recruitment_id
The ID of the recruitment module.
Definition rcpp_population.hpp:117
static std::map< uint32_t, std::shared_ptr< RecruitmentInterfaceBase > > live_objects
The map associating the IDs of RecruitmentInterfaceBase to the objects. This is a live object,...
Definition rcpp_recruitment.hpp:40
static std::map< uint32_t, std::shared_ptr< SelectivityInterfaceBase > > live_objects
The map associating the IDs of SelectivityInterfaceBase to the objects. This is a live object,...
Definition rcpp_selectivity.hpp:35
int get() const
Retrieve the value of the integer.
Definition rcpp_shared_primitive.hpp:127
std::string get() const
Retrieves the string value managed by the object.
Definition rcpp_shared_primitive.hpp:1617
uint32_t id_m
The local ID of the Variable object.
Definition rcpp_interface_base.hpp:156
size_t size()
Returns the size of a VariableVector.
Definition rcpp_interface_base.hpp:287
static std::string PrettyFormatJSON(const std::string &json)
Formats a JSON string.
Definition fims_json.hpp:182
static std::string EscapeString(const std::string &input)
Definition fims_json.hpp:143
Definition fims_vector.hpp:27
size_type size() const
Returns the number of elements.
Definition fims_vector.hpp:299
static std::shared_ptr< Model< Type > > GetInstance()
Evaluate. Calculates the joint negative log-likelihood function.
Definition model.hpp:55
Platform macros and the core FIMS logging system.
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:664
: Loops over model components and returns the negative log-likelihood function.
The Rcpp interface to declare different types of data, e.g., age-composition and index data....
The Rcpp interface to declare different distributions, e.g., normal and log normal....
The Rcpp interface to declare fleets. Allows for the use of methods::new() in R.
The Rcpp interface to declare different types of growth, e.g., empirical weight-at-age data....
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...
double sanitize_val(double x)
Sanitize a double value by replacing NaN or Inf with -999.0.
Definition rcpp_interface_base.hpp:112
The Rcpp interface to declare different maturity options, e.g., logistic. Allows for the use of metho...
The Rcpp interface to declare different types of populations. Allows for the use of methods::new() in...
The Rcpp interface to declare different types of recruitment, e.g., Beverton–Holt stock–recruitment r...
The Rcpp interface to declare different types of selectivity, e.g., logistic and double logistic....
Structure to hold dimension information for derived quantities.
Definition fishery_model_base.hpp:25
fims::Vector< int > dims
Definition fishery_model_base.hpp:28
int ndims
Definition fishery_model_base.hpp:27
fims::Vector< std::string > dim_names
Definition fishery_model_base.hpp:29