dcdfort
Modern Fortran library for analyzing DCD trajectories
Data Types | Functions/Subroutines
dcdfort_trajectory Module Reference

Module that contains Trajectory type. More...

Data Types

type  trajectory
 Trajectory class. More...
 

Functions/Subroutines

subroutine trajectory_open (this, filename, ndxfile)
 Trajectory class method which opens DCD file and optionally index file. More...
 
pure integer(kind=int32) function trajectory_get_natoms (this, group)
 Trajectory class method which gets the number of atoms in the system or an index group. More...
 
integer(kind=int32) function trajectory_skip_next (this, F)
 Trajectory class method which skips a specified number of frames. More...
 
integer(kind=int32) function trajectory_read_next (this, F, ndxgrp, every)
 Trajectory class method which reads a specified number of frames into memory after using the open() method. More...
 
pure real(kind=real32) function, dimension(3) trajectory_get_xyz (this, frame, atom, group)
 Trajectory class method which returns the coordinates of a particle. More...
 
subroutine trajectory_read (this, dcdfile, ndxfile, ndxgrp, every, skip)
 Trajectory class method which opens, reads, and closes a trajectory file. More...
 
pure real(kind=real64) function, dimension(6) trajectory_get_box (this, frame)
 Trajectory class method which returns the box from a simulation frame. More...
 
subroutine trajectory_close (this)
 Trajectory class method which closes a DCD file which was opened with open() More...
 
pure real(kind=real64) function trajectory_vol (this, frame)
 Trajectory class method which returns the volume of the box. More...
 

Detailed Description

Module that contains Trajectory type.

Function/Subroutine Documentation

◆ trajectory_close()

subroutine dcdfort_trajectory::trajectory_close ( class(trajectory), intent(inout)  this)
private

Trajectory class method which closes a DCD file which was opened with open()

Parameters
[in,out]thisTrajectory class
378 
379  implicit none
380  class(Trajectory), intent(inout) :: this
381 
382  call this%dcd%close()
383 

◆ trajectory_get_box()

pure real(kind=real64) function, dimension(6) dcdfort_trajectory::trajectory_get_box ( class(trajectory), intent(in)  this,
integer(kind=int32), intent(in)  frame 
)
private

Trajectory class method which returns the box from a simulation frame.

Parameters
[in,out]thisTrajectory class
[in]frameWhich frame to return the box dimensions
Returns
box dimensions of specified snapshot. Array containing 6 elements, ordered as A, B, C, alpha, beta, gamma. A = length of unit cell vector along x-axis; B = length of unit cell vector in xy-plane; C = length of unit cell vector in yz-plane; alpha = cosine of angle between B and C; beta = cosine of angle between A and C; gamma = cosine of angle between A and B;
346 
347  implicit none
348  real(kind=real64) :: trajectory_get_box(6)
349  class(Trajectory), intent(in) :: this
350  integer(kind=int32), intent(in) :: frame
351  integer(kind=int32) :: i
352 
353  call trajectory_check_frame(this, frame)
354  do i = 1, 6
355  trajectory_get_box(i) = this%frameArray(frame)%box(i)
356  end do
357 

◆ trajectory_get_natoms()

pure integer(kind=int32) function dcdfort_trajectory::trajectory_get_natoms ( class(trajectory), intent(in)  this,
character(len=*), intent(in), optional  group 
)
private

Trajectory class method which gets the number of atoms in the system or an index group.

