FIMS  v0.11.0
Loading...
Searching...
No Matches
rcpp_population.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_POPULATION_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_POPULATION_HPP
11
14
20 public:
24 static uint32_t id_g;
28 uint32_t id;
34 static std::map<uint32_t, std::shared_ptr<PopulationInterfaceBase>>
36
51 this->id = PopulationInterfaceBase::id_g++;
52 /* Create instance of map: key is id and value is pointer to
53 PopulationInterfaceBase */
54 // PopulationInterfaceBase::live_objects[this->id] = this;
55 }
56
64
69
73 virtual uint32_t get_id() = 0;
74};
75
81 public:
93 std::shared_ptr<std::set<uint32_t>> fleet_ids;
97 typedef typename std::set<uint32_t>::iterator fleet_ids_iterator;
166
167 // Population based derived quantities
173
179
184
189
194
199
205
234
239
244 this->proportion_female[0].initial_value_m = static_cast<double>(0.5);
245 this->proportion_female[0].estimation_type_m.set("constant");
246 this->fleet_ids = std::make_shared<std::set<uint32_t>>();
247 std::shared_ptr<PopulationInterface> population =
248 std::make_shared<PopulationInterface>(*this);
250 PopulationInterfaceBase::live_objects[this->id] = population;
251 }
252
292
297
302 virtual uint32_t get_id() { return this->id; }
303
308 void SetName(const std::string &name) { this->name.set(name); }
309
314 std::string GetName() const { return this->name.get(); }
315
320 void SetMaturityID(uint32_t maturity_id) {
321 this->maturity_id.set(maturity_id);
322 }
323
328 void SetGrowthID(uint32_t growth_id) { this->growth_id.set(growth_id); }
329
335 this->recruitment_id.set(recruitment_id);
336 }
337
342 void AddFleet(uint32_t fleet_id) { this->fleet_ids->insert(fleet_id); }
343
348 virtual void finalize() {
349 if (this->finalized) {
350 // log warning that finalize has been called more than once.
351 FIMS_WARNING_LOG("Population " + fims::to_string(this->id) +
352 " has been finalized already.");
353 }
354
355 this->finalized = true; // indicate this has been called already
356
357 std::shared_ptr<fims_info::Information<double>> info =
359
361
362 it = info->populations.find(this->id);
363
364 std::shared_ptr<fims_popdy::Population<double>> pop =
365 info->populations[this->id];
366 it = info->populations.find(this->id);
367 if (it == info->populations.end()) {
368 FIMS_WARNING_LOG("Population " + fims::to_string(this->id) +
369 " not found in Information.");
370 return;
371 } else {
372 for (size_t i = 0; i < this->log_M.size(); i++) {
373 if (this->log_M[i].estimation_type_m.get() == "constant") {
374 this->log_M[i].final_value_m = this->log_M[i].initial_value_m;
375 } else {
376 this->log_M[i].final_value_m = pop->log_M[i];
377 }
378 }
379
380 for (size_t i = 0; i < this->log_f_multiplier.size(); i++) {
381 if (this->log_f_multiplier[i].estimation_type_m.get() == "constant") {
382 this->log_f_multiplier[i].final_value_m =
383 this->log_f_multiplier[i].initial_value_m;
384 } else {
385 this->log_f_multiplier[i].final_value_m = pop->log_f_multiplier[i];
386 }
387 }
388
389 for (size_t i = 0; i < this->log_init_naa.size(); i++) {
390 if (this->log_init_naa[i].estimation_type_m.get() == "constant") {
391 this->log_init_naa[i].final_value_m =
392 this->log_init_naa[i].initial_value_m;
393 } else {
394 this->log_init_naa[i].final_value_m = pop->log_init_naa[i];
395 }
396 }
397
398 for (size_t i = 0; i < this->proportion_female.size(); i++) {
399 if (this->proportion_female[i].estimation_type_m.get() == "constant") {
400 this->proportion_female[i].final_value_m =
401 this->proportion_female[i].initial_value_m;
402 } else {
403 this->proportion_female[i].final_value_m =
404 pop->proportion_female.get_force_scalar(i);
405 }
406 }
407 }
408 }
409
410#ifdef TMB_MODEL
411
412 template <typename Type>
413 bool add_to_fims_tmb_internal() {
414 std::shared_ptr<fims_info::Information<Type>> info =
416
417 std::shared_ptr<fims_popdy::Population<Type>> population =
418 std::make_shared<fims_popdy::Population<Type>>();
419
420 std::stringstream ss;
421
422 // set relative info
423 population->id = this->id;
424 population->n_years = this->n_years.get();
425 population->n_fleets = this->n_fleets.get();
426 // only define ages if n_ages greater than 0
427 if (this->n_ages.get() > 0) {
428 population->n_ages = this->n_ages.get();
429 if (static_cast<size_t>(this->n_ages.get()) == this->ages.size()) {
430 population->ages.resize(this->n_ages.get());
431 } else {
432 throw std::invalid_argument(
433 "The size of the ages vector for population " +
434 fims::to_string(this->id) + " is not equal to n_ages.");
435 }
436 }
437
439 for (it = this->fleet_ids->begin(); it != this->fleet_ids->end(); it++) {
440 population->fleet_ids.insert(*it);
441 }
442
443 population->growth_id = this->growth_id.get();
444 population->recruitment_id = this->recruitment_id.get();
445 population->maturity_id = this->maturity_id.get();
446 population->log_M.resize(this->log_M.size());
447
448 if (this->log_f_multiplier.size() ==
449 static_cast<size_t>(this->n_years.get())) {
450 population->log_f_multiplier.resize(this->log_f_multiplier.size());
451 } else {
453 "The log_f_multiplier vector is not of size n_years. Filling with "
454 "zeros.");
455 this->log_f_multiplier.resize((this->n_years.get()));
456 for (size_t i = 0; i < log_f_multiplier.size(); i++) {
457 this->log_f_multiplier[i].initial_value_m = static_cast<double>(0.0);
458 this->log_f_multiplier[i].estimation_type_m.set("constant");
459 }
460 population->log_f_multiplier.resize(this->log_f_multiplier.size());
461 }
462
463 if (this->spawning_biomass_ratio.size() ==
464 static_cast<size_t>(this->n_years.get() + 1)) {
465 population->spawning_biomass_ratio.resize(
466 this->spawning_biomass_ratio.size());
467 } else {
469 "Setting spawning_biomass_ratio vector to size n_years + 1.");
470 this->spawning_biomass_ratio.resize((this->n_years.get() + 1));
471 population->spawning_biomass_ratio.resize(
472 this->spawning_biomass_ratio.size());
473 }
474 info->variable_map[this->spawning_biomass_ratio.id_m] =
475 &(population)->spawning_biomass_ratio;
476
477 population->log_init_naa.resize(this->log_init_naa.size());
478 for (size_t i = 0; i < log_M.size(); i++) {
479 population->log_M[i] = this->log_M[i].initial_value_m;
480 if (this->log_M[i].estimation_type_m.get() == "fixed_effects") {
481 ss.str("");
482 ss << "Population." << this->id << ".log_M." << this->log_M[i].id_m;
483 info->RegisterParameterName(ss.str());
484 info->RegisterParameter(population->log_M[i]);
485 }
486 if (this->log_M[i].estimation_type_m.get() == "random_effects") {
487 ss.str("");
488 ss << "Population." << this->id << ".log_M." << this->log_M[i].id_m;
489 info->RegisterRandomEffectName(ss.str());
490 info->RegisterRandomEffect(population->log_M[i]);
491 }
492 }
493 info->variable_map[this->log_M.id_m] = &(population)->log_M;
494
495 for (size_t i = 0; i < log_f_multiplier.size(); i++) {
496 population->log_f_multiplier[i] =
497 this->log_f_multiplier[i].initial_value_m;
498 if (this->log_f_multiplier[i].estimation_type_m.get() ==
499 "fixed_effects") {
500 ss.str("");
501 ss << "Population." << this->id << ".log_f_multiplier."
502 << this->log_f_multiplier[i].id_m;
503 info->RegisterParameterName(ss.str());
504 info->RegisterParameter(population->log_f_multiplier[i]);
505 }
506 if (this->log_f_multiplier[i].estimation_type_m.get() ==
507 "random_effects") {
508 ss.str("");
509 ss << "Population." << this->id << ".log_f_multiplier."
510 << this->log_f_multiplier[i].id_m;
511 info->RegisterRandomEffectName(ss.str());
512 info->RegisterRandomEffect(population->log_f_multiplier[i]);
513 }
514 }
515 info->variable_map[this->log_f_multiplier.id_m] =
516 &(population)->log_f_multiplier;
517
518 for (size_t i = 0; i < log_init_naa.size(); i++) {
519 population->log_init_naa[i] = this->log_init_naa[i].initial_value_m;
520 if (this->log_init_naa[i].estimation_type_m.get() == "fixed_effects") {
521 ss.str("");
522 ss << "Population." << this->id << ".log_init_naa."
523 << this->log_init_naa[i].id_m;
524 info->RegisterParameterName(ss.str());
525 info->RegisterParameter(population->log_init_naa[i]);
526 }
527 if (this->log_init_naa[i].estimation_type_m.get() == "random_effects") {
528 ss.str("");
529 ss << "Population." << this->id << ".log_init_naa."
530 << this->log_init_naa[i].id_m;
531 info->RegisterRandomEffectName(ss.str());
532 info->RegisterRandomEffect(population->log_init_naa[i]);
533 }
534 }
535 info->variable_map[this->log_init_naa.id_m] = &(population)->log_init_naa;
536
537 if (this->proportion_female.size() == 1 ||
538 this->proportion_female.size() ==
539 static_cast<size_t>(this->n_ages.get())) {
540 population->proportion_female.resize(this->proportion_female.size());
541 } else {
543 "The proportion_female vector is not of size 1 or n_ages. Filling "
544 "with 0.5.");
545 this->proportion_female.resize(1);
546 this->proportion_female[0].initial_value_m = static_cast<double>(0.5);
547 this->proportion_female[0].estimation_type_m.set("constant");
548 population->proportion_female.resize(this->proportion_female.size());
549 }
550
551 for (size_t i = 0; i < this->proportion_female.size(); i++) {
552 if (this->proportion_female[i].initial_value_m < 0.0 ||
553 this->proportion_female[i].initial_value_m > 1.0) {
555 "proportion_female should be in [0, 1]; got " +
556 fims::to_string(this->proportion_female[i].initial_value_m) +
557 " at index " + fims::to_string(i) + ".");
558 }
559 population->proportion_female[i] =
560 this->proportion_female[i].initial_value_m;
561 if (this->proportion_female[i].estimation_type_m.get() ==
562 "fixed_effects") {
563 ss.str("");
564 ss << "Population." << this->id << ".proportion_female."
565 << this->proportion_female[i].id_m;
566 info->RegisterParameterName(ss.str());
567 info->RegisterParameter(population->proportion_female[i]);
568 }
569 if (this->proportion_female[i].estimation_type_m.get() ==
570 "random_effects") {
571 ss.str("");
572 ss << "Population." << this->id << ".proportion_female."
573 << this->proportion_female[i].id_m;
574 info->RegisterRandomEffectName(ss.str());
575 info->RegisterRandomEffect(population->proportion_female[i]);
576 }
577 }
578 info->variable_map[this->proportion_female.id_m] =
579 &(population)->proportion_female;
580
581 for (size_t i = 0; i < ages.size(); i++) {
582 population->ages[i] = this->ages[i];
583 }
584
585 if (this->n_lengths.get() > 0) {
586 if (this->lengths.size() != static_cast<size_t>(this->n_lengths.get())) {
587 FIMS_ERROR_LOG("The size of `lengths` does not match `n_lengths`: " +
588 fims::to_string(this->lengths.size()) +
589 " != " + fims::to_string(this->n_lengths.get()));
590 throw std::invalid_argument(
591 "The size of the lengths vector for population " +
592 fims::to_string(this->id) + " is not equal to n_lengths.");
593 }
594 population->lengths.resize(this->lengths.size());
595 for (size_t i = 0; i < this->lengths.size(); i++) {
596 population->lengths[i] = this->lengths[i];
597 }
598 }
599
600 // add to Information
601 info->populations[population->id] = population;
602
603 return true;
604 }
605
610 virtual bool add_to_fims_tmb() {
611 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
612 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
613
614 return true;
615 }
616
617#endif
618};
619
620#endif
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
virtual ~PopulationInterfaceBase()
The destructor.
Definition rcpp_population.hpp:68
PopulationInterfaceBase()
The constructor.
Definition rcpp_population.hpp:50
SharedBoolean initialize_surplus_production
Initialize the surplus production model.
Definition rcpp_population.hpp:46
uint32_t id
The local id of the PopulationInterfaceBase object.
Definition rcpp_population.hpp:28
virtual uint32_t get_id()=0
Get the ID for the child population interface objects to inherit.
PopulationInterfaceBase(const PopulationInterfaceBase &other)
Construct a new Population Interface Base object.
Definition rcpp_population.hpp:62
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
SharedBoolean initialize_catch_at_age
Initialize the catch at age model.
Definition rcpp_population.hpp:41
std::set< uint32_t >::iterator fleet_ids_iterator
Definition rcpp_population.hpp:97
VariableVector mortality_Z
Total annual mortality a population is subject to.
Definition rcpp_population.hpp:193
std::shared_ptr< std::set< uint32_t > > fleet_ids
Definition rcpp_population.hpp:93
PopulationInterface()
The constructor.
Definition rcpp_population.hpp:243
VariableVector proportion_female
Proportion of females in the population.
Definition rcpp_population.hpp:151
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_population.hpp:302
VariableVector log_init_naa
The natural log of the initial numbers at age.
Definition rcpp_population.hpp:142
void SetMaturityID(uint32_t maturity_id)
Sets the unique ID for the Maturity object.
Definition rcpp_population.hpp:320
RealVector lengths
Lower edges of the population length bins, from length_bin rows with fleet = NA. The length of this v...
Definition rcpp_population.hpp:161
SharedString name
The name for the population.
Definition rcpp_population.hpp:165
SharedInt n_ages
The number of age bins.
Definition rcpp_population.hpp:85
SharedInt maturity_id
The ID of the maturity module.
Definition rcpp_population.hpp:114
VariableVector unfished_spawning_biomass
Total theoretical weight of mature fish in the population if no fishing had occurred.
Definition rcpp_population.hpp:223
VariableVector unfished_numbers_at_age
Theoretical population composition in numbers at age if no fishing had occurred.
Definition rcpp_population.hpp:204
VariableVector log_M
The natural log of the natural mortality for each year.
Definition rcpp_population.hpp:130
VariableVector spawning_biomass
Total weight of mature fish in the population.
Definition rcpp_population.hpp:213
SharedInt n_years
The number of years.
Definition rcpp_population.hpp:101
VariableVector log_f_multiplier
Log of the population annual fishing mortality multiplier.
Definition rcpp_population.hpp:138
void SetName(const std::string &name)
Sets the name of the population.
Definition rcpp_population.hpp:308
VariableVector unfished_biomass
Total theoretical weight of all fish in the population if no fishing had occurred.
Definition rcpp_population.hpp:218
std::string GetName() const
Gets the name of the population.
Definition rcpp_population.hpp:314
VariableVector total_catch_numbers
Total annual catch removed from a population by all fleets in numbers.
Definition rcpp_population.hpp:178
VariableVector mortality_F
Total annual fishing mortality a population is subject to.
Definition rcpp_population.hpp:183
RealVector ages
Ages that are modeled in the population, the length of this vector should equal "n_ages".
Definition rcpp_population.hpp:156
VariableVector mortality_M
Total annual natural mortality a population is subject to.
Definition rcpp_population.hpp:188
VariableVector spawning_biomass_ratio
The population spawning biomass ratio for each year.
Definition rcpp_population.hpp:134
VariableVector proportion_mature_at_age
Fraction of all fish at a given age that are sexually mature at each age.
Definition rcpp_population.hpp:228
SharedInt recruitment_err_id
The ID of the recruitment process module.
Definition rcpp_population.hpp:126
virtual ~PopulationInterface()
The destructor.
Definition rcpp_population.hpp:296
SharedInt n_lengths
The number of population length bins, 0 when FIMS builds the bins from the fleet bins.
Definition rcpp_population.hpp:110
VariableVector sum_selectivity
Sum of selectivity at age across all fleets for a population.
Definition rcpp_population.hpp:238
VariableVector numbers_at_age
Current population composition in numbers at age.
Definition rcpp_population.hpp:198
void SetGrowthID(uint32_t growth_id)
Set the unique ID for the growth object.
Definition rcpp_population.hpp:328
void AddFleet(uint32_t fleet_id)
Add a fleet id to the list of fleets operating on this population.
Definition rcpp_population.hpp:342
SharedInt growth_id
The ID of the growth module.
Definition rcpp_population.hpp:118
SharedInt recruitment_id
The ID of the recruitment module.
Definition rcpp_population.hpp:122
PopulationInterface(const PopulationInterface &other)
Construct a new Population Interface object.
Definition rcpp_population.hpp:258
VariableVector total_catch_weight
Total annual catch removed from a population by all fleets in weight.
Definition rcpp_population.hpp:172
VariableVector biomass
Total weight of all fish in the population.
Definition rcpp_population.hpp:209
SharedInt start_year
The first calendar year in the model.
Definition rcpp_population.hpp:105
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_population.hpp:348
SharedInt n_fleets
The number of fleets.
Definition rcpp_population.hpp:89
VariableVector expected_recruitment
Model-expected recruitment each year based on the stock–recruit relationship.
Definition rcpp_population.hpp:233
void SetRecruitmentID(uint32_t recruitment_id)
Set the unique ID for the recruitment object.
Definition rcpp_population.hpp:334
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:431
A class that provides shared ownership of a boolean value.
Definition rcpp_shared_primitive.hpp:1704
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
void set(size_t pos, const Variable &p)
An internal setter for setting a position of a VariableVector from R.
Definition rcpp_interface_base.hpp:282
size_t size()
Returns the size of a VariableVector.
Definition rcpp_interface_base.hpp:287
std::map< uint32_t, std::shared_ptr< fims_popdy::Population< Type > > > populations
Definition information.hpp:118
std::map< uint32_t, std::shared_ptr< fims_popdy::Population< Type > > >::iterator population_iterator
Definition information.hpp:123
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:262
#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
Defines the Population class and its fields and methods.
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...