Fortran:包含派生类型数组的派生类型的Allocatable数组

时间:2014-08-24 21:05:59

标签: gdb fortran derived-types allocatable-array

我目前正在开发一个大型的Fortran程序,我有一个离散的数字网格,其中包含一系列我在网格范围内跟踪的粒子。为此,我定义了以下三种派生类型:

type :: particle
    real(pr), dimension(3) :: r = 0.0_pr ! position
    real(pr), dimension(3) :: p = 0.0_pr ! momentum
end type particle

type :: rcell ! position cell
    integer, dimension(6) :: bpoints = 0 ! cell grid points
    integer :: np = 0 ! number of particles in cell
    type(particle), dimension(50) :: parts ! particles in cell
end type rcell

type :: pcell ! momentum cell
    integer, dimension(6) :: bpoints = 0 ! cell grid points
    integer :: np = 0 ! number of particles in cell
end type pcell

...

type(rcell), dimension(:), allocatable :: rbin ! position space bins
type(pcell), dimension(:), allocatable :: pbin ! momentum space bins

...

allocate(rbin(100))
allocate(pbin(100))

首先,这是派生类型的可接受使用(即具有包含派生类型数组的派生类型的可分配数组)?使用gfortran 4.8.3编译代码很好。

然而,在尝试使用Fedora下的gdb 7.7.1调试代码时遇到了一些奇怪的问题。在尝试查看rbin数组元素中的数据时(例如,使用print rbin(10)%bpoints)gdb始终打印出(0, 0, 0, 0, 0, 0),即使我已将数据分配给bpoints(例如rbin(10)%bpoints = (/1,2,1,2,1,2/))。例如,如果我使用pbin查看print pbin(10)%bpoints数组元素中的数据,那么我得到的就是我期望的结果。有没有人对这个问题有所了解?

1 个答案:

答案 0 :(得分:1)

我一直在我的代码中使用这些结构。使用gfortran在类似Linux的操作系统上编译或运行没问题。英特尔编译器在5年前遇到过这类代码的问题,但我最近已经离开了那个编译器,所以我不确定他们现在是否已经赶上了新的Fortran标准。我使用MPI,所以我很少使用gdb,并且无法解释为什么它会抛出错误。

无论如何,我同意马克;现代Fortran(使用gfortran编译)可以很好地处理这种类型的结构。