Search code examples
arraysfortranfortran77abaqus

Read file and find index of value in array in Fortran


I am making user subroutine file of Abaqus. However, in reading a file I met with a difficulty. Since, it is Fortran 77 based, it is so hard to find exact solution. My intention is to read a file where an 1X1 array is include. Then, to find an index of a value in the array.

My code to read a file is :

open (unit=99,file='D:\SIMULATION\dist.txt',status='old')
read (99,*) dist
close (99)

And the code for finding index of value in array is:

loc=minloc(abs(dist-1),1)

I think minloc is for Fortran 90, right? Is there any function in Fortran 77 similar to minloc?


Solution

  • The code you've shown should compile and run as expected. I'm assuming that you are actually reading a 1xN array and that when you said "1X1" it was a typo - otherwise, there's no point in using minloc.

    However, the error message you reported in a comment (An array-valued argument is required in this context) only occurs if you use the minloc intrinsic on a scalar value. Thus, my guess is that you did not declare dist as an array. Here is a quick example of what I mean:

    ! The contents of 'values.txt' are: -3.1, 4.1, 5.9, 2.6, -5.4
    ! Values may be separated by commas or blanks.
    
    program get_min_dist
        implicit none
        real :: x                   ! <-- Cannot be used to represent an array.
        real, dimension(5) :: a     ! <-- An array of 5 reals. Do this instead.
        integer :: loc, funit1
    
        open(newunit=funit1, file="values.txt", status="old")
        read(funit1,*) x
        rewind(funit1)
        read(funit1,*) a
        close(funit1)
    
        loc = minloc(abs(a-1),1)    ! <-- I'm assuming there is a reason to 
                                    ! subtract 1 from all values in the array
    
        ! loc = minloc(abs(x-1),1)  ! <-- Error 'An array-valued arg is required`
        print*, "x=",x
        print*, "a=",a
        print*, "index=", loc
        print*, "value=", a(loc)
    end program get_min_dist
    

    With read(funit1,*) x the first value will be assigned when the file is read, resulting in the error message you have seen. With the array a, however, you get the expected output.

    Your confusion regarding the need to use F77 compatible code may be due to the fact that Abaqus continues to provide examples and docs with F77-style fixed-formatting, and requires Fortran source code to be given the .f or .for extension1. By default, this extension tells ifort to expect fixed-format code2. However, any Fortran features supported by the version of the compiler you use is still valid - even in fixed-format, if you must. For further information about the availability of features from different Fortran versions, see your (Intel Fortran) documentation.

    1 I'd be glad to know if this can be changed somehow, e.g. to allow the .f90 extension.

    2 This setting can be changed in the Abaqus environment file, at least for the versions I've used (6.9-6.14). I don't think that has changed with newer releases, but maybe. I don't recommend changing it if you share the environment with other users without their consent, especially for newbies.