Fixes and debugging for gradient calculation in global minimization. Discovered a new bug for the magnetic model; the energies are incorrect.