本文整理汇总了Python中diffpy.Structure.Structure.read方法的典型用法代码示例。如果您正苦于以下问题:Python Structure.read方法的具体用法?Python Structure.read怎么用?Python Structure.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类diffpy.Structure.Structure
的用法示例。
在下文中一共展示了Structure.read方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: makeRecipe
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import read [as 别名]
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
示例2: test_write_and_read
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import read [as 别名]
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.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
示例3: main
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import read [as 别名]
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))
示例4: makeData
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import read [as 别名]
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
示例5: loadStructureFile
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import read [as 别名]
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)
示例6: outputFormats
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import read [as 别名]
print >> sys.stderr, "'%s' is not valid input format" % infmt
sys.exit(2)
if outfmt not in outputFormats():
print >> sys.stderr, "'%s' is not valid output format" % outfmt
sys.exit(2)
except ValueError:
print >> sys.stderr, \
"invalid format specification '%s' does not contain .." % args[0]
sys.exit(2)
# ready to do some real work
try:
strufile = args[1]
stru = Structure()
if args[1] == "-":
stru.readStr(sys.stdin.read(), infmt)
else:
stru.read(strufile, infmt)
sys.stdout.write( stru.writeStr(outfmt) )
except IndexError:
print >> sys.stderr, "strufile not specified"
sys.exit(2)
except IOError, (errno, errmsg):
print >> sys.stderr, "%s: %s" % (strufile, errmsg)
sys.exit(1)
except StructureFormatError, errmsg:
print >> sys.stderr, "%s: %s" % (strufile, errmsg)
sys.exit(1)
if __name__ == "__main__":
main()
示例7: setStructure
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import read [as 别名]
def setStructure(self, strufile):
"""Set the structure used in the calculation.
strufile -- The name of a structure file. A
diffpy.Structure.Structure object will be created from
the file, and that object will be passed to the 'iofq'
function whenever it is called.
This will create the refinement Parameters using the
DiffpyStructureParSet adapter from diffpy.srfit.structure.diffpyparset.
DiffpyStructureParSet is a ParameterSet object that organizes and gives
attribute access to Parameters and ParameterSets adapted from a diffpy
Structure object. The Parameters embedded in the DiffpyStructureParSet
are proxies for attributes of the diffpy.Structure.Structure object
that is needed by the 'iofq' function. The Parameters will be
accessible by name under the 'phase' attribute of this generator, and
are organized hierarchically:
phase
- lattice (retrieved with 'getLattice')
- a
- b
- c
- alpha
- beta
- gamma
- scatterers (retrieved with 'getScatterers')
- atom1 (the name depends on the element)
- x
- y
- z
- occ
- U11
- U22
- U33
- U12
- U13
- U23
- Uiso
- etc.
The diffpy.Structure.Structure instance is held within the
DiffpyStructureParSet as the 'stru' attribute.
"""
# Load the structure from file
from diffpy.Structure import Structure
stru = Structure()
stru.read(strufile)
# Create a ParameterSet designed to interface with
# diffpy.Structure.Structure objects that organizes the Parameter
# hierarchy. Note that the DiffpyStructureParSet holds a handle to the
# loaded structure that we use in the __call__ method below.
#
# We pass the diffpy.Structure.Structure instance, and give the
# DiffpyStructureParSet the name "phase".
parset = DiffpyStructureParSet("phase", stru)
# Put this ParameterSet in the ProfileGenerator.
self.addParameterSet(parset)
return
示例8: if
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import read [as 别名]
# Discard atom if (x/a)**2 + (y/b)**2 + (z/c)**2 > 1
if d > 1:
delList.append(j)
for i in delList:
newS.pop(i)
return newS
if __name__ == "__main__":
import os.path
datadir = "../../tests/testdata"
S = Structure()
S.read(os.path.join(datadir, "CdSe_bulk.stru"), "pdffit")
newS = makeEllipsoid(S, 12)
newS.write("CdSe_d24.stru", "pdffit")
newS = makeEllipsoid(S, 20, 10, 10)
newS.write("CdSe_a20_b10_c10.stru", "pdffit")
newS = makeEllipsoid(S, 20, 15, 10)
newS.write("CdSe_a20_b15_c10.stru", "pdffit")
S = Structure()
S.read(os.path.join(datadir, "Ni.stru"), "pdffit")
newS = makeEllipsoid(S, 10)
newS.write("Ni_d20.stru", "pdffit")
newS = makeEllipsoid(S, 20, 4)
newS.write("Ni_a20_b4_c20.stru", "pdffit")
newS = makeEllipsoid(S, 20, 15, 10)
newS.write("Ni_a20_b15_c10.stru", "pdffit")
示例9: TestP_pdffit
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import read [as 别名]
class TestP_pdffit(unittest.TestCase):
"""test Parser for PDFFit file format"""
assertListAlmostEqual = assertListAlmostEqual
def setUp(self):
self.stru = Structure()
self.format = "pdffit"
self.places = 8
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.assertListAlmostEqual(s_lat, f_lat)
s_dcell = stru.pdffit['dcell']
f_dcell = [0.000008, 0.000008, 0.000013, 0.0, 0.0, 0.0]
self.assertListAlmostEqual(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.assertListAlmostEqual(s_xyz, f_xyz)
self.assertListAlmostEqual(s_sigxyz, f_sigxyz)
self.assertListAlmostEqual(s_U, f_U)
self.assertAlmostEqual(s_o, f_o)
self.assertAlmostEqual(s_sigo, f_sigo)
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)
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]]
#.........这里部分代码省略.........
示例10: makeRecipe
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import read [as 别名]
def makeRecipe(ciffile, datname):
"""Create a fitting recipe for crystalline PDF data."""
## The Profile
# This will be used to store the observed and calculated PDF profile.
profile = Profile()
# Load data and add it to the Profile. Unlike in other examples, we use a
# class (PDFParser) to help us load the data. This class will read the data
# and relevant metadata from a two- to four-column data file generated
# with PDFGetX2 or PDFGetN. The metadata will be passed to the PDFGenerator
# when they are associated in the FitContribution, which saves some
# configuration steps.
parser = PDFParser()
parser.parseFile(datname)
profile.loadParsedData(parser)
profile.setCalculationRange(xmax = 20)
## The ProfileGenerator
# The PDFGenerator is for configuring and calculating a PDF profile. Here,
# we want to refine a Structure object from diffpy.Structure. We tell the
# PDFGenerator that with the 'setStructure' method. All other configuration
# options will be inferred from the metadata that is read by the PDFParser.
# In particular, this will set the scattering type (x-ray or neutron), the
# Qmax value, as well as initial values for the non-structural Parameters.
generator = PDFGenerator("G")
stru = Structure()
stru.read(ciffile)
generator.setStructure(stru)
## The FitContribution
# Here we associate the Profile and ProfileGenerator, as has been done
# before.
contribution = FitContribution("nickel")
contribution.addProfileGenerator(generator)
contribution.setProfile(profile, xname = "r")
## Make the FitRecipe and add the FitContribution.
recipe = FitRecipe()
recipe.addContribution(contribution)
## Configure the fit variables
# The PDFGenerator class holds the ParameterSet associated with the
# Structure passed above in a data member named "phase". (We could have
# given the ParameterSet a name other than "phase" when we added it to the
# PDFGenerator.) The ParameterSet in this case is a StructureParameterSet,
# the documentation for which is found in the
# diffpy.srfit.structure.diffpystructure module.
phase = generator.phase
# We start by constraining the phase to the known space group. We could do
# this by hand, but there is a method in diffpy.srfit.structure named
# 'constrainAsSpaceGroup' for this purpose. The constraints will by default
# be applied to the sites, the lattice and to the ADPs. See the method
# documentation for more details. The 'constrainAsSpaceGroup' method may
# create new Parameters, which it returns in a SpaceGroupParameters object.
from diffpy.srfit.structure import constrainAsSpaceGroup
sgpars = constrainAsSpaceGroup(phase, "Fm-3m")
# The SpaceGroupParameters object returned by 'constrainAsSpaceGroup' holds
# the free Parameters allowed by the space group constraints. Once a
# structure is constrained, we need (should) only use the Parameters
# provided in the SpaceGroupParameters, as the relevant structure
# Parameters are constrained to these.
#
# We know that the space group does not allow for any free sites because
# each atom is on a special position. There is one free (cubic) lattice
# parameter and one free (isotropic) ADP. We can access these Parameters in
# the xyzpars, latpars, and adppars members of the SpaceGroupParameters
# object.
for par in sgpars.latpars:
recipe.addVar(par)
for par in sgpars.adppars:
recipe.addVar(par, 0.005)
# We now select non-structural parameters to refine.
# This controls the scaling of the PDF.
recipe.addVar(generator.scale, 1)
# This is a peak-damping resolution term.
recipe.addVar(generator.qdamp, 0.01)
# This is a vibrational correlation term that sharpens peaks at low-r.
recipe.addVar(generator.delta2, 5)
# Give the recipe away so it can be used!
return recipe