xyz2lmp: 将xyz格式转成lammps的data file

目前,大部分的建模软件都支持导出xyz文件格式,而并不支持直接导出lammps所需要的data file文件格式。因此将xyz文件格式转换成lammps的格式是很有必要的。

原子数比较少的,可以自己手动编辑完成,对于原子数比较多的,实在比较麻烦。所以就写了下面的脚本可以帮助完成这一过程。

目前,程序只适用于合金体系,即不考虑力场参数(我的体系都属于这方面)。

因为xyz文件格式不包括晶胞信息,而lammps的data file文件是需要晶胞信息的,所以这个转换过程实际上是缺少信息的,为了弥补,请务必将晶胞信息按着类似以下格式放在xyz文件的注释行,也就是第二行,才可以完成转换,不然程序会出错。
system_name xlo xhi ylo yhi zlo zhi

输入xyz文件

test.xyz (只是为了测试程序,结构没有实际意义)

11
Cu 0 100 0 100 0 100
Cu 	0.000000 	 0.000000 	 0.000000
Cu 	3.800000 	 0.000000 	 0.000000
Fe 	7.600000 	 0.000000 	 0.000000
Cu 	11.400000 	 0.000000 	 0.000000
Fe 	1.900000 	 1.900000 	 0.000000
Cu 	5.700000 	 1.900000 	 0.000000
Cu 	9.500000 	 1.900000 	 0.000000
Au 	0.000000 	 3.800000 	 0.000000
Cu 	3.800000 	 3.800000 	 0.000000
Au 	7.600000 	 3.800000 	 0.000000
Cu 	11.400000 	 3.800000 	 0.000000

转换命令

>> xyz2lmp('test.xyz')

转换后lmp文件

test.lmp

Converted from .xyz to .lmp @ 04-May-2012 10:42:30

11 	 atoms
3 	 atom types
0.000000  100.000000 	xlo xhi
0.000000  100.000000 	ylo yhi
0.000000  100.000000 	zlo zhi

Atoms

1 	 1 	 0.000000 	 0.000000 	 0.000000
2 	 1 	 3.800000 	 0.000000 	 0.000000
3 	 2 	 7.600000 	 0.000000 	 0.000000
4 	 1 	 11.400000 	 0.000000 	 0.000000
5 	 2 	 1.900000 	 1.900000 	 0.000000
6 	 1 	 5.700000 	 1.900000 	 0.000000
7 	 1 	 9.500000 	 1.900000 	 0.000000
8 	 3 	 0.000000 	 3.800000 	 0.000000
9 	 1 	 3.800000 	 3.800000 	 0.000000
10 	 3 	 7.600000 	 3.800000 	 0.000000
11 	 1 	 11.400000 	 3.800000 	 0.000000

xyz2lmp源代码

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');

% comment
comment = textscan(fidin,'%s %f %f %f %f %f %f',1);
xlo = comment{2};
xhi= comment{3};
ylo = comment{4};
yhi= comment{5};
zlo = comment{6};
zhi= comment{7};

% 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);

如果你在使用过程中遇到什么问题,或者有改进的意见,欢迎与我交流。

标签: 文件格式, matlab, lammps

相关文章推荐

添加新评论 (无需注册,可直接评论)

已有 15 条评论

  1. 春水

    请问具体的过程是什么,我是初学者matlab也不太会用

  2. L

    请问怎么将xyz转换成bond文件呢

  3. 闪闪

    请问一下 如果是需要考虑力场参数的情况呢 我做的是氧化石墨烯的分子模拟 在VMD中转换了xyz文件 现在想转成LAMMPS的data 文件 楼主有解决办法吗

  4. 许菁

    真的可以哎,好炫酷啊!!

  5. p

    请问为什么我的出现这个错误,请帮忙解决啊?以下两种情况:
    [xpo@localhost ps]$ @gt;@gt; xyz2lmp('xiao.xyz')
    -bash: syntax error near unexpected token 'xiao.xyz'' [xpo@localhost ps]$ >> xyz2lmp('xiao.xyz') -bash: syntax error near unexpected token&'
    另外你的解释中:
    test.xyz (只是为了测试程序,结构没有实际意义)
    11
    Cu 0 100 0 100 0 100 这里加进去的第二行,我的是合金(两种元素)模型,请问怎么写?
    Cu 0.000000 0.000000 0.000000
    Cu 3.800000 0.000000 0.000000
    Fe 7.600000 0.000000 0.000000
    Cu 11.400000 0.000000 0.000000
    Fe 1.900000 1.900000 0.000000

    1. 我爱搜集网博主

      这个是matlab程序,不能直接在linux的命令行里执行。需要先运行matlab才行。

      1. p

        博主你好,.cfg格式文件可以转为.lmp格式文件吗?

        1. 我爱搜集网博主

          据我所知,cfg格式的文件一般都是lammps输出的;既然如此,为何还需要lammps格式的文件呢?
          当然如果转换也是可以的,思路是cfg->vasp->lammps

          1. p

            非常感谢楼主

  6. 李勇

    你好,请问如何将pdb格式转成lammps的data file呢?

    1. 我爱搜集网博主

      参考本站文章:Materials Studio构建的模型如何导入lammps?
      http://www.52souji.net/how-to-import-the-model-built-using-ms-into-lammps/

  7. 阿喀琉斯

    您好,请问,如果是碳纳米管,需要额外加入信息吗,键联结信息是不是缺失了?博主有好一点的方法吗,谢谢!

    1. 我爱搜集网博主

      一般来说,碳纳米管不需要加入额外信息,它的键直接由碳碳原子之间的距离确定。不过确实有更好的建模方法,参考博文:Materials Studio构建的模型如何导入lammps?

  8. Rsy

    这个使用的matlab?c++可以吗

    1. 我爱搜集网博主

      是matlab,C++肯定也可以,我没有写。你可以参考这个转换一下。