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\": \"" << population_interface->
name <<
"\",\n";
370 ss <<
" \"module_id\": " << population_interface->
id <<
",\n";
371 ss <<
" \"recruitment_id\": " << population_interface->
recruitment_id
373 ss <<
" \"growth_id\": " << population_interface->
growth_id <<
",\n";
374 ss <<
" \"maturity_id\": " << population_interface->
maturity_id <<
",\n";
376 ss <<
" \"parameters\": [\n";
377 for (
size_t i = 0; i < pop->log_M.size(); i++) {
378 population_interface_ptr->log_M[i].final_value_m = pop->log_M[i];
381 ss <<
"{\n \"name\": \"log_M\",\n";
382 ss <<
" \"id\":" << population_interface->
log_M.
id_m <<
",\n";
383 ss <<
" \"type\": \"vector\",\n";
384 ss <<
" \"dimensionality\": {\n";
385 ss <<
" \"header\": [" <<
"\"n_years\", \"n_ages\"" <<
"],\n";
386 ss <<
" \"dimensions\": [" << population_interface->
n_years.
get() <<
", "
387 << population_interface->
n_ages.
get() <<
"]\n},\n";
388 ss <<
" \"values\": " << population_interface->
log_M <<
"\n\n";
391 for (
size_t i = 0; i < pop->log_f_multiplier.size(); i++) {
392 population_interface_ptr->log_f_multiplier[i].final_value_m =
393 pop->log_f_multiplier[i];
396 ss <<
"{\n \"name\": \"log_f_multiplier\",\n";
398 ss <<
" \"type\": \"vector\",\n";
399 ss <<
" \"dimensionality\": {\n";
400 ss <<
" \"header\": [" <<
"\"n_years\"" <<
"],\n";
401 ss <<
" \"dimensions\": [" << population_interface->
n_years.
get()
406 for (
size_t i = 0; i < pop->spawning_biomass_ratio.size(); i++) {
407 population_interface_ptr->spawning_biomass_ratio[i].final_value_m =
408 pop->spawning_biomass_ratio[i];
411 ss <<
"{\n \"name\": \"spawning_biomass_ratio\",\n";
414 ss <<
" \"type\": \"vector\",\n";
415 ss <<
" \"dimensionality\": {\n";
416 ss <<
" \"header\": [" <<
"\"n_years\"" <<
"],\n";
417 ss <<
" \"dimensions\": [" << (population_interface->
n_years.
get() + 1)
423 for (
size_t i = 0; i < pop->log_init_naa.size(); i++) {
424 population_interface_ptr->log_init_naa[i].final_value_m =
425 pop->log_init_naa[i];
427 ss <<
" {\n\"name\": \"log_init_naa\",\n";
429 ss <<
" \"type\": \"vector\",\n";
430 ss <<
" \"dimensionality\": {\n";
431 ss <<
" \"header\": [" <<
"\"n_ages\"" <<
"],\n";
432 ss <<
" \"dimensions\": [" << population_interface->
n_ages.
get()
435 ss <<
" \"values\":" << population_interface->
log_init_naa <<
"\n";
440 population_interface_ptr->proportion_female[i].final_value_m =
441 pop->proportion_female.get_force_scalar(i);
443 ss <<
" {\n\"name\": \"proportion_female\",\n";
446 ss <<
" \"type\": \"vector\",\n";
447 ss <<
" \"dimensionality\": {\n";
448 ss <<
" \"header\": [" <<
"\"n_ages\"" <<
"],\n";
449 ss <<
" \"dimensions\": ["
455 ss <<
" \"derived_quantities\": [\n";
457 std::map<std::string, fims::Vector<double>> dqs =
458 model_ptr->GetPopulationDerivedQuantities(
459 population_interface->
get_id());
461 std::map<std::string, fims_popdy::DimensionInfo> dim_info =
462 model_ptr->GetPopulationDimensionInfo(population_interface->
get_id());
467 ss <<
" \"name\": \"Population\",\n";
469 ss <<
" \"type\": \"population\",\n";
470 ss <<
" \"tag\": \"" << population_interface->
get_id()
471 <<
" not found in Information.\",\n";
472 ss <<
" \"id\": " << population_interface->
get_id() <<
",\n";
473 ss <<
" \"recruitment_id\": " << population_interface->
recruitment_id
475 ss <<
" \"growth_id\": " << population_interface->
growth_id <<
",\n";
476 ss <<
" \"maturity_id\": " << population_interface->
maturity_id <<
",\n";
477 ss <<
" \"derived_quantities\": []}\n";
491 std::stringstream ss;
493 std::stringstream dim_entry;
495 switch (dim_info.
ndims) {
497 dim_entry <<
"\"dimensionality\": {\n";
498 dim_entry <<
" \"header\": [\"" << dim_info.
dim_names[0] <<
"\"],\n";
499 dim_entry <<
" \"dimensions\": [";
500 for (
size_t i = 0; i < dim_info.
dims.
size(); ++i) {
501 if (i > 0) dim_entry <<
", ";
502 dim_entry << dim_info.
dims[i];
508 dim_entry <<
"\"dimensionality\": {\n";
509 dim_entry <<
" \"header\": [\"" << dim_info.
dim_names[0] <<
"\", \""
511 dim_entry <<
" \"dimensions\": [";
512 for (
size_t i = 0; i < dim_info.
dims.
size(); ++i) {
513 if (i > 0) dim_entry <<
", ";
514 dim_entry << dim_info.
dims[i];
520 dim_entry <<
"\"dimensionality\": {\n";
521 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\": [],\n";
535 dim_entry <<
" \"dimensions\": []\n";
542 ss <<
"\"name\":\"" << (*it).first <<
"\",\n";
543 ss << dim_entry.str() <<
",\n";
545 ss << std::fixed << std::setprecision(10);
547 for (
size_t i = 0; i < dq.
size() - 1; i++) {
550 ss <<
"-999" <<
", ";
555 if (dq[dq.
size() - 1] != dq[dq.
size() - 1])
557 ss <<
"-999]" <<
"\n";
559 ss << dq[dq.
size() - 1] <<
"]\n";
575 std::map<std::string, fims_popdy::DimensionInfo> &dim_info) {
576 std::stringstream ss;
577 std::map<std::string, fims_popdy::DimensionInfo>::iterator dim_info_it;
578 std::map<std::string, fims::Vector<double>>::iterator it;
579 std::map<std::string, fims::Vector<double>>::iterator end_it;
581 typename std::map<std::string, fims::Vector<double>>::iterator
583 second_to_last = dqs.end();
589 for (; it != second_to_last; ++it) {
590 dim_info_it = dim_info.find(it->first);
594 dim_info_it = dim_info.find(second_to_last->first);
595 if (dim_info_it != dim_info.end()) {
609 std::stringstream ss;
611 if (!fleet_interface) {
613 "Fleet pointer is null; cannot get id. Not found in live objects.");
617 std::shared_ptr<fims_info::Information<double>> info =
622 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
623 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
628 fit = info->fleets.find(fleet_interface->
get_id());
630 if (fit != info->fleets.end()) {
631 std::shared_ptr<fims_popdy::Fleet<double>> &fleet = (*fit).second;
634 ss <<
" \"module_name\": \"Fleet\",\n";
635 ss <<
" \"fleet\": \"" << fleet_interface->
name <<
"\",\n";
636 ss <<
" \"module_id\": " << fleet_interface->
id <<
",\n";
637 ss <<
" \"n_ages\": " << fleet_interface->
n_ages.
get() <<
",\n";
638 ss <<
" \"n_years\": " << fleet_interface->
n_years.
get() <<
",\n";
639 ss <<
" \"n_lengths\": " << fleet_interface->
n_lengths.
get() <<
",\n";
640 ss <<
"\"data_ids\" : [\n";
643 ss <<
"{\"lengthcomp\": "
650 ss <<
"\"parameters\": [\n";
652 for (
size_t i = 0; i < fleet_interface->
log_Fmort.
size(); i++) {
653 fleet_interface->
log_Fmort[i].final_value_m = fleet->log_Fmort[i];
656 ss <<
" \"name\": \"log_Fmort\",\n";
657 ss <<
" \"id\":" << fleet_interface->
log_Fmort.
id_m <<
",\n";
658 ss <<
" \"type\": \"vector\",\n";
659 ss <<
" \"dimensionality\": {\n";
660 ss <<
" \"header\": [\"" <<
"n_years" <<
"\"],\n";
661 ss <<
" \"dimensions\": [" << fleet_interface->
n_years.
get()
663 ss <<
" \"values\": " << fleet_interface->
log_Fmort <<
"},\n";
666 for (
size_t i = 0; i < fleet->log_q.size(); i++) {
667 fleet_interface->
log_q[i].final_value_m = fleet->log_q[i];
669 ss <<
" \"name\": \"log_q\",\n";
670 ss <<
" \"id\":" << fleet_interface->
log_q.
id_m <<
",\n";
671 ss <<
" \"type\": \"vector\",\n";
672 ss <<
" \"dimensionality\": {\n";
673 ss <<
" \"header\": [\"" <<
"na" <<
"\"],\n";
674 ss <<
" \"dimensions\": [" << fleet->log_q.size() <<
"]\n},\n";
676 ss <<
" \"values\": " << fleet_interface->
log_q <<
"}\n";
678 ss <<
"], \"derived_quantities\": [";
680 std::map<std::string, fims::Vector<double>> dqs =
681 model_ptr->GetFleetDerivedQuantities(fleet_interface->
get_id());
682 std::map<std::string, fims_popdy::DimensionInfo> dim_info =
683 model_ptr->GetFleetDimensionInfo(fleet_interface->
get_id());
687 ss <<
" \"name\": \"Fleet\",\n";
688 ss <<
" \"type\": \"fleet\",\n";
689 ss <<
" \"tag\": \"" << fleet_interface->
get_id()
690 <<
" not found in Information.\",\n";
691 ss <<
" \"derived_quantities\": []}\n";
700 std::set<uint32_t> recruitment_ids;
701 std::set<uint32_t> growth_ids;
702 std::set<uint32_t> maturity_ids;
703 std::set<uint32_t> selectivity_ids;
704 std::set<uint32_t> fleet_ids;
706 typename std::set<uint32_t>::iterator module_id_it;
707 typename std::set<uint32_t>::iterator pit;
708 typename std::set<uint32_t>::iterator fids;
711 std::shared_ptr<PopulationInterface> population_interface =
712 std::dynamic_pointer_cast<PopulationInterface>(
714 if (population_interface) {
715 recruitment_ids.insert(population_interface->recruitment_id.get());
716 growth_ids.insert(population_interface->growth_id.get());
717 maturity_ids.insert(population_interface->maturity_id.get());
719 for (fids = population_interface->fleet_ids->begin();
720 fids != population_interface->fleet_ids->end(); fids++) {
721 fleet_ids.insert(*fids);
726 for (fids = fleet_ids.begin(); fids != fleet_ids.end(); fids++) {
727 std::shared_ptr<FleetInterface> fleet_interface =
728 std::dynamic_pointer_cast<FleetInterface>(
730 if (fleet_interface) {
731 selectivity_ids.insert(fleet_interface->GetSelectivityID());
735 std::shared_ptr<fims_info::Information<double>> info =
738 std::shared_ptr<fims_popdy::CatchAtAge<double>> model =
739 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
740 info->models_map[this->
get_id()]);
742 std::shared_ptr<fims_model::Model<double>> model_internal =
746 model->do_reporting =
false;
749 double value = model_internal->Evaluate();
751 std::stringstream ss;
756 ss <<
" \"name\": \"CatchAtAge\",\n";
757 ss <<
" \"type\": \"model\",\n";
758 ss <<
" \"estimation_framework\": ";
760 ss <<
"\"Template_Model_Builder (TMB)\",";
764 ss <<
" \"id\": " << this->
get_id() <<
",\n";
765 ss <<
" \"objective_function_value\": " <<
sanitize_val(value) <<
",\n";
766 ss <<
"\"growth\":[\n";
767 for (module_id_it = growth_ids.begin(); module_id_it != growth_ids.end();
769 std::shared_ptr<GrowthInterfaceBase> growth_interface =
772 if (growth_interface != NULL) {
773 growth_interface->finalize();
774 ss << growth_interface->to_json();
775 if (std::next(module_id_it) != growth_ids.end()) {
783 ss <<
"\"recruitment\": [\n";
784 for (module_id_it = recruitment_ids.begin();
785 module_id_it != recruitment_ids.end(); module_id_it++) {
786 std::shared_ptr<RecruitmentInterfaceBase> recruitment_interface =
788 if (recruitment_interface) {
789 recruitment_interface->finalize();
790 ss << recruitment_interface->to_json();
791 if (std::next(module_id_it) != recruitment_ids.end()) {
798 ss <<
"\"maturity\": [\n";
799 for (module_id_it = maturity_ids.begin();
800 module_id_it != maturity_ids.end(); module_id_it++) {
801 std::shared_ptr<MaturityInterfaceBase> maturity_interface =
803 if (maturity_interface) {
804 maturity_interface->finalize();
805 ss << maturity_interface->to_json();
806 if (std::next(module_id_it) != maturity_ids.end()) {
813 ss <<
"\"selectivity\": [\n";
814 for (module_id_it = selectivity_ids.begin();
815 module_id_it != selectivity_ids.end(); module_id_it++) {
816 std::shared_ptr<SelectivityInterfaceBase> selectivity_interface =
818 if (selectivity_interface) {
819 selectivity_interface->finalize();
820 ss << selectivity_interface->to_json();
821 if (std::next(module_id_it) != selectivity_ids.end()) {
828 ss <<
" \"population_ids\": [";
837 ss <<
" \"fleet_ids\": [";
839 for (fids = fleet_ids.begin(); fids != fleet_ids.end(); fids++) {
841 if (std::next(fids) != fleet_ids.end()) {
846 ss <<
"\"populations\": [\n";
847 typename std::set<uint32_t>::iterator pop_it;
848 typename std::set<uint32_t>::iterator pop_end_it;
850 typename std::set<uint32_t>::iterator pop_second_to_last_it;
852 pop_second_to_last_it = std::prev(pop_end_it);
854 pop_second_to_last_it = pop_end_it;
857 pop_it != pop_second_to_last_it; pop_it++) {
858 std::shared_ptr<PopulationInterface> population_interface =
859 std::dynamic_pointer_cast<PopulationInterface>(
861 if (population_interface) {
862 std::set<uint32_t>::iterator fids;
863 for (fids = population_interface->fleet_ids->begin();
864 fids != population_interface->fleet_ids->end(); fids++) {
865 fleet_ids.insert(*fids);
867 population_interface->finalize();
871 " not found in live objects.");
876 std::shared_ptr<PopulationInterface> population_interface =
877 std::dynamic_pointer_cast<PopulationInterface>(
879 if (population_interface) {
880 std::set<uint32_t>::iterator fids;
881 for (fids = population_interface->fleet_ids->begin();
882 fids != population_interface->fleet_ids->end(); fids++) {
883 fleet_ids.insert(*fids);
888 " not found in live objects.");
894 ss <<
"\"fleets\": [\n";
896 typename std::set<uint32_t>::iterator fleet_it;
897 typename std::set<uint32_t>::iterator fleet_end_it;
898 fleet_end_it = fleet_ids.end();
899 typename std::set<uint32_t>::iterator fleet_second_to_last_it;
901 if (fleet_end_it != fleet_ids.begin()) {
902 fleet_second_to_last_it = std::prev(fleet_end_it);
904 for (fleet_it = fleet_ids.begin(); fleet_it != fleet_second_to_last_it;
906 std::shared_ptr<FleetInterface> fleet_interface =
907 std::dynamic_pointer_cast<FleetInterface>(
909 if (fleet_interface) {
910 fleet_interface->finalize();
914 " not found in live objects.");
918 std::shared_ptr<FleetInterface> fleet_interface =
919 std::dynamic_pointer_cast<FleetInterface>(
921 if (fleet_interface) {
925 " not found in live objects.");
931 ss <<
"\"density_components\": [\n";
934 uint32_t, std::shared_ptr<DistributionsInterfaceBase>>::iterator dit;
937 std::shared_ptr<DistributionsInterfaceBase> dist_interface =
939 if (dist_interface) {
940 dist_interface->finalize();
941 ss << dist_interface->to_json();
948 ss <<
"\"data\": [\n";
949 typename std::map<uint32_t, std::shared_ptr<DataInterfaceBase>>::iterator
953 std::shared_ptr<DataInterfaceBase> data_interface = (*d_it).second;
954 if (data_interface) {
955 data_interface->finalize();
956 ss << data_interface->to_json();
965 model->do_reporting =
true;
972 template <
typename Type>
973 bool add_to_fims_tmb_internal() {
974 std::shared_ptr<fims_info::Information<Type>> info =
977 std::shared_ptr<fims_popdy::CatchAtAge<Type>> model =
978 std::make_shared<fims_popdy::CatchAtAge<Type>>();
984 model->AddPopulation((*it));
987 std::set<uint32_t> fleet_ids;
988 typedef typename std::set<uint32_t>::iterator fleet_ids_iterator;
991 info->models_map[this->
get_id()] = model;
995 auto population_interface_it =
997 if (population_interface_it ==
1000 " not found in live objects.");
1004 std::shared_ptr<PopulationInterface> population_interface =
1005 std::dynamic_pointer_cast<PopulationInterface>(
1006 population_interface_it->second);
1007 if (!population_interface) {
1009 " not found in live objects.");
1013 model->InitializePopulationDerivedQuantities(population_interface->id);
1014 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1015 model->GetPopulationDerivedQuantities(population_interface->id);
1017 std::map<std::string, fims_popdy::DimensionInfo>
1018 &derived_quantities_dim_info =
1019 model->GetPopulationDimensionInfo(population_interface->id);
1021 std::stringstream ss;
1023 derived_quantities[
"total_catch_weight"] =
1026 derived_quantities_dim_info[
"total_catch_weight"] =
1028 "total_catch_weight",
1031 info->variable_map[population_interface->total_catch_weight.id_m] =
1032 &derived_quantities[
"total_catch_weight"];
1034 derived_quantities[
"total_catch_numbers"] =
1037 derived_quantities_dim_info[
"total_catch_numbers"] =
1039 "total_catch_numbers",
1042 info->variable_map[population_interface->total_catch_numbers.id_m] =
1043 &derived_quantities[
"total_catch_numbers"];
1045 derived_quantities[
"mortality_F"] =
1047 population_interface->n_ages.get());
1051 population_interface->n_ages.get()},
1053 info->variable_map[population_interface->mortality_F.id_m] =
1054 &derived_quantities[
"mortality_F"];
1056 derived_quantities[
"mortality_M"] =
1058 population_interface->n_ages.get());
1062 population_interface->n_ages.get()},
1064 info->variable_map[population_interface->mortality_M.id_m] =
1065 &derived_quantities[
"mortality_M"];
1067 derived_quantities[
"mortality_Z"] =
1069 population_interface->n_ages.get());
1073 population_interface->n_ages.get()},
1075 info->variable_map[population_interface->mortality_Z.id_m] =
1076 &derived_quantities[
"mortality_Z"];
1078 derived_quantities[
"numbers_at_age"] =
1080 population_interface->n_ages.get());
1084 population_interface->n_ages.get()},
1086 info->variable_map[population_interface->numbers_at_age.id_m] =
1087 &derived_quantities[
"numbers_at_age"];
1089 derived_quantities[
"unfished_numbers_at_age"] =
1091 population_interface->n_ages.get());
1092 derived_quantities_dim_info[
"unfished_numbers_at_age"] =
1094 "unfished_numbers_at_age",
1096 population_interface->n_ages.get()},
1098 info->variable_map[population_interface->unfished_numbers_at_age.id_m] =
1099 &derived_quantities[
"unfished_numbers_at_age"];
1101 derived_quantities[
"biomass"] =
1107 info->variable_map[population_interface->biomass.id_m] =
1108 &derived_quantities[
"biomass"];
1110 derived_quantities[
"spawning_biomass"] =
1112 derived_quantities_dim_info[
"spawning_biomass"] =
1117 info->variable_map[population_interface->spawning_biomass.id_m] =
1118 &derived_quantities[
"spawning_biomass"];
1120 derived_quantities[
"unfished_biomass"] =
1122 derived_quantities_dim_info[
"unfished_biomass"] =
1127 info->variable_map[population_interface->unfished_biomass.id_m] =
1128 &derived_quantities[
"unfished_biomass"];
1130 derived_quantities[
"unfished_spawning_biomass"] =
1132 derived_quantities_dim_info[
"unfished_spawning_biomass"] =
1134 "unfished_spawning_biomass",
1137 info->variable_map[population_interface->unfished_spawning_biomass.id_m] =
1138 &derived_quantities[
"unfished_spawning_biomass"];
1140 derived_quantities[
"proportion_mature_at_age"] =
1142 population_interface->n_ages.get());
1143 derived_quantities_dim_info[
"proportion_mature_at_age"] =
1145 "proportion_mature_at_age",
1147 population_interface->n_ages.get()},
1149 info->variable_map[population_interface->proportion_mature_at_age.id_m] =
1150 &derived_quantities[
"proportion_mature_at_age"];
1152 derived_quantities[
"expected_recruitment"] =
1154 derived_quantities_dim_info[
"expected_recruitment"] =
1156 "expected_recruitment",
1159 info->variable_map[population_interface->expected_recruitment.id_m] =
1160 &derived_quantities[
"expected_recruitment"];
1162 derived_quantities[
"sum_selectivity"] =
1164 population_interface->n_ages.get());
1165 derived_quantities_dim_info[
"sum_selectivity"] =
1169 population_interface->n_ages.get()},
1171 info->variable_map[population_interface->sum_selectivity.id_m] =
1172 &derived_quantities[
"sum_selectivity"];
1176 for (fleet_ids_iterator fit = population_interface->fleet_ids->begin();
1177 fit != population_interface->fleet_ids->end(); ++fit) {
1178 fleet_ids.insert(*fit);
1182 for (fleet_ids_iterator it = fleet_ids.begin(); it != fleet_ids.end();
1187 " not found in live objects.");
1191 std::shared_ptr<FleetInterface> fleet_interface =
1192 std::dynamic_pointer_cast<FleetInterface>(fleet_interface_it->second);
1193 if (!fleet_interface) {
1195 " not found in live objects.");
1198 model->InitializeFleetDerivedQuantities(fleet_interface->id);
1199 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1200 model->GetFleetDerivedQuantities(fleet_interface->id);
1202 std::map<std::string, fims_popdy::DimensionInfo>
1203 &derived_quantities_dim_info =
1204 model->GetFleetDimensionInfo(fleet_interface->id);
1209 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1210 derived_quantities_dim_info[
"catch_numbers_at_age"] =
1212 "catch_numbers_at_age",
1214 fleet_interface->n_ages.get()},
1216 info->variable_map[fleet_interface->catch_numbers_at_age.id_m] =
1217 &derived_quantities[
"catch_numbers_at_age"];
1220 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1221 derived_quantities_dim_info[
"catch_weight_at_age"] =
1223 "catch_weight_at_age",
1225 fleet_interface->n_ages.get()},
1227 info->variable_map[fleet_interface->catch_weight_at_age.id_m] =
1228 &derived_quantities[
"catch_weight_at_age"];
1231 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1232 derived_quantities_dim_info[
"catch_numbers_at_length"] =
1234 "catch_numbers_at_length",
1236 fleet_interface->n_lengths.get()},
1238 info->variable_map[fleet_interface->catch_numbers_at_length.id_m] =
1239 &derived_quantities[
"catch_numbers_at_length"];
1241 derived_quantities[
"catch_weight"] =
1246 info->variable_map[fleet_interface->catch_weight.id_m] =
1247 &derived_quantities[
"catch_weight"];
1249 derived_quantities[
"catch_numbers"] =
1254 info->variable_map[fleet_interface->catch_numbers.id_m] =
1255 &derived_quantities[
"catch_numbers"];
1257 derived_quantities[
"catch_expected"] =
1262 info->variable_map[fleet_interface->catch_expected.id_m] =
1263 &derived_quantities[
"catch_expected"];
1265 derived_quantities[
"log_catch_expected"] =
1267 derived_quantities_dim_info[
"log_catch_expected"] =
1269 "log_catch_expected",
1272 info->variable_map[fleet_interface->log_catch_expected.id_m] =
1273 &derived_quantities[
"log_catch_expected"];
1276 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1277 derived_quantities_dim_info[
"agecomp_proportion"] =
1279 "agecomp_proportion",
1281 fleet_interface->n_ages.get()},
1283 info->variable_map[fleet_interface->agecomp_proportion.id_m] =
1284 &derived_quantities[
"agecomp_proportion"];
1287 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1288 derived_quantities_dim_info[
"lengthcomp_proportion"] =
1290 "lengthcomp_proportion",
1292 fleet_interface->n_lengths.get()},
1294 info->variable_map[fleet_interface->lengthcomp_proportion.id_m] =
1295 &derived_quantities[
"lengthcomp_proportion"];
1299 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1300 derived_quantities_dim_info[
"index_numbers_at_age"] =
1302 "index_numbers_at_age",
1304 fleet_interface->n_ages.get()},
1306 info->variable_map[fleet_interface->index_numbers_at_age.id_m] =
1307 &derived_quantities[
"index_numbers_at_age"];
1310 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1311 derived_quantities_dim_info[
"index_weight_at_age"] =
1313 "index_weight_at_age",
1315 fleet_interface->n_ages.get()},
1317 info->variable_map[fleet_interface->index_weight_at_age.id_m] =
1318 &derived_quantities[
"index_weight_at_age"];
1321 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1322 derived_quantities_dim_info[
"index_numbers_at_length"] =
1324 "index_numbers_at_length",
1326 fleet_interface->n_lengths.get()},
1328 info->variable_map[fleet_interface->index_numbers_at_length.id_m] =
1329 &derived_quantities[
"index_numbers_at_length"];
1331 derived_quantities[
"index_weight"] =
1336 info->variable_map[fleet_interface->index_weight.id_m] =
1337 &derived_quantities[
"index_weight"];
1339 derived_quantities[
"index_numbers"] =
1344 info->variable_map[fleet_interface->index_numbers.id_m] =
1345 &derived_quantities[
"index_numbers"];
1347 derived_quantities[
"index_expected"] =
1352 info->variable_map[fleet_interface->index_expected.id_m] =
1353 &derived_quantities[
"index_expected"];
1355 derived_quantities[
"log_index_expected"] =
1357 derived_quantities_dim_info[
"log_index_expected"] =
1359 "log_index_expected",
1362 info->variable_map[fleet_interface->log_index_expected.id_m] =
1363 &derived_quantities[
"log_index_expected"];
1366 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1367 derived_quantities_dim_info[
"agecomp_expected"] =
1371 (fleet_interface->n_ages.get())},
1373 info->variable_map[fleet_interface->agecomp_expected.id_m] =
1374 &derived_quantities[
"agecomp_expected"];
1377 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1378 derived_quantities_dim_info[
"lengthcomp_expected"] =
1380 "lengthcomp_expected",
1382 (fleet_interface->n_lengths.get())},
1384 info->variable_map[fleet_interface->lengthcomp_expected.id_m] =
1385 &derived_quantities[
"lengthcomp_expected"];
1392 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
1393 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:699
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:488
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:608
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:573
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:67
Base class for all interface objects.
Definition rcpp_interface_base.hpp:656
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:666
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:671
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
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
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
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
uint32_t id_m
The local ID of the Variable object.
Definition rcpp_interface_base.hpp:157
size_t size()
Returns the size of a VariableVector.
Definition rcpp_interface_base.hpp:288
static std::string PrettyFormatJSON(const std::string &json)
Formats a JSON string.
Definition fims_json.hpp:128
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:54
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:113
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