978142355 发表于 2016-1-14 10:39:58

通过3点坐标计算角度

想在输入文件中,输入行数,通过行数中的坐标,然后自动获得角度,代码已经附上,求助大家实现?    program angle
    implicit none
    real::x(3),y(3),z(3),angle,A,B,C,D
    integer::i,j,k

    write(*,*) 'Input the name of xyz:(e.g. a)'
    read(*,*) name
    name1=trim(name)//'.xyz'
    open(12,file=name1)
    open(13,file='result.txt')
    do i=1,3
      write(*,"(I1)") 'Input the atom number',i ,':(e.g.1)'
      read(*,*) j
      write(*,*)
      if (j==1) then
            read(12,"(11x,3(f12.6)") xi,yi,zi
      else
            do k=1,j-1
                write(*,*)
            end do
      end if
      read(12,"(11x,3(f12.6))") xi,yi,zi
      rewind(12)
    end do

    A=(x1-x2)+(y1-y2)+(z1-z2) //!first vector
    B=(x3-x2)+(y3-y2)+(z3-z2) //!second vector
    C=dsqrt((x1-x2)**2+(y1-y2)**2+(z1-z2)**2) //!quantities of first vector
    D=dsqrt((x3-x2)**2+(y3-y2)**2+(z3-z2)**2) //!quantities of second vector
    angle=acos(A*B/C/D)
    write(13,"(a,f12.4)") "The angle is:",angle/3.141592653589793238462d0*180
    end program



vvt 发表于 2016-1-14 11:45:09

Program www_fcode_cn
Implicit None
Real(kind=kind(0.0d0))::x(3),y(3),z(3),angle,A,B,C,D
Integer::i,k
Character(len=30) :: cstr
write(*,*) 'Input the name of xyz:(e.g. a)'
read(*,*) cstr
cstr=trim(cstr)//'.xyz'
open(12,file=cstr)
read(12,*)
open(13,file='result.txt')
do i=1,3
    read(12,*) k , cstr , x(i),y(i),z(i)   
end do
A=(x(1)-x(2))+(y(1)-y(2))+(z(1)-z(2)) !//!first vector
B=(x(3)-x(2))+(y(3)-y(2))+(z(3)-z(2)) !//!second vector
C=sqrt((x(1)-x(2))**2+(y(1)-y(2))**2+(z(1)-z(2))**2) !//!quantities of first vector
D=sqrt((x(3)-x(2))**2+(y(3)-y(2))**2+(z(3)-z(2))**2) !//!quantities of second vector
angle=acos(A*B/C/D)
write(13,"(a,f12.4)") "The angle is:",angle/3.141592653589793238462d0*180
end program www_fcode_cn

978142355 发表于 2016-1-14 13:44:54

vvt 发表于 2016-1-14 11:45
Program www_fcode_cn
Implicit None
Real(kind=kind(0.0d0))::x(3),y(3),z ...

非常感谢你给的代码,不过这并不是我所需要的,因为我打算要的是手动输入任意3行(有坐标的),然后给出角度,现在角度部分是没什么问题,但是主要是前面任意给出3行的那里不太清楚该如何去写。

vvt 发表于 2016-1-14 14:00:09

Program www_fcode_cn
Implicit None
Real(kind=kind(0.0d0))::x(3),y(3),z(3),angle,A,B,C,D
Integer::i,j,k
Character(len=30) :: cstr
write(*,*) 'Input the name of xyz:(e.g. a)'
read(*,*) cstr
cstr=trim(cstr)//'.xyz'
open(12,file=cstr)
read(12,*)
open(13,file='result.txt')
do i=1,3
    write(*,"(a,i1,a)") 'Input the atom number',i ,':(e.g.1)'
    read(*,*) j
    Do k = 1 , j-1
      read(12,*)
    End Do
    read(12,*) k , cstr , x(i),y(i),z(i)   
    rewind(12)
    read(12,*)
end do
A=(x(1)-x(2))+(y(1)-y(2))+(z(1)-z(2)) !//!first vector
B=(x(3)-x(2))+(y(3)-y(2))+(z(3)-z(2)) !//!second vector
C=sqrt((x(1)-x(2))**2+(y(1)-y(2))**2+(z(1)-z(2))**2) !//!quantities of first vector
D=sqrt((x(3)-x(2))**2+(y(3)-y(2))**2+(z(3)-z(2))**2) !//!quantities of second vector
angle=acos(A*B/C/D)
write(13,"(a,f12.4)") "The angle is:",angle/3.141592653589793238462d0*180
end program www_fcode_cn

978142355 发表于 2016-1-14 14:15:17

vvt 发表于 2016-1-14 14:00
Program www_fcode_cn
Implicit None
Real(kind=kind(0.0d0))::x(3),y(3),z ...

非常感谢你,完美的解决了我的问题。
页: [1]
查看完整版本: 通过3点坐标计算角度