We present a constrained formulation of Dedner et al's hyperbolic/parabolic
divergence cleaning scheme for enforcing the \nabla\dot B = 0 constraint in
Smoothed Particle Magnetohydrodynamics (SPMHD) simulations. The constraint we
impose is that energy removed must either be conserved or dissipated, such that
the scheme is guaranteed to decrease the overall magnetic energy. This is shown
to require use of conjugate numerical operators for evaluating \nabla\dot B and
\nabla{\psi} in the SPMHD cleaning equations. The resulting scheme is shown to
be stable at density jumps and free boundaries, in contrast to an earlier
implementation by Price & Monaghan (2005). Optimal values of the damping
parameter are found to be {\sigma} = 0.2-0.3 in 2D and {\sigma} = 0.8-1.2 in
3D. With these parameters, our constrained Hamiltonian formulation is found to
provide an effective means of enforcing the divergence constraint in SPMHD,
typically maintaining average values of h |\nabla\dot B| / |B| to 0.1-1%, up to
an order of magnitude better than artificial resistivity without the associated
dissipation in the physical field. Furthermore, when applied to realistic, 3D
simulations we find an improvement of up to two orders of magnitude in momentum
conservation with a corresponding improvement in numerical stability at
essentially zero additional computational expense.Comment: 28 pages, 25 figures, accepted to J. Comput. Phys. Movies at
http://www.youtube.com/playlist?list=PL215D649FD0BDA466 v2: fixed inverted
figs 1,4,6, and several color bar