22
22
"""
23
23
24
24
import numpy
25
- from scipy .optimize import leastsq
26
-
27
25
from pyobjcryst import loadCrystal
26
+ from scipy .optimize import leastsq
28
27
28
+ from diffpy .srfit .fitbase import FitContribution , FitRecipe , FitResults , Profile
29
29
from diffpy .srfit .pdf import PDFGenerator , PDFParser
30
- from diffpy .srfit .fitbase import Profile
31
- from diffpy .srfit .fitbase import FitContribution , FitRecipe
32
- from diffpy .srfit .fitbase import FitResults
33
30
34
31
####### Example Code
35
32
33
+
36
34
def makeRecipe (stru1 , stru2 , datname ):
37
35
"""Create a fitting recipe for crystalline PDF data."""
38
36
@@ -43,7 +41,7 @@ def makeRecipe(stru1, stru2, datname):
43
41
parser = PDFParser ()
44
42
parser .parseFile (datname )
45
43
profile .loadParsedData (parser )
46
- profile .setCalculationRange (xmin = 1.5 , xmax = 45 , dx = 0.1 )
44
+ profile .setCalculationRange (xmin = 1.5 , xmax = 45 , dx = 0.1 )
47
45
48
46
## The ProfileGenerator
49
47
# In order to fit the core and shell phases simultaneously, we must use two
@@ -66,15 +64,16 @@ def makeRecipe(stru1, stru2, datname):
66
64
contribution = FitContribution ("cdszns" )
67
65
contribution .addProfileGenerator (generator_cds )
68
66
contribution .addProfileGenerator (generator_zns )
69
- contribution .setProfile (profile , xname = "r" )
67
+ contribution .setProfile (profile , xname = "r" )
70
68
71
69
# Set up the characteristic functions. We use a spherical CF for the core
72
70
# and a spherical shell CF for the shell. Since this is set up as two
73
71
# phases, we implicitly assume that the core-shell correlations contribute
74
72
# very little to the PDF.
75
- from diffpy .srfit .pdf .characteristicfunctions import sphericalCF , shellCF
76
- contribution .registerFunction (sphericalCF , name = "f_CdS" )
77
- contribution .registerFunction (shellCF , name = "f_ZnS" )
73
+ from diffpy .srfit .pdf .characteristicfunctions import shellCF , sphericalCF
74
+
75
+ contribution .registerFunction (sphericalCF , name = "f_CdS" )
76
+ contribution .registerFunction (shellCF , name = "f_ZnS" )
78
77
79
78
# Write the fitting equation. We want to sum the PDFs from each phase and
80
79
# multiply it by a scaling factor.
@@ -106,27 +105,28 @@ def makeRecipe(stru1, stru2, datname):
106
105
# subsequent refinement.
107
106
phase_cds = generator_cds .phase
108
107
for par in phase_cds .sgpars .latpars :
109
- recipe .addVar (par , name = par .name + "_cds" , tag = "lat" )
108
+ recipe .addVar (par , name = par .name + "_cds" , tag = "lat" )
110
109
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" )
110
+ recipe .addVar (par , 1 , name = par .name + "_cds" , tag = "adp" )
111
+ recipe .addVar (phase_cds .sgpars .xyzpars .z_1 , name = "z_1_cds" , tag = "xyz" )
113
112
# Since we know these have stacking disorder, constrain the B33 adps for
114
113
# each atom type.
115
114
recipe .constrain ("B33_1_cds" , "B33_0_cds" )
116
- recipe .addVar (generator_cds .delta2 , name = "delta2_cds" , value = 5 )
115
+ recipe .addVar (generator_cds .delta2 , name = "delta2_cds" , value = 5 )
117
116
118
117
phase_zns = generator_zns .phase
119
118
for par in phase_zns .sgpars .latpars :
120
- recipe .addVar (par , name = par .name + "_zns" , tag = "lat" )
119
+ recipe .addVar (par , name = par .name + "_zns" , tag = "lat" )
121
120
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" )
121
+ recipe .addVar (par , 1 , name = par .name + "_zns" , tag = "adp" )
122
+ recipe .addVar (phase_zns .sgpars .xyzpars .z_1 , name = "z_1_zns" , tag = "xyz" )
124
123
recipe .constrain ("B33_1_zns" , "B33_0_zns" )
125
- recipe .addVar (generator_zns .delta2 , name = "delta2_zns" , value = 2.5 )
124
+ recipe .addVar (generator_zns .delta2 , name = "delta2_zns" , value = 2.5 )
126
125
127
126
# Give the recipe away so it can be used!
128
127
return recipe
129
128
129
+
130
130
def plotResults (recipe ):
131
131
"""Plot the results contained within a refined FitRecipe."""
132
132
@@ -138,10 +138,11 @@ def plotResults(recipe):
138
138
diff = g - gcalc + diffzero
139
139
140
140
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-' )
141
+
142
+ pylab .plot (r , g , "bo" , label = "G(r) Data" )
143
+ pylab .plot (r , gcalc , "r-" , label = "G(r) Fit" )
144
+ pylab .plot (r , diff , "g-" , label = "G(r) diff" )
145
+ pylab .plot (r , diffzero , "k-" )
145
146
pylab .xlabel (r"$r (\AA)$" )
146
147
pylab .ylabel (r"$G (\AA^{-2})$" )
147
148
pylab .legend (loc = 1 )
@@ -163,6 +164,7 @@ def main():
163
164
stru2 = loadCrystal (znsciffile )
164
165
recipe = makeRecipe (stru1 , stru2 , data )
165
166
from diffpy .srfit .fitbase .fithook import PlotFitHook
167
+
166
168
recipe .pushFitHook (PlotFitHook ())
167
169
recipe .fithooks [0 ].verbose = 3
168
170
@@ -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