FIMS  v0.10.0
Loading...
Searching...
No Matches
rcpp_models.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MODELS_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MODELS_HPP
11
12#include <set>
13#include "common/def.hpp"
15#include "../../../models/fisheries_models.hpp"
16#include "common/model.hpp"
17#include "../../../utilities/fims_json.hpp"
18#include "rcpp_population.hpp"
19#include "rcpp_fleet.hpp"
20#include "rcpp_growth.hpp"
21#include "rcpp_distribution.hpp"
22#include "rcpp_data.hpp"
23#include "rcpp_maturity.hpp"
24#include "rcpp_recruitment.hpp"
25#include "rcpp_selectivity.hpp"
26#include <valarray>
27#include <cmath>
28#include <mutex>
29
37 protected:
41 std::shared_ptr<std::set<uint32_t>> population_ids;
45 typedef typename std::set<uint32_t>::iterator population_id_iterator;
46
47 public:
51 static uint32_t id_g;
55 uint32_t id;
61 static std::map<uint32_t, std::shared_ptr<FisheryModelInterfaceBase>>
63
69 this->population_ids = std::make_shared<std::set<uint32_t>>();
70 /* Create instance of map: key is id and value is pointer to
71 FleetInterfaceBase */
72 // FisheryModelInterfaceBase::live_objects[this->id] = this;
73 }
74
82
87
101 virtual std::string to_json() {
102 return "std::string to_json() not yet implemented.";
103 }
104
108 virtual uint32_t get_id() = 0;
109
117 Rcpp::NumericVector get_fixed_parameters_vector() {
118 std::shared_ptr<fims_info::Information<double>> info0 =
120
121 Rcpp::NumericVector p;
122
123 for (size_t i = 0; i < info0->fixed_effects_parameters.size(); i++) {
124 p.push_back(*info0->fixed_effects_parameters[i]);
125 }
126
127 return p;
128 }
129
137 Rcpp::NumericVector get_random_parameters_vector() {
138 std::shared_ptr<fims_info::Information<double>> d0 =
140
141 Rcpp::NumericVector p;
142
143 for (size_t i = 0; i < d0->random_effects_parameters.size(); i++) {
144 p.push_back(*d0->random_effects_parameters[i]);
145 }
146
147 return p;
148 }
149
156 double sum(const std::valarray<double> &v) {
157 double sum = 0.0;
158 for (size_t i = 0; i < v.size(); i++) {
159 sum += v[i];
160 }
161 return sum;
162 }
163
170 double sum(const std::vector<double> &v) {
171 double sum = 0.0;
172 for (size_t i = 0; i < v.size(); i++) {
173 sum += v[i];
174 }
175 return sum;
176 }
177
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) {
189 min_value = v[i];
190 }
191 }
192 return min_value;
193 }
194
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]);
207 }
208 return result;
209 }
210};
211
217 public:
222 std::shared_ptr<CatchAtAgeInterface> caa =
223 std::make_shared<CatchAtAgeInterface>(*this);
226 }
227
235
239 void AddPopulation(uint32_t id) {
240 this->population_ids->insert(id);
241
242 std::map<uint32_t, std::shared_ptr<PopulationInterfaceBase>>::iterator pit;
244 if (pit != PopulationInterfaceBase::live_objects.end()) {
245 std::shared_ptr<PopulationInterfaceBase> &pop = (*pit).second;
246 pop->initialize_catch_at_age.set(true);
247 } else {
248 FIMS_ERROR_LOG("Population with id " + fims::to_string(id) +
249 " not found.");
250 }
251 }
252
260 void DoReporting(bool report) {
261#ifdef TMB_MODEL
262 std::shared_ptr<fims_info::Information<double>> info =
265 model_it = info->models_map.find(this->get_id());
266 if (model_it != info->models_map.end()) {
267 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
268 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
269 (*model_it).second);
270 model_ptr->do_reporting = report;
271 }
272#endif
273 }
274
282 bool IsReporting() {
283#ifdef TMB_MODEL
284 std::shared_ptr<fims_info::Information<double>> info =
287 model_it = info->models_map.find(this->get_id());
288 if (model_it != info->models_map.end()) {
289 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
290 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
291 (*model_it).second);
292 return model_ptr->do_reporting;
293 }
294 return false;
295#else
296 return false;
297#endif
298 }
299
306#ifdef TMB_MODEL
307 std::shared_ptr<fims_info::Information<double>> info =
310 model_it = info->models_map.find(this->get_id());
311 if (model_it != info->models_map.end()) {
312 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
313 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
314 (*model_it).second);
315 model_ptr->report_age_to_length_conversion_derived_tensor = report;
316 }
317#endif
318 }
319
323 virtual uint32_t get_id() { return this->id; }
324
328 virtual void finalize() {}
329
333 std::string population_to_json(PopulationInterface *population_interface) {
334 std::stringstream ss;
335
336 typename std::map<uint32_t,
337 std::shared_ptr<PopulationInterfaceBase>>::iterator
338 pi_it; // population interface iterator
340 population_interface->get_id());
341 if (pi_it == PopulationInterfaceBase::live_objects.end()) {
342 FIMS_ERROR_LOG("Population with id " +
343 fims::to_string(population_interface->get_id()) +
344 " not found in live objects.");
345 return "{}"; // Return empty JSON
346 }
347
348 std::shared_ptr<PopulationInterface> population_interface_ptr =
349 std::dynamic_pointer_cast<PopulationInterface>((*pi_it).second);
350
351 std::shared_ptr<fims_info::Information<double>> info =
353
355 model_it = info->models_map.find(this->get_id());
356 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
357 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
358 (*model_it).second);
359
361
362 pit = info->populations.find(population_interface->get_id());
363
364 if (pit != info->populations.end()) {
365 std::shared_ptr<fims_popdy::Population<double>> &pop = (*pit).second;
366 ss << "{\n";
367
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
372 << ",\n";
373 ss << " \"growth_id\": " << population_interface->growth_id << ",\n";
374 ss << " \"maturity_id\": " << population_interface->maturity_id << ",\n";
375
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];
379 }
380
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";
389 ss << "},\n";
390
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];
394 }
395
396 ss << "{\n \"name\": \"log_f_multiplier\",\n";
397 ss << " \"id\":" << population_interface->log_f_multiplier.id_m << ",\n";
398 ss << " \"type\": \"vector\",\n";
399 ss << " \"dimensionality\": {\n";
400 ss << " \"header\": [" << "\"n_years\"" << "],\n";
401 ss << " \"dimensions\": [" << population_interface->n_years.get()
402 << "]\n},\n";
403 ss << " \"values\": " << population_interface->log_f_multiplier << "\n\n";
404 ss << "},\n";
405
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];
409 }
410
411 ss << "{\n \"name\": \"spawning_biomass_ratio\",\n";
412 ss << " \"id\":" << population_interface->spawning_biomass_ratio.id_m
413 << ",\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)
418 << "]\n},\n";
419 ss << " \"values\": " << population_interface->spawning_biomass_ratio
420 << "\n\n";
421 ss << "},\n";
422
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];
426 }
427 ss << " {\n\"name\": \"log_init_naa\",\n";
428 ss << " \"id\":" << population_interface->log_init_naa.id_m << ",\n";
429 ss << " \"type\": \"vector\",\n";
430 ss << " \"dimensionality\": {\n";
431 ss << " \"header\": [" << "\"n_ages\"" << "],\n";
432 ss << " \"dimensions\": [" << population_interface->n_ages.get()
433 << "]\n},\n";
434
435 ss << " \"values\":" << population_interface->log_init_naa << "\n";
436 ss << "},\n";
437
438 for (size_t i = 0; i < population_interface->proportion_female.size();
439 i++) {
440 population_interface_ptr->proportion_female[i].final_value_m =
441 pop->proportion_female.get_force_scalar(i);
442 }
443 ss << " {\n\"name\": \"proportion_female\",\n";
444 ss << " \"id\":" << population_interface->proportion_female.id_m
445 << ",\n";
446 ss << " \"type\": \"vector\",\n";
447 ss << " \"dimensionality\": {\n";
448 ss << " \"header\": [" << "\"n_ages\"" << "],\n";
449 ss << " \"dimensions\": ["
450 << population_interface->proportion_female.size() << "]\n},\n";
451
452 ss << " \"values\":" << population_interface->proportion_female << "\n";
453 ss << "}],\n";
454
455 ss << " \"derived_quantities\": [\n";
456
457 std::map<std::string, fims::Vector<double>> dqs =
458 model_ptr->GetPopulationDerivedQuantities(
459 population_interface->get_id());
460
461 std::map<std::string, fims_popdy::DimensionInfo> dim_info =
462 model_ptr->GetPopulationDimensionInfo(population_interface->get_id());
463 ss << this->derived_quantities_component_to_json(dqs, dim_info)
464 << " ]}\n";
465 } else {
466 ss << "{\n";
467 ss << " \"name\": \"Population\",\n";
468
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
474 << ",\n";
475 ss << " \"growth_id\": " << population_interface->growth_id << ",\n";
476 ss << " \"maturity_id\": " << population_interface->maturity_id << ",\n";
477 ss << " \"derived_quantities\": []}\n";
478 }
479
480 return ss.str();
481 }
482
489 std::map<std::string, fims::Vector<double>>::iterator it,
490 const fims_popdy::DimensionInfo &dim_info) {
491 std::stringstream ss;
492 fims::Vector<double> &dq = (*it).second;
493 std::stringstream dim_entry;
494 // gather dimension information
495 switch (dim_info.ndims) {
496 case 1:
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];
503 }
504 dim_entry << "]\n";
505 dim_entry << "}";
506 break;
507 case 2:
508 dim_entry << "\"dimensionality\": {\n";
509 dim_entry << " \"header\": [\"" << dim_info.dim_names[0] << "\", \""
510 << dim_info.dim_names[1] << "\"],\n";
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];
515 }
516 dim_entry << "]\n";
517 dim_entry << "}";
518 break;
519 case 3:
520 dim_entry << "\"dimensionality\": {\n";
521 dim_entry << " \"header\": [\"" << dim_info.dim_names[0] << "\", \""
522 << dim_info.dim_names[1] << "\", \"" << dim_info.dim_names[2]
523 << "\"],\n";
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];
528 }
529 dim_entry << "]\n";
530 dim_entry << "}";
531 break;
532 default:
533 dim_entry << "\"dimensionality\": {\n";
534 dim_entry << " \"header\": [],\n";
535 dim_entry << " \"dimensions\": []\n";
536 dim_entry << "}";
537 break;
538 }
539
540 // build JSON string
541 ss << "{\n";
542 ss << "\"name\":\"" << (*it).first << "\",\n";
543 ss << dim_entry.str() << ",\n";
544 ss << "\"value\":[";
545 ss << std::fixed << std::setprecision(10);
546 if (dq.size() > 0) {
547 for (size_t i = 0; i < dq.size() - 1; i++) {
548 if (dq[i] != dq[i]) // check for NaN
549 {
550 ss << "-999" << ", ";
551 } else {
552 ss << dq[i] << ", ";
553 }
554 }
555 if (dq[dq.size() - 1] != dq[dq.size() - 1]) // check for NaN
556 {
557 ss << "-999]" << "\n";
558 } else {
559 ss << dq[dq.size() - 1] << "]\n";
560 }
561 } else {
562 ss << "]\n";
563 }
564 ss << "}";
565
566 return ss.str();
567 }
568
574 std::map<std::string, fims::Vector<double>> &dqs,
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;
580 end_it = dqs.end();
581 typename std::map<std::string, fims::Vector<double>>::iterator
582 second_to_last;
583 second_to_last = dqs.end();
584 if (it != end_it) {
585 second_to_last--;
586 }
587
588 it = dqs.begin();
589 for (; it != second_to_last; ++it) {
590 dim_info_it = dim_info.find(it->first);
591 ss << this->derived_quantity_to_json(it, dim_info_it->second) << ",\n";
592 }
593
594 dim_info_it = dim_info.find(second_to_last->first);
595 if (dim_info_it != dim_info.end()) {
596 ss << this->derived_quantity_to_json(second_to_last, dim_info_it->second)
597 << "\n";
598 } else {
599 ss << "{}";
600 // Handle case where dimension info is not found
601 }
602 return ss.str();
603 }
604
608 std::string fleet_to_json(FleetInterface *fleet_interface) {
609 std::stringstream ss;
610
611 if (!fleet_interface) {
613 "Fleet pointer is null; cannot get id. Not found in live objects.");
614 return "{}"; // Return empty JSON
615 }
616
617 std::shared_ptr<fims_info::Information<double>> info =
619
621 model_it = info->models_map.find(this->get_id());
622 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
623 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
624 (*model_it).second);
625
627
628 fit = info->fleets.find(fleet_interface->get_id());
629
630 if (fit != info->fleets.end()) {
631 std::shared_ptr<fims_popdy::Fleet<double>> &fleet = (*fit).second;
632
633 ss << "{\n";
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";
641 ss << "{\"agecomp\": " << fleet_interface->GetObservedAgeCompDataID()
642 << "},\n";
643 ss << "{\"lengthcomp\": "
644 << fleet_interface->GetObservedLengthCompDataID() << "},\n";
645 ss << "{\"index\": " << fleet_interface->GetObservedIndexDataID()
646 << "},\n";
647 ss << "{\"catch\": " << fleet_interface->GetObservedCatchDataID()
648 << "}\n";
649 ss << "],\n";
650 ss << "\"parameters\": [\n";
651 ss << "{\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];
654 }
655
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()
662 << "]\n},\n";
663 ss << " \"values\": " << fleet_interface->log_Fmort << "},\n";
664
665 ss << " {\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];
668 }
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";
675
676 ss << " \"values\": " << fleet_interface->log_q << "}\n";
677
678 ss << "], \"derived_quantities\": [";
679
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());
684 ss << this->derived_quantities_component_to_json(dqs, dim_info) << "]}\n";
685 } else {
686 ss << "{\n";
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";
692 }
693 return ss.str();
694 }
695
699 virtual std::string to_json() {
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;
705 // gather sub-module info from population and fleets
706 typename std::set<uint32_t>::iterator module_id_it; // generic
707 typename std::set<uint32_t>::iterator pit;
708 typename std::set<uint32_t>::iterator fids;
709 for (pit = this->population_ids->begin();
710 pit != this->population_ids->end(); pit++) {
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());
718
719 for (fids = population_interface->fleet_ids->begin();
720 fids != population_interface->fleet_ids->end(); fids++) {
721 fleet_ids.insert(*fids);
722 }
723 }
724 }
725
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());
732 }
733 }
734
735 std::shared_ptr<fims_info::Information<double>> info =
737
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()]);
741
742 std::shared_ptr<fims_model::Model<double>> model_internal =
744
745#ifdef TMB_MODEL
746 model->do_reporting = false;
747#endif
748
749 double value = model_internal->Evaluate();
750
751 std::stringstream ss;
752
753 ss.str("");
754
755 ss << "{\n";
756 ss << " \"name\": \"CatchAtAge\",\n";
757 ss << " \"type\": \"model\",\n";
758 ss << " \"estimation_framework\": ";
759#ifdef TMB_MODEL
760 ss << "\"Template_Model_Builder (TMB)\",";
761#else
762 ss << "\"FIMS\",";
763#endif
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();
768 module_id_it++) {
769 std::shared_ptr<GrowthInterfaceBase> growth_interface =
771
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()) {
776 ss << ", ";
777 }
778 }
779 }
780
781 ss << "],\n";
782
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()) {
792 ss << ", ";
793 }
794 }
795 }
796 ss << "],\n";
797
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()) {
807 ss << ", ";
808 }
809 }
810 }
811 ss << "],\n";
812
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()) {
822 ss << ", ";
823 }
824 }
825 }
826 ss << "],\n";
827
828 ss << " \"population_ids\": [";
829 for (pit = this->population_ids->begin();
830 pit != this->population_ids->end(); pit++) {
831 ss << *pit;
832 if (std::next(pit) != this->population_ids->end()) {
833 ss << ", ";
834 }
835 }
836 ss << "],\n";
837 ss << " \"fleet_ids\": [";
838
839 for (fids = fleet_ids.begin(); fids != fleet_ids.end(); fids++) {
840 ss << *fids;
841 if (std::next(fids) != fleet_ids.end()) {
842 ss << ", ";
843 }
844 }
845 ss << "],\n";
846 ss << "\"populations\": [\n";
847 typename std::set<uint32_t>::iterator pop_it;
848 typename std::set<uint32_t>::iterator pop_end_it;
849 pop_end_it = this->population_ids->end();
850 typename std::set<uint32_t>::iterator pop_second_to_last_it;
851 if (pop_end_it != this->population_ids->begin()) {
852 pop_second_to_last_it = std::prev(pop_end_it);
853 } else {
854 pop_second_to_last_it = pop_end_it;
855 }
856 for (pop_it = this->population_ids->begin();
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);
866 }
867 population_interface->finalize();
868 ss << this->population_to_json(population_interface.get()) << ",";
869 } else {
870 FIMS_ERROR_LOG("Population with id " + fims::to_string(*pop_it) +
871 " not found in live objects.");
872 ss << "{}"; // Return empty JSON for this population
873 }
874 }
875
876 std::shared_ptr<PopulationInterface> population_interface =
877 std::dynamic_pointer_cast<PopulationInterface>(
878 PopulationInterfaceBase::live_objects[*pop_second_to_last_it]);
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);
884 }
885 ss << this->population_to_json(population_interface.get());
886 } else {
887 FIMS_ERROR_LOG("Population with id " + fims::to_string(*pop_it) +
888 " not found in live objects.");
889 ss << "{}"; // Return empty JSON for this population
890 }
891
892 ss << "]";
893 ss << ",\n";
894 ss << "\"fleets\": [\n";
895
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;
900
901 if (fleet_end_it != fleet_ids.begin()) {
902 fleet_second_to_last_it = std::prev(fleet_end_it);
903 }
904 for (fleet_it = fleet_ids.begin(); fleet_it != fleet_second_to_last_it;
905 fleet_it++) {
906 std::shared_ptr<FleetInterface> fleet_interface =
907 std::dynamic_pointer_cast<FleetInterface>(
909 if (fleet_interface) {
910 fleet_interface->finalize();
911 ss << this->fleet_to_json(fleet_interface.get()) << ",";
912 } else {
913 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*fleet_it) +
914 " not found in live objects.");
915 ss << "{}"; // Return empty JSON for this fleet
916 }
917 }
918 std::shared_ptr<FleetInterface> fleet_interface =
919 std::dynamic_pointer_cast<FleetInterface>(
920 FleetInterfaceBase::live_objects[*fleet_second_to_last_it]);
921 if (fleet_interface) {
922 ss << this->fleet_to_json(fleet_interface.get());
923 } else {
924 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*fleet_it) +
925 " not found in live objects.");
926 ss << "{}"; // Return empty JSON for this fleet
927 }
928
929 ss << "],\n";
930
931 ss << "\"density_components\": [\n";
932
933 typename std::map<
934 uint32_t, std::shared_ptr<DistributionsInterfaceBase>>::iterator dit;
936 dit != DistributionsInterfaceBase::live_objects.end(); ++dit) {
937 std::shared_ptr<DistributionsInterfaceBase> dist_interface =
938 (*dit).second;
939 if (dist_interface) {
940 dist_interface->finalize();
941 ss << dist_interface->to_json();
942 if (std::next(dit) != DistributionsInterfaceBase::live_objects.end()) {
943 ss << ",\n";
944 }
945 }
946 }
947 ss << "\n],\n";
948 ss << "\"data\": [\n";
949 typename std::map<uint32_t, std::shared_ptr<DataInterfaceBase>>::iterator
950 d_it;
951 for (d_it = DataInterfaceBase::live_objects.begin();
952 d_it != DataInterfaceBase::live_objects.end(); ++d_it) {
953 std::shared_ptr<DataInterfaceBase> data_interface = (*d_it).second;
954 if (data_interface) {
955 data_interface->finalize();
956 ss << data_interface->to_json();
957 if (std::next(d_it) != DataInterfaceBase::live_objects.end()) {
958 ss << ",\n";
959 }
960 }
961 }
962 ss << "\n]\n";
963 ss << "}\n";
964#ifdef TMB_MODEL
965 model->do_reporting = true;
966#endif
967 return fims::JsonParser::PrettyFormatJSON(ss.str());
968 }
969
970#ifdef TMB_MODEL
971
972 template <typename Type>
973 bool add_to_fims_tmb_internal() {
974 std::shared_ptr<fims_info::Information<Type>> info =
976
977 std::shared_ptr<fims_popdy::CatchAtAge<Type>> model =
978 std::make_shared<fims_popdy::CatchAtAge<Type>>();
979
981
982 for (it = this->population_ids->begin(); it != this->population_ids->end();
983 ++it) {
984 model->AddPopulation((*it));
985 }
986
987 std::set<uint32_t> fleet_ids; // all fleets in the model
988 typedef typename std::set<uint32_t>::iterator fleet_ids_iterator;
989
990 // add to Information
991 info->models_map[this->get_id()] = model;
992
993 for (it = this->population_ids->begin(); it != this->population_ids->end();
994 ++it) {
995 auto population_interface_it =
997 if (population_interface_it ==
999 FIMS_ERROR_LOG("Population with id " + fims::to_string(*it) +
1000 " not found in live objects.");
1001 continue;
1002 }
1003
1004 std::shared_ptr<PopulationInterface> population_interface =
1005 std::dynamic_pointer_cast<PopulationInterface>(
1006 population_interface_it->second);
1007 if (!population_interface) {
1008 FIMS_ERROR_LOG("Population with id " + fims::to_string(*it) +
1009 " not found in live objects.");
1010 continue;
1011 }
1012
1013 model->InitializePopulationDerivedQuantities(population_interface->id);
1014 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1015 model->GetPopulationDerivedQuantities(population_interface->id);
1016
1017 std::map<std::string, fims_popdy::DimensionInfo>
1018 &derived_quantities_dim_info =
1019 model->GetPopulationDimensionInfo(population_interface->id);
1020
1021 std::stringstream ss;
1022
1023 derived_quantities["total_catch_weight"] =
1024 fims::Vector<Type>(population_interface->n_years.get());
1025
1026 derived_quantities_dim_info["total_catch_weight"] =
1028 "total_catch_weight",
1029 fims::Vector<int>{(int)population_interface->n_years.get()},
1030 fims::Vector<std::string>{"n_years"});
1031 info->variable_map[population_interface->total_catch_weight.id_m] =
1032 &derived_quantities["total_catch_weight"];
1033
1034 derived_quantities["total_catch_numbers"] =
1035 fims::Vector<Type>(population_interface->n_years.get());
1036
1037 derived_quantities_dim_info["total_catch_numbers"] =
1039 "total_catch_numbers",
1040 fims::Vector<int>{population_interface->n_years.get()},
1041 fims::Vector<std::string>{"n_years"});
1042 info->variable_map[population_interface->total_catch_numbers.id_m] =
1043 &derived_quantities["total_catch_numbers"];
1044
1045 derived_quantities["mortality_F"] =
1046 fims::Vector<Type>(population_interface->n_years.get() *
1047 population_interface->n_ages.get());
1048 derived_quantities_dim_info["mortality_F"] = fims_popdy::DimensionInfo(
1049 "mortality_F",
1050 fims::Vector<int>{population_interface->n_years.get(),
1051 population_interface->n_ages.get()},
1052 fims::Vector<std::string>{"n_years", "n_ages"});
1053 info->variable_map[population_interface->mortality_F.id_m] =
1054 &derived_quantities["mortality_F"];
1055
1056 derived_quantities["mortality_M"] =
1057 fims::Vector<Type>(population_interface->n_years.get() *
1058 population_interface->n_ages.get());
1059 derived_quantities_dim_info["mortality_M"] = fims_popdy::DimensionInfo(
1060 "mortality_M",
1061 fims::Vector<int>{population_interface->n_years.get(),
1062 population_interface->n_ages.get()},
1063 fims::Vector<std::string>{"n_years", "n_ages"});
1064 info->variable_map[population_interface->mortality_M.id_m] =
1065 &derived_quantities["mortality_M"];
1066
1067 derived_quantities["mortality_Z"] =
1068 fims::Vector<Type>(population_interface->n_years.get() *
1069 population_interface->n_ages.get());
1070 derived_quantities_dim_info["mortality_Z"] = fims_popdy::DimensionInfo(
1071 "mortality_Z",
1072 fims::Vector<int>{population_interface->n_years.get(),
1073 population_interface->n_ages.get()},
1074 fims::Vector<std::string>{"n_years", "n_ages"});
1075 info->variable_map[population_interface->mortality_Z.id_m] =
1076 &derived_quantities["mortality_Z"];
1077
1078 derived_quantities["numbers_at_age"] =
1079 fims::Vector<Type>((population_interface->n_years.get() + 1) *
1080 population_interface->n_ages.get());
1081 derived_quantities_dim_info["numbers_at_age"] = fims_popdy::DimensionInfo(
1082 "numbers_at_age",
1083 fims::Vector<int>{(population_interface->n_years.get() + 1),
1084 population_interface->n_ages.get()},
1085 fims::Vector<std::string>{"n_years+1", "n_ages"});
1086 info->variable_map[population_interface->numbers_at_age.id_m] =
1087 &derived_quantities["numbers_at_age"];
1088
1089 derived_quantities["unfished_numbers_at_age"] =
1090 fims::Vector<Type>((population_interface->n_years.get() + 1) *
1091 population_interface->n_ages.get());
1092 derived_quantities_dim_info["unfished_numbers_at_age"] =
1094 "unfished_numbers_at_age",
1095 fims::Vector<int>{(population_interface->n_years.get() + 1),
1096 population_interface->n_ages.get()},
1097 fims::Vector<std::string>{"n_years+1", "n_ages"});
1098 info->variable_map[population_interface->unfished_numbers_at_age.id_m] =
1099 &derived_quantities["unfished_numbers_at_age"];
1100
1101 derived_quantities["biomass"] =
1102 fims::Vector<Type>((population_interface->n_years.get() + 1));
1103 derived_quantities_dim_info["biomass"] = fims_popdy::DimensionInfo(
1104 "biomass",
1105 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1106 fims::Vector<std::string>{"n_years+1"});
1107 info->variable_map[population_interface->biomass.id_m] =
1108 &derived_quantities["biomass"];
1109
1110 derived_quantities["spawning_biomass"] =
1111 fims::Vector<Type>((population_interface->n_years.get() + 1));
1112 derived_quantities_dim_info["spawning_biomass"] =
1114 "spawning_biomass",
1115 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1116 fims::Vector<std::string>{"n_years+1"});
1117 info->variable_map[population_interface->spawning_biomass.id_m] =
1118 &derived_quantities["spawning_biomass"];
1119
1120 derived_quantities["unfished_biomass"] =
1121 fims::Vector<Type>((population_interface->n_years.get() + 1));
1122 derived_quantities_dim_info["unfished_biomass"] =
1124 "unfished_biomass",
1125 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1126 fims::Vector<std::string>{"n_years+1"});
1127 info->variable_map[population_interface->unfished_biomass.id_m] =
1128 &derived_quantities["unfished_biomass"];
1129
1130 derived_quantities["unfished_spawning_biomass"] =
1131 fims::Vector<Type>((population_interface->n_years.get() + 1));
1132 derived_quantities_dim_info["unfished_spawning_biomass"] =
1134 "unfished_spawning_biomass",
1135 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1136 fims::Vector<std::string>{"n_years+1"});
1137 info->variable_map[population_interface->unfished_spawning_biomass.id_m] =
1138 &derived_quantities["unfished_spawning_biomass"];
1139
1140 derived_quantities["proportion_mature_at_age"] =
1141 fims::Vector<Type>((population_interface->n_years.get() + 1) *
1142 population_interface->n_ages.get());
1143 derived_quantities_dim_info["proportion_mature_at_age"] =
1145 "proportion_mature_at_age",
1146 fims::Vector<int>{(population_interface->n_years.get() + 1),
1147 population_interface->n_ages.get()},
1148 fims::Vector<std::string>{"n_years+1", "n_ages"});
1149 info->variable_map[population_interface->proportion_mature_at_age.id_m] =
1150 &derived_quantities["proportion_mature_at_age"];
1151
1152 derived_quantities["expected_recruitment"] =
1153 fims::Vector<Type>((population_interface->n_years.get() + 1));
1154 derived_quantities_dim_info["expected_recruitment"] =
1156 "expected_recruitment",
1157 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1158 fims::Vector<std::string>{"n_years+1"});
1159 info->variable_map[population_interface->expected_recruitment.id_m] =
1160 &derived_quantities["expected_recruitment"];
1161
1162 derived_quantities["sum_selectivity"] =
1163 fims::Vector<Type>(population_interface->n_years.get() *
1164 population_interface->n_ages.get());
1165 derived_quantities_dim_info["sum_selectivity"] =
1167 "sum_selectivity",
1168 fims::Vector<int>{population_interface->n_years.get(),
1169 population_interface->n_ages.get()},
1170 fims::Vector<std::string>{"n_years", "n_ages"});
1171 info->variable_map[population_interface->sum_selectivity.id_m] =
1172 &derived_quantities["sum_selectivity"];
1173
1174 // replace elements in the variable map
1175
1176 for (fleet_ids_iterator fit = population_interface->fleet_ids->begin();
1177 fit != population_interface->fleet_ids->end(); ++fit) {
1178 fleet_ids.insert(*fit);
1179 }
1180 }
1181
1182 for (fleet_ids_iterator it = fleet_ids.begin(); it != fleet_ids.end();
1183 ++it) {
1184 auto fleet_interface_it = FleetInterfaceBase::live_objects.find(*it);
1185 if (fleet_interface_it == FleetInterfaceBase::live_objects.end()) {
1186 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*it) +
1187 " not found in live objects.");
1188 continue;
1189 }
1190
1191 std::shared_ptr<FleetInterface> fleet_interface =
1192 std::dynamic_pointer_cast<FleetInterface>(fleet_interface_it->second);
1193 if (!fleet_interface) {
1194 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*it) +
1195 " not found in live objects.");
1196 continue;
1197 }
1198 model->InitializeFleetDerivedQuantities(fleet_interface->id);
1199 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1200 model->GetFleetDerivedQuantities(fleet_interface->id);
1201
1202 std::map<std::string, fims_popdy::DimensionInfo>
1203 &derived_quantities_dim_info =
1204 model->GetFleetDimensionInfo(fleet_interface->id);
1205
1206 // initialize derive quantities
1207 // catch
1208 derived_quantities["catch_numbers_at_age"] = fims::Vector<Type>(
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",
1213 fims::Vector<int>{(fleet_interface->n_years.get()),
1214 fleet_interface->n_ages.get()},
1215 fims::Vector<std::string>{"n_years", "n_ages"});
1216 info->variable_map[fleet_interface->catch_numbers_at_age.id_m] =
1217 &derived_quantities["catch_numbers_at_age"];
1218
1219 derived_quantities["catch_weight_at_age"] = fims::Vector<Type>(
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",
1224 fims::Vector<int>{(fleet_interface->n_years.get()),
1225 fleet_interface->n_ages.get()},
1226 fims::Vector<std::string>{"n_years", "n_ages"});
1227 info->variable_map[fleet_interface->catch_weight_at_age.id_m] =
1228 &derived_quantities["catch_weight_at_age"];
1229
1230 derived_quantities["catch_numbers_at_length"] = fims::Vector<Type>(
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",
1235 fims::Vector<int>{(fleet_interface->n_years.get()),
1236 fleet_interface->n_lengths.get()},
1237 fims::Vector<std::string>{"n_years", "n_lengths"});
1238 info->variable_map[fleet_interface->catch_numbers_at_length.id_m] =
1239 &derived_quantities["catch_numbers_at_length"];
1240
1241 derived_quantities["catch_weight"] =
1242 fims::Vector<Type>(fleet_interface->n_years.get());
1243 derived_quantities_dim_info["catch_weight"] = fims_popdy::DimensionInfo(
1244 "catch_weight", fims::Vector<int>{(fleet_interface->n_years.get())},
1245 fims::Vector<std::string>{"n_years"});
1246 info->variable_map[fleet_interface->catch_weight.id_m] =
1247 &derived_quantities["catch_weight"];
1248
1249 derived_quantities["catch_numbers"] =
1250 fims::Vector<Type>(fleet_interface->n_years.get());
1251 derived_quantities_dim_info["catch_numbers"] = fims_popdy::DimensionInfo(
1252 "catch_numbers", fims::Vector<int>{(fleet_interface->n_years.get())},
1253 fims::Vector<std::string>{"n_years"});
1254 info->variable_map[fleet_interface->catch_numbers.id_m] =
1255 &derived_quantities["catch_numbers"];
1256
1257 derived_quantities["catch_expected"] =
1258 fims::Vector<Type>(fleet_interface->n_years.get());
1259 derived_quantities_dim_info["catch_expected"] = fims_popdy::DimensionInfo(
1260 "catch_expected", fims::Vector<int>{(fleet_interface->n_years.get())},
1261 fims::Vector<std::string>{"n_years"});
1262 info->variable_map[fleet_interface->catch_expected.id_m] =
1263 &derived_quantities["catch_expected"];
1264
1265 derived_quantities["log_catch_expected"] =
1266 fims::Vector<Type>(fleet_interface->n_years.get());
1267 derived_quantities_dim_info["log_catch_expected"] =
1269 "log_catch_expected",
1270 fims::Vector<int>{(fleet_interface->n_years.get())},
1271 fims::Vector<std::string>{"n_years"});
1272 info->variable_map[fleet_interface->log_catch_expected.id_m] =
1273 &derived_quantities["log_catch_expected"];
1274
1275 derived_quantities["agecomp_proportion"] = fims::Vector<Type>(
1276 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1277 derived_quantities_dim_info["agecomp_proportion"] =
1279 "agecomp_proportion",
1280 fims::Vector<int>{(fleet_interface->n_years.get()),
1281 fleet_interface->n_ages.get()},
1282 fims::Vector<std::string>{"n_years", "n_ages"});
1283 info->variable_map[fleet_interface->agecomp_proportion.id_m] =
1284 &derived_quantities["agecomp_proportion"];
1285
1286 derived_quantities["lengthcomp_proportion"] = fims::Vector<Type>(
1287 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1288 derived_quantities_dim_info["lengthcomp_proportion"] =
1290 "lengthcomp_proportion",
1291 fims::Vector<int>{(fleet_interface->n_years.get()),
1292 fleet_interface->n_lengths.get()},
1293 fims::Vector<std::string>{"n_years", "n_lengths"});
1294 info->variable_map[fleet_interface->lengthcomp_proportion.id_m] =
1295 &derived_quantities["lengthcomp_proportion"];
1296
1297 // index
1298 derived_quantities["index_numbers_at_age"] = fims::Vector<Type>(
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",
1303 fims::Vector<int>{(fleet_interface->n_years.get()),
1304 fleet_interface->n_ages.get()},
1305 fims::Vector<std::string>{"n_years", "n_ages"});
1306 info->variable_map[fleet_interface->index_numbers_at_age.id_m] =
1307 &derived_quantities["index_numbers_at_age"];
1308
1309 derived_quantities["index_weight_at_age"] = fims::Vector<Type>(
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",
1314 fims::Vector<int>{(fleet_interface->n_years.get()),
1315 fleet_interface->n_ages.get()},
1316 fims::Vector<std::string>{"n_years", "n_ages"});
1317 info->variable_map[fleet_interface->index_weight_at_age.id_m] =
1318 &derived_quantities["index_weight_at_age"];
1319
1320 derived_quantities["index_numbers_at_length"] = fims::Vector<Type>(
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",
1325 fims::Vector<int>{(fleet_interface->n_years.get()),
1326 fleet_interface->n_lengths.get()},
1327 fims::Vector<std::string>{"n_years", "n_lengths"});
1328 info->variable_map[fleet_interface->index_numbers_at_length.id_m] =
1329 &derived_quantities["index_numbers_at_length"];
1330
1331 derived_quantities["index_weight"] =
1332 fims::Vector<Type>(fleet_interface->n_years.get());
1333 derived_quantities_dim_info["index_weight"] = fims_popdy::DimensionInfo(
1334 "index_weight", fims::Vector<int>{(fleet_interface->n_years.get())},
1335 fims::Vector<std::string>{"n_years"});
1336 info->variable_map[fleet_interface->index_weight.id_m] =
1337 &derived_quantities["index_weight"];
1338
1339 derived_quantities["index_numbers"] =
1340 fims::Vector<Type>(fleet_interface->n_years.get());
1341 derived_quantities_dim_info["index_numbers"] = fims_popdy::DimensionInfo(
1342 "index_numbers", fims::Vector<int>{(fleet_interface->n_years.get())},
1343 fims::Vector<std::string>{"n_years"});
1344 info->variable_map[fleet_interface->index_numbers.id_m] =
1345 &derived_quantities["index_numbers"];
1346
1347 derived_quantities["index_expected"] =
1348 fims::Vector<Type>(fleet_interface->n_years.get());
1349 derived_quantities_dim_info["index_expected"] = fims_popdy::DimensionInfo(
1350 "index_expected", fims::Vector<int>{(fleet_interface->n_years.get())},
1351 fims::Vector<std::string>{"n_years"});
1352 info->variable_map[fleet_interface->index_expected.id_m] =
1353 &derived_quantities["index_expected"];
1354
1355 derived_quantities["log_index_expected"] =
1356 fims::Vector<Type>(fleet_interface->n_years.get());
1357 derived_quantities_dim_info["log_index_expected"] =
1359 "log_index_expected",
1360 fims::Vector<int>{(fleet_interface->n_years.get())},
1361 fims::Vector<std::string>{"n_years"});
1362 info->variable_map[fleet_interface->log_index_expected.id_m] =
1363 &derived_quantities["log_index_expected"];
1364
1365 derived_quantities["agecomp_expected"] = fims::Vector<Type>(
1366 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1367 derived_quantities_dim_info["agecomp_expected"] =
1369 "agecomp_expected",
1370 fims::Vector<int>{(fleet_interface->n_years.get()),
1371 (fleet_interface->n_ages.get())},
1372 fims::Vector<std::string>{"n_years", "n_ages"});
1373 info->variable_map[fleet_interface->agecomp_expected.id_m] =
1374 &derived_quantities["agecomp_expected"];
1375
1376 derived_quantities["lengthcomp_expected"] = fims::Vector<Type>(
1377 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1378 derived_quantities_dim_info["lengthcomp_expected"] =
1380 "lengthcomp_expected",
1381 fims::Vector<int>{(fleet_interface->n_years.get()),
1382 (fleet_interface->n_lengths.get())},
1383 fims::Vector<std::string>{"n_years", "n_lengths"});
1384 info->variable_map[fleet_interface->lengthcomp_expected.id_m] =
1385 &derived_quantities["lengthcomp_expected"];
1386 }
1387
1388 return true;
1389 }
1390
1391 virtual bool add_to_fims_tmb() {
1392 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
1393 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
1394 return true;
1395 }
1396
1397#endif
1398};
1399
1400#endif
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
std::unordered_map< uint32_t, std::shared_ptr< fims_popdy::FisheryModelBase< Type > > > models_map
Definition information.hpp:139
std::map< uint32_t, std::shared_ptr< fims_popdy::Fleet< Type > > >::iterator fleet_iterator
Definition information.hpp:113
std::map< uint32_t, std::shared_ptr< fims_popdy::Population< Type > > >::iterator population_iterator
Definition information.hpp:123
std::unordered_map< uint32_t, std::shared_ptr< fims_popdy::FisheryModelBase< Type > > >::iterator model_map_iterator
Definition information.hpp:143
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:269
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