FIMS  v0.8.1
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 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> info0 =
81 info0->Clear();
82
83 std::shared_ptr<fims_info::Information<TMBAD_FIMS_TYPE>> info =
85 info->Clear();
86
88 "Adding FIMS objects to TMB, " +
89 fims::to_string(FIMSRcppInterfaceBase::fims_interface_objects.size()) +
90 " objects");
91 for (size_t i = 0; i < FIMSRcppInterfaceBase::fims_interface_objects.size();
92 i++) {
94 }
95
96 // base model
97 info0->CreateModel();
98 info0->CheckModel();
99
100 info->CreateModel();
101
102 // instantiate the model? TODO: Ask Matthew what this does
103 std::shared_ptr<fims_model::Model<TMB_FIMS_REAL_TYPE>> m0 =
105
106 return true;
107}
108
109/* Dictionary block for shared documentation.
110 [details_set_x_parameters]
111 Updates the internal parameter values for the model base of type
112 TMB_FIMS_REAL_TYPE. It is typically called before finalize() or
113 @ref CatchAtAgeInterface::to_json "`get_output()`" to ensure the correct
114 values are used because TMB doesn't always keep the updated parameters in
115 the "double" version of the tape. So we need to update those first.
116 \n\n
117 Usage example in R:
118 \code{.R}
119 set_fixed_parameters(c(1, 2, 3))
120 set_random_parameters(c(1, 2, 3))
121 catch_at_age$get_output()
122 \endcode
123 [details_set_x_parameters]
124*/
125/* Dictionary block for shared documentation.
126 [param_par]
127 @param par A vector of parameter values.
128 [param_par]
129 */
130
137void set_fixed_parameters(Rcpp::NumericVector par) {
138 // base model
139 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> info0 =
141
142 for (size_t i = 0; i < info0->fixed_effects_parameters.size(); i++) {
143 *info0->fixed_effects_parameters[i] = par[i];
144 }
145}
146
152Rcpp::NumericVector get_fixed_parameters_vector() {
153 // base model
154 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> info0 =
156
157 Rcpp::NumericVector p;
158
159 for (size_t i = 0; i < info0->fixed_effects_parameters.size(); i++) {
160 p.push_back(*info0->fixed_effects_parameters[i]);
161 }
162
163 return p;
164}
165
172void set_random_parameters(Rcpp::NumericVector par) {
173 // base model
174 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> info0 =
176
177 for (size_t i = 0; i < info0->random_effects_parameters.size(); i++) {
178 *info0->random_effects_parameters[i] = par[i];
179 }
180}
181
187Rcpp::NumericVector get_random_parameters_vector() {
188 // base model
189 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> d0 =
191
192 Rcpp::NumericVector p;
193
194 for (size_t i = 0; i < d0->random_effects_parameters.size(); i++) {
195 p.push_back(*d0->random_effects_parameters[i]);
196 }
197
198 return p;
199}
200
207Rcpp::List get_parameter_names(Rcpp::List pars) {
208 // base model
209 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> d0 =
211
212 pars.attr("names") = d0->parameter_names;
213
214 return pars;
215}
216
223Rcpp::List get_random_names(Rcpp::List pars) {
224 // base model
225 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> d0 =
227
228 pars.attr("names") = d0->random_effects_names;
229
230 return pars;
231}
232
238template <typename Type>
240 std::shared_ptr<fims_info::Information<Type>> d0 =
242 d0->Clear();
243}
244
248void clear() {
249 FIMS_INFO_LOG("Clearing FIMS objects from interface stack");
250 // rcpp_interface_base.hpp
252
253 // Parameter and ParameterVector
254 Parameter::id_g = 1;
256 // rcpp_data.hpp
259
262
265
268
271
272 // rcpp_fleets.hpp
275
278
279 // rcpp_growth.hpp
282
285
286 // rcpp_maturity.hpp
289
292
293 // rcpp_population.hpp
296
299
300 // rcpp_recruitment.hpp
303
306
307 // rcpp_selectivity.hpp
310
313
316
317 // rcpp_distribution.hpp
320
323
326
329
332
335
337}
338
342std::string get_log() { return fims::FIMSLog::fims_log->get_log(); }
343
347std::string get_log_errors() { return fims::FIMSLog::fims_log->get_errors(); }
348
352std::string get_log_warnings() {
353 return fims::FIMSLog::fims_log->get_warnings();
354}
355
359std::string get_log_info() { return fims::FIMSLog::fims_log->get_info(); }
360
364void write_log(bool write) {
365 FIMS_INFO_LOG("Setting FIMS write log: " + fims::to_string(write));
366 fims::FIMSLog::fims_log->write_on_exit = write;
367}
368
372void set_log_path(const std::string &path) {
373 FIMS_INFO_LOG("Setting FIMS log path: " + path);
374 fims::FIMSLog::fims_log->set_path(path);
375}
376
380void set_log_throw_on_error(bool throw_on_error) {
381 fims::FIMSLog::fims_log->throw_on_error = throw_on_error;
382}
383
387void log_info(std::string log_entry) {
388 fims::FIMSLog::fims_log->info_message(log_entry, -1, "R_env",
389 "R_script_entry");
390}
391
395void log_warning(std::string log_entry) {
396 fims::FIMSLog::fims_log->warning_message(log_entry, -1, "R_env",
397 "R_script_entry");
398}
399
406std::string escapeQuotes(const std::string &input) {
407 std::string result = input;
408 std::string search = "\"";
409 std::string replace = "\\\"";
410
411 // Find each occurrence of `"` and replace it with `\"`
412 size_t pos = result.find(search);
413 while (pos != std::string::npos) {
414 result.replace(pos, search.size(), replace);
415 pos = result.find(search,
416 pos + replace.size()); // Move past the replaced position
417 }
418 return result;
419}
420
424void log_error(std::string log_entry) {
425 std::stringstream ss;
426 ss << "capture.output(traceback(4))";
429
430 PROTECT(expression = R_ParseVector(Rf_mkString(ss.str().c_str()), 1, &status,
431 R_NilValue));
432 if (status != PARSE_OK) {
433 Rcpp::Rcout << "Error parsing expression" << std::endl;
434 UNPROTECT(1);
435 }
436 Rcpp::Rcout << "before call.";
438 Rcpp::Rcout << "after call.";
439 UNPROTECT(2);
440 std::stringstream ss_ret;
441 ss_ret << "traceback: ";
442 for (int j = 0; j < LENGTH(result); j++) {
443 std::string str(CHAR(STRING_ELT(result, j)));
444 ss_ret << escapeQuotes(str) << "\\n";
445 }
446
447 std::string ret =
448 ss_ret.str(); //"find error";//Rcpp::as<std::string>(result);
449
450 fims::FIMSLog::fims_log->error_message(log_entry, -1, "R_env", ret.c_str());
451}
452#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:584
static uint32_t id_g
The static id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:38
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:49
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:147
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:215
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:537
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:372
std::string get_log_warnings()
Gets the warning entries from the log as a string in JSON format.
Definition rcpp_interface.hpp:352
std::string escapeQuotes(const std::string &input)
Escapes quotations.
Definition rcpp_interface.hpp:406
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:359
void log_error(std::string log_entry)
Adds a error entry to the log from the R environment.
Definition rcpp_interface.hpp:424
void set_random_parameters(Rcpp::NumericVector par)
Update random effect parameters in the tape, so the output is correct.
Definition rcpp_interface.hpp:172
std::string get_log_errors()
Gets the error entries from the log as a string in JSON format.
Definition rcpp_interface.hpp:347
void log_warning(std::string log_entry)
Adds a warning entry to the log from the R environment.
Definition rcpp_interface.hpp:395
Rcpp::List get_parameter_names(Rcpp::List pars)
Gets the parameter names object.
Definition rcpp_interface.hpp:207
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:380
std::string get_log()
Gets the log entries as a string in JSON format.
Definition rcpp_interface.hpp:342
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:239
void log_info(std::string log_entry)
Adds an info entry to the log from the R environment.
Definition rcpp_interface.hpp:387
void clear()
Clears the vector of independent variables.
Definition rcpp_interface.hpp:248
Rcpp::NumericVector get_fixed_parameters_vector()
Gets the fixed parameters vector object.
Definition rcpp_interface.hpp:152
Rcpp::NumericVector get_random_parameters_vector()
Gets the random parameters vector object.
Definition rcpp_interface.hpp:187
void write_log(bool write)
If true, writes the log on exit.
Definition rcpp_interface.hpp:364
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:137
Rcpp::List get_random_names(Rcpp::List pars)
Gets the random effects names object.
Definition rcpp_interface.hpp:223
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....