def calculate(self): """do the real calculation """ # clean up old results self.rcalc = [] self.Gcalc = [] # do the job if len(self.owner.strucs) == 0: raise ControlConfigError("No structure is given for calculation") # make sure parameters are initialized self.owner.updateParameters() from diffpy.pdffit2 import PdfFit server = PdfFit() # structure needs to be read before dataset allocation for struc in self.owner.strucs: server.read_struct_string(struc.writeStr('pdffit')) for key,var in struc.constraints.items(): server.constrain(key.encode('ascii'), var.formula.encode('ascii')) # set up dataset server.alloc(self.stype, self.qmax, self.qdamp, self.rmin, self.rmax, self.rlen) server.setvar('qbroad', self.qbroad) server.setvar('dscale', self.dscale) # phase related variables # pair selection applies to current dataset, # therefore it has to be done after alloc nstrucs = len(self.owner.strucs) for phaseidx0, struc in enumerate(self.owner.strucs): phaseidx1 = phaseidx0 + 1 server.setphase(phaseidx1) server.setvar('pscale', struc.getvar('pscale')) server.setvar('spdiameter', struc.getvar('spdiameter')) struc.applyPairSelection(server, phaseidx1) # set up parameters for index, par in self.owner.parameters.items(): server.setpar(index, par.initialValue()) # info[0] = init value # fix if fixed. Note: all parameters are free after server.reset(). if par.fixed: server.fixpar(index) # all ready here server.calc() # get results self.rcalc = server.getR() self.Gcalc = server.getpdf_fit()
def calculate(self): """do the real calculation """ # clean up old results self.rcalc = [] self.Gcalc = [] # do the job if len(self.owner.strucs) == 0: raise ControlConfigError("No structure is given for calculation") # make sure parameters are initialized self.owner.updateParameters() from diffpy.pdffit2 import PdfFit server = PdfFit() # structure needs to be read before dataset allocation for struc in self.owner.strucs: server.read_struct_string(struc.writeStr('pdffit')) for key, var in struc.constraints.items(): server.constrain(key.encode('ascii'), var.formula.encode('ascii')) # set up dataset server.alloc(self.stype, self.qmax, self.qdamp, self.rmin, self.rmax, self.rlen) server.setvar('qbroad', self.qbroad) server.setvar('dscale', self.dscale) # phase related variables # pair selection applies to current dataset, # therefore it has to be done after alloc for phaseidx0, struc in enumerate(self.owner.strucs): phaseidx1 = phaseidx0 + 1 server.setphase(phaseidx1) server.setvar('pscale', struc.getvar('pscale')) server.setvar('spdiameter', struc.getvar('spdiameter')) struc.applyPairSelection(server, phaseidx1) # set up parameters for index, par in self.owner.parameters.items(): server.setpar(index, par.initialValue()) # info[0] = init value # fix if fixed. Note: all parameters are free after server.reset(). if par.fixed: server.fixpar(index) # all ready here server.calc() # get results self.rcalc = server.getR() self.Gcalc = server.getpdf_fit()
class getparExceptions(unittest.TestCase): def setUp(self): self.P = PdfFit() def tearDown(self): del self.P def test_unassignedError1(self): """raise pdffit2.unassignedError when parameter does not exist""" self.assertRaises(pdffit2.unassignedError, self.P.getpar, 1) def test_unassignedError2(self): """raise pdffit2.unassignedError when parameter does not exist""" self.P.read_struct(datafile("Ni.stru")) self.P.constrain(self.P.lat(1), 2) self.assertRaises(pdffit2.unassignedError, self.P.getpar, 1)
class getparExceptions(unittest.TestCase): def setUp(self): self.P = PdfFit() def tearDown(self): del self.P def test_unassignedError1(self): """raise pdffit2.unassignedError when parameter does not exist""" self.assertRaises(pdffit2.unassignedError, self.P.getpar, 1) def test_unassignedError2(self): """raise pdffit2.unassignedError when parameter does not exist""" self.P.read_struct(datafile("Ni.stru")) self.P.constrain(self.P.lat(1), 2) self.assertRaises(pdffit2.unassignedError, self.P.getpar, 1)
class constrainExceptions(unittest.TestCase): def setUp(self): self.P = PdfFit() self.par = 1 return def tearDown(self): self.P = None return 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 def test_unassignedError(self): """raise pdffit2.unassignedError when variable is undefined""" self.assertRaises(pdffit2.unassignedError, self.P.constrain, self.P.x(1), self.par) return def test_ValueError(self): """raise ValueError when a variable index does not exist""" self.P.read_struct(datafile("Ni.stru")) self.assertRaises(ValueError, self.P.constrain, self.P.x(6), self.par) return 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
class constrainExceptions(unittest.TestCase): def setUp(self): self.P = PdfFit() self.par = 1 return def tearDown(self): self.P = None return 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 def test_unassignedError(self): """raise pdffit2.unassignedError when variable is undefined""" self.assertRaises(pdffit2.unassignedError, self.P.constrain, self.P.x(1), self.par) return def test_ValueError(self): """raise ValueError when a variable index does not exist""" self.P.read_struct(datafile("Ni.stru")) self.assertRaises(ValueError, self.P.constrain, self.P.x(6), self.par) return 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
class save_resExceptions(unittest.TestCase): def setUp(self): self.P = PdfFit() self.resfile = "temp.res" def tearDown(self): del self.P 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) def test_unassignedError(self): """raise pdffit2.unassignedError when structure is undefined""" self.assertRaises(pdffit2.unassignedError, self.P.save_res, self.resfile)
class save_resExceptions(unittest.TestCase): def setUp(self): self.P = PdfFit() self.resfile = "temp.res" def tearDown(self): del self.P 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) def test_unassignedError(self): """raise pdffit2.unassignedError when structure is undefined""" self.assertRaises(pdffit2.unassignedError, self.P.save_res, self.resfile)
class TestSphereEnvelope(unittest.TestCase): places = 6 def setUp(self): self.P = PdfFit() return def tearDown(self): self.P = None return 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 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 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 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 def test_spdiameter_io(self): """Check reading and writing of spdiameter from structure file. """ import re self.P.read_struct(datafile('Ni.stru')) self.assertEqual(0.0, self.P.getvar('spdiameter')) # engine should not write shape factor when not defined spdnone = self.P.save_struct_string(1) self.failUnless(not re.search('(?m)^shape +sphere,', spdnone)) self.P.setvar('spdiameter', 7) spd7 = self.P.save_struct_string(1) # spd7 should contain shape factor data self.failUnless(re.search('(?m)^shape +sphere,', spd7)) self.P.reset() self.P.read_struct_string(spd7) self.assertEqual(7.0, self.P.getvar('spdiameter')) # try to read without comma spd14 = re.sub('(?m)^shape +sphere.*$', 'shape sphere 14.00', spd7) self.P.read_struct_string(spd14) self.assertEqual(14.0, self.P.getvar('spdiameter')) # try to read invalid shape data sinvalid = re.sub('(?m)^shape .*', 'shape invalid, 1', spd7) self.assertRaises(pdffit2.structureError, self.P.read_struct_string, sinvalid) return
class TestSphereEnvelope(unittest.TestCase): places = 6 def setUp(self): self.P = PdfFit() return def tearDown(self): self.P = None return 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 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 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 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 def test_spdiameter_io(self): """Check reading and writing of spdiameter from structure file. """ import re self.P.read_struct(datafile('Ni.stru')) self.assertEqual(0.0, self.P.getvar('spdiameter')) # engine should not write shape factor when not defined spdnone = self.P.save_struct_string(1) self.assertTrue(not re.search('(?m)^shape +sphere,', spdnone)) self.P.setvar('spdiameter', 7) spd7 = self.P.save_struct_string(1) # spd7 should contain shape factor data self.assertTrue(re.search('(?m)^shape +sphere,', spd7)) self.P.reset() self.P.read_struct_string(spd7) self.assertEqual(7.0, self.P.getvar('spdiameter')) # try to read without comma spd14 = re.sub('(?m)^shape +sphere.*$', 'shape sphere 14.00', spd7) self.P.read_struct_string(spd14) self.assertEqual(14.0, self.P.getvar('spdiameter')) # try to read invalid shape data sinvalid = re.sub('(?m)^shape .*', 'shape invalid, 1', spd7) self.assertRaises(pdffit2.structureError, self.P.read_struct_string, sinvalid) return
# Load data ------------------------------------------------------------------ # Load experimental x-ray PDF data qmax = 30.0 # Q-cutoff used in PDF calculation in 1/A qdamp = 0.01 # instrument Q-resolution factor, responsible for PDF decay pf.read_data('Ni-xray.gr', 'X', qmax, qdamp) # Load nickel structure, must be in PDFFIT or DISCUS format pf.read_struct('Ni.stru') # Configure Refinement ------------------------------------------------------- # Refine lattice parameters a, b, c. # Make them all equal to parameter @1. pf.constrain(pf.lat(1), "@1") pf.constrain(pf.lat(2), "@1") pf.constrain(pf.lat(3), "@1") # set initial value of parameter @1 pf.setpar(1, pf.lat(1)) # Refine phase scale factor. Right side can have formulas. pf.constrain('pscale', '@20 * 2') pf.setpar(20, pf.getvar(pf.pscale) / 2.0) # Refine PDF damping due to instrument Q-resolution. # Left side can be also passed as a reference to PdfFit object pf.constrain(pf.qdamp, '@21') pf.setpar(21, 0.03) # Refine sharpening factor for correlated motion of close atoms.
# Load data ------------------------------------------------------------------ # Load experimental x-ray PDF data qmax = 30.0 # Q-cutoff used in PDF calculation in 1/A qdamp = 0.01 # instrument Q-resolution factor, responsible for PDF decay pf.read_data('Ni-xray.gr', 'X', qmax, qdamp) # Load nickel structure, must be in PDFFIT or DISCUS format pf.read_struct('Ni.stru') # Configure Refinement ------------------------------------------------------- # Refine lattice parameters a, b, c. # Make them all equal to parameter @1. pf.constrain(pf.lat(1), "@1") pf.constrain(pf.lat(2), "@1") pf.constrain(pf.lat(3), "@1") # set initial value of parameter @1 pf.setpar(1, pf.lat(1)) # Refine phase scale factor. Right side can have formulas. pf.constrain('pscale', '@20 * 2') pf.setpar(20, pf.getvar(pf.pscale) / 2.0) # Refine PDF damping due to instrument Q-resolution. # Left side can be also passed as a reference to PdfFit object pf.constrain(pf.qdamp, '@21') pf.setpar(21, 0.03) # Refine sharpening factor for correlated motion of close atoms.