def test_mismatched_atom_composition(self): mol1 = Molecule.from_file(os.path.join(test_dir, "benzene1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "t2.xyz")) mm = HungarianOrderMatcher(mol1) with self.assertRaises(ValueError): _, rmsd = mm.fit(mol2)
def test_to_and_from_dict(self): mol1 = Molecule.from_file(os.path.join(test_dir, "t3.xyz")) mm_source = HungarianOrderMatcher(mol1) d_source = mm_source.as_dict() mm_target = HungarianOrderMatcher.from_dict(d_source) self.assertDictEqual(d_source, mm_target.as_dict())
def test_get_rmsd(self): mol1 = Molecule.from_file(os.path.join(test_dir, "t3.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "t4.xyz")) mm = HungarianOrderMatcher(mol1) _, rmsd = mm.fit(mol2) self.assertAlmostEqual(rmsd, 0.002825344731118855, places=6)
def test_rotated_molecule(self): coords = [[0.000000, 0.000000, 0.000000], [0.000000, 0.000000, 1.089000], [1.026719, 0.000000, -0.363000], [-0.513360, -0.889165, -0.363000], [-0.513360, 0.889165, -0.363000]] op = SymmOp.from_origin_axis_angle([0, 0, 0], [0.1, 0.2, 0.3], 60) rotcoords = [op.operate(c) for c in coords] mol1 = Molecule(["C", "H", "H", "H", "H"], coords) mol2 = Molecule(["C", "H", "H", "H", "H"], rotcoords) mm = HungarianOrderMatcher(mol1) _, rmsd = mm.fit(mol2) self.assertAlmostEqual(rmsd, 0., places=6)
def _align_slot(self, slot: Fragment, fragment: Fragment ) -> Tuple[Fragment, float]: """ [summary] Parameters ---------- slot : Fragment [description] fragment : Fragment [description] Returns ------- Tuple[Fragment, float] [description] """ # m0 = slot.atoms.copy() m0 = slot.extract_dummies() m0.perturb(distance=0.01) m0.replace_species({"X": "H"}) m1 = fragment.extract_dummies() m1.perturb(distance=0.01) m1.replace_species({"X": "H"}) _, U, V, rmsd = HungarianOrderMatcher(m0).match(m1) new_coords = fragment.atoms.cart_coords.dot(U) + V fragment.atoms = Molecule(fragment.atoms.species, coords=new_coords) fragment_tags = numpy.array([-1, ] * len(fragment.atoms)) for j, frag_tag in enumerate(fragment_tags): fragment.atoms[j].properties["tags"] = frag_tag slot_tags = slot.atoms.site_properties["tags"] for i, slot_tag in enumerate(slot_tags): tag_by_dist = [] for j in fragment.atoms.indices_from_symbol("X"): d = slot.atoms[i].coords - fragment.atoms[j].coords tag_by_dist.append((numpy.linalg.norm(d), j)) _, best_match = sorted(tag_by_dist)[0] fragment.atoms[best_match].properties["tags"] = slot_tag return fragment, rmsd
def setUpClass(cls): cls.mol1 = Molecule.from_file(os.path.join(test_dir, "Si2O_cluster.xyz")) cls.mm = HungarianOrderMatcher(cls.mol1)
def test_to_and_from_dict(self): d = self.mm.as_dict() mm = HungarianOrderMatcher.from_dict(d) self.assertDictEqual(d, mm.as_dict())
def test_fit(self): mol1 = Molecule.from_file(os.path.join(test_dir, "benzene1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "benzene2.xyz")) mm = HungarianOrderMatcher(mol1) _, rmsd = mm.fit(mol2) self.assertAlmostEqual(rmsd, 1.4171601659148593e-05, places=6) mol1 = Molecule.from_file(os.path.join(test_dir, "c1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "c2.xyz")) mm = HungarianOrderMatcher(mol1) _, rmsd = mm.fit(mol2) self.assertAlmostEqual(rmsd, 9.479012116064961e-05, places=6) mol1 = Molecule.from_file(os.path.join(test_dir, "t3.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "t4.xyz")) mm = HungarianOrderMatcher(mol1) _, rmsd = mm.fit(mol2) self.assertAlmostEqual(rmsd, 0.002825344731118855, places=6) mol1 = Molecule.from_file(os.path.join(test_dir, "j1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "j2.xyz")) mm = HungarianOrderMatcher(mol1) _, rmsd = mm.fit(mol2) self.assertAlmostEqual(rmsd, 9.28245597473488e-05, places=6) mol1 = Molecule.from_file(os.path.join(test_dir, "ethene1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "ethene2.xyz")) mm = HungarianOrderMatcher(mol1) _, rmsd = mm.fit(mol2) self.assertAlmostEqual(rmsd, 0.00021150729609276233, places=6) mol1 = Molecule.from_file(os.path.join(test_dir, "toluene1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "toluene2.xyz")) mm = HungarianOrderMatcher(mol1) _, rmsd = mm.fit(mol2) self.assertAlmostEqual(rmsd, 0.0001445787263551832, places=6) mol1 = Molecule.from_file(os.path.join(test_dir, "cyclohexane1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "cyclohexane2.xyz")) mm = HungarianOrderMatcher(mol1) _, rmsd = mm.fit(mol2) self.assertAlmostEqual(rmsd, 0.00012447269440740117, places=6) mol1 = Molecule.from_file(os.path.join(test_dir, "oxygen1.xyz")) mol2 = Molecule.from_file(os.path.join(test_dir, "oxygen2.xyz")) mm = HungarianOrderMatcher(mol1) _, rmsd = mm.fit(mol2) self.assertAlmostEqual(rmsd, 0., places=6)