%
%   mspan_beam_assembly_unc_pm.m  ver 1.0  by Tom Irvine
%
function[stiff,mass,stiff_unc,mass_unc,T,r]=...
                beam_assembly_seismic(ne,klocal,mlocal,LBC,dof,M)
%
    sg=zeros(dof,dof);
    mg=zeros(dof,dof);    
%
   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

%%%%

%
clear mass;
clear stiff;
%
stiff=sg;

mass=mg;

stiff_unc=sg;
mass_unc=mg;

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

N=max(size(mass));
r=zeros(N,1);
%

LBC

if(LBC==1) % fixed-free
    T=zeros(N,N-1);
    T(1,1)=1;
    for i=3:N
        T(i,i-1)=1;
    end
    r(1)=1;
end  


if(LBC==2) % pinned-pinned
    T = build_T_pinned_pinned_general(ne);
    r = influence_r_pinned_pinned(ne);
end 

if(LBC==3)
    disp('fixed-fixed')
    T = build_T_fixed_fixed(ne);
    r = influence_r_fixed_fixed(ne);
end


size(T)
size(mass)
if(max(size(mass))<=8)
    T
end

mass=T'*mass*T;
mass(1,1)=mass(1,1)+M;

stiff=T'*stiff*T;     

end
function T = build_T_pinned_pinned_general(N)
    % N = number of beam elements
    nodes = N + 1;                     % number of nodes
    nFull   = 2 * nodes;              % full DOFs: u1,θ1, u2,θ2, …, u_n, θ_n
    % Reduced DOFs ordering:
    %   1 → U (seismic mass translation)
    %   2 → u2 (translation at internal node 2)
    %   3 → u3 (translation at internal node 3)
    %     … up to u_{n-1} if n>2
    %   then θ1, θ2, …, θ_n (rotations at all nodes)
    nRed    = 1 + (nodes-2) + nodes;  % U + translations of nodes 2…(n-1) + θ1…θ_n
    
    T = zeros(nFull, nRed);
    
    % --- Map seismic mass DOF U to both end‐translations ---
    % Full row index for u1: row = 1
    T(1,1) = 1;
    % Full row for u_{n} (translation of last node):
    T(2*(nodes-1) + 1, 1) = 1;
    
    % --- Map internal translations u2…u_{n-1} ---
    colIdx = 2;
    for iNode = 2 : (nodes-1)
        fullRow = 2*(iNode-1) + 1;    % translation DOF of node iNode
        T(fullRow, colIdx) = 1;
        colIdx = colIdx + 1;
    end
    
    % --- Map all rotations θ1…θ_n ---
    for iNode = 1 : nodes
        fullRow = 2*(iNode-1) + 2;    % rotation DOF of node iNode
        T(fullRow, colIdx) = 1;
        colIdx = colIdx + 1;
    end
    
    % Return
end
function T = build_T_fixed_fixed(N)
    nodes = N + 1;
    nFull = 2 * nodes;
    
    % Suppose we assume end rotations θ1 and θ_n are fixed (eliminated)
    % Reduced DOFs: [U, u2…u_{n-1}, θ2…θ_{n-1}]  (exclude θ1, θ_n)
    nRed = 1 + (nodes-2) + (nodes-2);  % U + translations internal + rotations internal
    
    T = zeros(nFull, nRed);
    
    % Map U → both end translations u1 & u_n
    T(1,1) = 1;
    T(2*(nodes-1)+1,1) = 1;
    
    % Map internal translations u2…u_{n-1}
    colIdx = 2;
    for iNode = 2 : (nodes-1)
        fullRow = 2*(iNode-1) + 1;     % translation DOF
        T(fullRow, colIdx) = 1;
        colIdx = colIdx + 1;
    end
    
    % Map internal rotations θ2…θ_{n-1}
    for iNode = 2 : (nodes-1)
        fullRow = 2*(iNode-1) + 2;     % rotation DOF
        T(fullRow, colIdx) = 1;
        colIdx = colIdx + 1;
    end
end

function r = influence_r_pinned_pinned(N)
    % N = number of beam elements
    nodes = N + 1;            % number of nodes
    nFull  = 2 * nodes;       % full DOFs: u1, θ1, u2, θ2, …, u_n, θ_n

    % Initialize r
    r = zeros(nFull,1);

    % Both end translations move with the base/mass
    r(1) = 1;                          % full row for u1
    r(2*(nodes-1)+1) = 1;             % full row for u_n (translation)

    % All other DOFs = 0
end
function r = influence_r_fixed_fixed(N)
    % N = number of beam elements
    nodes = N + 1;            % number of nodes
    nFull  = 2 * nodes;       % full DOFs: u1, θ1, u2, θ2, …, u_n, θ_n

    % Initialize r
    r = zeros(nFull,1);

    % Both end translations move with the base/mass
    r(1) = 1;                          % full row for u1
    r(2*(nodes-1)+1) = 1;             % full row for u_n (translation)

    % All other DOFs = 0
end
