def test_finite_diff_with_water(self): with path("saddle.optimizer.test.data", "water.xyz") as mol_path: mol = Utils.load_file(mol_path) red_int = ReducedInternal(mol.coordinates, mol.numbers, 0, 1) red_int.auto_select_ic() with path("saddle.optimizer.test.data", "water_old.fchk") as fchk_file: red_int.energy_from_fchk(fchk_file) assert red_int.energy - 75.99264142 < 1e-6 wt_p1 = PathPoint(red_int=red_int) step = [0.001, 0, 0] print(wt_p1._instance.vspace) ref_vspace = np.array([ [0.25801783, -0.66522226, 0.70064694], [-0.49526649, -0.71373819, -0.49526649], [-0.82954078, 0.21921937, 0.51361947], ]) # incase different vspace basis error wt_p1._instance.set_vspace(ref_vspace) wt_p2 = wt_p1.copy() wt_p2.update_coordinates_with_delta_v(step) # wt_p2._instance.create_gauss_input(title='water_new') with path("saddle.optimizer.test.data", "water_new.fchk") as fchk_file_new: wt_p2._instance.energy_from_fchk(fchk_file_new) wt_p2._instance.align_vspace(wt_p1._instance) assert np.allclose(wt_p1.vspace, wt_p2.vspace) result = PathPoint._calculate_finite_diff_h(wt_p1, wt_p2, 0.001) assert np.allclose(result, wt_p1._instance.v_hessian[:, 0], atol=1e-2)
def test_finite_diff_with_water_2(self): with path("saddle.optimizer.test.data", "water.xyz") as mol_path: mol = Utils.load_file(mol_path) red_int = ReducedInternal(mol.coordinates, mol.numbers, 0, 1) red_int.auto_select_ic() with path("saddle.optimizer.test.data", "water_old.fchk") as fchk_file: red_int.energy_from_fchk(fchk_file) assert red_int.energy - 75.99264142 < 1e-6 red_int.select_key_ic(0) ref_v = np.array([ [-1.00000000e00, -4.17292908e-16, 0.00000000e00], [2.10951257e-16, -4.69422035e-01, -8.82973926e-01], [3.39185671e-16, -8.82973926e-01, 4.69422035e-01], ]) ref_v2 = np.dot(ref_v, ref_v.T) assert np.allclose(ref_v2, np.dot(red_int.vspace, red_int.vspace.T)) red_int.set_vspace(ref_v) wt_p1 = PathPoint(red_int=red_int) step = [-0.001, 0, 0] wt_p2 = wt_p1.copy() wt_p2.update_coordinates_with_delta_v(step) # fchk file is for -0.001 with path("saddle.optimizer.test.data", "water_new_2.fchk") as fchk_file_new: wt_p2._instance.energy_from_fchk(fchk_file_new) wt_p2._instance.align_vspace(wt_p1._instance) assert np.allclose(wt_p1.vspace, wt_p2.vspace) result = PathPoint._calculate_finite_diff_h(wt_p1, wt_p2, -0.001) assert np.allclose(result, wt_p1._instance.v_hessian[:, 0], atol=1e-2)
def test_finite_different_with_water_3(self): with path("saddle.optimizer.test.data", "water.xyz") as mol_path: mol = Utils.load_file(mol_path) red_int = ReducedInternal(mol.coordinates, mol.numbers, 0, 1) red_int.auto_select_ic() red_int.add_bond(0, 2) with path("saddle.optimizer.test.data", "water_old.fchk") as fchk_file: red_int.energy_from_fchk(fchk_file) assert red_int.energy - 75.99264142 < 1e-6 red_int.select_key_ic(0) wt_p1 = PathPoint(red_int=red_int) step = [0.001, 0, 0] wt_p2 = wt_p1.copy() wt_p2.update_coordinates_with_delta_v(step) with path("saddle.optimizer.test.data", "water_new_3.fchk") as fchk_file_new: wt_p2._instance.energy_from_fchk(fchk_file_new) wt_p2._instance.align_vspace(wt_p1._instance) assert np.allclose(wt_p1.vspace, wt_p2.vspace, atol=1e-2) result = PathPoint._calculate_finite_diff_h(wt_p1, wt_p2, 0.001) assert np.allclose(result, wt_p1._instance.v_hessian[:, 0], atol=1e-2)
class TestSecant(unittest.TestCase): def setUp(self): with path("saddle.test.data", "water.xyz") as mol_path: mol = Utils.load_file(mol_path) self.old_ob = ReducedInternal(mol.coordinates, mol.numbers, 0, 1) self.old_ob.add_bond(0, 1) self.old_ob.add_bond(1, 2) self.old_ob.add_angle(0, 1, 2) with path("saddle.optimizer.test.data", "water_old.fchk") as fchk_file1: self.old_ob.energy_from_fchk(fchk_file1) self.new_ob = ReducedInternal(mol.coordinates, mol.numbers, 0, 1) self.new_ob.add_bond(0, 1) self.new_ob.add_bond(1, 2) self.new_ob.add_angle(0, 1, 2) with path("saddle.optimizer.test.data", "water_new.fchk") as fchk_file2: self.new_ob.energy_from_fchk(fchk_file2) self.new_ob.align_vspace(self.old_ob) assert_allclose(self.new_ob.vspace, self.old_ob.vspace, atol=1e-6) self.newp = PathPoint(self.new_ob) self.oldp = PathPoint(self.old_ob) def test_secant_condition(self): result = secant(self.newp, self.oldp) # separate calculation part1 = self.newp.v_gradient - self.oldp.v_gradient part2 = np.dot(self.newp.vspace.T, np.linalg.pinv(self.newp.b_matrix.T)) part3 = np.dot( self.newp.b_matrix.T, np.dot((self.newp.vspace - self.oldp.vspace), self.newp.v_gradient), ) part4 = np.dot((self.newp.b_matrix - self.oldp.b_matrix).T, self.newp.q_gradient) final = part1 - np.dot(part2, (part3 + part4)) assert np.allclose(result, final, atol=1e-6) def test_secant_condition_0(self): d_s = np.dot(self.old_ob.vspace.T, self.new_ob.ic_values - self.old_ob.ic_values) ref = np.dot(self.old_ob.v_hessian, d_s) result = secant(self.newp, self.oldp) assert_allclose(ref, result, atol=1e-5) def test_secant_condition_1(self): d_s = np.dot(self.old_ob.vspace.T, self.new_ob.ic_values - self.old_ob.ic_values) ref = np.dot(self.old_ob.v_hessian, d_s) result = secant_1(self.newp, self.oldp) assert_allclose(ref, result, atol=1e-5) def test_secant_condition_2(self): d_s = np.dot(self.old_ob.vspace.T, self.new_ob.ic_values - self.old_ob.ic_values) ref = np.dot(self.old_ob.v_hessian, d_s) result = secant_2(self.newp, self.oldp) assert_allclose(ref, result, atol=1e-5) def test_secant_condition_3(self): d_s = np.dot(self.old_ob.vspace.T, self.new_ob.ic_values - self.old_ob.ic_values) ref = np.dot(self.old_ob.v_hessian, d_s) result = secant_3(self.newp, self.oldp) assert_allclose(ref, result, atol=1e-5)