A physical model for cosmological simulations of galaxy formation