Fortran subroutine returning wrong values

后端 未结 2 1054
Happy的楠姐
Happy的楠姐 2020-12-02 01:14

Hey I\'m working on a fortran program and have ran across an odd problem. When I try to output some values of an array directly before calling a specific subroutine I get th

相关标签:
2条回答
  • 2020-12-02 01:52

    I figured it out. VP was allocated as

    VP(0:DIM,0:NPTMAX) 
    

    in the main program and

    VP(0:DIM-1,0:NPTMAX) 
    

    in the subroutine! This caused the error.

    0 讨论(0)
  • 2020-12-02 02:03

    Manually, setting explicit-shape arrays a(0:N-1,0:M) is extremely error prone. I suggest using assumed-shape arrays a(:,:) to pass arguments. Also, when you pass arrays as arguments to subroutines, the upper and lower bounds of the actual argument are not maintained in the call unless the lower bound is 1. However, when passing pointers, the lower and upper bounds are maintained. For instance,

    program main
    
      use, intrinsic :: iso_fortran_env, only: &
           stdout => OUTPUT_UNIT, &
           compiler_version, &
           compiler_options
    
      ! Explicit typing only
      implicit none
    
      real               :: foo(10,10)
      real, target       :: bar(0:9,0:9)
      real, pointer      :: ptr(:,:) => null()
    
      call peek_assumed_shape(foo, 'peek_assumed_shape: foo(10,10)')
      call peek_assumed_shape(bar, 'peek_assumed_shape: bar(0:9,0:9)')
    
      ptr => bar
      call peek_pointer(ptr, 'peek_pointer: ptr => bar')
    
      ptr(42:,42:) => bar
      call peek_pointer(ptr, 'peek_pointer: ptr(42:,42:) => bar')
    
      nullify( ptr )
    
      write (stdout, '(/4a/)') &
           'This file was compiled using compiler version ', compiler_version(), &
           ' and compiler options ', compiler_options()
    
    contains
    
    
      subroutine peek_assumed_shape(array, description)
        ! Calling arguments
        real,              intent (in) :: array(:,:)
        character (len=*), intent (in) :: description
    
        write (stdout, '(/a)') description
        write (stdout, *) 'dim=1 ', lbound(array, dim=1), ubound(array,dim=1)
        write (stdout, *) 'dim=2 ', lbound(array, dim=2), ubound(array,dim=2)
    
    
      end subroutine peek_assumed_shape
    
      subroutine peek_pointer(array, description)
        ! Calling arguments
        real, pointer,     intent (in) :: array(:,:)
        character (len=*), intent (in) :: description
    
        if (associated(array)) then
           write (stdout, '(/a)')  description
           write (stdout, *) 'dim=1 ', lbound(array, dim=1), ubound(array,dim=1)
           write (stdout, *) 'dim=2 ', lbound(array, dim=2), ubound(array,dim=2)
        end if
    
      end subroutine peek_pointer
    
    end program main
    

    returns the following

    peek_assumed_shape: foo(10,10)
     dim=1            1          10
     dim=2            1          10
    
    peek_assumed_shape: bar(0:9,0:9)
     dim=1            1          10
     dim=2            1          10
    
    peek_pointer: ptr => bar
     dim=1            0           9
     dim=2            0           9
    
    peek_pointer: ptr(42:,42:) => bar
     dim=1           42          51
     dim=2           42          51
    
    This file was compiled using compiler version GCC version 5.4.0 20160609 and compiler options -mtune=generic -march=x86-64 -O3 -Wall -std=f2008ts
    
    0 讨论(0)
提交回复
热议问题