Fixed a bug where gradient and energy input files are not properly closed