  In this paper we show how the Smoothed Particle Hydrodynamics (SPH) equations
for ideal magnetohydrodynamics (MHD) can be written in conservation form with
the positivity of the dissipation guaranteed. We call the resulting algorithm
Smoothed Particle Magnetohydrodynamics (SPMHD). The equations appear to be
accurate, robust and easy to apply and do not suffer from the instabilities
known to exist previously in formulations of the SPMHD equations. In addition
we formulate our MHD equations such that errors associated with non-zero
divergence of the magnetic field are naturally propagated by the flow and
should therefore remain small.
  In this and a companion paper (Price and Monaghan 2003b) we present a wide
range of numerical tests in one dimension to show that the algorithm gives very
good results for one dimensional flows in both adiabatic and isothermal MHD.
For the one dimensional tests the field structure is either two or three
dimensional.
  The algorithm has many astrophysical applications and is particularly suited
to star formation problems.
