FIMS  v0.8.1
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 "../../../population_dynamics/growth/growth.hpp"
14
20public:
24 static uint32_t id_g;
34 static std::map<uint32_t, std::shared_ptr<GrowthInterfaceBase>> live_objects;
35
40 this->id = GrowthInterfaceBase::id_g++;
41 /* Create instance of map: key is id and value is pointer to
42 GrowthInterfaceBase */
43 // GrowthInterfaceBase::live_objects[this->id] =
44 // std::make_shared<GrowthInterfaceBase>(*this);
45 }
46
53
58
62 virtual uint32_t get_id() = 0;
63
68 virtual double evaluate(double age) = 0;
69};
70// static id of the GrowthInterfaceBase object
72// local id of the GrowthInterfaceBase object map relating the ID of the
73// GrowthInterfaceBase to the GrowthInterfaceBase objects
74std::map<uint32_t, std::shared_ptr<GrowthInterfaceBase>>
76
83public:
101 std::shared_ptr<std::map<int, std::map<double, double>>> ewaa;
105 bool initialized = false;
106
111 this->ewaa = std::make_shared<std::map<int, std::map<double, double>>>();
113 std::make_shared<EWAAGrowthInterface>(*this);
115 std::make_shared<EWAAGrowthInterface>(*this));
116 }
117
127
132
137 virtual uint32_t get_id() { return this->id; }
138
146 inline std::map<int, std::map<double, double>> make_map(RealVector ages, RealVector weights,
148 std::map<int, std::map<double, double>> mymap;
149 const size_t n_years_plus_one = static_cast<size_t>(n_years.get() + 1);
150
151 // Reject invalid year counts because map keys are expected to include
152 // at least one model year.
153 if (n_years.get() < 1) {
154 Rcpp::stop("EWAA Error:: n_years must be at least 1");
155 }
156
157 // Reject empty vectors because we need at least one age-weight pair.
158 if (weights.size() == 0 || ages.size() == 0) {
159 Rcpp::stop("EWAA Error:: ages and weights must have at least one value");
160 }
161
162 // Accept either:
163 // 1) one weight vector by age (shared across years), or
164 // 2) a full year-by-age matrix flattened as (n_years + 1) * n_ages.
165 if ((weights.size() != ages.size() * n_years_plus_one) &&
166 (weights.size() != ages.size())) {
167 Rcpp::stop("weights size does not match ages size or ages size times "
168 "(n_years + 1), where the plus one is for the beginning "
169 "of the year after the terminal year spawning-biomass "
170 "calculations.");
171 } else if (weights.size() == ages.size()) {
172 // One age-specific vector was provided, so replicate the same
173 // weight-at-age values for every year key (0 through n_years).
174 for (size_t y = 0; y < n_years_plus_one; y++) {
175 for (size_t i = 0; i < ages.size(); i++) {
176 mymap[y][ages[i]] = weights[i];
177 }
178 }
179 } else if (weights.size() == ages.size() * n_years_plus_one) {
180 // A flattened year-by-age matrix was provided, so map each block of
181 // n_ages values to the corresponding year key (0 through n_years).
182 for (size_t y = 0; y < n_years_plus_one; y++) {
183 for (size_t i = 0; i < ages.size(); i++) {
184 mymap[y][ages[i]] = weights[y * ages.size() + i];
185 }
186 }
187 }
188 return mymap;
189 }
190
196 virtual double evaluate(double age) {
198
199 // Build the EWAA map once from R inputs the first time evaluate() is
200 // called.
201 if (initialized == false) {
202 EWAAGrowth.ewaa = make_map(this->ages, this->weights, this->n_years);
203 initialized = true;
204 } else {
205 // Prevent re-initializing this object with a second evaluate() call.
206 Rcpp::stop(
207 "this empirical weight at age object is already initialized");
208 }
209 return EWAAGrowth.evaluate(0, age);
210 }
211
219 virtual std::string to_json() {
220 std::stringstream ss;
221 ss << "{\n";
222 ss << " \"module_name\": \"Growth\",\n";
223 ss << " \"module_type\": \"EWAA\",\n";
224 ss << " \"module_id\":" << this->id << ",\n";
225 ss << " \"parameters\": [\n{\n";
226 ss << " \"name\": \"weight_at_age\",\n";
227 ss << " \"id\": null,\n";
228 ss << " \"type\": \"vector\",\n";
229 ss << " \"dimensionality\": {\n";
230 ss << " \"header\": [\"n_years+1\",\"n_ages\"],\n";
231 ss << " \"dimensions\": [" << this->n_years.get() + 1 << "," << this->ages.size() << "]\n},\n";
232
233 ss << " \"values\": [\n";
234 for (size_t i = 0; i < weights.size() - 1; i++) {
235 ss << "{\n";
236 ss << "\"id\": null,\n";
237 ss << "\"value\": " << weights[i] << ",\n";
238 ss << "\"estimated_value\": " << weights[i] << ",\n";
239 ss << "\"estimation_type\": \"constant\"\n";
240 ss << "},\n";
241 }
242 ss << "{\n";
243 ss << "\"id\": null,\n";
244 ss << "\"value\": " << weights[weights.size() - 1] << ",\n";
245 ss << "\"estimated_value\": " << weights[weights.size() - 1] << ",\n";
246 ss << "\"estimation_type\": \"constant\"\n";
247 ss << "}\n]\n";
248 ss << "}\n]\n}\n";
249 return ss.str();
250 }
251
252#ifdef TMB_MODEL
253
254 template <typename Type> bool add_to_fims_tmb_internal() {
255 std::shared_ptr<fims_info::Information<Type>> info =
257
258 std::shared_ptr<fims_popdy::EWAAGrowth<Type>> ewaa_growth =
259 std::make_shared<fims_popdy::EWAAGrowth<Type>>();
260
261 // set relative info
262 ewaa_growth->id = this->id;
263 ewaa_growth->ewaa =
264 make_map(this->ages, this->weights, this->n_years); // this->ewaa;
265 // add to Information
266 info->growth_models[ewaa_growth->id] = ewaa_growth;
267
268 return true;
269 }
270
275 virtual bool add_to_fims_tmb() {
278
279 return true;
280 }
281
282#endif
283 };
284
285#endif
Rcpp interface for EWAAGrowth to instantiate the object from R: ewaa <- methods::new(EWAAGrowth)....
Definition rcpp_growth.hpp:82
virtual ~EWAAGrowthInterface()
The destructor.
Definition rcpp_growth.hpp:131
SharedInt n_years
An integer specifying the number of years.
Definition rcpp_growth.hpp:96
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:101
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_growth.hpp:137
RealVector ages
Ages (years) for each age class.
Definition rcpp_growth.hpp:91
virtual double evaluate(double age)
Evaluate the growth using empirical weight at age.
Definition rcpp_growth.hpp:196
EWAAGrowthInterface(const EWAAGrowthInterface &other)
Construct a new EWAAGrowthInterface object.
Definition rcpp_growth.hpp:123
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:146
bool initialized
Have weight and age vectors been set? The default is false.
Definition rcpp_growth.hpp:105
RealVector weights
Weights (mt) for each age class.
Definition rcpp_growth.hpp:87
EWAAGrowthInterface()
The constructor.
Definition rcpp_growth.hpp:110
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_growth.hpp:219
Base class for all interface objects.
Definition rcpp_interface_base.hpp:574
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:584
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:589
Rcpp interface that serves as the parent class for Rcpp growth interfaces. This type should be inheri...
Definition rcpp_growth.hpp:19
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:34
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:52
static uint32_t id_g
The static id of the GrowthInterfaceBase object.
Definition rcpp_growth.hpp:24
virtual ~GrowthInterfaceBase()
The destructor.
Definition rcpp_growth.hpp:57
GrowthInterfaceBase()
The constructor.
Definition rcpp_growth.hpp:39
uint32_t id
The local id of the GrowthInterfaceBase object.
Definition rcpp_growth.hpp:28
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:374
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:535
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
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:238
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:239
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...
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