Fortran bug: rank problem gfortran

In summary, the conversation discusses a code that does not compile with gfortran and the use of the minloc function to find the minimum value in an array. The conversation reveals that the error is caused by a misunderstanding of the output of the minloc function, which returns an array of integers rather than a single integer. The code is then fixed by introducing a temporary array and extracting the single element from it. The conversation also mentions that rank errors indicate issues with variable dimensions.
  • #1
billiards
767
16
This is my own code, and it won't compile with gfortran. All I want to do is extract the location of the cell with the minimum value in an array. A seemingly simple task but one that does not work with the intrinsic function minloc, for reasons I do not understand.

The error message:

Code:
    iclosept=minloc(dist)
    1
Error: Incompatible ranks 0 and 1 in assignment at (1)

The code:

Code:
  subroutine distance_to_trench(pt,tr,mindist,iclosept)
  ! find (great circle) distance from point to each point on the trench
  ! distance = the minimum distance to the trench
  
    type(point) :: pt
    type(trench) :: tr
    real, parameter :: pi=3.14159265359
    real, parameter :: eradius=6371
    real :: rad,deg
    real :: x1,y1,z1,x2,y2,z2
    real,dimension(:),allocatable :: dist
    real :: mindist
    integer :: ii, length, iclosept, a
    
    rad=pi/180
    deg=180/pi
    
    length=size(tr%poly%points%x)
    allocate(dist(length))
    
  ! method:
  ! convert lat lons to cartesian vectors
  ! use angle between vectors to find the distance
  
    x1=cos(rad*pt%y)*cos(rad*pt%x)
    y1=cos(rad*pt%y)*sin(rad*pt%x)
    z1=sin(rad*pt%y)
  
    do ii=1, length
      
      x2=cos(rad*tr%poly%points(ii)%y)*cos(rad*tr%poly%points(ii)%x)
      y2=cos(rad*tr%poly%points(ii)%y)*sin(rad*tr%poly%points(ii)%x)
      z2=sin(rad*tr%poly%points(ii)%y)
  
      dist(ii)=eradius*acos(x1*x2+y1*y2+z1*z2)
      
      print *,dist(ii)
    
    enddo
    
    mindist=minval(dist)
    
    a=size(dist)
    print *,'size of dist', size(dist), a

    iclosept=minloc(dist)
    print *,'minloc', minloc(dist), iclosept
    
    deallocate(dist)
    
  end subroutine distance_to_trench
 
Technology news on Phys.org
  • #2
Fixed it.

Turns out I was confused about the output of the minloc function. It returns an array of integer(s). I was assuming it returned an integer. Because I was only thinking about a 1-d array. But in the case of a 1-d array, it actually returns an array of size 1, where the element contains the integer I was after.

To fix my above code I introduced a temporary array and assigned it by minloc. Then I extracted the single element from it into a pure integer.

Thus:

Code:
  subroutine distance_to_trench(pt,tr,mindist,iclosept)
  ! find (great circle) distance from point to each point on the trench
  ! distance = the minimum distance to the trench
  
    type(point) :: pt
    type(trench) :: tr
    real, parameter :: pi=3.14159265359
    real, parameter :: eradius=6371
    real :: rad,deg
    real :: x1,y1,z1,x2,y2,z2
    real,dimension(:),allocatable :: dist
    real :: mindist
    integer :: ii, length, iclosept, temp(1)
    
    rad=pi/180
    deg=180/pi
    
    length=size(tr%poly%points%x)
    allocate(dist(length))
    
  ! method:
  ! convert lat lons to cartesian vectors
  ! use angle between vectors to find the distance
  
    x1=cos(rad*pt%y)*cos(rad*pt%x)
    y1=cos(rad*pt%y)*sin(rad*pt%x)
    z1=sin(rad*pt%y)
  
    do ii=1, length
      
      x2=cos(rad*tr%poly%points(ii)%y)*cos(rad*tr%poly%points(ii)%x)
      y2=cos(rad*tr%poly%points(ii)%y)*sin(rad*tr%poly%points(ii)%x)
      z2=sin(rad*tr%poly%points(ii)%y)
  
      dist(ii)=eradius*acos(x1*x2+y1*y2+z1*z2)
      
      print *,dist(ii)
    
    enddo
    
    mindist=minval(dist)
    temp=minloc(dist)
    
    iclosept=temp(1)
    
    deallocate(dist)
    
  end subroutine distance_to_trench
 
  • #5


It appears that the issue with your code lies in the assignment of the minimum location, specifically in the line "iclosept=minloc(dist)". The error message indicates that there is an issue with the ranks of the variables being assigned. This means that the variable iclosept is expecting a rank 0 value (a single value) but the function minloc is returning a rank 1 value (an array).

To fix this, you may need to specify the location or index of the minimum value in the dist array, rather than just assigning it to iclosept. Alternatively, you could use the minloc function with the "DIM" argument to specify which dimension of the array to search for the minimum value.

It's also possible that there may be other issues with the code that could be causing this error, such as the allocation of the dist array or the use of the minval function. I would recommend reviewing the documentation for these functions and double-checking your code to ensure that it is correctly handling the array and the minimum value.

Overall, it's important to carefully review your code and error messages when encountering issues like this. Sometimes the cause of the error may not be immediately apparent, but with careful debugging and troubleshooting, you can often identify and resolve the issue. Best of luck with your code!
 

Related to Fortran bug: rank problem gfortran

1. What is a Fortran bug?

A Fortran bug is an error or flaw in the code written in the Fortran programming language. These bugs can cause unexpected or incorrect behavior in the program and can be difficult to identify and fix.

2. What is the rank problem in Fortran?

The rank problem in Fortran refers to an issue where the dimension of an array or variable is not properly defined, leading to errors in the program. This can happen when the programmer forgets to specify the dimension or uses incorrect syntax for defining the dimension.

3. How does the rank problem affect gfortran?

The rank problem can affect gfortran, which is the GNU Fortran compiler, in the same way it affects any other Fortran compiler. If the code has a rank problem, gfortran will not be able to compile it correctly, resulting in errors or unexpected behavior.

4. How can I fix a rank problem in Fortran?

To fix a rank problem in Fortran, you will need to carefully review your code and ensure that all arrays and variables have the correct dimensions specified. You may also need to check for any syntax errors or typos in your code that may be causing the rank problem.

5. Are there any tools or techniques for detecting rank problems in Fortran?

Yes, there are several tools and techniques that can help detect rank problems in Fortran. These include using debugging tools, such as the GNU Debugger (GDB), to step through the code and identify any errors. There are also code analysis tools, such as the Fortran lint tool, which can help identify potential issues in the code, including rank problems.

Similar threads

  • Programming and Computer Science
Replies
5
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • General Math
Replies
3
Views
924
  • Programming and Computer Science
Replies
2
Views
3K
Replies
11
Views
6K
  • Programming and Computer Science
Replies
4
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
  • Precalculus Mathematics Homework Help
Replies
5
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
1K
  • Astronomy and Astrophysics
Replies
4
Views
3K
Back
Top