FIMS  v0.9.3
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
13#include "../../../population_dynamics/population/population.hpp"
14
20 public:
24 static uint32_t id_g;
34 static std::map<uint32_t, std::shared_ptr<PopulationInterfaceBase>>
36
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;
155 SharedString name = fims::to_string("NA");
156
157 // Population based derived quantities
163
169
174
179
184
189
195
224
229
234 this->proportion_female[0].initial_value_m = static_cast<double>(0.5);
235 this->proportion_female[0].estimation_type_m.set("constant");
236 this->fleet_ids = std::make_shared<std::set<uint32_t>>();
237 std::shared_ptr<PopulationInterface> population =
238 std::make_shared<PopulationInterface>(*this);
241 }
242
280
285
290 virtual uint32_t get_id() { return this->id; }
291
296 void SetName(const std::string &name) { this->name.set(name); }
297
302 std::string GetName() const { return this->name.get(); }
303
309 this->maturity_id.set(maturity_id);
310 }
311
316 void SetGrowthID(uint32_t growth_id) { this->growth_id.set(growth_id); }
317
323 this->recruitment_id.set(recruitment_id);
324 }
325
330 void AddFleet(uint32_t fleet_id) { this->fleet_ids->insert(fleet_id); }
331
336 virtual void finalize() {
337 if (this->finalized) {
338 // log warning that finalize has been called more than once.
339 FIMS_WARNING_LOG("Population " + fims::to_string(this->id) +
340 " has been finalized already.");
341 }
342
343 this->finalized = true; // indicate this has been called already
344
345 std::shared_ptr<fims_info::Information<double>> info =
347
349
350 it = info->populations.find(this->id);
351
352 std::shared_ptr<fims_popdy::Population<double>> pop =
353 info->populations[this->id];
354 it = info->populations.find(this->id);
355 if (it == info->populations.end()) {
356 FIMS_WARNING_LOG("Population " + fims::to_string(this->id) +
357 " not found in Information.");
358 return;
359 } else {
360 for (size_t i = 0; i < this->log_M.size(); i++) {
361 if (this->log_M[i].estimation_type_m.get() == "constant") {
362 this->log_M[i].final_value_m = this->log_M[i].initial_value_m;
363 } else {
364 this->log_M[i].final_value_m = pop->log_M[i];
365 }
366 }
367
368 for (size_t i = 0; i < this->log_f_multiplier.size(); i++) {
369 if (this->log_f_multiplier[i].estimation_type_m.get() == "constant") {
370 this->log_f_multiplier[i].final_value_m =
371 this->log_f_multiplier[i].initial_value_m;
372 } else {
373 this->log_f_multiplier[i].final_value_m = pop->log_f_multiplier[i];
374 }
375 }
376
377 for (size_t i = 0; i < this->log_init_naa.size(); i++) {
378 if (this->log_init_naa[i].estimation_type_m.get() == "constant") {
379 this->log_init_naa[i].final_value_m =
380 this->log_init_naa[i].initial_value_m;
381 } else {
382 this->log_init_naa[i].final_value_m = pop->log_init_naa[i];
383 }
384 }
385
386 for (size_t i = 0; i < this->proportion_female.size(); i++) {
387 if (this->proportion_female[i].estimation_type_m.get() == "constant") {
388 this->proportion_female[i].final_value_m =
389 this->proportion_female[i].initial_value_m;
390 } else {
391 this->proportion_female[i].final_value_m =
392 pop->proportion_female.get_force_scalar(i);
393 }
394 }
395 }
396 }
397
398#ifdef TMB_MODEL
399
400 template <typename Type>
402 std::shared_ptr<fims_info::Information<Type>> info =
404
405 std::shared_ptr<fims_popdy::Population<Type>> population =
406 std::make_shared<fims_popdy::Population<Type>>();
407
408 std::stringstream ss;
409
410 // set relative info
411 population->id = this->id;
412 population->n_years = this->n_years.get();
413 population->n_fleets = this->n_fleets.get();
414 // only define ages if n_ages greater than 0
415 if (this->n_ages.get() > 0) {
416 population->n_ages = this->n_ages.get();
417 if (static_cast<size_t>(this->n_ages.get()) == this->ages.size()) {
418 population->ages.resize(this->n_ages.get());
419 } else {
420 throw std::invalid_argument(
421 "The size of the ages vector for population " +
422 fims::to_string(this->id) + " is not equal to n_ages.");
423 }
424 }
425
426 fleet_ids_iterator it;
427 for (it = this->fleet_ids->begin(); it != this->fleet_ids->end(); it++) {
428 population->fleet_ids.insert(*it);
429 }
430
431 population->growth_id = this->growth_id.get();
432 population->recruitment_id = this->recruitment_id.get();
433 population->maturity_id = this->maturity_id.get();
434 population->log_M.resize(this->log_M.size());
435
436 if (this->log_f_multiplier.size() ==
437 static_cast<size_t>(this->n_years.get())) {
438 population->log_f_multiplier.resize(this->log_f_multiplier.size());
439 } else {
441 "The log_f_multiplier vector is not of size n_years. Filling with "
442 "zeros.");
443 this->log_f_multiplier.resize((this->n_years.get()));
444 for (size_t i = 0; i < log_f_multiplier.size(); i++) {
445 this->log_f_multiplier[i].initial_value_m = static_cast<double>(0.0);
446 this->log_f_multiplier[i].estimation_type_m.set("constant");
447 }
448 population->log_f_multiplier.resize(this->log_f_multiplier.size());
449 }
450
451 if (this->spawning_biomass_ratio.size() ==
452 static_cast<size_t>(this->n_years.get() + 1)) {
453 population->spawning_biomass_ratio.resize(
454 this->spawning_biomass_ratio.size());
455 } else {
457 "Setting spawning_biomass_ratio vector to size n_years + 1.");
458 this->spawning_biomass_ratio.resize((this->n_years.get() + 1));
459 population->spawning_biomass_ratio.resize(
460 this->spawning_biomass_ratio.size());
461 }
462 info->variable_map[this->spawning_biomass_ratio.id_m] =
464
465 population->log_init_naa.resize(this->log_init_naa.size());
466 for (size_t i = 0; i < log_M.size(); i++) {
467 population->log_M[i] = this->log_M[i].initial_value_m;
468 if (this->log_M[i].estimation_type_m.get() == "fixed_effects") {
469 ss.str("");
470 ss << "Population." << this->id << ".log_M." << this->log_M[i].id_m;
471 info->RegisterParameterName(ss.str());
472 info->RegisterParameter(population->log_M[i]);
473 }
474 if (this->log_M[i].estimation_type_m.get() == "random_effects") {
475 ss.str("");
476 ss << "Population." << this->id << ".log_M." << this->log_M[i].id_m;
477 info->RegisterRandomEffectName(ss.str());
478 info->RegisterRandomEffect(population->log_M[i]);
479 }
480 }
481 info->variable_map[this->log_M.id_m] = &(population)->log_M;
482
483 for (size_t i = 0; i < log_f_multiplier.size(); i++) {
484 population->log_f_multiplier[i] =
485 this->log_f_multiplier[i].initial_value_m;
486 if (this->log_f_multiplier[i].estimation_type_m.get() ==
487 "fixed_effects") {
488 ss.str("");
489 ss << "Population." << this->id << ".log_f_multiplier."
490 << this->log_f_multiplier[i].id_m;
491 info->RegisterParameterName(ss.str());
492 info->RegisterParameter(population->log_f_multiplier[i]);
493 }
494 if (this->log_f_multiplier[i].estimation_type_m.get() ==
495 "random_effects") {
496 ss.str("");
497 ss << "Population." << this->id << ".log_f_multiplier."
498 << this->log_f_multiplier[i].id_m;
499 info->RegisterRandomEffectName(ss.str());
500 info->RegisterRandomEffect(population->log_f_multiplier[i]);
501 }
502 }
503 info->variable_map[this->log_f_multiplier.id_m] =
505
506 for (size_t i = 0; i < log_init_naa.size(); i++) {
507 population->log_init_naa[i] = this->log_init_naa[i].initial_value_m;
508 if (this->log_init_naa[i].estimation_type_m.get() == "fixed_effects") {
509 ss.str("");
510 ss << "Population." << this->id << ".log_init_naa."
511 << this->log_init_naa[i].id_m;
512 info->RegisterParameterName(ss.str());
513 info->RegisterParameter(population->log_init_naa[i]);
514 }
515 if (this->log_init_naa[i].estimation_type_m.get() == "random_effects") {
516 ss.str("");
517 ss << "Population." << this->id << ".log_init_naa."
518 << this->log_init_naa[i].id_m;
519 info->RegisterRandomEffectName(ss.str());
520 info->RegisterRandomEffect(population->log_init_naa[i]);
521 }
522 }
523 info->variable_map[this->log_init_naa.id_m] = &(population)->log_init_naa;
524
525 if (this->proportion_female.size() == 1 ||
526 this->proportion_female.size() ==
527 static_cast<size_t>(this->n_ages.get())) {
528 population->proportion_female.resize(this->proportion_female.size());
529 } else {
531 "The proportion_female vector is not of size 1 or n_ages. Filling "
532 "with 0.5.");
533 this->proportion_female.resize(1);
534 this->proportion_female[0].initial_value_m = static_cast<double>(0.5);
535 this->proportion_female[0].estimation_type_m.set("constant");
536 population->proportion_female.resize(this->proportion_female.size());
537 }
538
539 for (size_t i = 0; i < this->proportion_female.size(); i++) {
540 if (this->proportion_female[i].initial_value_m < 0.0 ||
541 this->proportion_female[i].initial_value_m > 1.0) {
543 "proportion_female should be in [0, 1]; got " +
544 fims::to_string(this->proportion_female[i].initial_value_m) +
545 " at index " + fims::to_string(i) + ".");
546 }
547 population->proportion_female[i] =
548 this->proportion_female[i].initial_value_m;
549 if (this->proportion_female[i].estimation_type_m.get() ==
550 "fixed_effects") {
551 ss.str("");
552 ss << "Population." << this->id << ".proportion_female."
553 << this->proportion_female[i].id_m;
554 info->RegisterParameterName(ss.str());
555 info->RegisterParameter(population->proportion_female[i]);
556 }
557 if (this->proportion_female[i].estimation_type_m.get() ==
558 "random_effects") {
559 ss.str("");
560 ss << "Population." << this->id << ".proportion_female."
561 << this->proportion_female[i].id_m;
562 info->RegisterRandomEffectName(ss.str());
563 info->RegisterRandomEffect(population->proportion_female[i]);
564 }
565 }
566 info->variable_map[this->proportion_female.id_m] =
568
569 for (size_t i = 0; i < ages.size(); i++) {
570 population->ages[i] = this->ages[i];
571 }
572
573 // add to Information
574 info->populations[population->id] = population;
575
576 return true;
577 }
578
583 virtual bool add_to_fims_tmb() {
586
587 return true;
588 }
589
590#endif
591};
592
593#endif
Base class for all interface objects.
Definition rcpp_interface_base.hpp:628
bool finalized
Is the object already finalized? The default is false.
Definition rcpp_interface_base.hpp:633
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:638
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:643
Rcpp interface that serves as the parent class for Rcpp population interfaces. This type should be in...
Definition rcpp_population.hpp:19
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 uint32_t id_g
The static id of the PopulationInterfaceBase object.
Definition rcpp_population.hpp:24
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
Rcpp interface for a new Population to instantiate from R: population <- methods::new(population)
Definition rcpp_population.hpp:80
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:183
std::shared_ptr< std::set< uint32_t > > fleet_ids
Definition rcpp_population.hpp:93
PopulationInterface()
The constructor.
Definition rcpp_population.hpp:233
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
void SetMaturityID(uint32_t maturity_id)
Sets the unique ID for the Maturity object.
Definition rcpp_population.hpp:308
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 unfished_spawning_biomass
Total theoretical weight of mature fish in the population if no fishing had occurred.
Definition rcpp_population.hpp:213
VariableVector unfished_numbers_at_age
Theoretical population composition in numbers at age if no fishing had occurred.
Definition rcpp_population.hpp:194
VariableVector log_M
The natural log of the natural mortality for each year.
Definition rcpp_population.hpp:125
VariableVector spawning_biomass
Total weight of mature fish in the population.
Definition rcpp_population.hpp:203
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
void SetName(const std::string &name)
Sets the name of the population.
Definition rcpp_population.hpp:296
VariableVector unfished_biomass
Total theoretical weight of all fish in the population if no fishing had occurred.
Definition rcpp_population.hpp:208
std::string GetName() const
Gets the name of the population.
Definition rcpp_population.hpp:302
VariableVector mortality_F
Total annual fishing mortality a population is subject to.
Definition rcpp_population.hpp:173
RealVector ages
Ages that are modeled in the population, the length of this vector should equal "n_ages".
Definition rcpp_population.hpp:151
VariableVector mortality_M
Total annual natural mortality a population is subject to.
Definition rcpp_population.hpp:178
VariableVector spawning_biomass_ratio
The population spawning biomass ratio for each year.
Definition rcpp_population.hpp:129
VariableVector proportion_mature_at_age
Fraction of all fish at a given age that are sexually mature at each age.
Definition rcpp_population.hpp:218
SharedInt recruitment_err_id
The ID of the recruitment process module.
Definition rcpp_population.hpp:121
virtual ~PopulationInterface()
The destructor.
Definition rcpp_population.hpp:284
SharedInt n_lengths
The number of length bins.
Definition rcpp_population.hpp:105
VariableVector sum_selectivity
Sum of selectivity at age across all fleets for a population.
Definition rcpp_population.hpp:228
VariableVector numbers_at_age
Current population composition in numbers at age.
Definition rcpp_population.hpp:188
void SetGrowthID(uint32_t growth_id)
Set the unique ID for the growth object.
Definition rcpp_population.hpp:316
void AddFleet(uint32_t fleet_id)
Add a fleet id to the list of fleets operating on this population.
Definition rcpp_population.hpp:330
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
PopulationInterface(const PopulationInterface &other)
Construct a new Population Interface object.
Definition rcpp_population.hpp:248
VariableVector biomass
Total weight of all fish in the population.
Definition rcpp_population.hpp:199
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_population.hpp:336
VariableVector total_landings_numbers
Total annual landings removed from a population by all fleets in numbers.
Definition rcpp_population.hpp:168
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:223
VariableVector total_landings_weight
Total annual landings removed from a population by all fleets in weight.
Definition rcpp_population.hpp:162
void SetRecruitmentID(uint32_t recruitment_id)
Set the unique ID for the recruitment object.
Definition rcpp_population.hpp:322
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:413
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:584
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:144
Variable & get(size_t pos)
An internal accessor for calling a position of a VariableVector from R.
Definition rcpp_interface_base.hpp:267
uint32_t id_m
The local ID of the Variable object.
Definition rcpp_interface_base.hpp:157
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:283
void resize(size_t size)
Resizes a VariableVector to the desired length.
Definition rcpp_interface_base.hpp:295
size_t size()
Returns the size of a VariableVector.
Definition rcpp_interface_base.hpp:288
std::map< uint32_t, std::shared_ptr< fims_popdy::Population< Type > > > populations
Definition information.hpp:114
std::map< uint32_t, std::shared_ptr< fims_popdy::Population< Type > > >::iterator population_iterator
Definition information.hpp:119
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:263
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:648
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:236
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...