implemented energy function