コード例 #1
0
 def test_read_rawxyz_bad(self):
     """check exceptions when reading unsupported xy file"""
     stru = self.stru
     self.assertRaises(StructureFormatError, stru.read,
                       datafile('hexagon-raw-bad.xyz'), self.format)
     self.assertRaises(StructureFormatError, stru.read,
                       datafile('hexagon-raw.xy'), self.format)
     return
コード例 #2
0
 def test_read_rawxyz_bad(self):
     """check exceptions when reading unsupported xy file"""
     stru = self.stru
     self.assertRaises(StructureFormatError, stru.read,
                       datafile('hexagon-raw-bad.xyz'), self.format)
     self.assertRaises(StructureFormatError, stru.read,
                       datafile('hexagon-raw.xy'), self.format)
     return
コード例 #3
0
 def test_read_pdffit_bad(self):
     """check exceptions when reading invalid pdffit file"""
     stru = self.stru
     self.assertRaises(StructureFormatError, stru.read,
             datafile('Ni-bad.stru'), self.format)
     self.assertRaises(StructureFormatError, stru.read,
             datafile('bucky.xyz'), self.format)
     return
コード例 #4
0
 def test_read_rawxyz(self):
     """check reading of raw xyz file"""
     stru = self.stru
     stru.read(datafile('bucky-raw.xyz'), self.format)
     s_els = [a.element for a in stru]
     self.assertEqual(stru.title, 'bucky-raw')
     self.assertEqual(s_els, 60 * [''])
     stru.read(datafile('hexagon-raw.xyz'), self.format)
     zs = [a.xyz[-1] for a in stru]
     self.assertEqual(zs, 6 * [0.0])
     return
コード例 #5
0
 def test_read_rawxyz(self):
     """check reading of raw xyz file"""
     stru = self.stru
     stru.read(datafile('bucky-raw.xyz'), self.format)
     s_els = [a.element for a in stru]
     self.assertEqual(stru.title, 'bucky-raw')
     self.assertEqual(s_els, 60*[''])
     stru.read(datafile('hexagon-raw.xyz'), self.format)
     zs = [a.xyz[-1] for a in stru]
     self.assertEqual(zs, 6*[0.0])
     return
コード例 #6
0
 def setUp(self):
     # load test structures once
     if TestSuperCell.stru_cdse is None:
         cdsefile = datafile("CdSe_bulk.stru")
         TestSuperCell.stru_cdse = Structure(filename=cdsefile)
     if TestSuperCell.stru_ni is None:
         nifile = datafile("Ni.stru")
         TestSuperCell.stru_ni = Structure(filename=nifile)
     # bring them to the instance
     self.stru_cdse = TestSuperCell.stru_cdse
     self.stru_ni = TestSuperCell.stru_ni
     return
コード例 #7
0
 def test_writeStr_pdffit(self):
     """check writing of normal xyz file"""
     stru = self.stru
     stru.read(datafile('Ni.stru'), self.format)
     with open(datafile('Ni.stru')) as fp:
         f_s = fp.read()
     f_s = re.sub('[ \t]+', ' ', f_s)
     f_s = re.sub('[ \t]+\n', '\n', f_s)
     s_s = stru.writeStr(self.format)
     s_s = re.sub('[ \t]+', ' ', s_s)
     self.assertEqual(f_s, s_s)
     return
コード例 #8
0
 def setUp(self):
     # load test structures once
     if TestSuperCell.stru_cdse is None:
         cdsefile = datafile("CdSe_bulk.stru")
         TestSuperCell.stru_cdse = Structure(filename=cdsefile)
     if TestSuperCell.stru_ni is None:
         nifile = datafile("Ni.stru")
         TestSuperCell.stru_ni = Structure(filename=nifile)
     # bring them to the instance
     self.stru_cdse = TestSuperCell.stru_cdse
     self.stru_ni = TestSuperCell.stru_ni
     return
コード例 #9
0
 def test_read_xyz_bad(self):
     """check exceptions when reading invalid xyz file"""
     stru = self.stru
     self.assertRaises(StructureFormatError, stru.read,
                       datafile('bucky-bad1.xyz'), self.format)
     self.assertRaises(StructureFormatError, stru.read,
                       datafile('bucky-bad2.xyz'), self.format)
     self.assertRaises(StructureFormatError, stru.read,
                       datafile('bucky-plain.xyz'), self.format)
     self.assertRaises(StructureFormatError, stru.read,
                       datafile('hexagon-raw.xy'), self.format)
     return
コード例 #10
0
 def test_read_xyz_bad(self):
     """check exceptions when reading invalid xyz file"""
     stru = self.stru
     self.assertRaises(StructureFormatError, stru.read,
                       datafile('bucky-bad1.xyz'), self.format)
     self.assertRaises(StructureFormatError, stru.read,
                       datafile('bucky-bad2.xyz'), self.format)
     self.assertRaises(StructureFormatError, stru.read,
                       datafile('bucky-plain.xyz'), self.format)
     self.assertRaises(StructureFormatError, stru.read,
                       datafile('hexagon-raw.xy'), self.format)
     return
