Theo
Theo

Reputation: 179

Passing different set of variables in a FORTRAN subroutine

I want to apply three different methods, selected with the value of an integer switch. The first method uses two integers, the second a real array and an integer and the third a real 2D array. In my current implementation, I allocate and pass as parameters all the above data (2 int + real_array + int + real_2array). I could also use a module, but it would be similar. I'm searching for a method to define only the data that my method will use (i.e. only the matrix for method 3) and nothing else. Any suggestions?

Edit:
I have made a simplified version of what I described above.
A part of the main program:

  INTEGER                             :: m, imeth
  REAL*8                              :: x, y
  REAL*8, DIMENSION(:), ALLOCATABLE   :: uu, wc
  REAL*8, DIMENSION(:,:), ALLOCATABLE :: BCH

  imeth = 0
  m = 64
  ALLOCATE(uu(m), uu_(m), wc(m))
  ALLOCATE(BCH(m,m))

  if (imeth .EQ. 0) then
     x = 1.0d0
     y = 2.0d0
  elseif (imeth .EQ. 1) then
     !Assign values to wc
  else
     !Assign values to BCH
  endif

  call subr(m,x,y,uu,uu_,imeth,BCH,wc)

  STOP
  END

and a subroutine

  SUBROUTINE subr(n,a,b,u,u_,imeth,DCH,ws)
  IMPLICIT NONE
  INTEGER,  INTENT(IN)  :: n, imeth
  REAL*8,   INTENT(IN)  :: u(n), DCH(n,n), ws(n)
  REAL*8,   INTENT(OUT) :: u_(n)
  INTEGER              :: i

  if (imeth .EQ. 0) then
     u_ = -u_ * 0.5d0 / (a+b)
  elseif (imeth .EQ. 1) then
     u_ = -u / ws
  else
     u_ = matmul(DCH,u)
  endif

  RETURN
  END SUBROUTINE subr

I want the main program to have a form like

  imeth = 0
  m = 64
  ALLOCATE(uu(m), uu_(m))


  if (imeth .EQ. 0) then
     a = 1.0d0
     b = 2.0d0
  elseif (imeth .EQ. 1) then
     ALLOCATE(wc(m))
     !Assign values to wc
  else
     ALLOCATE(BCH(m,m))
     !Assign values to BCH
  endif

  if (imeth .EQ. 0) then
     call subrA(m,x,y,uu,uu_)
  elseif (imeth .EQ. 1) then
     call subrB(m,wc,uu,uu_)
  else
     call subrC(m,BCH,uu,uu_)
  endif

Upvotes: 2

Views: 555

Answers (1)

Alexander Vogt
Alexander Vogt

Reputation: 18098

EDIT: After OP added the code I think that using optional arguments in conjunction with the present intrinsic might be better suited for this task. The subroutine could then read

SUBROUTINE subr(n,u_,a,b,u,DCH,ws)
  IMPLICIT NONE
  INTEGER,  INTENT(IN)           :: n
  REAL*8,   INTENT(OUT)          :: u_(n)
  REAL*8,   INTENT(IN),OPTIONAL  :: a(n)
  REAL*8,   INTENT(IN),OPTIONAL  :: b(n)
  REAL*8,   INTENT(IN),OPTIONAL  :: u(n)
  REAL*8,   INTENT(IN),OPTIONAL  :: DCH(n,n)
  REAL*8,   INTENT(IN),OPTIONAL  :: ws(n)
  INTEGER                        :: i

  if ( present(a) .and. present(b) ) then
     u_ = -u_ * 0.5d0 / (a+b)
  elseif ( present(u) .and. present(ws) ) then
     u_ = -u / ws
  elseif ( present(wch) .and. present(u) ) then
     u_ = matmul(DCH,u)
  else
     stop 'invalid combination'
  endif

END SUBROUTINE subr

Here is the old answer as it still might be helpful:


Maybe you could try interfaces:

module interface_test

  implicit none

  interface method
    module procedure method1
    module procedure method2
    module procedure method3
  end interface

contains

  subroutine method1(int1, int2)
    implicit none
    integer,intent(in)  :: int1
    integer,intent(out) :: int2

    int2 = 2*int1
  end subroutine

  subroutine method2(int, realArray)
    implicit none
    integer,intent(in)  :: int
    real,intent(out)    :: realArray(:)

    realArray = real(2*int)
  end subroutine

  subroutine method3(realArray)
    implicit none
    real,intent(inout)  :: realArray(:,:)

    realArray = 2*realArray
  end subroutine

end module

program test
  use interface_test, only: method
  implicit none
  integer :: int1, int2
  real    :: arr1D(10)
  real    :: arr2D(10,10)

  int1 = 1

  call method(int1, int2)
  print *, int2

  call method(int1,arr1D)
  print *, arr1D(1)

  arr2D = 1.
  call method(arr2D)
  print *, arr2D(1,1)
end program

Upvotes: 2

Related Questions