add a function to calculate energy