コード例 #11
0
 def test_rwStr_pdb_CdSe(self):
     """check conversion to PDB file format"""
     stru = self.stru
     stru.read(datafile('CdSe_bulk.stru'), 'pdffit')
     s = stru.writeStr(self.format)
     # all lines should be 80 characters long
     linelens = [len(l) for l in s.split('\n') if l != ""]
     self.assertEqual(linelens, len(linelens)*[80])
     # now clean and re-read structure
     stru = Structure()
     stru.readStr(s, self.format)
     s_els = [a.element for a in stru]
     f_els = ['Cd', 'Cd', 'Se', 'Se']
     self.assertEqual(s_els, f_els)
     s_lat = [stru.lattice.a, stru.lattice.b, stru.lattice.c,
              stru.lattice.alpha, stru.lattice.beta, stru.lattice.gamma]
     f_lat = [4.235204, 4.235204, 6.906027, 90.0, 90.0, 120.0]
     self.assertTrue(numpy.allclose(s_lat, f_lat, atol=5e-4))
     a0 = stru[0]
     s_Uii = [a0.U[i,i] for i in range(3)]
     f_Uii = [0.01303035, 0.01303035, 0.01401959]
     self.assertTrue(numpy.allclose(s_Uii, f_Uii, atol=5e-4))
     s_sigUii = [a0.sigU[i,i] for i in range(3)]
     f_sigUii = [0.00011127, 0.00011127, 0.00019575]
     self.assertTrue(numpy.allclose(s_sigUii, f_sigUii, atol=5e-4))
     s_title = stru.title
     f_title = "Cell structure file of CdSe #186"
     self.assertEqual(s_title, f_title)
コード例 #12
0
ファイル: testp_cif.py プロジェクト: diffpy/diffpy.Structure
 def test_badspacegroup_cif(self):
     """Test reading of CIF file with unrecognized space group.
     """
     ptest = self.ptest
     filename = datafile('badspacegroup.cif')
     self.assertRaises(StructureFormatError, ptest.parseFile, filename)
     return
コード例 #13
0
 def test_rwStr_pdb_CdSe(self):
     """check conversion to PDB file format"""
     stru = self.stru
     stru.read(datafile('CdSe_bulk.stru'), 'pdffit')
     s = stru.writeStr(self.format)
     # all lines should be 80 characters long
     linelens = [len(l) for l in s.split('\n') if l != ""]
     self.assertEqual(linelens, len(linelens) * [80])
     # now clean and re-read structure
     stru = Structure()
     stru.readStr(s, self.format)
     s_els = [a.element for a in stru]
     f_els = ['Cd', 'Cd', 'Se', 'Se']
     self.assertEqual(s_els, f_els)
     s_lat = [
         stru.lattice.a, stru.lattice.b, stru.lattice.c, stru.lattice.alpha,
         stru.lattice.beta, stru.lattice.gamma
     ]
     f_lat = [4.235204, 4.235204, 6.906027, 90.0, 90.0, 120.0]
     self.assertTrue(numpy.allclose(s_lat, f_lat, atol=5e-4))
     a0 = stru[0]
     s_Uii = [a0.U[i, i] for i in range(3)]
     f_Uii = [0.01303035, 0.01303035, 0.01401959]
     self.assertTrue(numpy.allclose(s_Uii, f_Uii, atol=5e-4))
     s_sigUii = [a0.sigU[i, i] for i in range(3)]
     f_sigUii = [0.00011127, 0.00011127, 0.00019575]
     self.assertTrue(numpy.allclose(s_sigUii, f_sigUii, atol=5e-4))
     s_title = stru.title
     f_title = "Cell structure file of CdSe #186"
     self.assertEqual(s_title, f_title)
コード例 #14
0
 def test_goodkwarg(self):
     """check loading of CIF file and passing of parser keyword argument.
     """
     f = datafile('graphite.cif')
     stru = loadStructure(f, eps=1e-10)
     self.assertEqual(8, len(stru))
     return
コード例 #15
0
 def test_except_other_formats(self):
     """check exceptions when reading files in other formats"""
     badfiles = [
         'LiCl-bad.cif',
         'PbTe.cif',
         'arginine.pdb',
         'ZnSb_RT_Q28X_VM_20_fxiso.rstr',
         'Ni-bad.stru',
         'Ni.stru',
         'BubbleRaftShort.xcfg',
         'bucky-bad1.xyz',
         'bucky-bad2.xyz',
         'bucky-plain-bad.xyz',
         'bucky-plain.xyz',
         'bucky-raw.xyz',
         'bucky.xyz',
         'hexagon-raw-bad.xyz',
         'hexagon-raw.xyz',
     ]
     for ft in badfiles:
         ff = datafile(ft)
         self.assertRaises(StructureFormatError,
                           self.stru.read,
                           ff,
                           format=self.format)
     return
コード例 #16
0
 def test_discus(self):
     """check loading of discus file format
     """
     f = datafile('Ni-discus.stru')
     stru = loadStructure(f)
     self.assertTrue(type(stru) is PDFFitStructure)
     return
