28
28
29
29
####### Example Code
30
30
31
+
31
32
def makeProfile (datafile ):
32
33
"""Make an place data within a Profile."""
33
34
profile = Profile ()
34
35
parser = PDFParser ()
35
36
parser .parseFile (datafile )
36
37
profile .loadParsedData (parser )
37
- profile .setCalculationRange (xmax = 20 )
38
+ profile .setCalculationRange (xmax = 20 )
38
39
return profile
39
40
41
+
40
42
def makeContribution (name , generator , profile ):
41
43
"""Make a FitContribution and add a generator and profile."""
42
44
contribution = FitContribution (name )
43
45
contribution .addProfileGenerator (generator )
44
- contribution .setProfile (profile , xname = "r" )
46
+ contribution .setProfile (profile , xname = "r" )
45
47
return contribution
46
48
47
- def makeRecipe ( ciffile_ni , ciffile_si , xdata_ni , ndata_ni , xdata_si ,
48
- xdata_sini ):
49
+
50
+ def makeRecipe ( ciffile_ni , ciffile_si , xdata_ni , ndata_ni , xdata_si , xdata_sini ):
49
51
"""Create a fitting recipe for crystalline PDF data."""
50
52
51
53
## The Profiles
@@ -81,8 +83,7 @@ def makeRecipe(ciffile_ni, ciffile_si, xdata_ni, ndata_ni, xdata_si,
81
83
xcontribution_ni = makeContribution ("xnickel" , xgenerator_ni , xprofile_ni )
82
84
xcontribution_si = makeContribution ("xsilicon" , xgenerator_si , xprofile_si )
83
85
ncontribution_ni = makeContribution ("nnickel" , ngenerator_ni , nprofile_ni )
84
- xcontribution_sini = makeContribution ("xsini" , xgenerator_sini_ni ,
85
- xprofile_sini )
86
+ xcontribution_sini = makeContribution ("xsini" , xgenerator_sini_ni , xprofile_sini )
86
87
xcontribution_sini .addProfileGenerator (xgenerator_sini_si )
87
88
xcontribution_sini .setEquation ("scale * (xG_sini_ni + xG_sini_si)" )
88
89
@@ -101,22 +102,22 @@ def makeRecipe(ciffile_ni, ciffile_si, xdata_ni, ndata_ni, xdata_si,
101
102
102
103
# Now we vary and constrain Parameters as before.
103
104
for par in phase_ni .sgpars :
104
- recipe .addVar (par , name = par .name + "_ni" )
105
+ recipe .addVar (par , name = par .name + "_ni" )
105
106
delta2_ni = recipe .newVar ("delta2_ni" , 2.5 )
106
107
recipe .constrain (xgenerator_ni .delta2 , delta2_ni )
107
108
recipe .constrain (ngenerator_ni .delta2 , delta2_ni )
108
109
recipe .constrain (xgenerator_sini_ni .delta2 , delta2_ni )
109
110
110
111
for par in phase_si .sgpars :
111
- recipe .addVar (par , name = par .name + "_si" )
112
+ recipe .addVar (par , name = par .name + "_si" )
112
113
delta2_si = recipe .newVar ("delta2_si" , 2.5 )
113
114
recipe .constrain (xgenerator_si .delta2 , delta2_si )
114
115
recipe .constrain (xgenerator_sini_si .delta2 , delta2_si )
115
116
116
117
# Now the experimental parameters
117
- recipe .addVar (xgenerator_ni .scale , name = "xscale_ni" )
118
- recipe .addVar (xgenerator_si .scale , name = "xscale_si" )
119
- recipe .addVar (ngenerator_ni .scale , name = "nscale_ni" )
118
+ recipe .addVar (xgenerator_ni .scale , name = "xscale_ni" )
119
+ recipe .addVar (xgenerator_si .scale , name = "xscale_si" )
120
+ recipe .addVar (ngenerator_ni .scale , name = "nscale_ni" )
120
121
recipe .addVar (xcontribution_sini .scale , 1.0 , "xscale_sini" )
121
122
recipe .newVar ("pscale_sini_ni" , 0.8 )
122
123
recipe .constrain (xgenerator_sini_ni .scale , "pscale_sini_ni" )
@@ -133,6 +134,7 @@ def makeRecipe(ciffile_ni, ciffile_si, xdata_ni, ndata_ni, xdata_si,
133
134
# Give the recipe away so it can be used!
134
135
return recipe
135
136
137
+
136
138
def plotResults (recipe ):
137
139
"""Plot the results contained within a refined FitRecipe."""
138
140
@@ -141,67 +143,67 @@ def plotResults(recipe):
141
143
xr_ni = xnickel .profile .x
142
144
xg_ni = xnickel .profile .y
143
145
xgcalc_ni = xnickel .profile .ycalc
144
- xdiffzero_ni = - 0.8 * max (xg_ni ) * numpy .ones_like (xg_ni )
146
+ xdiffzero_ni = - 0.8 * max (xg_ni ) * numpy .ones_like (xg_ni )
145
147
xdiff_ni = xg_ni - xgcalc_ni + xdiffzero_ni
146
148
147
149
xsilicon = recipe .xsilicon
148
150
xr_si = xsilicon .profile .x
149
151
xg_si = xsilicon .profile .y
150
152
xgcalc_si = xsilicon .profile .ycalc
151
- xdiffzero_si = - 0.8 * max (xg_si ) * numpy .ones_like (xg_si )
153
+ xdiffzero_si = - 0.8 * max (xg_si ) * numpy .ones_like (xg_si )
152
154
xdiff_si = xg_si - xgcalc_si + xdiffzero_si
153
155
154
156
nnickel = recipe .nnickel
155
157
nr_ni = nnickel .profile .x
156
158
ng_ni = nnickel .profile .y
157
159
ngcalc_ni = nnickel .profile .ycalc
158
- ndiffzero_ni = - 0.8 * max (ng_ni ) * numpy .ones_like (ng_ni )
160
+ ndiffzero_ni = - 0.8 * max (ng_ni ) * numpy .ones_like (ng_ni )
159
161
ndiff_ni = ng_ni - ngcalc_ni + ndiffzero_ni
160
162
161
163
xsini = recipe .xsini
162
164
xr_sini = xsini .profile .x
163
165
xg_sini = xsini .profile .y
164
166
xgcalc_sini = xsini .profile .ycalc
165
- xdiffzero_sini = - 0.8 * max (xg_sini ) * numpy .ones_like (xg_sini )
167
+ xdiffzero_sini = - 0.8 * max (xg_sini ) * numpy .ones_like (xg_sini )
166
168
xdiff_sini = xg_sini - xgcalc_sini + xdiffzero_sini
167
169
168
-
169
170
import pylab
171
+
170
172
pylab .subplot (2 , 2 , 1 )
171
- pylab .plot (xr_ni ,xg_ni ,'bo' , label = "G(r) x-ray nickel Data" )
172
- pylab .plot (xr_ni ,xgcalc_ni ,'r-' , label = "G(r) x-ray nickel Fit" )
173
- pylab .plot (xr_ni ,xdiff_ni ,'g-' , label = "G(r) x-ray nickel diff" )
174
- pylab .plot (xr_ni ,xdiffzero_ni ,'k-' )
173
+ pylab .plot (xr_ni , xg_ni , "bo" , label = "G(r) x-ray nickel Data" )
174
+ pylab .plot (xr_ni , xgcalc_ni , "r-" , label = "G(r) x-ray nickel Fit" )
175
+ pylab .plot (xr_ni , xdiff_ni , "g-" , label = "G(r) x-ray nickel diff" )
176
+ pylab .plot (xr_ni , xdiffzero_ni , "k-" )
175
177
pylab .xlabel (r"$r (\AA)$" )
176
178
pylab .ylabel (r"$G (\AA^{-2})$" )
177
179
pylab .legend (loc = 1 )
178
180
179
181
pylab .subplot (2 , 2 , 2 )
180
- pylab .plot (xr_si ,xg_si ,'bo' , label = "G(r) x-ray silicon Data" )
181
- pylab .plot (xr_si ,xgcalc_si ,'r-' , label = "G(r) x-ray silicon Fit" )
182
- pylab .plot (xr_si ,xdiff_si ,'g-' , label = "G(r) x-ray silicon diff" )
183
- pylab .plot (xr_si ,xdiffzero_si ,'k-' )
182
+ pylab .plot (xr_si , xg_si , "bo" , label = "G(r) x-ray silicon Data" )
183
+ pylab .plot (xr_si , xgcalc_si , "r-" , label = "G(r) x-ray silicon Fit" )
184
+ pylab .plot (xr_si , xdiff_si , "g-" , label = "G(r) x-ray silicon diff" )
185
+ pylab .plot (xr_si , xdiffzero_si , "k-" )
184
186
pylab .legend (loc = 1 )
185
187
186
188
pylab .subplot (2 , 2 , 3 )
187
- pylab .plot (nr_ni ,ng_ni ,'bo' , label = "G(r) neutron nickel Data" )
188
- pylab .plot (nr_ni ,ngcalc_ni ,'r-' , label = "G(r) neutron nickel Fit" )
189
- pylab .plot (nr_ni ,ndiff_ni ,'g-' , label = "G(r) neutron nickel diff" )
190
- pylab .plot (nr_ni ,ndiffzero_ni ,'k-' )
189
+ pylab .plot (nr_ni , ng_ni , "bo" , label = "G(r) neutron nickel Data" )
190
+ pylab .plot (nr_ni , ngcalc_ni , "r-" , label = "G(r) neutron nickel Fit" )
191
+ pylab .plot (nr_ni , ndiff_ni , "g-" , label = "G(r) neutron nickel diff" )
192
+ pylab .plot (nr_ni , ndiffzero_ni , "k-" )
191
193
pylab .legend (loc = 1 )
192
194
193
195
pylab .subplot (2 , 2 , 4 )
194
- pylab .plot (xr_sini ,xg_sini ,'bo' , label = "G(r) x-ray sini Data" )
195
- pylab .plot (xr_sini ,xgcalc_sini ,'r-' , label = "G(r) x-ray sini Fit" )
196
- pylab .plot (xr_sini ,xdiff_sini ,'g-' , label = "G(r) x-ray sini diff" )
197
- pylab .plot (xr_sini ,xdiffzero_sini ,'k-' )
196
+ pylab .plot (xr_sini , xg_sini , "bo" , label = "G(r) x-ray sini Data" )
197
+ pylab .plot (xr_sini , xgcalc_sini , "r-" , label = "G(r) x-ray sini Fit" )
198
+ pylab .plot (xr_sini , xdiff_sini , "g-" , label = "G(r) x-ray sini diff" )
199
+ pylab .plot (xr_sini , xdiffzero_sini , "k-" )
198
200
pylab .legend (loc = 1 )
199
201
200
202
pylab .show ()
201
203
return
202
204
203
- if __name__ == "__main__" :
204
205
206
+ if __name__ == "__main__" :
205
207
# Make the data and the recipe
206
208
ciffile_ni = "data/ni.cif"
207
209
ciffile_si = "data/si.cif"
@@ -211,8 +213,7 @@ def plotResults(recipe):
211
213
xdata_sini = "data/si90ni10-q27r60-xray.gr"
212
214
213
215
# Make the recipe
214
- recipe = makeRecipe (ciffile_ni , ciffile_si , xdata_ni , ndata_ni , xdata_si ,
215
- xdata_sini )
216
+ recipe = makeRecipe (ciffile_ni , ciffile_si , xdata_ni , ndata_ni , xdata_si , xdata_sini )
216
217
217
218
# Optimize
218
219
scipyOptimize (recipe )
0 commit comments