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
| function [Direct2Cartesian, AllElements, Element_Num,Num_Element_PositionD,Num_Element_PositionC_Height] = Read_CONTCAR(NumOftopCu,filename) %This function is used to read the information of optimized structure from %VASP calculation.
%Direct2Cartesian is the matrix to transform the Direct coordinates to %Cartesian coordiantes, i.e. the lattice parameters of the supercell.
%ALLElements contains all elemetns in the structure, for F4TCNQ/Cu(111) Cu %C N F.
%Element_Num contains the Number of atoms of each element according to the %order in AllElements.
%Num_Element_PositionD contains the Direct coordinates of all atoms with %the same order in CONTCAR
%,Num_Element_PositionC_Height contains the heights of all atoms with %the same order in CONTCAR
%NumOftopCu give the serial number of top-most atoms .
%filename the output from VASP, i.e. CONTCAR.
fid=fopen(filename); Direct2Cartesian = cell2mat(textscan(fid, '%f %f %f',3, 'Headerlines', 2,'CollectOutput',1)); A=textscan(fid, '%s %s %s %s',1 , 'CollectOutput', 1); %***** the number of %s should be changed to be the same as number of elements
AllElements=A{1,1}; B=textscan(fid, '%d %d %d %d',1, 'CollectOutput', 1); Element_Num=B{1,1}; Total_atoms = sum(Element_Num); C=textscan(fid, '%f %f %f %*s %*s %*s',Total_atoms, 'Headerlines', 3, 'CollectOutput',1); % %*s %*s %*s is to find and ignore "T T T" or "F F F" % The 'Headerlines' has to be 3 not 2.
AtomsPositionD = C{1,1}; Num_Element_PositionD=[[1:Total_atoms]' string([repelem(AllElements, Element_Num)]') AtomsPositionD]; AtomsPositionC= AtomsPositionD*Direct2Cartesian; TopLayerSubHeight=mean(AtomsPositionC(NumOftopCu,3)); Atom_Height= AtomsPositionC(:,3)-TopLayerSubHeight; disp(Atom_Height) Num_Element_PositionC_Height=[[1:Total_atoms]' string([repelem(AllElements, Element_Num)]') AtomsPositionC Atom_Height];
fclose(fid);
|