コード例 #17
0
 def test_read_pdffit_Ni(self):
     """check reading of Ni pdffit structure file"""
     stru = self.stru
     stru.read(datafile('Ni.stru'), self.format)
     f_title = "structure Ni  FCC"
     self.assertEqual(stru.title, f_title)
     self.assertEqual(stru.pdffit['spcgr'], 'Fm-3m')
     s_lat = [ stru.lattice.a, stru.lattice.b, stru.lattice.c,
         stru.lattice.alpha, stru.lattice.beta, stru.lattice.gamma ]
     f_lat = [3.52, 3.52, 3.52, 90.0, 90.0, 90.0]
     for i in range(len(s_lat)):
         self.assertAlmostEqual(s_lat[i], f_lat[i])
     self.assertEqual(stru.pdffit['ncell'], [1,1,1,4])
     s_els = [a.element for a in stru]
     self.assertEqual(s_els, 4*['Ni'])
     a0 = stru[0]
     s_xyz = a0.xyz
     f_xyz = [0.0, 0.0, 0.0];
     s_o = a0.occupancy
     f_o = 1.0
     s_U = [ a0.U[i][i] for i in range(3) ]
     f_U = 3*[0.00126651]
     for i in range(3):
         self.assertAlmostEqual(s_xyz[i], f_xyz[i])
         self.assertAlmostEqual(s_U[i], f_U[i])
     self.assertAlmostEqual(s_o, f_o)
コード例 #18
0
 def test_badspacegroup_cif(self):
     """Test reading of CIF file with unrecognized space group.
     """
     ptest = self.ptest
     filename = datafile('badspacegroup.cif')
     self.assertRaises(StructureFormatError, ptest.parseFile, filename)
     return
コード例 #19
0
 def test_read_pdffit_Ni_prim123(self):
     """check reading of Ni_prim supercell 1x2x3"""
     stru = self.stru
     stru.read(datafile('Ni_prim123.stru'), self.format)
     s_lat = [ stru.lattice.a, stru.lattice.b, stru.lattice.c,
         stru.lattice.alpha, stru.lattice.beta, stru.lattice.gamma ]
     f_lat = [2.489016,  2*2.489016,  3*2.489016, 60.0, 60.0, 60.0]
     for i in range(len(s_lat)):
         self.assertAlmostEqual(s_lat[i], f_lat[i])
     s_els = [a.element for a in stru]
     self.assertEqual(s_els, 6*['Ni'])
     a5 = stru[5]
     s_xyz = a5.xyz
     f_xyz = [0.0, 1.0/2.0, 2.0/3.0];
     for i in range(3):
         self.assertAlmostEqual(s_xyz[i], f_xyz[i])
     s_o = a5.occupancy
     f_o = 1.0
     self.assertAlmostEqual(s_o, f_o)
     s_U = [ a5.U[ij[0],ij[1]]
             for ij in [(0,0), (1,1), (2,2), (0,1), (0,2), (1,2)] ]
     f_U = 3*[0.00126651] + 3*[-0.00042217]
     for i in range(len(s_U)):
         self.assertAlmostEqual(s_U[i], f_U[i])
     return
コード例 #20
0
 def test_read_xyz(self):
     """check reading of normal xyz file"""
     stru = self.stru
     stru.read(datafile('bucky.xyz'), self.format)
     s_els = [a.element for a in stru]
     self.assertEqual(stru.title, 'bucky-ball')
     self.assertEqual(s_els, 60*['C'])
     return
コード例 #21
0
def nisph20():
    from diffpy.structure import Lattice, loadStructure
    from diffpy.structure.tests.testutils import datafile
    from diffpy.structure.expansion.makeellipsoid import makeSphere
    ni = loadStructure(datafile('Ni.stru'), 'pdffit')
    sph = makeSphere(ni, 20 / 2.0)
    sph.placeInLattice(Lattice())
    return sph
コード例 #22
0
 def test_cif(self):
     """check loading of CIF file format
     """
     f = datafile('PbTe.cif')
     stru = loadStructure(f)
     self.assertTrue(isinstance(stru, Structure))
     self.assertFalse(isinstance(stru, PDFFitStructure))
     return
コード例 #23
0
 def test_read_xyz(self):
     """check reading of normal xyz file"""
     stru = self.stru
     stru.read(datafile('bucky.xyz'), self.format)
     s_els = [a.element for a in stru]
     self.assertEqual(stru.title, 'bucky-ball')
     self.assertEqual(s_els, 60 * ['C'])
     return
コード例 #24
0
 def test_xcfg(self):
     """check loading of atomeye xcfg format
     """
     f = datafile('BubbleRaftShort.xcfg')
     stru = loadStructure(f)
     self.assertTrue(type(stru) is Structure)
     self.assertRaises(StructureFormatError,
             loadStructure, f, 'xyz')
     return
コード例 #25
0
 def test_custom_spacegroup_cif(self):
     """Test parsing of nonstandard symops-defined space group.
     """
     pfile = self.pfile
     filename = datafile('customsg.cif')
     pfile.parseFile(filename)
     sg = pfile.spacegroup
     self.assertEqual('CIF data', sg.short_name)
     self.assertEqual(6, len(sg.symop_list))
     return
コード例 #26
0
ファイル: testp_cif.py プロジェクト: diffpy/diffpy.Structure
 def test_nosites_cif(self):
     """Test reading of CIF file with no valid sites.
     """
     ptest = self.ptest
     stru = ptest.parseFile(datafile('nosites.cif'))
     self.assertEqual(0, len(stru))
     self.assertEqual(10.413, stru.lattice.a)
     self.assertEqual(10.413, stru.lattice.b)
     self.assertEqual(10.413, stru.lattice.c)
     return
コード例 #27
0
 def test_nosites_cif(self):
     """Test reading of CIF file with no valid sites.
     """
     ptest = self.ptest
     stru = ptest.parseFile(datafile('nosites.cif'))
     self.assertEqual(0, len(stru))
     self.assertEqual(10.413, stru.lattice.a)
     self.assertEqual(10.413, stru.lattice.b)
     self.assertEqual(10.413, stru.lattice.c)
     return
