FIMS  v0.11.0
Loading...
Searching...
No Matches
rcpp_growth.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_GROWTH_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_GROWTH_HPP
11
12#include <algorithm>
13#include <cmath>
14
15#include "common/fims_math.hpp"
19
25 public:
29 static uint32_t id_g;
33 uint32_t id;
39 static std::map<uint32_t, std::shared_ptr<GrowthInterfaceBase>> live_objects;
40
45 this->id = GrowthInterfaceBase::id_g++;
46 /* Create instance of map: key is id and value is pointer to
47 GrowthInterfaceBase */
48 // GrowthInterfaceBase::live_objects[this->id] =
49 // std::make_shared<GrowthInterfaceBase>(*this);
50 }
51
57 GrowthInterfaceBase(const GrowthInterfaceBase& other) : id(other.id) {}
58
63
67 virtual uint32_t get_id() = 0;
68
73 virtual double evaluate(double age) = 0;
74};
75
84class GrowthDerivedObservationInterfaceBase : public GrowthInterfaceBase {
85 public:
86 GrowthDerivedObservationInterfaceBase() : GrowthInterfaceBase() {}
87
93 const GrowthDerivedObservationInterfaceBase& other)
94 : GrowthInterfaceBase(other) {}
95
97
98 protected:
99 template <typename Type>
104 std::shared_ptr<fims_popdy::GrowthDerivedObservationBase<Type>>
106 std::shared_ptr<fims_info::Information<Type>> info =
108
110 info->growth_models.find(this->id);
111 if (it == info->growth_models.end()) {
112 FIMS_WARNING_LOG("Growth-derived observation model " +
113 fims::to_string(this->id) +
114 " not found in Information.");
115 return nullptr;
116 }
117
118 std::shared_ptr<fims_popdy::GrowthDerivedObservationBase<Type>>
119 growth_observation = std::dynamic_pointer_cast<
121 if (!growth_observation) {
122 FIMS_WARNING_LOG("Growth-derived observation type mismatch for id " +
123 fims::to_string(this->id));
124 return nullptr;
125 }
126
127 return growth_observation;
128 }
129
130 template <typename Type>
137 growth_observation) const {
138 std::shared_ptr<fims_info::Information<Type>> info =
140 info->growth_models[growth_observation->id] = growth_observation;
141 }
142};
143
150 public:
168 std::shared_ptr<std::map<int, std::map<double, double>>> ewaa;
172 bool initialized = false;
173
178 this->ewaa = std::make_shared<std::map<int, std::map<double, double>>>();
180 std::make_shared<EWAAGrowthInterface>(*this);
182 std::make_shared<EWAAGrowthInterface>(*this));
183 }
184
191 : GrowthInterfaceBase(other),
192 weights(other.weights),
193 ages(other.ages),
194 n_years(other.n_years),
195 ewaa(other.ewaa),
196 initialized(other.initialized) {}
197
202
207 virtual uint32_t get_id() { return this->id; }
208
216 inline std::map<int, std::map<double, double>> make_map(RealVector ages,
219 std::map<int, std::map<double, double>> mymap;
220 const size_t n_years_plus_one = static_cast<size_t>(n_years.get() + 1);
221
222 // Reject invalid year counts because map keys are expected to include
223 // at least one model year.
224 if (n_years.get() < 1) {
225 Rcpp::stop("EWAA Error:: n_years must be at least 1");
226 }
227
228 // Reject empty vectors because we need at least one age-weight pair.
229 if (weights.size() == 0 || ages.size() == 0) {
230 Rcpp::stop("EWAA Error:: ages and weights must have at least one value");
231 }
232
233 // Accept either:
234 // 1) one weight vector by age (shared across years), or
235 // 2) a full year-by-age matrix flattened as (n_years + 1) * n_ages.
236 if ((weights.size() != ages.size() * n_years_plus_one) &&
237 (weights.size() != ages.size())) {
238 Rcpp::stop(
239 "weights size does not match ages size or ages size times "
240 "(n_years + 1), where the plus one is for the beginning "
241 "of the year after the terminal year spawning-biomass "
242 "calculations. weights size: " +
243 std::to_string(weights.size()) +
244 " ages size: " + std::to_string(ages.size()) +
245 " n_years: " + std::to_string(n_years.get()));
246 } else if (weights.size() == ages.size()) {
247 // One age-specific vector was provided, so replicate the same
248 // weight-at-age values for every year key (0 through n_years).
249 for (size_t y = 0; y < n_years_plus_one; y++) {
250 for (size_t i = 0; i < ages.size(); i++) {
251 mymap[y][ages[i]] = weights[i];
252 }
253 }
254 } else if (weights.size() == ages.size() * n_years_plus_one) {
255 // A flattened year-by-age matrix was provided, so map each block of
256 // n_ages values to the corresponding year key (0 through n_years).
257 for (size_t y = 0; y < n_years_plus_one; y++) {
258 for (size_t i = 0; i < ages.size(); i++) {
259 mymap[y][ages[i]] = weights[y * ages.size() + i];
260 }
261 }
262 }
263 return mymap;
264 }
265
271 virtual double evaluate(double age) {
273
274 // Build the EWAA map once from R inputs the first time evaluate() is
275 // called.
276 if (initialized == false) {
277 EWAAGrowth.ewaa = make_map(this->ages, this->weights, this->n_years);
278 initialized = true;
279 } else {
280 // Prevent re-initializing this object with a second evaluate() call.
281 Rcpp::stop("this empirical weight at age object is already initialized");
282 }
283 return EWAAGrowth.evaluate(0, age);
284 }
285
293 virtual std::string to_json() {
294 std::stringstream ss;
295 ss << "{\n";
296 ss << " \"module_name\": \"Growth\",\n";
297 ss << " \"module_type\": \"EWAA\",\n";
298 ss << " \"module_id\":" << this->id << ",\n";
299 ss << " \"parameters\": [\n{\n";
300 ss << " \"name\": \"weight_at_age\",\n";
301 ss << " \"id\": null,\n";
302 ss << " \"type\": \"vector\",\n";
303 ss << " \"dimensionality\": {\n";
304 ss << " \"header\": [\"n_years+1\",\"n_ages\"],\n";
305 ss << " \"dimensions\": [" << this->n_years.get() + 1 << ","
306 << this->ages.size() << "]\n},\n";
307
308 ss << " \"values\": [\n";
309 for (size_t i = 0; i < weights.size() - 1; i++) {
310 ss << "{\n";
311 ss << "\"id\": null,\n";
312 ss << "\"value\": " << weights[i] << ",\n";
313 ss << "\"estimated_value\": " << weights[i] << ",\n";
314 ss << "\"estimation_type\": \"constant\"\n";
315 ss << "},\n";
316 }
317 ss << "{\n";
318 ss << "\"id\": null,\n";
319 ss << "\"value\": " << weights[weights.size() - 1] << ",\n";
320 ss << "\"estimated_value\": " << weights[weights.size() - 1] << ",\n";
321 ss << "\"estimation_type\": \"constant\"\n";
322 ss << "}\n]\n";
323 ss << "}\n]\n}\n";
324 return ss.str();
325 }
326
327#ifdef TMB_MODEL
328
329 template <typename Type>
330 bool add_to_fims_tmb_internal() {
331 std::shared_ptr<fims_info::Information<Type>> info =
333
334 std::shared_ptr<fims_popdy::EWAAGrowth<Type>> ewaa_growth =
335 std::make_shared<fims_popdy::EWAAGrowth<Type>>();
336
337 // set relative info
338 ewaa_growth->id = this->id;
339 ewaa_growth->ewaa =
340 make_map(this->ages, this->weights, this->n_years); // this->ewaa;
341 // add to Information
342 info->growth_models[ewaa_growth->id] = ewaa_growth;
343
344 return true;
345 }
346
351 virtual bool add_to_fims_tmb() {
352 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
353 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
354
355 return true;
356 }
357
358#endif
359};
360
368 : public GrowthDerivedObservationInterfaceBase {
369 public:
376 // clang-format off
381 // clang-format on
426
431 : GrowthDerivedObservationInterfaceBase() {
432 // Variability inputs are optional and mutually exclusive by path, so
433 // leave them absent until a caller explicitly supplies one path.
434 this->length_at_age_sd_young.resize(0);
435 this->length_at_age_sd_old.resize(0);
436 this->log_sd_mean_length_young.resize(0);
437 this->log_sd_mean_length_old.resize(0);
438 this->log_sd_growth_coefficient.resize(0);
439 this->mean_length_young_mean_length_old_logit_corr.resize(0);
440 this->mean_length_young_growth_coefficient_logit_corr.resize(0);
441 this->mean_length_old_growth_coefficient_logit_corr.resize(0);
442
443 // Register this interface instance in global registries so it can be
444 // discovered and linked by ID during model initialization.
446 std::make_shared<VonBertalanffySchnuteGrowthInterface>(*this);
448 std::make_shared<VonBertalanffySchnuteGrowthInterface>(*this));
449 }
450
477
479
480 // Return stable module ID used for linking this growth object to populations.
481 virtual uint32_t get_id() { return this->id; }
482
487 virtual void finalize() {
488 if (this->finalized) {
489 FIMS_WARNING_LOG("VonBertalanffySchnute Growth " +
490 fims::to_string(this->id) +
491 " has been finalized already.");
492 }
493
494 this->finalized = true;
495
496 std::shared_ptr<fims_popdy::GrowthDerivedObservationBase<double>>
497 growth_observation = this->GetGrowthObservationFromInfo<double>();
498 if (!growth_observation) {
499 return;
500 }
501
502 std::shared_ptr<fims_popdy::VonBertalanffySchnuteGrowthModelAdapter<double>>
503 vb = std::dynamic_pointer_cast<
505 growth_observation);
506 if (!vb) {
507 FIMS_WARNING_LOG("Growth model type mismatch for id " +
508 fims::to_string(this->id));
509 return;
510 }
511
512 auto set_final = [](VariableVector& pv, const fims::Vector<double>& src,
513 bool log_scale) {
514 for (size_t i = 0; i < pv.size(); i++) {
515 if (pv[i].estimation_type_m.get() == "constant") {
516 pv[i].final_value_m = pv[i].initial_value_m;
517 } else {
518 double v = src[i];
519 if (log_scale) {
520 v = fims_math::exp(v);
521 }
522 pv[i].final_value_m = v;
523 }
524 }
525 };
526
527 SetFinalLengthParameter(
528 this->mean_length_young,
529 FinalMeanLengthYoungFromWorkingScale(vb->MeanLengthYoungVector()));
530 SetFinalLengthParameter(
531 this->mean_length_old,
532 FinalMeanLengthOldFromWorkingScale(vb->MeanLengthYoungVector(),
533 vb->MeanLengthOldVector()));
534 set_final(this->growth_coefficient, vb->GrowthCoefficientVector(), true);
535 set_final(this->reference_age_for_length_young,
536 vb->ReferenceAgeForLengthYoungVector(), false);
537 set_final(this->reference_age_for_length_old,
538 vb->ReferenceAgeForLengthOldVector(), false);
539 set_final(this->length_weight_a, vb->LengthWeightAVector(), true);
540 set_final(this->length_weight_b, vb->LengthWeightBVector(), true);
541 set_final(this->length_at_age_sd_young, vb->LengthAtAgeSdYoungVector(),
542 true);
543 set_final(this->length_at_age_sd_old, vb->LengthAtAgeSdOldVector(), true);
544 set_final(this->log_sd_mean_length_young,
545 vb->LogSdLengthAtRefAgeYoungVector(), false);
546 set_final(this->log_sd_mean_length_old, vb->LogSdLengthAtRefAgeOldVector(),
547 false);
548 set_final(this->log_sd_growth_coefficient,
549 vb->LogSdGrowthCoefficientVector(), false);
550 set_final(this->mean_length_young_mean_length_old_logit_corr,
551 vb->LogitCorrLengthAtRefAgeYoungLengthAtRefAgeOldVector(), false);
552 set_final(this->mean_length_young_growth_coefficient_logit_corr,
553 vb->LogitCorrLengthAtRefAgeYoungKVector(), false);
554 set_final(this->mean_length_old_growth_coefficient_logit_corr,
555 vb->LogitCorrLengthAtRefAgeOldKVector(), false);
556 }
557
558 virtual double evaluate(double age) {
559 if (age < 0.0) {
560 Rcpp::stop("Negative age not supported");
561 }
562 if (this->n_ages.get() <= 0) {
563 Rcpp::stop("n_ages not set");
564 }
565 const double age_round = std::round(age);
566 const double tol = 1e-8;
567 if (std::fabs(age - age_round) > tol) {
568 Rcpp::stop("Non-integer age not supported yet");
569 }
570 ValidateVonBertalanffySchnuteGrowthInputs(false);
571
573
574 vb.mean_length_young = this->mean_length_young[0].initial_value_m;
575 vb.mean_length_old = this->mean_length_old[0].initial_value_m;
576 vb.growth_coefficient = this->growth_coefficient[0].initial_value_m;
578 this->reference_age_for_length_young[0].initial_value_m;
580 this->reference_age_for_length_old[0].initial_value_m;
581
582 vb.length_weight_a = this->length_weight_a[0].initial_value_m;
583 vb.length_weight_b = this->length_weight_b[0].initial_value_m;
584
585 return vb.evaluate(0, age);
586 }
587
588 private:
589 enum class LengthReferenceParameterization {
590 kEstimatedReferenceLengths = 0,
591 kConstantLengthYoungEstimatedLengthOld,
592 kEstimatedLengthYoungBelowConstantLengthOld,
593 kBothConstant
594 };
595
596 bool IsEstimated(VariableVector& pv) {
597 return pv.size() > 0 && pv[0].estimation_type_m.get() != "constant";
598 }
599
600 double InitialLengthAtRefAgeYoung() {
601 return this->mean_length_young[0].initial_value_m;
602 }
603
604 double InitialLengthAtRefAgeOld() {
605 return this->mean_length_old[0].initial_value_m;
606 }
607
608 LengthReferenceParameterization GetLengthReferenceParameterization() {
609 const bool estimate_length_young = IsEstimated(this->mean_length_young);
610 const bool estimate_length_old = IsEstimated(this->mean_length_old);
611
612 if (estimate_length_young && estimate_length_old) {
613 return LengthReferenceParameterization::kEstimatedReferenceLengths;
614 }
615 if (!estimate_length_young && estimate_length_old) {
616 return LengthReferenceParameterization::
617 kConstantLengthYoungEstimatedLengthOld;
618 }
619 if (estimate_length_young && !estimate_length_old) {
620 return LengthReferenceParameterization::
621 kEstimatedLengthYoungBelowConstantLengthOld;
622 }
623 return LengthReferenceParameterization::kBothConstant;
624 }
625
626 double FinalMeanLengthYoungFromWorkingScale(
627 const fims::Vector<double>& mean_length_young_src) {
628 switch (GetLengthReferenceParameterization()) {
629 case LengthReferenceParameterization::kEstimatedReferenceLengths:
630 return fims_math::exp(mean_length_young_src[0]);
631
632 case LengthReferenceParameterization::
633 kConstantLengthYoungEstimatedLengthOld:
634 return InitialLengthAtRefAgeYoung();
635
636 case LengthReferenceParameterization::
637 kEstimatedLengthYoungBelowConstantLengthOld:
638 return fims_math::inv_logit(0.0, InitialLengthAtRefAgeOld(),
639 mean_length_young_src[0]);
640
641 case LengthReferenceParameterization::kBothConstant:
642 return InitialLengthAtRefAgeYoung();
643 }
644
645 return InitialLengthAtRefAgeYoung();
646 }
647
648 double FinalMeanLengthOldFromWorkingScale(
649 const fims::Vector<double>& mean_length_young_src,
650 const fims::Vector<double>& mean_length_old_src) {
651 switch (GetLengthReferenceParameterization()) {
652 case LengthReferenceParameterization::kEstimatedReferenceLengths:
653 case LengthReferenceParameterization::
654 kConstantLengthYoungEstimatedLengthOld:
655 // Match evaluation: younger length plus the positive difference.
656 return FinalMeanLengthYoungFromWorkingScale(mean_length_young_src) +
657 fims_math::exp(mean_length_old_src[0]);
658
659 case LengthReferenceParameterization::
660 kEstimatedLengthYoungBelowConstantLengthOld:
661 return InitialLengthAtRefAgeOld();
662
663 case LengthReferenceParameterization::kBothConstant:
664 return InitialLengthAtRefAgeOld();
665 }
666
667 return InitialLengthAtRefAgeOld();
668 }
669
670 void SetFinalLengthParameter(VariableVector& pv, double final_value) const {
671 for (size_t i = 0; i < pv.size(); i++) {
672 if (pv[i].estimation_type_m.get() == "constant") {
673 pv[i].final_value_m = pv[i].initial_value_m;
674 } else {
675 pv[i].final_value_m = final_value;
676 }
677 }
678 }
679
680 template <typename Type>
681 void ConfigureLengthReferenceParameterization(
682 const std::shared_ptr<
683 fims_popdy::VonBertalanffySchnuteGrowthModelAdapter<Type>>& vb) {
684 switch (GetLengthReferenceParameterization()) {
685 case LengthReferenceParameterization::kEstimatedReferenceLengths:
686 vb->UseEstimatedReferenceMeanLengths();
687 break;
688
689 case LengthReferenceParameterization::
690 kConstantLengthYoungEstimatedLengthOld:
691 vb->UseConstantMeanLengthYoungWithEstimatedMeanLengthOld(
692 static_cast<Type>(InitialLengthAtRefAgeYoung()));
693 break;
694
695 case LengthReferenceParameterization::
696 kEstimatedLengthYoungBelowConstantLengthOld:
697 vb->UseEstimatedMeanLengthYoungBelowConstantMeanLengthOld(
698 static_cast<Type>(InitialLengthAtRefAgeOld()));
699 break;
700
701 case LengthReferenceParameterization::kBothConstant:
702 vb->UseConstantReferenceMeanLengths(
703 static_cast<Type>(InitialLengthAtRefAgeYoung()),
704 static_cast<Type>(InitialLengthAtRefAgeOld()));
705 break;
706 }
707 }
708
709 void ValidateVonBertalanffySchnuteGrowthInputs(bool require_variability) {
710 if (this->mean_length_young.size() < 1 ||
711 this->mean_length_old.size() < 1 ||
712 this->growth_coefficient.size() < 1 ||
713 this->reference_age_for_length_young.size() < 1 ||
714 this->reference_age_for_length_old.size() < 1 ||
715 this->length_weight_a.size() < 1 || this->length_weight_b.size() < 1) {
716 Rcpp::stop("VonBertalanffySchnuteGrowth parameters not set");
717 }
718
719 const bool has_sd = this->length_at_age_sd_young.size() > 0 ||
720 this->length_at_age_sd_old.size() > 0;
721 const bool has_any_structured_delta =
722 this->log_sd_mean_length_young.size() > 0 ||
723 this->log_sd_mean_length_old.size() > 0 ||
724 this->log_sd_growth_coefficient.size() > 0 ||
728 const bool has_structured_delta =
729 this->log_sd_mean_length_young.size() > 0 &&
730 this->log_sd_mean_length_old.size() > 0 &&
731 this->log_sd_growth_coefficient.size() > 0 &&
735
736 if (require_variability && !has_sd && !has_any_structured_delta) {
737 Rcpp::stop(
738 "VonBertalanffySchnuteGrowth requires either "
739 "both length_at_age_sd_young and length_at_age_sd_old, "
740 "or the structured delta-method growth variability inputs");
741 }
742
743 if (has_any_structured_delta && !has_structured_delta) {
744 Rcpp::stop(
745 "von Bertalanffy--Schnute growth requires all six structured "
746 "delta-method "
747 "variability inputs when using that path");
748 }
749
750 if (has_sd && has_structured_delta) {
751 Rcpp::stop(
752 "von Bertalanffy--Schnute growth requires variability inputs for "
753 "one supported path. Supply either the interpolation inputs "
754
755 "length_at_age_sd_young and length_at_age_sd_old or the full "
756 "delta-method variability inputs, but not both");
757 }
758
759 if (has_sd && (this->length_at_age_sd_young.size() != 1 ||
760 this->length_at_age_sd_old.size() != 1)) {
761 Rcpp::stop(
762 "Interpolation requires exactly one length_at_age_sd_young value "
763 "and one length_at_age_sd_old value");
764 }
765
766 if (has_structured_delta &&
767 (this->log_sd_mean_length_young.size() != 1 ||
768 this->log_sd_mean_length_old.size() != 1 ||
769 this->log_sd_growth_coefficient.size() != 1 ||
770 this->mean_length_young_mean_length_old_logit_corr.size() != 1 ||
771 this->mean_length_young_growth_coefficient_logit_corr.size() != 1 ||
772 this->mean_length_old_growth_coefficient_logit_corr.size() != 1)) {
773 Rcpp::stop(
774 "VonBertalanffySchnuteGrowth currently supports a single structured "
775 "delta-method variability parameter set; expected size 1 for "
776 "each structured uncertainty input");
777 }
778
779 const double a1 = this->reference_age_for_length_young[0].initial_value_m;
780 const double a2 = this->reference_age_for_length_old[0].initial_value_m;
781 if (!std::isfinite(a1)) {
782 Rcpp::stop("reference_age_for_length_young must be finite");
783 }
784 if (!std::isfinite(a2)) {
785 Rcpp::stop("reference_age_for_length_old must be finite");
786 }
787 if (a2 <= a1) {
788 Rcpp::stop(
789 "VonBertalanffySchnuteGrowth reference_age_for_length_old must be > "
790 "reference_age_for_length_young");
791 }
792
793 auto check_finite = [](VariableVector& pv, const std::string& base_name) {
794 for (size_t i = 0; i < pv.size(); i++) {
795 if (!std::isfinite(pv[i].initial_value_m)) {
796 Rcpp::stop((base_name + " must be finite").c_str());
797 }
798 }
799 };
800
801 auto check_positive = [&check_finite](VariableVector& pv,
802 const std::string& base_name) {
803 check_finite(pv, base_name);
804 for (size_t i = 0; i < pv.size(); i++) {
805 if (pv[i].initial_value_m <= 0.0) {
806 Rcpp::stop((base_name + " must be > 0").c_str());
807 }
808 }
809 };
810
811 check_positive(this->mean_length_young, "mean_length_young");
812 check_positive(this->mean_length_old, "mean_length_old");
813 check_positive(this->growth_coefficient, "growth_coefficient");
814 check_positive(this->length_weight_a, "length_weight_a");
815 check_positive(this->length_weight_b, "length_weight_b");
816
817 if (this->mean_length_old[0].initial_value_m <=
818 this->mean_length_young[0].initial_value_m) {
819 Rcpp::stop(
820 "VonBertalanffySchnuteGrowth mean_length_old must be > "
821 "mean_length_young");
822 }
823
824 if (has_sd) {
825 check_positive(this->length_at_age_sd_young, "length_at_age_sd_young");
826 check_positive(this->length_at_age_sd_old, "length_at_age_sd_old");
827 }
828 if (has_structured_delta) {
829 check_finite(this->log_sd_mean_length_young, "log_sd_mean_length_young");
830 check_finite(this->log_sd_mean_length_old, "log_sd_mean_length_old");
831 check_finite(this->log_sd_growth_coefficient,
832 "log_sd_growth_coefficient");
834 "mean_length_young_mean_length_old_logit_corr");
836 "mean_length_young_growth_coefficient_logit_corr");
838 "mean_length_old_growth_coefficient_logit_corr");
839 }
840 }
841
842 public:
843 virtual std::string to_json() {
844 std::stringstream ss;
845
846 ss << "{\n";
847 ss << " \"module_name\":\"Growth\",\n";
848 ss << " \"module_type\": \"VonBertalanffySchnute\",\n";
849 ss << " \"module_id\": " << this->id << ",\n";
850
851 ss << " \"parameters\": [\n";
852
853 // mean_length_young
854 ss << "{\n";
855 ss << " \"name\": \"mean_length_young\",\n";
856 ss << " \"id\":" << this->mean_length_young.id_m << ",\n";
857 ss << " \"type\": \"vector\",\n";
858 ss << " \"dimensionality\": {\n";
859 ss << " \"header\": [null],\n";
860 ss << " \"dimensions\": [" << this->mean_length_young.size() << "]\n";
861 ss << " },\n";
862 ss << " \"values\":" << this->mean_length_young << "\n";
863 ss << "},\n";
864
865 // growth_coefficient
866 ss << "{\n";
867 ss << " \"name\": \"growth_coefficient\",\n";
868 ss << " \"id\":" << this->growth_coefficient.id_m << ",\n";
869 ss << " \"type\": \"vector\",\n";
870 ss << " \"dimensionality\": {\n";
871 ss << " \"header\": [null],\n";
872 ss << " \"dimensions\": [" << this->growth_coefficient.size() << "]\n";
873 ss << " },\n";
874 ss << " \"values\":" << this->growth_coefficient << "\n";
875 ss << "},\n";
876
877 // mean_length_old
878 ss << "{\n";
879 ss << " \"name\": \"mean_length_old\",\n";
880 ss << " \"id\":" << this->mean_length_old.id_m << ",\n";
881 ss << " \"type\": \"vector\",\n";
882 ss << " \"dimensionality\": {\n";
883 ss << " \"header\": [null],\n";
884 ss << " \"dimensions\": [" << this->mean_length_old.size() << "]\n";
885 ss << " },\n";
886 ss << " \"values\":" << this->mean_length_old << "\n";
887 ss << "},\n";
888
889 // reference_age_for_length_young
890 ss << "{\n";
891 ss << " \"name\": \"reference_age_for_length_young\",\n";
892 ss << " \"id\":" << this->reference_age_for_length_young.id_m << ",\n";
893 ss << " \"type\": \"vector\",\n";
894 ss << " \"dimensionality\": {\n";
895 ss << " \"header\": [null],\n";
896 ss << " \"dimensions\": [" << this->reference_age_for_length_young.size()
897 << "]\n";
898 ss << " },\n";
899 ss << " \"values\":" << this->reference_age_for_length_young << "\n";
900 ss << "},\n";
901
902 // reference_age_for_length_old
903 ss << "{\n";
904 ss << " \"name\": \"reference_age_for_length_old\",\n";
905 ss << " \"id\":" << this->reference_age_for_length_old.id_m << ",\n";
906 ss << " \"type\": \"vector\",\n";
907 ss << " \"dimensionality\": {\n";
908 ss << " \"header\": [null],\n";
909 ss << " \"dimensions\": [" << this->reference_age_for_length_old.size()
910 << "]\n";
911 ss << " },\n";
912 ss << " \"values\":" << this->reference_age_for_length_old << "\n";
913 ss << "},\n";
914
915 // length_weight_a
916 ss << "{\n";
917 ss << " \"name\": \"length_weight_a\",\n";
918 ss << " \"id\":" << this->length_weight_a.id_m << ",\n";
919 ss << " \"type\": \"vector\",\n";
920 ss << " \"dimensionality\": {\n";
921 ss << " \"header\": [null],\n";
922 ss << " \"dimensions\": [" << this->length_weight_a.size() << "]\n";
923 ss << " },\n";
924 ss << " \"values\":" << this->length_weight_a << "\n";
925 ss << "},\n";
926
927 // length_weight_b (last)
928 ss << "{\n";
929 ss << " \"name\": \"length_weight_b\",\n";
930 ss << " \"id\":" << this->length_weight_b.id_m << ",\n";
931 ss << " \"type\": \"vector\",\n";
932 ss << " \"dimensionality\": {\n";
933 ss << " \"header\": [null],\n";
934 ss << " \"dimensions\": [" << this->length_weight_b.size() << "]\n";
935 ss << " },\n";
936 ss << " \"values\":" << this->length_weight_b << "\n";
937 ss << "}";
938
939 // Optional variability parameters
940 auto append_optional_parameter = [&](const std::string& name,
941 VariableVector& pv) {
942 if (pv.size() == 0) {
943 return;
944 }
945
946 ss << ",\n";
947 ss << "{\n";
948 ss << " \"name\": \"" << name << "\",\n";
949 ss << " \"id\":" << pv.id_m << ",\n";
950 ss << " \"type\": \"vector\",\n";
951 ss << " \"dimensionality\": {\n";
952 ss << " \"header\": [null],\n";
953 ss << " \"dimensions\": [" << pv.size() << "]\n";
954 ss << " },\n";
955 ss << " \"values\":" << pv << "\n";
956 ss << "}";
957 };
958
959 append_optional_parameter("length_at_age_sd_young",
960 this->length_at_age_sd_young);
961 append_optional_parameter("length_at_age_sd_old",
962 this->length_at_age_sd_old);
963 append_optional_parameter("log_sd_mean_length_young",
964 this->log_sd_mean_length_young);
965 append_optional_parameter("log_sd_mean_length_old",
966 this->log_sd_mean_length_old);
967 append_optional_parameter("log_sd_growth_coefficient",
968 this->log_sd_growth_coefficient);
969 append_optional_parameter(
970 "mean_length_young_mean_length_old_logit_corr",
971 this->mean_length_young_mean_length_old_logit_corr);
972 append_optional_parameter(
973 "mean_length_young_growth_coefficient_logit_corr",
974 this->mean_length_young_growth_coefficient_logit_corr);
975 append_optional_parameter(
976 "mean_length_old_growth_coefficient_logit_corr",
977 this->mean_length_old_growth_coefficient_logit_corr);
978
979 ss << "\n]\n";
980 ss << "}";
981
982 return ss.str();
983 }
984#ifdef TMB_MODEL
985 template <typename Type>
986 bool add_to_fims_tmb_internal() {
987 std::shared_ptr<fims_info::Information<Type>> info =
989
990 std::shared_ptr<fims_popdy::GrowthDerivedObservationBase<Type>>
991 growth_observation = std::make_shared<
993 std::shared_ptr<fims_popdy::VonBertalanffySchnuteGrowthModelAdapter<Type>>
994 vb = std::dynamic_pointer_cast<
996 growth_observation);
997 if (!vb) {
998 Rcpp::stop(
999 "Failed to create VonBertalanffySchnute growth-derived observation "
1000 "model");
1001 }
1002
1003 vb->id = this->id;
1004
1005 // Build parameter vectors and register if estimable. Growth variability
1006 // must be supplied through either the interpolation SD inputs or
1007 // the transformed delta-method variability inputs.
1008 ValidateVonBertalanffySchnuteGrowthInputs(true);
1009
1010 std::stringstream ss;
1011 auto load_and_register = [&](VariableVector& pv, fims::Vector<Type>& target,
1012 const std::string& base_name, bool log_scale) {
1013 target.resize(pv.size());
1014 for (size_t i = 0; i < pv.size(); i++) {
1015 double v = pv[i].initial_value_m;
1016 if (log_scale) {
1017 if (v <= 0.0) {
1018 Rcpp::stop((base_name + " must be > 0").c_str());
1019 }
1020
1021 if (&pv == &this->mean_length_young &&
1022 GetLengthReferenceParameterization() ==
1023 LengthReferenceParameterization::
1024 kEstimatedLengthYoungBelowConstantLengthOld) {
1025 // Match the bounded inverse logit used in evaluation and reporting.
1026 v = fims_math::logit(0.0, InitialLengthAtRefAgeOld(), v);
1027 } else if (&pv == &this->mean_length_old &&
1028 IsEstimated(this->mean_length_old)) {
1029 // Store the positive difference from the younger reference length.
1030 v = fims_math::log(v - InitialLengthAtRefAgeYoung());
1031 } else {
1032 v = fims_math::log(v);
1033 }
1034 }
1035 target[i] = static_cast<Type>(v);
1036 if (pv[i].estimation_type_m.get() == "fixed_effects") {
1037 ss.str("");
1038 ss << "Growth." << this->id << "." << base_name << "." << pv[i].id_m;
1039 info->RegisterParameterName(ss.str());
1040 info->RegisterParameter(target[i]);
1041 }
1042 if (pv[i].estimation_type_m.get() == "random_effects") {
1043 ss.str("");
1044 ss << "Growth." << this->id << "." << base_name << "." << pv[i].id_m;
1045 info->RegisterRandomEffectName(ss.str());
1046 info->RegisterRandomEffect(target[i]);
1047 }
1048 }
1049 info->variable_map[pv.id_m] = &target;
1050 };
1051
1052 auto load_optional_and_register =
1053 [&](VariableVector& pv, fims::Vector<Type>& target,
1054 const std::string& base_name, bool log_scale) {
1055 if (pv.size() == 0) {
1056 target.resize(0);
1057 return;
1058 }
1059 load_and_register(pv, target, base_name, log_scale);
1060 };
1061
1062 ConfigureLengthReferenceParameterization<Type>(vb);
1063
1064 load_and_register(this->mean_length_young, vb->MeanLengthYoungVector(),
1065 "mean_length_young", true);
1066 load_and_register(this->mean_length_old, vb->MeanLengthOldVector(),
1067 "mean_length_old", true);
1068 load_and_register(this->growth_coefficient, vb->GrowthCoefficientVector(),
1069 "growth_coefficient", true);
1070 load_and_register(this->reference_age_for_length_young,
1071 vb->ReferenceAgeForLengthYoungVector(),
1072 "reference_age_for_length_young", false);
1073 load_and_register(this->reference_age_for_length_old,
1074 vb->ReferenceAgeForLengthOldVector(),
1075 "reference_age_for_length_old", false);
1076 load_and_register(this->length_weight_a, vb->LengthWeightAVector(),
1077 "length_weight_a", true);
1078 load_and_register(this->length_weight_b, vb->LengthWeightBVector(),
1079 "length_weight_b", true);
1080 load_optional_and_register(this->length_at_age_sd_young,
1081 vb->LengthAtAgeSdYoungVector(),
1082 "length_at_age_sd_young", true);
1083 load_optional_and_register(this->length_at_age_sd_old,
1084 vb->LengthAtAgeSdOldVector(),
1085 "length_at_age_sd_old", true);
1086 load_optional_and_register(this->log_sd_mean_length_young,
1087 vb->LogSdLengthAtRefAgeYoungVector(),
1088 "log_sd_mean_length_young", false);
1089 load_optional_and_register(this->log_sd_mean_length_old,
1090 vb->LogSdLengthAtRefAgeOldVector(),
1091 "log_sd_mean_length_old", false);
1092 load_optional_and_register(this->log_sd_growth_coefficient,
1093 vb->LogSdGrowthCoefficientVector(),
1094 "log_sd_growth_coefficient", false);
1095 load_optional_and_register(
1097 vb->LogitCorrLengthAtRefAgeYoungLengthAtRefAgeOldVector(),
1098 "mean_length_young_mean_length_old_logit_corr", false);
1099 load_optional_and_register(
1101 vb->LogitCorrLengthAtRefAgeYoungKVector(),
1102 "mean_length_young_growth_coefficient_logit_corr", false);
1103 load_optional_and_register(
1105 vb->LogitCorrLengthAtRefAgeOldKVector(),
1106 "mean_length_old_growth_coefficient_logit_corr", false);
1107
1108 this->RegisterGrowthObservationInInfo<Type>(growth_observation);
1109 return true;
1110 }
1111
1112 virtual bool add_to_fims_tmb() {
1113 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
1114 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
1115 return true;
1116 }
1117#endif
1118
1119}; // end class
1120
1121#endif
virtual ~EWAAGrowthInterface()
The destructor.
Definition rcpp_growth.hpp:201
SharedInt n_years
An integer specifying the number of years.
Definition rcpp_growth.hpp:163
std::shared_ptr< std::map< int, std::map< double, double > > > ewaa
A map of empirical weight-at-age values allowing multiple modules to access and modify the weights wi...
Definition rcpp_growth.hpp:168
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_growth.hpp:207
RealVector ages
Ages (years) for each age class.
Definition rcpp_growth.hpp:158
virtual double evaluate(double age)
Evaluate the growth using empirical weight at age.
Definition rcpp_growth.hpp:271
EWAAGrowthInterface(const EWAAGrowthInterface &other)
Construct a new EWAAGrowthInterface object.
Definition rcpp_growth.hpp:190
std::map< int, std::map< double, double > > make_map(RealVector ages, RealVector weights, SharedInt n_years)
Create a map of input numeric vectors.
Definition rcpp_growth.hpp:216
bool initialized
Have weight and age vectors been set? The default is false.
Definition rcpp_growth.hpp:172
RealVector weights
Weights (mt) for each age class.
Definition rcpp_growth.hpp:154
EWAAGrowthInterface()
The constructor.
Definition rcpp_growth.hpp:177
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_growth.hpp:293
Base class for all interface objects.
Definition rcpp_interface_base.hpp:655
bool finalized
Is the object already finalized? The default is false.
Definition rcpp_interface_base.hpp:660
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
Rcpp interface base for growth models that can register a growth-derived observation capability with ...
Definition rcpp_growth.hpp:84
void RegisterGrowthObservationInInfo(const std::shared_ptr< fims_popdy::GrowthDerivedObservationBase< Type > > &growth_observation) const
Register a runtime growth-derived observation in Information.
Definition rcpp_growth.hpp:135
GrowthDerivedObservationInterfaceBase(const GrowthDerivedObservationInterfaceBase &other)
Copy constructor.
Definition rcpp_growth.hpp:92
std::shared_ptr< fims_popdy::GrowthDerivedObservationBase< Type > > GetGrowthObservationFromInfo() const
Look up the registered growth-derived observation object.
Definition rcpp_growth.hpp:105
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
virtual double evaluate(double age)=0
A method for each child growth interface object to inherit so each growth option can have an evaluate...
GrowthInterfaceBase(const GrowthInterfaceBase &other)
Construct a new Growth Interface Base object.
Definition rcpp_growth.hpp:57
virtual ~GrowthInterfaceBase()
The destructor.
Definition rcpp_growth.hpp:62
GrowthInterfaceBase()
The constructor.
Definition rcpp_growth.hpp:44
uint32_t id
The local id of the GrowthInterfaceBase object.
Definition rcpp_growth.hpp:33
virtual uint32_t get_id()=0
Get the ID for the child growth interface objects to inherit.
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:431
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:602
A class that provides shared ownership of an integer value.
Definition rcpp_shared_primitive.hpp:29
int get() const
Retrieve the value of the integer.
Definition rcpp_shared_primitive.hpp:127
An Rcpp interface class that defines the VariableVector class.
Definition rcpp_interface_base.hpp:143
Variable & get(size_t pos)
An internal accessor for calling a position of a VariableVector from R.
Definition rcpp_interface_base.hpp:266
uint32_t id_m
The local ID of the Variable object.
Definition rcpp_interface_base.hpp:156
void resize(size_t size)
Resizes a VariableVector to the desired length.
Definition rcpp_interface_base.hpp:294
size_t size()
Returns the size of a VariableVector.
Definition rcpp_interface_base.hpp:287
Rcpp-facing VonBertalanffySchnute growth module. Inherits "derived observation" capability so this gr...
Definition rcpp_growth.hpp:368
VonBertalanffySchnuteGrowthInterface(const VonBertalanffySchnuteGrowthInterface &other)
Copy constructor.
Definition rcpp_growth.hpp:455
VariableVector log_sd_growth_coefficient
Working-scale von Bertalanffy–Schnute variability parameter for sd(log(growth_coefficient)).
Definition rcpp_growth.hpp:408
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_growth.hpp:487
VariableVector mean_length_young
Expected length at the first reference age.
Definition rcpp_growth.hpp:371
virtual uint32_t get_id()
Get the ID for the child growth interface objects to inherit.
Definition rcpp_growth.hpp:481
VariableVector log_sd_mean_length_young
Working-scale von Bertalanffy–Schnute variability parameter for sd(log(mean_length_young)).
Definition rcpp_growth.hpp:398
VariableVector reference_age_for_length_young
First reference age for the length parameterization.
Definition rcpp_growth.hpp:378
VariableVector mean_length_young_growth_coefficient_logit_corr
Working-scale von Bertalanffy–Schnute variability parameter for corr(log(mean_length_young),...
Definition rcpp_growth.hpp:418
VariableVector growth_coefficient
Growth coefficient.
Definition rcpp_growth.hpp:375
virtual double evaluate(double age)
A method for each child growth interface object to inherit so each growth option can have an evaluate...
Definition rcpp_growth.hpp:558
VonBertalanffySchnuteGrowthInterface()
Construct a new VonBertalanffySchnute growth interface.
Definition rcpp_growth.hpp:430
VariableVector length_at_age_sd_old
Natural-scale length-at-age SD at the older reference age.
Definition rcpp_growth.hpp:393
SharedInt n_ages
Modeled number of ages used for validation.
Definition rcpp_growth.hpp:425
VariableVector reference_age_for_length_old
Second reference age for the length parameterization.
Definition rcpp_growth.hpp:380
VariableVector length_weight_a
Coefficient in the length-weight relationship, W = a * L^b.
Definition rcpp_growth.hpp:383
VariableVector log_sd_mean_length_old
Working-scale von Bertalanffy–Schnute variability parameter for sd(log(mean_length_old)).
Definition rcpp_growth.hpp:403
VariableVector mean_length_old_growth_coefficient_logit_corr
Working-scale von Bertalanffy–Schnute variability parameter for corr(log(mean_length_old),...
Definition rcpp_growth.hpp:423
VariableVector mean_length_young_mean_length_old_logit_corr
Working-scale von Bertalanffy–Schnute variability parameter for corr(log(mean_length_young),...
Definition rcpp_growth.hpp:413
VariableVector mean_length_old
Expected length at the second reference age.
Definition rcpp_growth.hpp:373
virtual std::string to_json()
Convert the data to json representation for the output.
Definition rcpp_growth.hpp:843
VariableVector length_weight_b
Exponent in the length-weight relationship, W = a * L^b.
Definition rcpp_growth.hpp:385
VariableVector length_at_age_sd_young
Natural-scale length-at-age SD at the younger reference age.
Definition rcpp_growth.hpp:389
Definition fims_vector.hpp:27
std::map< uint32_t, std::shared_ptr< fims_popdy::GrowthBase< Type > > >::iterator growth_models_iterator
Definition information.hpp:96
std::map< uint32_t, std::shared_ptr< fims_popdy::GrowthBase< Type > > > growth_models
Definition information.hpp:91
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:262
Generic capability interface for growth models that can feed the growth-derived age-to-length convers...
Definition growth_model_adapter.hpp:26
von Bertalanffy–Schnute growth model adapter implementing the generic growth-derived observation capa...
Definition growth_model_adapter.hpp:79
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:648
std::string to_string(T v)
Definition def.hpp:735
A collection of mathematical functions used in FIMS.
const Type logit(const Type &a, const Type &b, const Type &x)
A logit function for bounding of parameters.
Definition fims_math.hpp:279
const Type inv_logit(const Type &a, const Type &b, const Type &logit_x)
An inverse logit function for bounding of parameters.
Definition fims_math.hpp:294
Establishes the FIMS Vector class.
Includes any .hpp files within the subfolders so that only this file needs to included in include sta...
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...
uint32_t id
Definition model_object.hpp:56
EWAAGrowth class that returns the EWAA function value.
Definition ewaa.hpp:23
std::map< int, std::map< double, double > > ewaa
Definition ewaa.hpp:29
virtual const Type evaluate(int year, const double &a)
Returns the weight at age a (in kg) from the input vector.
Definition ewaa.hpp:44
VonBertalanffySchnute growth functor for length-at-age and weight-at-age.
Definition vonb_schnute.hpp:32
Type mean_length_young
Expected length at the first reference age.
Definition vonb_schnute.hpp:34
Type reference_age_for_length_old
Second reference age for the length parameterization.
Definition vonb_schnute.hpp:42
Type growth_coefficient
Growth coefficient.
Definition vonb_schnute.hpp:38
Type length_weight_b
Exponent in the length-weight relationship, W = a * L^b.
Definition vonb_schnute.hpp:47
Type reference_age_for_length_young
First reference age for the length parameterization.
Definition vonb_schnute.hpp:40
Type length_weight_a
Coefficient in the length-weight relationship, W = a * L^b.
Definition vonb_schnute.hpp:45
virtual const Type evaluate(int year, const double &a) override
Calculates the growth at the independent variable value.
Definition vonb_schnute.hpp:231
Type mean_length_old
Expected length at the second reference age.
Definition vonb_schnute.hpp:36