Simulating bulk viscosity in neutron stars. I. Formalism — arXiv2