コード例 #28
0
 def test_huge_occupancy(self):
     """check structure with huge occupancy can be read.
     """
     self.stru.read(datafile('Ni.stru'), self.format)
     self.stru[0].occupancy = 16e16
     s_s = self.stru.writeStr(self.format)
     stru1 = Structure()
     stru1.readStr(s_s, self.format)
     self.assertEqual(16e16, stru1[0].occupancy)
     return
コード例 #29
0
 def test_stepcut_parsing(self):
     """check parsing of stepcut record from a file.
     """
     stru = self.stru
     stru.read(datafile('Ni.stru'), self.format)
     self.assertEqual(0, stru.pdffit['stepcut'])
     snoshape = stru.writeStr(format=self.format)
     self.assertTrue(not re.search('(?m)^shape', snoshape))
     # produce a string with non-zero stepcut
     stru.pdffit['stepcut'] = 13
     s13 = stru.writeStr(format=self.format)
     self.assertTrue(re.search('(?m)^shape +stepcut, ', s13))
     stru13 = Structure()
     stru13.readStr(s13)
     self.assertEqual(13, stru13.pdffit['stepcut'])
     with open(datafile('Ni.stru')) as fp:
         ni_lines = fp.readlines()
     ni_lines.insert(3, 'shape invalid, 7\n')
     sbad = ''.join(ni_lines)
     self.assertRaises(StructureFormatError, self.stru.readStr,
             sbad, format=self.format)
     return
コード例 #30
0
 def test_read_xcfg(self):
     """check reading of BubbleRaft XCFG file"""
     stru = self.stru
     stru.read(datafile('BubbleRaftShort.xcfg'), self.format)
     f_els = 500 * ["Ar"]
     s_els = [a.element for a in stru]
     self.assertEqual(s_els, f_els)
     self.assertAlmostEqual(stru.distance(82, 357), 47.5627, 3)
     s_lat = [stru.lattice.a, stru.lattice.b, stru.lattice.c,
              stru.lattice.alpha, stru.lattice.beta, stru.lattice.gamma]
     f_lat = [127.5, 119.5, 3.0, 90.0, 90.0, 90.0]
     self.assertTrue(numpy.allclose(s_lat, f_lat))
     return
コード例 #31
0
 def test_read_pdb_arginine(self):
     """check reading of arginine PDB file"""
     stru = self.stru
     stru.read(datafile('arginine.pdb'), self.format)
     f_els = ["N", "C", "C", "O", "C", "C", "C", "N", "C", "N", "N", "H",
              "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "H",
              "H", "O", "H"]
     s_els = [a.element for a in stru]
     self.assertEqual(s_els, f_els)
     s_lat = [stru.lattice.a, stru.lattice.b, stru.lattice.c,
              stru.lattice.alpha, stru.lattice.beta, stru.lattice.gamma]
     f_lat = [1.0, 1.0, 1.0, 90.0, 90.0, 90.0]
     self.assertEqual(s_lat, f_lat)
     a0 = stru[0]
     self.assertTrue(numpy.allclose(a0.xyz, [0.735, 2.219, 1.389]))
コード例 #32
0
 def test_read_xcfg(self):
     """check reading of BubbleRaft XCFG file"""
     stru = self.stru
     stru.read(datafile('BubbleRaftShort.xcfg'), self.format)
     f_els = 500 * ["Ar"]
     s_els = [a.element for a in stru]
     self.assertEqual(s_els, f_els)
     self.assertAlmostEqual(stru.distance(82, 357), 47.5627, 3)
     s_lat = [
         stru.lattice.a, stru.lattice.b, stru.lattice.c, stru.lattice.alpha,
         stru.lattice.beta, stru.lattice.gamma
     ]
     f_lat = [127.5, 119.5, 3.0, 90.0, 90.0, 90.0]
     self.assertTrue(numpy.allclose(s_lat, f_lat))
     return
コード例 #33
0
 def test_ignored_lines(self):
     """check skipping of ignored lines in the header
     """
     r1 = 'ignored record 1\n'
     r2 = 'ignored record 2\n'
     with open(datafile('Ni-discus.stru')) as fp:
         ni_lines = fp.readlines()
     ni_lines.insert(2, r1)
     ni_lines.insert(4, r2)
     s_s1 = "".join(ni_lines)
     p = self.stru.readStr(s_s1, self.format)
     self.assertEqual([r1.rstrip(), r2.rstrip()], p.ignored_lines)
     ni_lines.append(r1)
     s_s2 = "".join(ni_lines)
     self.assertRaises(StructureFormatError, self.stru.readStr, s_s2,
                       self.format)
     return
コード例 #34
0
 def test_ignored_lines(self):
     """check skipping of ignored lines in the header
     """
     r1 = 'ignored record 1\n'
     r2 = 'ignored record 2\n'
     with open(datafile('Ni-discus.stru')) as fp:
         ni_lines = fp.readlines()
     ni_lines.insert(2, r1)
     ni_lines.insert(4, r2)
     s_s1 = "".join(ni_lines)
     p = self.stru.readStr(s_s1, self.format)
     self.assertEqual([r1.rstrip(), r2.rstrip()], p.ignored_lines)
     ni_lines.append(r1)
     s_s2 = "".join(ni_lines)
     self.assertRaises(StructureFormatError, self.stru.readStr,
             s_s2, self.format)
     return
