-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoordsIO.jl
97 lines (73 loc) · 2.34 KB
/
CoordsIO.jl
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
module CoordsIO
export read_xsf, get_frame
function read_xsf(filename; read_forces=false, dump=false)
f = open(filename)
if dump
for i=1:3
readline(f)
end
na = parse(Int, split(readline(f))[1])
for i=1:5
readline(f)
end
else
for i in 1:2
readline(f)
end
supercell = []
push!(supercell, parse(Float64, split(readline(f))[1]))
push!(supercell, parse(Float64, split(readline(f))[2]))
for i in 1:2
readline(f)
end
na = parse(Int, split(readline(f))[1])
end
atoms = zeros(Float64, (na, 3))
if read_forces
forces = zeros(Float64, (na, 3))
end
forces_in_file = false
for k in 1:na
split_line = split(readline(f))
x, y, z = split_line[2:4]
atoms[k, :] = [parse(Float64, x), parse(Float64, y), parse(Float64, z)]
if length(split_line) > 4
forces_in_file = true
fx, fy, fz = split_line[5:end]
forces[k, :] = [parse(Float64, fx), parse(Float64, fy), parse(Float64, fz)]
end
end
close(f)
if dump
return atoms
else
if forces_in_file && read_forces
return atoms, forces, supercell
else
return atoms, supercell
end
end
end
function get_Natoms_dump(filename)
fo = open(filename)
nchars = 39 #number of chars to read before reaching line containing the number of atoms (assuming file is a dump file ⟺ line 1 = 'ITEM: TIMESTEP\n')
seek(fo,nchars)
Natoms = parse(Int, readline(fo))
close(fo)
return Natoms
end
function get_frame(filename, frame_index; frame_step=1, frame0_index=0)
nb_non_coord_lines::Int = 9
Natoms = get_Natoms_dump(filename)
nlines_per_frame = Natoms + nb_non_coord_lines
nlines_tail = nlines_per_frame * (Int((frame_index - frame0_index)/frame_step) + 1)
pos = zeros(Float64, (Natoms, 3))
rawpos = read(pipeline(`head -n $nlines_tail $filename`, `tail -n $Natoms`), String) #this step is the bottleneck... might need to optimise
allpos = split(rawpos, '\n')
for i=1:Natoms
x, y, z = split(allpos[i])[2:4]
pos[i,:] = [parse(Float64, x), parse(Float64, y), parse(Float64, z)]
end
return pos
end
end