Fortran: Choosing the rank of an allocatable array

前端 未结 2 2023
萌比男神i
萌比男神i 2020-12-20 03:58

I am trying to write a program where I want the allocatable array A to be of either rank 1, 2, or 3, depending on my input at run-time. I want to do this since

2条回答
  •  梦毁少年i
    2020-12-20 04:44

    Declare the array to be rank three. If a lower rank array is required, allocate the relevant trailing dimensions to be of size one.

    real, allocatable :: array(:,:,:)
    ...
    select case (desired_rank)
    case (1) ; allocate(array(n,1,1))
    case (2) ; allocate(array(n,n,1))
    case (3) ; allocate(array(n,n,n))
    case default ; error stop 'bad desired rank'
    end select
    

    You can then use an array section to get a contiguous slice of array that is consistent with your desired rank. Alternatively, write the relevant procedures that operate on array to take a rank three argument, and make them aware of the meaning of a size one extent for the higher dimensions.

提交回复
热议问题