コード例 #35
0
 def test_ignored_lines(self):
     """check skipping of ignored lines in the header
     """
     r1 = 'ignored record 1'
     r2 = 'ignored record 2'
     with open(datafile('Ni.stru')) as fp:
         ni_lines = fp.readlines()
     ni_lines.insert(2, r1 + '\n')
     ni_lines.insert(4, r2 + '\n')
     s_s1 = "".join(ni_lines)
     p = self.stru.readStr(s_s1, self.format)
     self.assertEqual([r1, r2], p.ignored_lines)
     ni_lines.insert(-3, r1 + '\n')
     s_s2 = "".join(ni_lines)
     self.assertRaises(StructureFormatError, self.stru.readStr,
             s_s2, self.format)
     return
コード例 #36
0
 def test_rwStr_xcfg_CdSe(self):
     """check conversion to XCFG file format"""
     stru = self.stru
     stru.read(datafile('CdSe_bulk.stru'), 'pdffit')
     s = stru.writeStr(self.format)
     stru = Structure()
     stru.readStr(s, self.format)
     s_els = [a.element for a in stru]
     f_els = ['Cd', 'Cd', 'Se', 'Se']
     self.assertEqual(s_els, f_els)
     s_lat = [stru.lattice.a, stru.lattice.b, stru.lattice.c,
              stru.lattice.alpha, stru.lattice.beta, stru.lattice.gamma]
     f_lat = [4.235204, 4.235204, 6.906027, 90.0, 90.0, 120.0]
     self.assertTrue(numpy.allclose(s_lat, f_lat))
     a0 = stru[0]
     s_Uii = [a0.U[i,i] for i in range(3)]
     f_Uii = [0.01303035, 0.01303035, 0.01401959]
     self.assertTrue(numpy.allclose(s_Uii, f_Uii))
コード例 #37
0
 def test_read_pdb_arginine(self):
     """check reading of arginine PDB file"""
     stru = self.stru
     stru.read(datafile('arginine.pdb'), self.format)
     f_els = [
         "N", "C", "C", "O", "C", "C", "C", "N", "C", "N", "N", "H", "H",
         "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "O",
         "H"
     ]
     s_els = [a.element for a in stru]
     self.assertEqual(s_els, f_els)
     s_lat = [
         stru.lattice.a, stru.lattice.b, stru.lattice.c, stru.lattice.alpha,
         stru.lattice.beta, stru.lattice.gamma
     ]
     f_lat = [1.0, 1.0, 1.0, 90.0, 90.0, 90.0]
     self.assertEqual(s_lat, f_lat)
     a0 = stru[0]
     self.assertTrue(numpy.allclose(a0.xyz, [0.735, 2.219, 1.389]))
コード例 #38
0
 def test_rwStr_xcfg_CdSe(self):
     """check conversion to XCFG file format"""
     stru = self.stru
     stru.read(datafile('CdSe_bulk.stru'), 'pdffit')
     s = stru.writeStr(self.format)
     stru = Structure()
     stru.readStr(s, self.format)
     s_els = [a.element for a in stru]
     f_els = ['Cd', 'Cd', 'Se', 'Se']
     self.assertEqual(s_els, f_els)
     s_lat = [
         stru.lattice.a, stru.lattice.b, stru.lattice.c, stru.lattice.alpha,
         stru.lattice.beta, stru.lattice.gamma
     ]
     f_lat = [4.235204, 4.235204, 6.906027, 90.0, 90.0, 120.0]
     self.assertTrue(numpy.allclose(s_lat, f_lat))
     a0 = stru[0]
     s_Uii = [a0.U[i, i] for i in range(3)]
     f_Uii = [0.01303035, 0.01303035, 0.01401959]
     self.assertTrue(numpy.allclose(s_Uii, f_Uii))
コード例 #39
0
 def test_read_discus_Ni(self):
     """check reading of Ni structure in discus format"""
     stru = self.stru
     stru.read(datafile('Ni-discus.stru'), self.format)
     f_title = 'structure Ni  FCC'
     self.assertEqual(f_title, stru.title)
     self.assertEqual('Fm-3m', stru.pdffit['spcgr'])
     # cell record
     abcABG = (3.52, 3.52, 3.52, 90.0, 90.0, 90.0)
     self.assertEqual(abcABG, stru.lattice.abcABG())
     # ncell
     self.assertEqual([1, 1, 1, 4], stru.pdffit['ncell'])
     self.assertEqual(4, len(stru))
     # first atom
     a0 = stru[0]
     self.assertEqual((0.0, 0.0, 0.0), tuple(a0.xyz))
     self.assertTrue(not a0.anisotropy)
     Biso0 = 0.1
     self.assertAlmostEqual(Biso0, a0.Bisoequiv, self.places)
     return
コード例 #40
0
 def test_read_discus_Ni(self):
     """check reading of Ni structure in discus format"""
     stru = self.stru
     stru.read(datafile('Ni-discus.stru'), self.format)
     f_title = 'structure Ni  FCC'
     self.assertEqual(f_title, stru.title)
     self.assertEqual('Fm-3m', stru.pdffit['spcgr'])
     # cell record
     abcABG = (3.52, 3.52, 3.52, 90.0, 90.0, 90.0)
     self.assertEqual(abcABG, stru.lattice.abcABG())
     # ncell
     self.assertEqual([1, 1, 1, 4], stru.pdffit['ncell'])
     self.assertEqual(4, len(stru))
     # first atom
     a0 = stru[0]
     self.assertEqual((0.0, 0.0, 0.0), tuple(a0.xyz))
     self.assertTrue(not a0.anisotropy)
     Biso0 = 0.1
     self.assertAlmostEqual(Biso0, a0.Bisoequiv, self.places)
     return
