-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtfScan.m
126 lines (122 loc) · 2.61 KB
/
vtfScan.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
function Simul = vtfScan(vtf_fn)
% scan vtf files and write mat file
% DY170327
% DY180713
%%
if ~strcmp(vtf_fn(end-3:end),'.vtf')
SimName = vtf_fn;
vtf_fn = [vtf_fn,'.vtf'];
else
SimName = vtf_fn(1:end-4);
end
% MaxNBond = 4;
TimeStepMax = inf;
fid=fopen(vtf_fn,'r');
isTimeStep = 0; %Find 1st timestep
LineNumber = 0;
head = cell(0,1);
write2head = 1;
while ~isTimeStep
LineNumber = LineNumber + 1;
tline = fgetl(fid);
tSSP = strsplit(tline,{' ',':'});
switch tSSP{1}
case 'pbc'
write2head = 0;
pbc(1,1) = str2double(tSSP{2});
pbc(1,2) = str2double(tSSP{3});
pbc(1,3) = str2double(tSSP{4});
case 'atom'
write2head = 0;
% ltSSP = length(ltSSP);
aidx(1) = str2double(tSSP{2}) + 1;
aidx(2) = str2double(tSSP{3}) + 1;
aidx = sort(aidx);
f1 = tSSP{4};
f2 = tSSP{6};
f3 = tSSP{8};
f1v = str2double(tSSP{5});
f2v = tSSP{7};
f3v = str2double(tSSP{9});
try
f4 = tSSP{10};
f4v = str2double(tSSP{11});
catch
f4 = 'q';
f4v = 0;
end
for i = aidx(1):aidx(2)
atom(i).(f1) = f1v;
atom(i).(f2) = f2v;
atom(i).(f3) = f3v;
atom(i).(f4) = f4v;
if ~isfield(atom(i),'bond')
atom(i).bond = [];
end
end
case 'bond'
write2head = 0;
ai = str2double(tSSP{2}) + 1;
aj = str2double(tSSP{3}) + 1;
atom(ai).bond = [atom(ai).bond,aj];
atom(aj).bond = [atom(aj).bond,ai];
if ~exist('bond','var')
bond = [ai,aj];
else
bond = [bond;[ai,aj]];
end
case 'timestep'
write2head = 0;
isTimeStep = 1;
otherwise
if write2head
head{LineNumber,1} = tline;
end
end
end
nAtom = size(atom,2);
TimeStep = 0;
coords = zeros(nAtom,3,1);
while TimeStep < TimeStepMax
TimeStep = TimeStep + 1;
coords_temp = zeros(nAtom,3);
try
for a = 1:nAtom
% LineNumber = LineNumber + 1;
tline = fgetl(fid);
C = textscan(tline,'%f',3);
coords_temp(a,:) = C{1};
end
coords(:,:,TimeStep) = coords_temp;
catch
TimeStep = TimeStep - 1;
end
isTimeStep = 0;
while ~isTimeStep
% LineNumber = LineNumber + 1;
tline = fgetl(fid);
if ~isempty(tline)
if (tline(1) >= 0)
tSSP = strsplit(tline,{' ',':'});
if strcmp(tSSP{1},'timestep')
isTimeStep = 1;
end
else
isTimeStep = 1;
TimeStepMax = TimeStep;
end
end
end
end
if ~isempty(head)
Simul.head = head;
end
atom = struct2table(atom);
Simul.Name = SimName;
Simul.PBC = pbc;
Simul.Atom = atom;
Simul.Bond = bond;
Simul.Coords = coords;
Simul.TotalTimeSteps = TimeStep;
fclose('all');
save([SimName,'.mat'],'Simul','-v7.3');