Ejemplo n.º 1
0
 def setUp(self):
     self.P = PdfFit()
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.alloc('N', 0.0, 0.05, 0.1, 10, 200)
     return
Ejemplo n.º 2
0
 def setUp(self):
     self.P = PdfFit()
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.alloc('N', 0.0, 0.05, 0.1, 10, 200)
     return
Ejemplo n.º 3
0
 def test_getcrw(self):
     """check PdfFit.getcrw()
     """
     import numpy
     self.assertEqual(0, self.P.num_datasets())
     # Setting qmax=0 so that partial crw are not disturbed by
     # termination ripples.
     self.P.read_data(datafile('Ni.dat'), 'X', 0.0, 0.0)
     # crw is empty before data refinement
     self.assertEqual([], self.P.getcrw())
     self.P.read_struct(datafile('Ni.stru'))
     self.P.pdfrange(1, 2, 19)
     self.P.refine()
     crw19 = numpy.array(self.P.getcrw())
     self.assertTrue(numpy.all(crw19 >= 0.0))
     # check that crw19 is non decreasing
     self.assertTrue(numpy.all(numpy.diff(crw19) >= 0.0))
     # check that crw19 and getrw give the same value
     rw19 = crw19[-1]
     self.assertAlmostEqual(self.P.getrw(), rw19, self.places)
     # renormalize cumulative Rw and compare with Rw at r=15
     Gobs19 = numpy.array(self.P.getpdf_obs())
     Gnorm19 = numpy.sqrt(numpy.sum(Gobs19**2))
     r = numpy.array(self.P.getR())
     idx = numpy.nonzero(r <= 15)[0]
     Gnorm15 = numpy.sqrt(numpy.sum(Gobs19[idx]**2))
     i15 = idx[-1]
     rw15 = crw19[i15] * Gnorm19 / Gnorm15
     self.P.pdfrange(1, 2, r[i15] + 1e-5)
     self.P.refine()
     self.assertAlmostEqual(self.P.getrw(), rw15, self.places)
     return
Ejemplo n.º 4
0
 def test_getcrw(self):
     """check PdfFit.getcrw()
     """
     import numpy
     self.assertEqual(0, self.P.num_datasets())
     # Setting qmax=0 so that partial crw are not disturbed by
     # termination ripples.
     self.P.read_data(datafile('Ni.dat'), 'X', 0.0, 0.0)
     # crw is empty before data refinement
     self.assertEqual([], self.P.getcrw())
     self.P.read_struct(datafile('Ni.stru'))
     self.P.pdfrange(1, 2, 19)
     self.P.refine()
     crw19 = numpy.array(self.P.getcrw())
     self.failUnless(numpy.all(crw19 >= 0.0))
     # check that crw19 is non decreasing
     self.failUnless(numpy.all(numpy.diff(crw19) >= 0.0))
     # check that crw19 and getrw give the same value
     rw19 = crw19[-1]
     self.assertAlmostEqual(self.P.getrw(), rw19, self.places)
     # renormalize cumulative Rw and compare with Rw at r=15
     Gobs19 = numpy.array(self.P.getpdf_obs())
     Gnorm19 = numpy.sqrt(numpy.sum(Gobs19**2))
     r = numpy.array(self.P.getR())
     idx = numpy.nonzero(r <= 15)[0]
     Gnorm15 = numpy.sqrt(numpy.sum(Gobs19[idx]**2))
     i15 = idx[-1]
     rw15 = crw19[i15] * Gnorm19 / Gnorm15
     self.P.pdfrange(1, 2, r[i15] + 1e-5)
     self.P.refine()
     self.assertAlmostEqual(self.P.getrw(), rw15, self.places)
     return