コード例 #41
0
 def test_read_pdffit_ZnSb(self):
     """check reading of ZnSb pdffit structure file"""
     stru = self.stru
     stru.read(datafile('ZnSb_RT_Q28X_VM_20_fxiso.rstr'), self.format)
     f_title = "Cell structure file of Zn4Sb3 #167 interstitial"
     self.assertEqual(stru.title, f_title)
     self.assertAlmostEqual(stru.pdffit['scale'], 0.826145)
     self.assertAlmostEqual(stru.pdffit['delta2'], 4.687951)
     self.assertAlmostEqual(stru.pdffit['delta1'], 0.01)
     self.assertAlmostEqual(stru.pdffit['sratio'], 1.02)
     self.assertAlmostEqual(stru.pdffit['rcut'], 0.03)
     self.assertEqual(stru.pdffit['spcgr'], 'R-3c')
     s_lat = [ stru.lattice.a, stru.lattice.b, stru.lattice.c,
         stru.lattice.alpha, stru.lattice.beta, stru.lattice.gamma ]
     f_lat = [12.309436, 12.309436, 12.392839, 90.0, 90.0, 120.0]
     self.assertTrue(numpy.allclose(s_lat, f_lat))
     s_dcell = stru.pdffit['dcell']
     f_dcell = [0.000008, 0.000008, 0.000013, 0.0, 0.0, 0.0]
     self.assertTrue(numpy.allclose(s_dcell, f_dcell))
     self.assertEqual(stru.pdffit['ncell'], [1,1,1,66])
     s_els = [a.element for a in stru]
     self.assertEqual(s_els, 36*['Zn']+30*['Sb'])
     a0 = stru[0]
     s_xyz = a0.xyz
     f_xyz = [0.09094387, 0.24639539, 0.40080261];
     s_o = a0.occupancy
     f_o = 0.9
     s_sigxyz = a0.sigxyz
     f_sigxyz = [ 0.00000079, 0.00000076, 0.00000064];
     s_sigo = a0.sigo
     f_sigo = 0.0
     s_U = [ a0.U[i][i] for i in range(3) ]
     f_U = 3*[0.01]
     self.assertTrue(numpy.allclose(s_xyz, f_xyz))
     self.assertTrue(numpy.allclose(s_sigxyz, f_sigxyz))
     self.assertTrue(numpy.allclose(s_U, f_U))
     self.assertAlmostEqual(s_o, f_o)
     self.assertAlmostEqual(s_sigo, f_sigo)
コード例 #42
0
 def test_except_other_formats(self):
     """check exceptions when reading files in other formats"""
     badfiles = [
             'LiCl-bad.cif',
             'PbTe.cif',
             'arginine.pdb',
             'ZnSb_RT_Q28X_VM_20_fxiso.rstr',
             'Ni-bad.stru',
             'Ni.stru',
             'BubbleRaftShort.xcfg',
             'bucky-bad1.xyz',
             'bucky-bad2.xyz',
             'bucky-plain-bad.xyz',
             'bucky-plain.xyz',
             'bucky-raw.xyz',
             'bucky.xyz',
             'hexagon-raw-bad.xyz',
             'hexagon-raw.xyz',
     ]
     for ft in badfiles:
         ff = datafile(ft)
         self.assertRaises(StructureFormatError,
                 self.stru.read, ff, format=self.format)
     return
コード例 #43
0
##############################################################################
"""Unit tests for Structure class.
"""

import copy
import pickle
import unittest
import numpy

from diffpy.structure.tests.testutils import datafile
from diffpy.structure import Structure
from diffpy.structure import Lattice
from diffpy.structure import Atom

# useful variables
cdsefile = datafile('CdSe_bulk.stru')
teifile = datafile('TeI.cif')
pbtefile = datafile('PbTe.cif')

# ----------------------------------------------------------------------------


class TestStructure(unittest.TestCase):
    """test methods of Structure class"""

    _loaded_structures = {}

    def setUp(self):
        self.stru = Structure(
            [Atom('C', [0, 0, 0]), Atom('C', [1, 1, 1])],
            lattice=Lattice(1, 1, 1, 90, 90, 120))
