added function to calculate free energy