def setUp(self): """Setup function.""" with path("saddle.test.data", "water.xyz") as mol_path: mol = Utils.load_file(mol_path) self.mol = Internal(mol.coordinates, mol.numbers, 0, 1) with path("saddle.test.data", "h2o2.xyz") as mol_path: mol2 = Utils.load_file(mol_path) self.h2o2 = Internal(mol2.coordinates, mol2.numbers, 0, 1)
def setup_class(self): with path("saddle.test.data", "methanol.xyz") as file_path: mol = Utils.load_file(file_path) self.molecule = mol with path("saddle.test.data", "h2o2.xyz") as file_path2: mol2 = Utils.load_file(file_path2) self.h2o2 = mol2
def setUp(self): with path("saddle.test.data", "ch3_hf.xyz") as rct_path: self.rct = Utils.load_file(rct_path) with path("saddle.test.data", "ch3f_h.xyz") as prd_path: self.prd = Utils.load_file(prd_path) self.reactant_ic = Internal(self.rct.coordinates, self.rct.numbers, 0, 2) self.product_ic = Internal(self.prd.coordinates, self.prd.numbers, 0, 2)
def save_to(self, filename, mode="w"): """Save current coordinates of molecule into a .xzy file. Parameters ---------- filename : str File name of the saved file mode : str, optional I/O mode of file """ Utils.save_file(filename, self, mode=mode)
def test_auto_dihed_number_ethane(self): with path("saddle.test.data", "ethane.xyz") as mol_path: mol = Utils.load_file(mol_path) mol = Utils.load_file(mol_path) ethane = Internal(mol.coordinates, mol.numbers, 0, 1) ethane.auto_select_ic() counter = 0 for ic in ethane.ic: if isinstance(ic, DihedralAngle): counter += 1 assert counter == 5
def test_new_dihed_converge(self): with path("saddle.test.data", "h2o2.xyz") as mol_path: mol = Utils.load_file(mol_path) mol = Utils.load_file(mol_path) h2o2 = Internal(mol.coordinates, mol.numbers, 0, 1) h2o2.auto_select_ic(dihed_special=True) assert len(h2o2.ic) == 7 # print(h2o2.ic_values) target_ic = [2.4, 1.8, 1.8, 1.6, 1.6, 0.8, 0.6] h2o2.set_target_ic(target_ic) h2o2.converge_to_target_ic() # print(h2o2.ic_values) assert_allclose(h2o2.ic_values, target_ic, atol=1e-2)
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_get_delta_v(self): with path("saddle.test.data", "water.xyz") as mol_path: mol = Utils.load_file(mol_path) ri_mol = ReducedInternal(mol.coordinates, mol.numbers, 0, 1) ri_mol.add_bond(1, 0) ri_mol.add_bond(1, 2) ri_mol.add_angle(0, 1, 2) ri_mol.set_key_ic_number(1) vp_ref = np.array( [ [-1.00000000e00, 1.52461867e-16, -2.25191203e-16], [-1.57750703e-16, 3.51986473e-01, 9.36005087e-01], [-1.73472348e-16, -9.36005087e-01, 3.51986473e-01], ] ) ri_mol.set_vspace(vp_ref) print(ri_mol.vspace) assert np.allclose(ri_mol.vspace, vp_ref) v_change = np.array([-0.2, 0.07039729, 0.18720102]) ic_change = np.dot(ri_mol.vspace, v_change) assert np.allclose(ic_change, np.array([0.2, 0.2, 0.0])) ri_mol.update_to_new_structure_with_delta_v(v_change) assert np.allclose( ri_mol.ic_values, np.array([2.01413724, 2.01413724, 1.9106340176]) )
def test_ric_add_ic(self): with path("saddle.test.data", "water.xyz") as mol_path: mol = Utils.load_file(mol_path) ri_mol = Internal(mol.coordinates, mol.numbers, 0, 1) ri_mol = ReducedInternal.update_to_reduced_internal(ri_mol) ri_mol.add_bond(1, 0) ri_mol.add_bond(1, 2) ri_mol.add_bond(0, 2) ri_mol.vspace ri_mol.add_angle(0, 1, 2) ri_mol.add_angle(1, 0, 2) ri_mol.select_key_ic(0, 2) vp_ref = np.array( [ [4.40930006e-01, -7.79280781e-01, 6.12072474e-17], [-5.61408260e-01, -6.12047068e-01, -3.33066907e-16], [-4.57920570e-01, -3.28254718e-02, -7.16200549e-01], [-5.22760813e-01, 4.87258745e-02, 5.58315734e-01], [8.62054537e-02, 1.21112047e-01, -4.18736570e-01], ] ) for i in range(vp_ref.shape[1]): assert np.allclose(ri_mol.vspace[:, i], vp_ref[:, i]) or np.allclose( ri_mol.vspace[:, i], -1 * vp_ref[:, i] ) ri_mol.set_key_ic_number(1) assert ri_mol._red_space is None assert ri_mol._non_red_space is None
def test_auto_select_improper_ch3_hf(self): with path("saddle.test.data", "ch3_hf.xyz") as mol_path: mol = Utils.load_file(mol_path) mol = Internal(mol.coordinates, mol.numbers, 0, 1) mol.auto_select_ic() ic_ref = np.array([ 2.02762919, 2.02769736, 2.02761705, 1.77505755, 4.27707385, 4.87406146, 2.08356856, 2.08391343, 1.64995596, 2.08364916, 1.64984524, 1.64881837, 1.06512165, 0.42765264, 3.14154596, 2.71390135, 0.59485389, -1.70630517, 1.7061358, -3.14152957, 2.09455878, -2.09427619, -2.87079827, ]) assert_allclose(mol.ic_values, ic_ref)
def test_create_input_file(self): self.gwob.title = "test_untitled" input_file = self.gwob._create_input_file(0, 1) filepath = WORK_DIR / (input_file + ".com") mol = Utils.load_file(filepath) self.file_list.append(filepath) assert np.allclose(self.gwob.molecule.coordinates, mol.coordinates)
def test_align_v_space(self): with path("saddle.test.data", "water.xyz") as mol_path: mol = Utils.load_file(mol_path) mol_1 = ReducedInternal(mol.coordinates, mol.numbers, 0, 1) mol_1.add_bond(1, 0) mol_1.add_bond(1, 2) mol_1.add_angle(0, 1, 2) mol_1.set_key_ic_number(1) mol_2 = deepcopy(mol_1) mol_1.set_target_ic([2.0, 2.0, 2.0]) mol_1.converge_to_target_ic() assert np.allclose(mol_1.ic_values, [2.0, 2.0, 2.0], atol=1e-4) copy1 = deepcopy(mol_1) copy2 = deepcopy(mol_2) copy1.align_vspace(copy2) assert np.allclose(copy1.vspace, mol_2.vspace) with path("saddle.test.data", "water_1.fchk") as fchk_path: # print 'cv2',copy2.vspace copy2.energy_from_fchk(fchk_path) # print 'cv2, new',copy2.vspace, copy2.vspace_gradient ref_ic_gradient = np.dot(copy2.vspace, copy2.vspace_gradient) # print 'cv2,energy' copy2.align_vspace(copy1) # print 'cv2', copy2.vspace, copy2.vspace_gradient new_ic_gradient = np.dot(copy2.vspace, copy2.vspace_gradient) assert np.allclose(ref_ic_gradient, new_ic_gradient) assert np.allclose(copy1.vspace, copy2.vspace)
def test_create_input_gjf(self): self.gwob.create_gauss_input( 0, 1, spe_title="test_2nd_gauss", path=self.test_path, postfix=".gjf" ) filepath = self.test_path / "test_2nd_gauss.gjf" self.file_list.append(filepath) mol = Utils.load_file(filepath) assert np.allclose(self.gwob.molecule.coordinates, mol.coordinates)
def ts_to_file(self, filename=""): """Save the ts structure to xyz file. Parameters ---------- filename : str, optional filename of stored file Raises ------ InvalidArgumentError the input filename is not valid. """ if filename: Utils.save_file(filename, self.ts) else: raise InvalidArgumentError("Invalid empty filename")
def setUp(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.add_bond(0, 1) red_int.add_bond(1, 2) red_int.add_angle(0, 1, 2) self.mol = red_int
def test_save_xyz(self): with path("saddle.test.data", "water.xyz") as file_path: water_mol = Utils.load_file(file_path) with path("saddle.test.data", "") as file_path: new_file_name = file_path / "test_base_mole_test_file" Utils.save_file(new_file_name, water_mol) new_add_file = new_file_name.parent / (new_file_name.name + ".xyz") TestUtils.file_list.append(new_add_file) with path("saddle.test.data", "test_base_mole_test_file.xyz") as file_path: mol = Utils.load_file(file_path) ref_coor = np.array([ [1.481237149, -0.93019116, 0.0], [0.0, 0.11720080, 0], [-1.481237149, -0.93019116, 0.0], ]) assert np.allclose(mol.coordinates, ref_coor) assert np.allclose(mol.numbers, [1, 8, 1])
def setUp(self): with path("saddle.test.data", "water.xyz") as mol_path: mol = Utils.load_file(mol_path) self.red_int = ReducedInternal(mol.coordinates, mol.numbers, 0, 1, "water") self.red_int.add_bond(1, 0) self.red_int.add_bond(1, 2) self.red_int.add_bond(0, 2) self.red_int.add_angle(0, 1, 2) self.red_int.add_angle(1, 0, 2) self.red_int.set_key_ic_number(2)
def test_set_new_vspace(self): with path("saddle.test.data", "water.xyz") as mol_path: mol = Utils.load_file(mol_path) ri_mol = ReducedInternal(mol.coordinates, mol.numbers, 0, 1) ri_mol.add_bond(1, 0) ri_mol.add_bond(1, 2) ri_mol.add_angle(0, 1, 2) ri_mol.set_key_ic_number(1) new_vp = np.eye(3) ri_mol.set_vspace(new_vp) assert np.allclose(ri_mol.vspace, np.eye(3))
def test_load_xyz(self): with path("saddle.test.data", "water.xyz") as file_path: nums, coors, title = Utils._load_xyz(file_path) assert np.allclose(nums, np.array([1, 8, 1])) ref_coor = np.array([ [1.481237149, -0.93019116, 0.0], [0.0, 0.11720080, 0], [-1.481237149, -0.93019116, 0.0], ]) assert np.allclose(coors, ref_coor) assert title == "water"
def test_delete_ic(self): with path("saddle.test.data", "ethane.xyz") as mol_path: mol = Utils.load_file(mol_path) ethane = Internal(mol.coordinates, mol.numbers, 0, 1) ethane.auto_select_ic() ethane._delete_ic_index(0) assert len(ethane.ic) == 23 ethane.auto_select_ic(keep_bond=True) assert len(ethane.ic) == 12 # print(ethane.ic) ethane.delete_ic(1, 2, 3) assert len(ethane.ic) == 9
def test_select_key_ic(self): with path("saddle.test.data", "water.xyz") as mol_path: mol = Utils.load_file(mol_path) mol_1 = ReducedInternal(mol.coordinates, mol.numbers, 0, 1) mol_1.add_bond(1, 0) mol_1.add_bond(1, 2) mol_1.add_angle(0, 1, 2) mol_1.select_key_ic(0) assert mol_1.key_ic_number == 1 mol_1.select_key_ic(2) assert mol_1.key_ic_number == 1 mol_1.select_key_ic(0, 1, 2) assert mol_1.key_ic_number == 3
def test_dihedral_add(self): """Test add normal dihedral.""" with path("saddle.test.data", "2h-azirine.xyz") as mol_path: mol = Utils.load_file(mol_path) # create a water molecule internal = Internal(mol.coordinates, mol.numbers, 0, 1) internal.add_bond(0, 1) internal.add_bond(1, 2) internal.add_bond(1, 3) # fake add dihed internal.add_dihedral(0, 2, 3, 4) assert len(internal.ic) == 3 internal.add_dihedral(0, 1, 2, 3) assert len(internal.ic) == 4 assert internal.ic_values[3] == dihed_angle(internal.coordinates[:4])
def test_dihedral_repeak(self): with path("saddle.test.data", "h2o2.xyz") as mol_path: mol = Utils.load_file(mol_path) h2o2 = Internal(mol.coordinates, mol.numbers, 0, 1) h2o2.add_bond(0, 1) h2o2.add_bond(1, 2) h2o2.add_bond(2, 3) h2o2.add_bond(3, 2) h2o2.add_bond(0, 2) h2o2.add_bond(1, 3) assert len(h2o2.ic) == 5 h2o2.add_dihedral(0, 1, 2, 3) assert len(h2o2.ic) == 6 h2o2.add_dihedral(0, 2, 1, 3) assert len(h2o2.ic) == 6
def test_fragments_in_mole(self): with path("saddle.test.data", "ch3_hf.xyz") as mol_path: mol = Utils.load_file(mol_path) mol = Internal(mol.coordinates, mol.numbers, 0, 1) assert len(mol.fragments) == mol.natom mol.add_bond(0, 1) mol.add_bond(2, 3) # print(mol.fragments) assert len(mol.fragments) == mol.natom - 2 mol.add_bond(0, 2) assert len(mol.fragments) == mol.natom - 3 mol.add_bond(0, 3) assert len(mol.fragments) == mol.natom - 3 mol.add_bond(4, 5) assert len(mol.fragments) == mol.natom - 4 mol.add_bond(0, 5, b_type=3) assert len(mol.fragments) == 2
def test_new_dihed(self): with path("saddle.test.data", "h2o2.xyz") as mol_path: mol = Utils.load_file(mol_path) h2o2 = Internal(mol.coordinates, mol.numbers, 0, 1) h2o2.add_bond(0, 1) h2o2.add_bond(1, 2) h2o2.add_bond(2, 3) assert len(h2o2.ic) == 3 h2o2.add_dihedral(0, 1, 2, 3, special=True) assert len(h2o2.ic) == 5 assert h2o2.b_matrix.shape == (5, 12) h2o2.add_dihedral(3, 1, 2, 0) assert len(h2o2.ic) == 5 h2o2.add_dihedral(3, 2, 1, 0, special=True) assert len(h2o2.ic) == 5 ref_b = h2o2.b_matrix.copy() h2o2._regenerate_ic() assert_allclose(h2o2.b_matrix, ref_b)
def from_file( cls, filename: str, charge: int = 0, multi: int = 1, title="" ) -> "Cartesian": """Create an Cartesian instance from file .xyz, .com, .gjf or .fchk. Arguments --------- filename : str the path of the file charge : int, default is 0 the charge of the given molecule(system) multi : int, dufault is 1 the multiplicity of the given molecule(system) Return ------ new Cartesian instance : Cartesian """ mol = Utils.load_file(filename) return cls(mol.coordinates, mol.numbers, charge, multi, title=title)
def test_main_function(self): """Test the main function and use case for procrustes.""" # file_path = resource_filename( # Requirement.parse('saddle'), 'data/water.xyz') with path("saddle.procrustes.test.data", "water.xyz") as file_path: water = Utils.load_file(file_path) water.coordinates = np.array([[0, 1, 0], [1, 0, 0], [-1, -1, 1]]) water_2 = deepcopy(water) water_2.coordinates = np.array([[-1, 0, 0], [0, 1, 0], [1, -1, 0]]) water_2.coordinates += 1 water_3 = deepcopy(water) water_3.coordinates = np.array([[1, 0, 0], [0, -1, 0], [-1, 1, 0]]) water_3.coordinates -= 1 pcs = Procrustes(water, water_2, water_3) final_xyz = pcs.rotate_mols() assert isinstance(final_xyz, Iterable) assert len(list(final_xyz)) == 2 for i in final_xyz: assert np.allclose(i, water.coordinates)
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)
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)