FIMS  v0.11.0
Loading...
Searching...
No Matches
rcpp_maturity.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MATURITY_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_MATURITY_HPP
11
14
20 public:
24 static uint32_t id_g;
28 uint32_t id;
34 static std::map<uint32_t, std::shared_ptr<MaturityInterfaceBase>>
36
41 this->id = MaturityInterfaceBase::id_g++;
42 /* Create instance of map: key is id and value is pointer to
43 MaturityInterfaceBase */
44 // MaturityInterfaceBase::live_objects[this->id] = this;
45 }
46
53
58
62 virtual uint32_t get_id() = 0;
63
68 virtual double evaluate(double x) = 0;
69};
70
76 public:
85
91 std::make_shared<LogisticMaturityInterface>(*this);
94 }
95
105
110
115 virtual uint32_t get_id() { return this->id; }
116
122 virtual double evaluate(double x) {
124 LogisticMat.inflection_point.resize(1);
125 LogisticMat.inflection_point[0] = this->inflection_point[0].initial_value_m;
126 LogisticMat.slope.resize(1);
127 LogisticMat.slope[0] = this->slope[0].initial_value_m;
128 return LogisticMat.evaluate(x);
129 }
130
135 virtual void finalize() {
136 if (this->finalized) {
137 // log warning that finalize has been called more than once.
138 FIMS_WARNING_LOG("Logistic Maturity " + fims::to_string(this->id) +
139 " has been finalized already.");
140 }
141
142 this->finalized = true; // indicate this has been called already
143
144 std::shared_ptr<fims_info::Information<double>> info =
146
148
149 // search for maturity in Information
150 it = info->maturity_models.find(this->id);
151 // if not found, just return
152 if (it == info->maturity_models.end()) {
153 FIMS_WARNING_LOG("Logistic Maturity " + fims::to_string(this->id) +
154 " not found in Information.");
155 return;
156 } else {
157 std::shared_ptr<fims_popdy::LogisticMaturity<double>> mat =
158 std::dynamic_pointer_cast<fims_popdy::LogisticMaturity<double>>(
159 it->second);
160
161 for (size_t i = 0; i < inflection_point.size(); i++) {
162 if (this->inflection_point[i].estimation_type_m.get() == "constant") {
163 this->inflection_point[i].final_value_m =
164 this->inflection_point[i].initial_value_m;
165 } else {
166 this->inflection_point[i].final_value_m = mat->inflection_point[i];
167 }
168 }
169
170 for (size_t i = 0; i < slope.size(); i++) {
171 if (this->slope[i].estimation_type_m.get() == "constant") {
172 this->slope[i].final_value_m = this->slope[i].initial_value_m;
173 } else {
174 this->slope[i].final_value_m = mat->slope[i];
175 }
176 }
177 }
178 }
179
186 virtual std::string to_json() {
187 std::stringstream ss;
188 ss << "{\n";
189 ss << " \"module_name\": \"Maturity\",\n";
190 ss << " \"module_type\": \"Logistic\",\n";
191 ss << " \"module_id\": " << this->id << ",\n";
192
193 ss << " \"parameters\": [\n{\n";
194 ss << " \"name\": \"inflection_point\",\n";
195 ss << " \"id\":" << this->inflection_point.id_m << ",\n";
196 ss << " \"type\": \"vector\",\n";
197 ss << " \"dimensionality\": {\n";
198 ss << " \"header\": [null],\n";
199 ss << " \"dimensions\": [1]\n},\n";
200 ss << " \"values\":" << this->inflection_point << "},\n ";
201
202 ss << "{\n";
203 ss << " \"name\": \"slope\",\n";
204 ss << " \"id\":" << this->slope.id_m << ",\n";
205 ss << " \"type\": \"vector\",\n";
206 ss << " \"dimensionality\": {\n";
207 ss << " \"header\": [null],\n";
208 ss << " \"dimensions\": [1]\n},\n";
209 ss << " \"values\":" << this->slope << "}]\n";
210
211 ss << "}";
212
213 return ss.str();
214 }
215
216#ifdef TMB_MODEL
217
218 template <typename Type>
219 bool add_to_fims_tmb_internal() {
220 std::shared_ptr<fims_info::Information<Type>> info =
222
223 std::shared_ptr<fims_popdy::LogisticMaturity<Type>> maturity =
224 std::make_shared<fims_popdy::LogisticMaturity<Type>>();
225
226 // set relative info
227 maturity->id = this->id;
228 std::stringstream ss;
229 maturity->inflection_point.resize(this->inflection_point.size());
230 for (size_t i = 0; i < this->inflection_point.size(); i++) {
231 maturity->inflection_point[i] = this->inflection_point[i].initial_value_m;
232 if (this->inflection_point[i].estimation_type_m.get() ==
233 "fixed_effects") {
234 ss.str("");
235 ss << "Maturity." << this->id << ".inflection_point."
236 << this->inflection_point[i].id_m;
237 info->RegisterParameterName(ss.str());
238 info->RegisterParameter(maturity->inflection_point[i]);
239 }
240 if (this->inflection_point[i].estimation_type_m.get() ==
241 "random_effects") {
242 ss.str("");
243 ss << "Maturity." << this->id << ".inflection_point."
244 << this->inflection_point[i].id_m;
245 info->RegisterRandomEffectName(ss.str());
246 info->RegisterRandomEffect(maturity->inflection_point[i]);
247 }
248 }
249 info->variable_map[this->inflection_point.id_m] =
250 &(maturity)->inflection_point;
251
252 maturity->slope.resize(this->slope.size());
253 for (size_t i = 0; i < this->slope.size(); i++) {
254 maturity->slope[i] = this->slope[i].initial_value_m;
255 if (this->slope[i].estimation_type_m.get() == "fixed_effects") {
256 ss.str("");
257 ss << "Maturity." << this->id << ".slope." << this->slope[i].id_m;
258 info->RegisterParameterName(ss.str());
259 info->RegisterParameter(maturity->slope[i]);
260 }
261 if (this->slope[i].estimation_type_m.get() == "random_effects") {
262 ss.str("");
263 ss << "Maturity." << this->id << ".slope." << this->slope[i].id_m;
264 info->RegisterRandomEffect(maturity->slope[i]);
265 info->RegisterRandomEffectName(ss.str());
266 }
267 }
268 info->variable_map[this->slope.id_m] = &(maturity)->slope;
269
270 // add to Information
271 info->maturity_models[maturity->id] = maturity;
272
273 return true;
274 }
275
280 virtual bool add_to_fims_tmb() {
281 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
282 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
283
284 return true;
285 }
286
287#endif
288};
289
290#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 ~LogisticMaturityInterface()
The destructor.
Definition rcpp_maturity.hpp:109
VariableVector slope
How quickly the proportion mature changes with age around the inflection point, per year of age....
Definition rcpp_maturity.hpp:84
VariableVector inflection_point
The age at which 50% of fish are mature for models that start at age zero and each age bin represents...
Definition rcpp_maturity.hpp:80
LogisticMaturityInterface()
The constructor.
Definition rcpp_maturity.hpp:89
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_maturity.hpp:115
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_maturity.hpp:135
LogisticMaturityInterface(const LogisticMaturityInterface &other)
Construct a new Logistic Maturity Interface object.
Definition rcpp_maturity.hpp:101
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_maturity.hpp:186
virtual double evaluate(double x)
Evaluate maturity using the logistic function.
Definition rcpp_maturity.hpp:122
virtual double evaluate(double x)=0
A method for each child maturity interface object to inherit so each maturity option can have an eval...
virtual ~MaturityInterfaceBase()
The destructor.
Definition rcpp_maturity.hpp:57
virtual uint32_t get_id()=0
Get the ID for the child maturity interface objects to inherit.
static std::map< uint32_t, std::shared_ptr< MaturityInterfaceBase > > live_objects
The map associating the IDs of MaturityInterfaceBase to the objects. This is a live object,...
Definition rcpp_maturity.hpp:35
uint32_t id
The local id of the MaturityInterfaceBase object.
Definition rcpp_maturity.hpp:28
MaturityInterfaceBase(const MaturityInterfaceBase &other)
Construct a new Maturity Interface Base object.
Definition rcpp_maturity.hpp:52
MaturityInterfaceBase()
The constructor.
Definition rcpp_maturity.hpp:40
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
uint32_t id_m
The local ID of the Variable object.
Definition rcpp_interface_base.hpp:156
size_t size()
Returns the size of a VariableVector.
Definition rcpp_interface_base.hpp:287
std::map< uint32_t, std::shared_ptr< fims_popdy::MaturityBase< Type > > > maturity_models
Definition information.hpp:100
std::map< uint32_t, std::shared_ptr< fims_popdy::MaturityBase< Type > > >::iterator maturity_models_iterator
Definition information.hpp:104
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
Includes any .hpp files within the subfolders so that only this file needs to included in include sta...
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...
LogisticMaturity class that returns the logistic function value from fims_math.
Definition logistic.hpp:24
fims::Vector< Type > slope
How quickly the proportion mature changes with age around the inflection point, per year of age....
Definition logistic.hpp:35
fims::Vector< Type > inflection_point
The age at which 50% of fish are mature for models that start at age zero and each age bin represents...
Definition logistic.hpp:29
virtual const Type evaluate(const Type &x)
Method of the logistic maturity class that implements the logistic function from FIMS math.
Definition logistic.hpp:49