31
31
from diffpy .srfit .fitbase import FitContribution , FitRecipe
32
32
from diffpy .srfit .fitbase import FitResults
33
33
34
- ####### Example Code
34
+ # Example Code
35
+
35
36
36
37
def makeRecipe (stru1 , stru2 , datname ):
37
38
"""Create a fitting recipe for crystalline PDF data."""
38
39
39
- ## The Profile
40
+ # The Profile
40
41
profile = Profile ()
41
42
42
43
# Load data and add it to the profile
43
44
parser = PDFParser ()
44
45
parser .parseFile (datname )
45
46
profile .loadParsedData (parser )
46
- profile .setCalculationRange (xmin = 1.5 , xmax = 45 , dx = 0.1 )
47
+ profile .setCalculationRange (xmin = 1.5 , xmax = 45 , dx = 0.1 )
47
48
48
- ## The ProfileGenerator
49
+ # The ProfileGenerator
49
50
# In order to fit the core and shell phases simultaneously, we must use two
50
51
# PDFGenerators.
51
52
#
@@ -61,20 +62,20 @@ def makeRecipe(stru1, stru2, datname):
61
62
generator_zns .setQmax (26 )
62
63
generator_zns .qdamp .value = 0.0396
63
64
64
- ## The FitContribution
65
+ # The FitContribution
65
66
# Add both generators and the profile to the FitContribution.
66
67
contribution = FitContribution ("cdszns" )
67
68
contribution .addProfileGenerator (generator_cds )
68
69
contribution .addProfileGenerator (generator_zns )
69
- contribution .setProfile (profile , xname = "r" )
70
+ contribution .setProfile (profile , xname = "r" )
70
71
71
72
# Set up the characteristic functions. We use a spherical CF for the core
72
73
# and a spherical shell CF for the shell. Since this is set up as two
73
74
# phases, we implicitly assume that the core-shell correlations contribute
74
75
# very little to the PDF.
75
76
from diffpy .srfit .pdf .characteristicfunctions import sphericalCF , shellCF
76
- contribution .registerFunction (sphericalCF , name = "f_CdS" )
77
- contribution .registerFunction (shellCF , name = "f_ZnS" )
77
+ contribution .registerFunction (sphericalCF , name = "f_CdS" )
78
+ contribution .registerFunction (shellCF , name = "f_ZnS" )
78
79
79
80
# Write the fitting equation. We want to sum the PDFs from each phase and
80
81
# multiply it by a scaling factor.
@@ -90,7 +91,7 @@ def makeRecipe(stru1, stru2, datname):
90
91
recipe .addVar (contribution .thickness , 11 )
91
92
recipe .constrain (contribution .psize , "2 * radius" )
92
93
93
- ## Configure the fit variables
94
+ # Configure the fit variables
94
95
# Start by configuring the scale factor and resolution factors.
95
96
# We want the sum of the phase scale factors to be 1.
96
97
recipe .newVar ("scale_CdS" , 0.7 )
@@ -106,27 +107,28 @@ def makeRecipe(stru1, stru2, datname):
106
107
# subsequent refinement.
107
108
phase_cds = generator_cds .phase
108
109
for par in phase_cds .sgpars .latpars :
109
- recipe .addVar (par , name = par .name + "_cds" , tag = "lat" )
110
+ recipe .addVar (par , name = par .name + "_cds" , tag = "lat" )
110
111
for par in phase_cds .sgpars .adppars :
111
- recipe .addVar (par , 1 , name = par .name + "_cds" , tag = "adp" )
112
- recipe .addVar (phase_cds .sgpars .xyzpars .z_1 , name = "z_1_cds" , tag = "xyz" )
112
+ recipe .addVar (par , 1 , name = par .name + "_cds" , tag = "adp" )
113
+ recipe .addVar (phase_cds .sgpars .xyzpars .z_1 , name = "z_1_cds" , tag = "xyz" )
113
114
# Since we know these have stacking disorder, constrain the B33 adps for
114
115
# each atom type.
115
116
recipe .constrain ("B33_1_cds" , "B33_0_cds" )
116
- recipe .addVar (generator_cds .delta2 , name = "delta2_cds" , value = 5 )
117
+ recipe .addVar (generator_cds .delta2 , name = "delta2_cds" , value = 5 )
117
118
118
119
phase_zns = generator_zns .phase
119
120
for par in phase_zns .sgpars .latpars :
120
- recipe .addVar (par , name = par .name + "_zns" , tag = "lat" )
121
+ recipe .addVar (par , name = par .name + "_zns" , tag = "lat" )
121
122
for par in phase_zns .sgpars .adppars :
122
- recipe .addVar (par , 1 , name = par .name + "_zns" , tag = "adp" )
123
- recipe .addVar (phase_zns .sgpars .xyzpars .z_1 , name = "z_1_zns" , tag = "xyz" )
123
+ recipe .addVar (par , 1 , name = par .name + "_zns" , tag = "adp" )
124
+ recipe .addVar (phase_zns .sgpars .xyzpars .z_1 , name = "z_1_zns" , tag = "xyz" )
124
125
recipe .constrain ("B33_1_zns" , "B33_0_zns" )
125
- recipe .addVar (generator_zns .delta2 , name = "delta2_zns" , value = 2.5 )
126
+ recipe .addVar (generator_zns .delta2 , name = "delta2_zns" , value = 2.5 )
126
127
127
128
# Give the recipe away so it can be used!
128
129
return recipe
129
130
131
+
130
132
def plotResults (recipe ):
131
133
"""Plot the results contained within a refined FitRecipe."""
132
134
@@ -138,10 +140,10 @@ def plotResults(recipe):
138
140
diff = g - gcalc + diffzero
139
141
140
142
import pylab
141
- pylab .plot (r ,g , 'bo' ,label = "G(r) Data" )
142
- pylab .plot (r , gcalc ,'r-' ,label = "G(r) Fit" )
143
- pylab .plot (r ,diff ,'g-' ,label = "G(r) diff" )
144
- pylab .plot (r ,diffzero ,'k-' )
143
+ pylab .plot (r , g , 'bo' , label = "G(r) Data" )
144
+ pylab .plot (r , gcalc , 'r-' , label = "G(r) Fit" )
145
+ pylab .plot (r , diff , 'g-' , label = "G(r) diff" )
146
+ pylab .plot (r , diffzero , 'k-' )
145
147
pylab .xlabel (r"$r (\AA)$" )
146
148
pylab .ylabel (r"$G (\AA^{-2})$" )
147
149
pylab .legend (loc = 1 )
@@ -172,23 +174,23 @@ def main():
172
174
# Start with the lattice parameters. In makeRecipe, these were tagged with
173
175
# "lat". Here is how we use that.
174
176
recipe .free ("lat" )
175
- leastsq (recipe .residual , recipe .values , maxfev = 50 )
177
+ leastsq (recipe .residual , recipe .values , maxfev = 50 )
176
178
177
179
# Now the scale and phase fraction.
178
180
recipe .free ("scale" , "scale_CdS" )
179
- leastsq (recipe .residual , recipe .values , maxfev = 50 )
181
+ leastsq (recipe .residual , recipe .values , maxfev = 50 )
180
182
181
183
# The ADPs.
182
184
recipe .free ("adp" )
183
- leastsq (recipe .residual , recipe .values , maxfev = 100 )
185
+ leastsq (recipe .residual , recipe .values , maxfev = 100 )
184
186
185
187
# The delta2 parameters.
186
188
recipe .free ("delta2_cds" , "delta2_zns" )
187
- leastsq (recipe .residual , recipe .values , maxfev = 50 )
189
+ leastsq (recipe .residual , recipe .values , maxfev = 50 )
188
190
189
191
# The shape parameters.
190
192
recipe .free ("radius" , "thickness" )
191
- leastsq (recipe .residual , recipe .values , maxfev = 50 )
193
+ leastsq (recipe .residual , recipe .values , maxfev = 50 )
192
194
193
195
# The positional parameters.
194
196
recipe .free ("xyz" )
@@ -202,6 +204,7 @@ def main():
202
204
plotResults (recipe )
203
205
return
204
206
207
+
205
208
if __name__ == "__main__" :
206
209
main ()
207
210
0 commit comments