  It is well known that cosmic rays (CRs) contribute significantly to the
pressure of the interstellar medium in our own Galaxy, suggesting that they may
play an important role in regulating star formation during the formation and
evolution of galaxies. We here discuss a novel numerical treatment of the
physics of CRs and its implementation in the parallel smoothed particle
hydrodynamics code GADGET-2. In our methodology, the non-thermal CR population
of each gaseous fluid element is approximated by a simple power law spectrum in
particle momentum, characterized by an amplitude, a cut-off, and a fixed slope.
Adiabatic compression, and a number of physical source and sink terms are
modelled which modify the CR pressure of each particle. The most important
sources considered are injection by supernovae and diffusive shock
acceleration, while the primary sinks are thermalization by Coulomb
interactions, and catastrophic losses by hadronic interactions. We also include
diffusion of CRs. Our scheme allows us to carry out the first cosmological
structure formation simulations that self-consistently account for CR physics.
In simulations of isolated galaxies, we find that CRs can significantly reduce
the star formation efficiencies of small galaxies, with virial velocities below
\~80 km/s, an effect that becomes progressively stronger towards low mass
scales. In cosmological simulations at high redshift, the total mass-to-light
ratio of small halos and the faint-end of the luminosity function are strongly
affected. When CR acceleration in shocks is followed as well, up to ~40% of the
energy dissipated at structure formation shocks can appear as CR pressure at
z~3-6, but this fraction drops to ~10% at low redshifts when the shock
distribution becomes increasingly dominated by lower Mach numbers. (abridged)
