前几天我需要一些帮助来解决一个问题,并在这里充实了一个非常简化的示例:fortran "array of arrays" and "pack" issues。这个新颖的例子最终成功了,但它并没有解决我更大的程序的问题。为了进一步了解我遇到的错误,我扩展了新程序以更准确地模仿我的大型程序的结构。事实证明,这确实重现了我的错误。以下是我使用的代码片段:
测试.f90
program main
use locs, only: alloc_locs,all_locs
implicit none
INCLUDE 'test.h'
integer :: numpars, numlocs, n
type (par), allocatable, dimension(:) :: all_pars
CALL alloc_locs
!read numpars, numlocs from file etc
numpars = 10
allocate(all_pars(numpars))
!initialize all_pars
all_pars(1:4)%location = 1
all_pars(5:6)%location = 2
all_pars(7:8)%location = 3
all_pars(9:10)%location = 4
!get particles in each location
do n = 1,numlocs
all_locs(n)%pars = pack(all_pars, (all_pars(:)%location .eq. n))
enddo
write(*,*) all_locs(2)%pars(1)%location
end program
测试.h
type par
!data
integer :: location
end type par
type locations
! data
type (par), allocatable, dimension(:) :: pars
end type locations
locs.f90
MODULE LOCS
IMPLICIT NONE
PUBLIC
SAVE
include 'test.h'
type (locations), allocatable, dimension(:) :: all_locs
CONTAINS
SUBROUTINE alloc_locs()
integer :: numlocs
numlocs = 4
allocate(all_locs(numlocs))
END SUBROUTINE alloc_locs
END MODULE LOCS
生成文件
FC = ifort
OBJS = locs.o
FFLAGS = -vec-report0 -O2 -fp-model precise -standard-semantics
test : $(OBJS)
@echo " Compiling"
@$(FC) $(FFLAGS) -o test.exe test.f90 $(OBJS)
@\rm *.o *.mod
@echo " "
@echo " Compilation Successfully Completed"
@echo " "
%.o: %.f90
@echo " Compiling $<"
@$(FC) $(FFLAGS) -c $<
clean:
\rm *.o *.mod test.exe
制作结果:
Compiling locs.f90
Compiling
test.f90(27): error #6197: An assignment of different structure types is invalid.
all_locs(n)%pars = pack(all_pars, (all_pars(:)%location .eq. n))
---------------------^
compilation aborted for test.f90 (code 1)
对我来说,这段代码和上一个问题中的代码在功能上是相同的,但显然在翻译中丢失了与模块结构有关的东西,或者两次引用 test.h,并且结构不知何故不等效。我在网上找到的关于这个错误的信息很少,据我所知,没有任何与我的问题相关的信息。有人可以解释导致问题的原因,以及我可能如何进行排序吗?如果问题是后者,如何在模块之间传播用户定义的类型?
提前致谢。