二维数组出现分段错误

3
我有以下的代码。当我取消temperature(i,j) = anode_temperature的注释时,会出现SegFault错误。
forall(i=0:Cells(1), j=0:Cells(2), j >= nint(upperBoundary(i * delta(1)) / delta(2)))
    phi(i,j) = Anode_Voltage
!   temperature(i,j) = anode_temperature
end forall

然而,使用以下代码我可以访问每个元素。
do i = 0,Cells(1)
    do j = 0,Cells(2)
        temperature(i,j) = anode_temperature
        write(*,*) i,j,temperature(i,j)
    enddo
enddo

这里有一个自包含程序。因为有人提到他们使用ifort,我使用ifort 14。

program Main
implicit none

REAL, PARAMETER :: Pi = 3.14159265359
integer,parameter :: ND = 2

real, parameter :: RL = 1.0E-6  !Boundary radius
real, parameter :: HN = 5.0E-6  !Length of included needle
real, parameter :: HL = 2.0E-6  !Tip height

real, parameter :: LTT = 3E-6   !Tip-to-tip distance

real, parameter :: RU = 0.5E-6  !Boundary radius
real, parameter :: HU = 1.5E-6  !feature height

real, parameter :: zLen = HN + HL + LTT + HU

real,parameter,dimension(ND) :: Length = (/ 20E-6, zLen /)          !computational domain size (r,z)

! Grid Spacing
real, parameter, dimension(ND) :: delta = (/ 1.0E-8 , 1.0E-8 /)
integer, PARAMETER, dimension(ND) :: cells = Length / delta

real,parameter :: VApplied = -150

REAL, PARAMETER :: cathode_temperature = 1473.14        !Cathode Temperature [K]
REAL, parameter :: anode_temperature = 973.14           !Anode Temperature [K]
real :: Cathode_Voltage, Anode_Voltage

REAL, dimension(0:cells(1), 0:cells(2)) :: phi
REAL, dimension(0:cells(1), 0:cells(2)) :: temperature              !grid-wise gas temperature

integer:: i,j

phi = 0.0
temperature = 0.0

    Cathode_Voltage = VApplied
    Anode_Voltage = 0

forall(i=0:Cells(1), j=0:Cells(2), j <= nint(lowerBoundary(i * delta(1)) / delta(2)))
    phi(i,j) = Cathode_Voltage
    temperature(i,j) = cathode_temperature
end forall

forall(i=0:Cells(1), j=0:Cells(2), j >= nint(upperBoundary(i * delta(1)) / delta(2)))
    phi(i,j) = Anode_Voltage
    temperature(i,j) = anode_temperature
end forall

!forall(i=0:Cells(1), j=0:Cells(2), (j > nint(lowerBoundary(i * delta(1)) / delta(2))) .and. (j < nint(upperBoundary(i * delta(1)) / delta(2))))
!   phi(i,j) = Cathode_Voltage + (Anode_Voltage - Cathode_Voltage) * ((delta(2) * j - lowerBoundary(i * delta(1)) ) / ( upperBoundary(i * delta(1)) - lowerBoundary(i * delta(1)) ))
!   temperature(i,j) = cathode_temperature + (anode_temperature - cathode_temperature) * ((delta(2) * j - lowerBoundary(i * delta(1)) ) / ( upperBoundary(i * delta(1)) - lowerBoundary(i * delta(1)) ))
!end forall

contains
    pure function LowerRegion(r) result(k)

    implicit none
        real,intent(in) :: r
        integer :: k

        if (r < 0) then
            k = 0
        elseif (r < RL) then
            k = 1
        elseif (r >= RL) then
            k = 2
        elseif (r > R) then
            k = 3
        endif
    end function LowerRegion

    pure function UpperRegion(r) result(k)

    implicit none
        real,intent(in) :: r
        integer :: k

        if (r < 0) then
            k = 0
        elseif (r < RU) then
            k = 1
        elseif (r >= RU) then
            k = 2
        elseif (r > R) then
            k = 3
        endif
    end function UpperRegion


!!!!!!!!!!!! Define geometry for regions radially !!!!!!!!!!!!!!!!!!
    pure function lowerBoundary(r) result(z)

    implicit none
        real,intent(in) :: r
        real :: z

        if (LowerRegion(r) == 1) then
            z = HN + HL * (1.0 - (r / RL))
        elseif (LowerRegion(r) == 2) then
            z = 0.0
        endif
    !end function lowerBoundary_Scalar
    end function lowerBoundary

    pure function upperBoundary(r) result(z)

    implicit none
        real,intent(in) :: r
        real :: z

        if (UpperRegion(r) == 1) then
            z = zLen - HU * (1.0 - (r / RU))
        elseif (UpperRegion(r) == 2) then
            z = zLen
        end if
    end function upperBoundary

end program Main

差一?尝试 i=0:Cells(1)-1, j=0:Cells(2)-1 - erip
我从0开始声明了一个REAL, dimension(0:cells(1), 0:cells(2)) :: temperature - user1543042
3
最好让我们看到一个完整的示例,以便能够重现错误。我特别关注CellsdeltaupperBoundary的值和声明。您尝试过启用边界检查进行编译吗? - Ross
我不知道 : 是否包含在内。 - erip
1
我发现如果使用命令ifort main.f90 -o a -mkl -warn noalign -autodouble -check bounds -check pointers -check uninit -heap-arrays编译,它不会出现段错误,但如果使用ifort main.f90 -o a -g -CB -CU -debug -traceback -check bounds -mkl -warn noalign -autodouble -check bounds -check pointers -check uninit -heap-arrays编译,则会出现段错误。 - user1543042
显示剩余5条评论
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接