如何在Fortran代码中将2D数组转换为1D数组?



如何将 r(i,j( 转换为一维数组,以便我可以轻松地对数字进行排序?

program sort
implicit none
character CN*8,O*7
integer j,iconf,nconf
integer i,nbins,t
integer n,nmax,ind,num,b
parameter (n=216)
double precision xbox,rq
parameter (nmax=3091,nconf=1)
double precision atom(nmax),id(nmax),ox(nmax),oy(nmax),oz(nmax)
double precision xij,yij,zij,rij
double precision r(n,n),A(n)
open(unit=10,status='unknown',file='1000.gro')
do iconf= 1,nconf
write(*,*)iconf
read(10,*)
read(10,*)
do i=1,n
read(10,'(A8,A7,1i5,3f8.3)')CN,O,num,ox(i),oy(i),oz(i)
enddo
read(10,*)xbox        ! read the xbox for PBC
open(unit=3,file='dist.txt')
do i=1,n
do j=1,n
if(i .ne. j) then
xij=ox(i)-ox(j)
yij=oy(i)-oy(j)
zij=oz(i)-oz(j)
r(i,j)=dsqrt(xij**2 + yij**2 + zij**2)
write(3,'(i3,2x,i3,4x,f17.15)') i,j, r(i,j)
endif 
enddo
enddo
enddo
END

我必须计算距离并将其保存在数组中作为r (i,j).我想将r(i,j)转换为一维数组,以便我可以轻松地对r(i,j)进行排序。

前面的两个答案已经研究了如何将 rank-2 数组"转换"为rank-1 数组的字面问题。 他们使用不同的方法:

  • 将值复制到新的 rank-1 数组中;
  • 在最后的子例程中,有一个等级 1 虚拟参数与一个等级 2 实际参数相关联。

我们可以扩展这两种方法。

reshape最简单的形式返回所需的秩 1 数组。 作为替代方案,我们可以说数组构造函数(形式为[x](也这样做:

real r1d(6), r2d(3,2)
r2d=5.
r1d = [r2d]

当实际参数是数组元素时,数组虚拟参数和数组中包括和跟随实际参数的那些元素处于序列关联中。

通过序列关联,虚拟数组将假定为大小或显式形状。 在这种情况下,我们对整个数组感兴趣,我们可以只传递整个数组:

real r2d(3,2)
call sub(r2d)

哪里

subroutine sub(r1d)
real r1d(*) ! or r1d(6), etc.
end subroutine

这里重要的是,对于显式形状和假定大小的虚拟参数,秩不需要与实际参数的秩匹配。

如前所述,第一组方法涉及创建新阵列。 序列关联的使用不会。 第三种方法也可用:让一个指向排名 2 目标的 rank-1 指针。 当然,创建副本也意味着任何更改都不会反映在原始 rank-2 数组中。 序列关联和指针将看到传递的更改。

有关其中任何一项的更多详细信息,请参阅其他问题和答案。

对于排序,将秩 2 数组视为秩 1 数组是否有意义是一个不同的考虑因素。

您可以将 r(1,1( 传递给将参数声明为一维数组的子例程。这在 Fortran 中是合法的(有一些不适用于您的代码的限制(,并使用称为"序列关联"的功能。

这似乎是为reshape函数量身定制的: https://gcc.gnu.org/onlinedocs/gfortran/RESHAPE.html 在这个小例子中,首先使用 reshape 来制作二维数组 A,然后再次调用 reshape 来制作一维数组 C。

Program reshape_demo
use, intrinsic :: iso_c_binding
implicit none
real(kind=c_float),allocatable :: A(:,:),C(:)
integer(kind=c_int) :: krow
allocate(A(3,3))
A=reshape((/1,2,3,4,5,6,7,8,9/),(/3,3/))
do krow=1,3
write(*,fmt="(1p3e10.3)")A(krow,:)
end do
C=reshape(A,(/9/))
write(*,fmt="(9(1x,f4.1))")C
End Program reshape_demo

通过这样做,您将丢失有关距离对应的对的所有信息,但如果只是您想要的距离,那么我不会得到所有令人费解的答案。为什么不做:

[....]
implicit none
real(8), dimension(:), allocatable:: dist_arr
allocate(dist_arr(n**2))
k=0
do i=1,n
do j=1,n
k=k+1
dist_arr(k)=r(i,j)
enddo
enddo
[....]

如今,FORTRAN用户并不多,因此问题并不常见。接受的答案很好,可以形成一个简单的函数作为解决方案 1。但是还有另一种选择可以在不复制源数组的情况下使用 C_F_POINTER(可以通过大型数组获得更高的效率(。

解决方案 1:

function downsize(A)result(C)
real::A(:,:)
real,allocatable::C(:)
C=reshape(A,[size(A)])
end function downsize

使用该函数

real,allocatable::C(:); C=downsize(A);

ISO 绑定中的解决方案 2:
C_F_POINTER 可用于像 C 中一样传输内存地址。

USE, INTRINSIC :: ISO_C_BINDING
!EDIT...
integer::i,i1d(9)=[(i,i=1,9)]; !reshape((real([(i,i=1,9)])),[9])
integer,dimension(:,:),pointer::p2d
! Here has to be "Deferred-shape" but it only defined a pointer to an array, not an array of pointers as is always misleading...
!EDIT...
real::r2d(3,3)=reshape([(i*1.0,i=1,9)],[3,3])
real, pointer ::pr1d(:)
!Let's Grow the size from I1D to I2D by the pointer pI2D...
call c_f_pointer(c_loc(i1d),pi2d,[3,3])
!The pointer pi2d is a 2d array with the contents of i1d. Note you cannot deallocate the pointer pi2d - any deallocation has to be of pointee i1d.
!Let's Down the size from R2D to R1D by the pointer pR1D ...
call c_f_pointer(c_loc(r2d), pr1d,[size(r2d)])

最新更新