Guest User

Untitled

a guest
Nov 21st, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. int <FUNCTION_NAME> (realtype t, N_Vector y, N_Vector ydot, void *user_data)
  2.  
  3. time_t <- c(0.0, 0.4, seq(from = 10.4, len = 12, by = 10)) # time vector
  4. cvode(time_t, c(1,0,0), my_fun, 1e-04, c(1e-08, 1e-08, 1e-08))
  5.  
  6. if (fstr == "<USER_DEFINED_FUNCTION_NAME>")
  7. XPtr<funcPtr> fun(new funcPtr(&<USER_DEFINED_FUNCTION_NAME>), true);
  8. else
  9. Rcpp::Rout << "Supply the correct function name" << std::endl;
  10.  
  11. #include <Rcpp.h>
  12. using namespace Rcpp;
  13.  
  14. #include <cvode/cvode.h> /* prototypes for CVODE fcts., consts. */
  15. #include <nvector/nvector_serial.h> /* serial N_Vector types, fcts., macros */
  16. #include <cvode/cvode_dense.h> /* prototype for CVDense */
  17. #include <sundials/sundials_dense.h> /* definitions DlsMat DENSE_ELEM */
  18. #include <sundials/sundials_types.h> /* definition of type realtype */
  19.  
  20. int test (realtype t, N_Vector y, N_Vector ydot, void *user_data){
  21.  
  22. // static keyword before int is not really required here
  23.  
  24. NV_Ith_S(ydot,0) = -0.04 * NV_Ith_S(y,0) + 1e04 * NV_Ith_S(y,1) * NV_Ith_S(y,2);
  25. NV_Ith_S(ydot,2) = 3e07 * NV_Ith_S(y,1) * NV_Ith_S(y,1);
  26. NV_Ith_S(ydot,1) = -NV_Ith_S(ydot,0) - NV_Ith_S(ydot,2);
  27.  
  28. return(0);
  29.  
  30. }
  31.  
  32. // declare funcPtr as a type for function pointer to a function with the
  33. // signature same as function which describes the RHS for ODEs
  34. // see reference here - http://gallery.rcpp.org/articles/passing-cpp-function-pointers/
  35. typedef int (*funcPtr)(realtype t, N_Vector y, N_Vector ydot, void *user_data);
  36.  
  37. // [[Rcpp::export]]
  38. XPtr<funcPtr> putFunPtrInXPtr() {
  39.  
  40. // return(XPtr<funcPtr> (new funcPtr(&test)));
  41. XPtr<funcPtr> testptr(new funcPtr(&test), true);
  42. return testptr;
  43.  
  44. }
Add Comment
Please, Sign In to add comment