コード例 #44
0
class TestP_cif(unittest.TestCase):

    pbteciffile = datafile('PbTe.cif')
    badciffile = datafile('LiCl-bad.cif')
    graphiteciffile = datafile('graphite.cif')
    cdsebulkpdffitfile = datafile('CdSe_bulk.stru')
    teiciffile = datafile('TeI.cif')
    places = 6

    def setUp(self):
        self.ptest = P_cif()
        self.pfile = P_cif()
        return

    def tearDown(self):
        return

    def test_parse(self):
        """check P_cif.parse()
        """
        with open(self.pbteciffile) as fp1:
            sgood = fp1.read()
        with open(self.badciffile) as fp2:
            sbad = fp2.read()
        pfile, ptest = self.pfile, self.ptest
        stru_check = pfile.parseFile(self.pbteciffile)
        stru = ptest.parse(sgood)
        self.assertEqual(str(stru_check), str(stru))
        self.assertEqual(str(stru_check.lattice), str(stru.lattice))
        self.assertEqual(pfile.spacegroup.short_name,
                         ptest.spacegroup.short_name)
        ptestb = P_cif()
        self.assertRaises(StructureFormatError, ptestb.parse, sbad)
        return

    def test_parseLines(self):
        """check P_cif.parseLines()
        """
        with open(self.pbteciffile) as fp1:
            goodlines = fp1.readlines()
        with open(self.badciffile) as fp2:
            badlines = fp2.readlines()
        pfile, ptest = self.pfile, self.ptest
        stru_check = pfile.parseFile(self.pbteciffile)
        stru = ptest.parseLines(goodlines)
        self.assertEqual(str(stru_check), str(stru))
        self.assertEqual(str(stru_check.lattice), str(stru.lattice))
        self.assertEqual(pfile.spacegroup.short_name,
                         ptest.spacegroup.short_name)
        ptest2 = P_cif()
        self.assertRaises(StructureFormatError, ptest2.parseLines, badlines)
        return

    def test_parseFile(self):
        """check P_cif.parseFile()
        """
        # pbteciffile
        stru = self.pfile.parseFile(self.pbteciffile)
        self.assertEqual(8, len(stru))
        self.assertEqual(6.461, stru.lattice.a)
        self.assertEqual(6.461, stru.lattice.b)
        self.assertEqual(6.461, stru.lattice.c)
        self.assertEqual(90.0, stru.lattice.alpha)
        self.assertEqual(90.0, stru.lattice.beta)
        self.assertEqual(90.0, stru.lattice.gamma)
        self.assertEqual('Fm-3m', self.pfile.spacegroup.short_name)
        a0 = stru[0]
        self.assertEqual(0.5, a0.x)
        self.assertEqual(0.5, a0.y)
        self.assertEqual(0.5, a0.z)
        self.assertEqual(False, a0.anisotropy)
        self.assertEqual(1.0, a0.occupancy)
        self.assertEqual(0.0225566, a0.Uisoequiv)
        # badciffile
        pfile2 = P_cif()
        self.assertRaises(StructureFormatError, pfile2.parseFile,
                          self.badciffile)
        # graphite
        pgraphite = P_cif()
        graphite = pgraphite.parseFile(self.graphiteciffile)
        self.assertEqual(4, len(graphite))
        c1 = graphite[0]
        self.assertEqual(str, type(c1.element))
        self.assertEqual('C', c1.element)
        self.assertEqual(str, type(c1.label))
        self.assertEqual('C1', c1.label)
        # filename with unicode encoding
        ugraphite = P_cif().parseFile(six.u(self.graphiteciffile))
        self.assertEqual(4, len(ugraphite))
        # File with full space group name
        ptei = P_cif().parseFile(self.teiciffile)
        self.assertEqual(16, len(ptei))
        return

