Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/konrad1991/rfortranrcpp
Communication between Fortran, Rcpp and R. Passing R or Fortran user code to Fortran code from a package.
https://github.com/konrad1991/rfortranrcpp
fortran rcpp rlang
Last synced: about 2 months ago
JSON representation
Communication between Fortran, Rcpp and R. Passing R or Fortran user code to Fortran code from a package.
- Host: GitHub
- URL: https://github.com/konrad1991/rfortranrcpp
- Owner: Konrad1991
- Created: 2021-09-16T17:08:07.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-21T11:51:27.000Z (over 3 years ago)
- Last Synced: 2024-01-29T08:47:00.575Z (about 1 year ago)
- Topics: fortran, rcpp, rlang
- Language: C++
- Homepage:
- Size: 3.3 MB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# RFortranRcpp
Testing communication between fortran, Rcpp and RShowing the possibility to pass Rcpp fct to fortran and vice versa.
It is also possible to pass a fortran fct (e.g.from user) to Rcpp which afterwards can be passed to fortran.
This is done using an external pointer and the C interface of fortran.
It is also possible to pass a R function to fortran.
# How to pass a Fortran function to Fortran* Define a function which should do something
```Fortran
SUBROUTINE TEST(X, Y)
use ISO_C_BINDING, only : C_DOUBLE, C_PTR
IMPLICIT NONE
REAL(C_DOUBLE), intent(out) :: X, Y
X = X + 10
Y = Y*2
print *, x
print *, yEND SUBROUTINE
```* Define fortran code which calls the previously defined function
```Fortran
SUBROUTINE call_fortran2()use ISO_C_BINDING, only : C_CHAR, C_INT, C_FLOAT, C_DOUBLE, C_PTR
IMPLICIT NONE
REAL(C_DOUBLE) :: X, Y
INTERFACE
SUBROUTINE fortranfct(a, b) BIND(C, name ='fct')
import C_DOUBLE
REAL(C_DOUBLE) :: a, b
END SUBROUTINE fortranfct
END INTERFACE
X = 10.
Y = 12.
print*, X
print*, Y
call fortranfct(X, Y)END SUBROUTINE
```* Define extern C wrapper for both Fortran functions
* call the outer Fortran function via Rcpp```C++
#includetypedef void (*fp)(double*a, double* b);
// c interface to fortran fct 'test'
extern "C" {
void test_(double* x, double* y);
}// fortran function which calls fortran
extern "C" {
void call_fortran2_();
}// [[Rcpp::export]]
void call_fortran_which_call_fortran_via_c() {
call_fortran2_();
}
``````r
library(fortranfctpassing)
call_fortran_which_call_fortran_via_c()
```# Call R from Fortran
```C++
#include// [[Rcpp::export]]
void Rfct2() {
// Has to be in function if the function is not defined in a package
Rcpp::Environment global = Rcpp::Environment::global_env();
Rcpp::Function hello = global.get("hello");
hello();
}// this is called by fortran later
extern "C" {
void Rfctinput_() {
Rfct2();
}
}// call fortran which calls R
extern "C" {
void callr_();
}// [[Rcpp::export]]
void call_fortran_which_callsR() {
callr_();
}
``````Fortran
SUBROUTINE callr()
use ISO_C_BINDING, only : C_DOUBLE, C_PTR
IMPLICIT NONEINTERFACE
SUBROUTINE Rfct() BIND(C, name ='Rfctinput_')
END SUBROUTINE Rfct
END INTERFACE
call Rfct()END SUBROUTINE
``````r
hello <- function() {
print("Hello World")
}library(fortranfctpassing)
call_fortran_which_callsR()
```