这是我写的计算碳和氢原子坐标之间距离的程序。但是发生了一些错误,我不知道如何修复它们。谢谢你们太多了......
program C_H_bdlength
implicit none
integer :: ia ! integer atoms number
integer :: na ! number of atoms
real*8 :: x , y , z ! x y z coordinates of all atoms
real*8 :: x1 , y1 , z1 ! x y z coordinates of C atoms
real*8 :: x2 , y2 , z2 ! x y z coordinates of H atoms
character(len=2) :: ele ! element name
real*8 :: dis ! distance between C and H
open(100,file='cnt.ini',action='read',status='old')
na = 0
do
read(100,*,end=101)
na = na + 1
end do
101 continue
open(200, file='cnt.ini',action='wtire',status='replace')
rewind(100)
write(200,*)
do ia = 1 , na
read(100,*) ele, x, y, z
if ele == 'C' then x1 = x, y1 = y, z1 = z
do ia = ia , na
read(100,*) ele, x, y, z
if ele == 'H' then x2 = x, y2 = y, z2 = z
dis = sqrt((x1 - x2)**2 + (y1 - y2)**2 + (z1 - z2)**2)
write(200,*) dis
end if
end do
end if
end do
end program C_H_bdlength
在档案C_H_bdlength.f90:26
if ele == 'C' then x1 = x, y1 = y, z1 = z
1
Error: Unclassifiable statement at (1)
在档案C_H_bdlength.f90:27
do ia = ia , na
1
在档案C_H_bdlength.f90:24
do ia = 1 , na
2
错误:变量' ia' at(1)无法在(2)开始的内部循环中重新定义 在文件C_H_bdlength.f90:29
中 if ele == 'H' then x2 = x, y2 = y, z2 = z
1
错误:(1)处的不可分类陈述 在文件C_H_bdlength.f90:33
中 end if
1
错误:在(1)处期待END DO语句 在文件C_H_bdlength.f90:35
中 end if
1
错误:期待(1)
处的END DO语句答案 0 :(得分:0)
你有一个嵌套循环,并使用相同的变量(ia
)来控制内循环和外循环。在内循环中使用具有不同名称的变量。
if <condition> then
构造在同一行的then
之后无法执行其他操作。
答案 1 :(得分:0)
除了@Peter的答案之外,另一个(不是句法)问题是你的DO循环没有考虑所有的C-H对。例如,如果“cnt.ini”包含以下数据
O 1.0 1.0 1.0
H 8.0 1.0 6.0 # ia=2
C 2.0 3.0 1.0 # ia=3
N 3.0 3.0 3.0
H 4.0 1.0 6.0 # ia=5
O 5.0 5.0 5.0
C 1.0 7.0 8.0 # ia=7
它只考虑ia = 3和5对。此外,由于您在输入文件(cnt.ini)的同时将输出数据写入输入文件(cnt.ini),因此可能会破坏数据传输。所以我建议先将输入数据读入数组,计算C-H距离,然后将结果写入不同的文件。例如,代码可能如下所示。
integer :: ia, ja, na
real*8 :: dist
real*8, allocatable :: pos(:,:)
character(len=2), allocatable :: ele(:)
open(100,file='cnt.ini',status='old')
!! First, determine the number of atoms in the file.
na = 0
do
read( 100, *, end=101 )
na = na + 1
end do
101 continue
!! Then, allocate necessary arrays for atomic positions and element names.
allocate( pos( 3, na ), ele( na ) )
!! Now read the actual data in the file.
rewind( 100 )
do ia = 1, na
read( 100, * ) ele( ia ), pos( 1:3, ia )
! Alternative way using implied DO-loop.
! read( 100, * ) ele( ia ), ( pos( k, ia ), k = 1, 3 )
enddo
close( 100 )
!! Now calculate C-H distances.
open(200, file='dist.dat')
!! Loop over unique atom pairs.
do ia = 1 , na - 1
do ja = ia + 1, na
if ( ( ele(ia) == 'C' .and. ele(ja) == 'H' ) .or. &
( ele(ia) == 'H' .and. ele(ja) == 'C' ) ) then
dist = norm2( pos( :, ia ) - pos( :, ja ) )
! dist = sqrt( sum( (pos(:,ia) - pos(:,ja))**2 ) ) !! if norm2() not available
write(200,*) ia, ja, dist
endif
enddo
enddo