First-principles calculation of local energy and local stress for materials science and engineering