包裹fortran程序在R中使用

时间:2014-04-20 15:39:11

标签: r interface fortran

我正在与R合作,但需要进行大量的数字运算,我想在fortran中进行。我是R的新手,也是fortran的新手......我已经有了一个有效的R程序,我想优化它。我创建了一个Fortran程序来解决ODE系统,我将其保存在子程序中。我还使用一个名为aux.f90的模块来存储参数和一个函数,该函数创建一个馈入方程的信号。这按预期工作,数据保存在.txt文件中。

我现在要做的是创建一个R前端,向Fortran程序抛出参数,例如模拟的长度或解决方案中使用的步骤数。然后Fortran完成繁重的工作,将结果保存在文件中,我可以使用R来显示文件中的数据。请参阅下面的Fortran代码:

! The auxiliary module contains all parameters
module aux
implicit none

integer,parameter :: n = 2**8       ! number of steps 
real(kind=4) :: jout = 0.5          ! for normal metabolism
real(kind=4) :: alp = 4.0           ! factor for growth
real(kind=4) :: bet = 1.0           ! benefit value
real(kind=4) :: etay = 0.1          ! cost value y
real(kind=4) :: etaz = 0.10         ! cost value z
real(kind=4) :: h  = 3.0            ! Hill coefficient
real(kind=4) :: Kx = 0.07           ! saturation coefficient
real(kind=4) :: t1 = 0.0, t2 = 30.0 ! start and end point of the simulation

contains                            ! function f(t) to create a step signal

real(kind=4) function f(t)
    implicit none
    real(kind=4), intent(in)  :: t  ! taking time value
    real(kind=4)              :: tt 
    !real(kind=4), intent(out) :: s  ! giving out the signal 
    real(kind=4) :: period = 5      ! defining the period length

    tt = MODULO(t,period)

        ! Signal function
        if (tt > 0.5*period) then 
            f = 1
        else
            f = 0
        endif
end function

end module aux

! The program solving the ODE system and giving the output as a fileprogram ffl
program ffl
use aux         ! Use module aux
implicit none
integer                     :: m,j          ! iteration variable
real(kind=4), dimension(6)  :: b =(/0.0, 0.2, 0.4, 0.6, 0.8, 1.0/) ! expression
real(kind=4)                :: dt                                  ! time resolution
real(kind=4), dimension(n)  :: t                                   ! time vector
real(kind=4), dimension(4)  :: x_new, x_aux, y_new, y_aux, q0      ! vectors 

! computing the time vector 
dt=(t2-t1)/real(n) ! calculating time resolution
t(1) = t1          ! setting first time value to t1 = 0

do m = 1,n         ! filling the time vector
    t(m) = t(m-1)+dt
end do
open(unit = 10,file = 'ffl.txt', status = 'unknown')
do j=1,6
    k = b(j)
    ! initial conditions
    q0(1) = 0   ! x
    q0(2) = k   ! y
    q0(3) = 0   ! z
    q0(4) = 0   ! w

    !open(unit = 10,file = 'ffl.txt', status = 'unknown')
    x_new = q0                ! set initial conditions
    write(10,*)t(1),x_new(1),x_new(2),x_new(3),x_new(4) ! saving data

    do m = 2,n
        ! Solving with a second order method 
        call derivate(t(m-1),x_new,y_new) ! call the derivate routine
        x_aux = x_new + dt*y_new

        call derivate(t(m),x_aux,y_aux)
        x_new = x_new + 0.5*dt*(y_new+y_aux)
        write(10,*)t(m),x_new(1),x_new(2),x_new(3),x_new(4) ! saving data

    end do
 end do
 close(10)
end program ffl

! The subroutine derivate gives the system of ODE's to be solved

subroutine derivate(time,y,z)
use aux              ! Use module aux
implicit none
real(kind=4), intent(in)               :: time ! input: time vector  
real(kind=4), dimension(4), intent(in) :: y    ! input: initial conditions vector
real(kind=4), dimension(4), intent(out):: z    ! output: results vector

