using a Fortran module in R?

后端 未结 1 1081
一个人的身影
一个人的身影 2020-12-17 07:11

I am able to use a fortran subroutine in R as long as I don\'t have it inside a module.For example:

subroutine dboard(darts, dartsscore)
  implicit none
  in         


        
1条回答
  •  醉梦人生
    2020-12-17 07:59

    Thanks to @roygvib and @francescalus this is my working module:

    Module Fortranpi
    IMPLICIT NONE
    contains
    subroutine dboard(darts, dartsscore)
      integer, intent(in)                    :: darts
      double precision, intent(out)          :: dartsscore
      double precision                       :: x_coord, y_coord
      integer                                :: score, n
    
    score = 0
    do n = 1, darts
      call random_number(x_coord)
      call random_number(y_coord)
    
      if ((x_coord**2 + y_coord**2) <= 1.0d0) then
      score = score + 1
      end if
    end do
    
    dartsscore = 4.0d0*score/darts
    
    end subroutine dboard
    
    subroutine pi(avepi, DARTS, ROUNDS) bind(C, name="pi_")
      use, intrinsic                         :: iso_c_binding, only : c_double, c_int
      real(c_double), intent(out)            ::  avepi
      integer(c_int), intent(in)             ::  DARTS, ROUNDS
      integer                                ::  MASTER, rank, i, n
      integer, allocatable                   ::  seed(:)
      double precision                       ::  pi_est, homepi, pirecv, pisum
    
    ! we set it to zero in the sequential run
    rank = 0
    ! initialize the random number generator
    ! we make sure the seed is different for each task
    call random_seed()
    call random_seed(size = n)
    allocate(seed(n))
    seed = 12 + rank*11
    call random_seed(put=seed(1:n))
    deallocate(seed)
    
    avepi = 0
    do i = 0, ROUNDS-1
      call dboard(darts, pi_est)
      ! calculate the average value of pi over all iterations
      avepi = ((avepi*i) + pi_est)/(i + 1)
    end do
    end subroutine pi
    
    end module Fortranpi
    

    0 讨论(0)
提交回复
热议问题