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"
15#include "../../../models/fisheries_models.hpp"
16#include "common/model.hpp"
17#include "../../../utilities/fims_json.hpp"
18#include "rcpp_population.hpp"
19#include "rcpp_fleet.hpp"
20#include "rcpp_growth.hpp"
21#include "rcpp_distribution.hpp"
22#include "rcpp_data.hpp"
23#include "rcpp_maturity.hpp"
24#include "rcpp_recruitment.hpp"
25#include "rcpp_selectivity.hpp"
26#include <valarray>
27#include <cmath>
28#include <mutex>
29
37 protected:
41 std::shared_ptr<std::set<uint32_t>> population_ids;
45 typedef typename std::set<uint32_t>::iterator population_id_iterator;
46
47 public:
51 static uint32_t id_g;
55 uint32_t id;
61 static std::map<uint32_t, std::shared_ptr<FisheryModelInterfaceBase>>
63
69 this->population_ids = std::make_shared<std::set<uint32_t>>();
70 /* Create instance of map: key is id and value is pointer to
71 FleetInterfaceBase */
72 // FisheryModelInterfaceBase::live_objects[this->id] = this;
73 }
74
82
87
101 virtual std::string to_json() {
102 return "std::string to_json() not yet implemented.";
103 }
104
108 virtual uint32_t get_id() = 0;
109
117 Rcpp::NumericVector get_fixed_parameters_vector() {
118 std::shared_ptr<fims_info::Information<double>> info0 =
120
121 Rcpp::NumericVector p;
122
123 for (size_t i = 0; i < info0->fixed_effects_parameters.size(); i++) {
124 p.push_back(*info0->fixed_effects_parameters[i]);
125 }
126
127 return p;
128 }
129
137 Rcpp::NumericVector get_random_parameters_vector() {
138 std::shared_ptr<fims_info::Information<double>> d0 =
140
141 Rcpp::NumericVector p;
142
143 for (size_t i = 0; i < d0->random_effects_parameters.size(); i++) {
144 p.push_back(*d0->random_effects_parameters[i]);
145 }
146
147 return p;
148 }
149
156 double sum(const std::valarray<double> &v) {
157 double sum = 0.0;
158 for (size_t i = 0; i < v.size(); i++) {
159 sum += v[i];
160 }
161 return sum;
162 }
163
170 double sum(const std::vector<double> &v) {
171 double sum = 0.0;
172 for (size_t i = 0; i < v.size(); i++) {
173 sum += v[i];
174 }
175 return sum;
176 }
177
185 double min(const std::valarray<double> &v) {
186 double min_value = v[0];
187 for (size_t i = 1; i < v.size(); i++) {
188 if (v[i] < min_value) {
189 min_value = v[i];
190 }
191 }
192 return min_value;
193 }
194
203 std::valarray<double> fabs(const std::valarray<double> &v) {
204 std::valarray<double> result(v.size());
205 for (size_t i = 0; i < v.size(); i++) {
206 result[i] = std::fabs(v[i]);
207 }
208 return result;
209 }
210};
211
217 public:
222 std::shared_ptr<CatchAtAgeInterface> caa =
223 std::make_shared<CatchAtAgeInterface>(*this);
226 }
227
235
239 void AddPopulation(uint32_t id) {
240 this->population_ids->insert(id);
241
242 std::map<uint32_t, std::shared_ptr<PopulationInterfaceBase>>::iterator pit;
244 if (pit != PopulationInterfaceBase::live_objects.end()) {
245 std::shared_ptr<PopulationInterfaceBase> &pop = (*pit).second;
246 pop->initialize_catch_at_age.set(true);
247 } else {
248 FIMS_ERROR_LOG("Population with id " + fims::to_string(id) +
249 " not found.");
250 }
251 }
252
260 void DoReporting(bool report) {
261#ifdef TMB_MODEL
262 std::shared_ptr<fims_info::Information<double>> info =
265 model_it = info->models_map.find(this->get_id());
266 if (model_it != info->models_map.end()) {
267 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
268 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
269 (*model_it).second);
270 model_ptr->do_reporting = report;
271 }
272#endif
273 }
274
282 bool IsReporting() {
283#ifdef TMB_MODEL
284 std::shared_ptr<fims_info::Information<double>> info =
287 model_it = info->models_map.find(this->get_id());
288 if (model_it != info->models_map.end()) {
289 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
290 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
291 (*model_it).second);
292 return model_ptr->do_reporting;
293 }
294 return false;
295#else
296 return false;
297#endif
298 }
299
306#ifdef TMB_MODEL
307 std::shared_ptr<fims_info::Information<double>> info =
310 model_it = info->models_map.find(this->get_id());
311 if (model_it != info->models_map.end()) {
312 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
313 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
314 (*model_it).second);
315 model_ptr->report_age_to_length_conversion_derived_tensor = report;
316 }
317#endif
318 }
319
323 virtual uint32_t get_id() { return this->id; }
324
328 virtual void finalize() {}
329
333 std::string population_to_json(PopulationInterface *population_interface) {
334 std::stringstream ss;
335
336 typename std::map<uint32_t,
337 std::shared_ptr<PopulationInterfaceBase>>::iterator
338 pi_it; // population interface iterator
340 population_interface->get_id());
341 if (pi_it == PopulationInterfaceBase::live_objects.end()) {
342 FIMS_ERROR_LOG("Population with id " +
343 fims::to_string(population_interface->get_id()) +
344 " not found in live objects.");
345 return "{}"; // Return empty JSON
346 }
347
348 std::shared_ptr<PopulationInterface> population_interface_ptr =
349 std::dynamic_pointer_cast<PopulationInterface>((*pi_it).second);
350
351 std::shared_ptr<fims_info::Information<double>> info =
353
355 model_it = info->models_map.find(this->get_id());
356 std::shared_ptr<fims_popdy::CatchAtAge<double>> model_ptr =
357 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
358 (*model_it).second);
359
361
362 pit = info->populations.find(population_interface->get_id());
363
364 if (pit != info->populations.end()) {
365 std::shared_ptr<fims_popdy::Population<double>> &pop = (*pit).second;
366 ss << "{\n";
367
368 ss << " \"module_name\": \"Population\",\n";
369 ss << " \"population\": \""
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 ss << " \"header\": [\"" << "na" << "\"],\n";
695 ss << " \"dimensions\": [" << fleet->log_q.size() << "]\n},\n";
696
697 ss << " \"values\": " << fleet_interface->log_q << "}\n";
698
699 ss << "], \"derived_quantities\": [";
700
701 std::map<std::string, fims::Vector<double>> dqs =
702 model_ptr->GetFleetDerivedQuantities(fleet_interface->get_id());
703 std::map<std::string, fims_popdy::DimensionInfo> dim_info =
704 model_ptr->GetFleetDimensionInfo(fleet_interface->get_id());
705 ss << this->derived_quantities_component_to_json(dqs, dim_info) << "]}\n";
706 } else {
707 ss << "{\n";
708 ss << " \"name\": \"Fleet\",\n";
709 ss << " \"type\": \"fleet\",\n";
710 ss << " \"tag\": \"" << fleet_interface->get_id()
711 << " not found in Information.\",\n";
712 ss << " \"derived_quantities\": []}\n";
713 }
714 return ss.str();
715 }
716
720 virtual std::string to_json() {
721 std::set<uint32_t> recruitment_ids;
722 std::set<uint32_t> growth_ids;
723 std::set<uint32_t> maturity_ids;
724 std::set<uint32_t> selectivity_ids;
725 std::set<uint32_t> fleet_ids;
726 // gather sub-module info from population and fleets
727 typename std::set<uint32_t>::iterator module_id_it; // generic
728 typename std::set<uint32_t>::iterator pit;
729 typename std::set<uint32_t>::iterator fids;
730 for (pit = this->population_ids->begin();
731 pit != this->population_ids->end(); pit++) {
732 std::shared_ptr<PopulationInterface> population_interface =
733 std::dynamic_pointer_cast<PopulationInterface>(
735 if (population_interface) {
736 recruitment_ids.insert(population_interface->recruitment_id.get());
737 growth_ids.insert(population_interface->growth_id.get());
738 maturity_ids.insert(population_interface->maturity_id.get());
739
740 for (fids = population_interface->fleet_ids->begin();
741 fids != population_interface->fleet_ids->end(); fids++) {
742 fleet_ids.insert(*fids);
743 }
744 }
745 }
746
747 for (fids = fleet_ids.begin(); fids != fleet_ids.end(); fids++) {
748 std::shared_ptr<FleetInterface> fleet_interface =
749 std::dynamic_pointer_cast<FleetInterface>(
751 if (fleet_interface) {
752 selectivity_ids.insert(fleet_interface->GetSelectivityID());
753 }
754 }
755
756 std::shared_ptr<fims_info::Information<double>> info =
758
759 std::shared_ptr<fims_popdy::CatchAtAge<double>> model =
760 std::dynamic_pointer_cast<fims_popdy::CatchAtAge<double>>(
761 info->models_map[this->get_id()]);
762
763 std::shared_ptr<fims_model::Model<double>> model_internal =
765
766#ifdef TMB_MODEL
767 model->do_reporting = false;
768#endif
769
770 double value = model_internal->Evaluate();
771
772 std::stringstream ss;
773
774 ss.str("");
775
776 ss << "{\n";
777 ss << " \"name\": \"CatchAtAge\",\n";
778 ss << " \"type\": \"model\",\n";
779 ss << " \"estimation_framework\": ";
780#ifdef TMB_MODEL
781 ss << "\"Template_Model_Builder (TMB)\",";
782#else
783 ss << "\"FIMS\",";
784#endif
785 ss << " \"id\": " << this->get_id() << ",\n";
786 ss << " \"objective_function_value\": " << sanitize_val(value) << ",\n";
787 ss << "\"growth\":[\n";
788 for (module_id_it = growth_ids.begin(); module_id_it != growth_ids.end();
789 module_id_it++) {
790 std::shared_ptr<GrowthInterfaceBase> growth_interface =
792
793 if (growth_interface != NULL) {
794 growth_interface->finalize();
795 ss << growth_interface->to_json();
796 if (std::next(module_id_it) != growth_ids.end()) {
797 ss << ", ";
798 }
799 }
800 }
801
802 ss << "],\n";
803
804 ss << "\"recruitment\": [\n";
805 for (module_id_it = recruitment_ids.begin();
806 module_id_it != recruitment_ids.end(); module_id_it++) {
807 std::shared_ptr<RecruitmentInterfaceBase> recruitment_interface =
809 if (recruitment_interface) {
810 recruitment_interface->finalize();
811 ss << recruitment_interface->to_json();
812 if (std::next(module_id_it) != recruitment_ids.end()) {
813 ss << ", ";
814 }
815 }
816 }
817 ss << "],\n";
818
819 ss << "\"maturity\": [\n";
820 for (module_id_it = maturity_ids.begin();
821 module_id_it != maturity_ids.end(); module_id_it++) {
822 std::shared_ptr<MaturityInterfaceBase> maturity_interface =
824 if (maturity_interface) {
825 maturity_interface->finalize();
826 ss << maturity_interface->to_json();
827 if (std::next(module_id_it) != maturity_ids.end()) {
828 ss << ", ";
829 }
830 }
831 }
832 ss << "],\n";
833
834 ss << "\"selectivity\": [\n";
835 for (module_id_it = selectivity_ids.begin();
836 module_id_it != selectivity_ids.end(); module_id_it++) {
837 std::shared_ptr<SelectivityInterfaceBase> selectivity_interface =
839 if (selectivity_interface) {
840 selectivity_interface->finalize();
841 ss << selectivity_interface->to_json();
842 if (std::next(module_id_it) != selectivity_ids.end()) {
843 ss << ", ";
844 }
845 }
846 }
847 ss << "],\n";
848
849 ss << " \"population_ids\": [";
850 for (pit = this->population_ids->begin();
851 pit != this->population_ids->end(); pit++) {
852 ss << *pit;
853 if (std::next(pit) != this->population_ids->end()) {
854 ss << ", ";
855 }
856 }
857 ss << "],\n";
858 ss << " \"fleet_ids\": [";
859
860 for (fids = fleet_ids.begin(); fids != fleet_ids.end(); fids++) {
861 ss << *fids;
862 if (std::next(fids) != fleet_ids.end()) {
863 ss << ", ";
864 }
865 }
866 ss << "],\n";
867 ss << "\"populations\": [\n";
868 typename std::set<uint32_t>::iterator pop_it;
869 typename std::set<uint32_t>::iterator pop_end_it;
870 pop_end_it = this->population_ids->end();
871 typename std::set<uint32_t>::iterator pop_second_to_last_it;
872 if (pop_end_it != this->population_ids->begin()) {
873 pop_second_to_last_it = std::prev(pop_end_it);
874 } else {
875 pop_second_to_last_it = pop_end_it;
876 }
877 for (pop_it = this->population_ids->begin();
878 pop_it != pop_second_to_last_it; pop_it++) {
879 std::shared_ptr<PopulationInterface> population_interface =
880 std::dynamic_pointer_cast<PopulationInterface>(
882 if (population_interface) {
883 std::set<uint32_t>::iterator fids;
884 for (fids = population_interface->fleet_ids->begin();
885 fids != population_interface->fleet_ids->end(); fids++) {
886 fleet_ids.insert(*fids);
887 }
888 population_interface->finalize();
889 ss << this->population_to_json(population_interface.get()) << ",";
890 } else {
891 FIMS_ERROR_LOG("Population with id " + fims::to_string(*pop_it) +
892 " not found in live objects.");
893 ss << "{}"; // Return empty JSON for this population
894 }
895 }
896
897 std::shared_ptr<PopulationInterface> population_interface =
898 std::dynamic_pointer_cast<PopulationInterface>(
899 PopulationInterfaceBase::live_objects[*pop_second_to_last_it]);
900 if (population_interface) {
901 std::set<uint32_t>::iterator fids;
902 for (fids = population_interface->fleet_ids->begin();
903 fids != population_interface->fleet_ids->end(); fids++) {
904 fleet_ids.insert(*fids);
905 }
906 ss << this->population_to_json(population_interface.get());
907 } else {
908 FIMS_ERROR_LOG("Population with id " + fims::to_string(*pop_it) +
909 " not found in live objects.");
910 ss << "{}"; // Return empty JSON for this population
911 }
912
913 ss << "]";
914 ss << ",\n";
915 ss << "\"fleets\": [\n";
916
917 typename std::set<uint32_t>::iterator fleet_it;
918 typename std::set<uint32_t>::iterator fleet_end_it;
919 fleet_end_it = fleet_ids.end();
920 typename std::set<uint32_t>::iterator fleet_second_to_last_it;
921
922 if (fleet_end_it != fleet_ids.begin()) {
923 fleet_second_to_last_it = std::prev(fleet_end_it);
924 }
925 for (fleet_it = fleet_ids.begin(); fleet_it != fleet_second_to_last_it;
926 fleet_it++) {
927 std::shared_ptr<FleetInterface> fleet_interface =
928 std::dynamic_pointer_cast<FleetInterface>(
930 if (fleet_interface) {
931 fleet_interface->finalize();
932 ss << this->fleet_to_json(fleet_interface.get()) << ",";
933 } else {
934 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*fleet_it) +
935 " not found in live objects.");
936 ss << "{}"; // Return empty JSON for this fleet
937 }
938 }
939 std::shared_ptr<FleetInterface> fleet_interface =
940 std::dynamic_pointer_cast<FleetInterface>(
941 FleetInterfaceBase::live_objects[*fleet_second_to_last_it]);
942 if (fleet_interface) {
943 ss << this->fleet_to_json(fleet_interface.get());
944 } else {
945 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*fleet_it) +
946 " not found in live objects.");
947 ss << "{}"; // Return empty JSON for this fleet
948 }
949
950 ss << "],\n";
951
952 ss << "\"density_components\": [\n";
953
954 typename std::map<
955 uint32_t, std::shared_ptr<DistributionsInterfaceBase>>::iterator dit;
957 dit != DistributionsInterfaceBase::live_objects.end(); ++dit) {
958 std::shared_ptr<DistributionsInterfaceBase> dist_interface =
959 (*dit).second;
960 if (dist_interface) {
961 dist_interface->finalize();
962 ss << dist_interface->to_json();
963 if (std::next(dit) != DistributionsInterfaceBase::live_objects.end()) {
964 ss << ",\n";
965 }
966 }
967 }
968 ss << "\n],\n";
969 ss << "\"data\": [\n";
970 typename std::map<uint32_t, std::shared_ptr<DataInterfaceBase>>::iterator
971 d_it;
972 for (d_it = DataInterfaceBase::live_objects.begin();
973 d_it != DataInterfaceBase::live_objects.end(); ++d_it) {
974 std::shared_ptr<DataInterfaceBase> data_interface = (*d_it).second;
975 if (data_interface) {
976 data_interface->finalize();
977 ss << data_interface->to_json();
978 if (std::next(d_it) != DataInterfaceBase::live_objects.end()) {
979 ss << ",\n";
980 }
981 }
982 }
983 ss << "\n]\n";
984 ss << "}\n";
985#ifdef TMB_MODEL
986 model->do_reporting = true;
987#endif
988 return fims::JsonParser::PrettyFormatJSON(ss.str());
989 }
990
991#ifdef TMB_MODEL
992
993 template <typename Type>
994 bool add_to_fims_tmb_internal() {
995 std::shared_ptr<fims_info::Information<Type>> info =
997
998 std::shared_ptr<fims_popdy::CatchAtAge<Type>> model =
999 std::make_shared<fims_popdy::CatchAtAge<Type>>();
1000
1002
1003 for (it = this->population_ids->begin(); it != this->population_ids->end();
1004 ++it) {
1005 model->AddPopulation((*it));
1006 }
1007
1008 std::set<uint32_t> fleet_ids; // all fleets in the model
1009 typedef typename std::set<uint32_t>::iterator fleet_ids_iterator;
1010
1011 // add to Information
1012 info->models_map[this->get_id()] = model;
1013
1014 for (it = this->population_ids->begin(); it != this->population_ids->end();
1015 ++it) {
1016 auto population_interface_it =
1018 if (population_interface_it ==
1020 FIMS_ERROR_LOG("Population with id " + fims::to_string(*it) +
1021 " not found in live objects.");
1022 continue;
1023 }
1024
1025 std::shared_ptr<PopulationInterface> population_interface =
1026 std::dynamic_pointer_cast<PopulationInterface>(
1027 population_interface_it->second);
1028 if (!population_interface) {
1029 FIMS_ERROR_LOG("Population with id " + fims::to_string(*it) +
1030 " not found in live objects.");
1031 continue;
1032 }
1033
1034 model->InitializePopulationDerivedQuantities(population_interface->id);
1035 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1036 model->GetPopulationDerivedQuantities(population_interface->id);
1037
1038 std::map<std::string, fims_popdy::DimensionInfo>
1039 &derived_quantities_dim_info =
1040 model->GetPopulationDimensionInfo(population_interface->id);
1041
1042 std::stringstream ss;
1043
1044 derived_quantities["total_catch_weight"] =
1045 fims::Vector<Type>(population_interface->n_years.get());
1046
1047 derived_quantities_dim_info["total_catch_weight"] =
1049 "total_catch_weight",
1050 fims::Vector<int>{(int)population_interface->n_years.get()},
1051 fims::Vector<std::string>{"n_years"});
1052 info->variable_map[population_interface->total_catch_weight.id_m] =
1053 &derived_quantities["total_catch_weight"];
1054
1055 derived_quantities["total_catch_numbers"] =
1056 fims::Vector<Type>(population_interface->n_years.get());
1057
1058 derived_quantities_dim_info["total_catch_numbers"] =
1060 "total_catch_numbers",
1061 fims::Vector<int>{population_interface->n_years.get()},
1062 fims::Vector<std::string>{"n_years"});
1063 info->variable_map[population_interface->total_catch_numbers.id_m] =
1064 &derived_quantities["total_catch_numbers"];
1065
1066 derived_quantities["mortality_F"] =
1067 fims::Vector<Type>(population_interface->n_years.get() *
1068 population_interface->n_ages.get());
1069 derived_quantities_dim_info["mortality_F"] = fims_popdy::DimensionInfo(
1070 "mortality_F",
1071 fims::Vector<int>{population_interface->n_years.get(),
1072 population_interface->n_ages.get()},
1073 fims::Vector<std::string>{"n_years", "n_ages"});
1074 info->variable_map[population_interface->mortality_F.id_m] =
1075 &derived_quantities["mortality_F"];
1076
1077 derived_quantities["mortality_M"] =
1078 fims::Vector<Type>(population_interface->n_years.get() *
1079 population_interface->n_ages.get());
1080 derived_quantities_dim_info["mortality_M"] = fims_popdy::DimensionInfo(
1081 "mortality_M",
1082 fims::Vector<int>{population_interface->n_years.get(),
1083 population_interface->n_ages.get()},
1084 fims::Vector<std::string>{"n_years", "n_ages"});
1085 info->variable_map[population_interface->mortality_M.id_m] =
1086 &derived_quantities["mortality_M"];
1087
1088 derived_quantities["mortality_Z"] =
1089 fims::Vector<Type>(population_interface->n_years.get() *
1090 population_interface->n_ages.get());
1091 derived_quantities_dim_info["mortality_Z"] = fims_popdy::DimensionInfo(
1092 "mortality_Z",
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_Z.id_m] =
1097 &derived_quantities["mortality_Z"];
1098
1099 derived_quantities["numbers_at_age"] =
1100 fims::Vector<Type>((population_interface->n_years.get() + 1) *
1101 population_interface->n_ages.get());
1102 derived_quantities_dim_info["numbers_at_age"] = fims_popdy::DimensionInfo(
1103 "numbers_at_age",
1104 fims::Vector<int>{(population_interface->n_years.get() + 1),
1105 population_interface->n_ages.get()},
1106 fims::Vector<std::string>{"n_years+1", "n_ages"});
1107 info->variable_map[population_interface->numbers_at_age.id_m] =
1108 &derived_quantities["numbers_at_age"];
1109
1110 derived_quantities["unfished_numbers_at_age"] =
1111 fims::Vector<Type>((population_interface->n_years.get() + 1) *
1112 population_interface->n_ages.get());
1113 derived_quantities_dim_info["unfished_numbers_at_age"] =
1115 "unfished_numbers_at_age",
1116 fims::Vector<int>{(population_interface->n_years.get() + 1),
1117 population_interface->n_ages.get()},
1118 fims::Vector<std::string>{"n_years+1", "n_ages"});
1119 info->variable_map[population_interface->unfished_numbers_at_age.id_m] =
1120 &derived_quantities["unfished_numbers_at_age"];
1121
1122 derived_quantities["biomass"] =
1123 fims::Vector<Type>((population_interface->n_years.get() + 1));
1124 derived_quantities_dim_info["biomass"] = fims_popdy::DimensionInfo(
1125 "biomass",
1126 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1127 fims::Vector<std::string>{"n_years+1"});
1128 info->variable_map[population_interface->biomass.id_m] =
1129 &derived_quantities["biomass"];
1130
1131 derived_quantities["spawning_biomass"] =
1132 fims::Vector<Type>((population_interface->n_years.get() + 1));
1133 derived_quantities_dim_info["spawning_biomass"] =
1135 "spawning_biomass",
1136 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1137 fims::Vector<std::string>{"n_years+1"});
1138 info->variable_map[population_interface->spawning_biomass.id_m] =
1139 &derived_quantities["spawning_biomass"];
1140
1141 derived_quantities["unfished_biomass"] =
1142 fims::Vector<Type>((population_interface->n_years.get() + 1));
1143 derived_quantities_dim_info["unfished_biomass"] =
1145 "unfished_biomass",
1146 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1147 fims::Vector<std::string>{"n_years+1"});
1148 info->variable_map[population_interface->unfished_biomass.id_m] =
1149 &derived_quantities["unfished_biomass"];
1150
1151 derived_quantities["unfished_spawning_biomass"] =
1152 fims::Vector<Type>((population_interface->n_years.get() + 1));
1153 derived_quantities_dim_info["unfished_spawning_biomass"] =
1155 "unfished_spawning_biomass",
1156 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1157 fims::Vector<std::string>{"n_years+1"});
1158 info->variable_map[population_interface->unfished_spawning_biomass.id_m] =
1159 &derived_quantities["unfished_spawning_biomass"];
1160
1161 derived_quantities["proportion_mature_at_age"] =
1162 fims::Vector<Type>((population_interface->n_years.get() + 1) *
1163 population_interface->n_ages.get());
1164 derived_quantities_dim_info["proportion_mature_at_age"] =
1166 "proportion_mature_at_age",
1167 fims::Vector<int>{(population_interface->n_years.get() + 1),
1168 population_interface->n_ages.get()},
1169 fims::Vector<std::string>{"n_years+1", "n_ages"});
1170 info->variable_map[population_interface->proportion_mature_at_age.id_m] =
1171 &derived_quantities["proportion_mature_at_age"];
1172
1173 derived_quantities["expected_recruitment"] =
1174 fims::Vector<Type>((population_interface->n_years.get() + 1));
1175 derived_quantities_dim_info["expected_recruitment"] =
1177 "expected_recruitment",
1178 fims::Vector<int>{(population_interface->n_years.get() + 1)},
1179 fims::Vector<std::string>{"n_years+1"});
1180 info->variable_map[population_interface->expected_recruitment.id_m] =
1181 &derived_quantities["expected_recruitment"];
1182
1183 derived_quantities["sum_selectivity"] =
1184 fims::Vector<Type>(population_interface->n_years.get() *
1185 population_interface->n_ages.get());
1186 derived_quantities_dim_info["sum_selectivity"] =
1188 "sum_selectivity",
1189 fims::Vector<int>{population_interface->n_years.get(),
1190 population_interface->n_ages.get()},
1191 fims::Vector<std::string>{"n_years", "n_ages"});
1192 info->variable_map[population_interface->sum_selectivity.id_m] =
1193 &derived_quantities["sum_selectivity"];
1194
1195 // replace elements in the variable map
1196
1197 for (fleet_ids_iterator fit = population_interface->fleet_ids->begin();
1198 fit != population_interface->fleet_ids->end(); ++fit) {
1199 fleet_ids.insert(*fit);
1200 }
1201 }
1202
1203 for (fleet_ids_iterator it = fleet_ids.begin(); it != fleet_ids.end();
1204 ++it) {
1205 auto fleet_interface_it = FleetInterfaceBase::live_objects.find(*it);
1206 if (fleet_interface_it == FleetInterfaceBase::live_objects.end()) {
1207 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*it) +
1208 " not found in live objects.");
1209 continue;
1210 }
1211
1212 std::shared_ptr<FleetInterface> fleet_interface =
1213 std::dynamic_pointer_cast<FleetInterface>(fleet_interface_it->second);
1214 if (!fleet_interface) {
1215 FIMS_ERROR_LOG("Fleet with id " + fims::to_string(*it) +
1216 " not found in live objects.");
1217 continue;
1218 }
1219 model->InitializeFleetDerivedQuantities(fleet_interface->id);
1220 std::map<std::string, fims::Vector<Type>> &derived_quantities =
1221 model->GetFleetDerivedQuantities(fleet_interface->id);
1222
1223 std::map<std::string, fims_popdy::DimensionInfo>
1224 &derived_quantities_dim_info =
1225 model->GetFleetDimensionInfo(fleet_interface->id);
1226
1227 // initialize derive quantities
1228 // catch
1229 derived_quantities["catch_numbers_at_age"] = fims::Vector<Type>(
1230 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1231 derived_quantities_dim_info["catch_numbers_at_age"] =
1233 "catch_numbers_at_age",
1234 fims::Vector<int>{(fleet_interface->n_years.get()),
1235 fleet_interface->n_ages.get()},
1236 fims::Vector<std::string>{"n_years", "n_ages"});
1237 info->variable_map[fleet_interface->catch_numbers_at_age.id_m] =
1238 &derived_quantities["catch_numbers_at_age"];
1239
1240 derived_quantities["catch_weight_at_age"] = fims::Vector<Type>(
1241 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1242 derived_quantities_dim_info["catch_weight_at_age"] =
1244 "catch_weight_at_age",
1245 fims::Vector<int>{(fleet_interface->n_years.get()),
1246 fleet_interface->n_ages.get()},
1247 fims::Vector<std::string>{"n_years", "n_ages"});
1248 info->variable_map[fleet_interface->catch_weight_at_age.id_m] =
1249 &derived_quantities["catch_weight_at_age"];
1250
1251 derived_quantities["catch_numbers_at_length"] = fims::Vector<Type>(
1252 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1253 derived_quantities_dim_info["catch_numbers_at_length"] =
1255 "catch_numbers_at_length",
1256 fims::Vector<int>{(fleet_interface->n_years.get()),
1257 fleet_interface->n_lengths.get()},
1258 fims::Vector<std::string>{"n_years", "n_lengths"});
1259 info->variable_map[fleet_interface->catch_numbers_at_length.id_m] =
1260 &derived_quantities["catch_numbers_at_length"];
1261
1262 derived_quantities["catch_weight"] =
1263 fims::Vector<Type>(fleet_interface->n_years.get());
1264 derived_quantities_dim_info["catch_weight"] = fims_popdy::DimensionInfo(
1265 "catch_weight", fims::Vector<int>{(fleet_interface->n_years.get())},
1266 fims::Vector<std::string>{"n_years"});
1267 info->variable_map[fleet_interface->catch_weight.id_m] =
1268 &derived_quantities["catch_weight"];
1269
1270 derived_quantities["catch_numbers"] =
1271 fims::Vector<Type>(fleet_interface->n_years.get());
1272 derived_quantities_dim_info["catch_numbers"] = fims_popdy::DimensionInfo(
1273 "catch_numbers", fims::Vector<int>{(fleet_interface->n_years.get())},
1274 fims::Vector<std::string>{"n_years"});
1275 info->variable_map[fleet_interface->catch_numbers.id_m] =
1276 &derived_quantities["catch_numbers"];
1277
1278 derived_quantities["catch_expected"] =
1279 fims::Vector<Type>(fleet_interface->n_years.get());
1280 derived_quantities_dim_info["catch_expected"] = fims_popdy::DimensionInfo(
1281 "catch_expected", fims::Vector<int>{(fleet_interface->n_years.get())},
1282 fims::Vector<std::string>{"n_years"});
1283 info->variable_map[fleet_interface->catch_expected.id_m] =
1284 &derived_quantities["catch_expected"];
1285
1286 derived_quantities["log_catch_expected"] =
1287 fims::Vector<Type>(fleet_interface->n_years.get());
1288 derived_quantities_dim_info["log_catch_expected"] =
1290 "log_catch_expected",
1291 fims::Vector<int>{(fleet_interface->n_years.get())},
1292 fims::Vector<std::string>{"n_years"});
1293 info->variable_map[fleet_interface->log_catch_expected.id_m] =
1294 &derived_quantities["log_catch_expected"];
1295
1296 derived_quantities["agecomp_proportion"] = fims::Vector<Type>(
1297 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1298 derived_quantities_dim_info["agecomp_proportion"] =
1300 "agecomp_proportion",
1301 fims::Vector<int>{(fleet_interface->n_years.get()),
1302 fleet_interface->n_ages.get()},
1303 fims::Vector<std::string>{"n_years", "n_ages"});
1304 info->variable_map[fleet_interface->agecomp_proportion.id_m] =
1305 &derived_quantities["agecomp_proportion"];
1306
1307 derived_quantities["lengthcomp_proportion"] = fims::Vector<Type>(
1308 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1309 derived_quantities_dim_info["lengthcomp_proportion"] =
1311 "lengthcomp_proportion",
1312 fims::Vector<int>{(fleet_interface->n_years.get()),
1313 fleet_interface->n_lengths.get()},
1314 fims::Vector<std::string>{"n_years", "n_lengths"});
1315 info->variable_map[fleet_interface->lengthcomp_proportion.id_m] =
1316 &derived_quantities["lengthcomp_proportion"];
1317
1318 // index
1319 derived_quantities["index_numbers_at_age"] = fims::Vector<Type>(
1320 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1321 derived_quantities_dim_info["index_numbers_at_age"] =
1323 "index_numbers_at_age",
1324 fims::Vector<int>{(fleet_interface->n_years.get()),
1325 fleet_interface->n_ages.get()},
1326 fims::Vector<std::string>{"n_years", "n_ages"});
1327 info->variable_map[fleet_interface->index_numbers_at_age.id_m] =
1328 &derived_quantities["index_numbers_at_age"];
1329
1330 derived_quantities["index_weight_at_age"] = fims::Vector<Type>(
1331 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1332 derived_quantities_dim_info["index_weight_at_age"] =
1334 "index_weight_at_age",
1335 fims::Vector<int>{(fleet_interface->n_years.get()),
1336 fleet_interface->n_ages.get()},
1337 fims::Vector<std::string>{"n_years", "n_ages"});
1338 info->variable_map[fleet_interface->index_weight_at_age.id_m] =
1339 &derived_quantities["index_weight_at_age"];
1340
1341 derived_quantities["index_numbers_at_length"] = fims::Vector<Type>(
1342 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1343 derived_quantities_dim_info["index_numbers_at_length"] =
1345 "index_numbers_at_length",
1346 fims::Vector<int>{(fleet_interface->n_years.get()),
1347 fleet_interface->n_lengths.get()},
1348 fims::Vector<std::string>{"n_years", "n_lengths"});
1349 info->variable_map[fleet_interface->index_numbers_at_length.id_m] =
1350 &derived_quantities["index_numbers_at_length"];
1351
1352 derived_quantities["index_weight"] =
1353 fims::Vector<Type>(fleet_interface->n_years.get());
1354 derived_quantities_dim_info["index_weight"] = fims_popdy::DimensionInfo(
1355 "index_weight", fims::Vector<int>{(fleet_interface->n_years.get())},
1356 fims::Vector<std::string>{"n_years"});
1357 info->variable_map[fleet_interface->index_weight.id_m] =
1358 &derived_quantities["index_weight"];
1359
1360 derived_quantities["index_numbers"] =
1361 fims::Vector<Type>(fleet_interface->n_years.get());
1362 derived_quantities_dim_info["index_numbers"] = fims_popdy::DimensionInfo(
1363 "index_numbers", fims::Vector<int>{(fleet_interface->n_years.get())},
1364 fims::Vector<std::string>{"n_years"});
1365 info->variable_map[fleet_interface->index_numbers.id_m] =
1366 &derived_quantities["index_numbers"];
1367
1368 derived_quantities["index_expected"] =
1369 fims::Vector<Type>(fleet_interface->n_years.get());
1370 derived_quantities_dim_info["index_expected"] = fims_popdy::DimensionInfo(
1371 "index_expected", fims::Vector<int>{(fleet_interface->n_years.get())},
1372 fims::Vector<std::string>{"n_years"});
1373 info->variable_map[fleet_interface->index_expected.id_m] =
1374 &derived_quantities["index_expected"];
1375
1376 derived_quantities["log_index_expected"] =
1377 fims::Vector<Type>(fleet_interface->n_years.get());
1378 derived_quantities_dim_info["log_index_expected"] =
1380 "log_index_expected",
1381 fims::Vector<int>{(fleet_interface->n_years.get())},
1382 fims::Vector<std::string>{"n_years"});
1383 info->variable_map[fleet_interface->log_index_expected.id_m] =
1384 &derived_quantities["log_index_expected"];
1385
1386 derived_quantities["agecomp_expected"] = fims::Vector<Type>(
1387 fleet_interface->n_years.get() * fleet_interface->n_ages.get());
1388 derived_quantities_dim_info["agecomp_expected"] =
1390 "agecomp_expected",
1391 fims::Vector<int>{(fleet_interface->n_years.get()),
1392 (fleet_interface->n_ages.get())},
1393 fims::Vector<std::string>{"n_years", "n_ages"});
1394 info->variable_map[fleet_interface->agecomp_expected.id_m] =
1395 &derived_quantities["agecomp_expected"];
1396
1397 derived_quantities["lengthcomp_expected"] = fims::Vector<Type>(
1398 fleet_interface->n_years.get() * fleet_interface->n_lengths.get());
1399 derived_quantities_dim_info["lengthcomp_expected"] =
1401 "lengthcomp_expected",
1402 fims::Vector<int>{(fleet_interface->n_years.get()),
1403 (fleet_interface->n_lengths.get())},
1404 fims::Vector<std::string>{"n_years", "n_lengths"});
1405 info->variable_map[fleet_interface->lengthcomp_expected.id_m] =
1406 &derived_quantities["lengthcomp_expected"];
1407 }
1408
1409 return true;
1410 }
1411
1412 virtual bool add_to_fims_tmb() {
1413 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
1414 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
1415 return true;
1416 }
1417
1418#endif
1419};
1420
1421#endif
The CatchAtAgeInterface class is used to interface with the CatchAtAge model. It inherits from the Fi...
Definition rcpp_models.hpp:216
virtual std::string to_json()
Serialize the fishery model to a JSON string.
Definition rcpp_models.hpp:720
void AddPopulation(uint32_t id)
Definition rcpp_models.hpp:239
std::string derived_quantity_to_json(std::map< std::string, fims::Vector< double > >::iterator it, const fims_popdy::DimensionInfo &dim_info)
Definition rcpp_models.hpp:501
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_models.hpp:328
CatchAtAgeInterface()
The constructor.
Definition rcpp_models.hpp:221
void DoReporting(bool report)
Enable or disable reporting for the CatchAtAge model.
Definition rcpp_models.hpp:260
void ReportAgeToLengthConversionDerivedTensor(bool report)
Turn reporting of the Growth-derived age-to-length tensor on or off.
Definition rcpp_models.hpp:305
virtual uint32_t get_id()
Method to get this id.
Definition rcpp_models.hpp:323
std::string fleet_to_json(FleetInterface *fleet_interface)
Method to convert a fleet to a JSON string.
Definition rcpp_models.hpp:613
std::string derived_quantities_component_to_json(std::map< std::string, fims::Vector< double > > &dqs, std::map< std::string, fims_popdy::DimensionInfo > &dim_info)
Send the fleet-based derived quantities to the json file.
Definition rcpp_models.hpp:578
std::string population_to_json(PopulationInterface *population_interface)
Method to convert a population to a JSON string.
Definition rcpp_models.hpp:333
CatchAtAgeInterface(const CatchAtAgeInterface &other)
Construct a new Catch At Age Interface object.
Definition rcpp_models.hpp:233
bool IsReporting()
Check if reporting is enabled for the CatchAtAge model.
Definition rcpp_models.hpp:282
static std::map< uint32_t, std::shared_ptr< DataInterfaceBase > > live_objects
The map associating the IDs of DataInterfaceBase to the objects. This is a live object,...
Definition rcpp_data.hpp:43
static std::map< uint32_t, std::shared_ptr< DistributionsInterfaceBase > > live_objects
The map associating the ID of the DistributionsInterfaceBase to the DistributionsInterfaceBase object...
Definition rcpp_distribution.hpp:72
Base class for all interface objects.
Definition rcpp_interface_base.hpp:655
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:665
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:670
The FisheryModelInterfaceBase class is the base class for all fishery models in the FIMS Rcpp interfa...
Definition rcpp_models.hpp:36
double min(const std::valarray< double > &v)
Minimum method to calculate the minimum of an array or vector of doubles.
Definition rcpp_models.hpp:185
FisheryModelInterfaceBase(const FisheryModelInterfaceBase &other)
Construct a new Data Interface Base object.
Definition rcpp_models.hpp:80
static uint32_t id_g
The static id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:51
virtual uint32_t get_id()=0
Get the ID for the child fleet interface objects to inherit.
FisheryModelInterfaceBase()
The constructor.
Definition rcpp_models.hpp:67
std::set< uint32_t >::iterator population_id_iterator
Iterator for population ids.
Definition rcpp_models.hpp:45
uint32_t id
The local id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:55
std::valarray< double > fabs(const std::valarray< double > &v)
A function to compute the absolute value of a value array of floating-point values....
Definition rcpp_models.hpp:203
virtual std::string to_json()
Serialize the fishery model to a JSON string.
Definition rcpp_models.hpp:101
static std::map< uint32_t, std::shared_ptr< FisheryModelInterfaceBase > > live_objects
The map associating the IDs of FleetInterfaceBase to the objects. This is a live object,...
Definition rcpp_models.hpp:62
double sum(const std::valarray< double > &v)
Sum method to calculate the sum of an array or vector of doubles.
Definition rcpp_models.hpp:156
Rcpp::NumericVector get_random_parameters_vector()
Get the vector of random effect parameters for the model.
Definition rcpp_models.hpp:137
Rcpp::NumericVector get_fixed_parameters_vector()
Get the vector of fixed effect parameters for the model.
Definition rcpp_models.hpp:117
std::shared_ptr< std::set< uint32_t > > population_ids
The set of population ids that this fishery model operates on.
Definition rcpp_models.hpp:41
virtual ~FisheryModelInterfaceBase()
The destructor.
Definition rcpp_models.hpp:86
double sum(const std::vector< double > &v)
Sum method for a vector of doubles.
Definition rcpp_models.hpp:170
uint32_t id
The local id of the FleetInterfaceBase object.
Definition rcpp_fleet.hpp:28
static std::map< uint32_t, std::shared_ptr< FleetInterfaceBase > > live_objects
The map associating the IDs of FleetInterfaceBase to the objects. This is a live object,...
Definition rcpp_fleet.hpp:34
The Rcpp interface for Fleet to instantiate from R: fleet <- methods::new(Fleet)
Definition rcpp_fleet.hpp:68
SharedInt n_years
The number of years in the fleet data.
Definition rcpp_fleet.hpp:124
uint32_t GetSelectivityID()
Get the unique ID for the selectivity object.
Definition rcpp_fleet.hpp:374
int GetObservedIndexDataID()
Get the unique id for the observed index data object.
Definition rcpp_fleet.hpp:408
int GetObservedCatchDataID()
Get the unique id for the observed catch data object.
Definition rcpp_fleet.hpp:415
int GetObservedAgeCompDataID()
Get the unique ID for the observed age-composition data object.
Definition rcpp_fleet.hpp:379
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_fleet.hpp:300
int GetObservedLengthCompDataID()
Get the unique ID for the observed length-composition data object.
Definition rcpp_fleet.hpp:387
SharedString name
The name of the fleet.
Definition rcpp_fleet.hpp:108
SharedInt n_ages
The number of age bins in the fleet data.
Definition rcpp_fleet.hpp:112
VariableVector log_q
The natural log of the index of abundance scaling parameter for this fleet.
Definition rcpp_fleet.hpp:139
VariableVector log_Fmort
The vector of the natural log of fishing mortality rates for this fleet.
Definition rcpp_fleet.hpp:144
SharedInt n_lengths
The number of length bins in the fleet data.
Definition rcpp_fleet.hpp:116
RealVector lengths
Fleet-specific observation-bin centers.
Definition rcpp_fleet.hpp:120
static std::map< uint32_t, std::shared_ptr< GrowthInterfaceBase > > live_objects
The map associating the IDs of GrowthInterfaceBase to the objects. This is a live object,...
Definition rcpp_growth.hpp:39
static std::map< uint32_t, std::shared_ptr< MaturityInterfaceBase > > live_objects
The map associating the IDs of MaturityInterfaceBase to the objects. This is a live object,...
Definition rcpp_maturity.hpp:35
uint32_t id
The local id of the PopulationInterfaceBase object.
Definition rcpp_population.hpp:28
static std::map< uint32_t, std::shared_ptr< PopulationInterfaceBase > > live_objects
The map associating the IDs of PopulationInterfaceBase to the objects. This is a live object,...
Definition rcpp_population.hpp:35
Rcpp interface for a new Population to instantiate from R: population <- methods::new(population)
Definition rcpp_population.hpp:80
VariableVector proportion_female
Proportion of females in the population.
Definition rcpp_population.hpp:146
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_population.hpp:290
VariableVector log_init_naa
The natural log of the initial numbers at age.
Definition rcpp_population.hpp:137
SharedString name
The name for the population.
Definition rcpp_population.hpp:155
SharedInt n_ages
The number of age bins.
Definition rcpp_population.hpp:85
SharedInt maturity_id
The ID of the maturity module.
Definition rcpp_population.hpp:109
VariableVector log_M
The natural log of the natural mortality for each year.
Definition rcpp_population.hpp:125
SharedInt n_years
The number of years.
Definition rcpp_population.hpp:101
VariableVector log_f_multiplier
Log of the population annual fishing mortality multiplier.
Definition rcpp_population.hpp:133
RealVector ages
Ages that are modeled in the population, the length of this vector should equal "n_ages".
Definition rcpp_population.hpp:151
VariableVector spawning_biomass_ratio
The population spawning biomass ratio for each year.
Definition rcpp_population.hpp:129
SharedInt growth_id
The ID of the growth module.
Definition rcpp_population.hpp:113
SharedInt recruitment_id
The ID of the recruitment module.
Definition rcpp_population.hpp:117
static std::map< uint32_t, std::shared_ptr< RecruitmentInterfaceBase > > live_objects
The map associating the IDs of RecruitmentInterfaceBase to the objects. This is a live object,...
Definition rcpp_recruitment.hpp:40
static std::map< uint32_t, std::shared_ptr< SelectivityInterfaceBase > > live_objects
The map associating the IDs of SelectivityInterfaceBase to the objects. This is a live object,...
Definition rcpp_selectivity.hpp:35
int get() const
Retrieve the value of the integer.
Definition rcpp_shared_primitive.hpp:127
std::string get() const
Retrieves the string value managed by the object.
Definition rcpp_shared_primitive.hpp:1617
uint32_t id_m
The local ID of the Variable object.
Definition rcpp_interface_base.hpp:156
size_t size()
Returns the size of a VariableVector.
Definition rcpp_interface_base.hpp:287
static std::string PrettyFormatJSON(const std::string &json)
Formats a JSON string.
Definition fims_json.hpp:182
static std::string EscapeString(const std::string &input)
Definition fims_json.hpp:143
Definition fims_vector.hpp:27
size_type size() const
Returns the number of elements.
Definition fims_vector.hpp:299
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
static std::shared_ptr< Model< Type > > GetInstance()
Evaluate. Calculates the joint negative log-likelihood function.
Definition model.hpp:55
Platform macros and the core FIMS logging system.
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:664
: Loops over model components and returns the negative log-likelihood function.
The Rcpp interface to declare different types of data, e.g., age-composition and index data....
The Rcpp interface to declare different distributions, e.g., normal and log normal....
The Rcpp interface to declare fleets. Allows for the use of methods::new() in R.
The Rcpp interface to declare different types of growth, e.g., empirical weight-at-age data....
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...
double sanitize_val(double x)
Sanitize a double value by replacing NaN or Inf with -999.0.
Definition rcpp_interface_base.hpp:112
The Rcpp interface to declare different maturity options, e.g., logistic. Allows for the use of metho...
The Rcpp interface to declare different types of populations. Allows for the use of methods::new() in...
The Rcpp interface to declare different types of recruitment, e.g., Beverton–Holt stock–recruitment r...
The Rcpp interface to declare different types of selectivity, e.g., logistic and double logistic....
Structure to hold dimension information for derived quantities.
Definition fishery_model_base.hpp:25
fims::Vector< int > dims
Definition fishery_model_base.hpp:28
int ndims
Definition fishery_model_base.hpp:27
fims::Vector< std::string > dim_names
Definition fishery_model_base.hpp:29