首页 文章

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

提问于
浏览
1

我目前正在开发一个大型的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/) ) . 例如,如果我使用 print pbin(10)%bpoints 查看 pbin 数组元素中的数据,那么我得到了我期望的结果 . 有没有人对这个问题有所了解?

1 回答

  • 1

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

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

相关问题