Changeset 57 for trunk/GSASIIspc.py


Ignore:
Timestamp:
Apr 29, 2010 9:30:47 PM (15 years ago)
Author:
toby
Message:

fixup unit test for GSASIIspg.py; provide files & Makefile for pyspg.for

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/GSASIIspc.py

    r41 r57  
    55import sys
    66import os.path as ospath
    7 # determine a binary path pased on the host OS and the python version, path is relative to
    8 # location of this file
     7
     8# determine a binary path based on the host OS and the python version, path
     9# is relative to the location of this file
    910if sys.platform == "win32":
    1011    bindir = 'binwin%d.%d' % sys.version_info[0:2]
    1112elif sys.platform == "darwin":
    1213    bindir = 'binmac%d.%d' % sys.version_info[0:2]
     14    if sys.byteorder == 'big':
     15        bindir += 'PPC'
     16elif sys.platform == "linux2":
     17    bindir = 'binlinux%d.%d' % sys.version_info[0:2]
    1318else:
    1419    bindir = 'bin'
    15 if ospath.exists(ospath.join(sys.path[0],bindir)) and ospath.join(sys.path[0],bindir) not in sys.path:
    16     sys.path.insert(0,ospath.join(sys.path[0],bindir))
     20mypath = ospath.split(ospath.abspath( __file__ ))[0]
     21bindir = ospath.join(mypath,bindir)
     22if ospath.exists(bindir):
     23    if bindir not in sys.path: sys.path.insert(0,bindir)
     24else:
     25    print 'Expected binary directory (%s) for pyspg not found' % bindir
     26# use local bin directory preferentially
     27bindir = ospath.join(mypath,'bin')
     28if ospath.exists(bindir):
     29    if bindir not in sys.path: sys.path.insert(0,bindir)
    1730
    1831import pyspg as pyd
     
    499512    return GetKNsym(str(Isym)),Mult
    500513   
     514# self-test materials follow. Requires files in directory testinp
     515def test0():
     516    '''test #0: exercise MoveToUnitCell'''
     517    msg = "MoveToUnitCell failed"
     518    v = [0,1,2,-1,-2]; MoveToUnitCell(v); assert v==[0,0,0,0,0], msg
     519    v = np.array([-.1]); MoveToUnitCell(v); assert abs(v-0.9) < 1e-6, msg
     520    v = np.array([.1]); MoveToUnitCell(v); assert abs(v-0.1) < 1e-6, msg
     521
     522def test1():
     523    ''' test #1: SpcGroup and SGPrint against previous results'''
     524    testdir = ospath.join(mypath,'testinp')
     525    if ospath.exists(testdir):
     526        if testdir not in sys.path: sys.path.insert(0,testdir)
     527    import spctestinp
     528    def CompareSpcGroup(spc, referr, refdict, reflist):
     529        'Compare output from GSASIIspc.SpcGroup with results from a previous run'
     530        # if an error is reported, the dictionary can be ignored
     531        msg = "failed on space group %s" % spc
     532        result = SpcGroup(spc)
     533        if result[0] == referr and referr > 0: return True
     534        keys = result[1].keys()
     535        #print result[1]['SpGrp']
     536        assert len(keys) == len(refdict.keys()), msg
     537        for key in keys:
     538        #print key, type(refdict[key])
     539            if key == 'SGOps' or  key == 'SGCen':
     540                assert len(refdict[key]) == len(result[1][key]), msg
     541                for i in range(len(refdict[key])):
     542                    assert np.allclose(result[1][key][i][0],refdict[key][i][0]), msg
     543                    assert np.allclose(result[1][key][i][1],refdict[key][i][1]), msg
     544            else:
     545                assert result[1][key] == refdict[key], msg
     546        assert reflist == SGPrint(result[1]), 'SGPrint ' +msg
     547    for spc in spctestinp.SGdat:
     548        CompareSpcGroup(spc, 0, spctestinp.SGdat[spc], spctestinp.SGlist[spc] )
     549
     550def test2():
     551    ''' test #2: SpcGroup against cctbx (sgtbx) computations'''
     552    testdir = ospath.join(mypath,'testinp')
     553    if ospath.exists(testdir):
     554        if testdir not in sys.path: sys.path.insert(0,testdir)
     555    import sgtbxtestinp
     556    def CompareWcctbx(spcname, cctbx_in, debug=0):
     557        'Compare output from GSASIIspc.SpcGroup with results from cctbx.sgtbx'
     558        cctbx = cctbx_in[:] # make copy so we don't delete from the original
     559        spc = (SpcGroup(spcname))[1]
     560        if debug: print spc['SpGrp']
     561        if debug: print spc['SGCen']
     562        latticetype = spcname.strip().upper()[0]
     563        # lattice type of R implies Hexagonal centering", fix the rhombohedral settings
     564        if latticetype == "R" and len(spc['SGCen']) == 1: latticetype = 'P'
     565        assert latticetype == spc['SGLatt'], "Failed: %s does not match Lattice: %s" % (spcname, spc['SGLatt'])
     566        onebar = [1]
     567        if spc['SGInv']: onebar.append(-1)
     568        for (op,off) in spc['SGOps']:
     569            for inv in onebar:
     570                for cen in spc['SGCen']:
     571                    noff = off + cen
     572                    MoveToUnitCell(noff)
     573                    mult = tuple((op*inv).ravel().tolist())
     574                    if debug: print "\n%s: %s + %s" % (spcname,mult,noff)
     575                    for refop in cctbx:
     576                        if debug: print refop
     577                        # check the transform
     578                        if refop[:9] != mult: continue
     579                        if debug: print "mult match"
     580                        # check the translation
     581                        reftrans = list(refop[-3:])
     582                        MoveToUnitCell(reftrans)
     583                        if all(abs(noff - reftrans) < 1.e-5):
     584                            cctbx.remove(refop)
     585                            break
     586                    else:
     587                        assert False, "failed on %s:\n\t %s + %s" % (spcname,mult,noff)
     588    for key in sgtbxtestinp.sgtbx:
     589        CompareWcctbx(key, sgtbxtestinp.sgtbx[key])
     590
     591def test3():
     592    ''' test #3: exercise SytSym (includes GetOprPtrName, GenAtom, GetKNsym)
     593     for selected space groups against info in IT Volume A '''
     594    def ExerciseSiteSym (spc, crdlist):
     595        'compare site symmetries and multiplicities for a specified space group'
     596        msg = "failed on site sym test for %s" % spc
     597        (E,S) = SpcGroup(spc)
     598        assert not E, msg
     599        for t in crdlist:
     600            symb, m = SytSym(t[0],S)
     601            if symb.strip() != t[2].strip() or m != t[1]:
     602                print spc,t[0],m,symb
     603            assert m == t[1]
     604            #assert symb.strip() == t[2].strip()
     605
     606    ExerciseSiteSym('p 1',[
     607            ((0.13,0.22,0.31),1,'1'),
     608            ((0,0,0),1,'1'),
     609            ])
     610    ExerciseSiteSym('p -1',[
     611            ((0.13,0.22,0.31),2,'1'),
     612            ((0,0.5,0),1,'-1'),
     613            ])
     614    ExerciseSiteSym('C 2/c',[
     615            ((0.13,0.22,0.31),8,'1'),
     616            ((0.0,.31,0.25),4,'2(010)'),
     617            ((0.25,.25,0.5),4,'-1'),
     618            ((0,0.5,0),4,'-1'),
     619            ])
     620    ExerciseSiteSym('p 2 2 2',[
     621            ((0.13,0.22,0.31),4,'1'),
     622            ((0,0.5,.31),2,'2(001)'),
     623            ((0.5,.31,0.5),2,'2(010)'),
     624            ((.11,0,0),2,'2(100)'),
     625            ((0,0.5,0),1,'222'),
     626            ])
     627    ExerciseSiteSym('p 4/n',[
     628            ((0.13,0.22,0.31),8,'1'),
     629            ((0.25,0.75,.31),4,'2(001)'),
     630            ((0.5,0.5,0.5),4,'-1'),
     631            ((0,0.5,0),4,'-1'),
     632            ((0.25,0.25,.31),2,'4(001)'),
     633            ((0.25,.75,0.5),2,'-4(001)'),
     634            ((0.25,.75,0.0),2,'-4(001)'),
     635            ])
     636    ExerciseSiteSym('p 31 2 1',[
     637            ((0.13,0.22,0.31),6,'1'),
     638            ((0.13,0.0,0.833333333),3,'2(100)'),
     639            ((0.13,0.13,0.),3,'2(110)'),
     640            ])
     641    ExerciseSiteSym('R 3 c',[
     642            ((0.13,0.22,0.31),18,'1'),
     643            ((0.0,0.0,0.31),6,'3'),
     644            ])
     645    ExerciseSiteSym('R 3 c R',[
     646            ((0.13,0.22,0.31),6,'1'),
     647            ((0.31,0.31,0.31),2,'3(111)'),
     648            ])
     649    ExerciseSiteSym('P 63 m c',[
     650            ((0.13,0.22,0.31),12,'1'),
     651            ((0.11,0.22,0.31),6,'m(100)'),
     652            ((0.333333,0.6666667,0.31),2,'3m(100)'),
     653            ((0,0,0.31),2,'3m(100)'),
     654            ])
     655    ExerciseSiteSym('I a -3',[
     656            ((0.13,0.22,0.31),48,'1'),
     657            ((0.11,0,0.25),24,'2(100)'),
     658            ((0.11,0.11,0.11),16,'3(111)'),
     659            ((0,0,0),8,'-3(111)'),
     660            ])
     661
     662if __name__ == '__main__':
     663    test0()
     664    test1()
     665    test2()
     666    test3()
     667    print "OK"
Note: See TracChangeset for help on using the changeset viewer.