仅仅.xyz转过去是不完整的(见下),还有bond, angle, dihedral的定义。
xyz2lmp.m
--------------
function xyz2lmp(f_xyz)
% This script converts .xyz file to lammps data file.
% Input:
% f_xyz: name of the input .xyz file.
% Example:
% xyz2lmp('PdAu.xyz')
% NOTE: The second line must be in specified format as:
% PdAu xlo xhi ylo yhi zlo zhi
% Poweed by Xianbao Duan
% Email: xianbao.d@gmail.com
% Website: http://www.52souji.net/
% open the .xyz file
fidin = fopen(f_xyz,'r');
if fidin == -1
error('Failed to open the file. Please check!');
end
% number of all the atoms
atom_num_a = fscanf(fidin,'%d');
% coordinates of the atoms
atoms = textscan(fidin,'%s %f %f %f',atom_num_a);
fclose(fidin);
% initialize
type_name = atoms{1}(1); % names of types
atom_num(1) = 1; % atom number of each type
% sort the atoms according to their types
for i = 2: length(atoms{1})
flag = 0;
for j = 1:length(type_name)
if strcmp(atoms{1}(i),type_name(j)) == 1
atom_num(j) = atom_num(j) + 1;
flag = 1;
break;
end
end
if flag == 0
type_name(end+1) = atoms{1}(i);
atom_num(end+1) = 1;
end
end
type_num = length(type_name);
% write the lammps file
outfilename = strrep(f_xyz,'.xyz','.lmp');
fidout = fopen(outfilename, 'w');
new_comment = ['Converted from .xyz to .lmp @ ',datestr(now) ];
fprintf(fidout,'%s\n\n',new_comment);
fprintf(fidout,'%d \t %s\n',atom_num_a,'atoms');
fprintf(fidout,'%d \t %s\n',type_num,'atom types');
fprintf(fidout,'%f %f \t%s\n',xlo,xhi,'xlo xhi');
fprintf(fidout,'%f %f \t%s\n',ylo,yhi,'ylo yhi');
fprintf(fidout,'%f %f \t%s\n\n',zlo,zhi,'zlo zhi');
fprintf(fidout,'%s\n\n','Atoms');
% the data
for i = 1:length(atoms{1})
for j = 1:type_num
if strcmp(atoms{1}(i),type_name(j)) == 1
fprintf(fidout,'%d \t %d \t %f \t %f \t %f \n',...
i,j,atoms{2}(i),atoms{3}(i),atoms{4}(i));
break;
end
end
end
fclose(fidout);