3131from diffpy .srfit .fitbase import FitContribution , FitRecipe
3232from diffpy .srfit .fitbase import FitResults
3333
34- ####### Example Code
34+ # Example Code
35+
3536
3637def makeRecipe (stru1 , stru2 , datname ):
3738 """Create a fitting recipe for crystalline PDF data."""
3839
39- ## The Profile
40+ # The Profile
4041 profile = Profile ()
4142
4243 # Load data and add it to the profile
4344 parser = PDFParser ()
4445 parser .parseFile (datname )
4546 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 )
4748
48- ## The ProfileGenerator
49+ # The ProfileGenerator
4950 # In order to fit the core and shell phases simultaneously, we must use two
5051 # PDFGenerators.
5152 #
@@ -61,20 +62,20 @@ def makeRecipe(stru1, stru2, datname):
6162 generator_zns .setQmax (26 )
6263 generator_zns .qdamp .value = 0.0396
6364
64- ## The FitContribution
65+ # The FitContribution
6566 # Add both generators and the profile to the FitContribution.
6667 contribution = FitContribution ("cdszns" )
6768 contribution .addProfileGenerator (generator_cds )
6869 contribution .addProfileGenerator (generator_zns )
69- contribution .setProfile (profile , xname = "r" )
70+ contribution .setProfile (profile , xname = "r" )
7071
7172 # Set up the characteristic functions. We use a spherical CF for the core
7273 # and a spherical shell CF for the shell. Since this is set up as two
7374 # phases, we implicitly assume that the core-shell correlations contribute
7475 # very little to the PDF.
7576 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" )
7879
7980 # Write the fitting equation. We want to sum the PDFs from each phase and
8081 # multiply it by a scaling factor.
@@ -90,7 +91,7 @@ def makeRecipe(stru1, stru2, datname):
9091 recipe .addVar (contribution .thickness , 11 )
9192 recipe .constrain (contribution .psize , "2 * radius" )
9293
93- ## Configure the fit variables
94+ # Configure the fit variables
9495 # Start by configuring the scale factor and resolution factors.
9596 # We want the sum of the phase scale factors to be 1.
9697 recipe .newVar ("scale_CdS" , 0.7 )
@@ -106,27 +107,28 @@ def makeRecipe(stru1, stru2, datname):
106107 # subsequent refinement.
107108 phase_cds = generator_cds .phase
108109 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" )
110111 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" )
113114 # Since we know these have stacking disorder, constrain the B33 adps for
114115 # each atom type.
115116 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 )
117118
118119 phase_zns = generator_zns .phase
119120 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" )
121122 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" )
124125 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 )
126127
127128 # Give the recipe away so it can be used!
128129 return recipe
129130
131+
130132def plotResults (recipe ):
131133 """Plot the results contained within a refined FitRecipe."""
132134
@@ -138,10 +140,10 @@ def plotResults(recipe):
138140 diff = g - gcalc + diffzero
139141
140142 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-' )
145147 pylab .xlabel (r"$r (\AA)$" )
146148 pylab .ylabel (r"$G (\AA^{-2})$" )
147149 pylab .legend (loc = 1 )
@@ -172,23 +174,23 @@ def main():
172174 # Start with the lattice parameters. In makeRecipe, these were tagged with
173175 # "lat". Here is how we use that.
174176 recipe .free ("lat" )
175- leastsq (recipe .residual , recipe .values , maxfev = 50 )
177+ leastsq (recipe .residual , recipe .values , maxfev = 50 )
176178
177179 # Now the scale and phase fraction.
178180 recipe .free ("scale" , "scale_CdS" )
179- leastsq (recipe .residual , recipe .values , maxfev = 50 )
181+ leastsq (recipe .residual , recipe .values , maxfev = 50 )
180182
181183 # The ADPs.
182184 recipe .free ("adp" )
183- leastsq (recipe .residual , recipe .values , maxfev = 100 )
185+ leastsq (recipe .residual , recipe .values , maxfev = 100 )
184186
185187 # The delta2 parameters.
186188 recipe .free ("delta2_cds" , "delta2_zns" )
187- leastsq (recipe .residual , recipe .values , maxfev = 50 )
189+ leastsq (recipe .residual , recipe .values , maxfev = 50 )
188190
189191 # The shape parameters.
190192 recipe .free ("radius" , "thickness" )
191- leastsq (recipe .residual , recipe .values , maxfev = 50 )
193+ leastsq (recipe .residual , recipe .values , maxfev = 50 )
192194
193195 # The positional parameters.
194196 recipe .free ("xyz" )
@@ -202,6 +204,7 @@ def main():
202204 plotResults (recipe )
203205 return
204206
207+
205208if __name__ == "__main__" :
206209 main ()
207210
0 commit comments