user3381378
user3381378

Reputation: 23

Fortran "Error: Incompatible ranks 0 and 1 in assignment"

I'm writing a linear inverse program using geophysical data. I'm new to programming in Fortran although I have created programs relating to geophysical problems with Fortran before.

I'm encountering the error : "Error: Incompatible ranks 0 and 1 in assignment" when compiling. I know this has to do with lengths not agreeing, but I have been unable to resolve it. I want to assign the row of Prism_r(i,pp) with the values previously calculated, namely r1-r4. The error is as followed:

Prism_r(i,pp)=(/ r1(pp),r2(pp),r3(pp),r4(pp) /)                 
    1
Error: Incompatible ranks 0 and 1 in assignment at (1) 

Here is the relevant code:

real, dimension(0:P-1) :: r1, r2, r3, r4    
real, dimension(0:D-1,0:3) ::Prism_r, Prism_theta
.....
do i=0,D-1
    do pp=0,P-1
    r1(pp)=sqrt((x2+2*PP-0.2*i)**2+z1**2)
    r2(pp)=sqrt((x2+2*PP-0.2*i)**2+z2**2)
    r3(pp)=sqrt((x1+2*PP-0.2*i)**2+z2**2)
    r4(pp)=sqrt((x1+2*PP-0.2*i)**2+z1**2)
    Prism_r(i,pp)=(/ r1(pp),r2(pp),r3(pp),r4(pp) /)
    enddo  
enddo

The calculations are being performed correctly when I comment Prism_r out, but it will not assign values to it. Does anyone have advice to how I need to correctly define r1-r4 so their values will be assigned to Prism_r?

Upvotes: 2

Views: 38455

Answers (1)

eriktous
eriktous

Reputation: 6649

It actually doesn't have to do with lengths not agreeing, but instead with ranks not agreeing, just as the error message says.

Prism_r(i,pp) is a single element of the array: it's a scalar, i.e. rank 0.

(/ r1(pp),r2(pp),r3(pp),r4(pp) /) is a rank 1 array (of length four).

In fortran you can't assign an array to a scalar.

Upvotes: 4

Related Questions