FIMS  v0.8.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
12#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// static id of the PopulationInterfaceBase object
77// local id of the PopulationInterfaceBase object map relating the ID of the
78// PopulationInterfaceBase to the PopulationInterfaceBase objects
79std::map<uint32_t, std::shared_ptr<PopulationInterfaceBase>>
81
87 public:
99 std::shared_ptr<std::set<uint32_t>> fleet_ids;
103 typedef typename std::set<uint32_t>::iterator fleet_ids_iterator;
152 SharedString name = fims::to_string("NA");
153
158 this->fleet_ids = std::make_shared<std::set<uint32_t>>();
159 std::shared_ptr<PopulationInterface> population =
160 std::make_shared<PopulationInterface>(*this);
163 }
164
187
192
197 virtual uint32_t get_id() { return this->id; }
198
203 void SetName(const std::string &name) { this->name.set(name); }
204
209 std::string GetName() const { return this->name.get(); }
210
216 this->maturity_id.set(maturity_id);
217 }
218
223 void SetGrowthID(uint32_t growth_id) { this->growth_id.set(growth_id); }
224
230 this->recruitment_id.set(recruitment_id);
231 }
232
237 void AddFleet(uint32_t fleet_id) { this->fleet_ids->insert(fleet_id); }
238
243 virtual void finalize() {
244 if (this->finalized) {
245 // log warning that finalize has been called more than once.
246 FIMS_WARNING_LOG("Population " + fims::to_string(this->id) +
247 " has been finalized already.");
248 }
249
250 this->finalized = true; // indicate this has been called already
251
252 std::shared_ptr<fims_info::Information<double>> info =
254
256
257 it = info->populations.find(this->id);
258
259 std::shared_ptr<fims_popdy::Population<double>> pop =
260 info->populations[this->id];
261 it = info->populations.find(this->id);
262 if (it == info->populations.end()) {
263 FIMS_WARNING_LOG("Population " + fims::to_string(this->id) +
264 " not found in Information.");
265 return;
266 } else {
267 for (size_t i = 0; i < this->log_M.size(); i++) {
268 if (this->log_M[i].estimation_type_m.get() == "constant") {
269 this->log_M[i].final_value_m = this->log_M[i].initial_value_m;
270 } else {
271 this->log_M[i].final_value_m = pop->log_M[i];
272 }
273 }
274
275 for (size_t i = 0; i < this->log_f_multiplier.size(); i++) {
276 if (this->log_f_multiplier[i].estimation_type_m.get() == "constant") {
277 this->log_f_multiplier[i].final_value_m =
278 this->log_f_multiplier[i].initial_value_m;
279 } else {
280 this->log_f_multiplier[i].final_value_m = pop->log_f_multiplier[i];
281 }
282 }
283
284 for (size_t i = 0; i < this->log_init_naa.size(); i++) {
285 if (this->log_init_naa[i].estimation_type_m.get() == "constant") {
286 this->log_init_naa[i].final_value_m =
287 this->log_init_naa[i].initial_value_m;
288 } else {
289 this->log_init_naa[i].final_value_m = pop->log_init_naa[i];
290 }
291 }
292 }
293 }
294
295#ifdef TMB_MODEL
296
297 template <typename Type>
299 std::shared_ptr<fims_info::Information<Type>> info =
301
302 std::shared_ptr<fims_popdy::Population<Type>> population =
303 std::make_shared<fims_popdy::Population<Type>>();
304
305 std::stringstream ss;
306
307 // set relative info
308 population->id = this->id;
309 population->n_years = this->n_years.get();
310 population->n_fleets = this->n_fleets.get();
311 // only define ages if n_ages greater than 0
312 if (this->n_ages.get() > 0) {
313 population->n_ages = this->n_ages.get();
314 if (static_cast<size_t>(this->n_ages.get()) == this->ages.size()) {
315 population->ages.resize(this->n_ages.get());
316 } else {
317 warning("The ages vector is not of size n_ages.");
318 }
319 }
320
322 for (it = this->fleet_ids->begin(); it != this->fleet_ids->end(); it++) {
323 population->fleet_ids.insert(*it);
324 }
325
326 population->growth_id = this->growth_id.get();
327 population->recruitment_id = this->recruitment_id.get();
328 population->maturity_id = this->maturity_id.get();
329 population->log_M.resize(this->log_M.size());
330
331 if (this->log_f_multiplier.size() ==
332 static_cast<size_t>(this->n_years.get())) {
333 population->log_f_multiplier.resize(this->log_f_multiplier.size());
334 } else {
336 "The log_f_multiplier vector is not of size n_years. Filling with "
337 "zeros.");
338 this->log_f_multiplier.resize((this->n_years.get()));
339 for (size_t i = 0; i < log_f_multiplier.size(); i++) {
340 this->log_f_multiplier[i].initial_value_m = static_cast<double>(0.0);
341 this->log_f_multiplier[i].estimation_type_m.set("constant");
342 }
343 population->log_f_multiplier.resize(this->log_f_multiplier.size());
344 }
345
346 if (this->spawning_biomass_ratio.size() ==
347 static_cast<size_t>(this->n_years.get() + 1)) {
348 population->spawning_biomass_ratio.resize(
349 this->spawning_biomass_ratio.size());
350 } else {
352 "Setting spawning_biomass_ratio vector to size n_years + 1.");
353 this->spawning_biomass_ratio.resize((this->n_years.get() + 1));
354 population->spawning_biomass_ratio.resize(
355 this->spawning_biomass_ratio.size());
356 }
357 info->variable_map[this->spawning_biomass_ratio.id_m] =
359
360 population->log_init_naa.resize(this->log_init_naa.size());
361 for (size_t i = 0; i < log_M.size(); i++) {
362 population->log_M[i] = this->log_M[i].initial_value_m;
363 if (this->log_M[i].estimation_type_m.get() == "fixed_effects") {
364 ss.str("");
365 ss << "Population." << this->id << ".log_M." << this->log_M[i].id_m;
366 info->RegisterParameterName(ss.str());
367 info->RegisterParameter(population->log_M[i]);
368 }
369 if (this->log_M[i].estimation_type_m.get() == "random_effects") {
370 ss.str("");
371 ss << "Population." << this->id << ".log_M." << this->log_M[i].id_m;
372 info->RegisterRandomEffectName(ss.str());
373 info->RegisterRandomEffect(population->log_M[i]);
374 }
375 }
376 info->variable_map[this->log_M.id_m] = &(population)->log_M;
377
378 for (size_t i = 0; i < log_f_multiplier.size(); i++) {
379 population->log_f_multiplier[i] =
380 this->log_f_multiplier[i].initial_value_m;
381 if (this->log_f_multiplier[i].estimation_type_m.get() ==
382 "fixed_effects") {
383 ss.str("");
384 ss << "Population." << this->id << ".log_f_multiplier."
385 << this->log_f_multiplier[i].id_m;
386 info->RegisterParameterName(ss.str());
387 info->RegisterParameter(population->log_f_multiplier[i]);
388 }
389 if (this->log_f_multiplier[i].estimation_type_m.get() ==
390 "random_effects") {
391 ss.str("");
392 ss << "Population." << this->id << ".log_f_multiplier."
393 << this->log_f_multiplier[i].id_m;
394 info->RegisterRandomEffectName(ss.str());
395 info->RegisterRandomEffect(population->log_f_multiplier[i]);
396 }
397 }
398 info->variable_map[this->log_f_multiplier.id_m] =
400
401 for (size_t i = 0; i < log_init_naa.size(); i++) {
402 population->log_init_naa[i] = this->log_init_naa[i].initial_value_m;
403 if (this->log_init_naa[i].estimation_type_m.get() == "fixed_effects") {
404 ss.str("");
405 ss << "Population." << this->id << ".log_init_naa."
406 << this->log_init_naa[i].id_m;
407 info->RegisterParameterName(ss.str());
408 info->RegisterParameter(population->log_init_naa[i]);
409 }
410 if (this->log_init_naa[i].estimation_type_m.get() == "random_effects") {
411 ss.str("");
412 ss << "Population." << this->id << ".log_init_naa."
413 << this->log_init_naa[i].id_m;
414 info->RegisterRandomEffectName(ss.str());
415 info->RegisterRandomEffect(population->log_init_naa[i]);
416 }
417 }
418 info->variable_map[this->log_init_naa.id_m] = &(population)->log_init_naa;
419
420 for (size_t i = 0; i < ages.size(); i++) {
421 population->ages[i] = this->ages[i];
422 }
423
424 // add to Information
425 info->populations[population->id] = population;
426
427 return true;
428 }
429
434 virtual bool add_to_fims_tmb() {
435#ifdef TMBAD_FRAMEWORK
438#else
443#endif
444
445 return true;
446 }
447
448#endif
449};
450
451#endif
Base class for all interface objects.
Definition rcpp_interface_base.hpp:634
bool finalized
Is the object already finalized? The default is false.
Definition rcpp_interface_base.hpp:639
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:644
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:649
An Rcpp interface class that defines the ParameterVector class.
Definition rcpp_interface_base.hpp:178
void resize(size_t size)
Resizes a ParameterVector to the desired length.
Definition rcpp_interface_base.hpp:323
size_t size()
Returns the size of a ParameterVector.
Definition rcpp_interface_base.hpp:316
uint32_t id_m
The local ID of the Parameter object.
Definition rcpp_interface_base.hpp:191
Parameter & get(size_t pos)
An internal accessor for calling a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:295
void set(size_t pos, const Parameter &p)
An internal setter for setting a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:311
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:86
std::set< uint32_t >::iterator fleet_ids_iterator
Definition rcpp_population.hpp:103
std::shared_ptr< std::set< uint32_t > > fleet_ids
Definition rcpp_population.hpp:99
ParameterVector spawning_biomass_ratio
The population spawning biomass ratio for each year.
Definition rcpp_population.hpp:135
PopulationInterface()
The constructor.
Definition rcpp_population.hpp:157
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_population.hpp:197
void SetMaturityID(uint32_t maturity_id)
Sets the unique ID for the Maturity object.
Definition rcpp_population.hpp:215
ParameterVector log_init_naa
The natural log of the initial numbers at age.
Definition rcpp_population.hpp:143
SharedString name
The name for the population.
Definition rcpp_population.hpp:152
SharedInt n_ages
The number of age bins.
Definition rcpp_population.hpp:91
SharedInt maturity_id
The ID of the maturity module.
Definition rcpp_population.hpp:115
ParameterVector log_f_multiplier
Log of the population annual fishing mortality multiplier.
Definition rcpp_population.hpp:139
SharedInt n_years
The number of years.
Definition rcpp_population.hpp:107
void SetName(const std::string &name)
Sets the name of the population.
Definition rcpp_population.hpp:203
std::string GetName() const
Gets the name of the population.
Definition rcpp_population.hpp:209
RealVector ages
Ages that are modeled in the population, the length of this vector should equal "n_ages".
Definition rcpp_population.hpp:148
SharedInt recruitment_err_id
The ID of the recruitment process module.
Definition rcpp_population.hpp:127
ParameterVector log_M
The natural log of the natural mortality for each year.
Definition rcpp_population.hpp:131
virtual ~PopulationInterface()
The destructor.
Definition rcpp_population.hpp:191
SharedInt n_lengths
The number of length bins.
Definition rcpp_population.hpp:111
void SetGrowthID(uint32_t growth_id)
Set the unique ID for the growth object.
Definition rcpp_population.hpp:223
void AddFleet(uint32_t fleet_id)
Add a fleet id to the list of fleets operating on this population.
Definition rcpp_population.hpp:237
SharedInt growth_id
The ID of the growth module.
Definition rcpp_population.hpp:119
SharedInt recruitment_id
The ID of the recruitment module.
Definition rcpp_population.hpp:123
PopulationInterface(const PopulationInterface &other)
Construct a new Population Interface object.
Definition rcpp_population.hpp:170
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_population.hpp:243
SharedInt n_fleets
The number of fleets.
Definition rcpp_population.hpp:95
void SetRecruitmentID(uint32_t recruitment_id)
Set the unique ID for the recruitment object.
Definition rcpp_population.hpp:229
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:434
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:595
A class that provides shared ownership of a boolean value.
Definition rcpp_shared_primitive.hpp:1699
A class that provides shared ownership of an integer value.
Definition rcpp_shared_primitive.hpp:24
int get() const
Retrieve the value of the integer.
Definition rcpp_shared_primitive.hpp:122
A class that provides shared ownership of a string.
Definition rcpp_shared_primitive.hpp:1508
std::string get() const
Retrieves the string value managed by the object.
Definition rcpp_shared_primitive.hpp:1612
std::map< uint32_t, std::shared_ptr< fims_popdy::Population< Type > > > populations
Definition information.hpp:115
std::map< uint32_t, std::shared_ptr< fims_popdy::Population< Type > > >::iterator population_iterator
Definition information.hpp:120
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:238
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:598
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:279
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...