-
Notifications
You must be signed in to change notification settings - Fork 2
/
import_chgcar.m
executable file
·74 lines (58 loc) · 2.05 KB
/
import_chgcar.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
function [ chg mag geometry ] = import_chgcar( filename )
%IMPORT_CHGCAR Import a VASP CHGCAR file.
% [chg,mag,geometry] = import_chgcar(filename)
% Import a VASP CHGCAR file. If no filename is specified, data is read
% from CHGCAR. chg and mag are three dimensional arrays containing the
% charge magnetization densities in Bohr magneton per cubic Angstrom.
% Note that these are not the same units as the CHGCAR file. geometry is
% a struct describing the cell geometry; see IMPORT_POSCAR for a detailed
% description.
%
% See also IMPORT_POSCAR, IMPORT_LOCPOT.
% todo:
% check compatibility with non-spin-polarized files
% extract chemical symbols
% what about AECAR and ELFCAR files?
if nargin == 0
filename='CHGCAR';
end
fid = fopen(filename);
if fid==-1
error(['File ' filename ' not found']);
end
geometry = import_poscar(fid);
vol = abs(dot(geometry.lattice(1,:),cross(geometry.lattice(2,:),geometry.lattice(3,:))));
natoms = sum(geometry.atomcount);
fgetl(fid); % blank line
gridsize = fscanf(fid, '%d %d %d', [3 1])';
chg = fscanf(fid, '%f', [prod(gridsize,2) 1])';
chg = reshape(chg,gridsize);
chg = chg/vol;
fgetl(fid); % empty string (or padding)
pos = ftell(fid);
line = fgetl(fid);
fseek(fid,pos,'bof');
if line(1)=='a'
for i = 1:natoms
line = fgetl(fid);
nentries = sscanf(line,['augmentation occupancies ' num2str(i)...
' %d']); % number of occupancy entries
fscanf(fid, '%f', [nentries 1]);
fgetl(fid); % empty string
end
fscanf(fid, '%f', [natoms 1]); % don't know what these are
fgetl(fid); % empty string
end
pos = ftell(fid);
fgetl(fid);
if ~feof(fid)
fseek(fid,pos,'bof');
gridsize = fscanf(fid, '%d %d %d', [3 1])';
mag = fscanf(fid, '%f', [prod(gridsize,2) 1])';
mag = reshape(mag,gridsize);
mag = mag/vol;
else
mag = [];
end
fclose(fid);
end