传递Fortran数组形状的功能变化

时间:2017-04-10 16:44:59

标签: arrays fortran

我在一个程序块中声明并填充了一个数组,该程序块被传递给模块内的一个函数。但是,程序块中阵列的形状与功能块中的形状不同,这在实践中会导致错误。以下是一些用于说明的简化代码:

\K

在modQuadrature中:

[0-9]+

当然我错过了一些东西。我不知道为什么阵列的形状会改变。我知道Fortran通过引用传递,所以程序块和函数正在查看相同的内存块。任何有关形状变化原因的见解都值得赞赏!感谢。

modPoint2D:

program prgQuad
    use modPoint2D, only: Point2D
    use modQuadrature, only : QuadIntegrateTri2D
    implicit none

    ! the array in question
    type( Point2D ), dimension( : ) :: pts( 1: 3 )
    real :: res

        ! populate the points
        call pts( 1 ) % set( 0.0, 0.0 )
        call pts( 2 ) % set( 1.0, 0.0 )
        call pts( 3 ) % set( 1.0, 1.0 )

        write( *, * ) shape( pts ) ! prints '3'

        ! use the points
        res = QuadIntegrateTri2D( pts )

end program prgQuad

1 个答案:

答案 0 :(得分:0)

pts的宣言确实非常奇特。下面的代码按照预期编译并运行,并为点阵列提供正确的形状。

module modPoint2D

    type :: Point2D
        real :: x,y
    contains
        procedure, pass :: set
    end type
    contains

    subroutine set(pnt, px, py)
    class(Point2D), intent(out) :: pnt
    real, intent(in) :: px, py

        pnt%x = px
        pnt%y = py

    end subroutine
end module


module modQuadrature
use modPoint2D

    contains
    function QuadIntegrateTri2D( pts ) result( integral )
        class( Point2D ), dimension( : ), intent( in ) :: pts
        real :: integral
        ! other vars go here

            write( *, * ) shape( pts ) ! prints '3'

            ! actual function code goes here 
            integral = 0

    end function 
end module


program prgQuad
    use modPoint2D, only: Point2D
    use modQuadrature, only : QuadIntegrateTri2D
    implicit none

    ! the array in question
    type( Point2D ), dimension(:) :: pts(1:3)
    real :: res

        call pts( 1 ) % set( 0.0, 0.0 )
        call pts( 2 ) % set( 1.0, 0.0 )
        call pts( 3 ) % set( 1.0, 1.0 )

        write( *, * ) shape( pts ) ! prints '3'

        ! use the points
        res = QuadIntegrateTri2D( pts )

end program