FIMS  v0.8.0
Loading...
Searching...
No Matches
rcpp_interface.hpp
Go to the documentation of this file.
1
8#ifndef FIMS_INTERFACE_RCPP_INTERFACE_HPP
9#define FIMS_INTERFACE_RCPP_INTERFACE_HPP
10#include "../../common/model.hpp"
11#include "../../utilities/fims_json.hpp"
23
28 std::signal(SIGSEGV, &fims::WriteAtExit);
29 std::signal(SIGINT, &fims::WriteAtExit);
30 std::signal(SIGABRT, &fims::WriteAtExit);
31 std::signal(SIGFPE, &fims::WriteAtExit);
32 std::signal(SIGILL, &fims::WriteAtExit);
33 std::signal(SIGTERM, &fims::WriteAtExit);
34}
35
76
77 // clear first
78 // base model
79#ifdef TMBAD_FRAMEWORK
80 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> info0 =
82 info0->Clear();
83
84 std::shared_ptr<fims_info::Information<TMBAD_FIMS_TYPE>> info =
86 info->Clear();
87
88#else
89 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> info0 =
91 info0->Clear()
92
93 // first-order derivative
94 std::shared_ptr<fims_info::Information<TMB_FIMS_FIRST_ORDER>>
96 info1->Clear();
97
98 // second-order derivative
99 std::shared_ptr<fims_info::Information<TMB_FIMS_SECOND_ORDER>> info2 =
101 info2->Clear();
102
103 // third-order derivative
104 std::shared_ptr<fims_info::Information<TMB_FIMS_THIRD_ORDER>> info3 =
106 info3->Clear();
107#endif
108
110 "Adding FIMS objects to TMB, " +
111 fims::to_string(FIMSRcppInterfaceBase::fims_interface_objects.size()) +
112 " objects");
113 for (size_t i = 0; i < FIMSRcppInterfaceBase::fims_interface_objects.size();
114 i++) {
116 }
117
118 // base model
119#ifdef TMBAD_FRAMEWORK
120
121 info0->CreateModel();
122 info0->CheckModel();
123
124 info->CreateModel();
125
126#else
127
128 info0->CreateModel();
129 info0->CheckModel();
130
131 info1->CreateModel();
132
133 // second-order derivative
134
135 info2->CreateModel();
136
137 // third-order derivative
138
139 info3->CreateModel();
140#endif
141
142 // instantiate the model? TODO: Ask Matthew what this does
143 std::shared_ptr<fims_model::Model<TMB_FIMS_REAL_TYPE>> m0 =
145
146 return true;
147}
148
149/* Dictionary block for shared documentation.
150 [details_set_x_parameters]
151 Updates the internal parameter values for the model base of type
152 TMB_FIMS_REAL_TYPE. It is typically called before finalize() or
153 @ref CatchAtAgeInterface::to_json "`get_output()`" to ensure the correct
154 values are used because TMB doesn't always keep the updated parameters in
155 the "double" version of the tape. So we need to update those first.
156 \n\n
157 Usage example in R:
158 \code{.R}
159 set_fixed_parameters(c(1, 2, 3))
160 set_random_parameters(c(1, 2, 3))
161 catch_at_age$get_output(do_sd_report = FALSE)
162 \endcode
163 [details_set_x_parameters]
164*/
165/* Dictionary block for shared documentation.
166 [param_par]
167 @param par A vector of parameter values.
168 [param_par]
169 */
170
177void set_fixed_parameters(Rcpp::NumericVector par) {
178 // base model
179 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> info0 =
181
182 for (size_t i = 0; i < info0->fixed_effects_parameters.size(); i++) {
183 *info0->fixed_effects_parameters[i] = par[i];
184 }
185}
186
192Rcpp::NumericVector get_fixed_parameters_vector() {
193 // base model
194 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> info0 =
196
197 Rcpp::NumericVector p;
198
199 for (size_t i = 0; i < info0->fixed_effects_parameters.size(); i++) {
200 p.push_back(*info0->fixed_effects_parameters[i]);
201 }
202
203 return p;
204}
205
212void set_random_parameters(Rcpp::NumericVector par) {
213 // base model
214 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> info0 =
216
217 for (size_t i = 0; i < info0->random_effects_parameters.size(); i++) {
218 *info0->random_effects_parameters[i] = par[i];
219 }
220}
221
227Rcpp::NumericVector get_random_parameters_vector() {
228 // base model
229 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> d0 =
231
232 Rcpp::NumericVector p;
233
234 for (size_t i = 0; i < d0->random_effects_parameters.size(); i++) {
235 p.push_back(*d0->random_effects_parameters[i]);
236 }
237
238 return p;
239}
240
247Rcpp::List get_parameter_names(Rcpp::List pars) {
248 // base model
249 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> d0 =
251
252 pars.attr("names") = d0->parameter_names;
253
254 return pars;
255}
256
263Rcpp::List get_random_names(Rcpp::List pars) {
264 // base model
265 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> d0 =
267
268 pars.attr("names") = d0->random_effects_names;
269
270 return pars;
271}
272
278template <typename Type>
280 std::shared_ptr<fims_info::Information<Type>> d0 =
282 d0->Clear();
283}
284
288void clear() {
289 FIMS_INFO_LOG("Clearing FIMS objects from interface stack");
290 // rcpp_interface_base.hpp
292
293 // Parameter and ParameterVector
294 Parameter::id_g = 1;
296 // rcpp_data.hpp
299
302
305
308
311
312 // rcpp_fleets.hpp
315
318
319 // rcpp_growth.hpp
322
325
326 // rcpp_maturity.hpp
329
332
333 // rcpp_population.hpp
336
339
340 // rcpp_recruitment.hpp
343
346
347 // rcpp_selectivity.hpp
350
353
356
357 // rcpp_distribution.hpp
360
363
366
369
372
373#ifdef TMBAD_FRAMEWORK
376#else
381#endif
382
384}
385
389std::string get_log() { return fims::FIMSLog::fims_log->get_log(); }
390
394std::string get_log_errors() { return fims::FIMSLog::fims_log->get_errors(); }
395
399std::string get_log_warnings() {
400 return fims::FIMSLog::fims_log->get_warnings();
401}
402
406std::string get_log_info() { return fims::FIMSLog::fims_log->get_info(); }
407
411std::string get_log_module(const std::string &module) {
412 return fims::FIMSLog::fims_log->get_module(module);
413}
414
418void write_log(bool write) {
419 FIMS_INFO_LOG("Setting FIMS write log: " + fims::to_string(write));
420 fims::FIMSLog::fims_log->write_on_exit = write;
421}
422
426void set_log_path(const std::string &path) {
427 FIMS_INFO_LOG("Setting FIMS log path: " + path);
428 fims::FIMSLog::fims_log->set_path(path);
429}
430
434void set_log_throw_on_error(bool throw_on_error) {
435 fims::FIMSLog::fims_log->throw_on_error = throw_on_error;
436}
437
441void log_info(std::string log_entry) {
442 fims::FIMSLog::fims_log->info_message(log_entry, -1, "R_env",
443 "R_script_entry");
444}
445
449void log_warning(std::string log_entry) {
450 fims::FIMSLog::fims_log->warning_message(log_entry, -1, "R_env",
451 "R_script_entry");
452}
453
460std::string escapeQuotes(const std::string &input) {
461 std::string result = input;
462 std::string search = "\"";
463 std::string replace = "\\\"";
464
465 // Find each occurrence of `"` and replace it with `\"`
466 size_t pos = result.find(search);
467 while (pos != std::string::npos) {
468 result.replace(pos, search.size(), replace);
469 pos = result.find(search,
470 pos + replace.size()); // Move past the replaced position
471 }
472 return result;
473}
474
478void log_error(std::string log_entry) {
479 std::stringstream ss;
480 ss << "capture.output(traceback(4))";
483
484 PROTECT(expression = R_ParseVector(Rf_mkString(ss.str().c_str()), 1, &status,
485 R_NilValue));
486 if (status != PARSE_OK) {
487 Rcpp::Rcout << "Error parsing expression" << std::endl;
488 UNPROTECT(1);
489 }
490 Rcpp::Rcout << "before call.";
492 Rcpp::Rcout << "after call.";
493 UNPROTECT(2);
494 std::stringstream ss_ret;
495 ss_ret << "traceback: ";
496 for (int j = 0; j < LENGTH(result); j++) {
497 std::string str(CHAR(STRING_ELT(result, j)));
498 ss_ret << escapeQuotes(str) << "\\n";
499 }
500
501 std::string ret =
502 ss_ret.str(); //"find error";//Rcpp::as<std::string>(result);
503
504 fims::FIMSLog::fims_log->error_message(log_entry, -1, "R_env", ret.c_str());
505}
506#endif // FIMS_INTERFACE_RCPP_INTERFACE_HPP
static uint32_t id_g
The static id of the DataInterfaceBase object.
Definition rcpp_data.hpp:32
static std::map< uint32_t, std::shared_ptr< DataInterfaceBase > > live_objects
The map associating the IDs of DataInterfaceBase to the objects. This is a live object,...
Definition rcpp_data.hpp:43
static uint32_t id_g
The static ID of the DistributionsInterfaceBase object.
Definition rcpp_distribution.hpp:25
static std::map< uint32_t, std::shared_ptr< DistributionsInterfaceBase > > live_objects
The map associating the ID of the DistributionsInterfaceBase to the DistributionsInterfaceBase object...
Definition rcpp_distribution.hpp:68
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:644
static uint32_t id_g
The static id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:39
static std::map< uint32_t, std::shared_ptr< FisheryModelInterfaceBase > > live_objects
The map associating the IDs of FleetInterfaceBase to the objects. This is a live object,...
Definition rcpp_models.hpp:50
static uint32_t id_g
The static id of the FleetInterfaceBase object.
Definition rcpp_fleet.hpp:25
static std::map< uint32_t, std::shared_ptr< FleetInterfaceBase > > live_objects
The map associating the IDs of FleetInterfaceBase to the objects. This is a live object,...
Definition rcpp_fleet.hpp:35
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
static uint32_t id_g
The static id of the GrowthInterfaceBase object.
Definition rcpp_growth.hpp:24
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
static uint32_t id_g
The static id of the MaturityInterfaceBase object.
Definition rcpp_maturity.hpp:24
static uint32_t id_g
The static ID of the Parameter object.
Definition rcpp_interface_base.hpp:183
static uint32_t id_g
The static ID of the Parameter object.
Definition rcpp_interface_base.hpp:36
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
static uint32_t id_g
The static id of the RecruitmentInterfaceBase object.
Definition rcpp_recruitment.hpp:25
static std::map< uint32_t, std::shared_ptr< RecruitmentInterfaceBase > > live_objects
The map associating the IDs of RecruitmentInterfaceBase to the objects. This is a live object,...
Definition rcpp_recruitment.hpp:40
static uint32_t id_g
The static id of the SelectivityInterfaceBase.
Definition rcpp_selectivity.hpp:24
static std::map< uint32_t, std::shared_ptr< SelectivityInterfaceBase > > live_objects
The map associating the IDs of SelectivityInterfaceBase to the objects. This is a live object,...
Definition rcpp_selectivity.hpp:35
static std::shared_ptr< FIMSLog > fims_log
A singleton instance of the log, i.e., where there is only one log. The object is created when the ....
Definition def.hpp:224
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:238
static std::shared_ptr< Model< Type > > GetInstance()
Definition model.hpp:45
#define FIMS_INFO_LOG(MESSAGE)
Definition def.hpp:590
The Rcpp interface to declare different types of data, e.g., age-composition and index data....
The Rcpp interface to declare different distributions, e.g., normal and log normal....
The Rcpp interface to declare fleets. Allows for the use of methods::new() in R.
The Rcpp interface to declare different types of growth, e.g., empirical weight-at-age data....
void set_log_path(const std::string &path)
Sets the path for the log file to be written to.
Definition rcpp_interface.hpp:426
std::string get_log_warnings()
Gets the warning entries from the log as a string in JSON format.
Definition rcpp_interface.hpp:399
std::string escapeQuotes(const std::string &input)
Escapes quotations.
Definition rcpp_interface.hpp:460
std::string get_log_module(const std::string &module)
Gets log entries by module as a string in JSON format.
Definition rcpp_interface.hpp:411
bool CreateTMBModel()
Initialize and construct the FIMS model using TMB.
Definition rcpp_interface.hpp:74
std::string get_log_info()
Gets the info entries from the log as a string in JSON format.
Definition rcpp_interface.hpp:406
void log_error(std::string log_entry)
Adds a error entry to the log from the R environment.
Definition rcpp_interface.hpp:478
void set_random_parameters(Rcpp::NumericVector par)
Update random effect parameters in the tape, so the output is correct.
Definition rcpp_interface.hpp:212
std::string get_log_errors()
Gets the error entries from the log as a string in JSON format.
Definition rcpp_interface.hpp:394
void log_warning(std::string log_entry)
Adds a warning entry to the log from the R environment.
Definition rcpp_interface.hpp:449
Rcpp::List get_parameter_names(Rcpp::List pars)
Gets the parameter names object.
Definition rcpp_interface.hpp:247
void set_log_throw_on_error(bool throw_on_error)
If true, throws a runtime exception when an error is logged.
Definition rcpp_interface.hpp:434
std::string get_log()
Gets the log entries as a string in JSON format.
Definition rcpp_interface.hpp:389
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:279
void log_info(std::string log_entry)
Adds an info entry to the log from the R environment.
Definition rcpp_interface.hpp:441
void clear()
Clears the vector of independent variables.
Definition rcpp_interface.hpp:288
Rcpp::NumericVector get_fixed_parameters_vector()
Gets the fixed parameters vector object.
Definition rcpp_interface.hpp:192
Rcpp::NumericVector get_random_parameters_vector()
Gets the random parameters vector object.
Definition rcpp_interface.hpp:227
void write_log(bool write)
If true, writes the log on exit.
Definition rcpp_interface.hpp:418
void init_logging()
Definition rcpp_interface.hpp:27
void set_fixed_parameters(Rcpp::NumericVector par)
Update fixed parameters in the tape, so the output is correct.
Definition rcpp_interface.hpp:177
Rcpp::List get_random_names(Rcpp::List pars)
Gets the random effects names object.
Definition rcpp_interface.hpp:263
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...
The Rcpp interface to declare different maturity options, e.g., logistic. Allows for the use of metho...
The Rcpp interface to declare different types of models. Allows for the use of methods::new() in R.
The Rcpp interface to declare different types of natural mortality. Allows for the use of methods::ne...
The Rcpp interface to declare different types of populations. Allows for the use of methods::new() in...
The Rcpp interface to declare different types of recruitment, e.g., Beverton–Holt stock–recruitment r...
The Rcpp interface to declare different types of selectivity, e.g., logistic and double logistic....