source: trunk/GSASIIspc.py @ 230

Last change on this file since 230 was 230, checked in by vondreele, 13 years ago

add svn keywords & add log intensity plotting

  • Property svn:keywords set to Date Author Revision URL Id
File size: 51.8 KB
Line 
1"GSASII - Space group interpretion routines"
2
3########### SVN repository information ###################
4# $Date: 2011-01-07 19:27:24 +0000 (Fri, 07 Jan 2011) $
5# $Author: vondreele $
6# $Revision: 230 $
7# $URL: trunk/GSASIIspc.py $
8# $Id: GSASIIspc.py 230 2011-01-07 19:27:24Z vondreele $
9########### SVN repository information ###################
10import numpy as np
11import numpy.ma as ma
12import numpy.linalg as nl
13import math
14import sys
15import os.path as ospath
16
17import GSASIIpath
18import pyspg
19
20def SpcGroup(SGSymbol):
21    '''
22    Determines cell and symmetry information from a short H-M space group name
23    input:
24        SGSymbol - space group symbol (string) with spaces between axial fields
25    returns:
26        SGError = 0 for no errors; >0 for errors (see SGErrors below for details)
27        SGData - dictionary with entries:
28             'SpGrp': space group symbol slightly cleaned up
29             'Laue':  one of '-1','2/m','mmm','4/m','4/mmm','3R','3mR','3',
30                      '3m1','31m','6/m','6/mmm','m3','m3m'
31             'SGInv': boolean; True if centrosymmetric, False if not
32             'SGLatt': one of 'P','A','B','C','I','F','R'
33             'SGUniq': one of 'a','b','c' if monoclinic, '' otherwise
34             'SGCen': cell centering vectors [0,0,0] at least
35             'SGOps': symmetry operations as [M,T] so that M*x+T = x'
36             'SGSys': one of 'triclinic','monoclinic','orthorhombic','tetragonal','rhombohedral','trigonal','hexagonal','cubic'
37       '''
38    LaueSym = ('-1','2/m','mmm','4/m','4/mmm','3R','3mR','3','3m1','31m','6/m','6/mmm','m3','m3m')
39    LattSym = ('P','A','B','C','I','F','R')
40    UniqSym = ('','','a','b','c','',)
41    SysSym = ('triclinic','monoclinic','orthorhombic','tetragonal','rhombohedral','trigonal','hexagonal','cubic')
42    SGData = {}
43    SGData['SpGrp'] = SGSymbol.strip().lower().capitalize()
44    SGInfo = pyspg.sgforpy(SGSymbol)
45    SGData['SGLaue'] = LaueSym[SGInfo[0]-1]
46    SGData['SGInv'] = bool(SGInfo[1])
47    SGData['SGLatt'] = LattSym[SGInfo[2]-1]
48    SGData['SGUniq'] = UniqSym[SGInfo[3]+1]
49    if SGData['SGLatt'] == 'P':
50        SGData['SGCen'] = np.array(([0,0,0],))
51    elif SGData['SGLatt'] == 'A':
52        SGData['SGCen'] = np.array(([0,0,0],[0,.5,.5]))
53    elif SGData['SGLatt'] == 'B':
54        SGData['SGCen'] = np.array(([0,0,0],[.5,0,.5]))
55    elif SGData['SGLatt'] == 'C':
56        SGData['SGCen'] = np.array(([0,0,0],[.5,.5,0,]))
57    elif SGData['SGLatt'] == 'I':
58        SGData['SGCen'] = np.array(([0,0,0],[.5,.5,.5]))
59    elif SGData['SGLatt'] == 'F':
60        SGData['SGCen'] = np.array(([0,0,0],[0,.5,.5],[.5,0,.5],[.5,.5,0,]))
61    elif SGData['SGLatt'] == 'R':
62        SGData['SGCen'] = np.array(([0,0,0],[1./3.,2./3.,2./3.],[2./3.,1./3.,1./3.]))
63    SGData['SGOps'] = []
64    for i in range(SGInfo[5]):
65        Mat = np.array(SGInfo[6][i])
66        Trns = np.array(SGInfo[7][i])
67        SGData['SGOps'].append([Mat,Trns])
68    if SGData['SGLaue'] in '-1':
69        SGData['SGSys'] = SysSym[0]
70    elif SGData['SGLaue'] in '2/m':
71        SGData['SGSys'] = SysSym[1]
72    elif SGData['SGLaue'] in 'mmm':
73        SGData['SGSys'] = SysSym[2]
74    elif SGData['SGLaue'] in ['4/m','4/mmm']:
75        SGData['SGSys'] = SysSym[3]
76    elif SGData['SGLaue'] in ['3R','3mR']:
77        SGData['SGSys'] = SysSym[4]
78    elif SGData['SGLaue'] in ['3','3m1','31m']:
79        SGData['SGSys'] = SysSym[5]
80    elif SGData['SGLaue'] in ['6/m','6/mmm']:
81        SGData['SGSys'] = SysSym[6]
82    elif SGData['SGLaue'] in ['m3','m3m']:
83        SGData['SGSys'] = SysSym[7]
84    return SGInfo[8],SGData
85
86def SGErrors(IErr):
87    '''
88    Interprets the error message code from SpcGroup. Used in SpaceGroup.
89    input:
90        SGError - from SpcGroup
91    returns:
92        ErrString - a string with the error message or "Unknown error"
93    '''
94
95    ErrString = [' ',
96        'Less than 2 operator fields were found',
97        'Illegal Lattice type, not P, A, B, C, I, F or R',
98        'Rhombohedral lattice requires a 3-axis',
99        'Minus sign does not preceed 1, 2, 3, 4 or 6',
100        'Either a 5-axis anywhere or a 3-axis in field not allowed',
101        ' ',
102        'I for COMPUTED GO TO out of range.',
103        'An a-glide mirror normal to A not allowed',
104        'A b-glide mirror normal to B not allowed',
105        'A c-glide mirror normal to C not allowed',
106        'D-glide in a primitive lattice not allowed',
107        'A 4-axis not allowed in the 2nd operator field',
108        'A 6-axis not allowed in the 2nd operator field',
109        'More than 24 matrices needed to define group',
110        ' ',
111        'Improper construction of a rotation operator',
112        'Mirror following a / not allowed',
113        'A translation conflict between operators',
114        'The 2bar operator is not allowed',
115        '3 fields are legal only in R & m3 cubic groups',
116        'Syntax error. Expected I -4 3 d at this point',
117        ' ',
118        'A or B centered tetragonal not allowed',
119        ' ','unknown error in sgroup',' ',' ',' ',
120        'Illegal character in the space group symbol',
121        ]
122    try:
123        return ErrString[IErr]
124    except:
125        return "Unknown error"
126   
127def SGPrint(SGData):
128    '''
129    Print the output of SpcGroup in a nicely formatted way. Used in SpaceGroup
130    input:
131        SGData - from SpcGroup
132    returns:
133        SGText - list of strings with the space group details
134    '''
135    POL = (' ','x','y','x y','z','x z','y z','xyz','111')
136    Mult = len(SGData['SGCen'])*len(SGData['SGOps'])*(int(SGData['SGInv'])+1)
137    NP = [1,2,4]
138    NPZ = [0,1]
139    for M,T in SGData['SGOps']:
140        for i in range(3):
141            if M[i][i] <= 0.: NP[i] = 0
142        if M[0][2] > 0: NPZ[0] = 8
143        if M[1][2] > 0: NPZ[1] = 0
144    NPol = (NP[0]+NP[1]+NP[2]+NPZ[0]*NPZ[1])*(1-int(SGData['SGInv']))
145    SGText = []
146    SGText.append(' Space Group: '+SGData['SpGrp'])
147    CentStr = 'centrosymmetric'
148    if not SGData['SGInv']:
149        CentStr = 'non'+CentStr
150    if SGData['SGLatt'] in 'ABCIFR':
151        SGText.append(' The lattice is '+CentStr+' '+SGData['SGLatt']+'-centered '+SGData['SGSys'].lower())
152    else:
153        SGText.append(' The lattice is '+CentStr+' '+'primitive '+SGData['SGSys'].lower())       
154    SGText.append(' Multiplicity of a general site is '+str(Mult))
155    SGText.append(' The Laue symmetry is '+SGData['SGLaue'])
156    if SGData['SGUniq'] in ['a','b','c']:
157        SGText.append(' The unique monoclinic axis is '+SGData['SGUniq'])
158    if SGData['SGInv']:
159        SGText.append(' The inversion center is located at 0,0,0')
160    if NPol:
161        SGText.append(' The location of the origin is arbitrary in '+POL[NPol])
162    SGText.append('\n'+' The equivalent positions are:')
163    if SGData['SGLatt'] != 'P':
164        SGText.append('\n ('+Latt2text(SGData['SGLatt'])+')+')
165    if SGData['SGLaue'] in ['-1','2/m','mmm','4/m','4/mmm']:
166        Ncol = 2
167    else:
168        Ncol = 3
169    line = ' '
170    for iop,[M,T] in enumerate(SGData['SGOps']):
171        if iop % Ncol == 0:
172            SGText.append(line)       
173            line = ' '
174        Fld = '(%2i) ' % (iop+1)+MT2text(M,T)+'\t'
175        line += Fld
176    SGText.append(line)       
177    return SGText
178   
179def MT2text(M,T):
180    #From space group matrix/translation operator returns text version
181    XYZ = ('-Z ','-Y ','-X ','X-Y','ERR','Y-X',' X ',' Y ',' Z ','+X ','+Y ','+Z ')
182    TRA = ('   ','ERR','1/6','1/4','1/3','ERR','1/2','ERR','2/3','3/4','5/6','ERR')
183    Fld = ''
184    for j in range(3):
185        IJ = int(round(2*M[j][0]+3*M[j][1]+4*M[j][2]+4)) % 12
186        IK = int(round(T[j]*12)) % 12
187        if IK > 0 and IJ > 4: IJ += 3
188        Fld += TRA[IK]+XYZ[IJ]
189        if j != 2: Fld += ','
190    return Fld
191   
192def Latt2text(Latt):
193    #From lattice type ('P',A', etc.) returns ';' delimited cell centering vectors
194    lattTxt = {'A':'0,0,0; 0,1/2,1/2','B':'0,0,0; 1/2,0,1/2',
195        'C':'0,0,0; 1/2,1/2,0','I':'0,0,0; 1/2,1/2,1/2',
196        'F':'0,0,0; 0,1/2,1/2; 1/2,0,1/2; 1/2,1/2,0',
197        'R':'0,0,0; 1/3,2/3,2/3; 2/3,1/3,1/3','P':'0,0,0'}
198    return lattTxt[Latt]   
199       
200def SpaceGroup(SGSymbol):
201    '''
202    Print the output of SpcGroup in a nicely formatted way.
203    input:
204        SGSymbol - space group symbol (string) with spaces between axial fields
205    returns:
206        nothing
207    '''
208    E,A = SpcGroup(SGSymbol)
209    if E > 0:
210        print SGErrors(E)
211        return
212    for l in SGPrint(A):
213        print l
214
215def MoveToUnitCell(xyz):
216    '''
217    Translates a set of coordinates so that all values are >=0 and < 1
218    input:
219        xyz - a list or numpy array of fractional coordinates
220    returns:
221        XYZ - numpy array of new coordinates inside 0-1
222    '''
223    XYZ = np.zeros(3)
224    for i,x in enumerate(xyz):
225        XYZ[i] = (x-int(x))%1.0
226    return XYZ
227       
228def Opposite(XYZ,toler=0.0002):
229    '''
230    Gives opposite corner, edge or face of unit cell for position within tolerance.
231        Result may be just outside the cell within tolerance
232    input:
233        XYZ: 0 >= np.array[x,y,z] > 1 as by MoveToUnitCell
234        toler: unit cell fraction tolerance making opposite
235    returns:
236        XYZ: array of opposite positions; always contains XYZ
237    '''
238    perm3 = [[1,1,1],[0,1,1],[1,0,1],[1,1,0],[1,0,0],[0,1,0],[0,0,1],[0,0,0]]
239    TB = np.where(abs(XYZ-1)<toler,-1,0)+np.where(abs(XYZ)<toler,1,0)
240    perm = TB*perm3
241    cperm = ['%d%d%d'%(i,j,k) for i,j,k in perm]
242    D = dict(zip(cperm,perm))
243    new = []
244    for key in D:
245        new.append(np.array(D[key])+np.array(XYZ))
246    return new
247       
248def GenAtom(XYZ,SGData,All=False,Uij=[]):
249    '''
250    Generates the equivalent positions for a specified coordinate and space group
251    input: 
252        XYZ an array, tuple or list containing 3 elements: x, y & z
253        SGData, from SpcGroup
254        All  = True return all equivalent positions including duplicates
255             = False return only unique positions
256        Uij  = [U11,U22,U33,U12,U13,U23] or [] if no Uij       
257    return: [[XYZEquiv],Idup,[UijEquiv]]
258        [XYZEquiv] is list of equivalent positions (XYZ is first entry)
259        Idup = [-][C]SS where SS is the symmetry operator number (1-24), C (if not 0,0,0)
260        is centering operator number (1-4) and - is for inversion
261        Cell = unit cell translations needed to put new positions inside cell
262        [UijEquiv] - equivalent Uij; absent if no Uij given
263    '''
264    XYZEquiv = []
265    UijEquiv = []
266    Idup = []
267    Cell = []
268    X = np.array(XYZ)
269    X = MoveToUnitCell(X)
270    for ic,cen in enumerate(SGData['SGCen']):
271        C = np.array(cen)
272        for invers in range(int(SGData['SGInv']+1)):
273            for io,[M,T] in enumerate(SGData['SGOps']):
274                idup = ((io+1)+100*ic)*(1-2*invers)
275                XT = np.inner(M,X)+T
276                if len(Uij):
277                    U = Uij2U(Uij)
278                    U = np.inner(M,np.inner(U,M).T)
279                    newUij = U2Uij(U)
280                if invers:
281                    XT = -XT
282                XT += C
283                newX = MoveToUnitCell(XT)
284                cell = np.asarray(np.rint(newX-XT),dtype=np.int32)
285                if All:
286                    if np.allclose(newX,X,atol=0.0002):
287                        idup = False
288                else:
289                    if True in [np.allclose(newX,X,atol=0.0002) for oldX in XYZEquiv]:
290                        idup = False
291                if All or idup:
292                    XYZEquiv.append(newX)
293                    Idup.append(idup)
294                    Cell.append(cell)
295                    if len(Uij):
296                        UijEquiv.append(newUij)                   
297    if len(Uij):
298        return zip(XYZEquiv,UijEquiv,Idup,Cell)
299    else:
300        return zip(XYZEquiv,Idup,Cell)
301       
302def GenHKL(HKL,SGData,Friedel=False):
303    '''
304    Generate set of equivalent reflections
305    input:
306        HKL - [h,k,l]
307        SGData - space group data obtained from SpcGroup
308        Friedel = True to retain Friedel pairs for centrosymmetric case
309    returns:
310        iabsnt = True is reflection is forbidden by symmetry
311        mulp = reflection multiplicity including Fridel pairs
312        Uniq = numpy array of equivalent hkl in descending order of h,k,l
313        Phs = numpy array of corresponding phase factors (multiples of 2pi)
314    ''' 
315    iabsnt = False
316    H = np.array(HKL)
317    Inv = SGData['SGInv']
318    Cen = SGData['SGCen'][1:]           #skip P
319    # do cell centering extinction check
320    for cen in Cen:
321        iabsnt |= bool(int(round(np.sum(cen*H*12)))%12)
322    # get operators & expand if centrosymmetric
323    Ops = SGData['SGOps']
324    opM = np.array([op[0].T for op in Ops])
325    opT = np.array([op[1] for op in Ops])
326    if Inv:
327        opM = np.concatenate((opM,-opM))
328        opT = np.concatenate((opT,-opT))
329    # generate full hkl table and phase factors; find duplicates for multiplicity
330    eqH = np.inner(opM,H)
331    HT = np.dot(opT,H)%1.
332    dup = len(ma.nonzero([ma.allclose(HKL,hkl,atol=0.001) for hkl in eqH])[0])
333    mulp = len(eqH)/dup
334    # generate unique reflection set (with or without Friedel pairs)
335    HPT = [tuple(hp) for hp in np.column_stack((eqH,HT))]
336    HPT.sort()
337    UPT = HPT[::dup]
338    UPT.reverse()
339    if Inv and not Friedel:
340        UPT = UPT[:len(UPT)/2]           
341    Uniq = np.split(UPT,[3,4],axis=1)
342    Phs = Uniq[1].T[0]
343    Uniq = Uniq[0]
344    # determine if extinct
345    HPT = np.split(HPT,[3,4],axis=1)[1].T[0]
346    HPT = np.reshape(HPT,(mulp,-1)).T
347    HPT = np.around(HPT-HPT[0],4)%1.
348    iabsnt |= np.any(HPT)
349    return iabsnt,mulp,Uniq,Phs
350                                   
351def GetOprPtrName(key):           
352    OprPtrName = {
353        '-6643':[   2,' 1bar ', 1],'6479' :[  10,'  2z  ', 2],'-6479':[   9,'  mz  ', 3],
354        '6481' :[   7,'  my  ', 4],'-6481':[   6,'  2y  ', 5],'6641' :[   4,'  mx  ', 6],
355        '-6641':[   3,'  2x  ', 7],'6591' :[  28,' m+-0 ', 8],'-6591':[  27,' 2+-0 ', 9],
356        '6531' :[  25,' m110 ',10],'-6531':[  24,' 2110 ',11],'6537' :[  61,'  4z  ',12],
357        '-6537':[  62,' -4z  ',13],'975'  :[  68,' 3+++1',14],'6456' :[ 114,'  3z1 ',15],
358        '-489' :[  73,' 3+-- ',16],'483'  :[  78,' 3-+- ',17],'-969' :[  83,' 3--+ ',18],
359        '819'  :[  22,' m+0- ',19],'-819' :[  21,' 2+0- ',20],'2431' :[  16,' m0+- ',21],
360        '-2431':[  15,' 20+- ',22],'-657' :[  19,' m101 ',23],'657'  :[  18,' 2101 ',24],
361        '1943' :[  48,' -4x  ',25],'-1943':[  47,'  4x  ',26],'-2429':[  13,' m011 ',27],
362        '2429' :[  12,' 2011 ',28],'639'  :[  55,' -4y  ',29],'-639' :[  54,'  4y  ',30],
363        '-6484':[ 146,' 2010 ', 4],'6484' :[ 139,' m010 ', 5],'-6668':[ 145,' 2100 ', 6],
364        '6668' :[ 138,' m100 ', 7],'-6454':[ 148,' 2120 ',18],'6454' :[ 141,' m120 ',19],
365        '-6638':[ 149,' 2210 ',20],'6638' :[ 142,' m210 ',21],              #search ends here
366        '2223' :[  68,' 3+++2',39],
367        '6538' :[ 106,'  6z1 ',40],'-2169':[  83,' 3--+2',41],'2151' :[  73,' 3+--2',42],
368        '2205' :[  79,'-3-+-2',43],'-2205':[  78,' 3-+-2',44],'489'  :[  74,'-3+--1',45],
369        '801'  :[  53,'  4y1 ',46],'1945' :[  47,'  4x3 ',47],'-6585':[  62,' -4z3 ',48],
370        '6585' :[  61,'  4z3 ',49],'6584' :[ 114,'  3z2 ',50],'6666' :[ 106,'  6z5 ',51],
371        '6643' :[   1,' Iden ',52],'-801' :[  55,' -4y1 ',53],'-1945':[  48,' -4x3 ',54],
372        '-6666':[ 105,' -6z5 ',55],'-6538':[ 105,' -6z1 ',56],'-2223':[  69,'-3+++2',57],
373        '-975' :[  69,'-3+++1',58],'-6456':[ 113,' -3z1 ',59],'-483' :[  79,'-3-+-1',60],
374        '969'  :[  84,'-3--+1',61],'-6584':[ 113,' -3z2 ',62],'2169' :[  84,'-3--+2',63],
375        '-2151':[  74,'-3+--2',64],'0':[0,' ????',0]
376        }
377    return OprPtrName[key]
378
379def GetKNsym(key):
380    KNsym = {
381        '0'         :'    1   ','1'         :'   -1   ','64'        :'  2(100)','32'        :'  m(100)',
382        '97'        :'2/m(100)','16'        :'  2(010)','8'         :'  m(010)','25'        :'2/m(010)',
383        '2'         :'  2(001)','4'         :'  m(001)','7'         :'2/m(001)','134217728' :'  2(011)',
384        '67108864'  :'  m(011)','201326593' :'2/m(011)','2097152'   :'  2(0+-)','1048576'   :'  m(0+-)',
385        '3145729'   :'2/m(0+-)','8388608'   :'  2(101)','4194304'   :'  m(101)','12582913'  :'2/m(101)',
386        '524288'    :'  2(+0-)','262144'    :'  m(+0-)','796433'    :'2/m(+0-)','1024'      :'  2(110)',
387        '512'       :'  m(110)','1537'      :'2/m(110)','256'       :'  2(+-0)','128'       :'  m(+-0)',
388        '385'       :'2/m(+-0)','76'        :'mm2(100)','52'        :'mm2(010)','42'        :'mm2(001)',
389        '135266336' :'mm2(011)','69206048'  :'mm2(0+-)','8650760'   :'mm2(101)','4718600'   :'mm2(+0-)',
390        '1156'      :'mm2(110)','772'       :'mm2(+-0)','82'        :'  222   ','136314944' :'222(100)',
391        '8912912'   :'222(010)','1282'      :'222(001)','127'       :'  mmm   ','204472417' :'mmm(100)',
392        '13369369'  :'mmm(010)','1927'      :'mmm(001)','33554496'  :'  4(100)','16777280'  :' -4(100)',
393        '50331745'  :'4/m(100)','169869394' :'422(100)','84934738'  :'-42m 100','101711948' :'4mm(100)',
394        '254804095' :'4/mmm100','536870928 ':'  4(010)','268435472' :' -4(010)','805306393' :'4/m (10)',
395        '545783890' :'422(010)','272891986' :'-42m 010','541327412' :'4mm(010)','818675839' :'4/mmm010',
396        '2050'      :'  4(001)','4098'      :' -4(001)','6151'      :'4/m(001)','3410'      :'422(001)',
397        '4818'      :'-42m 001','2730'      :'4mm(001)','8191'      :'4/mmm001','8192'      :'  3(111)',
398        '8193'      :' -3(111)','2629888'   :' 32(111)','1319040'   :' 3m(111)','3940737'   :'-3m(111)',
399        '32768'     :'  3(+--)','32769'     :' -3(+--)','10519552'  :' 32(+--)','5276160'   :' 3m(+--)',
400        '15762945'  :'-3m(+--)','65536'     :'  3(-+-)','65537'     :' -3(-+-)','134808576' :' 32(-+-)',
401        '67437056'  :' 3m(-+-)','202180097' :'-3m(-+-)','131072'    :'  3(--+)','131073'    :' -3(--+)',
402        '142737664' :' 32(--+)','71434368'  :' 3m(--+)','214040961' :'-3m(--+)','237650'    :'   23   ',
403        '237695'    :'   m3   ','715894098' :'   432  ','358068946' :'  -43m  ','1073725439':'   m3m  ',
404        '68157504'  :' mm2d100','4456464'   :' mm2d010','642'       :' mm2d001','153092172' :'-4m2 100',
405        '277348404' :'-4m2 010','5418'      :'-4m2 001','1075726335':'  6/mmm ','1074414420':'-6m2 100',
406        '1075070124':'-6m2 120','1075069650':'   6mm  ','1074414890':'   622  ','1073758215':'   6/m  ',
407        '1073758212':'   -6   ','1073758210':'    6   ','1073759865':'-3m(100)','1075724673':'-3m(120)',
408        '1073758800':' 3m(100)','1075069056':' 3m(120)','1073759272':' 32(100)','1074413824':' 32(120)',
409        '1073758209':'   -3   ','1073758208':'    3   ','1074135143':'mmm(100)','1075314719':'mmm(010)',
410        '1073743751':'mmm(110)','1074004034':' mm2z100','1074790418':' mm2z010','1073742466':' mm2z110',
411        '1074004004':'mm2(100)','1074790412':'mm2(010)','1073742980':'mm2(110)','1073872964':'mm2(120)',
412        '1074266132':'mm2(210)','1073742596':'mm2(+-0)','1073872930':'222(100)','1074266122':'222(010)',
413        '1073743106':'222(110)','1073741831':'2/m(001)','1073741921':'2/m(100)','1073741849':'2/m(010)',
414        '1073743361':'2/m(110)','1074135041':'2/m(120)','1075314689':'2/m(210)','1073742209':'2/m(+-0)',
415        '1073741828':' m(001) ','1073741888':' m(100) ','1073741840':' m(010) ','1073742336':' m(110) ',
416        '1074003968':' m(120) ','1074790400':' m(210) ','1073741952':' m(+-0) ','1073741826':' 2(001) ',
417        '1073741856':' 2(100) ','1073741832':' 2(010) ','1073742848':' 2(110) ','1073872896':' 2(120) ',
418        '1074266112':' 2(210) ','1073742080':' 2(+-0) ','1073741825':'   -1   '
419        }
420    return KNsym[key]       
421
422def GetNXUPQsym(siteSym):       
423    NXUPQsym = {
424        '    1   ':(28,29,28,28),'   -1   ':( 1,29,28, 0),'  2(100)':(12,18,12,25),'  m(100)':(25,18,12,25),
425        '2/m(100)':( 1,18, 0,-1),'  2(010)':(13,17,13,24),'  m(010)':(24,17,13,24),'2/m(010)':( 1,17, 0,-1),
426        '  2(001)':(14,16,14,23),'  m(001)':(23,16,14,23),'2/m(001)':( 1,16, 0,-1),'  2(011)':(10,23,10,22),
427        '  m(011)':(22,23,10,22),'2/m(011)':( 1,23, 0,-1),'  2(0+-)':(11,24,11,21),'  m(0+-)':(21,24,11,21),
428        '2/m(0+-)':( 1,24, 0,-1),'  2(101)':( 8,21, 8,20),'  m(101)':(20,21, 8,20),'2/m(101)':( 1,21, 0,-1),
429        '  2(+0-)':( 9,22, 9,19),'  m(+0-)':(19,22, 9,19),'2/m(+0-)':( 1,22, 0,-1),'  2(110)':( 6,19, 6,18),
430        '  m(110)':(18,19, 6,18),'2/m(110)':( 1,19, 0,-1),'  2(+-0)':( 7,20, 7,17),'  m(+-0)':(17,20, 7,17),
431        '2/m(+-0)':( 1,20, 0,-1),'mm2(100)':(12,10, 0,-1),'mm2(010)':(13,10, 0,-1),'mm2(001)':(14,10, 0,-1),
432        'mm2(011)':(10,13, 0,-1),'mm2(0+-)':(11,13, 0,-1),'mm2(101)':( 8,12, 0,-1),'mm2(+0-)':( 9,12, 0,-1),
433        'mm2(110)':( 6,11, 0,-1),'mm2(+-0)':( 7,11, 0,-1),'  222   ':( 1,10, 0,-1),'222(100)':( 1,13, 0,-1),
434        '222(010)':( 1,12, 0,-1),'222(001)':( 1,11, 0,-1),'  mmm   ':( 1,10, 0,-1),'mmm(100)':( 1,13, 0,-1),
435        'mmm(010)':( 1,12, 0,-1),'mmm(001)':( 1,11, 0,-1),'  4(100)':(12, 4,12, 0),' -4(100)':( 1, 4,12, 0),
436        '4/m(100)':( 1, 4,12,-1),'422(100)':( 1, 4, 0,-1),'-42m 100':( 1, 4, 0,-1),'4mm(100)':(12, 4, 0,-1),
437        '4/mmm100':( 1, 4, 0,-1),'  4(010)':(13, 3,13, 0),' -4(010)':( 1, 3,13, 0),'4/m (10)':( 1, 3,13,-1),
438        '422(010)':( 1, 3, 0,-1),'-42m 010':( 1, 3, 0,-1),'4mm(010)':(13, 3, 0,-1),'4/mmm010':(1, 3, 0,-1,),
439        '  4(001)':(14, 2,14, 0),' -4(001)':( 1, 2,14, 0),'4/m(001)':( 1, 2,14,-1),'422(001)':( 1, 2, 0,-1),
440        '-42m 001':( 1, 2, 0,-1),'4mm(001)':(14, 2, 0,-1),'4/mmm001':( 1, 2, 0,-1),'  3(111)':( 2, 5, 2, 0),
441        ' -3(111)':( 1, 5, 2, 0),' 32(111)':( 1, 5, 0, 2),' 3m(111)':( 2, 5, 0, 2),'-3m(111)':( 1, 5, 0,-1),
442        '  3(+--)':( 5, 8, 5, 0),' -3(+--)':( 1, 8, 5, 0),' 32(+--)':( 1, 8, 0, 5),' 3m(+--)':( 5, 8, 0, 5),
443        '-3m(+--)':( 1, 8, 0,-1),'  3(-+-)':( 4, 7, 4, 0),' -3(-+-)':( 1, 7, 4, 0),' 32(-+-)':( 1, 7, 0, 4),
444        ' 3m(-+-)':( 4, 7, 0, 4),'-3m(-+-)':( 1, 7, 0,-1),'  3(--+)':( 3, 6, 3, 0),' -3(--+)':( 1, 6, 3, 0),
445        ' 32(--+)':( 1, 6, 0, 3),' 3m(--+)':( 3, 6, 0, 3),'-3m(--+)':( 1, 6, 0,-1),'   23   ':( 1, 1, 0, 0),
446        '   m3   ':( 1, 1, 0, 0),'   432  ':( 1, 1, 0, 0),'  -43m  ':( 1, 1, 0, 0),'   m3m  ':( 1, 1, 0, 0),
447        ' mm2d100':(12,13, 0,-1),' mm2d010':(13,12, 0,-1),' mm2d001':(14,11, 0,-1),'-4m2 100':( 1, 4, 0,-1),
448        '-4m2 010':( 1, 3, 0,-1),'-4m2 001':( 1, 2, 0,-1),'  6/mmm ':( 1, 9, 0,-1),'-6m2 100':( 1, 9, 0,-1),
449        '-6m2 120':( 1, 9, 0,-1),'   6mm  ':(14, 9, 0,-1),'   622  ':( 1, 9, 0,-1),'   6/m  ':( 1, 9,14,-1),
450        '   -6   ':( 1, 9,14, 0),'    6   ':(14, 9,14, 0),'-3m(100)':( 1, 9, 0,-1),'-3m(120)':( 1, 9, 0,-1),
451        ' 3m(100)':(14, 9, 0,14),' 3m(120)':(14, 9, 0,14),' 32(100)':( 1, 9, 0,14),' 32(120)':( 1, 9, 0,14),
452        '   -3   ':( 1, 9,14, 0),'    3   ':(14, 9,14, 0),'mmm(100)':( 1,14, 0,-1),'mmm(010)':( 1,15, 0,-1),
453        'mmm(110)':( 1,11, 0,-1),' mm2z100':(14,14, 0,-1),' mm2z010':(14,15, 0,-1),' mm2z110':(14,11, 0,-1),
454        'mm2(100)':(12,14, 0,-1),'mm2(010)':(13,15, 0,-1),'mm2(110)':( 6,11, 0,-1),'mm2(120)':(15,14, 0,-1),
455        'mm2(210)':(16,15, 0,-1),'mm2(+-0)':( 7,11, 0,-1),'222(100)':( 1,14, 0,-1),'222(010)':( 1,15, 0,-1),
456        '222(110)':( 1,11, 0,-1),'2/m(001)':( 1,16,14,-1),'2/m(100)':( 1,25,12,-1),'2/m(010)':( 1,28,13,-1),
457        '2/m(110)':( 1,19, 6,-1),'2/m(120)':( 1,27,15,-1),'2/m(210)':( 1,26,16,-1),'2/m(+-0)':( 1,20,17,-1),
458        ' m(001) ':(23,16,14,23),' m(100) ':(26,25,12,26),' m(010) ':(27,28,13,27),' m(110) ':(18,19, 6,18),
459        ' m(120) ':(24,27,15,24),' m(210) ':(25,26,16,25),' m(+-0) ':(17,20, 7,17),' 2(001) ':(14,16,14,23),
460        ' 2(100) ':(12,25,12,26),' 2(010) ':(13,28,13,27),' 2(110) ':( 6,19, 6,18),' 2(120) ':(15,27,15,24),
461        ' 2(210) ':(16,26,16,25),' 2(+-0) ':( 7,20, 7,17),'   -1   ':( 1,29,28, 0)
462        }
463    return NXUPQsym[siteSym]
464
465def GetCSxinel(siteSym): 
466    CSxinel = [[],                         # 0th empty - indices are Fortran style
467        [[0,0,0],[ 0.0, 0.0, 0.0]],      #  0  0  0
468        [[1,1,1],[ 1.0, 1.0, 1.0]],      #  X  X  X
469        [[1,1,1],[ 1.0, 1.0,-1.0]],      #  X  X -X
470        [[1,1,1],[ 1.0,-1.0, 1.0]],      #  X -X  X
471        [[1,1,1],[ 1.0,-1.0,-1.0]],      # -X  X  X
472        [[1,1,0],[ 1.0, 1.0, 0.0]],      #  X  X  0
473        [[1,1,0],[ 1.0,-1.0, 0.0]],      #  X -X  0
474        [[1,0,1],[ 1.0, 0.0, 1.0]],      #  X  0  X
475        [[1,0,1],[ 1.0, 0.0,-1.0]],      #  X  0 -X
476        [[0,1,1],[ 0.0, 1.0, 1.0]],      #  0  Y  Y
477        [[0,1,1],[ 0.0, 1.0,-1.0]],      #  0  Y -Y
478        [[1,0,0],[ 1.0, 0.0, 0.0]],      #  X  0  0
479        [[0,1,0],[ 0.0, 1.0, 0.0]],      #  0  Y  0
480        [[0,0,1],[ 0.0, 0.0, 1.0]],      #  0  0  Z
481        [[1,1,0],[ 1.0, 2.0, 0.0]],      #  X 2X  0
482        [[1,1,0],[ 2.0, 1.0, 0.0]],      # 2X  X  0
483        [[1,1,2],[ 1.0, 1.0, 1.0]],      #  X  X  Z
484        [[1,1,2],[ 1.0,-1.0, 1.0]],      #  X -X  Z
485        [[1,2,1],[ 1.0, 1.0, 1.0]],      #  X  Y  X
486        [[1,2,1],[ 1.0, 1.0,-1.0]],      #  X  Y -X
487        [[1,2,2],[ 1.0, 1.0, 1.0]],      #  X  Y  Y
488        [[1,2,2],[ 1.0, 1.0,-1.0]],      #  X  Y -Y
489        [[1,2,0],[ 1.0, 1.0, 0.0]],      #  X  Y  0
490        [[1,0,2],[ 1.0, 0.0, 1.0]],      #  X  0  Z
491        [[0,1,2],[ 0.0, 1.0, 1.0]],      #  0  Y  Z
492        [[1,1,2],[ 1.0, 2.0, 1.0]],      #  X 2X  Z
493        [[1,1,2],[ 2.0, 1.0, 1.0]],      # 2X  X  Z
494        [[1,2,3],[ 1.0, 1.0, 1.0]],      #  X  Y  Z
495        ]
496    indx = GetNXUPQsym(siteSym)
497    return CSxinel[indx[0]]
498   
499def GetCSuinel(siteSym):
500    # returns Uij terms, multipliers, GUI flags & Uiso2Uij multipliers
501    CSuinel = [[],                                             # 0th empty - indices are Fortran style
502        [[1,1,1,0,0,0],[ 1.0, 1.0, 1.0, 0.0, 0.0, 0.0],[1,0,0,0,0,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  A  A  0  0  0
503        [[1,1,2,0,0,0],[ 1.0, 1.0, 1.0, 0.0, 0.0, 0.0],[1,0,1,0,0,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  A  C  0  0  0
504        [[1,2,1,0,0,0],[ 1.0, 1.0, 1.0, 0.0, 0.0, 0.0],[1,1,0,0,0,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  B  A  0  0  0
505        [[1,2,2,0,0,0],[ 1.0, 1.0, 1.0, 0.0, 0.0, 0.0],[1,1,0,0,0,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  B  B  0  0  0
506        [[1,1,1,2,2,2],[ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],[1,0,0,1,0,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  A  A  D  D  D
507        [[1,1,1,2,2,2],[ 1.0, 1.0, 1.0, 1.0,-1.0,-1.0],[1,0,0,1,0,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  A  A  D -D -D
508        [[1,1,1,2,2,2],[ 1.0, 1.0, 1.0, 1.0,-1.0, 1.0],[1,0,0,1,0,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  A  A  D -D  D
509        [[1,1,1,2,2,2],[ 1.0, 1.0, 1.0, 1.0, 1.0,-1.0],[1,0,0,1,0,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  A  A  D  D -D
510        [[1,1,2,1,0,0],[ 1.0, 1.0, 1.0, 0.5, 0.0, 0.0],[1,0,1,0,0,0],[1.0,1.0,1.0,0.5,0.0,0.0]],    #  A  A  C A/2 0  0
511        [[1,2,3,0,0,0],[ 1.0, 1.0, 1.0, 0.0, 0.0, 0.0],[1,1,1,0,0,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  B  C  0  0  0
512        [[1,1,2,3,0,0],[ 1.0, 1.0, 1.0, 1.0, 0.0, 0.0],[1,0,1,1,0,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  A  C  D  0  0
513        [[1,2,1,0,3,0],[ 1.0, 1.0, 1.0, 0.0, 1.0, 0.0],[1,1,0,0,1,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  B  A  0  E  0
514        [[1,2,2,0,0,3],[ 1.0, 1.0, 1.0, 0.0, 0.0, 1.0],[1,1,0,0,0,1],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  B  B  0  0  F
515        [[1,2,3,2,0,0],[ 1.0, 1.0, 1.0, 0.5, 0.0, 0.0],[1,1,1,0,0,0],[1.0,1.0,1.0,0.0,0.5,0.0]],    #  A  B  C B/2 0  0
516        [[1,2,3,1,0,0],[ 1.0, 1.0, 1.0, 0.5, 0.0, 0.0],[1,1,1,0,0,0],[1.0,1.0,1.0,0.0,0.5,0.0]],    #  A  B  C A/2 0  0
517        [[1,2,3,4,0,0],[ 1.0, 1.0, 1.0, 1.0, 0.0, 0.0],[1,1,1,1,0,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  B  C  D  0  0
518        [[1,2,3,0,4,0],[ 1.0, 1.0, 1.0, 0.0, 1.0, 0.0],[1,1,1,0,1,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  B  C  0  E  0
519        [[1,2,3,0,0,4],[ 1.0, 1.0, 1.0, 0.0, 0.0, 1.0],[1,1,1,0,0,1],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  B  C  0  0  F
520        [[1,1,2,3,4,4],[ 1.0, 1.0, 1.0, 1.0, 1.0,-1.0],[1,0,1,1,1,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  A  C  D  E -E
521        [[1,1,2,3,4,4],[ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],[1,0,1,1,1,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  A  C  D  E  E
522        [[1,2,1,3,4,3],[ 1.0, 1.0, 1.0, 1.0, 1.0,-1.0],[1,1,0,1,1,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  B  A  D  E -D
523        [[1,2,1,3,4,3],[ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],[1,1,0,1,1,0],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  B  A  D  E  D
524        [[1,2,2,3,3,4],[ 1.0, 1.0, 1.0, 1.0,-1.0, 1.0],[1,1,0,1,0,1],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  B  B  D -D  F
525        [[1,2,2,3,3,4],[ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],[1,1,0,1,0,1],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  B  B  D  D  F
526        [[1,2,3,2,4,4],[ 1.0, 1.0, 1.0, 0.5, 0.5, 1.0],[1,1,1,0,0,1],[1.0,1.0,1.0,0.5,0.0,0.0]],    #  A  B  C B/2 F/2 F
527        [[1,2,3,1,0,4],[ 1.0, 1.0, 1.0, 0.5, 0.0, 1.0],[1,1,1,0,0,1],[1.0,1.0,1.0,0.5,0.0,0.0]],    #  A  B  C A/2  0  F
528        [[1,2,3,2,4,0],[ 1.0, 1.0, 1.0, 0.5, 1.0, 0.0],[1,1,1,0,1,0],[1.0,1.0,1.0,0.5,0.0,0.0]],    #  A  B  C B/2  E  0
529        [[1,2,3,1,4,4],[ 1.0, 1.0, 1.0, 0.5, 1.0, 0.5],[1,1,1,0,1,0],[1.0,1.0,1.0,0.5,0.0,0.0]],    #  A  B  C A/2  E E/2
530        [[1,2,3,4,5,6],[ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],[1,1,1,1,1,1],[1.0,1.0,1.0,0.0,0.0,0.0]],    #  A  B  C  D  E   F
531        ]
532    indx = GetNXUPQsym(siteSym)
533    return CSuinel[indx[1]]
534   
535def MustrainNames(SGData):
536    laue = SGData['SGLaue']
537    uniq = SGData['SGUniq']
538    if laue in ['m3','m3m']:
539        return ['S400','S220']
540    elif laue in ['6/m','6/mmm','3m1']:
541        return ['S400','S004','S202']
542    elif laue in ['31m','3']:
543        return ['S400','S004','S202','S211']
544    elif laue in ['3R','3mR']:
545        return ['S400','S220','S310','S211']
546    elif laue in ['4/m','4/mmm']:
547        return ['S400','S004','S220','S022']
548    elif laue in ['mmm']:
549        return ['S400','S040','S004','S220','S202','S022']
550    elif laue in ['2/m']:
551        SHKL = ['S400','S040','S004','S220','S202','S022']
552        if uniq == 'a':
553            SHKL += ['S013','S031','S211']
554        elif uniq == 'b':
555            SHKL += ['S301','S103','S121']
556        elif uniq == 'c':
557            SHKL += ['S130','S310','S112']
558        return SHKL
559    else:
560        SHKL = ['S400','S040','S004','S220','S202','S022']
561        SHKL += ['S310','S103','S031','S130','S301','S013']
562        SHKL += ['S211','S121','S112']
563        return SHKL
564
565def MustrainCoeff(HKL,SGData):
566    #NB: order of terms is the same as returned by MustrainNames
567    laue = SGData['SGLaue']
568    uniq = SGData['SGUniq']
569    h,k,l = HKL
570    Strm = []
571    if laue in ['m3','m3m']:
572        Strm.append(h**4+k**4+l**4)
573        Strm.append(3.0*((h*k)**2+(h*l)**2+(k*l)**2))
574    elif laue in ['6/m','6/mmm','3m1']:
575        Strm.append(h**4+k**4+2.0*k*h**3+2.0*h*k**3+3.0*(h*k)**2)
576        Strm.append(l**4)
577        Strm.append(3.0*((h*l)**2+(k*l)**2+h*k*l**2))
578    elif laue in ['31m','3']:
579        Strm.append(h**4+k**4+2.0*k*h**3+2.0*h*k**3+3.0*(h*k)**2)
580        Strm.append(l**4)
581        Strm.append(3.0*((h*l)**2+(k*l)**2+h*k*l**2))
582        Strm.append(4.0*h*k*l*(h+k))
583    elif laue in ['3R','3mR']:
584        Strm.append(h**4+k**4+l**4)
585        Strm.append(3.0*((h*k)**2+(h*l)**2+(k*l)**2))
586        Strm.append(2.0*(h*l**3+l*k**3+k*h**3)+2.0*(l*h**3+k*l**3+l*k**3))
587        Strm.append(4.0*(k*l*h**2+h*l*k**2+h*k*l**2))
588    elif laue in ['4/m','4/mmm']:
589        Strm.append(h**4+k**4)
590        Strm.append(l**4)
591        Strm.append(3.0*(h*k)**2)
592        Strm.append(3.0*((h*l)**2+(k*l)**2))
593    elif laue in ['mmm']:
594        Strm.append(h**4)
595        Strm.append(k**4)
596        Strm.append(l**4)
597        Strm.append(3.0*(h*k)**2)
598        Strm.append(3.0*(h*l)**2)
599        Strm.append(3.0*(k*l)**2)
600    elif laue in ['2/m']:
601        Strm.append(h**4)
602        Strm.append(k**4)
603        Strm.append(l**4)
604        Strm.append(3.0*(h*k)**2)
605        Strm.append(3.0*(h*l)**2)
606        Strm.append(3.0*(k*l)**2)
607        if uniq == 'a':
608            Strm.append(2.0*k*l**3)
609            Strm.append(2.0*l*k**3)
610            Strm.append(4.0*k*l*h**2)
611        elif uniq == 'b':
612            Strm.append(2.0*l*h**3)
613            Strm.append(2.0*h*l**3)
614            Strm.append(4.0*h*l*k**2)
615        elif uniq == 'c':
616            Strm.append(2.0*h*k**3)
617            Strm.append(2.0*k*h**3)
618            Strm.append(4.0*h*k*l**2)
619    else:
620        Strm.append(h**4)
621        Strm.append(k**4)
622        Strm.append(l**4)
623        Strm.append(3.0*(h*k)**2)
624        Strm.append(3.0*(h*l)**2)
625        Strm.append(3.0*(k*l)**2)
626        Strm.append(2.0*k*h**3)
627        Strm.append(2.0*h*l**3)
628        Strm.append(2.0*l*k**3)
629        Strm.append(2.0*h*k**3)
630        Strm.append(2.0*l*h**3)
631        Strm.append(2.0*k*l**3)
632        Strm.append(4.0*k*l*h**2)
633        Strm.append(4.0*h*l*k**2)
634        Strm.append(4.0*k*h*l**2)
635    return Strm
636   
637def Muiso2Shkl(muiso,SGData,cell):
638    #this is to convert isotropic mustrain to generalized Shkls - doesn't work just now
639    import GSASIIlattice as G2lat
640    from scipy.optimize import fmin
641    A = G2lat.cell2AB(cell)[0]
642    def minMus(Shkl,H,muiso,SGData,A):
643        U = np.inner(A.T,H)
644        S = np.array(MustrainCoeff(H.T,SGData))
645        sum = np.sqrt(np.sum(np.multiply(S,Shkl)))
646        return abs(muiso-sum*H)
647    laue = SGData['SGLaue']
648    if laue in ['m3','m3m']:
649        H = [[1,0,0],[1,1,0]]
650        S0 = [0.01,0.01]
651    elif laue in ['6/m','6/mmm','3m1']:
652        H = [[1,0,0],[0,0,1],[1,0,1]]
653        S0 = [0.01,0.01,0.01]
654    elif laue in ['31m','3']:
655        H = [[1,0,0],[0,0,1],[1,0,1],[1,1,1]]
656        S0 = [0.01,0.01,0.01,0.01]
657    elif laue in ['3R','3mR']:
658        H = [[1,0,0],[1,1,0],[1,0,1],[1,1,1]]
659        S0 = [0.01,0.01,0.01,0.01]
660    elif laue in ['4/m','4/mmm']:
661        H = [[1,0,0],[0,0,1],[1,1,0],[1,0,1]]
662        S0 = [0.01,0.01,0.01,0.01]
663    elif laue in ['mmm']:
664        H = [[1,0,0],[0,1,0],[0,0,1],[1,1,0],[1,0,1],[0,1,1]]
665        S0 = [0.01,0.01,0.01,0.01,0.01,0.01]
666    elif laue in ['2/m']:
667        H = [[1,0,0],[0,1,0],[0,0,1],[1,1,0],[1,0,1],[0,1,1]]
668        if uniq == 'a':
669            H.append([0,1,-1])
670            H.append([0,-2,1])
671        elif uniq == 'b':
672            H.append([1,0,-1])
673            H.append([-2,0,1])
674        elif uniq == 'c':
675            H.append([1,-1,0])
676            H.append([-2,1,0])
677        H.append([1,1,1])
678        S0 = [9*[0.01,]]
679    else:
680        H = [[1,0,0],[0,1,0],[0,0,1],[1,1,0],[1,0,1],[0,1,1],
681            [-1,1,0],[1,0,-1],[0,-1,1],[1,-2,0],[-2,0,1],[0,1,-2],
682            [1,-1,1],[-1, 1, 1],[1,-1,1]]
683        S0 = [15*[0.01,]]
684    H = np.array(H)
685    S0 = np.array(S0)
686    return fmin(minMus,S0,(H,muiso,SGData,A))
687       
688def SytSym(XYZ,SGData):
689    '''
690    Generates the number of equivalent positions and a site symmetry code for a specified coordinate and space group
691    input: 
692       XYZ: an array, tuple or list containing 3 elements: x, y & z
693       SGData: from SpcGroup
694    Returns a two element tuple:
695       The 1st element is a code for the site symmetry (see GetKNsym)
696       The 2nd element is the site multiplicity
697    '''
698    def PackRot(SGOps):
699        IRT = []
700        for ops in SGOps:
701            M = ops[0]
702            irt = 0
703            for j in range(2,-1,-1):
704                for k in range(2,-1,-1):
705                    irt *= 3
706                    irt += M[k][j]
707            IRT.append(int(irt))
708        return IRT
709       
710    SymName = ''
711    Mult = 1
712    Isym = 0
713    if SGData['SGLaue'] in ['3','3m1','31m','6/m','6/mmm']:
714        Isym = 1073741824
715    Jdup = 0
716    Xeqv = GenAtom(XYZ,SGData,True)
717    IRT = PackRot(SGData['SGOps'])
718    L = -1
719    for ic,cen in enumerate(SGData['SGCen']):
720        for invers in range(int(SGData['SGInv']+1)):
721            for io,ops in enumerate(SGData['SGOps']):
722                irtx = (1-2*invers)*IRT[io]
723                L += 1
724                if not Xeqv[L][1]:
725                    Jdup += 1
726                    jx = GetOprPtrName(str(irtx))
727                    if jx[2] < 39:
728                        Isym += 2**(jx[2]-1)
729    if Isym == 1073741824: Isym = 0
730    Mult = len(SGData['SGOps'])*len(SGData['SGCen'])*(int(SGData['SGInv'])+1)/Jdup
731         
732    return GetKNsym(str(Isym)),Mult
733   
734def ElemPosition(SGData):
735    ''' Under development
736    Object here is to return a list of symmetry element types and locations suitable
737    for say drawing them.
738    So far I have the element type... getting all possible locations without lookup may be impossible!
739    '''
740    SymElements = []
741    Inv = SGData['SGInv']
742    Cen = SGData['SGCen']
743    eleSym = {-3:['','-1'],-2:['',-6],-1:['2','-4'],0:['3','-3'],1:['4','m'],2:['6',''],3:['1','']}
744    # get operators & expand if centrosymmetric
745    Ops = SGData['SGOps']
746    opM = np.array([op[0].T for op in Ops])
747    opT = np.array([op[1] for op in Ops])
748    if Inv:
749        opM = np.concatenate((opM,-opM))
750        opT = np.concatenate((opT,-opT))
751    opMT = zip(opM,opT)
752    for M,T in opMT[1:]:        #skip I
753        Dt = int(nl.det(M))
754        Tr = int(np.trace(M))
755        Dt = -(Dt-1)/2
756        Es = eleSym[Tr][Dt]
757        if Dt:              #rotation-inversion
758            I = np.eye(3)
759            if Tr == 1:     #mirrors/glides
760                if np.any(T):       #glide
761                    M2 = np.inner(M,M)
762                    MT = np.inner(M,T)+T
763                    print 'glide',Es,MT
764                    print M2
765                else:               #mirror
766                    print 'mirror',Es,T
767                    print I-M
768                X = [-1,-1,-1]
769            elif Tr == -3:  # pure inversion
770                X = np.inner(nl.inv(I-M),T)
771                print 'inversion',Es,X
772            else:           #other rotation-inversion
773                M2 = np.inner(M,M)
774                MT = np.inner(M,T)+T
775                print 'rot-inv',Es,MT
776                print M2
777                X = [-1,-1,-1]
778               
779           
780           
781        else:               #rotations
782            print 'rotation',Es
783            X = [-1,-1,-1]
784        #SymElements.append([Es,X])
785       
786    return #SymElements
787   
788def ApplyStringOps(A,SGData,X,Uij=[]):
789    SGOps = SGData['SGOps']
790    SGCen = SGData['SGCen']
791    Ax = A.split('+')
792    Ax[0] = int(Ax[0])
793    iC = 0
794    if Ax[0] < 0:
795        iC = 1
796    Ax[0] = abs(Ax[0])
797    nA = Ax[0]%100-1
798    cA = Ax[0]/100
799    Cen = SGCen[cA]
800    M,T = SGOps[nA]
801    if len(Ax)>1:
802        cellA = Ax[1].split(',')
803        cellA = np.array([int(a) for a in cellA])
804    else:
805        cellA = np.zeros(3)
806    newX = (1-2*iC)*(Cen+np.inner(M,X)+T)+cellA
807    if len(Uij):
808        U = Uij2U(Uij)
809        U = np.inner(M,np.inner(U,M).T)
810        newUij = U2Uij(U)
811        return [newX,newUij]
812    else:
813        return newX
814       
815def StringOpsProd(A,B,SGData):
816    ''' Find A*B where A & B are in strings '-' + '100*c+n' + '+ijk'
817    where '-' indicates inversion, c(>0) is the cell centering operator,
818    n is operator number from SgOps and ijk are unit cell translations (each may be <0).
819    Should return resultant string - C.
820        SGData - dictionary using entries:
821             'SGCen': cell centering vectors [0,0,0] at least
822             'SGOps': symmetry operations as [M,T] so that M*x+T = x'
823    '''
824    SGOps = SGData['SGOps']
825    SGCen = SGData['SGCen']
826    #1st split out the cell translation part & work on the operator parts
827    Ax = A.split('+'); Bx = B.split('+')
828    Ax[0] = int(Ax[0]); Bx[0] = int(Bx[0])
829    iC = 0
830    if Ax[0]*Bx[0] < 0:
831        iC = 1
832    Ax[0] = abs(Ax[0]); Bx[0] = abs(Bx[0])
833    nA = Ax[0]%100-1;  nB = Bx[0]%100-1
834    cA = Ax[0]/100;  cB = Bx[0]/100
835    Cen = (SGCen[cA]+SGCen[cB])%1.0
836    cC = np.nonzero([np.allclose(C,Cen) for C in SGCen])[0][0]
837    Ma,Ta = SGOps[nA]; Mb,Tb = SGOps[nB]
838    Mc = np.inner(Ma,Mb.T)
839#    print Ma,Mb,Mc
840    Tc = (np.add(np.inner(Mb,Ta)+1.,Tb))%1.0
841#    print Ta,Tb,Tc
842#    print [np.allclose(M,Mc)&np.allclose(T,Tc) for M,T in SGOps]
843    nC = np.nonzero([np.allclose(M,Mc)&np.allclose(T,Tc) for M,T in SGOps])[0][0]
844    #now the cell translation part
845    if len(Ax)>1:
846        cellA = Ax[1].split(',')
847        cellA = [int(a) for a in cellA]
848    else:
849        cellA = [0,0,0]
850    if len(Bx)>1:
851        cellB = Bx[1].split(',')
852        cellB = [int(b) for b in cellB]
853    else:
854        cellB = [0,0,0]
855    cellC = np.add(cellA,cellB)
856    C = str(((nC+1)+(100*cC))*(1-2*iC))+'+'+ \
857        str(int(cellC[0]))+','+str(int(cellC[1]))+','+str(int(cellC[2]))
858    return C
859           
860def U2Uij(U):
861    #returns the UIJ vector U11,U22,U33,U12,U13,U23 from tensor U
862    return [U[0][0],U[1][1],U[2][2],U[0][1],U[0][2],U[1][2]]
863   
864def Uij2U(Uij):
865    #returns the thermal motion tensor U from Uij as numpy array
866    return np.array([[Uij[0],Uij[3],Uij[4]],[Uij[3],Uij[1],Uij[5]],[Uij[4],Uij[5],Uij[2]]])
867   
868'''A list of space groups as ordered and named in the pre-2002 International
869Tables Volume A, except that spaces are used following the GSAS convention to
870separate the different crystallographic directions.
871Note that the symmetry codes here will recognize many non-standard space group
872symbols with different settings.
873'''
874spglist = {
875    'triclinic' : ('P 1','P -1',), # 1-2
876    'monoclinic': ('P 2','P 21','C 2','P m','P c','C m','C c','P 2/m','P 21/m',
877                   'C 2/m','P 2/c','P 21/c','C 2/c',), #3-15
878    'orthorhombic': ('P 2 2 2','P 2 2 21','P 21 21 2','P 21 21 21','C 2 2 21',
879                     'C 2 2 2','F 2 2 2','I 2 2 2','I 21 21 21',
880                     'P m m 2','P m c 21','P c c 2','P m a 2','P c a 21',
881                     'P n c 2','P m n 21','P b a 2','P n a 21','P n n 2',
882                     'C m m 2','C m c 21','C c c 2','A m m 2','A b m 2',
883                     'A m a 2','A b a 2','F m m 2','F d d 2','I m m 2',
884                     'I b a 2','I m a 2','P m m m','P n n n','P c c m',
885                     'P b a n','P m m a','P n n a','P m n a','P c c a',
886                     'P b a m','P c c n','P b c m','P n n m','P m m n',
887                     'P b c n','P b c a','P n m a','C m c m','C m c a',
888                     'C m m m','C c c m','C m m a','C c c a','F m m m',
889                     'F d d d','I m m m','I b a m','I b c a','I m m a',), #16-74
890    'tetragonal': ('P 4','P 41','P 42','P 43','I 4','I 41','P -4','I -4',
891                   'P 4/m','P 42/m','P 4/n','P 42/n','I 4/m','I 41/a',
892                   'P 4 2 2','P 4 21 2','P 41 2 2','P 41 21 2','P 42 2 2',
893                   'P 42 21 2','P 43 2 2','P 43 21 2','I 4 2 2','I 41 2 2',
894                   'P 4 m m','P 4 b m','P 42 c m','P 42 n m','P 4 c c',
895                   'P 4 n c','P 42 m c','P 42 b c','I 4 m m','I 4 c m',
896                   'I 41 m d','I 41 c d','P -4 2 m','P -4 2 c','P -4 21 m',
897                   'P -4 21 c','P -4 m 2','P -4 c 2','P -4 b 2','P -4 n 2',
898                   'I -4 m 2','I -4 c 2','I -4 2 m','I -4 2 d','P 4/m m m',
899                   'P 4/m c c','P 4/n b m','P 4/n n c','P 4/m b m','P 4/m n c',
900                   'P 4/n m m','P 4/n c c','P 42/m m c','P 42/m c m',
901                   'P 42/n b c','P 42/n n m','P 42/m b c','P 42/m n m',
902                   'P 42/n m c','P 42/n c m','I 4/m m m','I 4/m c m',
903                   'I 41/a m d','I 41/a c d',), # 75-142
904    'trigonal': ('P 3','P 31','P 32','R 3','P -3','R -3','P 3 1 2','P 3 2 1',
905                 'P 31 1 2','P 31 2 1','P 32 1 2','P 32 2 1','R 3 2', 'P 3 m 1',
906                 'P 3 1 m','P 3 c 1','P 3 1 c','R 3 m','R 3 c','P -3 1 m',
907                 'P -3 1 c','P -3 m 1','P -3 c 1','R -3 m','R -3 c',), #143-167
908    'hexagonal': ('P 6','P 61','P 65','P 62','P 64','P 63','P -6','P 6/m',
909                  'P 63/m','P 6 2 2','P 61 2 2','P 65 2 2','P 62 2 2',
910                  'P 64 2 2','P 63 2 2','P 6 m m','P 6 c c','P 63 c m',
911                  'P 63 m c','P -6 m 2','P -6 c 2','P -6 2 m','P -6 2 c',
912                  'P 6/m m m','P 6/m c c','P 63/m c m','P 63/m m c',), #144-194
913    'cubic': ('P 2 3','F 2 3','I 2 3','P 21 3','I 21 3','P m -3','P n -3',
914              'F m -3','F d -3','I m -3','P a -3','I a -3','P 4 3 2','P 42 3 2',
915              'F 4 3 2','F 41 3 2','I 4 3 2','P 43 3 2','P 41 3 2','I 41 3 2',
916              'P -4 3 m','F -4 3 m','I -4 3 m','P -4 3 n','F -4 3 c','I -4 3 d',
917              'P m -3 m','P n -3 n','P m -3 n','P n -3 m','F m -3 m','F m -3 c',
918              'F d -3 m','F d -3 c','I m -3 m','I a -3 d',), #195-230
919}
920'A few non-standard space groups for test use'
921nonstandard_sglist = ('P 21 1 1','P 1 21 1','P 1 1 21','R 3 r','R 3 2 h', 
922                      'R -3 r', 'R 3 2 r','R 3 m h', 'R 3 m r',
923                      'R 3 c r','R -3 c r','R -3 m r',),
924'''A list of orthorhombic space groups that were renamed in the 2002 Volume A,
925along with the pre-2002 name. The e designates a double glide-plane'''
926sgequiv_2002_orthorhombic= (('A e m 2', 'A b m 2',),
927                            ('A e a 2', 'A b a 2',),
928                            ('C m c e', 'C m c a',),
929                            ('C m m e', 'C m m a',),
930                            ('C c c e', 'C c c a'),)
931'''Use the space groups types in this order to list the symbols in the
932order they are listed in the International Tables, vol. A'''
933symtypelist = ('triclinic', 'monoclinic', 'orthorhombic', 'tetragonal', 
934               'trigonal', 'hexagonal', 'cubic')
935
936# self-test materials follow. Requires files in directory testinp
937def test0():
938    '''test #0: exercise MoveToUnitCell'''
939    msg = "MoveToUnitCell failed"
940    assert (MoveToUnitCell([1,2,3]) == [0,0,0]).all, msg
941    assert (MoveToUnitCell([2,-1,-2]) == [0,0,0]).all, msg
942    assert abs(MoveToUnitCell(np.array([-.1]))[0]-0.9) < 1e-6, msg
943    assert abs(MoveToUnitCell(np.array([.1]))[0]-0.1) < 1e-6, msg
944
945def test1():
946    ''' test #1: SpcGroup and SGPrint against previous results'''
947    testdir = ospath.join(ospath.split(ospath.abspath( __file__ ))[0],'testinp')
948    if ospath.exists(testdir):
949        if testdir not in sys.path: sys.path.insert(0,testdir)
950    import spctestinp
951    def CompareSpcGroup(spc, referr, refdict, reflist): 
952        'Compare output from GSASIIspc.SpcGroup with results from a previous run'
953        # if an error is reported, the dictionary can be ignored
954        msg0 = "CompareSpcGroup failed on space group %s" % spc
955        result = SpcGroup(spc)
956        if result[0] == referr and referr > 0: return True
957        keys = result[1].keys()
958        #print result[1]['SpGrp']
959        msg = msg0 + " in list lengths"
960        assert len(keys) == len(refdict.keys()), msg
961        for key in keys:
962            if key == 'SGOps' or  key == 'SGCen':
963                msg = msg0 + (" in key %s length" % key)
964                assert len(refdict[key]) == len(result[1][key]), msg
965                for i in range(len(refdict[key])):
966                    msg = msg0 + (" in key %s level 0" % key)
967                    assert np.allclose(result[1][key][i][0],refdict[key][i][0]), msg
968                    msg = msg0 + (" in key %s level 1" % key)
969                    assert np.allclose(result[1][key][i][1],refdict[key][i][1]), msg
970            else:
971                msg = msg0 + (" in key %s" % key)
972                assert result[1][key] == refdict[key], msg
973        msg = msg0 + (" in key %s reflist" % key)
974        #for (l1,l2) in zip(reflist, SGPrint(result[1])):
975        #    assert l2.replace('\t','').replace(' ','') == l1.replace(' ',''), 'SGPrint ' +msg
976        assert reflist == SGPrint(result[1]), 'SGPrint ' +msg
977    for spc in spctestinp.SGdat:
978        CompareSpcGroup(spc, 0, spctestinp.SGdat[spc], spctestinp.SGlist[spc] )
979
980def test2():
981    ''' test #2: SpcGroup against cctbx (sgtbx) computations'''
982    testdir = ospath.join(ospath.split(ospath.abspath( __file__ ))[0],'testinp')
983    if ospath.exists(testdir):
984        if testdir not in sys.path: sys.path.insert(0,testdir)
985    import sgtbxtestinp
986    def CompareWcctbx(spcname, cctbx_in, debug=0):
987        'Compare output from GSASIIspc.SpcGroup with results from cctbx.sgtbx'
988        cctbx = cctbx_in[:] # make copy so we don't delete from the original
989        spc = (SpcGroup(spcname))[1]
990        if debug: print spc['SpGrp']
991        if debug: print spc['SGCen']
992        latticetype = spcname.strip().upper()[0]
993        # lattice type of R implies Hexagonal centering", fix the rhombohedral settings
994        if latticetype == "R" and len(spc['SGCen']) == 1: latticetype = 'P'
995        assert latticetype == spc['SGLatt'], "Failed: %s does not match Lattice: %s" % (spcname, spc['SGLatt'])
996        onebar = [1]
997        if spc['SGInv']: onebar.append(-1)
998        for (op,off) in spc['SGOps']:
999            for inv in onebar:
1000                for cen in spc['SGCen']:
1001                    noff = off + cen
1002                    noff = MoveToUnitCell(noff)
1003                    mult = tuple((op*inv).ravel().tolist())
1004                    if debug: print "\n%s: %s + %s" % (spcname,mult,noff)
1005                    for refop in cctbx:
1006                        if debug: print refop
1007                        # check the transform
1008                        if refop[:9] != mult: continue
1009                        if debug: print "mult match"
1010                        # check the translation
1011                        reftrans = list(refop[-3:])
1012                        reftrans = MoveToUnitCell(reftrans)
1013                        if all(abs(noff - reftrans) < 1.e-5):
1014                            cctbx.remove(refop)
1015                            break
1016                    else:
1017                        assert False, "failed on %s:\n\t %s + %s" % (spcname,mult,noff)
1018    for key in sgtbxtestinp.sgtbx:
1019        CompareWcctbx(key, sgtbxtestinp.sgtbx[key])
1020
1021def test3(): 
1022    ''' test #3: exercise SytSym (includes GetOprPtrName, GenAtom, GetKNsym)
1023     for selected space groups against info in IT Volume A '''
1024    def ExerciseSiteSym (spc, crdlist):
1025        'compare site symmetries and multiplicities for a specified space group'
1026        msg = "failed on site sym test for %s" % spc
1027        (E,S) = SpcGroup(spc)
1028        assert not E, msg
1029        for t in crdlist:
1030            symb, m = SytSym(t[0],S)
1031            if symb.strip() != t[2].strip() or m != t[1]:
1032                print spc,t[0],m,symb
1033            assert m == t[1]
1034            #assert symb.strip() == t[2].strip()
1035
1036    ExerciseSiteSym('p 1',[
1037            ((0.13,0.22,0.31),1,'1'),
1038            ((0,0,0),1,'1'),
1039            ])
1040    ExerciseSiteSym('p -1',[
1041            ((0.13,0.22,0.31),2,'1'),
1042            ((0,0.5,0),1,'-1'),
1043            ])
1044    ExerciseSiteSym('C 2/c',[
1045            ((0.13,0.22,0.31),8,'1'),
1046            ((0.0,.31,0.25),4,'2(010)'),
1047            ((0.25,.25,0.5),4,'-1'),
1048            ((0,0.5,0),4,'-1'),
1049            ])
1050    ExerciseSiteSym('p 2 2 2',[
1051            ((0.13,0.22,0.31),4,'1'),
1052            ((0,0.5,.31),2,'2(001)'),
1053            ((0.5,.31,0.5),2,'2(010)'),
1054            ((.11,0,0),2,'2(100)'),
1055            ((0,0.5,0),1,'222'),
1056            ])
1057    ExerciseSiteSym('p 4/n',[
1058            ((0.13,0.22,0.31),8,'1'),
1059            ((0.25,0.75,.31),4,'2(001)'),
1060            ((0.5,0.5,0.5),4,'-1'),
1061            ((0,0.5,0),4,'-1'),
1062            ((0.25,0.25,.31),2,'4(001)'),
1063            ((0.25,.75,0.5),2,'-4(001)'),
1064            ((0.25,.75,0.0),2,'-4(001)'),
1065            ])
1066    ExerciseSiteSym('p 31 2 1',[
1067            ((0.13,0.22,0.31),6,'1'),
1068            ((0.13,0.0,0.833333333),3,'2(100)'),
1069            ((0.13,0.13,0.),3,'2(110)'),
1070            ])
1071    ExerciseSiteSym('R 3 c',[
1072            ((0.13,0.22,0.31),18,'1'),
1073            ((0.0,0.0,0.31),6,'3'),
1074            ])
1075    ExerciseSiteSym('R 3 c R',[
1076            ((0.13,0.22,0.31),6,'1'),
1077            ((0.31,0.31,0.31),2,'3(111)'),
1078            ])
1079    ExerciseSiteSym('P 63 m c',[
1080            ((0.13,0.22,0.31),12,'1'),
1081            ((0.11,0.22,0.31),6,'m(100)'),
1082            ((0.333333,0.6666667,0.31),2,'3m(100)'),
1083            ((0,0,0.31),2,'3m(100)'),
1084            ])
1085    ExerciseSiteSym('I a -3',[
1086            ((0.13,0.22,0.31),48,'1'),
1087            ((0.11,0,0.25),24,'2(100)'),
1088            ((0.11,0.11,0.11),16,'3(111)'),
1089            ((0,0,0),8,'-3(111)'),
1090            ])
1091
1092if __name__ == '__main__':
1093    test0()
1094    test1()
1095    test2()
1096    test3()
1097    print "OK"
Note: See TracBrowser for help on using the repository browser.