A variational approach to discrete element modelling in rock mechanics