A first-principles investigation of the transition between two- and three-dimensional thermal transport in graphene and graphite