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.assertListAlmostEqual([0.3334, 0.6667, 0.0], a0.xyz) self.failUnless(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.assertListAlmostEqual([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_nometa(self): '''check NoMetaStructureAdapter. ''' r0, g0 = PDFCalculator()(nickel) ni1 = Structure(nickel) ni1.pdffit['scale'] = 2.0 r1, g1 = PDFCalculator()(ni1) self.failUnless(numpy.array_equal(r0, r1)) self.failUnless(numpy.allclose(2 * g0, g1)) ni1nm = nometa(ni1) self.failUnless(ni1nm is nometa(ni1nm)) r1nm, g1nm = PDFCalculator()(ni1nm) self.failUnless(numpy.array_equal(r0, r1nm)) self.failUnless(numpy.allclose(g0, g1nm)) ni2 = Structure(nickel) ni2.pdffit['delta2'] = 4 r2, g2 = PDFCalculator()(ni2) r2, g2nm = PDFCalculator()(nometa(ni2)) self.failIf(numpy.allclose(g0, g2)) self.failUnless(numpy.allclose(g0, g2nm)) adpt2 = createStructureAdapter(ni2) ra2, ga2 = PDFCalculator()(adpt2) ra2, ga2nm = PDFCalculator()(nometa(adpt2)) self.failUnless(numpy.allclose(g2, ga2)) self.failUnless(numpy.allclose(g0, ga2nm)) return
def test__get_lattice(self): """check Structure._get_lattice() """ lat = Lattice() stru = Structure() self.assertEqual((1, 1, 1, 90, 90, 90), stru.lattice.abcABG()) stru2 = Structure(lattice=lat) self.failUnless(lat is stru2.lattice) return
def setUp(self): self.stru = Structure( [ Atom('C', [0,0,0]), Atom('C', [1,1,1]) ], lattice=Lattice(1, 1, 1, 90, 90, 120) ) if not self._loaded_structures: self._loaded_structures.update([ ('cdse', Structure(filename=cdsefile)), ('tei', Structure(filename=teifile)), ('pbte', Structure(filename=pbtefile)), ]) self.__dict__.update(self._loaded_structures) self.places = 12 return
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
def makeRecipe(ciffile, datname): """Create a fitting recipe for crystalline PDF data.""" # Work directly with a custom PDFContribution to load the data contribution = PDFContribution("nickel") contribution.loadData(datname) contribution.setCalculationRange(xmin=1, xmax=20, dx=0.1) # and the phase stru = Structure() stru.read(ciffile) contribution.addStructure("nickel", stru) ## Make the FitRecipe and add the FitContribution. recipe = FitRecipe() recipe.addContribution(contribution) ## Configure the fit variables phase = contribution.nickel.phase from diffpy.srfit.structure import constrainAsSpaceGroup sgpars = constrainAsSpaceGroup(phase, "Fm-3m") for par in sgpars.latpars: recipe.addVar(par) for par in sgpars.adppars: recipe.addVar(par, 0.005) recipe.addVar(contribution.scale, 1) recipe.addVar(contribution.qdamp, 0.03, fixed=True) recipe.addVar(contribution.nickel.delta2, 5) # Give the recipe away so it can be used! return recipe
def test___getitem__(self): """check Structure.__getitem__() """ stru = self.stru self.assertTrue(stru[0] is stru.tolist()[0]) intidx = range(len(stru))[::-1] self.assertEqual(stru[intidx].tolist(), stru.tolist()[::-1]) flagidx = (numpy.arange(len(stru)) > 0) self.assertEqual(stru[flagidx].tolist(), stru.tolist()[1:]) cdse = Structure(self.cdse) self.assertEqual([cdse[0], cdse[-2]], cdse[0, -2].tolist()) cdse013 = cdse.tolist() cdse013.pop(2) self.assertEqual(cdse013, cdse[:2, 3].tolist()) self.assertRaises(IndexError, cdse.__getitem__, 'Cd1') cdse.assignUniqueLabels() self.assertTrue(cdse[0] is cdse['Cd1']) cdse[0].label = 'Hohenzollern' self.assertRaises(IndexError, cdse.__getitem__, 'Cd1') self.assertTrue(cdse[0] is cdse['Hohenzollern']) self.assertEqual([cdse[0], cdse[3], cdse[1]], cdse['Hohenzollern', 3:0:-2].tolist()) stru.label = ['A', 'B'] self.assertTrue(stru[0] is stru['A']) self.assertTrue(stru[1] is stru['B']) stru[1].label = 'A' self.assertRaises(IndexError, stru.__getitem__, 'A') return
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.assertListAlmostEqual(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.assertListAlmostEqual(s_Uii, f_Uii) s_sigUii = [a0.sigU[i, i] for i in range(3)] f_sigUii = [0.00011127, 0.00011127, 0.00019575] self.assertListAlmostEqual(s_sigUii, f_sigUii) s_title = stru.title f_title = "Cell structure file of CdSe #186" self.assertEqual(s_title, f_title)
def main(): s = raw_input('Enter rmin: ') if s.strip(): distprint.rmin = float(s) print "rmin =", distprint.rmin s = raw_input('Enter rmax: ') if s.strip(): distprint.rmax = float(s) print "rmax =", distprint.rmax print linesep = 78 * '-' # C60bucky print linesep raw_input('Press enter for distances in C60 molecule.') distprint.eval(bucky) # nickel print linesep raw_input('Press enter for distances in a nickel crystal.') distprint.eval(nickel) # objcryst sphalerite print linesep raw_input('Press enter for distances in objcryst loaded sphalerite.cif.') crst = get_pyobjcryst_sphalerite() distprint.eval(crst) print linesep raw_input('Press enter for distances in diffpy.Structure sphalerite.cif.') crst = Structure(filename='datafiles/sphalerite.cif') distprint.eval(crst) return
def test_angle(self): """check Structure.angle() """ cdse = Structure(filename=cdsefile) cdse.assignUniqueLabels() self.assertEqual(109, round(cdse.angle(0, 2, 1))) self.assertEqual(109, round(cdse.angle("Cd1", "Se1", "Cd2"))) return
def parseLines(self, lines): """Parse list of lines in RAWXYZ format. Return Structure object or raise StructureFormatError. """ linefields = [l.split() for l in lines] # prepare output structure stru = Structure() # find first valid record start = 0 for field in linefields: if len(field) == 0 or field[0] == "#": start += 1 else: break # find the last valid record stop = len(lines) while stop > start and len(linefields[stop-1]) == 0: stop -= 1 # get out for empty structure if start >= stop: return stru # here we have at least one valid record line # figure out xyz layout from the first line for plain and raw formats floatfields = [ isfloat(f) for f in linefields[start] ] nfields = len(linefields[start]) if nfields not in (3, 4): emsg = ("%d: invalid RAWXYZ format, expected 3 or 4 columns" % (start + 1)) raise StructureFormatError(emsg) if floatfields[:3] == [True, True, True]: el_idx, x_idx = (None, 0) elif floatfields[:4] == [False, True, True, True]: el_idx, x_idx = (0, 1) else: emsg = "%d: invalid RAWXYZ format" % (start + 1) raise StructureFormatError(emsg) # now try to read all record lines try: p_nl = start for fields in linefields[start:] : p_nl += 1 if fields == []: continue elif len(fields) != nfields: emsg = ('%d: all lines must have ' + 'the same number of columns') % p_nl raise StructureFormatError, emsg element = el_idx is not None and fields[el_idx] or "" xyz = [ float(f) for f in fields[x_idx:x_idx+3] ] if len(xyz) == 2: xyz.append(0.0) stru.addNewAtom(element, xyz=xyz) except ValueError: emsg = "%d: invalid number" % p_nl exc_type, exc_value, exc_traceback = sys.exc_info() raise StructureFormatError, emsg, exc_traceback return stru
def main(): # load structure from a specified file, by default "lj50.xyz" filename = len(sys.argv) > 1 and sys.argv[1] or "lj50.xyz" stru = Structure() stru.read(filename) # create an instance of LennardJonesCalculator ljcalc = LennardJonesCalculator() # calculate and print the LJ potential. print "LJ potential of %s is %g" % (filename, ljcalc(stru))
def makeC60(): """Make the C60 molecule using diffpy.Structure.""" from diffpy.Structure import Structure stru = Structure() for line in c60xyz.splitlines(): if not line: continue xyz = map(float, line.split()) stru.addNewAtom("C", xyz) return stru
def loadStructureFile(filename, format="auto"): """Load structure from specified file. Return a tuple of (Structure, fileformat). """ from diffpy.Structure import Structure stru = Structure() p = stru.read(filename, format) fileformat = p.format return (stru, fileformat)
def test_pickling(self): """Test pickling of DiffpyStructureParSet. """ stru = Structure([Atom("C", [0, 0.2, 0.5])]) dsps = DiffpyStructureParSet("dsps", stru) data = pickle.dumps(dsps) dsps2 = pickle.loads(data) self.assertEqual(1, len(dsps2.atoms)) self.assertEqual(0.2, dsps2.atoms[0].y.value) return
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
def test_label(self): """check Structure.label """ cdse = Structure(self.cdse) self.assertEqual(4 * [''], cdse.label.tolist()) cdse.assignUniqueLabels() self.assertEqual('Cd1 Cd2 Se1 Se2'.split(), cdse.label.tolist()) cdse.label = cdse.label.lower() self.assertEqual('cd1 cd2 se1 se2'.split(), cdse.label.tolist()) return
def supercell(S, mno): """Perform supercell expansion for a structure. New lattice parameters are multiplied and fractional coordinates divided by corresponding multiplier. New atoms are grouped with their source in the original cell. S -- an instance of Structure from diffpy.Structure. mno -- sequence of 3 integers for cell multipliers along the a, b and c axes. Return a new expanded structure instance. Raise TypeError when S is not Structure instance. Raise ValueError for invalid mno argument. """ # check arguments if len(mno) != 3: emsg = "Argument mno must contain 3 numbers." raise ValueError, emsg elif min(mno) < 1: emsg = "Multipliers must be greater or equal 1" raise ValueError, emsg if not isinstance(S, Structure): emsg = "The first argument must be a Structure instance." raise TypeError, emsg # convert mno to a tuple of integers so it can be used as range limit. mno = (int(mno[0]), int(mno[1]), int(mno[2])) # create return instance newS = Structure(S) if mno == (1, 1, 1): return newS # back to business ijklist = [(i, j, k) for i in range(mno[0]) for j in range(mno[1]) for k in range(mno[2])] # numpy.floor returns float array mnofloats = numpy.array(mno, dtype=float) # build a list of new atoms newAtoms = [] for a in S: for ijk in ijklist: adup = Atom(a) adup.xyz = (a.xyz + ijk) / mnofloats newAtoms.append(adup) # newS can own references in newAtoms, no need to make copies newS.__setslice__(0, len(newS), newAtoms, copy=False) # take care of lattice parameters newS.lattice.setLatPar(a=mno[0] * S.lattice.a, b=mno[1] * S.lattice.b, c=mno[2] * S.lattice.c) return newS
def test___repr__(self): """Test representation of DiffpyStructureParSet objects. """ lat = Lattice(3, 3, 2, 90, 90, 90) atom = Atom("C", [0, 0.2, 0.5]) stru = Structure([atom], lattice=lat) dsps = DiffpyStructureParSet("dsps", stru) self.assertEqual(repr(stru), repr(dsps)) self.assertEqual(repr(lat), repr(dsps.lattice)) self.assertEqual(repr(atom), repr(dsps.atoms[0])) return
def test___copy__(self): """check Structure.__copy__() """ cdse = Structure(filename=cdsefile) cdse_str = cdse.writeStr('pdffit') cdse2 = copy.copy(cdse) self.assertEqual(cdse_str, cdse2.writeStr('pdffit')) self.failIf(cdse.lattice is cdse2.lattice) sameatoms = set(cdse).intersection(cdse2) self.failIf(sameatoms) return
def makeData(strufile, q, datname, scale, a, Uiso, sig, bkgc, nl = 1): """Make some fake data and save it to file. Make some data to fit. This uses iofq to calculate an intensity curve, and adds to it a background, broadens the peaks, and noise. strufile-- A filename holding the sample structure q -- The q-range to calculate over. datname -- The name of the file we're saving to. scale -- The scale factor a -- The lattice constant to use Uiso -- The thermal factor for all atoms sig -- The broadening factor bkgc -- A parameter that gives minor control of the background. nl -- Noise level (0, inf), default 1, larger -> less noise. """ from diffpy.Structure import Structure S = Structure() S.read(strufile) # Set the lattice parameters S.lattice.setLatPar(a, a, a) # Set a DW factor for a in S: a.Uisoequiv = Uiso y = iofq(S, q) # We want to broaden the peaks as well. This simulates instrument effects. q0 = q[len(q)/2] g = numpy.exp(-0.5*((q-q0)/sig)**2) y = numpy.convolve(y, g, mode='same')/sum(g) # Add a polynomial background. bkgd = (q + bkgc)**2 * (1.5*max(q) - q)**5 bkgd *= 0.2 * max(y) / max(bkgd) y += bkgd # Multipy by a scale factor y *= scale # Calculate the uncertainty u = (y/nl)**0.5 # And apply the noise if nl > 0: y = numpy.random.poisson(y*nl) / nl # Now save it numpy.savetxt(datname, zip(q, y, u)) return
def main(): # Make the data and the recipe data = "../data/MEF_300-00000.gr" basename = "MEF_300K_LS" print basename # Make the recipe from diffpy.Structure import Structure stru1 = Structure(filename='../data/MEF.cif') stru2 = Structure(filename='../data/MEF.xyz') stru3 = Structure(filename='../data/MEF.xyz') recipe = makeRecipe(stru1, stru2, stru3, data) if _plot: from diffpy.srfit.fitbase.fithook import PlotFitHook recipe.pushFitHook(PlotFitHook()) recipe.fithooks[0].verbose = 3 from scipy.optimize import leastsq leastsq(recipe.residual, recipe.values) # Save structures stru1.write(basename + "_Cryst_B_zoomed.stru", "pdffit") stru2.write(basename + "_Mole_B_zoomed.xyz", "xyz") stru2.write(basename + "_Intra_zoomed.xyz", "xyz") profile = recipe.MEF.profile #import IPython.Shell; IPython.Shell.IPShellEmbed(argv=[])() profile.savetxt(basename + ".fit") # Generate and print the FitResults res = FitResults(recipe) res.printResults() header = "MEF Organic PDF fit.\n" res.saveResults(basename + ".res", header=header) # Plot! if _plot: plotResults(recipe)
def test_extend(self): """check Structure.extend() """ stru = self.stru cdse = Structure(filename=cdsefile) lst = stru.tolist() stru.extend(cdse) self.assertEqual(6, len(stru)) self.assertTrue(all(a.lattice is stru.lattice for a in stru)) self.assertEqual(lst, stru.tolist()[:2]) self.assertNotEqual(stru[-1], cdse[-1]) return
def test_nometa_pickling(self): '''check pickling of the NoMetaStructureAdapter wrapper. ''' r0, g0 = PDFCalculator()(nickel) ni1 = Structure(nickel) ni1.pdffit['scale'] = 2.0 ni1nm = cPickle.loads(cPickle.dumps(nometa(ni1))) self.failIf(ni1nm is ni1) r1nm, g1nm = PDFCalculator()(ni1nm) self.failUnless(numpy.array_equal(r0, r1nm)) self.failUnless(numpy.array_equal(g0, g1nm)) return
def test_setQmax(self): """check PDFContribution.setQmax() """ from diffpy.Structure import Structure pc = self.pc pc.setQmax(21) pc.addStructure('empty', Structure()) self.assertEqual(21, pc.empty.getQmax()) pc.setQmax(22) self.assertEqual(22, pc.getQmax()) self.assertEqual(22, pc.empty.getQmax()) return
def main(): # Make the data and the recipe data = "../data/ON_RT_5-00000.gr" basename = "ROY_Least_Squares" print basename # Make the recipe from diffpy.Structure import Structure stru1 = Structure(filename='../data/QAXMEH_ON.cif') stru2 = Structure(filename='../data/QAXMEH_ON.xyz' ) # this is non-distorted structure, keep it fixed. stru3 = Structure(filename='../data/QAXMEH_ON.xyz') recipe = makeRecipe(stru1, stru2, stru3, data) if _plot: from diffpy.srfit.fitbase.fithook import PlotFitHook recipe.pushFitHook(PlotFitHook()) recipe.fithooks[0].verbose = 3 from scipy.optimize import leastsq leastsq(recipe.residual, recipe.values) # Save structures stru1.write(basename + "_Cryst_B_zoomed.stru", "pdffit") stru2.write(basename + "_Mole_B_zoomed.xyz", "xyz") stru2.write(basename + "_Intra_zoomed.xyz", "xyz") profile = recipe.ROY.profile profile.savetxt(basename + ".fit") res = FitResults(recipe) res.printResults() header = "A+B-C.\n" res.saveResults(basename + ".res", header=header) # Plot! if _plot: plotResults(recipe, basename)
def test___init__(self): """check Structure.__init__() """ atoms = [Atom('C', [0, 0, 0]), Atom('C', [0.5, 0.5, 0.5])] self.assertRaises(ValueError, Structure, atoms, filename=teifile) self.assertRaises(ValueError, Structure, lattice=Lattice(), filename=teifile) self.assertRaises(ValueError, Structure, title='test', filename=teifile) stru1 = Structure(title='my title') self.assertEqual('my title', stru1.title) stru2a = Structure(atoms) stru2b = Structure(iter(atoms)) stru2c = Structure(a for a in atoms) s2a = str(stru2a) self.assertEqual(s2a, str(stru2b)) self.assertEqual(s2a, str(stru2c)) return
def test___sub__(self): """check Structure.__sub__() """ cdse = Structure(filename=cdsefile) cadmiums = cdse - cdse[2:] self.assertEqual(2, len(cadmiums)) self.assertEqual('Cd', cadmiums[0].element) self.assertEqual('Cd', cadmiums[1].element) self.failUnless(numpy.array_equal(cdse[0].xyz, cadmiums[0].xyz)) self.failUnless(numpy.array_equal(cdse[1].xyz, cadmiums[1].xyz)) self.failIf(cdse[0] is cadmiums[0]) self.failIf(cdse.lattice is cadmiums.lattice) return
def test___isub__(self): """check Structure.__isub__() """ cdse = Structure(filename=cdsefile) lat = cdse.lattice lst = cdse.tolist() cdse -= cdse[2:] self.assertEqual(2, len(cdse)) self.assertEqual(4, len(lst)) self.assertEqual('Cd', cdse[0].element) self.assertEqual('Cd', cdse[1].element) self.assertEqual(lat, cdse.lattice) self.assertEqual(lst[:2], cdse.tolist()) return
def test_setStructure(self): """check PairQuantity.setStructure() """ from diffpy.Structure import Structure, Atom from diffpy.srreal.structureadapter import EMPTY stru = Structure([Atom("Ar", [0.1, 0.2, 0.3])]) self.pq.setStructure(stru) adpt = self.pq.getStructure() self.assertEqual(1, adpt.countSites()) self.assertEqual("Ar", adpt.siteAtomType(0)) self.pq.setStructure(EMPTY) adpt = self.pq.getStructure() self.assertEqual(0, adpt.countSites()) return