%% 刚度矩阵组装 Stiffness matrix assembly
% k_{ij}^e={\frac{1}{\mu_r }∭}_{V_e }  {\left(\nabla \times {\mathit{\mathbf{N}}}_i^e \right)}\cdot {\left(\nabla \times {\mathit{\mathbf{N}}}_j^e \right)}\mathrm{d}V
% \frac{1}{\mu_r }\frac{4l_i^e l_j^e V_e }{{{\left(6V_e \right)}}^4 }\left\lbrack {\left(c_{i1}^e d_{i2}^e -c_{i2}^e d_{i1}^e \right)}{\left(c_{j1}^e d_{j2}^e -c_{j2}^e d_{j1}^e \right)}+{\left(b_{i2}^e d_{i1}^e -b_{i1}^e d_{i2}^e \right)}{\left(b_{j2}^e d_{j1}^e -b_{j1}^e d_{j2}^e \right)}+{\left(b_{i1}^e c_{i2}^e -b_{i2}^e c_{i1}^e \right)}{\left(b_{j1}^e c_{j2}^e -b_{j2}^e c_{j1}^e \right)}\right\rbrack
% Jin, 2014.  8.3.4,  equation 8.68

function K = get_K_v2(abcd,vol,elems2edges,edgeLength,elemEdgeLength,signs,mur) 
mu0 = 4*pi*1e-7;
b=abcd(:,5:8);
c=abcd(:,9:12);
d =abcd(:,13:16);
neles    = size(elems2edges,1);
nedges   = size(edgeLength,1);
K_ij     = zeros(36*neles,1);
ii       = zeros(36*neles,1);
jj       = zeros(36*neles,1);
edge_ij     = [1 2;1 3;1 4;2 3;4 2;3 4];

index   = 0;
for ei= 1:6
    for ej = 1:6
        
        ii(index+1:index+neles) = elems2edges(:,ei);
        jj(index+1:index+neles) = elems2edges(:,ej);
        length_i = elemEdgeLength(:,ei);   %第i边的边长 The length of ith edge
        length_j = elemEdgeLength(:,ej);   %第j边边长 The length of jth edge
        Sign_ij = signs(:,ei).*signs(:,ej);
        
        i1 = edge_ij(ei,1);   %单元内第ei条边的第1个节点 The first node of ei edge on tetrahedron element
        i2 = edge_ij(ei,2);   %单元内第ei条边的第2个节点 The second node of ei edge on tetrahedron element
        j1 = edge_ij(ej,1);   %单元内第ej条边的第1个节点 The first node of ej edge on tetrahedron element
        j2 = edge_ij(ej,2);   %单元内第ej条边的第2个节点 The second node of ej edge on tetrahedron element
        
        K_ij(index+1:index+neles) = 4*Sign_ij.*(1./mur).*((length_i.*length_j.*vol)./(6*vol).^4).*(...
            (c(:,i1).*d(:,i2)-d(:,i1).*c(:,i2)).*(c(:,j1).*d(:,j2)-d(:,j1).*c(:,j2)) +...
            (d(:,i1).*b(:,i2)-b(:,i1).*d(:,i2)).*(d(:,j1).*b(:,j2)-b(:,j1).*d(:,j2)) +...
            (b(:,i1).*c(:,i2)-c(:,i1).*b(:,i2)).*(b(:,j1).*c(:,j2)-c(:,j1).*b(:,j2)) );
        
        index = index + neles;
    end
end

K = sparse(ii,jj,K_ij,nedges,nedges);
end