%
%   assembly.m  ver 1.1  January 30, 2012
%
function[stiff,mass,dof,dof_status]=assembly(ne,klocal,mlocal,ibc,dof,ipm,pm,xpm,dx)
%
    dof_status=ones(dof,1);
%
    for i=1:dof
			for j=1:dof
         		 sg(i,j)=0.;
				 mg(i,j)=0.;        
             end
    end
%
   ii=0;
   jj=0;
    for k=1:ne
        for i=1:4
			for j=1:4
				 sg(ii+i,jj+j)=sg(ii+i,jj+j)+klocal(k,i,j);
				 mg(ii+i,jj+j)=mg(ii+i,jj+j)+mlocal(k,i,j);
             end
        end
        ii=ii+2;
        jj=jj+2;
    end
%%%%
%%%%   Add point mass
%%%%
if (ipm==1)
    nnn=0;
    xv(1)=0.;
    for i=2:dof/2
        xv(i)=xv(i-1)+dx(i-1);
    end
    for i=1:dof/2
        if(xv(i)==xpm)
            nnn=i;
            break;
        end
    end
end
if (ipm==1)
    i=nnn;
    2*i-1;
    mg(2*i-1,2*i-1)=mg(2*i-1,2*i-1)+pm;
end
%%%%
%%%%    Apply bc
%%%%
%%%% disp(' ');
%%%% disp(' Enter boundary conditions ');
%%%% disp('  1=free-free ');
%%%% disp('  2=fixed-free ');
%%%% disp('  3=fixed-fixed ');
%%%% disp('  4=fixed-pinned ');
%%%% disp('  5=pinned-pinned ');
%%%%
%
clear mass;
clear stiff;
%
stiff=sg;
mass=mg;
%
N=max(size(mass));
%
if(ibc==1) % free-free 
%
%  no bcs applied
%
end
if(ibc==1) % fixed-free 
%
%  
%
end
if(ibc==3) % fixed-fixed
%
     stiff(N,:)=[];
     stiff(:,N)=[];
     mass(N,:)=[];
     mass(:,N)=[];
%
     stiff(N-1,:)=[];
     stiff(:,N-1)=[];
     mass(N-1,:)=[];
     mass(:,N-1)=[];
%
     dof_status(N)=0;
     dof_status(N-1)=0;    
%
end
if(ibc==4) % fixed-pinned
%    
     stiff(N-1,:)=[];
     stiff(:,N-1)=[];
     mass(N-1,:)=[];
     mass(:,N-1)=[];
%
     dof_status(N-1)=0;       
%     
end
if(ibc==2 || ibc==3 || ibc==4)
%
     stiff(2,:)=[];
     stiff(1,:)=[];
     stiff(:,2)=[];
     stiff(:,1)=[];
%
     mass(2,:)=[];
     mass(1,:)=[];
     mass(:,2)=[];
     mass(:,1)=[];
%
     dof_status(2)=0;
     dof_status(1)=0; 
%
end
%
if(ibc==5) % pinned-pinned
%    
     stiff(N-1,:)=[];
     stiff(:,N-1)=[];
     mass(N-1,:)=[];
     mass(:,N-1)=[];
%
     stiff(1,:)=[];
     stiff(:,1)=[];
%
     mass(1,:)=[];
     mass(:,1)=[];
%
     dof_status(N-1)=0;
     dof_status(1)=0; 
%
end
%
dof=max(size(mass));
%