FIMS  v0.9.2
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
20 public:
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
83 public:
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
130
135
140 virtual uint32_t get_id() { return this->id; }
141
149 inline std::map<int, std::map<double, double>> make_map(RealVector ages,
152 std::map<int, std::map<double, double>> mymap;
153 const size_t n_years_plus_one = static_cast<size_t>(n_years.get() + 1);
154
155 // Reject invalid year counts because map keys are expected to include
156 // at least one model year.
157 if (n_years.get() < 1) {
158 Rcpp::stop("EWAA Error:: n_years must be at least 1");
159 }
160
161 // Reject empty vectors because we need at least one age-weight pair.
162 if (weights.size() == 0 || ages.size() == 0) {
163 Rcpp::stop("EWAA Error:: ages and weights must have at least one value");
164 }
165
166 // Accept either:
167 // 1) one weight vector by age (shared across years), or
168 // 2) a full year-by-age matrix flattened as (n_years + 1) * n_ages.
169 if ((weights.size() != ages.size() * n_years_plus_one) &&
170 (weights.size() != ages.size())) {
171 Rcpp::stop(
172 "weights size does not match ages size or ages size times "
173 "(n_years + 1), where the plus one is for the beginning "
174 "of the year after the terminal year spawning-biomass "
175 "calculations.");
176 } else if (weights.size() == ages.size()) {
177 // One age-specific vector was provided, so replicate the same
178 // weight-at-age values for every year key (0 through n_years).
179 for (size_t y = 0; y < n_years_plus_one; y++) {
180 for (size_t i = 0; i < ages.size(); i++) {
181 mymap[y][ages[i]] = weights[i];
182 }
183 }
184 } else if (weights.size() == ages.size() * n_years_plus_one) {
185 // A flattened year-by-age matrix was provided, so map each block of
186 // n_ages values to the corresponding year key (0 through n_years).
187 for (size_t y = 0; y < n_years_plus_one; y++) {
188 for (size_t i = 0; i < ages.size(); i++) {
189 mymap[y][ages[i]] = weights[y * ages.size() + i];
190 }
191 }
192 }
193 return mymap;
194 }
195
201 virtual double evaluate(double age) {
203
204 // Build the EWAA map once from R inputs the first time evaluate() is
205 // called.
206 if (initialized == false) {
207 EWAAGrowth.ewaa = make_map(this->ages, this->weights, this->n_years);
208 initialized = true;
209 } else {
210 // Prevent re-initializing this object with a second evaluate() call.
211 Rcpp::stop("this empirical weight at age object is already initialized");
212 }
213 return EWAAGrowth.evaluate(0, age);
214 }
215
223 virtual std::string to_json() {
224 std::stringstream ss;
225 ss << "{\n";
226 ss << " \"module_name\": \"Growth\",\n";
227 ss << " \"module_type\": \"EWAA\",\n";
228 ss << " \"module_id\":" << this->id << ",\n";
229 ss << " \"parameters\": [\n{\n";
230 ss << " \"name\": \"weight_at_age\",\n";
231 ss << " \"id\": null,\n";
232 ss << " \"type\": \"vector\",\n";
233 ss << " \"dimensionality\": {\n";
234 ss << " \"header\": [\"n_years+1\",\"n_ages\"],\n";
235 ss << " \"dimensions\": [" << this->n_years.get() + 1 << ","
236 << this->ages.size() << "]\n},\n";
237
238 ss << " \"values\": [\n";
239 for (size_t i = 0; i < weights.size() - 1; i++) {
240 ss << "{\n";
241 ss << "\"id\": null,\n";
242 ss << "\"value\": " << weights[i] << ",\n";
243 ss << "\"estimated_value\": " << weights[i] << ",\n";
244 ss << "\"estimation_type\": \"constant\"\n";
245 ss << "},\n";
246 }
247 ss << "{\n";
248 ss << "\"id\": null,\n";
249 ss << "\"value\": " << weights[weights.size() - 1] << ",\n";
250 ss << "\"estimated_value\": " << weights[weights.size() - 1] << ",\n";
251 ss << "\"estimation_type\": \"constant\"\n";
252 ss << "}\n]\n";
253 ss << "}\n]\n}\n";
254 return ss.str();
255 }
256
257#ifdef TMB_MODEL
258
259 template <typename Type>
261 std::shared_ptr<fims_info::Information<Type>> info =
263
264 std::shared_ptr<fims_popdy::EWAAGrowth<Type>> ewaa_growth =
265 std::make_shared<fims_popdy::EWAAGrowth<Type>>();
266
267 // set relative info
268 ewaa_growth->id = this->id;
269 ewaa_growth->ewaa =
270 make_map(this->ages, this->weights, this->n_years); // this->ewaa;
271 // add to Information
272 info->growth_models[ewaa_growth->id] = ewaa_growth;
273
274 return true;
275 }
276
281 virtual bool add_to_fims_tmb() {
284
285 return true;
286 }
287
288#endif
289};
290
291#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:134
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:140
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:201
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:149
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:223
Base class for all interface objects.
Definition rcpp_interface_base.hpp:575
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:585
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:590
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:375
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:536
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:235
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