Changeset 939 for trunk/GSASIIlattice.py


Ignore:
Timestamp:
Jun 2, 2013 11:07:35 PM (9 years ago)
Author:
toby
Message:

fix & cleanup unit tests; add/change doc strings for sphinx; add all G2 py files to sphinx

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIlattice.py

    r885 r939  
    11# -*- coding: utf-8 -*-
    2 '''Perform lattice-related computations'''
     2'''
     3*GSASIIlattice: Unit cells*
     4---------------------------
     5
     6Perform lattice-related computations'''
    37########### SVN repository information ###################
    48# $Date$
     
    2630   
    2731    :param sec: time in seconds
    28     return: H:M:S string (to nearest 100th second)
     32    :return: H:M:S string (to nearest 100th second)
    2933   
    3034    """
     
    8892    """Fill real & reciprocal metric tensor (G) from A
    8993
    90     :param
    91         A: reciprocal metric tensor elements as [G11,G22,G33,2*G12,2*G13,2*G23]
    92         inverse: if True return bot G and g; else just G
     94    :param A: reciprocal metric tensor elements as [G11,G22,G33,2*G12,2*G13,2*G23]
     95    :param bool inverse: if True return bot G and g; else just G
    9396    :return: reciprocal (G) & real (g) metric tensors (list of two numpy 3x3 arrays)
    9497
     
    165168       
    166169def calc_rVsq(A):
    167     """Compute the square of the reciprocal lattice volume (V* **2) from A'
     170    """Compute the square of the reciprocal lattice volume (1/V**2) from A'
    168171
    169172    """
     
    193196def Gmat2AB(G):
    194197    """Computes orthogonalization matrix from reciprocal metric tensor G
    195     returns tuple of two 3x3 numpy arrays (A,B)
    196        A for crystal to Cartesian transformations A*x = np.inner(A,x) = X
    197        B (= inverse of A) for Cartesian to crystal transformation B*X = np.inner(B,X) = x
     198
     199    :returns: tuple of two 3x3 numpy arrays (A,B)
     200
     201       * A for crystal to Cartesian transformations A*x = np.inner(A,x) = X
     202       * B (= inverse of A) for Cartesian to crystal transformation B*X = np.inner(B,X) = x
     203
    198204    """
    199205    cellstar = Gmat2cell(G)
     
    214220def cell2AB(cell):
    215221    """Computes orthogonalization matrix from unit cell constants
    216     cell is tuple with a,b,c,alpha, beta, gamma (degrees)
    217     returns tuple of two 3x3 numpy arrays (A,B)
     222
     223    :param tuple cell: a,b,c, alpha, beta, gamma (degrees)
     224    :returns: tuple of two 3x3 numpy arrays (A,B)
    218225       A for crystal to Cartesian transformations A*x = np.inner(A,x) = X
    219226       B (= inverse of A) for Cartesian to crystal transformation B*X = np.inner(B,X) = x
     
    235242    """Fill matrix (Uij) from U6 = [U11,U22,U33,U12,U13,U23]
    236243    NB: there is a non numpy version in GSASIIspc: U2Uij
    237     input:
    238         U6 - 6 terms of u11,u22,...
    239     returns:
     244
     245    :param list U6: 6 terms of u11,u22,...
     246    :returns:
    240247        Uij - numpy [3][3] array of uij
    241248    """
     
    255262def Uij2betaij(Uij,G):
    256263    """
    257     Convert Uij to beta-ij tensors
    258     input:
    259         Uij - numpy array [Uij]
    260         G - reciprocal metric tensor
    261     returns:
    262     beta-ij - numpy array [beta-ij]
     264    Convert Uij to beta-ij tensors -- stub for eventual completion
     265   
     266    :param Uij: numpy array [Uij]
     267    :param G: reciprocal metric tensor
     268    :returns: beta-ij - numpy array [beta-ij]
    263269    """
    264270    pass
     
    267273    """ calculate sin & cos of angle betwee U & V in generalized coordinates
    268274    defined by metric tensor G
    269     input:
    270         U & V - 3-vectors assume numpy arrays
    271         G - metric tensor for U & V defined space assume numpy array
    272     return:
     275
     276    :param U: 3-vectors assume numpy arrays
     277    :param V: 3-vectors assume numpy arrays
     278    :param G: metric tensor for U & V defined space assume numpy array
     279    :returns:
    273280        cos(phi) & sin(phi)
    274281    """
     
    309316       
    310317def CellAbsorption(ElList,Volume):
    311 # ElList = dictionary of element contents including mu
     318    '''Compute unit cell absorption
     319
     320    :param dict ElList: dictionary of element contents including mu and
     321      number of atoms be cell
     322    :param float Volume: unit cell volume
     323    :returns: mu-total/Volume
     324    '''
    312325    muT = 0
    313326    for El in ElList:
     
    352365   
    353366def calc_rDsq(H,A):
     367    'needs doc string'
    354368    rdsq = H[0]*H[0]*A[0]+H[1]*H[1]*A[1]+H[2]*H[2]*A[2]+H[0]*H[1]*A[3]+H[0]*H[2]*A[4]+H[1]*H[2]*A[5]
    355369    return rdsq
    356370   
    357371def calc_rDsq2(H,G):
     372    'needs doc string'
    358373    return np.inner(H,np.inner(G,H))
    359374   
    360375def calc_rDsqZ(H,A,Z,tth,lam):
     376    'needs doc string'
    361377    rpd = np.pi/180.
    362378    rdsq = calc_rDsq(H,A)+Z*sind(tth)*2.0*rpd/lam**2
     
    364380       
    365381def MaxIndex(dmin,A):
     382    'needs doc string'
    366383    Hmax = [0,0,0]
    367384    try:
     
    374391   
    375392def sortHKLd(HKLd,ifreverse,ifdup):
    376     #HKLd is a list of [h,k,l,d,...]; ifreverse=True for largest d first
    377     #ifdup = True if duplicate d-spacings allowed
     393    '''needs doc string
     394
     395    :param HKLd: a list of [h,k,l,d,...];
     396    :param ifreverse: True for largest d first
     397    :param ifdup: True if duplicate d-spacings allowed
     398    '''
    378399    T = []
    379400    for i,H in enumerate(HKLd):
     
    394415   
    395416def SwapIndx(Axis,H):
     417    'needs doc string'
    396418    if Axis in [1,-1]:
    397419        return H
     
    402424       
    403425def Rh2Hx(Rh):
     426    'needs doc string'
    404427    Hx = [0,0,0]
    405428    Hx[0] = Rh[0]-Rh[1]
     
    409432   
    410433def Hx2Rh(Hx):
    411         Rh = [0,0,0]
    412         itk = -Hx[0]+Hx[1]+Hx[2]
    413         if itk%3 != 0:
    414             return 0        #error - not rhombohedral reflection
    415         else:
    416             Rh[1] = itk/3
    417             Rh[0] = Rh[1]+Hx[0]
    418             Rh[2] = Rh[1]-Hx[1]
    419             if Rh[0] < 0:
    420                 for i in range(3):
    421                     Rh[i] = -Rh[i]
    422             return Rh
     434    'needs doc string'
     435    Rh = [0,0,0]
     436    itk = -Hx[0]+Hx[1]+Hx[2]
     437    if itk%3 != 0:
     438        return 0        #error - not rhombohedral reflection
     439    else:
     440        Rh[1] = itk/3
     441        Rh[0] = Rh[1]+Hx[0]
     442        Rh[2] = Rh[1]-Hx[1]
     443        if Rh[0] < 0:
     444            for i in range(3):
     445                Rh[i] = -Rh[i]
     446        return Rh
    423447       
    424448def CentCheck(Cent,H):
     449    'needs doc string'
    425450    h,k,l = H
    426451    if Cent == 'A' and (k+l)%2:
     
    444469    :param center: one of: 'P', 'C', 'I', 'F', 'R' (see SGLatt from GSASIIspc.SpcGroup)
    445470    :param system: one of 'cubic', 'hexagonal', 'tetragonal', 'orthorhombic', 'trigonal' (for R)
    446              'monoclinic', 'triclinic' (see SGSys from GSASIIspc.SpcGroup)
     471      'monoclinic', 'triclinic' (see SGSys from GSASIIspc.SpcGroup)
    447472    :return: a number between 0 and 13
    448           or throws a ValueError exception if the combination of center, system is not found (i.e. non-standard)
     473      or throws a ValueError exception if the combination of center, system is not found (i.e. non-standard)
     474
    449475    """
    450476    if center.upper() == 'F' and system.lower() == 'cubic':
     
    482508     
    483509    :param dmin: minimum d-spacing in A
    484     :param Bravais: lattice type (see GetBraviasNum)
    485         Bravais is one of::
     510    :param Bravais: lattice type (see GetBraviasNum). Bravais is one of::
    486511             0 F cubic
    487512             1 I cubic
     
    494519             8 I orthorhombic
    495520             9 C orthorhombic
    496             10 P orthorhombic
    497             11 C monoclinic
    498             12 P monoclinic
    499             13 P triclinic
     521             10 P orthorhombic
     522             11 C monoclinic
     523             12 P monoclinic
     524             13 P triclinic
     525           
    500526    :param A: reciprocal metric tensor elements as [G11,G22,G33,2*G12,2*G13,2*G23]
    501527    :return: HKL unique d list of [h,k,l,d,-1] sorted with largest d first
     
    590616   
    591617def getHKLmax(dmin,SGData,A):
    592     #finds maximum allowed hkl for given A within dmin
     618    'finds maximum allowed hkl for given A within dmin'
    593619    SGLaue = SGData['SGLaue']
    594620    if SGLaue in ['3R','3mR']:        #Rhombohedral axes
     
    609635   
    610636    :param dmin: minimum d-spacing
    611     :param SGData: space group dictionary with at least::
    612    
    613         'SGLaue': Laue group symbol: one of '-1','2/m','mmm','4/m','6/m','4/mmm','6/mmm',
    614                  '3m1', '31m', '3', '3R', '3mR', 'm3', 'm3m'
    615         'SGLatt': lattice centering: one of 'P','A','B','C','I','F'
    616         'SGUniq': code for unique monoclinic axis one of 'a','b','c' (only if 'SGLaue' is '2/m')
    617             otherwise ' '
     637    :param SGData: space group dictionary with at least
     638   
     639        * 'SGLaue': Laue group symbol: one of '-1','2/m','mmm','4/m','6/m','4/mmm','6/mmm', '3m1', '31m', '3', '3R', '3mR', 'm3', 'm3m'
     640        * 'SGLatt': lattice centering: one of 'P','A','B','C','I','F'
     641        * 'SGUniq': code for unique monoclinic axis one of 'a','b','c' (only if 'SGLaue' is '2/m') otherwise an empty string
    618642       
    619643    :param A: reciprocal metric tensor elements as [G11,G22,G33,2*G12,2*G13,2*G23]
     
    722746#Spherical harmonics routines
    723747def OdfChk(SGLaue,L,M):
     748    'needs doc string'
    724749    if not L%2 and abs(M) <= L:
    725750        if SGLaue == '0':                      #cylindrical symmetry
     
    758783       
    759784def GenSHCoeff(SGLaue,SamSym,L,IfLMN=True):
     785    'needs doc string'
    760786    coeffNames = []
    761787    for iord in [2*i+2 for i in range(L/2)]:
     
    771797   
    772798def CrsAng(H,cell,SGData):
     799    'needs doc string'
    773800    a,b,c,al,be,ga = cell
    774801    SQ3 = 1.732050807569
     
    831858def SamAng(Tth,Gangls,Sangl,IFCoup):
    832859    """Compute sample orientation angles vs laboratory coord. system
    833     input:
    834         Tth:        Signed theta                                   
    835         Gangls:     Sample goniometer angles phi,chi,omega,azmuth 
    836         Sangl:      Sample angle zeros om-0, chi-0, phi-0         
    837         IFCoup:     =.TRUE. if omega & 2-theta coupled in CW scan
    838     returns: 
     860
     861    :param Tth:        Signed theta                                   
     862    :param Gangls:     Sample goniometer angles phi,chi,omega,azmuth 
     863    :param Sangl:      Sample angle zeros om-0, chi-0, phi-0         
     864    :param IFCoup:     True if omega & 2-theta coupled in CW scan
     865    :returns: 
    839866        psi,gam:    Sample odf angles                             
    840867        dPSdA,dGMdA:    Angle zero derivatives
     
    942969
    943970def GetKcl(L,N,SGLaue,phi,beta):
     971    'needs doc string'
    944972    import pytexture as ptx
    945973    RSQ2PI = 0.3989422804014
     
    969997   
    970998def GetKsl(L,M,SamSym,psi,gam):
     999    'needs doc string'
    9711000    import pytexture as ptx
    9721001    RSQPI = 0.5641895835478
     
    10241053   
    10251054def Glnh(Start,SHCoef,psi,gam,SamSym):
     1055    'needs doc string'
    10261056    import pytexture as ptx
    10271057    RSQPI = 0.5641895835478
     
    10471077
    10481078def Flnh(Start,SHCoef,phi,beta,SGData):
     1079    'needs doc string'
    10491080    import pytexture as ptx
    10501081   
     
    10851116   
    10861117def polfcal(ODFln,SamSym,psi,gam):
     1118    'needs doc string'
    10871119    import pytexture as ptx
    10881120    RSQPI = 0.5641895835478
     
    11071139   
    11081140def invpolfcal(ODFln,SGData,phi,beta):
     1141    'needs doc string'
    11091142    import pytexture as ptx
    11101143   
     
    11431176   
    11441177def textureIndex(SHCoef):
     1178    'needs doc string'
    11451179    Tindx = 1.0
    11461180    for term in SHCoef:
     
    11491183    return Tindx
    11501184   
    1151 # output from uctbx computed on platform darwin on 2010-05-28
     1185# self-test materials follow.
     1186selftestlist = []
     1187'''Defines a list of self-tests'''
     1188selftestquiet = True
     1189def _ReportTest():
     1190    'Report name and doc string of current routine when ``selftestquiet`` is False'
     1191    if not selftestquiet:
     1192        import inspect
     1193        caller = inspect.stack()[1][3]
     1194        doc = eval(caller).__doc__
     1195        if doc is not None:
     1196            print('testing '+__file__+' with '+caller+' ('+doc+')')
     1197        else:
     1198            print('testing '+__file__()+" with "+caller)
    11521199NeedTestData = True
    11531200def TestData():
     
    11561203    NeedTestData = False
    11571204    global CellTestData
     1205    # output from uctbx computed on platform darwin on 2010-05-28
    11581206    CellTestData = [
    11591207# cell, g, G, cell*, V, V*
     
    12711319        tcell = Gmat2cell(G)
    12721320        assert np.allclose(tcell,trcell),msg
     1321selftestlist.append(test0)
    12731322
    12741323def test1():
     1324    'test cell2A and A2Gmat'
     1325    _ReportTest()
    12751326    if NeedTestData: TestData()
    12761327    msg = 'test cell2A and A2Gmat'
     
    12791330        assert np.allclose(G,tG),msg
    12801331        assert np.allclose(g,tg),msg
     1332selftestlist.append(test1)
    12811333
    12821334def test2():
     1335    'test Gmat2A, A2cell, A2Gmat, Gmat2cell'
     1336    _ReportTest()
    12831337    if NeedTestData: TestData()
    12841338    msg = 'test Gmat2A, A2cell, A2Gmat, Gmat2cell'
     
    12871341        tcell = A2cell(Gmat2A(G))
    12881342        assert np.allclose(cell,tcell),msg
     1343selftestlist.append(test2)
    12891344
    12901345def test3():
     1346    'test invcell2Gmat'
     1347    _ReportTest()
    12911348    if NeedTestData: TestData()
    12921349    msg = 'test invcell2Gmat'
     
    12951352        assert np.allclose(G,tG),msg
    12961353        assert np.allclose(g,tg),msg
     1354selftestlist.append(test3)
    12971355
    12981356def test4():
     1357    'test calc_rVsq, calc_rV, calc_V'
     1358    _ReportTest()
    12991359    if NeedTestData: TestData()
    13001360    msg = 'test calc_rVsq, calc_rV, calc_V'
     
    13021362        assert np.allclose(calc_rV(cell2A(cell)),trV), msg
    13031363        assert np.allclose(calc_V(cell2A(cell)),tV), msg
     1364selftestlist.append(test4)
    13041365
    13051366def test5():
     1367    'test A2invcell'
     1368    _ReportTest()
    13061369    if NeedTestData: TestData()
    13071370    msg = 'test A2invcell'
     
    13091372        rcell = A2invcell(cell2A(cell))
    13101373        assert np.allclose(rcell,trcell),msg
     1374selftestlist.append(test5)
    13111375
    13121376def test6():
     1377    'test cell2AB'
     1378    _ReportTest()
    13131379    if NeedTestData: TestData()
    13141380    msg = 'test cell2AB'
     
    13241390            assert np.allclose(ortho,to), msg
    13251391            assert np.allclose(frac,tf), msg
    1326 
    1327 # test GetBraviasNum(...) and GenHBravais(...)
     1392selftestlist.append(test6)
     1393
    13281394def test7():
     1395    'test GetBraviasNum(...) and GenHBravais(...)'
     1396    _ReportTest()
    13291397    import os.path
    13301398    import sys
     
    13721440            else:
    13731441                assert 0,'No match for %s at %s (%s)' % ((h,k,l),d,key)
     1442selftestlist.append(test7)
    13741443
    13751444def test8():
     1445    'test GenHLaue'
     1446    _ReportTest()
    13761447    import GSASIIspc as spc
    13771448    import sgtbxlattinp
     
    14331504            #for hkllist,dref in sgtbxlattinp.sgtbx8[key][1]: print '  ',hkllist,dref
    14341505            #print center, Laue, Axis, system
     1506selftestlist.append(test8)
    14351507           
    14361508def test9():
     1509    'test GenHLaue'
     1510    _ReportTest()
    14371511    import GSASIIspc as G2spc
    14381512    if NeedTestData: TestData()
     
    14481522        hkls = np.array(GenHLaue(dmin,SGData,A))
    14491523        hklN = hkls.T[:3].T
    1450         print spc,hklO.shape,hklN.shape
     1524        #print spc,hklO.shape,hklN.shape
     1525        err = True
    14511526        for H in hklO:
    14521527            if H not in hklN:
    14531528                print H,' missing from hkl from GSASII'
     1529                err = False
     1530        assert(err)
     1531selftestlist.append(test9)
    14541532       
    14551533       
     
    14571535
    14581536if __name__ == '__main__':
    1459     test0()
    1460     test1()
    1461     test2()
    1462     test3()
    1463     test4()
    1464     test5()
    1465     test6()
    1466     test7()
    1467     test8()
     1537    # run self-tests
     1538    selftestquiet = False
     1539    for test in selftestlist:
     1540        test()
    14681541    print "OK"
Note: See TracChangeset for help on using the changeset viewer.