我正在嘗試編寫Lennard Jones潛在的Fortran代碼,並從文件posinp_38.xyz讀取座標,但我遇到了SIGSEGV, segmentation fault occurred
我怎麼能找到問題出在哪裏。我是Fortran的新成員,所以任何幫助都很有幫助。從Fortran文件中讀取數字,SIGSEGV錯誤
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
你知道使用調試器嗎?如果這是在Linux或Unix上,用'-g'編譯並使用'gdb'運行。 – wallyk 2014-10-11 17:39:07
這是在Linux中,關於調試器從來沒有。我谷歌它。 – Abolfazl 2014-10-11 17:41:42
請務必使用標籤[tag:fortran],並在必要時添加版本以區分您的問題是否具體。例如,您不能使用Fortran 2008,但只能使用Fortran 90. – 2015-12-17 11:54:45