1

I am very new in Fortran programming world. I am trying to write a function find in Fortran which accomplish the same as MATLAB's find() or Python's numpy.where() to be included in LS-DYNA UMAT. I can't use findloc function introduced in Fortran 2008 as LS-DYNA recognizes Fortran 90 in fixed format. The following function is executing properly, but I have to send the size of the array as an argument.

      program test
        integer*8, parameter:: n = 5
        integer*8:: var(n), val

        var = (/2,9,4,5,8/)
        val = 9

        index  = find(var,n,val)
        print *, index
      end program test


      function find(array,n,val) result(loc)
      implicit none
      integer*8:: i,n,val,loc
      integer*8:: array(n)

      do i = 1,n
        if (array(i) .eq. val) then
          loc = i
          exit
        else
          loc = 0
        endif
      end do

      return
      end function find

When I tried to modify the code to send an array without a size as function argument as follows, the array is not passed. Your help would be appreciated to modify the function to pass an array without size (n) argument.


      program test
        integer*8, parameter:: n = 5
        integer*8:: var(n), val

        var = (/2,9,4,5,8/)
        val = 9

        index  = find(var,val)
        print *, index
      end program test


      function find(array,val) result(loc)
      implicit none
      integer*8:: i,n,val,loc
      integer*8:: array(:)
      n = size(array)

      do i = 1,n
        if (array(i) .eq. val) then
          loc = i
          exit
        else
          loc = 0
        endif
      end do

      return
      end function find

I also tried following code snippets to make the function working, but no luck!

      integer*8, allocatable:: array(:)
      n = size(array)
      allocate(array(n))
      ...
      ...
      deallocate(array)
      ...
      ...

0 Answers0