simonblaha
simonblaha

Reputation: 1

What causes fotran ranks not matching error

I have no experience with fortran, but I need to define the variable error.

a = 3.086e18

error = ABS(((xCoord / a)**2 + (9 / 4)*(yCoord / a)**2 + (zCoord / a)**2 -1)**3 - (xCoord / a)**2 * (yCoord / a)**3 - (9 / 200) * (yCoord / a)**2 * (zCoord / a)**3)

When I run the code, I get the Incompatible ranks 0 and 1 in assignment error.

I don't know anything about fortran and this issue is probably trivial. Can you please tell me where's the problem?

Upvotes: 0

Views: 69

Answers (2)

jalex
jalex

Reputation: 1524

So please try to include more details in your question. Specifically variable declarations, function arguments and expected output.

In this case, calculations in Fortran are vectorized, applying the same math formula to all elements of an array. Because xCoord is a vector, then any math formula involving xCoord would result in a vector.

To fix your error, you might want to keep only the largest value from the result. You do this with the maxval() function

error = maxval(abs(((xCoord / a)**2 + (9 / 4d0)*(yCoord / a)**2 &
        + (zCoord / a)**2 -1)**3 - (xCoord / a)**2 * (yCoord / a)**3 &
        - (9 / 200d0) * (yCoord / a)**2 * (zCoord / a)**3))

Also note that (9 / 4) results in 2 not 2.25 as the compiler assumes you want integer division. To correct it type (9 / 4d0).

PS. Fortran = Formula Translation which exactly this process of applying a formula to all elements of an array.

Upvotes: 1

hakim
hakim

Reputation: 139

Here is a small example (inspired from your code) in order to understand better:

! example.f90

program main
  implicit none
  integer,dimension(2)::array !< Declaration of the array 
  real::a=3.086e18
  real::error !< Scalar 
  integer::i
  do i=1,2
     array(i)=1 !< Initialization of the array  
  end do
  error=array/a
  print*,error
end program main

Output:

11 |   error=array/a
      |  1
Error: Incompatible ranks 0 and 1 in assignment at (1)

The problem is that error is a scalar while array/a is an array of 2 elements (as the declaration of array). You canno't store an array in a scalar (obviously).

In order to make it work you need to declare error as an array of 2 elements like this:

  real,dimension(2)::error

Output:

 3.24044050E-19   3.24044050E-19

Upvotes: 1

Related Questions