# This product includes software produced by UChicago Argonne, LLC under Contract # No. DE-AC02-06CH11357 with the Department of Energy. # GSAS-II commit c13e7308142f26f0c22ded684f016d87ca02d7aa. # Adapted by the Physchem project; see scripts/extract-gsas-index.py and gsas-license.txt. # General Structure Analysis System - II (GSAS-II) # OPEN SOURCE LICENSE # # Copyright 2010, UChicago Argonne, LLC, Operator of Argonne National Laboratory # All rights reserved. # # GSAS-II may be used by anyone on a royalty-free basis. Use and # redistribution, with or without modification, are permitted provided # that the following conditions are met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Software changes, modifications, or derivative works should be noted # with comments and the author and organization's name. # * Distribution of changed, modified or derivative works based on # GSAS-II grants the GSAS-II copyright holder unrestricted permission # to include any, or all, new and changed code in future GSAS-II # releases. # * Redistributions that include binary forms must include all relevant # source code and reproduce the above copyright notice, this list of # conditions and the following disclaimers in the documentation and/or # other materials provided with the distribution. # * Neither the names of UChicago Argonne, LLC or the Department of # Energy nor the names of its contributors may be used to endorse or # promote products derived from this software without specific prior # written permission. # * The software and the end-user documentation included with the # redistribution, if any, must include the following acknowledgment: # "This product includes software produced by UChicago Argonne, LLC # under Contract No. DE-AC02-06CH11357 with the Department of Energy." # # Also note that GSAS-II is normally distributed with the following # included software: # # NIST_profile (Fundamental Parameters Python Code): U.S. Government work, # not subject to copyright # # DIFFaX: Not copyrighted; Distributed with direct permission of # author (M. Treacy) # # NIST*LATTICE: U.S. Government work, not subject to copyright # # Shapes: GNU GPL 3+ license; see initial text in file GSASII/G2shapes.py # # ***************************************************************************** # WARRANTY DISCLAIMER: THE SOFTWARE IS SUPPLIED "AS IS" WITHOUT WARRANTY # OF ANY KIND. THE COPYRIGHT HOLDERS, THEIR THIRD PARTY LICENSORS, THE # UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND THEIR # EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR IMPLIED, INCLUDING # BUT NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS # FOR A PARTICULAR PURPOSE, TITLE OR NON-INFRINGEMENT, (2) DO NOT ASSUME # ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, # OR USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF THE # SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) DO NOT WARRANT # THAT THE SOFTWARE WILL FUNCTION UNINTERRUPTED, THAT IT IS ERROR-FREE # OR THAT ANY ERRORS WILL BE CORRECTED. # # LIMITATION OF LIABILITY: IN NO EVENT WILL THE COPYRIGHT HOLDERS, THEIR # THIRD PARTY LICENSORS, THE UNITED STATES, THE UNITED STATES DEPARTMENT # OF ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, INCIDENTAL, # CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF ANY KIND OR NATURE, # INCLUDING BUT NOT LIMITED TO LOSS OF PROFITS OR LOSS OF DATA, FOR ANY # REASON WHATSOEVER, WHETHER SUCH LIABILITY IS ASSERTED ON THE BASIS OF # CONTRACT, TORT (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR # OTHERWISE, EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE # POSSIBILITY OF SUCH LOSS OR DAMAGES. # ****************************************************************************** import math,copy,sys import random as ran import numpy as np import numpy.linalg as nl rpd = np.pi/180. RSQ2PI=1./np.sqrt(2.*np.pi) SQ2=np.sqrt(2.) RSQPI=1./np.sqrt(np.pi) R2pisq=1./(2.*np.pi**2) Forpi=4.0*np.pi sind = lambda x: np.sin(x*np.pi/180.) asind = lambda x: 180.*np.arcsin(x)/np.pi tand = lambda x: np.tan(x*np.pi/180.) atand = lambda x: 180.*np.arctan(x)/np.pi atan2d = lambda y,x: 180.*np.arctan2(y,x)/np.pi cosd = lambda x: np.cos(x*np.pi/180.) acosd = lambda x: 180.*np.arccos(x)/np.pi rdsq2d = lambda x,p: round(1.0/math.sqrt(x),p) nprdsq2d = lambda x,p: np.round(1.0/np.sqrt(x),p) nxs = np.newaxis def sec2HMS(sec): """Convert time in sec to H:M:S string :param sec: time in seconds :return: H:M:S string (to nearest 100th second) """ H = int(sec//3600) M = int(sec//60-H*60) S = sec-3600*H-60*M return '%d:%2d:%.2f'%(H,M,S) def fillgmat(cell): """Compute lattice metric tensor from unit cell constants :param cell: tuple with a,b,c,alpha, beta, gamma (degrees) :return: 3x3 numpy array """ a,b,c,alp,bet,gam = cell g = np.array([ [a*a, a*b*cosd(gam), a*c*cosd(bet)], [a*b*cosd(gam), b*b, b*c*cosd(alp)], [a*c*cosd(bet) ,b*c*cosd(alp), c*c]]) return g def cell2Gmat(cell): """Compute real and reciprocal lattice metric tensor from unit cell constants :param cell: tuple with a,b,c,alpha, beta, gamma (degrees) :return: reciprocal (G) & real (g) metric tensors (list of two numpy 3x3 arrays) """ g = fillgmat(cell) G = nl.inv(g) return G,g def A2Gmat(A,inverse=True): """Fill real & reciprocal metric tensor (G) from A. :param A: reciprocal metric tensor elements as [G11,G22,G33,2*G12,2*G13,2*G23] :param bool inverse: if True return both G and g; else just G :return: reciprocal (G) & real (g) metric tensors (list of two numpy 3x3 arrays) """ G = np.array([ [A[0], A[3]/2., A[4]/2.], [A[3]/2.,A[1], A[5]/2.], [A[4]/2.,A[5]/2., A[2]]]) if inverse: g = nl.inv(G) return G,g else: return G def Gmat2A(G): """Extract A from reciprocal metric tensor (G) :param G: reciprocal metric tensor (3x3 numpy array) :return: A = [G11,G22,G33,2*G12,2*G13,2*G23] """ return [G[0][0],G[1][1],G[2][2],2.*G[0][1],2.*G[0][2],2.*G[1][2]] def cell2A(cell): """Obtain A = [G11,G22,G33,2*G12,2*G13,2*G23] from lattice parameters :param cell: [a,b,c,alpha,beta,gamma] (degrees) :return: G reciprocal metric tensor as 3x3 numpy array """ G,g = cell2Gmat(cell) return Gmat2A(G) def A2cell(A): """Compute unit cell constants from A :param A: [G11,G22,G33,2*G12,2*G13,2*G23] G - reciprocal metric tensor :return: a,b,c,alpha, beta, gamma (degrees) - lattice parameters """ G,g = A2Gmat(A) return Gmat2cell(g) def Gmat2cell(g): """Compute real/reciprocal lattice parameters from real/reciprocal metric tensor (g/G) The math works the same either way. :param g (or G): real (or reciprocal) metric tensor 3x3 array :return: a,b,c,alpha, beta, gamma (degrees) (or a*,b*,c*,alpha*,beta*,gamma* degrees) """ oldset = np.seterr('raise') a = np.sqrt(max(0,g[0][0])) b = np.sqrt(max(0,g[1][1])) c = np.sqrt(max(0,g[2][2])) alp = acosd(g[2][1]/(b*c)) bet = acosd(g[2][0]/(a*c)) gam = acosd(g[0][1]/(a*b)) np.seterr(**oldset) return a,b,c,alp,bet,gam cellXformRelations = {0: ['1.0*A0*T[0,0]**2', '1.0*A1*T[0,1]**2', '1.0*A2*T[0,2]**2', '1.0*A3*T[0,0]*T[0,1]', '1.0*A4*T[0,0]*T[0,2]', '1.0*A5*T[0,1]*T[0,2]'], 1: ['1.0*A0*T[1,0]**2', '1.0*A1*T[1,1]**2', '1.0*A2*T[1,2]**2', '1.0*A3*T[1,0]*T[1,1]', '1.0*A4*T[1,0]*T[1,2]', '1.0*A5*T[1,1]*T[1,2]'], 2: ['1.0*A0*T[2,0]**2', '1.0*A1*T[2,1]**2', '1.0*A2*T[2,2]**2', '1.0*A3*T[2,0]*T[2,1]', '1.0*A4*T[2,0]*T[2,2]', '1.0*A5*T[2,1]*T[2,2]'], 3: ['2.0*A0*T[0,0]*T[1,0]', '2.0*A1*T[0,1]*T[1,1]', '2.0*A2*T[0,2]*T[1,2]', '1.0*A3*(T[0,0]*T[1,1] + T[1,0]*T[0,1])', '1.0*A4*(T[0,0]*T[1,2] + T[1,0]*T[0,2])', '1.0*A5*(T[0,1]*T[1,2] + T[1,1]*T[0,2])'], 4: ['2.0*A0*T[0,0]*T[2,0]', '2.0*A1*T[0,1]*T[2,1]', '2.0*A2*T[0,2]*T[2,2]', '1.0*A3*(T[0,0]*T[2,1] + T[2,0]*T[0,1])', '1.0*A4*(T[0,0]*T[2,2] + T[2,0]*T[0,2])', '1.0*A5*(T[0,1]*T[2,2] + T[2,1]*T[0,2])'], 5: ['2.0*A0*T[1,0]*T[2,0]', '2.0*A1*T[1,1]*T[2,1]', '2.0*A2*T[1,2]*T[2,2]', '1.0*A3*(T[1,0]*T[2,1] + T[2,0]*T[1,1])', '1.0*A4*(T[1,0]*T[2,2] + T[2,0]*T[1,2])', '1.0*A5*(T[1,1]*T[2,2] + T[2,1]*T[1,2])']} def calc_rVsq(A): """Compute the square of the reciprocal lattice volume (1/V**2) from A' """ G,g = A2Gmat(A) rVsq = nl.det(G) if rVsq < 0: return 1 return rVsq def calc_rV(A): """Compute the reciprocal lattice volume (V*) from A """ return np.sqrt(calc_rVsq(A)) def calc_V(A): """Compute the real lattice volume (V) from A """ return 1./calc_rV(A) def calc_rDsqA(H,A): 'calc array of 1/d^2 from array of hkl & A-terms' # B = np.array([H[0]**2,H[1]**2,H[2]**2,H[0]*H[1],H[0]*H[2],H[1]*H[2]]) # return np.sum(np.array(A)[:,nxs]*B,axis=0) h,k,l = H return A[0]*h*h+A[1]*k*k+A[2]*l*l+A[3]*h*k+A[4]*h*l+A[5]*k*l def calc_rDsqZ(H,A,Z,tth,lam): 'computes 1/d^2 from hkl array & reciprocal metric tensor A with CW ZERO shift' rdsq = calc_rDsqA(H,A)+Z*sind(tth)*2.0*rpd/lam**2 return rdsq def MaxIndex(dmin,A): 'needs doc string' Hmax = [0,0,0] try: cell = A2cell(A) except: cell = [1.,1.,1.,90.,90.,90.] for i in range(3): Hmax[i] = int(np.round(cell[i]/dmin)) return Hmax def sortHKLd(HKLd,ifreverse,ifdup,ifSS=False): '''sort reflection list on d-spacing; can sort in either order :param HKLd: a list of [h,k,l,d,...]; :param ifreverse: True for largest d first :param ifdup: True if duplicate d-spacings allowed :return: sorted reflection list ''' T = [] N = 3 if ifSS: N = 4 for i,H in enumerate(HKLd): if ifdup: T.append((H[N],i)) else: T.append(H[N]) D = dict(zip(T,HKLd)) T.sort() if ifreverse: T.reverse() X = [] okey = '' for key in T: if key != okey: X.append(D[key]) #remove duplicate d-spacings okey = key return X def newCentCheck(Cent,H): 'checks np.array of HKls for centering extinction; returns allowed HKLs - fast' K = H.T if Cent == 'A': return H[np.where((K[1]+K[2])%2 == 0)] elif Cent == 'B': return H[np.where((K[0]+K[2])%2 == 0)] elif Cent == 'C': return H[np.where((K[0]+K[1])%2 == 0)] elif Cent == 'I': return H[np.where((K[0]+K[1]+K[2])%2 == 0)] elif Cent == 'F': H = H[np.where((K[0]+K[1])%2 == 0)] K = H.T H = H[np.where((K[0]+K[2])%2 == 0)] K = H.T return H[np.where((K[1]+K[2])%2 == 0)] elif Cent == 'R': return H[np.where((-K[0]+K[1]+K[2])%3 == 0)] else: return H def _GenHBravais_cctbx(dmin, Bravais, A, sg_type, uctbx_unit_cell, miller_index_generator): '''Alternate form of :func:`GenHBravais` that uses CCTBX internals ''' g_inv = np.array([[A[0], A[3]/2, A[4]/2], [A[3]/2, A[1], A[5]/2], [A[4]/2, A[5]/2, A[2]]]) g = np.linalg.inv(g_inv) g_elems = (g[0][0], g[1][1], g[2][2], g[0][1], g[0][2], g[1][2]) try: uc = uctbx_unit_cell(metrical_matrix=g_elems) except ValueError: # this function sometimes receives an A matrix that gives # numbers <0 in the diagonal elems of g. Not sure why. return [] #if sg_type is None: # sg_type = make_sgtype(Bravais) mig = miller_index_generator(uc, sg_type, 0, dmin) result = [] for h,k,l in mig: d = uc.d((h,k,l)) result.append([h, k, l, d, -1]) result.sort(key=lambda l: l[3], reverse=True) return result def GenHBravais(dmin, Bravais, A, cctbx_args=None,ifList=False): """Generate the positionally unique powder diffraction reflections :param dmin: minimum d-spacing in A :param Bravais: lattice type (see GetBraviasNum). Bravais is one of: * 0 F cubic * 1 I cubic * 2 P cubic * 3 R hexagonal (trigonal not rhombohedral) * 4 P hexagonal * 5 I tetragonal * 6 P tetragonal * 7 F orthorhombic * 8 I orthorhombic * 9 A orthorhombic * 10 B orthorhombic * 11 C orthorhombic * 12 P orthorhombic * 13 I monoclinic * 14 A monoclinic * 15 C monoclinic * 16 P monoclinic * 17 P triclinic :param A: reciprocal metric tensor elements as [G11,G22,G33,2*G12,2*G13,2*G23] :param dict cctbx_args: items defined in CCTBX: * 'sg_type': value from cctbx.sgtbx.space_group_type(symmorphic_sgs[ibrav]) * 'uctbx_unit_cell': pointer to :meth:`cctbx.uctbx.unit_cell` * 'miller_index_generator': pointer to :meth:`cctbx.miller.index_generator` :param ifList: if True output is 2D list of HKL; if False (default) output is 2D nd.array of HKLs * ifList=False is fast & suitable for indexing routines; ifList=True is needed for graphics. * Currently applies only to Triclinic, Monoclinic & Orthorhombic; otherwise output is 2D list. :returns: HKL unique d list of [h,k,l,d,-1] sorted with largest d first """ if cctbx_args: return _GenHBravais_cctbx(dmin, Bravais, A, cctbx_args['sg_type'], cctbx_args['uctbx_unit_cell'], cctbx_args['miller_index_generator']) if Bravais in [9,14]: Cent = 'A' elif Bravais in [10,]: Cent = 'B' elif Bravais in [11,15]: Cent = 'C' elif Bravais in [1,5,8,13]: Cent = 'I' elif Bravais in [0,7]: Cent = 'F' elif Bravais in [3]: Cent = 'R' else: Cent = 'P' Hmax = MaxIndex(dmin,A) HKL = [] #generate HKL block first - no extinction conditions; obey Laue symmetry if Bravais == 17: #triclinic H0 = np.reshape(np.mgrid[0:1,1:Hmax[1]+1,0:1],(3,-1)).T #0k0, k>0 H1 = np.reshape(np.mgrid[0:1,1:Hmax[1]+1,-Hmax[2]:0],(3,-1)).T #0kl, k>=0,l>0 H2 = np.reshape(np.mgrid[0:1,0:Hmax[1]+1,1:Hmax[2]+1],(3,-1)).T #0kl, k>=0,l>0 H = np.reshape(np.mgrid[1:Hmax[0]+1,-Hmax[1]:Hmax[1]+1,-Hmax[2]:Hmax[2]+1],(3,-1)).T H = np.vstack((H0,H1,H2,H)) elif Bravais in [13,14,15,16]: #monoclinic - b unique H0 = np.reshape(np.mgrid[0:1,1:Hmax[1]+1,0:1],(3,-1)).T #0k0, k>0 H1 = np.reshape(np.mgrid[0:1,0:Hmax[1]+1,1:Hmax[2]+1],(3,-1)).T #0kl, k>=0,l>0 H = np.reshape(np.mgrid[1:Hmax[0]+1,0:Hmax[1]+1,-Hmax[2]:Hmax[2]+1],(3,-1)).T H = np.vstack((H0,H1,H)) elif Bravais in [7,8,9,10,11,12]: #orthorhombic H0 = np.reshape(np.mgrid[0:1,1:Hmax[1]+1,0:1],(3,-1)).T #0k0, k>0 H1 = np.reshape(np.mgrid[0:1,0:Hmax[1]+1,1:Hmax[2]+1],(3,-1)).T #0kl, k>=0,l>0 H = np.reshape(np.mgrid[1:Hmax[0]+1,0:Hmax[1]+1,0:Hmax[2]+1],(3,-1)).T H = np.vstack((H0,H1,H)) elif Bravais in [5,6]: #tetragonal H = [] for l in range(Hmax[2]+1): for k in range(Hmax[1]+1): for h in range(k,Hmax[0]+1): if [h,k,l] == [0,0,0]: continue H.append([h,k,l]) H = np.array(H) elif Bravais in [3,4]: #hexagonal/trigonal H = [] lmin = 0 if Bravais == 3: lmin = -Hmax[2] for l in range(lmin,Hmax[2]+1): for k in range(Hmax[1]+1): hmin = k if l < 0: hmin += 1 for h in range(hmin,Hmax[0]+1): if [h,k,l] == [0,0,0]: continue H.append([h,k,l]) H = np.array(H) else: #cubic H = [] for l in range(Hmax[2]+1): for k in range(l,Hmax[1]+1): for h in range(k,Hmax[0]+1): if [h,k,l] == [0,0,0]: continue H.append([h,k,l]) H = np.array(H) #then enforce centering rules & d >= dmin limit; make array or list as needed H = newCentCheck(Cent,H) rdsq = nprdsq2d(calc_rDsqA(H.T,A),6) if ifList: #structure needed for plotting HKL = np.vstack((H.T,rdsq)).T HKL = [[int(h[0]),int(h[1]),int(h[2]),h[3],-1] for h in HKL if h[3] >= dmin] return sortHKLd(HKL,True,False) else: #fast for indexing HKL = np.vstack((H.T,rdsq,[-1]*len(H))).T[rdsq >= dmin] if HKL.shape[0] > 1: D = HKL[:,3] Indx = np.argsort(D) return np.flipud(HKL[Indx]) else: return HKL BOH = { 'L=2':[[],[],[]], 'L=4':[[0.30469720,0.36418281],[],[]], 'L=6':[[-0.14104740,0.52775103],[],[]], 'L=8':[[0.28646862,0.21545346,0.32826995],[],[]], 'L=9':[[],[],[]], 'L=10':[[-0.16413497,0.33078546,0.39371345],[],[]], 'L=12':[[0.26141975,0.27266871,0.03277460,0.32589402], [0.09298802,-0.23773812,0.49446631,0.0],[]], 'L=13':[[],[],[]], 'L=14':[[-0.17557309,0.25821932,0.27709173,0.33645360],[],[]], 'L=15':[[],[],[]], 'L=16':[[0.24370673,0.29873515,0.06447688,0.00377,0.32574495], [0.12039646,-0.25330128,0.23950998,0.40962508,0.0],[]], 'L=17':[[],[],[]], 'L=18':[[-0.16914245,0.17017340,0.34598142,0.07433932,0.32696037], [-0.06901768,0.16006562,-0.24743528,0.47110273,0.0],[]], 'L=19':[[],[],[]], 'L=20':[[0.23067026,0.31151832,0.09287682,0.01089683,0.00037564,0.32573563], [0.13615420,-0.25048007,0.12882081,0.28642879,0.34620433,0.0],[]], 'L=21':[[],[],[]], 'L=22':[[-0.16109560,0.10244188,0.36285175,0.13377513,0.01314399,0.32585583], [-0.09620055,0.20244115,-0.22389483,0.17928946,0.42017231,0.0],[]], 'L=23':[[],[],[]], 'L=24':[[0.22050742,0.31770654,0.11661736,0.02049853,0.00150861,0.00003426,0.32573505], [0.13651722,-0.21386648,0.00522051,0.33939435,0.10837396,0.32914497,0.0], [0.05378596,-0.11945819,0.16272298,-0.26449730,0.44923956,0.0,0.0]], 'L=25':[[],[],[]], 'L=26':[[-0.15435003,0.05261630,0.35524646,0.18578869,0.03259103,0.00186197,0.32574594], [-0.11306511,0.22072681,-0.18706142,0.05439948,0.28122966,0.35634355,0.0],[]], 'L=27':[[],[],[]], 'L=28':[[0.21225019,0.32031716,0.13604702,0.03132468,0.00362703,0.00018294,0.00000294,0.32573501], [0.13219496,-0.17206256,-0.08742608,0.32671661,0.17973107,0.02567515,0.32619598,0.0], [0.07989184,-0.16735346,0.18839770,-0.20705337,0.12926808,0.42715602,0.0,0.0]], 'L=29':[[],[],[]], 'L=30':[[-0.14878368,0.01524973,0.33628434,0.22632587,0.05790047,0.00609812,0.00022898,0.32573594], [-0.11721726,0.20915005,-0.11723436,-0.07815329,0.31318947,0.13655742,0.33241385,0.0], [-0.04297703,0.09317876,-0.11831248,0.17355132,-0.28164031,0.42719361,0.0,0.0]], 'L=31':[[],[],[]], 'L=32':[[0.20533892,0.32087437,0.15187897,0.04249238,0.00670516,0.00054977,0.00002018,0.00000024,0.32573501], [0.12775091,-0.13523423,-0.14935701,0.28227378,0.23670434,0.05661270,0.00469819,0.32578978,0.0], [0.09703829,-0.19373733,0.18610682,-0.14407046,0.00220535,0.26897090,0.36633402,0.0,0.0]], 'L=33':[[],[],[]], 'L=34':[[-0.14409234,-0.01343681,0.31248977,0.25557722,0.08571889,0.01351208,0.00095792,0.00002550,0.32573508], [-0.11527834,0.18472133,-0.04403280,-0.16908618,0.27227021,0.21086614,0.04041752,0.32688152,0.0], [-0.06773139,0.14120811,-0.15835721,0.18357456,-0.19364673,0.08377174,0.43116318,0.0,0.0]] } Lnorm = lambda L: 4.*np.pi/(2.0*L+1.) UniqueCellByLaue = [ [['m3','m3m'],(0,)], [['3R','3mR'],(0,3)], [['3','3m1','31m','6/m','6/mmm','4/m','4/mmm'],(0,2)], [['mmm'],(0,1,2)], [['2/m'+'a'],(0,1,2,3)], [['2/m'+'b'],(0,1,2,4)], [['2/m'+'c'],(0,1,2,5)], [['-1'],(0,1,2,3,4,5)], ] cellAlbl = ('a','b','c', 'alpha', 'beta', 'gamma') cellUlbl = ('a','b','c','\u03B1','\u03B2','\u03B3')