Ejemplo n.º 5
0
 def test_twophase_calculation(self):
     """check PDF calculation for 2 phases with different spdiameters
     """
     d1 = 6
     d2 = 9
     self.P.read_struct(datafile('Ni.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.setvar('spdiameter', d1)
     self.P.calc()
     G1 = numpy.array(self.P.getpdf_fit())
     self.P.reset()
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.setvar('spdiameter', d2)
     self.P.calc()
     G2 = numpy.array(self.P.getpdf_fit())
     self.P.reset()
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.setphase(1)
     self.P.setvar('spdiameter', d1)
     self.P.setphase(2)
     self.P.setvar('spdiameter', d2)
     self.P.calc()
     Gtot = numpy.array(self.P.getpdf_fit())
     dG = (G1 + G2 - Gtot)
     r = numpy.array(self.P.getR())
     msd = numpy.dot(dG, dG) / len(r)
     self.assertAlmostEqual(0.0, numpy.sqrt(msd), self.places)
     return
Ejemplo n.º 6
0
 def test_twophase_calculation(self):
     """check PDF calculation for 2 phases with different spdiameters
     """
     d1 = 6
     d2 = 9
     self.P.read_struct(datafile('Ni.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.setvar('spdiameter', d1)
     self.P.calc()
     G1 = numpy.array(self.P.getpdf_fit())
     self.P.reset()
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.setvar('spdiameter', d2)
     self.P.calc()
     G2 = numpy.array(self.P.getpdf_fit())
     self.P.reset()
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.setphase(1)
     self.P.setvar('spdiameter', d1)
     self.P.setphase(2)
     self.P.setvar('spdiameter', d2)
     self.P.calc()
     Gtot = numpy.array(self.P.getpdf_fit())
     dG = (G1 + G2 - Gtot)
     r = numpy.array(self.P.getR())
     msd = numpy.dot(dG, dG)/len(r)
     self.assertAlmostEqual(0.0, numpy.sqrt(msd), self.places)
     return
Ejemplo n.º 7
0
 def test_alloc(self):
     """check PdfFit.alloc()
     """
     # alloc and read_struct can be called in any order.
     self.P.alloc('X', 25, 0.0, 0.01, 10, 1000)
     # without a structure calculated PDF is all zero
     self.P.calc()
     Gzero = self.P.getpdf_fit()
     self.assertEqual(1000*[0.0], Gzero)
     self.P.read_struct(datafile('Ni.stru'))
     self.P.calc()
     # check r-values
     r = self.P.getR()
     self.assertEqual(1000, len(r))
     for i in range(1000):
         self.assertAlmostEqual(0.01*(i + 1), r[i], self.places)
     Gfit_alloc_read = self.P.getpdf_fit()
     # now try the other order
     self.P.reset()
     self.P.read_struct(datafile('Ni.stru'))
     self.P.alloc('X', 25, 0.0, 0.01, 10, 1000)
     self.P.calc()
     Gfit_read_alloc = self.P.getpdf_fit()
     # and they should be the same
     self.assertEqual(Gfit_read_alloc, Gfit_alloc_read)
     return
Ejemplo n.º 8
0
 def test_alloc(self):
     """check PdfFit.alloc()
     """
     # alloc and read_struct can be called in any order.
     self.P.alloc('X', 25, 0.0, 0.01, 10, 1000)
     # without a structure calculated PDF is all zero
     self.P.calc()
     Gzero = self.P.getpdf_fit()
     self.assertEqual(1000 * [0.0], Gzero)
     self.P.read_struct(datafile('Ni.stru'))
     self.P.calc()
     # check r-values
     r = self.P.getR()
     self.assertEqual(1000, len(r))
     for i in range(1000):
         self.assertAlmostEqual(0.01 * (i + 1), r[i], self.places)
     Gfit_alloc_read = self.P.getpdf_fit()
     # now try the other order
     self.P.reset()
     self.P.read_struct(datafile('Ni.stru'))
     self.P.alloc('X', 25, 0.0, 0.01, 10, 1000)
     self.P.calc()
     Gfit_read_alloc = self.P.getpdf_fit()
     # and they should be the same
     self.assertEqual(Gfit_read_alloc, Gfit_alloc_read)
     return
Ejemplo n.º 9
0
 def test_ijcharValueError(self):
     """raise ValueError when ijchar is neither 'i' nor 'j'"""
     self.P.read_struct(datafile("Ni.stru"))
     self.P.read_data(datafile("Ni.dat"), 'X', 25.0, 0.0)
     self.P.selectAtomType(self.iset, 'i', 'Ni', True)
     self.P.selectAtomType(self.iset, 'j', 'Ni', True)
     self.assertRaises(ValueError, self.P.selectAtomType,
             self.iset, 'k', 'Ni', True)
Ejemplo n.º 10
0
 def test_IOError(self):
     """raise IOError when dif cannot be saved"""
     self.P.read_struct(datafile("Ni.stru"))
     self.P.alloc('X', 30.0, 0.05, 2, 10, 100)
     self.P.calc()
     self.P.read_data(datafile("Ni.dat"), 'X', 25.0, 0.5)
     self.assertRaises(IOError, self.P.save_dif, 1,
             "nodir183160/"+self.strufile)
Ejemplo n.º 11
0
 def test_constraintError(self):
     """raise constraintError when constraint is bad"""
     self.P.read_struct(datafile("Ni.stru"))
     self.P.read_data(datafile("Ni.dat"), 'X', 25.0, 0.0)
     self.P.constrain('x(1)', 'junk+@1')
     self.P.setpar(1, 0.01)
     self.assertRaises(pdffit2.constraintError, self.P.calc)
     self.assertRaises(pdffit2.constraintError, self.P.refine)
     return
Ejemplo n.º 12
0
 def test_unassignedError2(self):
     """raise pdffit2.unassignedError when set does not exist"""
     self.P.read_struct(datafile("Ni.stru"))
     # pass for phase 1
     self.P.read_data(datafile("Ni.dat"), 'X', 25.0, 0.0)
     self.P.selectAtomIndex(self.iset, 'i', 1, True)
     self.P.selectAtomIndex(self.iset, 'i', 2, False)
     # fail for phase 2
     self.assertRaises(pdffit2.unassignedError, self.P.selectAtomIndex,
             2, 'i', 1, True)
Ejemplo n.º 13
0
 def test_unassignedError2(self):
     """raise pdffit2.unassignedError when set does not exist"""
     self.P.read_struct(datafile("Ni.stru"))
     # selectAtomType should pass with one phase defined
     self.P.read_data(datafile("Ni.dat"), 'X', 25.0, 0.0)
     self.P.selectAtomType(self.iset, 'i', 'Ni', True)
     self.P.selectAtomType(self.iset, 'j', 'Ni', False)
     # but fail for phase 2 which is not present
     self.assertRaises(pdffit2.unassignedError, self.P.selectAtomType,
             2, 'i', 'Ca', True)
Ejemplo n.º 14
0
 def test_IOError(self):
     """raise IOError when residual file cannot be saved"""
     self.P.read_struct(datafile("Ni.stru"))
     self.P.read_data(datafile("Ni.dat"), 'X', 30.0, 0.0)
     self.P.constrain(self.P.lat(1), 1)
     self.P.setpar(1, 3.0)
     self.P.pdfrange(1,2.0,10.0)
     self.P.refine_step()
     self.assertRaises(IOError, self.P.save_res,
             "nodir183160/"+self.resfile)
Ejemplo n.º 15
0
 def test_bond_length_atoms(self):
     """check PdfFit.bond_length_atoms()
     """
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     dij, ddij = self.P.bond_length_atoms(1, 5)
     self.assertAlmostEqual(4.03635, dij, self.places)
     self.P.setphase(1)
     self.assertRaises(ValueError, self.P.bond_length_atoms, 1, 5)
     return
Ejemplo n.º 16
0
 def test_bond_length_atoms(self):
     """check PdfFit.bond_length_atoms()
     """
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     dij, ddij = self.P.bond_length_atoms(1, 5)
     self.assertAlmostEqual(4.03635, dij, self.places)
     self.P.setphase(1)
     self.assertRaises(ValueError, self.P.bond_length_atoms, 1, 5)
     return
Ejemplo n.º 17
0
 def test_get_atoms(self):
     """check PdfFit.get_atoms()
     """
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.setphase(1)
     a1 = self.P.get_atoms()
     a2 = self.P.get_atoms(2)
     self.assertEqual(4*['NI'], a1)
     self.assertEqual(8*['PB']+24*['O']+8*['SC']+8*['W']+8*['TI'], a2)
     return
Ejemplo n.º 18
0
 def test_get_atom_types(self):
     """check PdfFit.get_atom_types()
     """
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.setphase(1)
     atp1 = self.P.get_atom_types()
     atp2 = self.P.get_atom_types(2)
     self.assertEqual(['NI'], atp1)
     self.assertEqual(['PB', 'O', 'SC', 'W', 'TI'], atp2)
     return
Ejemplo n.º 19
0
 def test_num_phases(self):
     """check PdfFit.num_phases()
     """
     self.assertEqual(0, self.P.num_phases())
     self.P.read_struct(datafile('Ni.stru'))
     self.assertEqual(1, self.P.num_phases())
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.assertEqual(2, self.P.num_phases())
     self.P.reset()
     self.assertEqual(0, self.P.num_phases())
     return
Ejemplo n.º 20
0
 def test_constrainNonRefVar(self):
     "raise constraintError when attempting to constrain non-refinables"
     self.P.read_struct(datafile("Ni.stru"))
     self.P.read_data(datafile("Ni.dat"), 'X', 25.0, 0.0)
     self.assertRaises(pdffit2.constraintError,
             self.P.constrain, 'rcut', '@7')
     self.assertRaises(pdffit2.constraintError,
             self.P.constrain, 'rcut', 13)
     self.assertRaises(pdffit2.constraintError,
             self.P.constrain, 'stepcut', '@17')
     return
Ejemplo n.º 21
0
 def test_num_phases(self):
     """check PdfFit.num_phases()
     """
     self.assertEqual(0, self.P.num_phases())
     self.P.read_struct(datafile('Ni.stru'))
     self.assertEqual(1, self.P.num_phases())
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.assertEqual(2, self.P.num_phases())
     self.P.reset()
     self.assertEqual(0, self.P.num_phases())
     return
Ejemplo n.º 22
0
 def test_get_atom_types(self):
     """check PdfFit.get_atom_types()
     """
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.setphase(1)
     atp1 = self.P.get_atom_types()
     atp2 = self.P.get_atom_types(2)
     self.assertEqual(['NI'], atp1)
     self.assertEqual(['PB', 'O', 'SC', 'W', 'TI'], atp2)
     return
Ejemplo n.º 23
0
 def test_get_structure(self):
     """check PdfFit.get_structure()
     """
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     stru1 = self.P.get_structure(1)
     self.assertEqual(4, len(stru1))
     self.assertEqual('Ni', stru1[0].element)
     stru2 = self.P.get_structure(2)
     self.assertEqual(56, len(stru2))
     self.assertEqual('Ti', stru2[-1].element)
     return
Ejemplo n.º 24
0
 def test_get_structure(self):
     """check PdfFit.get_structure()
     """
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     stru1 = self.P.get_structure(1)
     self.assertEqual(4, len(stru1))
     self.assertEqual('Ni', stru1[0].element)
     stru2 = self.P.get_structure(2)
     self.assertEqual(56, len(stru2))
     self.assertEqual('Ti', stru2[-1].element)
     return
Ejemplo n.º 25
0
 def test_num_atoms(self):
     """check PdfFit.num_atoms()
     """
     self.P.read_struct(datafile('Ni.stru'))
     self.assertEqual(4, self.P.num_atoms())
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.assertEqual(56, self.P.num_atoms())
     self.P.setphase(1)
     self.assertEqual(4, self.P.num_atoms())
     self.P.setphase(2)
     self.assertEqual(56, self.P.num_atoms())
     return
Ejemplo n.º 26
0
 def test_get_atoms(self):
     """check PdfFit.get_atoms()
     """
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.setphase(1)
     a1 = self.P.get_atoms()
     a2 = self.P.get_atoms(2)
     self.assertEqual(4 * ['NI'], a1)
     self.assertEqual(
         8 * ['PB'] + 24 * ['O'] + 8 * ['SC'] + 8 * ['W'] + 8 * ['TI'], a2)
     return
Ejemplo n.º 27
0
 def test_num_atoms(self):
     """check PdfFit.num_atoms()
     """
     self.P.read_struct(datafile('Ni.stru'))
     self.assertEqual(4, self.P.num_atoms())
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.assertEqual(56, self.P.num_atoms())
     self.P.setphase(1)
     self.assertEqual(4, self.P.num_atoms())
     self.P.setphase(2)
     self.assertEqual(56, self.P.num_atoms())
     return
Ejemplo n.º 28
0
 def test_unassignedError2(self):
     """raise pdffit2.unassignedError when set does not exist"""
     self.P.read_struct(datafile("Ni.stru"))
     # fail when there is no dataset
     self.assertRaises(pdffit2.unassignedError, self.P.selectNone,
             self.iset, 'i')
     # pass with dataset
     self.P.read_data(datafile("Ni.dat"), 'X', 25.0, 0.0)
     self.P.selectNone(self.iset, 'i')
     self.P.selectNone(self.iset, 'j')
     # fail for phase 2
     self.assertRaises(pdffit2.unassignedError, self.P.selectNone, 2, 'i')
     self.assertRaises(pdffit2.unassignedError, self.P.selectNone, 2, 'j')
Ejemplo n.º 29
0
 def test_stepcut_io(self):
     """Check reading and writing of stepcut from structure file.
     """
     import re
     self.P.read_struct(datafile('Ni.stru'))
     self.assertEqual(0.0, self.P.getvar('stepcut'))
     # engine should not write shape factor when not defined
     sscnone = self.P.save_struct_string(1)
     self.assertTrue(not re.search('(?m)^shape +stepcut,', sscnone))
     self.P.setvar('stepcut', 7)
     ssc7 = self.P.save_struct_string(1)
     # ssc7 should contain shape factor data
     self.assertTrue(re.search('(?m)^shape +stepcut,', ssc7))
     self.P.reset()
     self.P.read_struct_string(ssc7)
     self.assertEqual(7.0, self.P.getvar('stepcut'))
     # try to read without comma
     ssc14 = re.sub('(?m)^shape +stepcut.*$', 'shape stepcut 14.00', ssc7)
     self.P.read_struct_string(ssc14)
     self.assertEqual(14.0, self.P.getvar('stepcut'))
     # try to read invalid shape data
     sinvalid = re.sub('(?m)^shape .*', 'shape invalid, 1', ssc7)
     self.assertRaises(pdffit2.structureError, self.P.read_struct_string,
                       sinvalid)
     return
Ejemplo n.º 30
0
 def test_stepcut_io(self):
     """Check reading and writing of stepcut from structure file.
     """
     import re
     self.P.read_struct(datafile('Ni.stru'))
     self.assertEqual(0.0, self.P.getvar('stepcut'))
     # engine should not write shape factor when not defined
     sscnone = self.P.save_struct_string(1)
     self.failUnless(not re.search('(?m)^shape +stepcut,', sscnone))
     self.P.setvar('stepcut', 7)
     ssc7 = self.P.save_struct_string(1)
     # ssc7 should contain shape factor data
     self.failUnless(re.search('(?m)^shape +stepcut,', ssc7))
     self.P.reset()
     self.P.read_struct_string(ssc7)
     self.assertEqual(7.0, self.P.getvar('stepcut'))
     # try to read without comma
     ssc14 = re.sub('(?m)^shape +stepcut.*$', 'shape stepcut 14.00', ssc7)
     self.P.read_struct_string(ssc14)
     self.assertEqual(14.0, self.P.getvar('stepcut'))
     # try to read invalid shape data
     sinvalid = re.sub('(?m)^shape .*', 'shape invalid, 1', ssc7)
     self.assertRaises(pdffit2.structureError,
             self.P.read_struct_string, sinvalid)
     return
Ejemplo n.º 31
0
 def test_add_structure(self):
     """check PdfFit.add_structure()
     """
     from diffpy.structure import Structure
     ni = Structure(filename=datafile('Ni.stru'))
     self.P.add_structure(ni)
     self.assertEqual(4, self.P.num_atoms())
     return
Ejemplo n.º 32
0
 def test_get_structure(self):
     """check PdfFit.get_structure()
     """
     # skip test when diffpy.Structure is not installed
     try:
         from diffpy.Structure import Structure
     except ImportError:
         return
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     stru1 = self.P.get_structure(1)
     self.assertEqual(4, len(stru1))
     self.assertEqual('Ni', stru1[0].element)
     stru2 = self.P.get_structure(2)
     self.assertEqual(56, len(stru2))
     self.assertEqual('Ti', stru2[-1].element)
     return
Ejemplo n.º 33
0
 def test_bond_angle(self):
     """check PdfFit.bond_angle()
     """
     self.P.read_struct(datafile('Ni.stru'))
     a, e = self.P.bond_angle(1, 2, 3)
     self.assertAlmostEqual(60.0, a, self.places)
     self.assertRaises(ValueError, self.P.bond_angle, 0, 1, 2)
     self.assertRaises(ValueError, self.P.bond_angle, 1, 2, 7)
     return
Ejemplo n.º 34
0
 def test_bond_angle(self):
     """check PdfFit.bond_angle()
     """
     self.P.read_struct(datafile('Ni.stru'))
     a, e = self.P.bond_angle(1, 2, 3)
     self.assertAlmostEqual(60.0, a, self.places)
     self.assertRaises(ValueError, self.P.bond_angle, 0, 1, 2)
     self.assertRaises(ValueError, self.P.bond_angle, 1, 2, 7)
     return
Ejemplo n.º 35
0
 def test_bond_length_types(self):
     """check PdfFit.bond_length_types()
     """
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     dPbO = self.P.bond_length_types('Pb', 'O', 0.1, 3.0)
     # check if keys are present
     self.assertTrue('dij' in dPbO)
     self.assertTrue('ddij' in dPbO)
     self.assertTrue('ij0' in dPbO)
     self.assertTrue('ij1' in dPbO)
     # check if they have the same length
     npts = len(dPbO['dij'])
     self.assertEqual(npts, len(dPbO['ddij']))
     self.assertEqual(npts, len(dPbO['ij0']))
     self.assertEqual(npts, len(dPbO['ij1']))
     # 8 Pb atoms have coordination 12 in perovskite structure
     self.assertEqual(8 * 12, len(dPbO['dij']))
     self.P.setphase(1)
     dfcc = self.P.bond_length_types('ALL', 'ALL', 0.1, 2.6)
     # 4 Ni atoms with coordination 12
     self.assertEqual(4 * 12, len(dfcc['dij']))
     # invalid element
     self.assertRaises(ValueError, self.P.bond_length_types, 'Ni', 'Nix',
                       0.1, 5.0)
     # check indices ij0
     allij0 = sum(dfcc['ij0'], tuple())
     self.assertEqual(0, min(allij0))
     self.assertEqual(3, max(allij0))
     # check indices ij1
     allij1 = sum(dfcc['ij1'], tuple())
     self.assertEqual(1, min(allij1))
     self.assertEqual(4, max(allij1))
     # check index values
     ij0check = [(i1 - 1, j1 - 1) for i1, j1 in dfcc['ij1']]
     self.assertEqual(ij0check, dfcc['ij0'])
     # test valid element which is not present in the structure
     dnone = self.P.bond_length_types('Ni', 'Au', 0.1, 5.0)
     self.assertEqual(0, len(dnone['dij']))
     self.assertEqual(0, len(dnone['ddij']))
     self.assertEqual(0, len(dnone['ij0']))
     self.assertEqual(0, len(dnone['ij1']))
     return
Ejemplo n.º 36
0
 def test_bond_length_types(self):
     """check PdfFit.bond_length_types()
     """
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     dPbO = self.P.bond_length_types('Pb', 'O', 0.1, 3.0)
     # check if keys are present
     self.failUnless('dij' in dPbO)
     self.failUnless('ddij' in dPbO)
     self.failUnless('ij0' in dPbO)
     self.failUnless('ij1' in dPbO)
     # check if they have the same length
     npts = len(dPbO['dij'])
     self.assertEqual(npts, len(dPbO['ddij']))
     self.assertEqual(npts, len(dPbO['ij0']))
     self.assertEqual(npts, len(dPbO['ij1']))
     # 8 Pb atoms have coordination 12 in perovskite structure
     self.assertEqual(8*12, len(dPbO['dij']))
     self.P.setphase(1)
     dfcc = self.P.bond_length_types('ALL', 'ALL', 0.1, 2.6)
     # 4 Ni atoms with coordination 12
     self.assertEqual(4*12, len(dfcc['dij']))
     # invalid element
     self.assertRaises(ValueError, self.P.bond_length_types, 'Ni', 'Nix', 0.1, 5.0)
     # check indices ij0
     allij0 = sum(dfcc['ij0'], tuple())
     self.assertEqual(0, min(allij0))
     self.assertEqual(3, max(allij0))
     # check indices ij1
     allij1 = sum(dfcc['ij1'], tuple())
     self.assertEqual(1, min(allij1))
     self.assertEqual(4, max(allij1))
     # check index values
     ij0check = [(i1 - 1, j1 - 1) for i1, j1 in dfcc['ij1']]
     self.assertEqual(ij0check, dfcc['ij0'])
     # test valid element which is not present in the structure
     dnone = self.P.bond_length_types('Ni', 'Au', 0.1, 5.0)
     self.assertEqual(0, len(dnone['dij']))
     self.assertEqual(0, len(dnone['ddij']))
     self.assertEqual(0, len(dnone['ij0']))
     self.assertEqual(0, len(dnone['ij1']))
     return
Ejemplo n.º 37
0
 def test_num_datasets(self):
     """check PdfFit.num_datasets()
     """
     self.assertEqual(0, self.P.num_datasets())
     self.P.read_data(datafile('Ni.dat'), 'X', 25.0, 0.5)
     self.assertEqual(1, self.P.num_datasets())
     # failed data should not increase num_datasets
     try:
         self.P.read_data(datafile('badNi.dat'))
     except:
         pass
     self.assertEqual(1, self.P.num_datasets())
     # alloc should increase number of datasets
     # alloc requires a loaded structure
     self.P.read_struct(datafile('Ni.stru'))
     self.P.alloc('X', 30.0, 0.05, 2, 10, 100)
     self.assertEqual(2, self.P.num_datasets())
     self.P.reset()
     self.assertEqual(0, self.P.num_datasets())
     return
Ejemplo n.º 38
0
 def test_num_datasets(self):
     """check PdfFit.num_datasets()
     """
     self.assertEqual(0, self.P.num_datasets())
     self.P.read_data(datafile('Ni.dat'), 'X', 25.0, 0.5)
     self.assertEqual(1, self.P.num_datasets())
     # failed data should not increase num_datasets
     try:
         self.P.read_data(datafile('badNi.dat'))
     except:
         pass
     self.assertEqual(1, self.P.num_datasets())
     # alloc should increase number of datasets
     # alloc requires a loaded structure
     self.P.read_struct(datafile('Ni.stru'))
     self.P.alloc('X', 30.0, 0.05, 2, 10, 100)
     self.assertEqual(2, self.P.num_datasets())
     self.P.reset()
     self.assertEqual(0, self.P.num_datasets())
     return
Ejemplo n.º 39
0
 def test_getcrw_two_datasets(self):
     """check that getcrw() and getrw() are consistent for two datasets.
     """
     self.P.read_data(datafile('Ni.dat'), 'X', 25.0, 0.0)
     self.P.pdfrange(1, 2, 8)
     self.P.read_data(datafile('300K.gr'), 'N', 32.0, 0.0)
     self.P.pdfrange(2, 1, 11)
     self.P.read_struct(datafile('Ni.stru'))
     # mess lattice parameters to have comparable Rw contributions
     self.P.setvar('lat(1)', 3)
     self.P.setvar('lat(2)', 3)
     self.P.setvar('lat(3)', 3)
     self.P.refine()
     rwtot = self.P.getrw()
     self.failUnless(rwtot > 0.0)
     self.P.setdata(1)
     rw1 = self.P.getcrw()[-1]
     self.P.setdata(2)
     rw2 = self.P.getcrw()[-1]
     self.assertAlmostEqual(rwtot**2, rw1**2 + rw2**2, self.places)
     return
Ejemplo n.º 40
0
 def test_add_structure(self):
     """check PdfFit.add_structure()
     """
     # skip test when diffpy.Structure is not installed
     try:
         from diffpy.Structure import Structure
     except ImportError:
         return
     ni = Structure(filename=datafile('Ni.stru'))
     self.P.add_structure(ni)
     self.assertEqual(4, self.P.num_atoms())
     return
Ejemplo n.º 41
0
 def test_getcrw_two_datasets(self):
     """check that getcrw() and getrw() are consistent for two datasets.
     """
     self.P.read_data(datafile('Ni.dat'), 'X', 25.0, 0.0)
     self.P.pdfrange(1, 2, 8)
     self.P.read_data(datafile('300K.gr'), 'N', 32.0, 0.0)
     self.P.pdfrange(2, 1, 11)
     self.P.read_struct(datafile('Ni.stru'))
     # mess lattice parameters to have comparable Rw contributions
     self.P.setvar('lat(1)', 3)
     self.P.setvar('lat(2)', 3)
     self.P.setvar('lat(3)', 3)
     self.P.refine()
     rwtot = self.P.getrw()
     self.assertTrue(rwtot > 0.0)
     self.P.setdata(1)
     rw1 = self.P.getcrw()[-1]
     self.P.setdata(2)
     rw2 = self.P.getcrw()[-1]
     self.assertAlmostEqual(rwtot**2, rw1**2 + rw2**2, self.places)
     return
Ejemplo n.º 42
0
    def test_psel(self):
        """check PdfFit.psel()
        """
        def doalloc():
            self.P.alloc('X', 30.0, 0.05, 2, 10, 100)
            return

        self.assertRaises(pdffit2.unassignedError, self.P.psel, 0)
        self.assertRaises(pdffit2.unassignedError, self.P.psel, 1)
        self.P.read_struct(datafile('Ni.stru'))
        doalloc()
        self.P.calc()
        G1 = self.P.getpdf_fit()
        self.P.reset()
        self.P.read_struct(datafile('PbScW25TiO3.stru'))
        doalloc()
        self.P.calc()
        G2 = self.P.getpdf_fit()
        self.P.reset()
        self.P.read_struct(datafile('Ni.stru'))
        self.P.read_struct(datafile('PbScW25TiO3.stru'))
        doalloc()
        self.P.pdesel('ALL')
        self.P.psel(1)
        self.P.calc()
        self.assertEqual(G1, self.P.getpdf_fit())
        self.P.pdesel('ALL')
        self.P.psel(2)
        self.P.calc()
        self.assertEqual(G2, self.P.getpdf_fit())
        self.P.psel('ALL')
        self.P.calc()
        Gall = self.P.getpdf_fit()
        dGmax = max(
            [abs(g1 + g2 - gall) for g1, g2, gall in zip(G1, G2, Gall)])
        self.assertAlmostEqual(0, dGmax, self.places)
        self.assertRaises(pdffit2.unassignedError, self.P.psel, 10)
        self.assertRaises(pdffit2.unassignedError, self.P.psel, 0)
        self.assertRaises(pdffit2.unassignedError, self.P.psel, -100)
        return
Ejemplo n.º 43
0
 def test_twophase_refinement(self):
     """check PDF refinement of 2 phases that have different spdiameter.
     """
     dcheck1 = 8.0
     dstart1 = 8.2
     dcheck2 = 6.0
     dstart2 = 5.5
     self.P.read_struct(datafile('Ni.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.setvar('spdiameter', dcheck1)
     self.P.calc()
     G1 = numpy.array(self.P.getpdf_fit())
     self.P.reset()
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.setvar('spdiameter', dcheck2)
     self.P.calc()
     G2 = numpy.array(self.P.getpdf_fit())
     r = numpy.array(self.P.getR())
     Gnoise = G1 + G2
     Gnoise[::2] += 0.01
     Gnoise[1::2] -= 0.01
     self.P.reset()
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.read_data_lists('X', 0.0, 0.05, list(r), list(Gnoise))
     self.P.setphase(1)
     self.P.constrain('spdiameter', '@11')
     self.P.setphase(2)
     self.P.constrain('spdiameter', '@12')
     self.P.setpar(11, dstart1)
     self.P.setpar(12, dstart2)
     self.P.refine()
     dfinal2 = self.P.getvar('spdiameter')
     self.P.setphase(1)
     dfinal1 = self.P.getvar('spdiameter')
     self.assertAlmostEqual(dcheck1, dfinal1, 3)
     self.assertAlmostEqual(dcheck2, dfinal2, 3)
     return
Ejemplo n.º 44
0
 def test_psel(self):
     """check PdfFit.psel()
     """
     def doalloc():
         self.P.alloc('X', 30.0, 0.05, 2, 10, 100)
         return
     self.assertRaises(pdffit2.unassignedError, self.P.psel, 0)
     self.assertRaises(pdffit2.unassignedError, self.P.psel, 1)
     self.P.read_struct(datafile('Ni.stru'))
     doalloc()
     self.P.calc()
     G1 = self.P.getpdf_fit()
     self.P.reset()
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     doalloc()
     self.P.calc()
     G2 = self.P.getpdf_fit()
     self.P.reset()
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     doalloc()
     self.P.pdesel('ALL')
     self.P.psel(1)
     self.P.calc()
     self.assertEqual(G1, self.P.getpdf_fit())
     self.P.pdesel('ALL')
     self.P.psel(2)
     self.P.calc()
     self.assertEqual(G2, self.P.getpdf_fit())
     self.P.psel('ALL')
     self.P.calc()
     Gall = self.P.getpdf_fit()
     dGmax = max([abs(g1 + g2 - gall)
         for g1, g2, gall in zip(G1, G2, Gall)])
     self.assertAlmostEqual(0, dGmax, self.places)
     self.assertRaises(pdffit2.unassignedError, self.P.psel, 10)
     self.assertRaises(pdffit2.unassignedError, self.P.psel, 0)
     self.assertRaises(pdffit2.unassignedError, self.P.psel, -100)
     return
Ejemplo n.º 45
0
 def test_twophase_refinement(self):
     """check PDF refinement of 2 phases that have different spdiameter.
     """
     dcheck1 = 8.0
     dstart1 = 8.2
     dcheck2 = 6.0
     dstart2 = 5.5
     self.P.read_struct(datafile('Ni.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.setvar('spdiameter', dcheck1)
     self.P.calc()
     G1 = numpy.array(self.P.getpdf_fit())
     self.P.reset()
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.setvar('spdiameter', dcheck2)
     self.P.calc()
     G2 = numpy.array(self.P.getpdf_fit())
     r = numpy.array(self.P.getR())
     Gnoise = G1 + G2
     Gnoise[::2] += 0.01
     Gnoise[1::2] -= 0.01
     self.P.reset()
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.read_data_lists('X', 0.0, 0.05, list(r), list(Gnoise))
     self.P.setphase(1)
     self.P.constrain('spdiameter', '@11')
     self.P.setphase(2)
     self.P.constrain('spdiameter', '@12')
     self.P.setpar(11, dstart1)
     self.P.setpar(12, dstart2)
     self.P.refine()
     dfinal2 = self.P.getvar('spdiameter')
     self.P.setphase(1)
     dfinal1 = self.P.getvar('spdiameter')
     self.assertAlmostEqual(dcheck1, dfinal1, 3)
     self.assertAlmostEqual(dcheck2, dfinal2, 3)
     return
Ejemplo n.º 46
0
    def test_pdesel(self):
        """check PdfFit.pdesel()
        """
        def doalloc():
            self.P.alloc('X', 30.0, 0.05, 2, 10, 100)
            return

        self.assertRaises(pdffit2.unassignedError, self.P.pdesel, 0)
        self.assertRaises(pdffit2.unassignedError, self.P.pdesel, 1)
        self.P.read_struct(datafile('Ni.stru'))
        doalloc()
        self.P.calc()
        G1 = self.P.getpdf_fit()
        self.P.reset()
        self.P.read_struct(datafile('PbScW25TiO3.stru'))
        doalloc()
        self.P.calc()
        G2 = self.P.getpdf_fit()
        self.P.reset()
        self.P.read_struct(datafile('Ni.stru'))
        self.P.read_struct(datafile('PbScW25TiO3.stru'))
        doalloc()
        self.P.psel('ALL')
        self.P.pdesel(2)
        self.P.calc()
        self.assertEqual(G1, self.P.getpdf_fit())
        self.P.psel('ALL')
        self.P.pdesel(1)
        self.P.calc()
        self.assertEqual(G2, self.P.getpdf_fit())
        self.P.pdesel('ALL')
        self.P.calc()
        G0 = self.P.getpdf_fit()
        self.assertEqual([0.0] * len(G0), G0)
        self.assertRaises(pdffit2.unassignedError, self.P.pdesel, 10)
        self.assertRaises(pdffit2.unassignedError, self.P.pdesel, 0)
        self.assertRaises(pdffit2.unassignedError, self.P.pdesel, -100)
        return
Ejemplo n.º 47
0
 def test_reset_scat(self):
     """check PdfFit.reset_scat()
     """
     # raises exception when no phase exists
     self.assertRaises(pdffit2.unassignedError, self.P.reset_scat, 'Ti')
     # check if it is local to phase
     fPb = self.P.get_scat('X', 'Pb')
     bPb = self.P.get_scat('N', 'Pb')
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.set_scat('X', 'Pb', 142)
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.set_scat('N', 'Pb', -17)
     self.P.setphase(1)
     self.assertNotEqual(fPb, self.P.get_scat('X', 'Pb'))
     self.P.reset_scat('Pb')
     self.assertEqual(fPb, self.P.get_scat('X', 'Pb'))
     self.P.setphase(2)
     self.assertNotEqual(bPb, self.P.get_scat('N', 'Pb'))
     self.P.reset_scat('Pb')
     self.assertEqual(bPb, self.P.get_scat('N', 'Pb'))
     # check exception for invalid inputs
     self.assertRaises(ValueError, self.P.reset_scat, 'Zz')
     return
Ejemplo n.º 48
0
 def test_reset_scat(self):
     """check PdfFit.reset_scat()
     """
     # raises exception when no phase exists
     self.assertRaises(pdffit2.unassignedError, self.P.reset_scat, 'Ti')
     # check if it is local to phase
     fPb = self.P.get_scat('X', 'Pb')
     bPb = self.P.get_scat('N', 'Pb')
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.set_scat('X', 'Pb', 142)
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     self.P.set_scat('N', 'Pb', -17)
     self.P.setphase(1)
     self.assertNotEqual(fPb, self.P.get_scat('X', 'Pb'))
     self.P.reset_scat('Pb')
     self.assertEqual(fPb, self.P.get_scat('X', 'Pb'))
     self.P.setphase(2)
     self.assertNotEqual(bPb, self.P.get_scat('N', 'Pb'))
     self.P.reset_scat('Pb')
     self.assertEqual(bPb, self.P.get_scat('N', 'Pb'))
     # check exception for invalid inputs
     self.assertRaises(ValueError, self.P.reset_scat, 'Zz')
     return
Ejemplo n.º 49
0
 def test_refinement(self):
     """check refinement of sphere envelope factor
     """
     dcheck = 8.0
     dstart = 12.0
     self.P.read_struct(datafile('Ni.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.setvar('spdiameter', dcheck)
     self.P.calc()
     r = numpy.array(self.P.getR())
     Gd8 = numpy.array(self.P.getpdf_fit())
     Gd8noise = Gd8
     Gd8noise[::2] += 0.01
     Gd8noise[1::2] -= 0.01
     self.P.reset()
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_data_lists('X', 0.0, 0.05, list(r), list(Gd8noise))
     self.P.constrain('spdiameter', '@8')
     self.P.setpar(8, dstart)
     self.P.refine()
     dfinal = self.P.getvar('spdiameter')
     self.assertAlmostEqual(dcheck, dfinal, 3)
     return
Ejemplo n.º 50
0
 def test_refinement(self):
     """check refinement of sphere envelope factor
     """
     dcheck = 8.0
     dstart = 12.0
     self.P.read_struct(datafile('Ni.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.setvar('spdiameter', dcheck)
     self.P.calc()
     r = numpy.array(self.P.getR())
     Gd8 = numpy.array(self.P.getpdf_fit())
     Gd8noise = Gd8
     Gd8noise[::2] += 0.01
     Gd8noise[1::2] -= 0.01
     self.P.reset()
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_data_lists('X', 0.0, 0.05, list(r), list(Gd8noise))
     self.P.constrain('spdiameter', '@8')
     self.P.setpar(8, dstart)
     self.P.refine()
     dfinal = self.P.getvar('spdiameter')
     self.assertAlmostEqual(dcheck, dfinal, 3)
     return
Ejemplo n.º 51
0
 def test_pdesel(self):
     """check PdfFit.pdesel()
     """
     def doalloc():
         self.P.alloc('X', 30.0, 0.05, 2, 10, 100)
         return
     self.assertRaises(pdffit2.unassignedError, self.P.pdesel, 0)
     self.assertRaises(pdffit2.unassignedError, self.P.pdesel, 1)
     self.P.read_struct(datafile('Ni.stru'))
     doalloc()
     self.P.calc()
     G1 = self.P.getpdf_fit()
     self.P.reset()
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     doalloc()
     self.P.calc()
     G2 = self.P.getpdf_fit()
     self.P.reset()
     self.P.read_struct(datafile('Ni.stru'))
     self.P.read_struct(datafile('PbScW25TiO3.stru'))
     doalloc()
     self.P.psel('ALL')
     self.P.pdesel(2)
     self.P.calc()
     self.assertEqual(G1, self.P.getpdf_fit())
     self.P.psel('ALL')
     self.P.pdesel(1)
     self.P.calc()
     self.assertEqual(G2, self.P.getpdf_fit())
     self.P.pdesel('ALL')
     self.P.calc()
     G0 = self.P.getpdf_fit()
     self.assertEqual([0.0]*len(G0), G0)
     self.assertRaises(pdffit2.unassignedError, self.P.pdesel, 10)
     self.assertRaises(pdffit2.unassignedError, self.P.pdesel, 0)
     self.assertRaises(pdffit2.unassignedError, self.P.pdesel, -100)
     return
Ejemplo n.º 52
0
 def test_calculation(self):
     """check calculation of sphere envelope factor
     """
     self.P.read_struct(datafile('Ni.stru'))
     self.P.alloc('X', 0.0, 0.05, 0.1, 10, 200)
     self.P.calc()
     d = 8.0
     r = numpy.array(self.P.getR())
     G0 = numpy.array(self.P.getpdf_fit())
     self.P.setvar('spdiameter', d)
     self.P.calc()
     G1 = numpy.array(self.P.getpdf_fit())
     dG = (G0 * spherefactor(r, d) - G1)
     msd = numpy.dot(dG, dG) / len(r)
     self.assertAlmostEqual(0.0, numpy.sqrt(msd), self.places)
     return