fortranvmd

how to read vmd file (.xyz format) of mixed variable for molecular dynamics in Fortran


I want to read .xyz file which looks like....

3                                !no. of particles
1.0000                           ! time step
a   2.345   2.458  0.564         ! ID x y z for 1st particle
a   5.455   2.486  5.456         ! ID x y z for 2nd particle
a   6.545   4.566  2.665         ! ID x y z for 3rd particle
3                                !no.of particles (same as before)
2.000                            ! next time step
a   4.345   3.458  4.564         ! ID x y z for 1st particle
a   4.455   3.486  4.456         ! ID x y z for 2nd particle
a   8.545   3.566  4.665         ! ID x y z for 3rd particle
...... continue..... for 1000 time step

I want to calculate distance of each particle from others at each time step. How to read this file?

program size
implicit none  
integer i,j,k  
integer,parameter :: n=400,t=714  
integer dn(n),n1  
real*8 px(n,t),py(n,t),dt,pz  
character(75) nam  
open (unit=50,file='vmd.xyz',status='old',action='read')  

do k=1,t   
do i=1,n  
read(50,*) n1   
read(50,*) dt  
read(50,*) nam,px(i,k),py(i,k),pz  
end do  
end do  


close(50)
end program

Solution

  • Now that you posted a code, it is much easier to help you.

    1. You always read 400 coordinates, whatever the number of particles.

    2. You repeat 400 times the reading of the number of particles and of the timestep. Only the line read(50,*) nam,px(i,k),py(i,k),pz should be in the loop. Try

      program size
      implicit none  
      integer i,j,k  
      integer,parameter :: n=400,t=2
      integer dn(n),n1  
      real*8 px(n,t),py(n,t),dt,pz  
      character(75) nam  
      open (unit=50,file='vmd.xyz',status='old',action='read')  
      
      do k=1,t   
      read(50,*) n1   
      read(50,*) dt  
      do i=1,n1
      read(50,*) nam,px(i,k),py(i,k),pz  
      end do  
      end do  
      
      close(50)
      end program
      
    3. This method for analyzing MD data (read all in memory then proceed) can be very memory intensive and useful only for short/small simulations.

    You can try existing libraries:

    1. Chemfiles has a Fortran interface and will ease the reading of coordinates: http://chemfiles.org/chemfiles.f03/0.7.4/ (Fortran binding)
    2. Consider other languages. MDAnalysis is written in Python, supports xyz files and provides a lot of the basic features for trajectory analysis.