0

I'm a beginner to Fortran and trying to solve a problem to group some datasets which has the same distance to a point. I'm trying to create a 3D array range_data with different length (n,5,120) (n varies). I want to group 5 1D-arrays URS PROB ix iy and radius (has the same length, same index location) into variable range_data. I'm stuck with this error and don't know exactly what leads to this. I ran debugger and it said that Segmentation fault at if (int(radius(m,1))==k) sz=sz+1

The script is below:

function radius_group(radius,dims,ix,iy,URS,PROB) result (range_data)
use typDefMod
real, dimension(:), allocatable, intent(in) :: URS,PROB
real, dimension(:,:), allocatable, intent(in) :: radius
integer,dimension(:), allocatable,  intent(in) :: ix,iy
integer :: i,k,m,sz
integer, intent(in) :: dims
type(multi_array), allocatable:: range_data,temp
integer, dimension(:), allocatable :: loc

do k=1,int(maxval(radius(:,1)))
    sz=0
    do m=1,dims
        if (int(radius(m,1))==k) sz=sz+1
    end do
    allocate(loc(sz))
    i=1
    do m=1,dims
        if (int(radius(m,1))==k) then
            loc(i)=m
            i=i+1
        end if
    end do
    allocate(temp%range_num(k)%element(sz,5))
    temp%range_num(k)%element(:,1)=radius(loc(:),1)
    temp%range_num(k)%element(:,2)=ix(loc(:))
    temp%range_num(k)%element(:,3)=iy(loc(:))
    temp%range_num(k)%element(:,4)=URS(loc(:))
    temp%range_num(k)%element(:,5)=PROB(loc(:))
    deallocate(loc)
end do
range_data=temp
end function radius_group
halfer
  • 19,824
  • 17
  • 99
  • 186
PIZGY
  • 1
  • 1
    What compiler are you using? Most have debugging options, e.g., gfortran has `-fcheck=all`, which will report an array index that is out-of-range. – evets Apr 29 '20 at 15:49

1 Answers1

0

yesterday I solved this problem, it is due to the missing allocation of the parent array. But now I fixed it! Thanks all! the correct code is:

function radius_group(radius,dims,ix,iy,URS,PROB) result (range_data)
    use typDefMod
    real, dimension(:), allocatable, intent(in) :: URS,PROB
    real, dimension(:,:), allocatable, intent(in) :: radius
    integer,dimension(:), allocatable,  intent(in) :: ix,iy
    integer :: i,k,m,sz
    integer, intent(in) :: dims
    type(multi_array):: range_data,temp
    type(array_R), allocatable:: range_num(:)
    real, allocatable:: element(:,:)
    integer, dimension(:), allocatable :: loc

    allocate(temp%range_num(int(maxval(radius(:,1)))))
    do k=1,int(maxval(radius(:,1)))
        sz=0
        do m=1,dims
            if (int(radius(m,1))==k) sz=sz+1
        end do
        allocate(loc(sz))
        i=1
        do m=1,dims
            if (int(radius(m,1))==k) then
                loc(i)=m
                i=i+1
            end if
        end do
        allocate(element(sz,5))
        allocate(temp%range_num(k)%element(sz,5))
        temp%range_num(k)%element(:,1)=radius(loc(:),1)
        temp%range_num(k)%element(:,2)=ix(loc(:))
        temp%range_num(k)%element(:,3)=iy(loc(:))
        temp%range_num(k)%element(:,4)=URS(loc(:))
        temp%range_num(k)%element(:,5)=PROB(loc(:))
        deallocate(loc)
        deallocate(element)
    end do
    range_data=temp
end function radius_group
PIZGY
  • 1