Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Having problem to understand kind notation in fortran 90

  1. Apr 26, 2012 #1
    hi all,

    i have codes for 1d fdtd maxwell equation. the problem i am facing right now is some error regarding the kind notation.

    Code (Text):

    !1d fdtd Simulation in free space
    subroutine fd1d01(f0,miu,delta,S,E0)
    implicit none

    real        :: f0
    real        :: miu
    real        :: delta
    real        :: S
    real        :: E0
    integer     :: iinit
    integer     :: ilast
    real        :: Ca
    real        :: Da
    integer (kind = 2)  :: i
    integer (kind = 5)  :: n
    real        :: tdelta
    real        :: c
    real,dimension(:,:),allocatable   :: Ez
    real,dimension(:,:),allocatable   :: Hy
    real, parameter :: pi = 3.14159265
    real        :: Cb
    real        :: Db
    real        :: lambda
    real        :: alpha
    character(len=20)  ::filename

    allocate (Ez(-1:103,-1:503))
    allocate (Hy(-1:103,-1:503))

    f0 = 1.0
    miu = 1.0
    delta = 1.0
    S = 1.0
    E0 = 1.0
    iinit = 0
    ilast = 100
     
    c = 3.e8
    lambda = c/f0
    print *,'lambda=',lambda
    alpha = 0.04*lambda
    print *,'alpha=',alpha
    tdelta = 1.0*alpha/(S*c)
    print*, 'tdelta=',tdelta

    !initialization
    do i = iinit,ilast
      Ez(iinit+1/2,0) = 0
      Hy(iinit+1,0) = 0
    end do


    Ca = 1.0
    Cb = tdelta/(delta*alpha)
    Da = 1.0
    Db = tdelta/(miu*alpha)

    !write (filename, "('ez',I3.3,'.dat')") n
    !open (unit=130,file=filename)
    do n = 1,500
     write (filename, "('ez',I3.3,'.dat')") n
     open (unit=130,file=filename)
      do i = iinit+1,ilast
       Ez(real(i-0.5,n+0.5)) = Ca*(Ez(real(i-0.5,n-0.5))) + Cb*(Hy(i,n)-Hy(i-1,n))
       Hy(i,n+1) = Da*(Hy(i,n)) + Db*(Ez(real(i+0.5,n+0.5))-Ez(real(i-0.5,n+0.5)))  

      Write (130,*) i-0.5, Ez(real (i-0.5,n+0.5))
      end do !i

      !plane wave source

      Ez(1./2+(ilast-iinit)/2,n) = E0*sin (2*pi*f0*n*tdelta)

     close (unit=130)

    end do !n

    end SUBROUTINE fd1d01

     
    the error as below:

    Code (Text):

    subroutines.f90:15.17:

    integer (kind = 5)  :: n
                     1
    Error: Kind 5 not supported for type INTEGER at (1)
    subroutines.f90:62.4:

    do n = 1,500
        1
    Error: Symbol 'n' at (1) has no IMPLICIT type
    subroutines.f90:76.5:

      Ez(1./2+(ilast-iinit)/2,n) = E0*sin (2*pi*f0*n*tdelta)
         1
    Warning: Extension: REAL array index at (1)
    make: *** [subroutines.o] Error 1

     
    can anyone advice me how to solve this problem.

    thank you
     
  2. jcsd
  3. Apr 26, 2012 #2

    Mark44

    Staff: Mentor

    It seems to me that on the computer you are compiling this code, kind = 5 is not supported for integer types. You are asking the compiler to allocate half the normal size for an integer, but the compiler can't do this.

    I would either take the kind part out of this line of code
    Code (Text):
    integer (kind = 5)  :: n
    or change to kind = 2.

    Here's a link to some info as pertains to the GNU fortran compiler - http://gcc.gnu.org/onlinedocs/gcc-3.4.6/g77/Kind-Notation.html
     
  4. Apr 26, 2012 #3
    hi mark44,

    i did change KIND= 2, but some error came out as below:

    Code (Text):

    subroutines.f90:68.17:

       Ez(real(i-0.5,n+0.5)) = Ca*(Ez(real(i-0.5,n-0.5))) + Cb*(Hy(i,n)-Hy(i-1,n))
                     1
    Error: 'kind' argument of 'real' intrinsic at (1) must be INTEGER
    subroutines.f90:68.45:

       Ez(real(i-0.5,n+0.5)) = Ca*(Ez(real(i-0.5,n-0.5))) + Cb*(Hy(i,n)-Hy(i-1,n))
                                                 1
    Error: 'kind' argument of 'real' intrinsic at (1) must be INTEGER
    subroutines.f90:69.70:

       Hy(i,n+1) = Da*(Hy(i,n)) + Db*(Ez(real(i+0.5,n+0.5))-Ez(real(i-0.5,n+0.5)))  
                                                                          1
    Error: 'kind' argument of 'real' intrinsic at (1) must be INTEGER
    subroutines.f90:71.38:

      Write (130,*) i-0.5, Ez(real (i-0.5,n+0.5))
                                          1
    Error: 'kind' argument of 'real' intrinsic at (1) must be INTEGER
    subroutines.f90:76.5:

      Ez(1./2+(ilast-iinit)/2,n) = E0*sin (2*pi*f0*n*tdelta)
         1
    Warning: Extension: REAL array index at (1)
     
    what is that mean?

    thank you
     
  5. Apr 27, 2012 #4

    Mark44

    Staff: Mentor

    Most or all of your errors seem to be that your array indexes for the Ez array are real, not integer.

    I'm not sure what you're trying to do in this line:
    Code (Text):
    Ez(real(i-0.5,n+0.5))= Ca*(Ez(real(i-0.5,n-0.5))) + Cb*(Hy(i,n)-Hy(i-1,n))
     
    The real intrinsic function converts a single integer to a single-precision floating point number. It takes one argument, not two as you are doing.

    Your Ez array is two-dimensional - each index needs to be an integer value, not a floating point value. Why do you have indexes that are obviously fractional?

    Notice that all five errors point out problems in how you are indexing your Ez array.
     
  6. Apr 27, 2012 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    IMO, "kind" is a good idea with a very dumb implementation. The good idea is that you can describe what range of values (for integers) or precision (for reals) your program needs, and the compiler can then select the best way to implement this depending on the hardware (32 or 64 bit systems, etc).

    The very dumb implementation is that there is NO standardization of what the different integers that denote "kinds" represent, and sometimes no obvious logic either. For example GNU fortran uses KIND=2 to mean "integers of twice the normal length" (that seem logical enough), but KIND=5 means "half the normal length", and "KIND = 7" means "integers large enough to hold a pointer". Yeah, right, it's obvious to everybody that's what 5 and 7 mean :devil:

    Even worse, GNU says it might change the definitions the definitions in future, so even old "GNU-compatible" code will break. http://gcc.gnu.org/onlinedocs/gcc-3.4.5/g77/Kind-Notation.html

    And other compilers might use KIND=2 to mean "2-byte integers", which are "half the normal length" compared with GNU's interpretation of "twice the normal length".
     
  7. May 9, 2012 #6
    i have make some change. it's compiled but with some warning

    Code (Text):


    !1d fdtd Simulation in free space
    subroutine fd1d01(f0,miu,delta,S,E0)
    implicit none

    real                               :: f0
    real                               :: miu
    real                               :: delta
    real                               :: S
    real                               :: E0
    integer                            :: iinit
    integer                            :: ilast
    integer                            :: i
    integer                            :: n
    double precision                   :: Ca
    double precision                   :: Da
    double precision                   :: tdelta
    double precision                   :: c
    double precision,allocatable       :: Ez(:,:)
    double precision,allocatable       :: Hy(:,:)
    real, parameter                    :: pi = 3.14159265
    double precision                   :: Cb
    double precision                   :: Db
    double precision                   :: lambda
    double precision                   :: alpha
    character(len=20)                  :: filename

    allocate (Ez(-0.5:103,-0.5:503))
    allocate (Hy(-0.5:103,-0.5:503))

    f0 = 1.0
    miu = 1.0
    delta = 1.0
    S = 1.0
    E0 = 1.0
    iinit = 0
    ilast = 100
     
    c = 3.e8
    lambda = c/f0
    alpha = 0.04*lambda
    tdelta = 1.0*alpha/(S*c)

    !initialization
    Ez = 0
    Hy = 0

    Ca = 1.0
    Cb = tdelta/(delta*alpha)
    Da = 1.0
    Db = tdelta/(miu*alpha)

    do n = 1,500
     write (filename, "('ez',I3.3,'.dat')") n
     open (unit=130,file=filename)
      do i = iinit+1,ilast
       Ez(i-1./2,n+1./2) = Ca*(Ez(i-1./2,n-1./2)) + Cb*(Hy(i,n)-Hy(i-1.,n))
       Hy(i,n+1.) = Da*(Hy(i,n)) + Db*(Ez(i+1./2,n+1./2)-Ez(i-1./2,n+1./2))
    !  Print*, 'i+1./2=',i+1./2,'i-1./2=',i-1./2
      Write (130,*) i-1./2, Ez(i-1./2,n+1./2)
      end do !i

      !plane wave source

      Ez(1./2+(ilast-iinit)/2,n) = E0*sin (2*pi*f0*n*tdelta)

     close (unit=130)

    end do !n

    end SUBROUTINE fd1d01

     
    and the warnings are as below:

    Code (Text):


    gfortran -g  -I/usr/include -c main.f90
    gfortran -g  -I/usr/include -c subroutines.f90
    subroutines.f90:27.13:

    allocate (Ez(-0.5:103,-0.5:503))
                 1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:27.22:

    allocate (Ez(-0.5:103,-0.5:503))
                          1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:28.13:

    allocate (Hy(-0.5:103,-0.5:503))
                 1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:28.22:

    allocate (Hy(-0.5:103,-0.5:503))
                          1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:65.6:

       Ez(i-1./2,n+1./2) = Ca*(Ez(i-1./2,n-1./2)) + Cb*(Hy(i,n)-Hy(i-1.,n))
          1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:65.13:

       Ez(i-1./2,n+1./2) = Ca*(Ez(i-1./2,n-1./2)) + Cb*(Hy(i,n)-Hy(i-1.,n))
                 1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:65.63:

       Ez(i-1./2,n+1./2) = Ca*(Ez(i-1./2,n-1./2)) + Cb*(Hy(i,n)-Hy(i-1.,n))
                                                                   1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:65.30:

       Ez(i-1./2,n+1./2) = Ca*(Ez(i-1./2,n-1./2)) + Cb*(Hy(i,n)-Hy(i-1.,n))
                                  1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:65.37:

       Ez(i-1./2,n+1./2) = Ca*(Ez(i-1./2,n-1./2)) + Cb*(Hy(i,n)-Hy(i-1.,n))
                                         1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:66.8:

       Hy(i,n+1.) = Da*(Hy(i,n)) + Db*(Ez(i+1./2,n+1./2)-Ez(i-1./2,n+1./2))
            1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:66.56:

       Hy(i,n+1.) = Da*(Hy(i,n)) + Db*(Ez(i+1./2,n+1./2)-Ez(i-1./2,n+1./2))
                                                            1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:66.63:

       Hy(i,n+1.) = Da*(Hy(i,n)) + Db*(Ez(i+1./2,n+1./2)-Ez(i-1./2,n+1./2))
                                                                   1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:66.38:

       Hy(i,n+1.) = Da*(Hy(i,n)) + Db*(Ez(i+1./2,n+1./2)-Ez(i-1./2,n+1./2))
                                          1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:66.45:

       Hy(i,n+1.) = Da*(Hy(i,n)) + Db*(Ez(i+1./2,n+1./2)-Ez(i-1./2,n+1./2))
                                                 1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:70.27:

      Write (130,*) i-1./2, Ez(i-1./2,n+1./2)
                               1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:70.34:

      Write (130,*) i-1./2, Ez(i-1./2,n+1./2)
                                      1
    Warning: Extension: REAL array index at (1)
    subroutines.f90:75.5:

      Ez(1./2+(ilast-iinit)/2,n) = E0*sin (2*pi*f0*n*tdelta)
         1
    Warning: Extension: REAL array index at (1)
    gfortran -g  -I/usr/include -o main main.o subroutines.o -L/usr/lib64/liblapack -L/usr/lib64/libblas
     Main finished.

     
    can u guys explain why i get this warning?

    thank you
     
  8. May 10, 2012 #7

    Mark44

    Staff: Mentor

    This is what I said a couple of posts back (emphasis added). Did you read it?
     
  9. May 10, 2012 #8
    mark 44,

    i read your reply, and i make change...but somehow it came out with lib64 error and so on.

    maybe i misunderstood, ( my english is freaking bad, and fortran for me is just a few months experience)....i've change this part

    allocate (Ez(-0.5:103,-0.5:503))
    allocate (Hy(-0.5:103,-0.5:503))

    into

    allocate (Ez(-2:100,0:501))
    allocate (Hy(-2:100,0:501))

    or

    allocate (Ez(0:100,0:500))
    allocate (Hy(0:100,0:500))

    and then this error came out:

    *** glibc detected *** ./main: free(): invalid pointer: 0x00007fdb92dbf010 ***

    and so on.


    or is it the warning came out because of this algorithm

    Ez(i-1./2,n+1./2) = Ca*(Ez(i-1./2,n-1./2)) + Cb*(Hy(i,n)-Hy(i-1.,n))


    i just translate the finite difference expression for maxwell equations from taflove's book into fortran language.

    please highlight me...which part i should change.

    and very big thank you for your time
     
  10. May 10, 2012 #9

    Mark44

    Staff: Mentor

    I don't have much recent experience with fortran, but I suspect that this error is cause by not deallocating the memory that you allocated. Also, I don't believe that your allocate statements are right. They should just indicate how many cells in each dimension to allocate.

    I might be wrong, but I think the above statements should be something like this:
    Code (Text):
    allocate(Ez(101, 501), STAT =  status)
    and similar for your Hy array.
    Do a web search for "fortran allocate" for more information.


    This is what I've been saying. You can't do this!!!!
    Ez(i-1./2,n+1./2) = Ca*(Ez(i-1./2,n-1./2)) + Cb*(Hy(i,n)-Hy(i-1.,n))

    The indexes in an array have to be integer numbers. You can't access the element whose index is 3.5, for example.

    You have to change your code so that you store values at integer indexes in the arrays.
     
    Last edited: May 10, 2012
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Having problem to understand kind notation in fortran 90
Loading...