我试图为Lennard Jones潜在的Fortran代码编写并从文件posinp_38.xyz读取坐标,但我遇到了SIGSEGV, segmentation fault occurred
我怎么能找到问题所在。我是Fortran的新手,所以任何帮助都会有所帮助。
PROGRAM lennardjones_1
IMPLICIT NONE
REAL(8), allocatable :: rat(:,:) ! coordinates of atoms
REAL(8), allocatable :: fat(:,:) ! force between atoms
INTEGER :: nat ! number of atoms
!INTEGER :: iat, jat ! loop counter
REAL :: epot ! potential energy
REAL :: ftot ! total force on atoms
CHARACTER(len=20) :: filename ! Input data file name
CHARACTER(len=3) :: sat ! for reading the file
INTEGER :: status ! I/O status: 0 for success
! Get the name of the file containing the input data.
WRITE (*,1000)
1000 FORMAT (1X,'Enter the file name with the data to be sorted: ')
WRITE (*,*) 'The file name is: posinp_38.xyz or posinp_1000.xyz '
READ (*,'(A20)') filename
! Open input data file.
OPEN ( UNIT=21, FILE=filename, status='OLD', ACTION='READ', &
IOSTAT=status )
READ (21,*) nat
READ (21,*)
CALL force_energy(rat, nat)
write (*,*) epot, ftot
END PROGRAM lennardjones_1
SUBROUTINE force_energy(rat,nat)
IMPLICIT NONE
REAL(8), allocatable :: rat(:,:)
REAL(8), allocatable :: fat(:,:)
INTEGER, INTENT(IN) :: nat
INTEGER :: iat, jat ! local variables
REAL :: epot
REAL :: r , dx, dy, dz, d
REAL :: ftot
INTEGER :: status ! I/O status: 0 for success
CHARACTER(len=3) :: sat
allocate (rat(3,nat))
allocate (fat(3,nat))
! Was the OPEN successful?
fileopen: IF ( status == 0 ) THEN ! Open successful
DO iat = 1, nat
READ (21,*) sat, rat(1, iat), rat(2, iat), rat(3, iat)
END DO
CLOSE(21)
END if fileopen
DO iat = 1, nat
DO jat = iat+1, nat
dx = rat(1,jat)-rat(1,iat)
dy = rat(2,jat)-rat(2,iat)
dz = rat(3,jat)-rat(3,iat)
r = sqrt(dx**2 + dy**2 + dz**2)
d = 4*(-12/r**14 + 6/r**8)
fat(1,iat) = fat(1,iat) + d * dx
fat(2,iat) = fat(2,iat) + d * dy
fat(3,iat) = fat(3,iat) + d * dz
fat(1,jat) = fat(1,jat) - d * dx
fat(2,jat) = fat(1,jat) - d * dy
fat(3,jat) = fat(1,jat) - d * dz
ftot = ftot + (fat(1,iat)**2+fat(2,iat)**2+fat(3,iat)**2)+ &
(fat(1,jat)**2+fat(2,jat)**2+fat(3,jat)**2)
epot = epot+4*(1/r**12-1/r**6)
END DO
END DO
END SUBROUTINE force_energy
答案 0 :(得分:1)
我很惊讶你得到了编译的代码。您已经使用可分配参数定义了子例程,并且没有提供显式接口,符合标准的编译器应该发现并引发错误。事实上,我很惊讶我怀疑你没有编译发布的代码,而且错误源于一些完全不同的问题。
但是,如果那真的是你的代码,解决问题的简单方法就是移动行
END PROGRAM lennardjones_1
到源文件的末尾,并在它腾出的空间中插入行
CONTAINS
这些更改将使子程序成为程序内部的,编译器将负责定义显式接口。
在您完成这些更改后,将INTENT(OUT)
添加到子例程中rat
的声明中。