#   def test__parseCifBlock(self):
#       """check P_cif._parseCifBlock()
#       """
#       return
#
#   def test__parse_lattice(self):
#       """check P_cif._parse_lattice()
#       """
#       return
#
#   def test__parse_atom_site_label(self):
#       """check P_cif._parse_atom_site_label()
#       """
#       return
#
#   def test__parse_atom_site_aniso_label(self):
#       """check P_cif._parse_atom_site_aniso_label()
#       """
#       return
#
#   def test__parse_space_group_symop_operation_xyz(self):
#       """check P_cif._parse_space_group_symop_operation_xyz()
#       """
#       return
#
#   def test__expandAsymmetricUnit(self):
#       """check P_cif._expandAsymmetricUnit()
#       """
#       return
#
#   def test_toLines(self):
#       """check P_cif.toLines()
#       """
#       return
#
#   def test_tostring(self):
#       """check P_cif.tostring()
#       """
#       return

    def test_write_and_read(self):
        """high-level check of P_cif.tostring()
        """
        # high-level check
        stru_check = Structure()
        stru_check.read(self.cdsebulkpdffitfile)
        s_s = stru_check.writeStr('cif')
        stru = Structure()
        stru.readStr(s_s, 'cif')
        self.assertAlmostEqual(4.2352, stru.lattice.a, self.places)
        self.assertAlmostEqual(4.2352, stru.lattice.b, self.places)
        self.assertAlmostEqual(6.90603, stru.lattice.c, self.places)
        self.assertEqual(4, len(stru))
        a0 = stru[0]
        self.assertEqual('Cd', a0.element)
        self.assertTrue(numpy.allclose([0.3334, 0.6667, 0.0], a0.xyz))
        self.assertTrue(a0.anisotropy)
        self.assertAlmostEqual(0.01303, a0.U[0, 0])
        self.assertAlmostEqual(0.01303, a0.U[1, 1])
        self.assertAlmostEqual(0.01402, a0.U[2, 2])
        a3 = stru[3]
        self.assertEqual('Se', a3.element)
        self.assertTrue(numpy.allclose([0.6666, 0.333300, 0.87667], a3.xyz))
        self.assertAlmostEqual(0.015673, a3.U[0, 0])
        self.assertAlmostEqual(0.015673, a3.U[1, 1])
        self.assertAlmostEqual(0.046164, a3.U[2, 2])
        return

    def test_eps(self):
        """Test the P_cif.eps coordinates resolution.
        """
        pcif = P_cif()
        pcif.eps = 1e-8
        grph = pcif.parseFile(self.graphiteciffile)
        self.assertEqual(8, len(grph))
        self.assertTrue(all(a.label.startswith('C1') for a in grph[:2]))
        self.assertTrue(all(a.label.startswith('C2') for a in grph[2:]))
        pcif2 = P_cif()
        pcif2.eps = 1e-3
        grph2 = pcif2.parseFile(self.graphiteciffile)
        self.assertEqual(4, len(grph2))
        return

    def test_unknown_occupancy(self):
        "test CIF file with unknown occupancy data"
        stru = self.ptest.parseFile(datafile('TeI-unkocc.cif'))
        self.assertTrue(numpy.array_equal(16 * [1], stru.occupancy))
        return

    def test_nosites_cif(self):
        """Test reading of CIF file with no valid sites.
        """
        ptest = self.ptest
        stru = ptest.parseFile(datafile('nosites.cif'))
        self.assertEqual(0, len(stru))
        self.assertEqual(10.413, stru.lattice.a)
        self.assertEqual(10.413, stru.lattice.b)
        self.assertEqual(10.413, stru.lattice.c)
        return

    def test_badspacegroup_cif(self):
        """Test reading of CIF file with unrecognized space group.
        """
        ptest = self.ptest
        filename = datafile('badspacegroup.cif')
        self.assertRaises(StructureFormatError, ptest.parseFile, filename)
        return

    def test_custom_spacegroup_cif(self):
        """Test parsing of nonstandard symops-defined space group.
        """
        pfile = self.pfile
        filename = datafile('customsg.cif')
        pfile.parseFile(filename)
        sg = pfile.spacegroup
        self.assertEqual('CIF data', sg.short_name)
        self.assertEqual(6, len(sg.symop_list))
        return

    def test_spacegroup_isotropy(self):
        "verify site isotropy due to site symmetry."
        # remove the _atom_site_thermal_displace_type field
        with open(self.pbteciffile) as fp:
            lines = [
                line.replace(' Uiso ', ' ') for line in fp
                if '_atom_site_thermal_displace_type' not in line
            ]
        ciftxt = ''.join(lines)
        ptest = self.ptest
        stru = ptest.parse(ciftxt)
        self.assertFalse(any(stru.anisotropy))
        self.assertTrue(all(not a.anisotropy for a in ptest.asymmetric_unit))
        return

    def test_spacegroup_anisotropy(self):
        "verify site anisotropy due to site symmetry."
        stru = self.ptest.parseFile(self.graphiteciffile)
        self.assertTrue(all(stru.anisotropy))
        return

    def test_adp_type_ani(self):
        "verify adp type override to anisotropic"
        with open(self.pbteciffile) as fp:
            ciftxt = fp.read()
            ciftxt = ciftxt.replace(' Uiso ', ' Uani ')
        stru = self.ptest.parse(ciftxt)
        self.assertTrue(all(stru.anisotropy))
        return

    def test_adp_type_iso(self):
        "verify adp type override to isotropic"
        with open(self.graphiteciffile) as fp:
            lines = fp.readlines()
        lines.insert(-2, '_atom_site_adp_type\n')
        lines[-2] = lines[-2].rstrip() + '   Uiso\n'
        lines[-1] = lines[-1].rstrip() + '   Uiso\n'
        ciftxt = ''.join(lines)
        stru = self.ptest.parse(ciftxt)
        self.assertFalse(any(a.anisotropy for a in stru))
        return

    def test_adp_aniso_label(self):
        "verify ADP type setting from _atom_site_aniso_label loop"
        with open(self.teiciffile) as fp:
            lines = [
                line.replace(' Uani ', ' ') for line in fp
                if not '_atom_site_adp_type' in line
            ]
        ciftxt = ''.join(lines)
        stru = self.ptest.parse(ciftxt)
        self.assertTrue(all(stru.anisotropy))
        return

    def test_getParser(self):
        """Test passing of eps keyword argument by getParser function.
        """
        pcif = getParser('cif', eps=1e-6)
        grph = pcif.parseFile(self.graphiteciffile)
        self.assertEqual(8, len(grph))
        self.assertTrue(all(a.label.startswith('C1') for a in grph[:2]))
        self.assertTrue(all(a.label.startswith('C2') for a in grph[2:]))
        pcif2 = getParser('cif')
        grph2 = pcif2.parseFile(self.graphiteciffile)
        self.assertEqual(4, len(grph2))
        return
コード例 #45
0
ファイル: testp_cif.py プロジェクト: diffpy/diffpy.Structure
 def test_unknown_occupancy(self):
     "test CIF file with unknown occupancy data"
     stru = self.ptest.parseFile(datafile('TeI-unkocc.cif'))
     self.assertTrue(numpy.array_equal(16 * [1], stru.occupancy))
     return
コード例 #46
0
 def test_badfile(self):
     """check loading of CIF file format
     """
     f = datafile('Ni-bad.stru')
     self.assertRaises(StructureFormatError, loadStructure, f)
     return
コード例 #47
0
 def test_badkwarg(self):
     """check loading of xyz file format with invalid keyword argument
     """
     f = datafile('bucky.xyz')
     self.assertRaises(TypeError, loadStructure, f, eps=1e-10)
     return
コード例 #48
0
 def test_read_plainxyz_bad(self):
     """check exceptions when reading invalid plain xyz file"""
     stru = self.stru
     self.assertRaises(StructureFormatError, stru.read,
                       datafile('bucky-plain-bad.xyz'), self.format)
     return