function [A,Edges,EdgesOfElements,Dof]=PhysicMatrixAssembly(Nodes,Elements,Domains,lam0,epsilonr,mur)
%description: Assembly matrix of maxwell's equation

%physical variable
k0=2*pi/lam0;

%gauss quadracture points
[xvt,yvt,zvt,pvt,Integral3DOrder]=Get3DGuassPoints(1,2);

%process mesh
el2no=Elements';
n1=el2no([1 1 1 2 2 3],:);
n2=el2no([2 3 4 3 4 4],:);
el_ed2no_array=[n1(:) n2(:)];
[Edges,~,EdgesOfElements]=unique(el_ed2no_array,'rows');
NbrEdges=length(Edges);
NbrElements=length(Elements);

%Initialization of matrix assembly variables
Dof=NbrEdges;
NbrNonZers=NbrElements*6*6;
NumA=0;
Ai=zeros(NbrNonZers,1);
Aj=zeros(NbrNonZers,1);
Av=complex(zeros(NbrNonZers,1));

%loop over all elements
for n=1:NbrElements
    %coordinate of nodes
    x=Nodes(Elements(n,:),1);
    y=Nodes(Elements(n,:),2);
    z=Nodes(Elements(n,:),3);
    %length of edge
    l=ones(6,1);
    l(1)=sqrt((x(1)-x(2))*(x(1)-x(2))+(y(1)-y(2))*(y(1)-y(2))+(z(1)-z(2))*(z(1)-z(2)));
    l(2)=sqrt((x(1)-x(3))*(x(1)-x(3))+(y(1)-y(3))*(y(1)-y(3))+(z(1)-z(3))*(z(1)-z(3)));
    l(3)=sqrt((x(1)-x(4))*(x(1)-x(4))+(y(1)-y(4))*(y(1)-y(4))+(z(1)-z(4))*(z(1)-z(4)));
    l(4)=sqrt((x(2)-x(3))*(x(2)-x(3))+(y(2)-y(3))*(y(2)-y(3))+(z(2)-z(3))*(z(2)-z(3)));
    l(5)=sqrt((x(2)-x(4))*(x(2)-x(4))+(y(2)-y(4))*(y(2)-y(4))+(z(2)-z(4))*(z(2)-z(4)));
    l(6)=sqrt((x(3)-x(4))*(x(3)-x(4))+(y(3)-y(4))*(y(3)-y(4))+(z(3)-z(4))*(z(3)-z(4)));

    %Jac matrix
    Jac=zeros(3,3);
    Jac(1,1)=x(1)-x(4);Jac(1,2)=y(1)-y(4);Jac(1,3)=z(1)-z(4);
    Jac(2,1)=x(2)-x(4);Jac(2,2)=y(2)-y(4);Jac(2,3)=z(2)-z(4);
    Jac(3,1)=x(3)-x(4);Jac(3,2)=y(3)-y(4);Jac(3,3)=z(3)-z(4);
    InvJac=inv(Jac);
    TJac=Jac'/det(Jac);
    DetJac=abs(det(Jac));

    %basis function 
    Ne=zeros(3,6,Integral3DOrder);curlNe=zeros(3,6,Integral3DOrder);
    for i=1:6
        for k=1:Integral3DOrder
            temp=BF_Edge(1,i,xvt(k),yvt(k),zvt(k));
            Ne(:,i,k)=InvJac*temp*l(i);
            temp=BF_curlEdge(1,i,xvt(k),yvt(k),zvt(k));
            curlNe(:,i,k)=TJac*temp*l(i);
        end
    end
    
    %material
    domain=Domains(n);
    mu=1/mur(domain);
    epsilon=epsilonr(domain);

    %submatrix
    Ae=zeros(6,6);
    for i=1:6
        for j=1:6
            for k=1:Integral3DOrder
                Ae(i,j)=Ae(i,j)+pvt(k)*DetJac*mu*dot(curlNe(:,i,k),curlNe(:,j,k));
                Ae(i,j)=Ae(i,j)-pvt(k)*DetJac*k0*k0*epsilon*dot(Ne(:,i,k),Ne(:,j,k));
            end
        end
    end


    %adding to matrix sequence
    for i=1:6
        for j=1:6
            MappingIndexi=EdgesOfElements((n-1)*6+i);
            MappingIndexj=EdgesOfElements((n-1)*6+j);
            NumA=NumA+1;
            Ai(NumA)=MappingIndexi;
            Aj(NumA)=MappingIndexj;
            Av(NumA)=Ae(i,j);
        end
    end

end

%matrix assembly
A=sparse(Ai,Aj,Av);


end