1function [varargout] = readdump_one(varargin)
2% Read LAMMPS dump file one timestep at a time
3% Input
4%       Dump file name with path
5%       Starting file pointer position in dump file
6%       Number of columns in the dump file
7% Output is in the form of a structure with following variables
8% .timestep     --> Vector containing all time steps
9% .Natoms       --> Vector containing number of atoms at each time step
10% .x_bound      --> [t,2] array with xlo,xhi at each time step
11% .y_bound      --> [t,2] array with ylo,yhi at each time step
12% .z_bound      --> [t,2] array with zlo,zhi at each time step
13% .atom_data    --> 2 dimensional array with data
14% .position     --> file pointer for reading next time
15%
16% Example
17%       data = readdump_one('dump.LAMMPS',0,5);
18%           Reads the first timestep in the file dump.LAMMPS
19%           Specify position = -1 if only the last dump step in the
20%               file is needed
21%
22% See also readdump, scandump
23%
24%  Author :  Arun K. Subramaniyan
25%            sarunkarthi@gmail.com
26%            http://web.ics.purdue.edu/~asubrama/pages/Research_Main.htm
27%            School of Aeronautics and Astronautics
28%            Purdue University, West Lafayette, IN - 47907, USA.
29
30try
31    dump = fopen(varargin{1},'r');
32catch
33    error('Dumpfile not found!');
34end
35position = varargin{2}; % from beg of file
36ncol = varargin{3}; %number of columns
37
38i=1;
39t=0;
40done = 0;
41last_status = 0;
42if position ~= -1
43    fseek(dump,position,'bof');
44else
45    last_status = 1;
46end
47while done == 0 & last_status == 0
48    id = fgetl(dump);
49    if (strncmpi(id,'ITEM: TIMESTEP',numel('ITEM: TIMESTEP')))
50            if t == 0
51                timestep(i) = str2num(fgetl(dump));
52                t=1;
53            end
54    else
55     if (strcmpi(id,'ITEM: NUMBER OF ATOMS',numel('ITEM: NUMBER OF ATOMS')))
56            Natoms = str2num(fgetl(dump));
57     else
58      if (strcmpi(id,'ITEM: BOX BOUNDS',numel('ITEM: BOX BOUNDS')))
59            x_bound(1,:) = str2num(fgetl(dump));
60            y_bound(1,:) = str2num(fgetl(dump));
61            z_bound(1,:) = str2num(fgetl(dump));
62      else
63       if (strncmpi('ITEM: ATOMS',numel('ITEM: ATOMS')))
64            atom_data = zeros(Natoms,ncol);%Allocate memory for atom data
65            for j = 1 : 1: Natoms
66                atom_data(j,:) = str2num(fgetl(dump));
67            end
68            done = 1;
69            p = ftell(dump);
70       end
71      end
72     end
73    end
74end
75
76% Getting only the last step
77if last_status == 1
78    % First get the position of the beginning of the last step in the
79    % dumpfile
80    while ~feof(dump)
81        temp = fgetl(dump);
82        if length(temp) == 14
83            if strcmpi(temp,'ITEM: TIMESTEP')
84                p = ftell(dump); % starting position of line next to the header
85            end
86        end
87    end
88    fclose(dump);
89    dump = fopen(varargin{1},'r');
90    fseek(dump,p,'bof');
91    % getting Timestep
92    timestep = str2num(fgetl(dump));
93
94    while ~feof(dump)
95        id = fgetl(dump);
96     if (strcmpi(id,'ITEM: NUMBER OF ATOMS'))
97            Natoms = str2num(fgetl(dump));
98     else
99      if (strcmpi(id,'ITEM: BOX BOUNDS'))
100            x_bound(1,:) = str2num(fgetl(dump));
101            y_bound(1,:) = str2num(fgetl(dump));
102            z_bound(1,:) = str2num(fgetl(dump));
103      else
104       if (strcmpi(id(1:11),'ITEM: ATOMS'))
105            atom_data = zeros(Natoms,ncol);%Allocate memory for atom data
106            for j = 1 : 1: Natoms
107                atom_data(j,:) = str2num(fgetl(dump));
108            end
109       end
110      end
111     end
112    end
113
114
115
116end
117
118%----------Outputs-------------
119
120%OUTPUTS IN SAME VARIABLE STRUCTURE
121varargout{1}.timestep = timestep;
122varargout{1}.Natoms = Natoms;
123varargout{1}.x_bound = x_bound;
124varargout{1}.y_bound = y_bound;
125varargout{1}.z_bound = z_bound;
126varargout{1}.atom_data = atom_data;
127varargout{1}.position = p; %gives position of ITEM: TIMESTEP line
128%------------------------------
129
130fclose(dump);
131
132