Mechanical properties of granular materials: A variational approach to grain‐scale simulations