Structure Preserving Energy Functions in Power Systems: Theory and Applications