Skip to content

Commit ea8b784

Browse files
authored
Merge pull request #1894 from ReactionMechanismGenerator/isomorphismfix
Find and fix a bug in Molecule isomorphism
2 parents cc9bb3b + bcd515c commit ea8b784

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

rmgpy/molecule/isomorphismTest.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,49 @@ def test_isotope_subgraph_isomorphism_molecule_and_group():
485485
assert_true(methanei.is_subgraph_isomorphic(group_methane))
486486
assert_true(methane.is_subgraph_isomorphic(group_methane))
487487
assert_false(methanei.is_isomorphic(methane))
488+
489+
def test_isomorphism_wrong_mapping():
490+
"""
491+
Checks isomorphism finds things not isomorphic if given a wrong mapping.
492+
"""
493+
# These molecules are not isomorphic
494+
n1butane = Molecule().from_adjacency_list("""
495+
1 *1 X u0 p0 c0 {2,S}
496+
2 *2 C u0 p0 c0 {1,S} {3,S} {5,S} {6,S}
497+
3 *3 C u0 p0 c0 {2,S} {4,S} {7,S} {8,S}
498+
4 *4 H u0 p0 c0 {3,S}
499+
5 H u0 p0 c0 {2,S}
500+
6 H u0 p0 c0 {2,S}
501+
7 H u0 p0 c0 {3,S}
502+
8 C u0 p0 c0 {3,S} {9,S} {10,S} {11,S}
503+
9 H u0 p0 c0 {8,S}
504+
10 H u0 p0 c0 {8,S}
505+
11 C u0 p0 c0 {8,S} {12,S} {13,S} {14,S}
506+
12 H u0 p0 c0 {11,S}
507+
13 H u0 p0 c0 {11,S}
508+
14 H u0 p0 c0 {11,S}
509+
""")
510+
511+
n2butane = Molecule().from_adjacency_list("""
512+
1 *1 X u0 p0 c0 {3,S}
513+
2 *2 C u0 p0 c0 {3,S} {5,S} {6,S} {4,S}
514+
3 *3 C u0 p0 c0 {2,S} {1,S} {7,S} {8,S}
515+
4 *4 H u0 p0 c0 {2,S}
516+
5 H u0 p0 c0 {2,S}
517+
6 H u0 p0 c0 {2,S}
518+
7 H u0 p0 c0 {3,S}
519+
8 C u0 p0 c0 {3,S} {9,S} {10,S} {11,S}
520+
9 H u0 p0 c0 {8,S}
521+
10 H u0 p0 c0 {8,S}
522+
11 C u0 p0 c0 {8,S} {12,S} {13,S} {14,S}
523+
12 H u0 p0 c0 {11,S}
524+
13 H u0 p0 c0 {11,S}
525+
14 H u0 p0 c0 {11,S}
526+
""")
527+
assert_false(n1butane.is_isomorphic(n2butane))
528+
assert_false(n1butane.is_isomorphic(n2butane, generate_initial_map=True))
529+
530+
mapping = {}
531+
for label in ['*1', '*2', '*3', '*4']:
532+
mapping[n1butane.get_labeled_atoms(label)[0]] = n2butane.get_labeled_atoms(label)[0]
533+
assert_false(n1butane.is_isomorphic(n2butane, initial_map=mapping))

rmgpy/molecule/molecule.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,11 @@ def is_isomorphic(self, other, initial_map=None, generate_initial_map=False, sav
14521452
# check multiplicity
14531453
if self.multiplicity != other.multiplicity:
14541454
return False
1455+
1456+
# if given an initial map, ensure that it's valid.
1457+
if initial_map:
1458+
if not self.is_mapping_valid(other, initial_map, equivalent=True):
1459+
return False
14551460

14561461
if generate_initial_map:
14571462
initial_map = dict()

0 commit comments

Comments
 (0)