function [griddata, elemdata, Frhodata, rhordata, phirdata] = readLAMMPSeamfs(potfilename)
%read LAMMPS potential file of the eam.fs type
%notice: phirdata stores r*phi (eV*Angstrom)

fid = fopen(potfilename,'r');
% skip the first three lines
myline = fgets(fid);
myline = fgets(fid);
myline = fgets(fid);

% read number of elements
myline = fgets(fid);
Nele = sscanf(myline,'%d',1);

% read grid info
myline = fgets(fid);
tmp = sscanf(myline,'%e',5);
Nrho = tmp(1); drho = tmp(2); Nr = tmp(3); dr = tmp(4); rcut = tmp(5);
griddata = struct('Nele',Nele,'Nrho',Nrho,'drho',drho,'Nr',Nr,'dr',dr,'rcut',rcut);

% loop over each element
for iele = 1:Nele,
    % read element info
    myline = fgets(fid);
    tmp = sscanf(myline,'%e',3);
    Z=tmp(1); atommass=tmp(2); latticeconst=tmp(3);
    elemdata{iele} = struct('Z',Z,'atommass',atommass,'latticeconst',latticeconst);
    
    % read F(rho)
    Frho = zeros(Nrho,1);
    j = 0;
    while j < Nrho,
        myline = fgets(fid);
        tmp = sscanf(myline,'%e',inf);
        Frho(j+1:j+length(tmp)) = tmp;
        j = j+length(tmp);
    end
    Frhodata{iele} = Frho;
    %
    figure(iele);
    subplot(1,3,1);
    plot([0:Nrho-1],Frho);
    
    % read rho(r)
    rhor = zeros(Nr,1);
    for jele = 1:Nele,
        n = 0;
        while n < Nr,
            myline = fgets(fid);
            tmp = sscanf(myline,'%e',inf);
            rhor(n+1:n+length(tmp)) = tmp;
            n = n+length(tmp);
        end
        rhordata_iele{jele} = rhor;
    end
    rhordata{iele} = rhordata_iele;
    
    figure(iele);
    if Nele == 3,
        subplot(1,3,2);
        plot([0:Nr-1],rhordata_iele{1},'-', ...
             [0:Nr-1],rhordata_iele{2},'.', ...
             [0:Nr-1],rhordata_iele{3},'--');
    end
end    
    
for iele = 1:Nele,
    % read phi(r)
    phir = zeros(Nr,1);
    for jele = 1:iele,
        n = 0;
        while n < Nr,
            myline = fgets(fid);
            tmp = sscanf(myline,'%e',inf);
            phir(n+1:n+length(tmp)) = tmp;
            n = n+length(tmp);
        end
        phirdata_iele{jele} = phir;
    end
    phirdata{iele} = phirdata_iele;
end

for iele = 1:Nele,
    for jele = iele+1:Nele,
        phirdata{iele}{jele} = phirdata{jele}{iele};
    end
    figure(iele);
    if Nele == 3,
        subplot(1,3,3);
        hold on
        plot([0:Nr-1],phirdata{iele}{1},'-', ...
             [0:Nr-1],phirdata{iele}{2},'-', ...
             [0:Nr-1],phirdata{iele}{3},'-');
        hold off
    end
end

fclose(fid);