  We construct a structure-preserving finite element method and time-stepping
scheme for compressible barotropic magnetohydrodynamics (MHD) both in the ideal
and resistive cases, and in the presence of viscosity. The method is deduced
from the geometric variational formulation of the equations. It preserves the
balance laws governing the evolution of total energy and magnetic helicity, and
preserves mass and the constraint $ \operatorname{div}B = 0$ to machine
precision, both at the spatially and temporally discrete levels. In particular,
conservation of energy and magnetic helicity hold at the discrete levels in the
ideal case. It is observed that cross helicity is well conserved in our
simulation in the ideal case.
