  We introduce and test a new and highly efficient method for treating the
thermal and radiative effects influencing the energy equation in SPH
simulations of star formation. The method uses the density, temperature and
gravitational potential of each particle to estimate a mean optical depth,
which then regulates the particle's heating and cooling. The method captures --
at minimal computational cost -- the effects of (i) the rotational and
vibrational degrees of freedom of H2, H2 dissociation, H0 ionisation, (ii)
opacity changes due to ice mantle melting, sublimation of dust, molecular
lines, H-, bound-free and free-free processes and electron scattering; (iv)
external irradiation; and (v) thermal inertia. The new algorithm reproduces the
results of previous authors and/or known analytic solutions. The computational
cost is comparable to a standard SPH simulation with a simple barotropic
equation of state. The method is easy to implement, can be applied to both
particle- and grid-based codes, and handles optical depths 0<tau<10^{11}.