z(1) = f(time)-y(1)                                                             !dx/dt 
z(2) = k+(1-k)*((1+Kx)*(y(1)**h))/((y(1)**h)+Kx)-y(2)                           !dy/dt
z(3) = ((1+Kx)*(y(1)**h)/((y(1)**h)+Kx)*((1+Kx)*(y(2)**h))/((y(2)**h)+Kx)-y(3)) !dz/dt
z(4) = f(time)*y(3)-etay*(k+(1-k)*((1+Kx)*(y(1)**h))/((y(1)**h)+Kx)) &          !dw/dt
       -etaz*(((1+Kx)*(y(1)**h))/((y(1)**h)+Kx)*((1+Kx)*(y(2)**h))/((y(2)**h)+Kx)) 

结束子程序派生

我已经阅读了“编写R扩展”文档,但没有发现它非常有用......

现在问题:由于R需要Fortran子程序,我想在fortran中创建一个包装器子程序,利用我现有的文件,然后我可以从R调用。但是,我找不到方法首先创建这个包装器子程序。甚至可以在子程序中调用实际程序吗?我在网上找不到任何有用的东西。

2 个答案:

答案 0 :(得分:4)

程序应该作为可执行文件链接,因此你不能从子程序中调用它 - 或者你调用可执行文件(在gfortran中使用SYSTEM),但你可以这样做{{3} }。

从R调用Fortran的简单方法是directly from R R函数,它调用Fortran subroutine(不是function,也不是program

基本步骤是:

  • 编译一个Fortran DLL,导出你需要的子程序(当然它们可能是其他子程序或函数的包装器)
  • 将DLL放入系统路径中的目录
  • 来自R,使用.Fortran
  • 加载DLL
  • 使用.Fortran
  • 调用您的子程序

如果你使用gfortran,你可以安装Rtools,它拥有你需要的一切。如果你想使用另一个编译器,你可能会遇到一些麻烦,尤其是名字。

从您的评论到user2188538的回答,我看到您已经知道所有这些步骤,但要非常小心符号名称。从.Fortran帮助:使用.Fortran小心编译的Fortran 9x代码:如果使用的Fortran 9x编译器与配置R时使用的Fortran 77编译器不同,它可能不起作用,特别是如果子例程名称是不小写或包括下划线。也可以使用.C并自己进行任何必要的符号名称翻译。

另外,我怀疑你的包装器子程序不应该驻留在一个模块中,否则你可能会遇到额外的名字问题。但这只是包装函数的一个限制,它必须在R中可见。

您可以检查DLL中的导出名称(将objdump -x your.so发送到文件并查找导出的符号)。加载DLL后,还可以使用is.loaded("your.symbol")检查R。请注意,通常,gfortran会在名称中添加额外的下划线,而当您从R调用.Fortran时则不需要它。如上所述,您可以使用.C代替(但是,请记住Fortran参数已通过通过参考)。

为了检查您是否理解整个过程,我建议您在一个简单的示例上进行测试,例如仅mysub(x,y,z)的唯一子例程z=x+y。当这个运行时,你可以详细说明它来调用更复杂的例程。

修改 当你将数组参数从R传递给Fortran时,你不应该使用假定形状或延迟形状数组,而只使用假定大小的数组,即通常在Fortran 77中传递的数组。这是因为R只知道如何通过指向原始数据的指针,而假定形状和延迟形状需要更多信息,R不知道数据结构。

例如,您可以这样做:

subroutine mysub(n, a)
    real :: a(n, n)
    ...
end subroutine

但这肯定会失败:

subroutine mysub(a)
    real :: a(:, :)
    ...
end subroutine

此外,您不能将函数参数从R传递给Fortran,因为这需要回调的特殊数据结构(在引擎盖下,R是Scheme方言,它使用S表达式)。您可以通过.C.Call在C中执行此操作(请参阅dyn.load.Call的帮助)。

答案 1 :(得分:1)

您确实可以使用R base包中的.Foreign函数从R调用Fortran(请参阅?.Foreign)。有关如何执行此操作的一些明确示例,请参阅以下页面,了解如何从R调用Fortran(以及C):http://users.stat.umn.edu/~geyer/rc/