FIMS  v0.11.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"
16#include "common/model.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
68 this->id = FisheryModelInterfaceBase::id_g++;
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\": \""
370 << fims::JsonParser::EscapeString(population_interface->name.get())
371 << "\",\n";
372 ss << " \"module_id\": " << population_interface->id << ",\n";
373 ss << " \"recruitment_id\": " << population_interface->recruitment_id
374 << ",\n";
375 ss << " \"growth_id\": " << population_interface->growth_id << ",\n";
376 ss << " \"maturity_id\": " << population_interface->maturity_id << ",\n";
377 // Dimensions are written as positions (age_i), so the ages are written
378 // too for output to be labeled with the ages they represent. A separate
379 // stream keeps the precision of derived quantities without changing how
380 // the parameters below are written.
381 std::stringstream age;
382 age << std::fixed << std::setprecision(10);
383 for (int i = 0; i < population_interface->n_ages.get(); i++) {
384 if (i > 0) age << ", ";
385 age << sanitize_val(population_interface->ages[i]);
386 }
387 ss << " \"age\": [" << age.str() << "],\n";
388
389 ss << " \"parameters\": [\n";
390 for (size_t i = 0; i < pop->log_M.size(); i++) {
391 population_interface_ptr->log_M[i].final_value_m = pop->log_M[i];
392 }
393
394 ss << "{\n \"name\": \"log_M\",\n";
395 ss << " \"id\":" << population_interface->log_M.id_m << ",\n";
396 ss << " \"type\": \"vector\",\n";
397 ss << " \"dimensionality\": {\n";
398 ss << " \"header\": [" << "\"n_years\", \"n_ages\"" << "],\n";
399 ss << " \"dimensions\": [" << population_interface->n_years.get() << ", "
400 << population_interface->n_ages.get() << "]\n},\n";
401 ss << " \"values\": " << population_interface->log_M << "\n\n";
402 ss << "},\n";
403
404 for (size_t i = 0; i < pop->log_f_multiplier.size(); i++) {
405 population_interface_ptr->log_f_multiplier[i].final_value_m =
406 pop->log_f_multiplier[i];
407 }
408
409 ss << "{\n \"name\": \"log_f_multiplier\",\n";
410 ss << " \"id\":" << population_interface->log_f_multiplier.id_m << ",\n";
411 ss << " \"type\": \"vector\",\n";
412 ss << " \"dimensionality\": {\n";
413 ss << " \"header\": [" << "\"n_years\"" << "],\n";
414 ss << " \"dimensions\": [" << population_interface->n_years.get()
415 << "]\n},\n";
416 ss << " \"values\": " << population_interface->log_f_multiplier << "\n\n";
417 ss << "},\n";
418
419 for (size_t i = 0; i < pop->spawning_biomass_ratio.size(); i++) {
420 population_interface_ptr->spawning_biomass_ratio[i].final_value_m =
421 pop->spawning_biomass_ratio[i];
422 }
423
424 ss << "{\n \"name\": \"spawning_biomass_ratio\",\n";
425 ss << " \"id\":" << population_interface->spawning_biomass_ratio.id_m
426 << ",\n";
427 ss << " \"type\": \"vector\",\n";
428 ss << " \"dimensionality\": {\n";
429 ss << " \"header\": [" << "\"n_years\"" << "],\n";
430 ss << " \"dimensions\": [" << (population_interface->n_years.get() + 1)
431 << "]\n},\n";
432 ss << " \"values\": " << population_interface->spawning_biomass_ratio
433 << "\n\n";
434 ss << "},\n";
435
436 for (size_t i = 0; i < pop->log_init_naa.size(); i++) {
437 population_interface_ptr->log_init_naa[i].final_value_m =
438 pop->log_init_naa[i];
439 }
440 ss << " {\n\"name\": \"log_init_naa\",\n";
441 ss << " \"id\":" << population_interface->log_init_naa.id_m << ",\n";
442 ss << " \"type\": \"vector\",\n";
443 ss << " \"dimensionality\": {\n";
444 ss << " \"header\": [" << "\"n_ages\"" << "],\n";
445 ss << " \"dimensions\": [" << population_interface->n_ages.get()
446 << "]\n},\n";
447
448 ss << " \"values\":" << population_interface->log_init_naa << "\n";
449 ss << "},\n";
450
451 for (size_t i = 0; i < population_interface->proportion_female.size();
452 i++) {
453 population_interface_ptr->proportion_female[i].final_value_m =
454 pop->proportion_female.get_force_scalar(i);
455 }
456 ss << " {\n\"name\": \"proportion_female\",\n";
457 ss << " \"id\":" << population_interface->proportion_female.id_m
458 << ",\n";
459 ss << " \"type\": \"vector\",\n";
460 ss << " \"dimensionality\": {\n";
461 ss << " \"header\": [" << "\"n_ages\"" << "],\n";
462 ss << " \"dimensions\": ["
463 << population_interface->proportion_female.size() << "]\n},\n";
464
465 ss << " \"values\":" << population_interface->proportion_female << "\n";
466 ss << "}],\n";
467
468 ss << " \"derived_quantities\": [\n";
469
470 std::map<std::string, fims::Vector<double>> dqs =
471 model_ptr->GetPopulationDerivedQuantities(
472 population_interface->get_id());
473
474 std::map<std::string, fims_popdy::DimensionInfo> dim_info =
475 model_ptr->GetPopulationDimensionInfo(population_interface->get_id());
476 ss << this->derived_quantities_component_to_json(dqs, dim_info)
477 << " ]}\n";
478 } else {
479 ss << "{\n";
480 ss << " \"name\": \"Population\",\n";
481
482 ss << " \"type\": \"population\",\n";
483 ss << " \"tag\": \"" << population_interface->get_id()
484 << " not found in Information.\",\n";
485 ss << " \"id\": " << population_interface->get_id() << ",\n";
486 ss << " \"recruitment_id\": " << population_interface->recruitment_id
487 << ",\n";
488 ss << " \"growth_id\": " << population_interface->growth_id << ",\n";
489 ss << " \"maturity_id\": " << population_interface->maturity_id << ",\n";
490 ss << " \"derived_quantities\": []}\n";
491 }
492
493 return ss.str();
494 }
495
502 std::map<std::string, fims::Vector<double>>::iterator it,
503 const fims_popdy::DimensionInfo &dim_info) {
504 std::stringstream ss;
505 fims::Vector<double> &dq = (*it).second;
506 std::stringstream dim_entry;
507 // gather dimension information
508 switch (dim_info.ndims) {
509 case 1:
510 dim_entry << "\"dimensionality\": {\n";
511 dim_entry << " \"header\": [\"" << dim_info.dim_names[0] << "\"],\n";
512 dim_entry << " \"dimensions\": [";
513 for (size_t i = 0; i < dim_info.dims.size(); ++i) {
514 if (i > 0) dim_entry << ", ";
515 dim_entry << dim_info.dims[i];
516 }
517 dim_entry << "]\n";
518 dim_entry << "}";
519 break;
520 case 2:
521 dim_entry << "\"dimensionality\": {\n";
522 dim_entry << " \"header\": [\"" << dim_info.dim_names[0] << "\", \""
523 << dim_info.dim_names[1] << "\"],\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 case 3:
533 dim_entry << "\"dimensionality\": {\n";
534 dim_entry << " \"header\": [\"" << dim_info.dim_names[0] << "\", \""
535 << dim_info.dim_names[1] << "\", \"" << dim_info.dim_names[2]
536 << "\"],\n";
537 dim_entry << " \"dimensions\": [";
538 for (size_t i = 0; i < dim_info.dims.size(); ++i) {
539 if (i > 0) dim_entry << ", ";
540 dim_entry << dim_info.dims[i];
541 }
542 dim_entry << "]\n";
543 dim_entry << "}";
544 break;
545 default:
546 dim_entry << "\"dimensionality\": {\n";
547 dim_entry << " \"header\": [],\n";
548 dim_entry << " \"dimensions\": []\n";
549 dim_entry << "}";
550 break;
551 }
552
553 // build JSON string
554 ss << "{\n";
555 ss << "\"name\":\"" << (*it).first << "\",\n";
556 ss << dim_entry.str() << ",\n";
557 ss << "\"value\":[";
558 ss << std::fixed << std::setprecision(10);
559 if (dq.size() > 0) {
560 // JSON has no NaN or Inf, e.g., log(0) for an expected catch of zero, so
561 // non-finite values are written as -999 like missing values elsewhere
562 for (size_t i = 0; i < dq.size() - 1; i++) {
563 ss << sanitize_val(dq[i]) << ", ";
564 }
565 ss << sanitize_val(dq[dq.size() - 1]) << "]\n";
566 } else {
567 ss << "]\n";
568 }
569 ss << "}";
570
571 return ss.str();
572 }
573
579 std::map<std::string, fims::Vector<double>> &dqs,
580 std::map<std::string, fims_popdy::DimensionInfo> &dim_info) {
581 std::stringstream ss;
582 // Stepping back from end() of an empty map is undefined, and there is no
583 // last element to write without a trailing comma
584 if (dqs.empty()) {
585 return ss.str();
586 }
587 std::map<std::string, fims_popdy::DimensionInfo>::iterator dim_info_it;
588 std::map<std::string, fims::Vector<double>>::iterator it;
589 typename std::map<std::string, fims::Vector<double>>::iterator
590 second_to_last = dqs.end();
591 second_to_last--;
592
593 it = dqs.begin();
594 for (; it != second_to_last; ++it) {
595 dim_info_it = dim_info.find(it->first);
596 ss << this->derived_quantity_to_json(it, dim_info_it->second) << ",\n";
597 }
598
599 dim_info_it = dim_info.find(second_to_last->first);
600 if (dim_info_it != dim_info.end()) {
601 ss << this->derived_quantity_to_json(second_to_last, dim_info_it->second)
602 << "\n";
603 } else {
604 ss << "{}";
605 // Handle case where dimension info is not found
606 }
607 return ss.str();
608 }
609
613 std::string fleet_to_json(FleetInterface *fleet_interface) {
614 std::stringstream ss;
615
616 if (!fleet_interface) {
618 "Fleet pointer is null; cannot get id. Not found in live objects.");
619 return "{}"; // Return empty JSON
620 }
621
622 std::shared_ptr<fims_info::Information<double>> info =
624
626 model_it = info->models_map.find(this->get_id());
627 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
628 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
629 (*model_it).second);
630
632
633 fit = info->fleets.find(fleet_interface->get_id());
634
635 if (fit != info->fleets.end()) {
636 std::shared_ptr<fims_popdy::Fleet<double>> &fleet = (*fit).second;
637
638 ss << "{\n";
639 ss << " \"module_name\": \"Fleet\",\n";
640 ss << " \"fleet\": \""
641 << fims::JsonParser::EscapeString(fleet_interface->name.get())
642 << "\",\n";
643 ss << " \"module_id\": " << fleet_interface->id << ",\n";
644 // Selectivity is written in its own section, so this is what links a
645 // selectivity module to its fleet; an unset id is written as -999
646 ss << " \"selectivity_id\": "
647 << static_cast<int>(fleet_interface->GetSelectivityID()) << ",\n";
648 ss << " \"n_ages\": " << fleet_interface->n_ages.get() << ",\n";
649 ss << " \"n_years\": " << fleet_interface->n_years.get() << ",\n";
650 ss << " \"n_lengths\": " << fleet_interface->n_lengths.get() << ",\n";
651 // The length bins that length_i indexes, written like the age in
652 // population_to_json(); length holds a placeholder value when
653 // n_lengths is 0, so only n_lengths values are written
654 std::stringstream length;
655 length << std::fixed << std::setprecision(10);
656 for (int i = 0; i < fleet_interface->n_lengths.get(); i++) {
657 if (i > 0) length << ", ";
658 length << sanitize_val(fleet_interface->lengths[i]);
659 }
660 ss << " \"length\": [" << length.str() << "],\n";
661 ss << "\"data_ids\" : [\n";
662 ss << "{\"agecomp\": " << fleet_interface->GetObservedAgeCompDataID()
663 << "},\n";
664 ss << "{\"lengthcomp\": "
665 << fleet_interface->GetObservedLengthCompDataID() << "},\n";
666 ss << "{\"index\": " << fleet_interface->GetObservedIndexDataID()
667 << "},\n";
668 ss << "{\"catch\": " << fleet_interface->GetObservedCatchDataID()
669 << "}\n";
670 ss << "],\n";
671 ss << "\"parameters\": [\n";
672 ss << "{\n";
673 for (size_t i = 0; i < fleet_interface->log_Fmort.size(); i++) {
674 fleet_interface->log_Fmort[i].final_value_m = fleet->log_Fmort[i];
675 }
676
677 ss << " \"name\": \"log_Fmort\",\n";
678 ss << " \"id\":" << fleet_interface->log_Fmort.id_m << ",\n";
679 ss << " \"type\": \"vector\",\n";
680 ss << " \"dimensionality\": {\n";
681 ss << " \"header\": [\"" << "n_years" << "\"],\n";
682 ss << " \"dimensions\": [" << fleet_interface->n_years.get()
683 << "]\n},\n";
684 ss << " \"values\": " << fleet_interface->log_Fmort << "},\n";
685
686 ss << " {\n";
687 for (size_t i = 0; i < fleet->log_q.size(); i++) {
688 fleet_interface->log_q[i].final_value_m = fleet->log_q[i];
689 }
690 ss << " \"name\": \"log_q\",\n";
691 ss << " \"id\":" << fleet_interface->log_q.id_m << ",\n";
692 ss << " \"type\": \"vector\",\n";
693 ss << " \"dimensionality\": {\n";
694 // With 1 value per year, log_q is labeled by year like log_Fmort, so
695 // get_estimates() can report the year of each value
696 if (fleet->log_q.size() > 1) {
697 ss << " \"header\": [\"n_years\"],\n";
698 } else {
699 ss << " \"header\": [\"na\"],\n";
700 }
701 ss << " \"dimensions\": [" << fleet->log_q.size() << "]\n},\n";
702
703 ss << " \"values\": " << fleet_interface->log_q << "}\n";
704
705 ss << "], \"derived_quantities\": [";
706
707 std::map<std::string, fims::Vector<double>> dqs =
708 model_ptr->GetFleetDerivedQuantities(fleet_interface->get_id());
709 std::map<std::string, fims_popdy::DimensionInfo> dim_info =
710 model_ptr->GetFleetDimensionInfo(fleet_interface->get_id());
711 ss << this->derived_quantities_component_to_json(dqs, dim_info) << "]}\n";
712 } else {
713 ss << "{\n";
714 ss << " \"name\": \"Fleet\",\n";
715 ss << " \"type\": \"fleet\",\n";
716 ss << " \"tag\": \"" << fleet_interface->get_id()
717 << " not found in Information.\",\n";
718 ss << " \"derived_quantities\": []}\n";
719 }
720 return ss.str();
721 }
722
726 virtual std::string to_json() {
727 std::set<uint32_t> recruitment_ids;
728 std::set<uint32_t> growth_ids;
729 std::set<uint32_t> maturity_ids;
730 std::set<uint32_t> selectivity_ids;
731 std::set<uint32_t> fleet_ids;
732 // gather sub-module info from population and fleets
733 typename std::set<uint32_t>::iterator module_id_it; // generic
734 typename std::set<uint32_t>::iterator pit;
735 typename std::set<uint32_t>::iterator fids;
736 for (pit = this->population_ids->begin();
737 pit != this->population_ids->end(); pit++) {
738 std::shared_ptr<PopulationInterface> population_interface =
739 std::dynamic_pointer_cast<PopulationInterface>(
741 if (population_interface) {
742 recruitment_ids.insert(population_interface->recruitment_id.get());
743 growth_ids.insert(population_interface->growth_id.get());
744 maturity_ids.insert(population_interface->maturity_id.get());
745
746 for (fids = population_interface->fleet_ids->begin();
747 fids != population_interface->fleet_ids->end(); fids++) {
748 fleet_ids.insert(*fids);
749 }
750 }
751 }
752
753 for (fids = fleet_ids.begin(); fids != fleet_ids.end(); fids++) {
754 std::shared_ptr<FleetInterface> fleet_interface =
755 std::dynamic_pointer_cast<FleetInterface>(
757 if (fleet_interface) {
758 selectivity_ids.insert(fleet_interface->GetSelectivityID());
759 }
760 }
761
762 std::shared_ptr<fims_info::Information<double>> info =
764
765 std::shared_ptr<fims_popdy::CatchAtAge<double>> model =
766 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
767 info->models_map[this->get_id()]);
768
769 std::shared_ptr<fims_model::Model<double>> model_internal =
770 fims_model::Model<double>::GetInstance();
771
772#ifdef TMB_MODEL
773 model->do_reporting = false;
774#endif
775
776 double value = model_internal->Evaluate();
777
778 std::stringstream ss;
779
780 ss.str("");
781
782 ss << "{\n";
783 ss << " \"name\": \"CatchAtAge\",\n";
784 ss << " \"type\": \"model\",\n";
785 ss << " \"estimation_framework\": ";
786#ifdef TMB_MODEL
787 ss << "\"Template_Model_Builder (TMB)\",";
788#else
789 ss << "\"FIMS\",";
790#endif
791 ss << " \"id\": " << this->get_id() << ",\n";
792 ss << " \"objective_function_value\": " << sanitize_val(value) << ",\n";
793 ss << "\"growth\":[\n";
794 for (module_id_it = growth_ids.begin(); module_id_it != growth_ids.end();
795 module_id_it++) {
796 std::shared_ptr<GrowthInterfaceBase> growth_interface =
798
799 if (growth_interface != NULL) {
800 growth_interface->finalize();
801 ss << growth_interface->to_json();
802 if (std::next(module_id_it) != growth_ids.end()) {
803 ss << ", ";
804 }
805 }
806 }
807
808 ss << "],\n";
809
810 ss << "\"recruitment\": [\n";
811 for (module_id_it = recruitment_ids.begin();
812 module_id_it != recruitment_ids.end(); module_id_it++) {
813 std::shared_ptr<RecruitmentInterfaceBase> recruitment_interface =
815 if (recruitment_interface) {
816 recruitment_interface->finalize();
817 ss << recruitment_interface->to_json();
818 if (std::next(module_id_it) != recruitment_ids.end()) {
819 ss << ", ";
820 }
821 }
822 }
823 ss << "],\n";
824
825 ss << "\"maturity\": [\n";
826 for (module_id_it = maturity_ids.begin();
827 module_id_it != maturity_ids.end(); module_id_it++) {
828 std::shared_ptr<MaturityInterfaceBase> maturity_interface =
830 if (maturity_interface) {
831 maturity_interface->finalize();
832 ss << maturity_interface->to_json();
833 if (std::next(module_id_it) != maturity_ids.end()) {
834 ss << ", ";
835 }
836 }
837 }
838 ss << "],\n";
839
840 ss << "\"selectivity\": [\n";
841 for (module_id_it = selectivity_ids.begin();
842 module_id_it != selectivity_ids.end(); module_id_it++) {
843 std::shared_ptr<SelectivityInterfaceBase> selectivity_interface =
845 if (selectivity_interface) {
846 selectivity_interface->finalize();
847 ss << selectivity_interface->to_json();
848 if (std::next(module_id_it) != selectivity_ids.end()) {
849 ss << ", ";
850 }
851 }
852 }
853 ss << "],\n";
854
855 ss << " \"population_ids\": [";
856 for (pit = this->population_ids->begin();
857 pit != this->population_ids->end(); pit++) {
858 ss << *pit;
859 if (std::next(pit) != this->population_ids->end()) {
860 ss << ", ";
861 }
862 }
863 ss << "],\n";
864 ss << " \"fleet_ids\": [";
865
866 for (fids = fleet_ids.begin(); fids != fleet_ids.end(); fids++) {
867 ss << *fids;
868 if (std::next(fids) != fleet_ids.end()) {
869 ss << ", ";
870 }
871 }
872 ss << "],\n";
873 ss << "\"populations\": [\n";
874 typename std::set<uint32_t>::iterator pop_it;
875 typename std::set<uint32_t>::iterator pop_end_it;
876 pop_end_it = this->population_ids->end();
877 typename std::set<uint32_t>::iterator pop_second_to_last_it;
878 if (pop_end_it != this->population_ids->begin()) {
879 pop_second_to_last_it = std::prev(pop_end_it);
880 } else {
881 pop_second_to_last_it = pop_end_it;
882 }
883 for (pop_it = this->population_ids->begin();
884 pop_it != pop_second_to_last_it; pop_it++) {
885 std::shared_ptr<PopulationInterface> population_interface =
886 std::dynamic_pointer_cast<PopulationInterface>(
888 if (population_interface) {
889 std::set<uint32_t>::iterator fids;
890 for (fids = population_interface->fleet_ids->begin();
891 fids != population_interface->fleet_ids->end(); fids++) {
892 fleet_ids.insert(*fids);
893 }
894 population_interface->finalize();
895 ss << this->population_to_json(population_interface.get()) << ",";
896 } else {
897 FIMS_ERROR_LOG("Population with id " + fims::to_string(*pop_it) +
898 " not found in live objects.");
899 ss << "{}"; // Return empty JSON for this population
900 }
901 }
902
903 std::shared_ptr<PopulationInterface> population_interface =
904 std::dynamic_pointer_cast<PopulationInterface>(
905 PopulationInterfaceBase::live_objects[*pop_second_to_last_it]);
906 if (population_interface) {
907 std::set<uint32_t>::iterator fids;
908 for (fids = population_interface->fleet_ids->begin();
909 fids != population_interface->fleet_ids->end(); fids++) {
910 fleet_ids.insert(*fids);
911 }
912 ss << this->population_to_json(population_interface.get());
913 } else {
914 FIMS_ERROR_LOG("Population with id " + fims::to_string(*pop_it) +
915 " not found in live objects.");
916 ss << "{}"; // Return empty JSON for this population
917 }
918
919 ss << "]";
920 ss << ",\n";
921 ss << "\"fleets\": [\n";
922
923 typename std::set<uint32_t>::iterator fleet_it;
924 typename std::set<uint32_t>::iterator fleet_end_it;
925 fleet_end_it = fleet_ids.end();
926 typename std::set<uint32_t>::iterator fleet_second_to_last_it;
927
928 if (fleet_end_it != fleet_ids.begin()) {
929 fleet_second_to_last_it = std::prev(fleet_end_it);
930 }
931 for (fleet_it = fleet_ids.begin(); fleet_it != fleet_second_to_last_it;
932 fleet_it++) {
933 std::shared_ptr<FleetInterface> fleet_interface =
934 std::dynamic_pointer_cast<FleetInterface>(
936 if (fleet_interface) {
937 fleet_interface->finalize();
938 ss << this->fleet_to_json(fleet_interface.get()) << ",";
939 } else {
940 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*fleet_it) +
941 " not found in live objects.");
942 ss << "{}"; // Return empty JSON for this fleet
943 }
944 }
945 std::shared_ptr<FleetInterface> fleet_interface =
946 std::dynamic_pointer_cast<FleetInterface>(
947 FleetInterfaceBase::live_objects[*fleet_second_to_last_it]);
948 if (fleet_interface) {
949 ss << this->fleet_to_json(fleet_interface.get());
950 } else {
951 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*fleet_it) +
952 " not found in live objects.");
953 ss << "{}"; // Return empty JSON for this fleet
954 }
955
956 ss << "],\n";
957
958 ss << "\"density_components\": [\n";
959
960 typename std::map<
961 uint32_t, std::shared_ptr<DistributionsInterfaceBase>>::iterator dit;
963 dit != DistributionsInterfaceBase::live_objects.end(); ++dit) {
964 std::shared_ptr<DistributionsInterfaceBase> dist_interface =
965 (*dit).second;
966 if (dist_interface) {
967 dist_interface->finalize();
968 ss << dist_interface->to_json();
969 if (std::next(dit) != DistributionsInterfaceBase::live_objects.end()) {
970 ss << ",\n";
971 }
972 }
973 }
974 ss << "\n],\n";
975 ss << "\"data\": [\n";
976 typename std::map<uint32_t, std::shared_ptr<DataInterfaceBase>>::iterator
977 d_it;
978 for (d_it = DataInterfaceBase::live_objects.begin();
979 d_it != DataInterfaceBase::live_objects.end(); ++d_it) {
980 std::shared_ptr<DataInterfaceBase> data_interface = (*d_it).second;
981 if (data_interface) {
982 data_interface->finalize();
983 ss << data_interface->to_json();
984 if (std::next(d_it) != DataInterfaceBase::live_objects.end()) {
985 ss << ",\n";
986 }
987 }
988 }
989 ss << "\n]\n";
990 ss << "}\n";
991#ifdef TMB_MODEL
992 model->do_reporting = true;
993#endif
994 return fims::JsonParser::PrettyFormatJSON(ss.str());
995 }
996
997#ifdef TMB_MODEL
998
999 template <typename Type>
1000 bool add_to_fims_tmb_internal() {
1001 std::shared_ptr<fims_info::Information<Type>> info =
1003
1004 std::shared_ptr<fims_popdy::CatchAtAge<Type>> model =
1005 std::make_shared<fims_popdy::CatchAtAge<Type>>();
1006
1008
1009 for (it = this->population_ids->begin(); it != this->population_ids->end();
1010 ++it) {
1011 model->AddPopulation((*it));
1012 }
1013
1014 std::set<uint32_t> fleet_ids; // all fleets in the model
1015 typedef typename std::set<uint32_t>::iterator fleet_ids_iterator;
1016
1017 // add to Information
1018 info->models_map[this->get_id()] = model;
1019
1020 for (it = this->population_ids->begin(); it != this->population_ids->end();
1021 ++it) {
1022 auto population_interface_it =
1024 if (population_interface_it ==
1026 FIMS_ERROR_LOG("Population with id " + fims::to_string(*it) +
1027 " not found in live objects.");
1028 continue;
1029 }
1030
1031 std::shared_ptr<PopulationInterface> population_interface =
1032 std::dynamic_pointer_cast<PopulationInterface>(
1033 population_interface_it->second);
1034 if (!population_interface) {
1035 FIMS_ERROR_LOG("Population with id " + fims::to_string(*it) +
1036 " not found in live objects.");
1037 continue;
1038 }
1039
1040 model->InitializePopulationDerivedQuantities(population_interface->id);
1041 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1042 model->GetPopulationDerivedQuantities(population_interface->id);
1043
1044 std::map<std::string, fims_popdy::DimensionInfo>
1045 &derived_quantities_dim_info =
1046 model->GetPopulationDimensionInfo(population_interface->id);
1047
1048 std::stringstream ss;
1049
1050 derived_quantities["total_catch_weight"] =
1051 fims::Vector<Type>(population_interface->n_years.get());
1052
1053 derived_quantities_dim_info["total_catch_weight"] =
1054 fims_popdy::DimensionInfo(
1055 "total_catch_weight",
1056 fims::Vector<int>{(int)population_interface->n_years.get()},
1057 fims::Vector<std::string>{"n_years"});
1058 info->variable_map[population_interface->total_catch_weight.id_m] =
1059 &derived_quantities["total_catch_weight"];
1060
1061 derived_quantities["total_catch_numbers"] =
1062 fims::Vector<Type>(population_interface->n_years.get());
1063
1064 derived_quantities_dim_info["total_catch_numbers"] =
1065 fims_popdy::DimensionInfo(
1066 "total_catch_numbers",
1067 fims::Vector<int>{population_interface->n_years.get()},
1068 fims::Vector<std::string>{"n_years"});
1069 info->variable_map[population_interface->total_catch_numbers.id_m] =
1070 &derived_quantities["total_catch_numbers"];
1071
1072 derived_quantities["mortality_F"] =
1073 fims::Vector<Type>(population_interface->n_years.get() *
1074 population_interface->n_ages.get());
1075 derived_quantities_dim_info["mortality_F"] = fims_popdy::DimensionInfo(
1076 "mortality_F",
1077 fims::Vector<int>{population_interface->n_years.get(),
1078 population_interface->n_ages.get()},
1079 fims::Vector<std::string>{"n_years", "n_ages"});
1080 info->variable_map[population_interface->mortality_F.id_m] =
1081 &derived_quantities["mortality_F"];
1082
1083 derived_quantities["mortality_M"] =
1084 fims::Vector<Type>(population_interface->n_years.get() *
1085 population_interface->n_ages.get());
1086 derived_quantities_dim_info["mortality_M"] = fims_popdy::DimensionInfo(
1087 "mortality_M",
1088 fims::Vector<int>{population_interface->n_years.get(),
1089 population_interface->n_ages.get()},
1090 fims::Vector<std::string>{"n_years", "n_ages"});
1091 info->variable_map[population_interface->mortality_M.id_m] =
1092 &derived_quantities["mortality_M"];
1093
1094 derived_quantities["mortality_Z"] =
1095 fims::Vector<Type>(population_interface->n_years.get() *
1096 population_interface->n_ages.get());
1097 derived_quantities_dim_info["mortality_Z"] = fims_popdy::DimensionInfo(
1098 "mortality_Z",
1099 fims::Vector<int>{population_interface->n_years.get(),
1100 population_interface->n_ages.get()},
1101 fims::Vector<std::string>{"n_years", "n_ages"});
1102 info->variable_map[population_interface->mortality_Z.id_m] =
1103 &derived_quantities["mortality_Z"];
1104
1105 derived_quantities["numbers_at_age"] =
1106 fims::Vector<Type>((population_interface->n_years.get() + 1) *
1107 population_interface->n_ages.get());
1108 derived_quantities_dim_info["numbers_at_age"] = fims_popdy::DimensionInfo(
1109 "numbers_at_age",
1110 fims::Vector<int>{(population_interface->n_years.get() + 1),
1111 population_interface->n_ages.get()},
1112 fims::Vector<std::string>{"n_years+1", "n_ages"});
1113 info->variable_map[population_interface->numbers_at_age.id_m] =
1114 &derived_quantities["numbers_at_age"];
1115
1116 derived_quantities["unfished_numbers_at_age"] =
1117 fims::Vector<Type>((population_interface->n_years.get() + 1) *
1118 population_interface->n_ages.get());
1119 derived_quantities_dim_info["unfished_numbers_at_age"] =
1120 fims_popdy::DimensionInfo(
1121 "unfished_numbers_at_age",
1122 fims::Vector<int>{(population_interface->n_years.get() + 1),
1123 population_interface->n_ages.get()},
1124 fims::Vector<std::string>{"n_years+1", "n_ages"});
1125 info->variable_map[population_interface->unfished_numbers_at_age.id_m] =
1126 &derived_quantities["unfished_numbers_at_age"];
1127
1128 derived_quantities["biomass"] =
1129 fims::Vector<Type>((population_interface->n_years.get() + 1));
1130 derived_quantities_dim_info["biomass"] = fims_popdy::DimensionInfo(
1131 "biomass",
1132 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1133 fims::Vector<std::string>{"n_years+1"});
1134 info->variable_map[population_interface->biomass.id_m] =
1135 &derived_quantities["biomass"];
1136
1137 derived_quantities["spawning_biomass"] =
1138 fims::Vector<Type>((population_interface->n_years.get() + 1));
1139 derived_quantities_dim_info["spawning_biomass"] =
1140 fims_popdy::DimensionInfo(
1141 "spawning_biomass",
1142 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1143 fims::Vector<std::string>{"n_years+1"});
1144 info->variable_map[population_interface->spawning_biomass.id_m] =
1145 &derived_quantities["spawning_biomass"];
1146
1147 derived_quantities["unfished_biomass"] =
1148 fims::Vector<Type>((population_interface->n_years.get() + 1));
1149 derived_quantities_dim_info["unfished_biomass"] =
1150 fims_popdy::DimensionInfo(
1151 "unfished_biomass",
1152 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1153 fims::Vector<std::string>{"n_years+1"});
1154 info->variable_map[population_interface->unfished_biomass.id_m] =
1155 &derived_quantities["unfished_biomass"];
1156
1157 derived_quantities["unfished_spawning_biomass"] =
1158 fims::Vector<Type>((population_interface->n_years.get() + 1));
1159 derived_quantities_dim_info["unfished_spawning_biomass"] =
1160 fims_popdy::DimensionInfo(
1161 "unfished_spawning_biomass",
1162 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1163 fims::Vector<std::string>{"n_years+1"});
1164 info->variable_map[population_interface->unfished_spawning_biomass.id_m] =
1165 &derived_quantities["unfished_spawning_biomass"];
1166
1167 derived_quantities["proportion_mature_at_age"] =
1168 fims::Vector<Type>((population_interface->n_years.get() + 1) *
1169 population_interface->n_ages.get());
1170 derived_quantities_dim_info["proportion_mature_at_age"] =
1171 fims_popdy::DimensionInfo(
1172 "proportion_mature_at_age",
1173 fims::Vector<int>{(population_interface->n_years.get() + 1),
1174 population_interface->n_ages.get()},
1175 fims::Vector<std::string>{"n_years+1", "n_ages"});
1176 info->variable_map[population_interface->proportion_mature_at_age.id_m] =
1177 &derived_quantities["proportion_mature_at_age"];
1178
1179 derived_quantities["expected_recruitment"] =
1180 fims::Vector<Type>((population_interface->n_years.get() + 1));
1181 derived_quantities_dim_info["expected_recruitment"] =
1182 fims_popdy::DimensionInfo(
1183 "expected_recruitment",
1184 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1185 fims::Vector<std::string>{"n_years+1"});
1186 info->variable_map[population_interface->expected_recruitment.id_m] =
1187 &derived_quantities["expected_recruitment"];
1188
1189 derived_quantities["sum_selectivity"] =
1190 fims::Vector<Type>(population_interface->n_years.get() *
1191 population_interface->n_ages.get());
1192 derived_quantities_dim_info["sum_selectivity"] =
1193 fims_popdy::DimensionInfo(
1194 "sum_selectivity",
1195 fims::Vector<int>{population_interface->n_years.get(),
1196 population_interface->n_ages.get()},
1197 fims::Vector<std::string>{"n_years", "n_ages"});
1198 info->variable_map[population_interface->sum_selectivity.id_m] =
1199 &derived_quantities["sum_selectivity"];
1200
1201 // replace elements in the variable map
1202
1203 for (fleet_ids_iterator fit = population_interface->fleet_ids->begin();
1204 fit != population_interface->fleet_ids->end(); ++fit) {
1205 fleet_ids.insert(*fit);
1206 }
1207 }
1208
1209 for (fleet_ids_iterator it = fleet_ids.begin(); it != fleet_ids.end();
1210 ++it) {
1211 auto fleet_interface_it = FleetInterfaceBase::live_objects.find(*it);
1212 if (fleet_interface_it == FleetInterfaceBase::live_objects.end()) {
1213 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*it) +
1214 " not found in live objects.");
1215 continue;
1216 }
1217
1218 std::shared_ptr<FleetInterface> fleet_interface =
1219 std::dynamic_pointer_cast<FleetInterface>(fleet_interface_it->second);
1220 if (!fleet_interface) {
1221 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*it) +
1222 " not found in live objects.");
1223 continue;
1224 }
1225 model->InitializeFleetDerivedQuantities(fleet_interface->id);
1226 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1227 model->GetFleetDerivedQuantities(fleet_interface->id);
1228
1229 std::map<std::string, fims_popdy::DimensionInfo>
1230 &derived_quantities_dim_info =
1231 model->GetFleetDimensionInfo(fleet_interface->id);
1232
1233 // initialize derive quantities
1234 // catch
1235 derived_quantities["catch_numbers_at_age"] = fims::Vector<Type>(
1236 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1237 derived_quantities_dim_info["catch_numbers_at_age"] =
1238 fims_popdy::DimensionInfo(
1239 "catch_numbers_at_age",
1240 fims::Vector<int>{(fleet_interface->n_years.get()),
1241 fleet_interface->n_ages.get()},
1242 fims::Vector<std::string>{"n_years", "n_ages"});
1243 info->variable_map[fleet_interface->catch_numbers_at_age.id_m] =
1244 &derived_quantities["catch_numbers_at_age"];
1245
1246 derived_quantities["catch_weight_at_age"] = fims::Vector<Type>(
1247 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1248 derived_quantities_dim_info["catch_weight_at_age"] =
1249 fims_popdy::DimensionInfo(
1250 "catch_weight_at_age",
1251 fims::Vector<int>{(fleet_interface->n_years.get()),
1252 fleet_interface->n_ages.get()},
1253 fims::Vector<std::string>{"n_years", "n_ages"});
1254 info->variable_map[fleet_interface->catch_weight_at_age.id_m] =
1255 &derived_quantities["catch_weight_at_age"];
1256
1257 derived_quantities["catch_numbers_at_length"] = fims::Vector<Type>(
1258 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1259 derived_quantities_dim_info["catch_numbers_at_length"] =
1260 fims_popdy::DimensionInfo(
1261 "catch_numbers_at_length",
1262 fims::Vector<int>{(fleet_interface->n_years.get()),
1263 fleet_interface->n_lengths.get()},
1264 fims::Vector<std::string>{"n_years", "n_lengths"});
1265 info->variable_map[fleet_interface->catch_numbers_at_length.id_m] =
1266 &derived_quantities["catch_numbers_at_length"];
1267
1268 derived_quantities["catch_weight"] =
1269 fims::Vector<Type>(fleet_interface->n_years.get());
1270 derived_quantities_dim_info["catch_weight"] = fims_popdy::DimensionInfo(
1271 "catch_weight", fims::Vector<int>{(fleet_interface->n_years.get())},
1272 fims::Vector<std::string>{"n_years"});
1273 info->variable_map[fleet_interface->catch_weight.id_m] =
1274 &derived_quantities["catch_weight"];
1275
1276 derived_quantities["catch_numbers"] =
1277 fims::Vector<Type>(fleet_interface->n_years.get());
1278 derived_quantities_dim_info["catch_numbers"] = fims_popdy::DimensionInfo(
1279 "catch_numbers", fims::Vector<int>{(fleet_interface->n_years.get())},
1280 fims::Vector<std::string>{"n_years"});
1281 info->variable_map[fleet_interface->catch_numbers.id_m] =
1282 &derived_quantities["catch_numbers"];
1283
1284 derived_quantities["catch_expected"] =
1285 fims::Vector<Type>(fleet_interface->n_years.get());
1286 derived_quantities_dim_info["catch_expected"] = fims_popdy::DimensionInfo(
1287 "catch_expected", fims::Vector<int>{(fleet_interface->n_years.get())},
1288 fims::Vector<std::string>{"n_years"});
1289 info->variable_map[fleet_interface->catch_expected.id_m] =
1290 &derived_quantities["catch_expected"];
1291
1292 derived_quantities["log_catch_expected"] =
1293 fims::Vector<Type>(fleet_interface->n_years.get());
1294 derived_quantities_dim_info["log_catch_expected"] =
1295 fims_popdy::DimensionInfo(
1296 "log_catch_expected",
1297 fims::Vector<int>{(fleet_interface->n_years.get())},
1298 fims::Vector<std::string>{"n_years"});
1299 info->variable_map[fleet_interface->log_catch_expected.id_m] =
1300 &derived_quantities["log_catch_expected"];
1301
1302 derived_quantities["agecomp_proportion"] = fims::Vector<Type>(
1303 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1304 derived_quantities_dim_info["agecomp_proportion"] =
1305 fims_popdy::DimensionInfo(
1306 "agecomp_proportion",
1307 fims::Vector<int>{(fleet_interface->n_years.get()),
1308 fleet_interface->n_ages.get()},
1309 fims::Vector<std::string>{"n_years", "n_ages"});
1310 info->variable_map[fleet_interface->agecomp_proportion.id_m] =
1311 &derived_quantities["agecomp_proportion"];
1312
1313 derived_quantities["lengthcomp_proportion"] = fims::Vector<Type>(
1314 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1315 derived_quantities_dim_info["lengthcomp_proportion"] =
1316 fims_popdy::DimensionInfo(
1317 "lengthcomp_proportion",
1318 fims::Vector<int>{(fleet_interface->n_years.get()),
1319 fleet_interface->n_lengths.get()},
1320 fims::Vector<std::string>{"n_years", "n_lengths"});
1321 info->variable_map[fleet_interface->lengthcomp_proportion.id_m] =
1322 &derived_quantities["lengthcomp_proportion"];
1323
1324 // index
1325 derived_quantities["index_numbers_at_age"] = fims::Vector<Type>(
1326 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1327 derived_quantities_dim_info["index_numbers_at_age"] =
1328 fims_popdy::DimensionInfo(
1329 "index_numbers_at_age",
1330 fims::Vector<int>{(fleet_interface->n_years.get()),
1331 fleet_interface->n_ages.get()},
1332 fims::Vector<std::string>{"n_years", "n_ages"});
1333 info->variable_map[fleet_interface->index_numbers_at_age.id_m] =
1334 &derived_quantities["index_numbers_at_age"];
1335
1336 derived_quantities["index_weight_at_age"] = fims::Vector<Type>(
1337 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1338 derived_quantities_dim_info["index_weight_at_age"] =
1339 fims_popdy::DimensionInfo(
1340 "index_weight_at_age",
1341 fims::Vector<int>{(fleet_interface->n_years.get()),
1342 fleet_interface->n_ages.get()},
1343 fims::Vector<std::string>{"n_years", "n_ages"});
1344 info->variable_map[fleet_interface->index_weight_at_age.id_m] =
1345 &derived_quantities["index_weight_at_age"];
1346
1347 derived_quantities["index_numbers_at_length"] = fims::Vector<Type>(
1348 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1349 derived_quantities_dim_info["index_numbers_at_length"] =
1350 fims_popdy::DimensionInfo(
1351 "index_numbers_at_length",
1352 fims::Vector<int>{(fleet_interface->n_years.get()),
1353 fleet_interface->n_lengths.get()},
1354 fims::Vector<std::string>{"n_years", "n_lengths"});
1355 info->variable_map[fleet_interface->index_numbers_at_length.id_m] =
1356 &derived_quantities["index_numbers_at_length"];
1357
1358 derived_quantities["index_weight"] =
1359 fims::Vector<Type>(fleet_interface->n_years.get());
1360 derived_quantities_dim_info["index_weight"] = fims_popdy::DimensionInfo(
1361 "index_weight", fims::Vector<int>{(fleet_interface->n_years.get())},
1362 fims::Vector<std::string>{"n_years"});
1363 info->variable_map[fleet_interface->index_weight.id_m] =
1364 &derived_quantities["index_weight"];
1365
1366 derived_quantities["index_numbers"] =
1367 fims::Vector<Type>(fleet_interface->n_years.get());
1368 derived_quantities_dim_info["index_numbers"] = fims_popdy::DimensionInfo(
1369 "index_numbers", fims::Vector<int>{(fleet_interface->n_years.get())},
1370 fims::Vector<std::string>{"n_years"});
1371 info->variable_map[fleet_interface->index_numbers.id_m] =
1372 &derived_quantities["index_numbers"];
1373
1374 derived_quantities["index_expected"] =
1375 fims::Vector<Type>(fleet_interface->n_years.get());
1376 derived_quantities_dim_info["index_expected"] = fims_popdy::DimensionInfo(
1377 "index_expected", fims::Vector<int>{(fleet_interface->n_years.get())},
1378 fims::Vector<std::string>{"n_years"});
1379 info->variable_map[fleet_interface->index_expected.id_m] =
1380 &derived_quantities["index_expected"];
1381
1382 derived_quantities["log_index_expected"] =
1383 fims::Vector<Type>(fleet_interface->n_years.get());
1384 derived_quantities_dim_info["log_index_expected"] =
1385 fims_popdy::DimensionInfo(
1386 "log_index_expected",
1387 fims::Vector<int>{(fleet_interface->n_years.get())},
1388 fims::Vector<std::string>{"n_years"});
1389 info->variable_map[fleet_interface->log_index_expected.id_m] =
1390 &derived_quantities["log_index_expected"];
1391
1392 derived_quantities["agecomp_expected"] = fims::Vector<Type>(
1393 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1394 derived_quantities_dim_info["agecomp_expected"] =
1395 fims_popdy::DimensionInfo(
1396 "agecomp_expected",
1397 fims::Vector<int>{(fleet_interface->n_years.get()),
1398 (fleet_interface->n_ages.get())},
1399 fims::Vector<std::string>{"n_years", "n_ages"});
1400 info->variable_map[fleet_interface->agecomp_expected.id_m] =
1401 &derived_quantities["agecomp_expected"];
1402
1403 derived_quantities["lengthcomp_expected"] = fims::Vector<Type>(
1404 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1405 derived_quantities_dim_info["lengthcomp_expected"] =
1406 fims_popdy::DimensionInfo(
1407 "lengthcomp_expected",
1408 fims::Vector<int>{(fleet_interface->n_years.get()),
1409 (fleet_interface->n_lengths.get())},
1410 fims::Vector<std::string>{"n_years", "n_lengths"});
1411 info->variable_map[fleet_interface->lengthcomp_expected.id_m] =
1412 &derived_quantities["lengthcomp_expected"];
1413 }
1414
1415 return true;
1416 }
1417
1418 virtual bool add_to_fims_tmb() {
1419 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
1420 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
1421 return true;
1422 }
1423
1424#endif
1425};
1426
1427#endif
virtual std::string to_json()
Serialize the fishery model to a JSON string.
Definition rcpp_models.hpp:726
void AddPopulation(uint32_t id)
Definition rcpp_models.hpp:239
std::string derived_quantity_to_json(std::map< std::string, fims::Vector< double > >::iterator it, const fims_popdy::DimensionInfo &dim_info)
Definition rcpp_models.hpp:501
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_models.hpp:328
CatchAtAgeInterface()
The constructor.
Definition rcpp_models.hpp:221
void DoReporting(bool report)
Enable or disable reporting for the CatchAtAge model.
Definition rcpp_models.hpp:260
void ReportAgeToLengthConversionDerivedTensor(bool report)
Turn reporting of the Growth-derived age-to-length tensor on or off.
Definition rcpp_models.hpp:305
virtual uint32_t get_id()
Method to get this id.
Definition rcpp_models.hpp:323
std::string fleet_to_json(FleetInterface *fleet_interface)
Method to convert a fleet to a JSON string.
Definition rcpp_models.hpp:613
std::string derived_quantities_component_to_json(std::map< std::string, fims::Vector< double > > &dqs, std::map< std::string, fims_popdy::DimensionInfo > &dim_info)
Send the fleet-based derived quantities to the json file.
Definition rcpp_models.hpp:578
std::string population_to_json(PopulationInterface *population_interface)
Method to convert a population to a JSON string.
Definition rcpp_models.hpp:333
CatchAtAgeInterface(const CatchAtAgeInterface &other)
Construct a new Catch At Age Interface object.
Definition rcpp_models.hpp:233
bool IsReporting()
Check if reporting is enabled for the CatchAtAge model.
Definition rcpp_models.hpp:282
static std::map< uint32_t, std::shared_ptr< DataInterfaceBase > > live_objects
The map associating the IDs of DataInterfaceBase to the objects. This is a live object,...
Definition rcpp_data.hpp:43
static std::map< uint32_t, std::shared_ptr< DistributionsInterfaceBase > > live_objects
The map associating the ID of the DistributionsInterfaceBase to the DistributionsInterfaceBase object...
Definition rcpp_distribution.hpp:72
Base class for all interface objects.
Definition rcpp_interface_base.hpp:655
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:665
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:670
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
virtual uint32_t get_id()=0
Get the ID for the child fleet interface objects to inherit.
FisheryModelInterfaceBase()
The constructor.
Definition rcpp_models.hpp:67
std::set< uint32_t >::iterator population_id_iterator
Iterator for population ids.
Definition rcpp_models.hpp:45
uint32_t id
The local id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:55
std::valarray< double > fabs(const std::valarray< double > &v)
A function to compute the absolute value of a value array of floating-point values....
Definition rcpp_models.hpp:203
virtual std::string to_json()
Serialize the fishery model to a JSON string.
Definition rcpp_models.hpp:101
static std::map< uint32_t, std::shared_ptr< FisheryModelInterfaceBase > > live_objects
The map associating the IDs of FleetInterfaceBase to the objects. This is a live object,...
Definition rcpp_models.hpp:62
double sum(const std::valarray< double > &v)
Sum method to calculate the sum of an array or vector of doubles.
Definition rcpp_models.hpp:156
Rcpp::NumericVector get_random_parameters_vector()
Get the vector of random effect parameters for the model.
Definition rcpp_models.hpp:137
Rcpp::NumericVector get_fixed_parameters_vector()
Get the vector of fixed effect parameters for the model.
Definition rcpp_models.hpp:117
std::shared_ptr< std::set< uint32_t > > population_ids
The set of population ids that this fishery model operates on.
Definition rcpp_models.hpp:41
virtual ~FisheryModelInterfaceBase()
The destructor.
Definition rcpp_models.hpp:86
double sum(const std::vector< double > &v)
Sum method for a vector of doubles.
Definition rcpp_models.hpp:170
uint32_t id
The local id of the FleetInterfaceBase object.
Definition rcpp_fleet.hpp:28
static std::map< uint32_t, std::shared_ptr< FleetInterfaceBase > > live_objects
The map associating the IDs of FleetInterfaceBase to the objects. This is a live object,...
Definition rcpp_fleet.hpp:34
The Rcpp interface for Fleet to instantiate from R: fleet <- methods::new(Fleet).
Definition rcpp_fleet.hpp:68
SharedInt n_years
The number of years in the fleet data.
Definition rcpp_fleet.hpp:124
uint32_t GetSelectivityID()
Get the unique ID for the selectivity object.
Definition rcpp_fleet.hpp:395
int GetObservedIndexDataID()
Get the unique id for the observed index data object.
Definition rcpp_fleet.hpp:429
int GetObservedCatchDataID()
Get the unique id for the observed catch data object.
Definition rcpp_fleet.hpp:436
int GetObservedAgeCompDataID()
Get the unique ID for the observed age-composition data object.
Definition rcpp_fleet.hpp:400
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_fleet.hpp:321
int GetObservedLengthCompDataID()
Get the unique ID for the observed length-composition data object.
Definition rcpp_fleet.hpp:408
SharedString name
The name of the fleet.
Definition rcpp_fleet.hpp:108
SharedInt n_ages
The number of age bins in the fleet data.
Definition rcpp_fleet.hpp:112
VariableVector log_q
The natural log of the index of abundance scaling parameter for this fleet.
Definition rcpp_fleet.hpp:139
VariableVector log_Fmort
The vector of the natural log of fishing mortality rates for this fleet.
Definition rcpp_fleet.hpp:144
SharedInt n_lengths
The number of length bins in the fleet data.
Definition rcpp_fleet.hpp:116
RealVector lengths
Fleet-specific observation-bin lower edges.
Definition rcpp_fleet.hpp:120
static std::map< uint32_t, std::shared_ptr< GrowthInterfaceBase > > live_objects
The map associating the IDs of GrowthInterfaceBase to the objects. This is a live object,...
Definition rcpp_growth.hpp:39
static std::map< uint32_t, std::shared_ptr< MaturityInterfaceBase > > live_objects
The map associating the IDs of MaturityInterfaceBase to the objects. This is a live object,...
Definition rcpp_maturity.hpp:35
uint32_t id
The local id of the PopulationInterfaceBase object.
Definition rcpp_population.hpp:28
static std::map< uint32_t, std::shared_ptr< PopulationInterfaceBase > > live_objects
The map associating the IDs of PopulationInterfaceBase to the objects. This is a live object,...
Definition rcpp_population.hpp:35
Rcpp interface for a new Population to instantiate from R: population <- methods::new(population).
Definition rcpp_population.hpp:80
VariableVector proportion_female
Proportion of females in the population.
Definition rcpp_population.hpp:146
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_population.hpp:290
VariableVector log_init_naa
The natural log of the initial numbers at age.
Definition rcpp_population.hpp:137
SharedString name
The name for the population.
Definition rcpp_population.hpp:155
SharedInt n_ages
The number of age bins.
Definition rcpp_population.hpp:85
SharedInt maturity_id
The ID of the maturity module.
Definition rcpp_population.hpp:109
VariableVector log_M
The natural log of the natural mortality for each year.
Definition rcpp_population.hpp:125
SharedInt n_years
The number of years.
Definition rcpp_population.hpp:101
VariableVector log_f_multiplier
Log of the population annual fishing mortality multiplier.
Definition rcpp_population.hpp:133
RealVector ages
Ages that are modeled in the population, the length of this vector should equal "n_ages".
Definition rcpp_population.hpp:151
VariableVector spawning_biomass_ratio
The population spawning biomass ratio for each year.
Definition rcpp_population.hpp:129
SharedInt growth_id
The ID of the growth module.
Definition rcpp_population.hpp:113
SharedInt recruitment_id
The ID of the recruitment module.
Definition rcpp_population.hpp:117
static std::map< uint32_t, std::shared_ptr< RecruitmentInterfaceBase > > live_objects
The map associating the IDs of RecruitmentInterfaceBase to the objects. This is a live object,...
Definition rcpp_recruitment.hpp:40
static std::map< uint32_t, std::shared_ptr< SelectivityInterfaceBase > > live_objects
The map associating the IDs of SelectivityInterfaceBase to the objects. This is a live object,...
Definition rcpp_selectivity.hpp:35
int get() const
Retrieve the value of the integer.
Definition rcpp_shared_primitive.hpp:127
std::string get() const
Retrieves the string value managed by the object.
Definition rcpp_shared_primitive.hpp:1617
uint32_t id_m
The local ID of the Variable object.
Definition rcpp_interface_base.hpp:156
size_t size()
Returns the size of a VariableVector.
Definition rcpp_interface_base.hpp:287
static std::string PrettyFormatJSON(const std::string &json)
Formats a JSON string.
Definition fims_json.hpp:182
static std::string EscapeString(const std::string &input)
Definition fims_json.hpp:143
Definition fims_vector.hpp:27
size_type size() const
Returns the number of elements.
Definition fims_vector.hpp:299
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:262
Platform macros and the core FIMS logging system.
std::string to_string(T v)
Definition def.hpp:735
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:664
A simple JSON parsing and generation library.
Includes any .hpp files within the subfolders so that only this file needs to included in include sta...
: Loops over model components and returns the negative log-likelihood function.
The Rcpp interface to declare different types of data, e.g., age-composition and index data....
The Rcpp interface to declare different distributions, e.g., normal and log normal....
The Rcpp interface to declare fleets. Allows for the use of methods::new() in R.
The Rcpp interface to declare different types of growth, e.g., empirical weight-at-age data....
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...
double sanitize_val(double x)
Sanitize a double value by replacing NaN or Inf with -999.0.
Definition rcpp_interface_base.hpp:112
The Rcpp interface to declare different maturity options, e.g., logistic. Allows for the use of metho...
The Rcpp interface to declare different types of populations. Allows for the use of methods::new() in...
The Rcpp interface to declare different types of recruitment, e.g., Beverton–Holt stock–recruitment r...
The Rcpp interface to declare different types of selectivity, e.g., logistic and double logistic....
Structure to hold dimension information for derived quantities.
Definition fishery_model_base.hpp:25
fims::Vector< int > dims
Definition fishery_model_base.hpp:28
int ndims
Definition fishery_model_base.hpp:27
fims::Vector< std::string > dim_names
Definition fishery_model_base.hpp:29