function [Ex, Ey, Ez, normE] = get_ele_vertices(mesh, solver)
%GET_ELE_VERTICES  Recover E at all mesh vertices (2nd-order Nedelec).

nV = mesh.NbrVertex;
Ex = zeros(nV, 1);
Ey = zeros(nV, 1);
Ez = zeros(nV, 1);
nCnt = zeros(nV, 1);

u = [1, 0, 0, 0];
v = [0, 1, 0, 0];
w = [0, 0, 1, 0];

for n = 1:mesh.NbrTet
    verts = mesh.Tet(n, :);
    x = mesh.Vertex(verts, 1);
    y = mesh.Vertex(verts, 2);
    z = mesh.Vertex(verts, 3);

    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);

    map = zeros(20, 1);
    for i = 1:6
        map(i * 2 - 1) = mesh.EdgeOfTet(n, i);
        map(i * 2) = mesh.EdgeOfTet(n, i) + mesh.NbrEdge;
    end
    for i = 1:4
        map(12 + i * 2 - 1) = 2 * mesh.NbrEdge + mesh.FaceOfTet(n, i);
        map(12 + i * 2) = 2 * mesh.NbrEdge + mesh.FaceOfTet(n, i) + mesh.NbrFace;
    end

    E = zeros(3, 20, 4);
    for i = 1:4
        for j = 1:20
            E(:, j, i) = getBF(1, j, u(i), v(i), w(i));
            E(:, j, i) = Jac \ E(:, j, i);
        end
    end

    for j = 1:4
        vi = verts(j);
        for i = 1:20
            Ex(vi) = Ex(vi) + E(1, i, j) * solver.x(map(i));
            Ey(vi) = Ey(vi) + E(2, i, j) * solver.x(map(i));
            Ez(vi) = Ez(vi) + E(3, i, j) * solver.x(map(i));
        end
        nCnt(vi) = nCnt(vi) + 1;
    end
end

mask = nCnt > 0;
Ex(mask) = Ex(mask) ./ nCnt(mask);
Ey(mask) = Ey(mask) ./ nCnt(mask);
Ez(mask) = Ez(mask) ./ nCnt(mask);
normE = sqrt(abs(Ex .* conj(Ex) + Ey .* conj(Ey) + Ez .* conj(Ez)));

end
