0

I try to introduce complex-valued array and variables in Fortran. For the following code

program 

   integer ::  i , j !two dimensional real array
   real(dp)  :: m1(3,2) 
   complex(dp) ::  a1(3,2),a2(3,2), c0, c1


   !assigning some values to the array numbers


   c0 = (1.0_dp, 0.0_dp)
   c1 = (0.000000001_dp, 0.0_dp)

   do i=1,3
      do j = 1, 2
         a1(i,j) = c0*i*j
      end do
   end do   

   do i=1,3
      do j = 1, 2
         a2(i,j) = c0*i*j + c1
      end do
   end do   

   do i=1,3
      do j = 1, 2
        if (dabs( dreal(a1(i,j)) - dreal(a2(i,j))) > 1.e-6 then
          write (*,*), 'warning',i,j, a1(i,j), a2(i,j)
        end if
      end do
   end do      

   write (*,*), a1(1,1), a2(1,1)




end program 

ifort gives me

   complex(dp) ::  a1(3,2), a2(3,2)
-----------^

why complex(dp) requires compile-time constant and how to fix it? Thank you.

AlphaF20
  • 583
  • 6
  • 14

1 Answers1

2

The kind parameter dp must be a constant, see Fortran - setting kind/precision of a variable at run time

However, you do not have the same problem as in the link, you did not try to define dp at all! First, you must use IMPLICIT NONE, it is absolutely necessary for safe programming and the biggest problem with your code. Then it will tell you that the type of dp is not declared.

You just define the kind constant in one of the usual ways, as a constant. The most simple is:

program main

  !NECESSARY!
  implicit none

  integer, parameter :: dp = kind(1.d0)

Note that I named the program main, you have to name your program. Or just omit the program.

More about defining real kinds can be found at Fortran 90 kind parameter


Another note: Forget dabs() and dreal(). dabs() is an old remnant of Fortran 66 and dreal() is not standard Fortran at all. Just use abs and either dble() or better real( ,dp).