@@ -73,6 +73,66 @@ def IsSpaceGroupIdentifier(sgid):
7373 return rv
7474
7575
76+ def FindSpaceGroup (symops , shuffle = False ):
77+ """Lookup SpaceGroup from a given list of symmetry operations.
78+
79+ Parameters
80+ ----------
81+ symops : list
82+ The list of `SymOp` objects for which to find SpaceGroup.
83+ shuffle : bool, optional
84+ Flag for allowing different order of symops in the returned
85+ SpaceGroup. The default is ``False``.
86+
87+ Returns
88+ -------
89+ SpaceGroup
90+ The SpaceGroup object with equivalent list of symmetry
91+ operations. Return predefined SpaceGroup instance when
92+ symmetry operations have the same order or when the
93+ `shuffle` flag is set.
94+
95+ Raises
96+ ------
97+ ValueError
98+ When `symops` do not match any known SpaceGroup.
99+ """
100+ import copy
101+ from six .moves import zip_longest
102+ tb = _getSGHashLookupTable ()
103+ hh = _hashSymOpList (symops )
104+ if not hh in tb :
105+ raise ValueError ('Cannot find SpaceGroup for the specified symops.' )
106+ rv = tb [hh ]
107+ if not shuffle :
108+ zz = zip_longest (rv .iter_symops (), symops , fillvalue = '' )
109+ sameorder = all (str (o0 ) == str (o1 ) for o0 , o1 in zz )
110+ if not sameorder :
111+ rv = copy .copy (rv )
112+ rv .symop_list = symops
113+ return rv
114+
115+
116+ def _hashSymOpList (symops ):
117+ """Return hash value for a sequence of `SymOp` objects.
118+
119+ The symops are sorted so the results is independent of symops order.
120+
121+ Parameters
122+ ----------
123+ symops : sequence
124+ The sequence of `SymOp` objects to be hashed
125+
126+ Returns
127+ -------
128+ int
129+ The hash value.
130+ """
131+ ssop = sorted (str (o ) for o in symops )
132+ rv = hash (tuple (ssop ))
133+ return rv
134+
135+
76136def _buildSGLookupTable ():
77137 """Rebuild space group lookup table from the SpaceGroupList data.
78138
@@ -116,6 +176,19 @@ def _buildSGLookupTable():
116176 return
117177_sg_lookup_table = {}
118178
179+
180+ def _getSGHashLookupTable ():
181+ """Return lookup table of symop hashes to standard SpaceGroup objects.
182+ """
183+ if _sg_hash_lookup_table :
184+ return _sg_hash_lookup_table
185+ for sg in SpaceGroupList :
186+ h = _hashSymOpList (sg .symop_list )
187+ _sg_hash_lookup_table [h ] = sg
188+ assert len (_sg_hash_lookup_table ) == len (SpaceGroupList )
189+ return _getSGHashLookupTable ()
190+ _sg_hash_lookup_table = {}
191+
119192# Import SpaceGroup objects --------------------------------------------------
120193
121194from diffpy .structure .spacegroupmod import (
0 commit comments