source: trunk/GSASIIElem.py @ 661

Last change on this file since 661 was 661, checked in by toby, 13 years ago

complete revision number tracking

  • Property svn:keywords set to Date Author Revision URL Id
File size: 14.2 KB
Line 
1# -*- coding: utf-8 -*-
2"""Element: functions for element types
3   Copyright: 2008, Robert B. Von Dreele & Brian H. Toby (Argonne National Laboratory)
4"""
5########### SVN repository information ###################
6# $Date: 2012-06-29 21:24:59 +0000 (Fri, 29 Jun 2012) $
7# $Author: toby $
8# $Revision: 661 $
9# $URL: trunk/GSASIIElem.py $
10# $Id: GSASIIElem.py 661 2012-06-29 21:24:59Z toby $
11########### SVN repository information ###################
12
13import math
14import os.path
15import GSASIIpath
16GSASIIpath.SetVersionNumber("$Revision: 661 $")
17import numpy as np
18
19def GetFormFactorCoeff(El):
20    """Read X-ray form factor coefficients from `atomdata.asc` file
21
22    :param El: element 1-2 character symbol case irrevelant
23    :return: `FormFactors`: list of form factor dictionaries
24   
25    Each X-ray form factor dictionary is:
26   
27    * `Symbol`: 4 character element symbol with valence (e.g. 'NI+2')
28    * `Z`: atomic number
29    * `fa`: 4 A coefficients
30    * `fb`: 4 B coefficients
31    * `fc`: C coefficient
32   
33    """
34    ElS = El.upper()
35    ElS = ElS.rjust(2)
36    filename = os.path.join(os.path.split(__file__)[0],'atmdata.dat')
37    try:
38        FFdata = open(filename,'Ur')
39    except:
40        print "**** ERROR - File atmdata.dat not found in directory %s" % sys.path[0]
41        sys.exit()
42    S = '1'
43    FormFactors = []
44    while S:
45        S = FFdata.readline()
46        if S[3:5] == ElS:
47            if S[5:6] != '_' and S[8] not in ['N','M']:
48                Z=int(S[:2])
49                Symbol = S[3:7].strip()
50                S = S[12:]
51                fa = (float(S[:7]),float(S[14:21]),float(S[28:35]),float(S[42:49]))
52                fb = (float(S[7:14]),float(S[21:28]),float(S[35:42]),float(S[49:56]))
53                FormFac = {'Symbol':Symbol,'Z':Z,'fa':fa,'fb':fb,'fc':float(S[56:63])}
54                FormFactors.append(FormFac)               
55    FFdata.close()
56    return FormFactors
57   
58def GetFFC5(ElSym):
59    '''Get 5 term form factor and Compton scattering data
60    @param ElSym: str(1-2 character element symbol with proper case);
61    @return El: dictionary with 5 term form factor & compton coefficients
62    '''
63    import FormFactors as FF
64    El = {}
65    FF5 = FF.FFac5term[ElSym]
66    El['fa'] = FF5[:5]
67    El['fc'] = FF5[5]
68    El['fb'] = FF5[6:]
69    Cmp5 = FF.Compton[ElSym]
70    El['cmpz'] = Cmp5[0]
71    El['cmpa'] = Cmp5[1:6]
72    El['cmpb'] = Cmp5[6:]
73    return El
74   
75def CheckElement(El):
76    import ElementTable as ET
77    Elements = []
78    for elem in ET.ElTable:
79        Elements.append(elem[0][0])
80    if El.capitalize() in Elements:
81        return True
82    else:
83        return False 
84       
85def GetAtomInfo(El):
86   
87    import ElementTable as ET
88    Elements = []
89    for elem in ET.ElTable:
90        Elements.append(elem[0][0])
91    if len(El) in [2,4]:
92        ElS = El.upper()[:2].rjust(2)
93    else:
94        ElS = El.upper()[:1].rjust(2)
95    filename = os.path.join(os.path.split(__file__)[0],'atmdata.dat')
96    try:
97        FFdata = open(filename,'Ur')
98    except:
99        print '**** ERROR - File atmdata.dat not found in directory %s' % sys.path[0]
100        sys.exit()
101    S = '1'
102    AtomInfo = {}
103    Isotopes = {}
104    Mass = []
105    while S:
106        S = FFdata.readline()
107        if S[3:5] == ElS:
108            if S[5:6] == '_':
109                if not Mass:                                 #picks 1st one; natural abundance or 1st isotope
110                    Mass = float(S[10:19])
111                if S[6] in [' ','1','2','3','4','5','6','7','8','9']:                       
112                    isoName = S[6:9]
113                    if isoName == '   ':
114                        isoName = 'Nat. Abund.'              #natural abundance
115                    if S[76:78] in ['LS','BW']:     #special anomalous scattering length info
116                        St = [S[10:19],S[19:25],S[25:31],S[31:38],S[38:44],S[44:50],
117                            S[50:56],S[56:62],S[62:68],S[68:74],]
118                        Vals = []
119                        for item in St:
120                            if item.strip():
121                                Vals.append(float(item.strip()))
122                        Isotopes[isoName.rstrip()] = Vals                       
123                    else:
124                        Isotopes[isoName.rstrip()] = [float(S[10:19]),float(S[19:25])]
125                elif S[5:9] == '_SIZ':
126                    Z=int(S[:2])
127                    Symbol = S[3:5].strip().lower().capitalize()
128                    Drad = float(S[12:22])
129                    Arad = float(S[22:32])
130                    Vdrad = float(S[32:38])
131                    Color = ET.ElTable[Elements.index(Symbol)][6]
132    FFdata.close()
133    AtomInfo={'Symbol':Symbol,'Isotopes':Isotopes,'Mass':Mass,'Z':Z,'Drad':Drad,'Arad':Arad,'Vdrad':Vdrad,'Color':Color}   
134    return AtomInfo
135     
136def GetXsectionCoeff(El):
137    """Read atom orbital scattering cross sections for fprime calculations via Cromer-Lieberman algorithm
138    @param El: 2 character element symbol
139    @return: Orbs: list of orbitals each a dictionary with detailed orbital information used by FPcalc
140    each dictionary is:
141    'OrbName': Orbital name read from file
142    'IfBe' 0/2 depending on orbital
143    'BindEn': binding energy
144    'BB': BindEn/0.02721
145    'XSectIP': 5 cross section inflection points
146    'ElEterm': energy correction term
147    'SEdge': absorption edge for orbital
148    'Nval': 10/11 depending on IfBe
149    'LEner': 10/11 values of log(energy)
150    'LXSect': 10/11 values of log(cross section)
151    """
152    AU = 2.80022e+7
153    C1 = 0.02721
154    ElS = El.upper()
155    ElS = ElS.ljust(2)
156    filename = os.path.join(os.path.split(__file__)[0],'Xsect.dat')
157    try:
158        xsec = open(filename,'Ur')
159    except:
160        print '**** ERROR - File Xsect.dat not found in directory %s' % sys.path[0]
161        sys.exit()
162    S = '1'
163    Orbs = []
164    while S:
165        S = xsec.readline()
166        if S[:2] == ElS:
167            S = S[:-1]+xsec.readline()[:-1]+xsec.readline()
168            OrbName = S[9:14]
169            S = S[14:]
170            IfBe = int(S[0])
171            S = S[1:]
172            val = S.split()
173            BindEn = float(val[0])
174            BB = BindEn/C1
175            Orb = {'OrbName':OrbName,'IfBe':IfBe,'BindEn':BindEn,'BB':BB}
176            Energy = []
177            XSect = []
178            for i in range(11):
179                Energy.append(float(val[2*i+1]))
180                XSect.append(float(val[2*i+2]))
181            XSecIP = []
182            for i in range(5): XSecIP.append(XSect[i+5]/AU)
183            Orb['XSecIP'] = XSecIP
184            if IfBe == 0:
185                Orb['SEdge'] = XSect[10]/AU
186                Nval = 11
187            else:
188                Orb['ElEterm'] = XSect[10]
189                del Energy[10]
190                del XSect[10]
191                Nval = 10
192                Orb['SEdge'] = 0.0
193            Orb['Nval'] = Nval
194            D = dict(zip(Energy,XSect))
195            Energy.sort()
196            X = []
197            for key in Energy:
198                X.append(D[key])
199            XSect = X
200            LEner = []
201            LXSect = []
202            for i in range(Nval):
203                LEner.append(math.log(Energy[i]))
204                if XSect[i] > 0.0:
205                    LXSect.append(math.log(XSect[i]))
206                else:
207                    LXSect.append(0.0)
208            Orb['LEner'] = LEner
209            Orb['LXSect'] = LXSect
210            Orbs.append(Orb)
211    xsec.close()
212    return Orbs
213   
214def GetMagFormFacCoeff(El):
215    """Read magnetic form factor data from atomdata.asc file
216    @param El: 2 character element symbol
217    @return: MagFormFactors: list of all magnetic form factors dictionaries for element El.
218    each dictionary contains:
219    'Symbol':Symbol
220    'Z':Z
221    'mfa': 4 MA coefficients
222    'nfa': 4 NA coefficients
223    'mfb': 4 MB coefficients
224    'nfb': 4 NB coefficients
225    'mfc': MC coefficient
226    'nfc': NC coefficient
227    """
228    ElS = El.upper()
229    ElS = ElS.rjust(2)
230    filename = os.path.join(os.path.split(__file__)[0],'atmdata.dat')
231    try:
232        FFdata = open(filename,'Ur')
233    except:
234        print '**** ERROR - File atmdata.dat not found in directory %s' % sys.path[0]
235        sys.exit()
236    S = '1'
237    MagFormFactors = []
238    while S:
239        S = FFdata.readline()
240        if S[3:5] == ElS:
241            if S[8:9] == 'M':
242                SN = FFdata.readline()               #'N' is assumed to follow 'M' in Atomdata.asc
243                Z=int(S[:2])
244                Symbol = S[3:7]
245                S = S[12:]
246                SN = SN[12:]
247                mfa = (float(S[:7]),float(S[14:21]),float(S[28:35]),float(S[42:49]))
248                mfb = (float(S[7:14]),float(S[21:28]),float(S[35:42]),float(S[49:56]))
249                nfa = (float(SN[:7]),float(SN[14:21]),float(SN[28:35]),float(SN[42:49]))
250                nfb = (float(SN[7:14]),float(SN[21:28]),float(SN[35:42]),float(SN[49:56]))
251                FormFac = {'Symbol':Symbol,'Z':Z,'mfa':mfa,'nfa':nfa,'mfb':mfb,'nfb':nfb,
252                    'mfc':float(S[56:63]),'nfc':float(SN[56:63])}
253                MagFormFactors.append(FormFac)
254    FFdata.close()
255    return MagFormFactors
256
257def ScatFac(El, SQ):
258    """compute value of form factor
259    @param El: element dictionary defined in GetFormFactorCoeff
260    @param SQ: (sin-theta/lambda)**2
261    @return: real part of form factor
262    """
263    fa = np.array(El['fa'])
264    fb = np.array(El['fb'])
265    t = -fb[:,np.newaxis]*SQ
266    return np.sum(fa[:,np.newaxis]*np.exp(t)[:],axis=0)+El['fc']
267       
268def BlenRes(Elist,BLtables,wave):
269    FP = np.zeros(len(Elist))
270    FPP = np.zeros(len(Elist))
271    Emev = 81.80703/wave**2
272    for i,El in enumerate(Elist):
273        BL = BLtables[El]
274        if len(BL) >= 6:
275            Emev = 81.80703/wave**2
276            G2 = BL[5]**2
277            T = [Emev-BL[4],0,0]
278            D = [T**2+G2,0,0]
279            fp = T/D
280            fpp = 1.0/D
281            if len(BL) == 8:
282                T = Emev-BL[7]
283                D = T**2+G2
284                fp += BL[6]*T/D
285                fpp += BL[6]/D
286            if len(BL) == 10:
287                T = Emev-BL[9]
288                D = T**2+G2
289                fp += BL[8]*T/D
290                fpp += BL[8]/D
291            FP[i] = (BL[2]*fp)
292            FPP[i] = (-BL[3]*fpp)
293        else:
294            FP[i] = 0.0
295            FPP[i] = 0.0
296    return FP,FPP
297   
298#def BlenRes(BLdata,wave):
299#    FP = []
300#    FPP = []
301#    Emev = 81.80703/wave**2
302#    for BL in BLdata:
303#        if len(BL) >= 6:
304#            Emev = 81.80703/wave**2
305#            G2 = BL[5]**2
306#            T = [Emev-BL[4],0,0]
307#            D = [T**2+G2,0,0]
308#            fp = T/D
309#            fpp = 1.0/D
310#            if len(BL) == 8:
311#                T = Emev-BL[7]
312#                D = T**2+G2
313#                fp += BL[6]*T/D
314#                fpp += BL[6]/D
315#            if len(BL) == 10:
316#                T = Emev-BL[9]
317#                D = T**2+G2
318#                fp += BL[8]*T/D
319#                fpp += BL[8]/D
320#            FP.append(BL[2]*fp)
321#            FPP.append(-BL[3]*fpp)
322#        else:
323#            FP.append(0.0)
324#            FPP.append(0.0)
325#    return np.array(FP),np.array(FPP)
326   
327def ComptonFac(El,SQ):
328    """compute Compton scattering factor
329    @param El: element dictionary
330    @param SQ: (sin-theta/lambda)**2
331    @return: compton scattering factor
332    """   
333    ca = np.array(El['cmpa'])
334    cb = np.array(El['cmpb'])
335    t = -cb[:,np.newaxis]*SQ       
336    return El['cmpz']-np.sum(ca[:,np.newaxis]*np.exp(t),axis=0) 
337           
338def FPcalc(Orbs, KEv):
339    """Compute real & imaginary resonant X-ray scattering factors
340    @param Orbs: list of orbital dictionaries as defined in GetXsectionCoeff
341    @param KEv: x-ray energy in keV
342    @return: C: (f',f",mu): real, imaginary parts of resonant scattering & atomic absorption coeff.
343    """
344    def Aitken(Orb, LKev):
345        Nval = Orb['Nval']
346        j = Nval-1
347        LEner = Orb['LEner']
348        for i in range(Nval):
349            if LEner[i] <= LKev: j = i
350        if j > Nval-3: j= Nval-3
351        T = [0,0,0,0,0,0]
352        LXSect = Orb['LXSect']
353        for i in range(3):
354           T[i] = LXSect[i+j]
355           T[i+3] = LEner[i+j]-LKev
356        T[1] = (T[0]*T[4]-T[1]*T[3])/(LEner[j+1]-LEner[j])
357        T[2] = (T[0]*T[5]-T[2]*T[3])/(LEner[j+2]-LEner[j])
358        T[2] = (T[1]*T[5]-T[2]*T[4])/(LEner[j+2]-LEner[j+1])
359        C = T[2]
360        return C
361   
362    def DGauss(Orb,CX,RX,ISig):
363        ALG = (0.11846344252810,0.23931433524968,0.284444444444,
364        0.23931433524968,0.11846344252810)
365        XLG = (0.04691007703067,0.23076534494716,0.5,
366        0.76923465505284,0.95308992296933)
367       
368        D = 0.0
369        B2 = Orb['BB']**2
370        R2 = RX**2
371        XSecIP = Orb['XSecIP']
372        for i in range(5):
373            X = XLG[i]
374            X2 = X**2
375            XS = XSecIP[i]
376            if ISig == 0:
377                S = BB*(XS*(B2/X2)-CX*R2)/(R2*X2-B2)
378            elif ISig == 1:
379                S = 0.5*BB*B2*XS/(math.sqrt(X)*(R2*X2-X*B2))
380            elif ISig == 2:
381                T = X*X2*R2-B2/X
382                S = 2.0*BB*(XS*B2/(T*X2**2)-(CX*R2/T))
383            else:
384                S = BB*B2*(XS-Orb['SEdge']*X2)/(R2*X2**2-X2*B2)
385            A = ALG[i]
386            D += A*S
387        return D
388   
389    AU = 2.80022e+7
390    C1 = 0.02721
391    C = 137.0367
392    FP = 0.0
393    FPP = 0.0
394    Mu = 0.0
395    LKev = math.log(KEv)
396    RX = KEv/C1
397    if Orbs:
398        for Orb in Orbs:
399            CX = 0.0
400            BB = Orb['BB']
401            BindEn = Orb['BindEn']
402            if Orb['IfBe'] != 0: ElEterm = Orb['ElEterm']
403            if BindEn <= KEv:
404                CX = math.exp(Aitken(Orb,LKev))
405                Mu += CX
406                CX /= AU
407            Corr = 0.0
408            if Orb['IfBe'] == 0 and BindEn >= KEv:
409                CX = 0.0
410                FPI = DGauss(Orb,CX,RX,3)
411                Corr = 0.5*Orb['SEdge']*BB**2*math.log((RX-BB)/(-RX-BB))/RX
412            else:
413                FPI = DGauss(Orb,CX,RX,Orb['IfBe'])
414                if CX != 0.0: Corr = -0.5*CX*RX*math.log((RX+BB)/(RX-BB))
415            FPI = (FPI+Corr)*C/(2.0*math.pi**2)
416            FPPI = C*CX*RX/(4.0*math.pi)
417            FP += FPI
418            FPP += FPPI
419        FP -= ElEterm
420   
421    return (FP, FPP, Mu)
422   
423
Note: See TracBrowser for help on using the repository browser.