将 .xyz 文件读入数组
Reading .xyz file into array(s)
我目前创建了一个代码,可以从输入目录打开文件,然后开始将该数据排序到两个不同的数组中,一个整数和一个字符。输入文件如下所示:
1817
Plot50.0.xyz
C 0.70900 0.00000 0.00000
C 0.00000 1.22802 0.00000
C 2.12700 0.00000 0.00000
C 2.83600 1.22802 0.00000
C 4.96300 0.00000 0.00000
C 4.25400 1.22802 0.00000
C 6.38100 0.00000 0.00000
C 7.09000 1.22802 0.00000
C 9.21700 0.00000 0.00000
C 8.50800
datafile
现在我不确定我是否在打开文件(格式等)时没有正确分配所有内容,但这是代码[工作]:
program test01
character(len=40) :: filename !Storing the file name
character(len=20) :: nam !Collecting the file name within the file
integer(8) :: N !Number of coordinates within the file
real(8), allocatable :: coa(:,:) !Array containing xyz coordinates
character(len=6), allocatable :: atom(:,:) !Array containing the atomic make up
integer(8) :: i,j,k !Do loop parameters
filename = ''
write (6,*) 'Enter file name'
read (5,*) filename
open (10, file=filename, status='OLD')
write (6,*) 'Sucessfully opened file:', filename
read(10,*) N
allocate (coa(N,4))
allocate (atom(N,1))
read (10,*) nam
print*, nam
i = 0
do while (i < N)
read(10,*) atom(i,1), coa(i,2:4)
i = i+1
end do
print*, atom(0,1), atom(1,1), atom(4,1), atom(1818,1) !checking
print*, coa(0,2), coa(1500,3) !checking
close (10)
end program test01
所以我的主要问题在于,是否有更好的方法来创建数组(需要两个,因为我认为字符和真实不能混合,coa
数组稍后用于计算on) 并专门从文件中提取某些数据(更重要的是跳过文件的第 2 行,而不是将其插入到字符中以将其删除,因为它在我尝试创建数组时导致了所有问题)。
你写有没有更好的方法来创建数组。让我们从正确的方法开始,这不是...
i = 0
do while (i < N)
read(10,*) atom(i,1), coa(i,2:4)
i = i+1
end do
这看起来像是用 Fortran 编写的 C 循环 (for (i=0; i<N; i++)
),它错误地在第 0
行开始对数组进行索引。由于代码没有努力从 0
开始数组索引,并且 Fortran 默认从 1
开始索引,因此 read
的第一次执行会将数据读入范围之外的内存位置数组。
一个正确且更 Fortranic 的方法是
do i = 1, N
read(10,*) atom(i,1), coa(i,2:4)
end do
我看到您在别处打印了数组第 0
行的值。你 'lucky' 那些 outside-the-bounds 访问不会转到程序地址 space 之外的地址并导致分段错误。
程序坏了,只是坏了一点,还没有给你带来任何痛苦。
我目前创建了一个代码,可以从输入目录打开文件,然后开始将该数据排序到两个不同的数组中,一个整数和一个字符。输入文件如下所示:
1817
Plot50.0.xyz
C 0.70900 0.00000 0.00000
C 0.00000 1.22802 0.00000
C 2.12700 0.00000 0.00000
C 2.83600 1.22802 0.00000
C 4.96300 0.00000 0.00000
C 4.25400 1.22802 0.00000
C 6.38100 0.00000 0.00000
C 7.09000 1.22802 0.00000
C 9.21700 0.00000 0.00000
C 8.50800
datafile
现在我不确定我是否在打开文件(格式等)时没有正确分配所有内容,但这是代码[工作]:
program test01
character(len=40) :: filename !Storing the file name
character(len=20) :: nam !Collecting the file name within the file
integer(8) :: N !Number of coordinates within the file
real(8), allocatable :: coa(:,:) !Array containing xyz coordinates
character(len=6), allocatable :: atom(:,:) !Array containing the atomic make up
integer(8) :: i,j,k !Do loop parameters
filename = ''
write (6,*) 'Enter file name'
read (5,*) filename
open (10, file=filename, status='OLD')
write (6,*) 'Sucessfully opened file:', filename
read(10,*) N
allocate (coa(N,4))
allocate (atom(N,1))
read (10,*) nam
print*, nam
i = 0
do while (i < N)
read(10,*) atom(i,1), coa(i,2:4)
i = i+1
end do
print*, atom(0,1), atom(1,1), atom(4,1), atom(1818,1) !checking
print*, coa(0,2), coa(1500,3) !checking
close (10)
end program test01
所以我的主要问题在于,是否有更好的方法来创建数组(需要两个,因为我认为字符和真实不能混合,coa
数组稍后用于计算on) 并专门从文件中提取某些数据(更重要的是跳过文件的第 2 行,而不是将其插入到字符中以将其删除,因为它在我尝试创建数组时导致了所有问题)。
你写有没有更好的方法来创建数组。让我们从正确的方法开始,这不是...
i = 0
do while (i < N)
read(10,*) atom(i,1), coa(i,2:4)
i = i+1
end do
这看起来像是用 Fortran 编写的 C 循环 (for (i=0; i<N; i++)
),它错误地在第 0
行开始对数组进行索引。由于代码没有努力从 0
开始数组索引,并且 Fortran 默认从 1
开始索引,因此 read
的第一次执行会将数据读入范围之外的内存位置数组。
一个正确且更 Fortranic 的方法是
do i = 1, N
read(10,*) atom(i,1), coa(i,2:4)
end do
我看到您在别处打印了数组第 0
行的值。你 'lucky' 那些 outside-the-bounds 访问不会转到程序地址 space 之外的地址并导致分段错误。
程序坏了,只是坏了一点,还没有给你带来任何痛苦。