9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_FLEET_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_FLEET_HPP
34 static std::map<uint32_t, std::shared_ptr<FleetInterfaceBase>>
live_objects;
40 this->
id = FleetInterfaceBase::id_g++;
72 SharedInt interface_observed_agecomp_data_id_m = -999;
76 SharedInt interface_observed_lengthcomp_data_id_m = -999;
94 SharedInt interface_observed_index_data_id_m = -999;
98 SharedInt interface_observed_catch_data_id_m = -999;
102 SharedInt interface_selectivity_id_m = -999;
252 this->weight_at_age.
resize(0);
254 this->ageing_error.
resize(0);
255 std::shared_ptr<FleetInterface> fleet =
256 std::make_shared<FleetInterface>(*
this);
270 interface_observed_agecomp_data_id_m(
271 other.interface_observed_agecomp_data_id_m),
272 interface_observed_lengthcomp_data_id_m(
273 other.interface_observed_lengthcomp_data_id_m),
274 interface_requires_age_length_mapping(
275 other.interface_requires_age_length_mapping),
276 interface_observed_index_data_id_m(
277 other.interface_observed_index_data_id_m),
278 interface_observed_catch_data_id_m(
279 other.interface_observed_catch_data_id_m),
280 interface_selectivity_id_m(other.interface_selectivity_id_m),
340 interface_observed_agecomp_data_id_m.set(observed_agecomp_data_id);
348 interface_observed_lengthcomp_data_id_m.set(observed_lengthcomp_data_id);
364 interface_requires_age_length_mapping.set(requires_age_length_mapping);
372 interface_observed_index_data_id_m.set(observed_index_data_id);
380 interface_observed_catch_data_id_m.set(observed_catch_data_id);
387 interface_selectivity_id_m.set(selectivity_id);
401 return interface_observed_agecomp_data_id_m.get();
409 return interface_observed_lengthcomp_data_id_m.get();
423 return interface_requires_age_length_mapping.get();
430 return interface_observed_index_data_id_m.get();
437 return interface_observed_catch_data_id_m.get();
447 " has been finalized already.");
452 std::shared_ptr<fims_info::Information<double>> info =
457 it = info->
fleets.find(this->
id);
459 if (it == info->
fleets.end()) {
461 " not found in Information.");
464 std::shared_ptr<fims_popdy::Fleet<double>> fleet =
465 std::dynamic_pointer_cast<fims_popdy::Fleet<double>>(it->second);
467 for (
size_t i = 0; i < this->log_Fmort.
size(); i++) {
468 if (this->log_Fmort[i].estimation_type_m.
get() ==
"constant") {
469 this->log_Fmort[i].final_value_m = this->log_Fmort[i].initial_value_m;
471 this->log_Fmort[i].final_value_m = fleet->log_Fmort[i];
475 for (
size_t i = 0; i < this->log_q.
size(); i++) {
476 if (this->log_q[i].estimation_type_m.
get() ==
"constant") {
477 this->log_q[i].final_value_m = this->log_q[i].initial_value_m;
479 this->log_q[i].final_value_m = fleet->log_q[i];
483 for (
size_t i = 0; i < fleet->age_to_length_conversion.size(); i++) {
484 if (this->age_to_length_conversion[i].estimation_type_m.
get() ==
486 this->age_to_length_conversion[i].final_value_m =
487 this->age_to_length_conversion[i].initial_value_m;
489 this->age_to_length_conversion[i].final_value_m =
490 fleet->age_to_length_conversion[i];
498 template <
typename Type>
499 bool add_to_fims_tmb_internal() {
500 std::shared_ptr<fims_info::Information<Type>> info =
503 std::shared_ptr<fims_popdy::Fleet<Type>> fleet =
504 std::make_shared<fims_popdy::Fleet<Type>>();
506 std::stringstream ss;
509 fleet->id = this->
id;
510 fleet->n_ages = this->n_ages.
get();
511 fleet->n_lengths = this->n_lengths.
get();
512 fleet->n_years = this->n_years.
get();
513 fleet->observed_catch_units = this->observed_catch_units;
514 fleet->observed_index_units = this->observed_index_units;
516 fleet->fleet_observed_agecomp_data_id_m =
517 interface_observed_agecomp_data_id_m.
get();
519 fleet->fleet_observed_lengthcomp_data_id_m =
520 interface_observed_lengthcomp_data_id_m.
get();
521 fleet->requires_age_length_mapping =
522 interface_requires_age_length_mapping.
get();
524 fleet->fleet_observed_index_data_id_m =
525 interface_observed_index_data_id_m.
get();
526 fleet->fleet_observed_catch_data_id_m =
527 interface_observed_catch_data_id_m.
get();
529 fleet->fleet_selectivity_id_m = interface_selectivity_id_m.
get();
532 if (this->log_q.
size() != 1 &&
533 this->log_q.size() !=
static_cast<size_t>(this->n_years.get())) {
537 throw std::invalid_argument(
538 "Fleet log_q size mismatch. Fleet log_q has " +
540 " values, but it needs 1 value or 1 value per year (" +
543 fleet->log_q.resize(this->
log_q.size());
544 for (
size_t i = 0; i < this->
log_q.size(); i++) {
545 fleet->log_q[i] = this->
log_q[i].initial_value_m;
547 if (this->
log_q[i].estimation_type_m.get() ==
"fixed_effects") {
549 ss <<
"Fleet." << this->
id <<
".log_q." << this->
log_q[i].id_m;
550 info->RegisterParameterName(ss.str());
551 info->RegisterParameter(fleet->log_q[i]);
553 if (this->
log_q[i].estimation_type_m.get() ==
"random_effects") {
555 ss <<
"Fleet." << this->
id <<
".log_q." << this->
log_q[i].id_m;
556 info->RegisterRandomEffectName(ss.str());
557 info->RegisterRandomEffect(fleet->log_q[i]);
561 info->variable_map[this->
log_q.id_m] = &(fleet)->
log_q;
563 if (this->
log_Fmort.size() !=
static_cast<size_t>(this->n_years.get())) {
564 FIMS_ERROR_LOG(
"The size of `log_Fmort` does not match `n_years`: " +
565 fims::to_string(this->
log_Fmort.size()) +
566 " != " + fims::to_string(this->n_years.get()));
567 throw std::invalid_argument(
568 "Fleet log_Fmort size mismatch."
569 "Fleet log_Fmort is of size " +
570 fims::to_string(this->
log_Fmort.size()) +
571 " and the number of years is " +
572 fims::to_string(this->n_years.get()));
574 fleet->log_Fmort.resize(
static_cast<size_t>(this->
log_Fmort.size()));
575 for (
size_t i = 0; i <
log_Fmort.size(); i++) {
576 fleet->log_Fmort[i] = this->
log_Fmort[i].initial_value_m;
578 if (this->
log_Fmort[i].estimation_type_m.get() ==
"fixed_effects") {
580 ss <<
"Fleet." << this->
id <<
".log_Fmort." << this->
log_Fmort[i].id_m;
581 info->RegisterParameterName(ss.str());
582 info->RegisterParameter(fleet->log_Fmort[i]);
584 if (this->
log_Fmort[i].estimation_type_m.get() ==
"random_effects") {
586 ss <<
"Fleet." << this->
id <<
".log_Fmort." << this->
log_Fmort[i].id_m;
587 info->RegisterRandomEffectName(ss.str());
588 info->RegisterRandomEffect(fleet->log_Fmort[i]);
594 const size_t expected_weight_at_age_size =
595 static_cast<size_t>(this->
n_years.get() * this->
n_ages.get());
597 this->weight_at_age.size() != expected_weight_at_age_size) {
599 "The size of `weight_at_age` does not match n_years x "
602 " != " + fims::to_string(expected_weight_at_age_size));
603 throw std::invalid_argument(
604 "Fleet weight_at_age size mismatch. Expected 0 or " +
605 fims::to_string(expected_weight_at_age_size) +
" values, found " +
609 for (
size_t i = 0; i < fleet->weight_at_age.size(); i++) {
614 if (this->
lengths.size() !=
static_cast<size_t>(this->n_lengths.get())) {
615 FIMS_ERROR_LOG(
"The size of `lengths` does not match `n_lengths`: " +
616 fims::to_string(this->
lengths.size()) +
617 " != " + fims::to_string(this->n_lengths.get()));
618 throw std::invalid_argument(
619 "Fleet lengths size mismatch. Fleet lengths is of size " +
620 fims::to_string(this->
lengths.size()) +
621 " and the number of lengths is " +
622 fims::to_string(this->n_lengths.get()));
625 fleet->lengths.resize(this->
lengths.size());
626 for (
size_t i = 0; i < fleet->lengths.size(); i++) {
627 fleet->lengths[i] = this->
lengths[i];
630 fleet->age_to_length_conversion.resize(
634 const size_t expected_age_to_length_conversion_size =
636 const size_t expected_age_to_length_conversion_size_by_year =
637 static_cast<size_t>(this->
n_years.get()) *
638 expected_age_to_length_conversion_size;
639 const size_t supplied_age_to_length_conversion_size =
642 if (supplied_age_to_length_conversion_size != 0 &&
643 supplied_age_to_length_conversion_size !=
644 expected_age_to_length_conversion_size &&
645 supplied_age_to_length_conversion_size !=
646 expected_age_to_length_conversion_size_by_year) {
648 "age_to_length_conversion size mismatch, " +
649 fims::to_string(supplied_age_to_length_conversion_size) +
" != " +
650 fims::to_string(expected_age_to_length_conversion_size) +
" or " +
651 fims::to_string(expected_age_to_length_conversion_size_by_year));
652 throw std::invalid_argument(
653 "Fleet age_to_length_conversion size mismatch. Expected " +
654 fims::to_string(expected_age_to_length_conversion_size) +
" or " +
655 fims::to_string(expected_age_to_length_conversion_size_by_year) +
656 " values but received " +
657 fims::to_string(supplied_age_to_length_conversion_size) +
".");
660 for (
size_t i = 0; i < fleet->age_to_length_conversion.size(); i++) {
661 fleet->age_to_length_conversion[i] =
667 ss <<
"Fleet." << this->
id <<
".age_to_length_conversion."
669 info->RegisterParameterName(ss.str());
670 info->RegisterParameter(fleet->age_to_length_conversion[i]);
675 "age_to_length_conversion cannot be set to random effects");
684 const size_t matrix_size =
685 static_cast<size_t>(this->
n_ages.get() * this->
n_ages.get());
687 if (supplied_size != matrix_size &&
689 matrix_size *
static_cast<size_t>(this->
n_years.get())) {
691 fims::to_string(supplied_size) +
692 " is not n_ages^2 or n_years * n_ages^2.");
693 throw std::invalid_argument(
694 "Fleet ageing_error size mismatch. Expected " +
695 fims::to_string(matrix_size) +
" or " +
696 fims::to_string(matrix_size * this->
n_years.get()) +
697 " values but received " + fims::to_string(supplied_size) +
".");
701 for (
size_t i = 0; i < fleet->ageing_error.size(); i++) {
706 info->fleets[fleet->id] = fleet;
716 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
717 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
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
FleetInterfaceBase()
The constructor.
Definition rcpp_fleet.hpp:39
uint32_t id
The local id of the FleetInterfaceBase object.
Definition rcpp_fleet.hpp:28
virtual ~FleetInterfaceBase()
The destructor.
Definition rcpp_fleet.hpp:56
virtual uint32_t get_id()=0
Get the ID for the child fleet interface objects to inherit.
FleetInterfaceBase(const FleetInterfaceBase &other)
Construct a new Fleet Interface Base object.
Definition rcpp_fleet.hpp:51
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
bool GetRequiresAgeLengthMapping()
Return whether this fleet requires age-to-length mapping.
Definition rcpp_fleet.hpp:422
SharedInt n_years
The number of years in the fleet data.
Definition rcpp_fleet.hpp:124
uint32_t GetSelectivityID()
Get the unique ID for the selectivity object.
Definition rcpp_fleet.hpp:395
VariableVector catch_numbers_at_age
Annual catch at age in numbers for a specific fleet.
Definition rcpp_fleet.hpp:170
int GetObservedIndexDataID()
Get the unique id for the observed index data object.
Definition rcpp_fleet.hpp:429
VariableVector catch_weight
Total catch in weight for a specific fleet.
Definition rcpp_fleet.hpp:182
VariableVector index_numbers_at_length
Numbers at length for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:218
int GetObservedCatchDataID()
Get the unique id for the observed catch data object.
Definition rcpp_fleet.hpp:436
VariableVector agecomp_proportion
Proportion of total catch by age used to specify the age composition.
Definition rcpp_fleet.hpp:201
FleetInterface(const FleetInterface &other)
Construct a new Fleet Interface object.
Definition rcpp_fleet.hpp:268
void SetObservedLengthCompDataID(int observed_lengthcomp_data_id)
Set the unique ID for the observed length-composition data object.
Definition rcpp_fleet.hpp:347
virtual void finalize()
Extracts the derived quantities from Information to the Rcpp object.
Definition rcpp_fleet.hpp:443
int GetObservedAgeCompDataID()
Get the unique ID for the observed age-composition data object.
Definition rcpp_fleet.hpp:400
void SetObservedAgeCompDataID(int observed_agecomp_data_id)
Set the unique ID for the observed age-composition data object.
Definition rcpp_fleet.hpp:339
RealVector weight_at_age
Weight at age for this fleet's catch and index weight, ordered by year and then age,...
Definition rcpp_fleet.hpp:157
void SetRequiresAgeLengthMapping(bool requires_age_length_mapping)
Set whether this fleet requires age-to-length mapping.
Definition rcpp_fleet.hpp:363
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_fleet.hpp:321
void SetSelectivityID(int selectivity_id)
Set the unique ID for the selectivity object.
Definition rcpp_fleet.hpp:386
virtual ~FleetInterface()
The destructor.
Definition rcpp_fleet.hpp:315
VariableVector catch_weight_at_age
Annual catch at age in weight for a specific fleet.
Definition rcpp_fleet.hpp:174
VariableVector index_expected
Expected value for a fleet index, where the units of the measurement depend on the specified units fo...
Definition rcpp_fleet.hpp:231
VariableVector lengthcomp_expected
Expected length-composition proportions for the fleet.
Definition rcpp_fleet.hpp:244
void SetObservedCatchDataID(int observed_catch_data_id)
Set the unique ID for the observed catch data object.
Definition rcpp_fleet.hpp:379
RealVector ageing_error
Ageing error matrices, 1 for all years or 1 per year, with true age as rows and observed age varying ...
Definition rcpp_fleet.hpp:164
int GetObservedLengthCompDataID()
Get the unique ID for the observed length-composition data object.
Definition rcpp_fleet.hpp:408
VariableVector age_to_length_conversion
The vector of conversions to go from age to length, i.e., the age-to-length-conversion matrix.
Definition rcpp_fleet.hpp:150
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
void SetName(const std::string &name)
Sets the name of the fleet.
Definition rcpp_fleet.hpp:327
VariableVector lengthcomp_proportion
Proportion of total catch by length used to specify the length composition.
Definition rcpp_fleet.hpp:206
VariableVector log_q
The natural log of the index of abundance scaling parameter for this fleet.
Definition rcpp_fleet.hpp:139
std::string GetName() const
Gets the name of the fleet.
Definition rcpp_fleet.hpp:333
VariableVector index_weight_at_age
Weight at age for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:214
VariableVector index_weight
Total weight for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:222
VariableVector log_catch_expected
Log total expected catch for a specific fleet, where the units of the measurement depend on the speci...
Definition rcpp_fleet.hpp:196
SharedString observed_catch_units
What units are the observed catch for this fleet measured in. Options are "weight" or "number",...
Definition rcpp_fleet.hpp:129
VariableVector log_Fmort
The vector of the natural log of fishing mortality rates for this fleet.
Definition rcpp_fleet.hpp:144
FleetInterface()
The constructor.
Definition rcpp_fleet.hpp:249
SharedInt n_lengths
The number of length bins in the fleet data.
Definition rcpp_fleet.hpp:116
VariableVector index_numbers
Total numbers for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:226
VariableVector catch_numbers
Total catch in numbers for a specific fleet.
Definition rcpp_fleet.hpp:186
VariableVector agecomp_expected
Expected age-composition proportions for the fleet.
Definition rcpp_fleet.hpp:240
void SetObservedIndexDataID(int observed_index_data_id)
Set the unique ID for the observed index data object.
Definition rcpp_fleet.hpp:371
SharedString observed_index_units
What units is the observed index of abundance for this fleet measured in. Options are "weight" or "nu...
Definition rcpp_fleet.hpp:134
RealVector lengths
Fleet-specific observation-bin lower edges.
Definition rcpp_fleet.hpp:120
VariableVector index_numbers_at_age
Numbers at age for a fleet index such as survey or CPUE.
Definition rcpp_fleet.hpp:210
VariableVector log_index_expected
Log expected value for a fleet index, where the units of the measurement depend on the specified unit...
Definition rcpp_fleet.hpp:236
VariableVector catch_expected
Total expected catch for a specific fleet, where the units of the measurement depend on the specified...
Definition rcpp_fleet.hpp:191
VariableVector catch_numbers_at_length
Annual catch at length in numbers for a specific fleet.
Definition rcpp_fleet.hpp:178
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:431
void resize(size_t size)
Resizes a RealVector to the desired length.
Definition rcpp_interface_base.hpp:609
A class that provides shared ownership of a boolean value.
Definition rcpp_shared_primitive.hpp:1704
bool get() const
Retrieves the boolean value managed by the object.
Definition rcpp_shared_primitive.hpp:1809
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
A class that provides shared ownership of a string.
Definition rcpp_shared_primitive.hpp:1513
std::string get() const
Retrieves the string value managed by the object.
Definition rcpp_shared_primitive.hpp:1617
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
size_t size()
Returns the size of a VariableVector.
Definition rcpp_interface_base.hpp:287
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:648
std::string to_string(T v)
Definition def.hpp:735
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:664
Declare the fleet functor class which is the base class for all fleet functors.
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...