Parameters
[in,out]thisthe Trajectory object
[in]groupname of index group
Returns
number of atoms in index group (if specified) or in system
119 
120  implicit none
121  integer(kind=int32) :: trajectory_get_natoms
122  class(Trajectory), intent(in) :: this
123  character(len=*), intent(in), optional :: group
124 
125  if (this%read_only_index_group .and. present(group)) then
126  call error_stop_program("Do not specify an index group in natoms() when already specifying an &
127  &index group with read() or read_next().")
128  end if
129 
130  trajectory_get_natoms = merge(this%ndx%get_natoms(group), this%NUMATOMS, present(group))
131 

◆ trajectory_get_xyz()

pure real(kind=real32) function, dimension(3) dcdfort_trajectory::trajectory_get_xyz ( class(trajectory), intent(in)  this,
integer(kind=int32), intent(in)  frame,
integer(kind=int32), intent(in)  atom,
character(len=*), intent(in), optional  group 
)
private

Trajectory class method which returns the coordinates of a particle.

Gets the coordinates of a particle from the read in trajectory. Returns a real array with 3 elements (x, y, and z). An optional index group can be specified; if so, the atomid is in relationship to the group.

Parameters
[in,out]thisTrajectory class
[in]frame
[in]atomatomid of particle to get
[in]groupoptional index group
Returns
coordinate of the particle
266 
267  implicit none
268  real(kind=real32) :: trajectory_get_xyz(3)
269  integer(kind=int32), intent(in) :: frame, atom
270  integer(kind=int32) :: atom_tmp, natoms
271  class(Trajectory), intent(in) :: this
272  character(len=1024) :: message
273  character(len=*), intent(in), optional :: group
274 
275  call trajectory_check_frame(this, frame)
276 
277  if (this%read_only_index_group .and. present(group)) then
278  call error_stop_program("Do not specify an index group in x() when already specifying an &
279  &index group with read() or read_next().")
280  end if
281 
282  atom_tmp = merge(this%ndx%get(group, atom), atom, present(group))
283  natoms = merge(this%natoms(group), this%natoms(), present(group))
284 
285  if (atom > natoms .or. atom < 1) then
286  write(message, "(a,i0,a,i0,a)") "Tried to access atom number ", atom_tmp, " when there are ", &
287  natoms, ". Note that Fortran uses one-based indexing."
288  call error_stop_program(trim(message))
289  end if
290 
291  ! Faster to list individually
292  trajectory_get_xyz(1) = this%frameArray(frame)%xyz(atom_tmp,1)
293  trajectory_get_xyz(2) = this%frameArray(frame)%xyz(atom_tmp,2)
294  trajectory_get_xyz(3) = this%frameArray(frame)%xyz(atom_tmp,3)
295 

◆ trajectory_open()

subroutine dcdfort_trajectory::trajectory_open ( class(trajectory), intent(inout)  this,
character(len=*), intent(in)  filename,
character(len=*), intent(in), optional  ndxfile 
)

Trajectory class method which opens DCD file and optionally index file.

Parameters
[in,out]thisthe Trajectory object
[in]filenamename of DCD file
[in]ndxfilename of GROMACS style index file
88 
89  implicit none
90  class(Trajectory), intent(inout) :: this
91  character(len=*), intent(in) :: filename
92  character(len=*), intent(in), optional :: ndxfile
93 
94  call this%dcd%open(trim(filename))
95 
96  write(error_unit,'(a)') prompt//"Opened "//trim(filename)//" for reading."
97  call this%dcd%read_header(this%nframes, this%istart, this%nevery, this%iend, this%timestep, this%NUMATOMS)
98 
99  write(error_unit,'(a,i0,a)') prompt, this%NUMATOMS, " atoms present in system."
100  write(error_unit,'(a,i0,a)') prompt, this%NFRAMES, " frames present in trajectory file."
101  write(error_unit,'(a,i0)') prompt//"First timestep in trajectory file: ", this%ISTART
102  write(error_unit,'(a,i0)') prompt//"Last timestep in strajectory file: ", this%IEND
103  write(error_unit,'(a,f12.6)') prompt//"Simulation timestep: ", this%timestep
104  write(error_unit,'(a,i0,a)') prompt//"Trajectory written every ", this%nevery, " timesteps."
105 
106  this%FRAMES_REMAINING = this%NFRAMES
107 
108  this%N = this%NUMATOMS ! Save for use when user selects just one group
109  if (present(ndxfile)) call this%ndx%read(ndxfile, this%NUMATOMS)
110 

◆ trajectory_read()

subroutine dcdfort_trajectory::trajectory_read ( class(trajectory), intent(inout)  this,
character(len=*)  dcdfile,
character(len=*), optional  ndxfile,
character(len=*), optional  ndxgrp,
integer(kind=int32), intent(in), optional  every,
integer(kind=int32), intent(in), optional  skip 
)
private

Trajectory class method which opens, reads, and closes a trajectory file.

Parameters
[in,out]thisTrajectory class
[in]dcdfileName of DCD trajectory file
[in]ndxfileName of optional index file
[in]ndxgrpName of optional group. If specified, only that group will be read into memory; otherwise, all particles read into memory.
[in]everyRead in every this many frames; default is 1
[in]skipSkip this many frames at the beginning of the trajectory file; default is 0
307 
308  implicit none
309  class(Trajectory), intent(inout) :: this
310  character(len=*), optional :: ndxfile, ndxgrp
311  character(len=*) :: dcdfile
312  integer(kind=int32) :: N
313  integer(kind=int32), intent(in), optional :: every, skip
314 
315  call this%open(dcdfile, ndxfile)
316 
317  if (present(every)) then
318  write(error_unit,'(a,i0,a)') prompt//"Saving ", every, " snapshots into memory."
319  end if
320  if (present(skip)) then
321  write(error_unit,'(a,i0,a)') prompt//"Skipping first ", skip, " snapshots."
322  end if
323 
324  if (present(skip)) then
325  n = this%skip_next(skip)
326  n = this%read_next(this%NFRAMES-n, ndxgrp, every)
327  else
328  n = this%read_next(this%NFRAMES, ndxgrp, every)
329  end if
330 
331  call this%close()
332 

◆ trajectory_read_next()

integer(kind=int32) function dcdfort_trajectory::trajectory_read_next ( class(trajectory), intent(inout)  this,
integer(kind=int32), intent(in), optional  F,
character(len=*), optional  ndxgrp,
integer(kind=int32), intent(in), optional  every 
)
private

Trajectory class method which reads a specified number of frames into memory after using the open() method.

Parameters
[in,out]thisTrajectory class
[in]Fnumber of frames to read in; if not specified, 1 frame is read
[in]ndxgrpread only this index group into memory
[in]everyRead in every this many frames; default is to read in every frame
Returns
number of frames read in
167 
168  implicit none
169  integer(kind=int32) :: trajectory_read_next
170  class(Trajectory), intent(inout) :: this
171  integer(kind=int32), intent(in), optional :: F, every
172  character(len=*), optional :: ndxgrp
173  integer(kind=int32) :: I, J, N, S, K
174  real(kind=real32), allocatable :: xyz(:,:)
175 
176  ! If the user specified how many frames to read and it is greater than one, use it
177  n = merge(f, 1, present(f))
178  s = merge(every, 1, present(every)) - 1
179 
180  ! Are we near the end of the file?
181  n = min(this%FRAMES_REMAINING, n)
182  this%FRAMES_REMAINING = this%FRAMES_REMAINING - n
183 
184  if (present(every)) n = n / every
185 
186  if (allocated(this%frameArray)) deallocate(this%frameArray)
187  allocate(this%frameArray(n))
188 
189  write(error_unit,*)
190 
191  this%read_only_index_group = .false.
192 
193  if (present(ndxgrp)) then
194 
195  allocate(xyz(this%N,3))
196  this%NUMATOMS = this%natoms(trim(ndxgrp))
197  do i = 1, n
198 
199  if (modulo(i*(s+1), 1000) .eq. 0) call print_frames_saved(i, ndxgrp)
200 
201  allocate(this%frameArray(i)%xyz(this%NUMATOMS,3))
202  call this%dcd%skip_next(s)
203  call this%dcd%read_next(xyz, this%frameArray(i)%box)
204 
205  do j = 1, size(this%ndx%group)
206  if (trim(this%ndx%group(j)%title) .eq. trim(ndxgrp)) then
207  ! Tends to be faster to list this out individually
208  do k = 1, this%NUMATOMS
209  this%frameArray(i)%xyz(k,1) = xyz(this%ndx%group(j)%LOC(k),1)
210  this%frameArray(i)%xyz(k,2) = xyz(this%ndx%group(j)%LOC(k),2)
211  this%frameArray(i)%xyz(k,3) = xyz(this%ndx%group(j)%LOC(k),3)
212  end do
213  exit
214  end if
215  end do
216 
217  end do
218  deallocate(xyz)
219 
220  this%read_only_index_group = .true.
221 
222  else
223 
224  do i = 1, n
225 
226  if (modulo(i*(s+1), 1000) .eq. 0) call print_frames_saved(i)
227 
228  allocate(this%frameArray(i)%xyz(this%NUMATOMS,3))
229  call this%dcd%skip_next(s)
230  call this%dcd%read_next(this%frameArray(i)%xyz, this%frameArray(i)%box)
231 
232  end do
233 
234  end if
235 
236  this%frames_read = n
237  trajectory_read_next = n
238  call print_frames_saved(n, ndxgrp)
239 

◆ trajectory_skip_next()

integer(kind=int32) function dcdfort_trajectory::trajectory_skip_next ( class(trajectory), intent(inout)  this,
integer(kind=int32), intent(in), optional  F 
)
private

Trajectory class method which skips a specified number of frames.

Parameters
[in,out]thisTrajectory class
[in]Fnumber of frames to skip; if not specified, 1 frame is skipped
Returns
number of frames skipped
140 
141  implicit none
142  class(Trajectory), intent(inout) :: this
143  integer(kind=int32), intent(in), optional :: F
144  integer(kind=int32) :: trajectory_skip_next, N
145 
146  ! If the user specified how many frames to read and it is greater than one, use it
147  n = merge(f, 1, present(f))
148 
149  ! Are we near the end of the file?
150  n = min(this%FRAMES_REMAINING, n)
151  this%FRAMES_REMAINING = this%FRAMES_REMAINING - n
152 
153  call this%dcd%skip_next(n)
154  write(error_unit,'(a,i0,a)') prompt//"Skipped ", n, " frames."
155  trajectory_skip_next = n
156 

◆ trajectory_vol()

pure real(kind=real64) function dcdfort_trajectory::trajectory_vol ( class(trajectory), intent(in)  this,
integer(kind=int32), intent(in)  frame 
)
private

Trajectory class method which returns the volume of the box.

Parameters
[in,out]thisTrajectory class
[in]framesnapshot to get box volume of
Returns
the volume of the box of the frame specified
391 
392  implicit none
393  class(Trajectory), intent(in) :: this
394  integer(kind=int32), intent(in) :: frame
395  real(kind=real64) :: trajectory_vol
396 
397  trajectory_vol = vol(this%box(frame))
398