% ex_hodge.m — Hodge decomposition of a tangential vector field. % % Any tangential vector field f on a surface splits into curl-free, % divergence-free, and harmonic parts: % % f = grad(u) + n x grad(v) + w. % % hodge() computes this by solving two Laplace-Beltrami problems. We use a % modest torus here so the demo stays quick (the docs use a finer mesh). mip load --install flatironinstitute/flatironinstitute/surfacefun p = 8; nu = 8; nv = 16; dom = surfacemesh.torus(p + 1, nu, nv); % A smooth random tangential vector field. rng(0) bb = boundingbox(dom); gx = randnfun3(10, bb); gy = randnfun3(10, bb); gz = randnfun3(10, bb); g = cross([0 1 1], surfacefunv(@(x, y, z) gx(x, y, z), ... @(x, y, z) gy(x, y, z), ... @(x, y, z) gz(x, y, z), dom)); vn = normal(dom); f = -cross(vn, vn, g); % project g onto the tangent plane % Compute the decomposition. [u, v, w] = hodge(f); figure(1); quiver(f, 0.3, 4) title('Tangential field f'); figure(2); quiver(grad(u), 0.3, 4) title('Curl-free part grad(u)'); figure(3); quiver(cross(vn, grad(v)), 0.3, 4) title('Divergence-free part n x grad(v)'); figure(4); quiver(w, 0.3, 4) title('Harmonic part w'); % The harmonic remainder should be nearly divergence-free. fprintf('|| div(w) || = %.3e\n', norm(div(w)));