FIMS  v0.9.3
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 "../../common/model_object.hpp"
12#include "../../utilities/fims_json.hpp"
24
29 std::signal(SIGSEGV, &fims::WriteAtExit);
30 std::signal(SIGINT, &fims::WriteAtExit);
31 std::signal(SIGABRT, &fims::WriteAtExit);
32 std::signal(SIGFPE, &fims::WriteAtExit);
33 std::signal(SIGILL, &fims::WriteAtExit);
34 std::signal(SIGTERM, &fims::WriteAtExit);
35}
36
77
78 // clear first
79 // base model
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 for (size_t i = 0; i < FIMSRcppInterfaceBase::fims_interface_objects.size();
89 i++) {
91 }
92
93 // base model
94 info0->CreateModel();
95 info0->CheckModel();
96
97 info->CreateModel();
98
99 // instantiate the model? TODO: Ask Matthew what this does
100 std::shared_ptr<fims_model::Model<TMB_FIMS_REAL_TYPE>> m0 =
102
103 return true;
104}
105
106/* Dictionary block for shared documentation.
107 [details_set_x_parameters]
108 Updates the internal parameter values for the model base of type
109 TMB_FIMS_REAL_TYPE. It is typically called before finalize() or
110 @ref CatchAtAgeInterface::to_json "`get_output()`" to ensure the correct
111 values are used because TMB doesn't always keep the updated parameters in
112 the "double" version of the tape. So we need to update those first.
113 \n\n
114 Usage example in R:
115 \code{.R}
116 set_fixed_parameters(c(1, 2, 3))
117 set_random_parameters(c(1, 2, 3))
118 catch_at_age$get_output()
119 \endcode
120 [details_set_x_parameters]
121*/
122/* Dictionary block for shared documentation.
123 [param_par]
124 @param par A vector of parameter values.
125 [param_par]
126 */
127
134void set_fixed_parameters(Rcpp::NumericVector par) {
135 // base model
136 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> info0 =
138
139 for (size_t i = 0; i < info0->fixed_effects_parameters.size(); i++) {
140 *info0->fixed_effects_parameters[i] = par[i];
141 }
142}
143
149Rcpp::NumericVector get_fixed_parameters_vector() {
150 // base model
151 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> info0 =
153
154 Rcpp::NumericVector p;
155
156 for (size_t i = 0; i < info0->fixed_effects_parameters.size(); i++) {
157 p.push_back(*info0->fixed_effects_parameters[i]);
158 }
159
160 return p;
161}
162
169void set_random_parameters(Rcpp::NumericVector par) {
170 // base model
171 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> info0 =
173
174 for (size_t i = 0; i < info0->random_effects_parameters.size(); i++) {
175 *info0->random_effects_parameters[i] = par[i];
176 }
177}
178
184Rcpp::NumericVector get_random_parameters_vector() {
185 // base model
186 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> d0 =
188
189 Rcpp::NumericVector p;
190
191 for (size_t i = 0; i < d0->random_effects_parameters.size(); i++) {
192 p.push_back(*d0->random_effects_parameters[i]);
193 }
194
195 return p;
196}
197
204Rcpp::List get_parameter_names(Rcpp::List pars) {
205 // base model
206 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> d0 =
208
209 pars.attr("names") = d0->parameter_names;
210
211 return pars;
212}
213
220Rcpp::List get_random_names(Rcpp::List pars) {
221 // base model
222 std::shared_ptr<fims_info::Information<TMB_FIMS_REAL_TYPE>> d0 =
224
225 pars.attr("names") = d0->random_effects_names;
226
227 return pars;
228}
229
235template <typename Type>
237 std::shared_ptr<fims_info::Information<Type>> d0 =
239 d0->Clear();
240}
241
248void clear_impl(bool get_error_msg) {
249 // rcpp_interface_base.hpp
251
252 // Variable and VariableVector
253 Variable::id_g = 1;
255 // rcpp_data.hpp
258
261
264
267
270
271 // rcpp_fleets.hpp
274
277
278 // rcpp_growth.hpp
281
284
285 // rcpp_maturity.hpp
288
291
292 // rcpp_population.hpp
295
298
299 // rcpp_recruitment.hpp
302
305
306 // rcpp_selectivity.hpp
309
312
315
316 // rcpp_distribution.hpp
319
322
325
328
331
334
336
337 std::unique_ptr<fims_popdy::LogisticSelectivity<double>> test_obj;
338 if (get_error_msg) {
339 test_obj = std::make_unique<fims_popdy::LogisticSelectivity<double>>();
340 }
341
342 // --- AUTOMATED DANGLING POINTER DIAGNOSTIC PRINT ---
344 std::ostringstream msg;
345 msg << "\n⚠️ WARNING: FIMS Dangling Pointer or Module Detected after "
346 "clear()!\n";
347 msg << "--------------------------------------------------\n";
348 msg << "A total of "
350 << " pointer(s) NOT cleared\n";
351 msg << "--------------------------------------------------\n";
352 msg << "Ensure all pointers are being reset.\n\n";
353
354 Rcpp::warning(msg.str());
355 }
356}
357
361void clear() { clear_impl(false); }
362
369
373std::string get_log() { return fims::FIMSLog::fims_log->get_log(); }
374
378std::string get_log_errors() { return fims::FIMSLog::fims_log->get_errors(); }
379
383std::string get_log_warnings() {
384 return fims::FIMSLog::fims_log->get_warnings();
385}
386
390std::string get_log_info() { return fims::FIMSLog::fims_log->get_info(); }
391
395void write_log(bool write) { fims::FIMSLog::fims_log->write_on_exit = write; }
396
400void set_log_path(const std::string &path) {
401 fims::FIMSLog::fims_log->set_path(path);
402}
403
407void set_log_throw_on_error(bool throw_on_error) {
408 fims::FIMSLog::fims_log->throw_on_error = throw_on_error;
409}
410
414void log_info(std::string log_entry) {
415 fims::FIMSLog::fims_log->info_message(log_entry, -1, "R_env",
416 "R_script_entry");
417}
418
422void log_warning(std::string log_entry) {
423 fims::FIMSLog::fims_log->warning_message(log_entry, -1, "R_env",
424 "R_script_entry");
425}
426
433std::string escapeQuotes(const std::string &input) {
434 std::string result = input;
435 std::string search = "\"";
436 std::string replace = "\\\"";
437
438 // Find each occurrence of `"` and replace it with `\"`
439 size_t pos = result.find(search);
440 while (pos != std::string::npos) {
441 result.replace(pos, search.size(), replace);
442 pos = result.find(search,
443 pos + replace.size()); // Move past the replaced position
444 }
445 return result;
446}
447
451void log_error(std::string log_entry) {
452 std::stringstream ss;
453 ss << "capture.output(traceback(4))";
456
457 PROTECT(expression = R_ParseVector(Rf_mkString(ss.str().c_str()), 1, &status,
458 R_NilValue));
459 if (status != PARSE_OK) {
460 Rcpp::Rcout << "Error parsing expression" << std::endl;
461 UNPROTECT(1);
462 }
463 Rcpp::Rcout << "before call.";
465 Rcpp::Rcout << "after call.";
466 UNPROTECT(2);
467 std::stringstream ss_ret;
468 ss_ret << "traceback: ";
469 for (int j = 0; j < LENGTH(result); j++) {
470 std::string str(CHAR(STRING_ELT(result, j)));
471 ss_ret << escapeQuotes(str) << "\\n";
472 }
473
474 std::string ret =
475 ss_ret.str(); //"find error";//Rcpp::as<std::string>(result);
476
477 fims::FIMSLog::fims_log->error_message(log_entry, -1, "R_env", ret.c_str());
478}
479#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:24
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:67
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:638
static uint32_t id_g
The static id of the FleetInterfaceBase object.
Definition rcpp_models.hpp:51
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:62
static uint32_t id_g
The static id of the FleetInterfaceBase object.
Definition rcpp_fleet.hpp:24
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:34
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 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 uint32_t id_g
The static ID of the Variable object.
Definition rcpp_interface_base.hpp:149
static uint32_t id_g
The static ID of the Variable object.
Definition rcpp_interface_base.hpp:36
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:270
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:263
static std::shared_ptr< Model< Type > > GetInstance()
Evaluate. Calculates the joint negative log-likelihood function.
Definition model.hpp:54
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:400
std::string get_log_warnings()
Gets the warning entries from the log as a string in JSON format.
Definition rcpp_interface.hpp:383
std::string escapeQuotes(const std::string &input)
Escapes quotations.
Definition rcpp_interface.hpp:433
bool CreateTMBModel()
Initialize and construct the FIMS model using TMB.
Definition rcpp_interface.hpp:75
std::string get_log_info()
Gets the info entries from the log as a string in JSON format.
Definition rcpp_interface.hpp:390
void clear_impl(bool get_error_msg)
Clears all FIMS pointers and resets model state.
Definition rcpp_interface.hpp:248
void log_error(std::string log_entry)
Adds a error entry to the log from the R environment.
Definition rcpp_interface.hpp:451
void set_random_parameters(Rcpp::NumericVector par)
Update random effect parameters in the tape, so the output is correct.
Definition rcpp_interface.hpp:169
std::string get_log_errors()
Gets the error entries from the log as a string in JSON format.
Definition rcpp_interface.hpp:378
void log_warning(std::string log_entry)
Adds a warning entry to the log from the R environment.
Definition rcpp_interface.hpp:422
Rcpp::List get_parameter_names(Rcpp::List pars)
Gets the parameter names object.
Definition rcpp_interface.hpp:204
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:407
std::string get_log()
Gets the log entries as a string in JSON format.
Definition rcpp_interface.hpp:373
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:236
void log_info(std::string log_entry)
Adds an info entry to the log from the R environment.
Definition rcpp_interface.hpp:414
void clear()
Clears all FIMS pointers and resets model state.
Definition rcpp_interface.hpp:361
Rcpp::NumericVector get_fixed_parameters_vector()
Gets the fixed parameters vector object.
Definition rcpp_interface.hpp:149
Rcpp::NumericVector get_random_parameters_vector()
Gets the random parameters vector object.
Definition rcpp_interface.hpp:184
void write_log(bool write)
If true, writes the log on exit.
Definition rcpp_interface.hpp:395
void test_clear_with_leak_check()
Test-only variant of clear() that retains a lingering pointer to validate dangling pointer diagnostic...
Definition rcpp_interface.hpp:368
void init_logging()
Definition rcpp_interface.hpp:28
void set_fixed_parameters(Rcpp::NumericVector par)
Update fixed parameters in the tape, so the output is correct.
Definition rcpp_interface.hpp:134
Rcpp::List get_random_names(Rcpp::List pars)
Gets the random effects names object.
Definition rcpp_interface.hpp:220
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....
static int total_active_objects
Total number of active FIMSObject instances currently in memory.
Definition model_object.hpp:24