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 // year_i is a position in the flattened output. Write the calendar years
794 // once at the model level so R can replace that position with its year.
795 if (!this->population_ids->empty()) {
796 std::shared_ptr<PopulationInterface> population_interface =
797 std::dynamic_pointer_cast<PopulationInterface>(
799 ->begin()]);
800 if (population_interface) {
801 ss << " \"year\": [";
802 for (int i = 0; i <= population_interface->n_years.get(); i++) {
803 if (i > 0) ss << ", ";
804 ss << population_interface->start_year.get() + i;
805 }
806 ss << "],\n";
807 }
808 }
809 ss << "\"growth\":[\n";
810 for (module_id_it = growth_ids.begin(); module_id_it != growth_ids.end();
811 module_id_it++) {
812 std::shared_ptr<GrowthInterfaceBase> growth_interface =
814
815 if (growth_interface != NULL) {
816 growth_interface->finalize();
817 ss << growth_interface->to_json();
818 if (std::next(module_id_it) != growth_ids.end()) {
819 ss << ", ";
820 }
821 }
822 }
823
824 ss << "],\n";
825
826 ss << "\"recruitment\": [\n";
827 for (module_id_it = recruitment_ids.begin();
828 module_id_it != recruitment_ids.end(); module_id_it++) {
829 std::shared_ptr<RecruitmentInterfaceBase> recruitment_interface =
831 if (recruitment_interface) {
832 recruitment_interface->finalize();
833 ss << recruitment_interface->to_json();
834 if (std::next(module_id_it) != recruitment_ids.end()) {
835 ss << ", ";
836 }
837 }
838 }
839 ss << "],\n";
840
841 ss << "\"maturity\": [\n";
842 for (module_id_it = maturity_ids.begin();
843 module_id_it != maturity_ids.end(); module_id_it++) {
844 std::shared_ptr<MaturityInterfaceBase> maturity_interface =
846 if (maturity_interface) {
847 maturity_interface->finalize();
848 ss << maturity_interface->to_json();
849 if (std::next(module_id_it) != maturity_ids.end()) {
850 ss << ", ";
851 }
852 }
853 }
854 ss << "],\n";
855
856 ss << "\"selectivity\": [\n";
857 for (module_id_it = selectivity_ids.begin();
858 module_id_it != selectivity_ids.end(); module_id_it++) {
859 std::shared_ptr<SelectivityInterfaceBase> selectivity_interface =
861 if (selectivity_interface) {
862 selectivity_interface->finalize();
863 ss << selectivity_interface->to_json();
864 if (std::next(module_id_it) != selectivity_ids.end()) {
865 ss << ", ";
866 }
867 }
868 }
869 ss << "],\n";
870
871 ss << " \"population_ids\": [";
872 for (pit = this->population_ids->begin();
873 pit != this->population_ids->end(); pit++) {
874 ss << *pit;
875 if (std::next(pit) != this->population_ids->end()) {
876 ss << ", ";
877 }
878 }
879 ss << "],\n";
880 ss << " \"fleet_ids\": [";
881
882 for (fids = fleet_ids.begin(); fids != fleet_ids.end(); fids++) {
883 ss << *fids;
884 if (std::next(fids) != fleet_ids.end()) {
885 ss << ", ";
886 }
887 }
888 ss << "],\n";
889 ss << "\"populations\": [\n";
890 typename std::set<uint32_t>::iterator pop_it;
891 typename std::set<uint32_t>::iterator pop_end_it;
892 pop_end_it = this->population_ids->end();
893 typename std::set<uint32_t>::iterator pop_second_to_last_it;
894 if (pop_end_it != this->population_ids->begin()) {
895 pop_second_to_last_it = std::prev(pop_end_it);
896 } else {
897 pop_second_to_last_it = pop_end_it;
898 }
899 for (pop_it = this->population_ids->begin();
900 pop_it != pop_second_to_last_it; pop_it++) {
901 std::shared_ptr<PopulationInterface> population_interface =
902 std::dynamic_pointer_cast<PopulationInterface>(
904 if (population_interface) {
905 std::set<uint32_t>::iterator fids;
906 for (fids = population_interface->fleet_ids->begin();
907 fids != population_interface->fleet_ids->end(); fids++) {
908 fleet_ids.insert(*fids);
909 }
910 population_interface->finalize();
911 ss << this->population_to_json(population_interface.get()) << ",";
912 } else {
913 FIMS_ERROR_LOG("Population with id " + fims::to_string(*pop_it) +
914 " not found in live objects.");
915 ss << "{}"; // Return empty JSON for this population
916 }
917 }
918
919 std::shared_ptr<PopulationInterface> population_interface =
920 std::dynamic_pointer_cast<PopulationInterface>(
921 PopulationInterfaceBase::live_objects[*pop_second_to_last_it]);
922 if (population_interface) {
923 std::set<uint32_t>::iterator fids;
924 for (fids = population_interface->fleet_ids->begin();
925 fids != population_interface->fleet_ids->end(); fids++) {
926 fleet_ids.insert(*fids);
927 }
928 ss << this->population_to_json(population_interface.get());
929 } else {
930 FIMS_ERROR_LOG("Population with id " + fims::to_string(*pop_it) +
931 " not found in live objects.");
932 ss << "{}"; // Return empty JSON for this population
933 }
934
935 ss << "]";
936 ss << ",\n";
937 ss << "\"fleets\": [\n";
938
939 typename std::set<uint32_t>::iterator fleet_it;
940 typename std::set<uint32_t>::iterator fleet_end_it;
941 fleet_end_it = fleet_ids.end();
942 typename std::set<uint32_t>::iterator fleet_second_to_last_it;
943
944 if (fleet_end_it != fleet_ids.begin()) {
945 fleet_second_to_last_it = std::prev(fleet_end_it);
946 }
947 for (fleet_it = fleet_ids.begin(); fleet_it != fleet_second_to_last_it;
948 fleet_it++) {
949 std::shared_ptr<FleetInterface> fleet_interface =
950 std::dynamic_pointer_cast<FleetInterface>(
952 if (fleet_interface) {
953 fleet_interface->finalize();
954 ss << this->fleet_to_json(fleet_interface.get()) << ",";
955 } else {
956 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*fleet_it) +
957 " not found in live objects.");
958 ss << "{}"; // Return empty JSON for this fleet
959 }
960 }
961 std::shared_ptr<FleetInterface> fleet_interface =
962 std::dynamic_pointer_cast<FleetInterface>(
963 FleetInterfaceBase::live_objects[*fleet_second_to_last_it]);
964 if (fleet_interface) {
965 ss << this->fleet_to_json(fleet_interface.get());
966 } else {
967 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*fleet_it) +
968 " not found in live objects.");
969 ss << "{}"; // Return empty JSON for this fleet
970 }
971
972 ss << "],\n";
973
974 ss << "\"density_components\": [\n";
975
976 typename std::map<
977 uint32_t, std::shared_ptr<DistributionsInterfaceBase>>::iterator dit;
979 dit != DistributionsInterfaceBase::live_objects.end(); ++dit) {
980 std::shared_ptr<DistributionsInterfaceBase> dist_interface =
981 (*dit).second;
982 if (dist_interface) {
983 dist_interface->finalize();
984 ss << dist_interface->to_json();
985 if (std::next(dit) != DistributionsInterfaceBase::live_objects.end()) {
986 ss << ",\n";
987 }
988 }
989 }
990 ss << "\n],\n";
991 ss << "\"data\": [\n";
992 typename std::map<uint32_t, std::shared_ptr<DataInterfaceBase>>::iterator
993 d_it;
994 for (d_it = DataInterfaceBase::live_objects.begin();
995 d_it != DataInterfaceBase::live_objects.end(); ++d_it) {
996 std::shared_ptr<DataInterfaceBase> data_interface = (*d_it).second;
997 if (data_interface) {
998 data_interface->finalize();
999 ss << data_interface->to_json();
1000 if (std::next(d_it) != DataInterfaceBase::live_objects.end()) {
1001 ss << ",\n";
1002 }
1003 }
1004 }
1005 ss << "\n]\n";
1006 ss << "}\n";
1007#ifdef TMB_MODEL
1008 model->do_reporting = true;
1009#endif
1010 return fims::JsonParser::PrettyFormatJSON(ss.str());
1011 }
1012
1013#ifdef TMB_MODEL
1014
1015 template <typename Type>
1016 bool add_to_fims_tmb_internal() {
1017 std::shared_ptr<fims_info::Information<Type>> info =
1019
1020 std::shared_ptr<fims_popdy::CatchAtAge<Type>> model =
1021 std::make_shared<fims_popdy::CatchAtAge<Type>>();
1022
1024
1025 for (it = this->population_ids->begin(); it != this->population_ids->end();
1026 ++it) {
1027 model->AddPopulation((*it));
1028 }
1029
1030 std::set<uint32_t> fleet_ids; // all fleets in the model
1031 typedef typename std::set<uint32_t>::iterator fleet_ids_iterator;
1032
1033 // add to Information
1034 info->models_map[this->get_id()] = model;
1035
1036 for (it = this->population_ids->begin(); it != this->population_ids->end();
1037 ++it) {
1038 auto population_interface_it =
1040 if (population_interface_it ==
1042 FIMS_ERROR_LOG("Population with id " + fims::to_string(*it) +
1043 " not found in live objects.");
1044 continue;
1045 }
1046
1047 std::shared_ptr<PopulationInterface> population_interface =
1048 std::dynamic_pointer_cast<PopulationInterface>(
1049 population_interface_it->second);
1050 if (!population_interface) {
1051 FIMS_ERROR_LOG("Population with id " + fims::to_string(*it) +
1052 " not found in live objects.");
1053 continue;
1054 }
1055
1056 model->InitializePopulationDerivedQuantities(population_interface->id);
1057 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1058 model->GetPopulationDerivedQuantities(population_interface->id);
1059
1060 std::map<std::string, fims_popdy::DimensionInfo>
1061 &derived_quantities_dim_info =
1062 model->GetPopulationDimensionInfo(population_interface->id);
1063
1064 std::stringstream ss;
1065
1066 derived_quantities["total_catch_weight"] =
1067 fims::Vector<Type>(population_interface->n_years.get());
1068
1069 derived_quantities_dim_info["total_catch_weight"] =
1070 fims_popdy::DimensionInfo(
1071 "total_catch_weight",
1072 fims::Vector<int>{(int)population_interface->n_years.get()},
1073 fims::Vector<std::string>{"n_years"});
1074 info->variable_map[population_interface->total_catch_weight.id_m] =
1075 &derived_quantities["total_catch_weight"];
1076
1077 derived_quantities["total_catch_numbers"] =
1078 fims::Vector<Type>(population_interface->n_years.get());
1079
1080 derived_quantities_dim_info["total_catch_numbers"] =
1081 fims_popdy::DimensionInfo(
1082 "total_catch_numbers",
1083 fims::Vector<int>{population_interface->n_years.get()},
1084 fims::Vector<std::string>{"n_years"});
1085 info->variable_map[population_interface->total_catch_numbers.id_m] =
1086 &derived_quantities["total_catch_numbers"];
1087
1088 derived_quantities["mortality_F"] =
1089 fims::Vector<Type>(population_interface->n_years.get() *
1090 population_interface->n_ages.get());
1091 derived_quantities_dim_info["mortality_F"] = fims_popdy::DimensionInfo(
1092 "mortality_F",
1093 fims::Vector<int>{population_interface->n_years.get(),
1094 population_interface->n_ages.get()},
1095 fims::Vector<std::string>{"n_years", "n_ages"});
1096 info->variable_map[population_interface->mortality_F.id_m] =
1097 &derived_quantities["mortality_F"];
1098
1099 derived_quantities["mortality_M"] =
1100 fims::Vector<Type>(population_interface->n_years.get() *
1101 population_interface->n_ages.get());
1102 derived_quantities_dim_info["mortality_M"] = fims_popdy::DimensionInfo(
1103 "mortality_M",
1104 fims::Vector<int>{population_interface->n_years.get(),
1105 population_interface->n_ages.get()},
1106 fims::Vector<std::string>{"n_years", "n_ages"});
1107 info->variable_map[population_interface->mortality_M.id_m] =
1108 &derived_quantities["mortality_M"];
1109
1110 derived_quantities["mortality_Z"] =
1111 fims::Vector<Type>(population_interface->n_years.get() *
1112 population_interface->n_ages.get());
1113 derived_quantities_dim_info["mortality_Z"] = fims_popdy::DimensionInfo(
1114 "mortality_Z",
1115 fims::Vector<int>{population_interface->n_years.get(),
1116 population_interface->n_ages.get()},
1117 fims::Vector<std::string>{"n_years", "n_ages"});
1118 info->variable_map[population_interface->mortality_Z.id_m] =
1119 &derived_quantities["mortality_Z"];
1120
1121 derived_quantities["numbers_at_age"] =
1122 fims::Vector<Type>((population_interface->n_years.get() + 1) *
1123 population_interface->n_ages.get());
1124 derived_quantities_dim_info["numbers_at_age"] = fims_popdy::DimensionInfo(
1125 "numbers_at_age",
1126 fims::Vector<int>{(population_interface->n_years.get() + 1),
1127 population_interface->n_ages.get()},
1128 fims::Vector<std::string>{"n_years+1", "n_ages"});
1129 info->variable_map[population_interface->numbers_at_age.id_m] =
1130 &derived_quantities["numbers_at_age"];
1131
1132 derived_quantities["unfished_numbers_at_age"] =
1133 fims::Vector<Type>((population_interface->n_years.get() + 1) *
1134 population_interface->n_ages.get());
1135 derived_quantities_dim_info["unfished_numbers_at_age"] =
1136 fims_popdy::DimensionInfo(
1137 "unfished_numbers_at_age",
1138 fims::Vector<int>{(population_interface->n_years.get() + 1),
1139 population_interface->n_ages.get()},
1140 fims::Vector<std::string>{"n_years+1", "n_ages"});
1141 info->variable_map[population_interface->unfished_numbers_at_age.id_m] =
1142 &derived_quantities["unfished_numbers_at_age"];
1143
1144 derived_quantities["biomass"] =
1145 fims::Vector<Type>((population_interface->n_years.get() + 1));
1146 derived_quantities_dim_info["biomass"] = fims_popdy::DimensionInfo(
1147 "biomass",
1148 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1149 fims::Vector<std::string>{"n_years+1"});
1150 info->variable_map[population_interface->biomass.id_m] =
1151 &derived_quantities["biomass"];
1152
1153 derived_quantities["spawning_biomass"] =
1154 fims::Vector<Type>((population_interface->n_years.get() + 1));
1155 derived_quantities_dim_info["spawning_biomass"] =
1156 fims_popdy::DimensionInfo(
1157 "spawning_biomass",
1158 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1159 fims::Vector<std::string>{"n_years+1"});
1160 info->variable_map[population_interface->spawning_biomass.id_m] =
1161 &derived_quantities["spawning_biomass"];
1162
1163 derived_quantities["unfished_biomass"] =
1164 fims::Vector<Type>((population_interface->n_years.get() + 1));
1165 derived_quantities_dim_info["unfished_biomass"] =
1166 fims_popdy::DimensionInfo(
1167 "unfished_biomass",
1168 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1169 fims::Vector<std::string>{"n_years+1"});
1170 info->variable_map[population_interface->unfished_biomass.id_m] =
1171 &derived_quantities["unfished_biomass"];
1172
1173 derived_quantities["unfished_spawning_biomass"] =
1174 fims::Vector<Type>((population_interface->n_years.get() + 1));
1175 derived_quantities_dim_info["unfished_spawning_biomass"] =
1176 fims_popdy::DimensionInfo(
1177 "unfished_spawning_biomass",
1178 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1179 fims::Vector<std::string>{"n_years+1"});
1180 info->variable_map[population_interface->unfished_spawning_biomass.id_m] =
1181 &derived_quantities["unfished_spawning_biomass"];
1182
1183 derived_quantities["proportion_mature_at_age"] =
1184 fims::Vector<Type>((population_interface->n_years.get() + 1) *
1185 population_interface->n_ages.get());
1186 derived_quantities_dim_info["proportion_mature_at_age"] =
1187 fims_popdy::DimensionInfo(
1188 "proportion_mature_at_age",
1189 fims::Vector<int>{(population_interface->n_years.get() + 1),
1190 population_interface->n_ages.get()},
1191 fims::Vector<std::string>{"n_years+1", "n_ages"});
1192 info->variable_map[population_interface->proportion_mature_at_age.id_m] =
1193 &derived_quantities["proportion_mature_at_age"];
1194
1195 derived_quantities["expected_recruitment"] =
1196 fims::Vector<Type>((population_interface->n_years.get() + 1));
1197 derived_quantities_dim_info["expected_recruitment"] =
1198 fims_popdy::DimensionInfo(
1199 "expected_recruitment",
1200 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1201 fims::Vector<std::string>{"n_years+1"});
1202 info->variable_map[population_interface->expected_recruitment.id_m] =
1203 &derived_quantities["expected_recruitment"];
1204
1205 derived_quantities["sum_selectivity"] =
1206 fims::Vector<Type>(population_interface->n_years.get() *
1207 population_interface->n_ages.get());
1208 derived_quantities_dim_info["sum_selectivity"] =
1209 fims_popdy::DimensionInfo(
1210 "sum_selectivity",
1211 fims::Vector<int>{population_interface->n_years.get(),
1212 population_interface->n_ages.get()},
1213 fims::Vector<std::string>{"n_years", "n_ages"});
1214 info->variable_map[population_interface->sum_selectivity.id_m] =
1215 &derived_quantities["sum_selectivity"];
1216
1217 // replace elements in the variable map
1218
1219 for (fleet_ids_iterator fit = population_interface->fleet_ids->begin();
1220 fit != population_interface->fleet_ids->end(); ++fit) {
1221 fleet_ids.insert(*fit);
1222 }
1223 }
1224
1225 for (fleet_ids_iterator it = fleet_ids.begin(); it != fleet_ids.end();
1226 ++it) {
1227 auto fleet_interface_it = FleetInterfaceBase::live_objects.find(*it);
1228 if (fleet_interface_it == FleetInterfaceBase::live_objects.end()) {
1229 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*it) +
1230 " not found in live objects.");
1231 continue;
1232 }
1233
1234 std::shared_ptr<FleetInterface> fleet_interface =
1235 std::dynamic_pointer_cast<FleetInterface>(fleet_interface_it->second);
1236 if (!fleet_interface) {
1237 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*it) +
1238 " not found in live objects.");
1239 continue;
1240 }
1241 model->InitializeFleetDerivedQuantities(fleet_interface->id);
1242 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1243 model->GetFleetDerivedQuantities(fleet_interface->id);
1244
1245 std::map<std::string, fims_popdy::DimensionInfo>
1246 &derived_quantities_dim_info =
1247 model->GetFleetDimensionInfo(fleet_interface->id);
1248
1249 // initialize derive quantities
1250 // catch
1251 derived_quantities["catch_numbers_at_age"] = fims::Vector<Type>(
1252 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1253 derived_quantities_dim_info["catch_numbers_at_age"] =
1254 fims_popdy::DimensionInfo(
1255 "catch_numbers_at_age",
1256 fims::Vector<int>{(fleet_interface->n_years.get()),
1257 fleet_interface->n_ages.get()},
1258 fims::Vector<std::string>{"n_years", "n_ages"});
1259 info->variable_map[fleet_interface->catch_numbers_at_age.id_m] =
1260 &derived_quantities["catch_numbers_at_age"];
1261
1262 derived_quantities["catch_weight_at_age"] = fims::Vector<Type>(
1263 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1264 derived_quantities_dim_info["catch_weight_at_age"] =
1265 fims_popdy::DimensionInfo(
1266 "catch_weight_at_age",
1267 fims::Vector<int>{(fleet_interface->n_years.get()),
1268 fleet_interface->n_ages.get()},
1269 fims::Vector<std::string>{"n_years", "n_ages"});
1270 info->variable_map[fleet_interface->catch_weight_at_age.id_m] =
1271 &derived_quantities["catch_weight_at_age"];
1272
1273 derived_quantities["catch_numbers_at_length"] = fims::Vector<Type>(
1274 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1275 derived_quantities_dim_info["catch_numbers_at_length"] =
1276 fims_popdy::DimensionInfo(
1277 "catch_numbers_at_length",
1278 fims::Vector<int>{(fleet_interface->n_years.get()),
1279 fleet_interface->n_lengths.get()},
1280 fims::Vector<std::string>{"n_years", "n_lengths"});
1281 info->variable_map[fleet_interface->catch_numbers_at_length.id_m] =
1282 &derived_quantities["catch_numbers_at_length"];
1283
1284 derived_quantities["catch_weight"] =
1285 fims::Vector<Type>(fleet_interface->n_years.get());
1286 derived_quantities_dim_info["catch_weight"] = fims_popdy::DimensionInfo(
1287 "catch_weight", fims::Vector<int>{(fleet_interface->n_years.get())},
1288 fims::Vector<std::string>{"n_years"});
1289 info->variable_map[fleet_interface->catch_weight.id_m] =
1290 &derived_quantities["catch_weight"];
1291
1292 derived_quantities["catch_numbers"] =
1293 fims::Vector<Type>(fleet_interface->n_years.get());
1294 derived_quantities_dim_info["catch_numbers"] = fims_popdy::DimensionInfo(
1295 "catch_numbers", fims::Vector<int>{(fleet_interface->n_years.get())},
1296 fims::Vector<std::string>{"n_years"});
1297 info->variable_map[fleet_interface->catch_numbers.id_m] =
1298 &derived_quantities["catch_numbers"];
1299
1300 derived_quantities["catch_expected"] =
1301 fims::Vector<Type>(fleet_interface->n_years.get());
1302 derived_quantities_dim_info["catch_expected"] = fims_popdy::DimensionInfo(
1303 "catch_expected", fims::Vector<int>{(fleet_interface->n_years.get())},
1304 fims::Vector<std::string>{"n_years"});
1305 info->variable_map[fleet_interface->catch_expected.id_m] =
1306 &derived_quantities["catch_expected"];
1307
1308 derived_quantities["log_catch_expected"] =
1309 fims::Vector<Type>(fleet_interface->n_years.get());
1310 derived_quantities_dim_info["log_catch_expected"] =
1311 fims_popdy::DimensionInfo(
1312 "log_catch_expected",
1313 fims::Vector<int>{(fleet_interface->n_years.get())},
1314 fims::Vector<std::string>{"n_years"});
1315 info->variable_map[fleet_interface->log_catch_expected.id_m] =
1316 &derived_quantities["log_catch_expected"];
1317
1318 derived_quantities["agecomp_proportion"] = fims::Vector<Type>(
1319 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1320 derived_quantities_dim_info["agecomp_proportion"] =
1321 fims_popdy::DimensionInfo(
1322 "agecomp_proportion",
1323 fims::Vector<int>{(fleet_interface->n_years.get()),
1324 fleet_interface->n_ages.get()},
1325 fims::Vector<std::string>{"n_years", "n_ages"});
1326 info->variable_map[fleet_interface->agecomp_proportion.id_m] =
1327 &derived_quantities["agecomp_proportion"];
1328
1329 derived_quantities["lengthcomp_proportion"] = fims::Vector<Type>(
1330 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1331 derived_quantities_dim_info["lengthcomp_proportion"] =
1332 fims_popdy::DimensionInfo(
1333 "lengthcomp_proportion",
1334 fims::Vector<int>{(fleet_interface->n_years.get()),
1335 fleet_interface->n_lengths.get()},
1336 fims::Vector<std::string>{"n_years", "n_lengths"});
1337 info->variable_map[fleet_interface->lengthcomp_proportion.id_m] =
1338 &derived_quantities["lengthcomp_proportion"];
1339
1340 // index
1341 derived_quantities["index_numbers_at_age"] = fims::Vector<Type>(
1342 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1343 derived_quantities_dim_info["index_numbers_at_age"] =
1344 fims_popdy::DimensionInfo(
1345 "index_numbers_at_age",
1346 fims::Vector<int>{(fleet_interface->n_years.get()),
1347 fleet_interface->n_ages.get()},
1348 fims::Vector<std::string>{"n_years", "n_ages"});
1349 info->variable_map[fleet_interface->index_numbers_at_age.id_m] =
1350 &derived_quantities["index_numbers_at_age"];
1351
1352 derived_quantities["index_weight_at_age"] = fims::Vector<Type>(
1353 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1354 derived_quantities_dim_info["index_weight_at_age"] =
1355 fims_popdy::DimensionInfo(
1356 "index_weight_at_age",
1357 fims::Vector<int>{(fleet_interface->n_years.get()),
1358 fleet_interface->n_ages.get()},
1359 fims::Vector<std::string>{"n_years", "n_ages"});
1360 info->variable_map[fleet_interface->index_weight_at_age.id_m] =
1361 &derived_quantities["index_weight_at_age"];
1362
1363 derived_quantities["index_numbers_at_length"] = fims::Vector<Type>(
1364 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1365 derived_quantities_dim_info["index_numbers_at_length"] =
1366 fims_popdy::DimensionInfo(
1367 "index_numbers_at_length",
1368 fims::Vector<int>{(fleet_interface->n_years.get()),
1369 fleet_interface->n_lengths.get()},
1370 fims::Vector<std::string>{"n_years", "n_lengths"});
1371 info->variable_map[fleet_interface->index_numbers_at_length.id_m] =
1372 &derived_quantities["index_numbers_at_length"];
1373
1374 derived_quantities["index_weight"] =
1375 fims::Vector<Type>(fleet_interface->n_years.get());
1376 derived_quantities_dim_info["index_weight"] = fims_popdy::DimensionInfo(
1377 "index_weight", fims::Vector<int>{(fleet_interface->n_years.get())},
1378 fims::Vector<std::string>{"n_years"});
1379 info->variable_map[fleet_interface->index_weight.id_m] =
1380 &derived_quantities["index_weight"];
1381
1382 derived_quantities["index_numbers"] =
1383 fims::Vector<Type>(fleet_interface->n_years.get());
1384 derived_quantities_dim_info["index_numbers"] = fims_popdy::DimensionInfo(
1385 "index_numbers", fims::Vector<int>{(fleet_interface->n_years.get())},
1386 fims::Vector<std::string>{"n_years"});
1387 info->variable_map[fleet_interface->index_numbers.id_m] =
1388 &derived_quantities["index_numbers"];
1389
1390 derived_quantities["index_expected"] =
1391 fims::Vector<Type>(fleet_interface->n_years.get());
1392 derived_quantities_dim_info["index_expected"] = fims_popdy::DimensionInfo(
1393 "index_expected", fims::Vector<int>{(fleet_interface->n_years.get())},
1394 fims::Vector<std::string>{"n_years"});
1395 info->variable_map[fleet_interface->index_expected.id_m] =
1396 &derived_quantities["index_expected"];
1397
1398 derived_quantities["log_index_expected"] =
1399 fims::Vector<Type>(fleet_interface->n_years.get());
1400 derived_quantities_dim_info["log_index_expected"] =
1401 fims_popdy::DimensionInfo(
1402 "log_index_expected",
1403 fims::Vector<int>{(fleet_interface->n_years.get())},
1404 fims::Vector<std::string>{"n_years"});
1405 info->variable_map[fleet_interface->log_index_expected.id_m] =
1406 &derived_quantities["log_index_expected"];
1407
1408 derived_quantities["agecomp_expected"] = fims::Vector<Type>(
1409 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1410 derived_quantities_dim_info["agecomp_expected"] =
1411 fims_popdy::DimensionInfo(
1412 "agecomp_expected",
1413 fims::Vector<int>{(fleet_interface->n_years.get()),
1414 (fleet_interface->n_ages.get())},
1415 fims::Vector<std::string>{"n_years", "n_ages"});
1416 info->variable_map[fleet_interface->agecomp_expected.id_m] =
1417 &derived_quantities["agecomp_expected"];
1418
1419 derived_quantities["lengthcomp_expected"] = fims::Vector<Type>(
1420 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1421 derived_quantities_dim_info["lengthcomp_expected"] =
1422 fims_popdy::DimensionInfo(
1423 "lengthcomp_expected",
1424 fims::Vector<int>{(fleet_interface->n_years.get()),
1425 (fleet_interface->n_lengths.get())},
1426 fims::Vector<std::string>{"n_years", "n_lengths"});
1427 info->variable_map[fleet_interface->lengthcomp_expected.id_m] =
1428 &derived_quantities["lengthcomp_expected"];
1429 }
1430
1431 return true;
1432 }
1433
1434 virtual bool add_to_fims_tmb() {
1435 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
1436 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
1437 return true;
1438 }
1439
1440#endif
1441};
1442
1443#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:151
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_population.hpp:302
VariableVector log_init_naa
The natural log of the initial numbers at age.
Definition rcpp_population.hpp:142
SharedString name
The name for the population.
Definition rcpp_population.hpp:165
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:114
VariableVector log_M
The natural log of the natural mortality for each year.
Definition rcpp_population.hpp:130
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:138
RealVector ages
Ages that are modeled in the population, the length of this vector should equal "n_ages".
Definition rcpp_population.hpp:156
VariableVector spawning_biomass_ratio
The population spawning biomass ratio for each year.
Definition rcpp_population.hpp:134
SharedInt growth_id
The ID of the growth module.
Definition rcpp_population.hpp:118
SharedInt recruitment_id
The ID of the recruitment module.
Definition rcpp_population.hpp:122
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