forked from diffpy/diffpy.srfit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnppdfcrystal.py
More file actions
130 lines (99 loc) · 3.78 KB
/
nppdfcrystal.py
File metadata and controls
130 lines (99 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
########################################################################
#
# diffpy.srfit by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2009 The Trustees of Columbia University
# in the City of New York. All rights reserved.
#
# File coded by: Chris Farrow
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE_DANSE.txt for license information.
#
########################################################################
"""Example of fitting a crystal-like nanoparticle (nanocrystal) PDF.
This is an example of modeling the PDF from a nanocrystal as an
attenuated bulk PDF. This involves a crystal PDF calculation and a
spherical nanoparticle characteristic function. The equation we model is
Gnano(r) = f(r) * Gbulk(r), where f(r) is the nanoparticle
characteristic function for the nanoparticle shape. Functions for
calculating the characteristic function in the
diffpy.srfit.pdf.characteristicfunctions module.
"""
import numpy
from gaussianrecipe import scipyOptimize
from pyobjcryst import loadCrystal
from diffpy.srfit.fitbase import (
FitContribution,
FitRecipe,
FitResults,
Profile,
ProfileParser,
)
from diffpy.srfit.pdf import PDFGenerator
def makeRecipe(ciffile, grdata):
"""Make a recipe to model a crystal-like nanoparticle PDF."""
# Set up a PDF fit as has been done in other examples.
pdfprofile = Profile()
pdfparser = ProfileParser()
pdfparser.parseFile(grdata)
pdfprofile.load_parsed_data(pdfparser)
pdfprofile.set_calculation_range(xmin=0.1, xmax=20)
pdfcontribution = FitContribution("pdf")
pdfcontribution.set_profile(pdfprofile, xname="r")
pdfgenerator = PDFGenerator("G")
pdfgenerator.setQmax(30.0)
stru = loadCrystal(ciffile)
pdfgenerator.setStructure(stru)
pdfcontribution.add_profile_generator(pdfgenerator)
# Register the nanoparticle shape factor.
from diffpy.srfit.pdf.characteristicfunctions import sphericalCF
pdfcontribution.register_function(sphericalCF, name="f")
# Now we set up the fitting equation.
pdfcontribution.set_equation("f * G")
# Now make the recipe. Make sure we fit the characteristic function shape
# parameters, in this case 'psize', which is the diameter of the particle.
recipe = FitRecipe()
recipe.add_contribution(pdfcontribution)
phase = pdfgenerator.phase
for par in phase.sgpars:
recipe.add_variable(par)
recipe.add_variable(pdfcontribution.psize, 20)
recipe.add_variable(pdfgenerator.scale, 1)
recipe.add_variable(pdfgenerator.delta2, 0)
recipe.B11_0 = 0.1
return recipe
def plotResults(recipe):
"""Plot the results contained within a refined FitRecipe."""
# All this should be pretty familiar by now.
r = recipe.pdf.profile.x
g = recipe.pdf.profile.y
gcalc = recipe.pdf.profile.ycalc
diffzero = -0.8 * max(g) * numpy.ones_like(g)
diff = g - gcalc + diffzero
gcryst = recipe.pdf.evaluate_equation("G")
gcryst /= recipe.scale.value
fr = recipe.pdf.evaluate_equation("f")
fr *= max(g) / fr[0]
import pylab
pylab.plot(r, g, "bo", label="G(r) Data")
pylab.plot(r, gcryst, "y--", label="G(r) Crystal")
pylab.plot(r, fr, "k--", label="f(r) calculated (scaled)")
pylab.plot(r, gcalc, "r-", label="G(r) Fit")
pylab.plot(r, diff, "g-", label="G(r) diff")
pylab.plot(r, diffzero, "k-")
pylab.xlabel(r"$r (\AA)$")
pylab.ylabel(r"$G (\AA^{-2})$")
pylab.legend(loc=1)
pylab.show()
return
if __name__ == "__main__":
ciffile = "data/pb.cif"
grdata = "data/pb_100_qmin1.gr"
recipe = makeRecipe(ciffile, grdata)
scipyOptimize(recipe)
res = FitResults(recipe)
res.print_results()
plotResults(recipe)
# End of file