-1

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
Pierre de Buyl
  • 7,074
  • 2
  • 16
  • 22
  • Please take the Welcom [Tour] and read [ask]. Also use tag [tag:fortran] for all Fortran questions. Your question is absolutely unreadable. Please learn the formatting options to make it readable. You should show us some code. Have you tried to solve this problem yourself? This is not a code writing service not a homework writing service. We don't know into which datastructures it needs to be read. – Vladimir F Героям слава Oct 26 '17 at 14:33
  • you have to tell us what you've tried. I've done simulations in LIGGGHTS, and my first suggestion is to use a more modern language for post processing. SO is not a coding service, but we are willing to help if you show you've put in effort – Mohammad Athar Oct 26 '17 at 14:44
  • Thank you for posting the code. Please always use the correct formattingx it is the `{}` button. Don't forget to always tell us what kind of problem you have with the code. Any error message? Which message? Wrong result? Which result? What would be the correct result? – Vladimir F Героям слава Oct 27 '17 at 05:25

1 Answers1

1

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.
Pierre de Buyl
  • 7,074
  • 2
  • 16
  • 22