Micromechanics of sediments: a variational approach to grain-scale simulations