Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 29 additions & 26 deletions doc/examples/coreshellnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@
from diffpy.srfit.fitbase import FitContribution, FitRecipe
from diffpy.srfit.fitbase import FitResults

####### Example Code
# Example Code


def makeRecipe(stru1, stru2, datname):
"""Create a fitting recipe for crystalline PDF data."""

## The Profile
# The Profile
profile = Profile()

# Load data and add it to the profile
parser = PDFParser()
parser.parseFile(datname)
profile.loadParsedData(parser)
profile.setCalculationRange(xmin=1.5, xmax = 45, dx = 0.1)
profile.setCalculationRange(xmin=1.5, xmax=45, dx=0.1)

## The ProfileGenerator
# The ProfileGenerator
# In order to fit the core and shell phases simultaneously, we must use two
# PDFGenerators.
#
Expand All @@ -61,20 +62,20 @@ def makeRecipe(stru1, stru2, datname):
generator_zns.setQmax(26)
generator_zns.qdamp.value = 0.0396

## The FitContribution
# The FitContribution
# Add both generators and the profile to the FitContribution.
contribution = FitContribution("cdszns")
contribution.addProfileGenerator(generator_cds)
contribution.addProfileGenerator(generator_zns)
contribution.setProfile(profile, xname = "r")
contribution.setProfile(profile, xname="r")

# Set up the characteristic functions. We use a spherical CF for the core
# and a spherical shell CF for the shell. Since this is set up as two
# phases, we implicitly assume that the core-shell correlations contribute
# very little to the PDF.
from diffpy.srfit.pdf.characteristicfunctions import sphericalCF, shellCF
contribution.registerFunction(sphericalCF, name = "f_CdS")
contribution.registerFunction(shellCF, name = "f_ZnS")
contribution.registerFunction(sphericalCF, name="f_CdS")
contribution.registerFunction(shellCF, name="f_ZnS")

# Write the fitting equation. We want to sum the PDFs from each phase and
# multiply it by a scaling factor.
Expand All @@ -90,7 +91,7 @@ def makeRecipe(stru1, stru2, datname):
recipe.addVar(contribution.thickness, 11)
recipe.constrain(contribution.psize, "2 * radius")

## Configure the fit variables
# Configure the fit variables
# Start by configuring the scale factor and resolution factors.
# We want the sum of the phase scale factors to be 1.
recipe.newVar("scale_CdS", 0.7)
Expand All @@ -106,27 +107,28 @@ def makeRecipe(stru1, stru2, datname):
# subsequent refinement.
phase_cds = generator_cds.phase
for par in phase_cds.sgpars.latpars:
recipe.addVar(par, name = par.name + "_cds", tag = "lat")
recipe.addVar(par, name=par.name + "_cds", tag="lat")
for par in phase_cds.sgpars.adppars:
recipe.addVar(par, 1, name = par.name + "_cds", tag = "adp")
recipe.addVar(phase_cds.sgpars.xyzpars.z_1, name = "z_1_cds", tag = "xyz")
recipe.addVar(par, 1, name=par.name + "_cds", tag="adp")
recipe.addVar(phase_cds.sgpars.xyzpars.z_1, name="z_1_cds", tag="xyz")
# Since we know these have stacking disorder, constrain the B33 adps for
# each atom type.
recipe.constrain("B33_1_cds", "B33_0_cds")
recipe.addVar(generator_cds.delta2, name = "delta2_cds", value = 5)
recipe.addVar(generator_cds.delta2, name="delta2_cds", value=5)

phase_zns = generator_zns.phase
for par in phase_zns.sgpars.latpars:
recipe.addVar(par, name = par.name + "_zns", tag = "lat")
recipe.addVar(par, name=par.name + "_zns", tag="lat")
for par in phase_zns.sgpars.adppars:
recipe.addVar(par, 1, name = par.name + "_zns", tag = "adp")
recipe.addVar(phase_zns.sgpars.xyzpars.z_1, name = "z_1_zns", tag = "xyz")
recipe.addVar(par, 1, name=par.name + "_zns", tag="adp")
recipe.addVar(phase_zns.sgpars.xyzpars.z_1, name="z_1_zns", tag="xyz")
recipe.constrain("B33_1_zns", "B33_0_zns")
recipe.addVar(generator_zns.delta2, name = "delta2_zns", value = 2.5)
recipe.addVar(generator_zns.delta2, name="delta2_zns", value=2.5)

# Give the recipe away so it can be used!
return recipe


def plotResults(recipe):
"""Plot the results contained within a refined FitRecipe."""

Expand All @@ -138,10 +140,10 @@ def plotResults(recipe):
diff = g - gcalc + diffzero

import pylab
pylab.plot(r,g,'bo',label="G(r) Data")
pylab.plot(r, gcalc,'r-',label="G(r) Fit")
pylab.plot(r,diff,'g-',label="G(r) diff")
pylab.plot(r,diffzero,'k-')
pylab.plot(r, g, 'bo', label="G(r) Data")
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)
Expand Down Expand Up @@ -172,23 +174,23 @@ def main():
# Start with the lattice parameters. In makeRecipe, these were tagged with
# "lat". Here is how we use that.
recipe.free("lat")
leastsq(recipe.residual, recipe.values, maxfev = 50)
leastsq(recipe.residual, recipe.values, maxfev=50)

# Now the scale and phase fraction.
recipe.free("scale", "scale_CdS")
leastsq(recipe.residual, recipe.values, maxfev = 50)
leastsq(recipe.residual, recipe.values, maxfev=50)

# The ADPs.
recipe.free("adp")
leastsq(recipe.residual, recipe.values, maxfev = 100)
leastsq(recipe.residual, recipe.values, maxfev=100)

# The delta2 parameters.
recipe.free("delta2_cds", "delta2_zns")
leastsq(recipe.residual, recipe.values, maxfev = 50)
leastsq(recipe.residual, recipe.values, maxfev=50)

# The shape parameters.
recipe.free("radius", "thickness")
leastsq(recipe.residual, recipe.values, maxfev = 50)
leastsq(recipe.residual, recipe.values, maxfev=50)

# The positional parameters.
recipe.free("xyz")
Expand All @@ -202,6 +204,7 @@ def main():
plotResults(recipe)
return


if __name__ == "__main__":
main()

Expand Down
5 changes: 1 addition & 4 deletions src/diffpy/srfit/fitbase/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from diffpy.srfit.fitbase.parameterset import ParameterSet
from diffpy.srfit.equation.literals.operators import Operator


class Calculator(Operator, ParameterSet):
"""Base class for calculators.

Expand Down Expand Up @@ -75,12 +76,10 @@ def __init__(self, name):
Operator.__init__(self, name)
return


@property
def symbol(self):
return self.name


# Overload me!
def __call__(self, *args):
"""Calculate something.
Expand All @@ -92,12 +91,10 @@ def __call__(self, *args):
"""
return 0


def operation(self, *args):
self._value = self.__call__(*args)
return self._value


def _validate(self):
"""Validate my state.

Expand Down
75 changes: 38 additions & 37 deletions src/diffpy/srfit/structure/diffpyparset.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,49 +94,49 @@ def __init__(self, name, atom):
self.atom = atom
a = atom
# x, y, z, occupancy
self.addParameter(ParameterAdapter("x", a, _xyzgetter(0),
_xyzsetter(0)))
self.addParameter(ParameterAdapter("y", a, _xyzgetter(1),
_xyzsetter(1)))
self.addParameter(ParameterAdapter("z", a, _xyzgetter(2),
_xyzsetter(2)))
occupancy = ParameterAdapter("occupancy", a, attr = "occupancy")
self.addParameter(ParameterAdapter("x", a,
_xyzgetter(0), _xyzsetter(0)))
self.addParameter(ParameterAdapter("y", a,
_xyzgetter(1), _xyzsetter(1)))
self.addParameter(ParameterAdapter("z", a,
_xyzgetter(2), _xyzsetter(2)))
occupancy = ParameterAdapter("occupancy", a, attr="occupancy")
self.addParameter(occupancy)
self.addParameter(ParameterProxy("occ", occupancy))
# U
self.addParameter(ParameterAdapter("U11", a, attr = "U11"))
self.addParameter(ParameterAdapter("U22", a, attr = "U22"))
self.addParameter(ParameterAdapter("U33", a, attr = "U33"))
U12 = ParameterAdapter("U12", a, attr = "U12")
self.addParameter(ParameterAdapter("U11", a, attr="U11"))
self.addParameter(ParameterAdapter("U22", a, attr="U22"))
self.addParameter(ParameterAdapter("U33", a, attr="U33"))
U12 = ParameterAdapter("U12", a, attr="U12")
U21 = ParameterProxy("U21", U12)
U13 = ParameterAdapter("U13", a, attr = "U13")
U13 = ParameterAdapter("U13", a, attr="U13")
U31 = ParameterProxy("U31", U13)
U23 = ParameterAdapter("U23", a, attr = "U23")
U23 = ParameterAdapter("U23", a, attr="U23")
U32 = ParameterProxy("U32", U23)
self.addParameter(U12)
self.addParameter(U21)
self.addParameter(U13)
self.addParameter(U31)
self.addParameter(U23)
self.addParameter(U32)
self.addParameter(ParameterAdapter("Uiso", a, attr = "Uisoequiv"))
self.addParameter(ParameterAdapter("Uiso", a, attr="Uisoequiv"))
# B
self.addParameter(ParameterAdapter("B11", a, attr = "B11"))
self.addParameter(ParameterAdapter("B22", a, attr = "B22"))
self.addParameter(ParameterAdapter("B33", a, attr = "B33"))
B12 = ParameterAdapter("B12", a, attr = "B12")
self.addParameter(ParameterAdapter("B11", a, attr="B11"))
self.addParameter(ParameterAdapter("B22", a, attr="B22"))
self.addParameter(ParameterAdapter("B33", a, attr="B33"))
B12 = ParameterAdapter("B12", a, attr="B12")
B21 = ParameterProxy("B21", B12)
B13 = ParameterAdapter("B13", a, attr = "B13")
B13 = ParameterAdapter("B13", a, attr="B13")
B31 = ParameterProxy("B31", B13)
B23 = ParameterAdapter("B23", a, attr = "B23")
B23 = ParameterAdapter("B23", a, attr="B23")
B32 = ParameterProxy("B32", B23)
self.addParameter(B12)
self.addParameter(B21)
self.addParameter(B13)
self.addParameter(B31)
self.addParameter(B23)
self.addParameter(B32)
self.addParameter(ParameterAdapter("Biso", a, attr = "Bisoequiv"))
self.addParameter(ParameterAdapter("Biso", a, attr="Bisoequiv"))
return

def __repr__(self):
Expand All @@ -156,6 +156,7 @@ def _setElem(self, el):
def _latgetter(par):
return bind2nd(getattr, par)


def _latsetter(par):
return bind2nd(setattr, par)

Expand Down Expand Up @@ -185,19 +186,19 @@ def __init__(self, lattice):
ParameterSet.__init__(self, "lattice")
self.angunits = "deg"
self.lattice = lattice
l = lattice
self.addParameter(ParameterAdapter("a", l, _latgetter("a"),
_latsetter("a")))
self.addParameter(ParameterAdapter("b", l, _latgetter("b"),
_latsetter("b")))
self.addParameter(ParameterAdapter("c", l, _latgetter("c"),
_latsetter("c")))
self.addParameter(ParameterAdapter("alpha", l, _latgetter("alpha"),
_latsetter("alpha")))
self.addParameter(ParameterAdapter("beta", l, _latgetter("beta"),
_latsetter("beta")))
self.addParameter(ParameterAdapter("gamma", l, _latgetter("gamma"),
_latsetter("gamma")))
lat = lattice
self.addParameter(ParameterAdapter("a", lat,
_latgetter("a"), _latsetter("a")))
self.addParameter(ParameterAdapter("b", lat,
_latgetter("b"), _latsetter("b")))
self.addParameter(ParameterAdapter("c", lat,
_latgetter("c"), _latsetter("c")))
self.addParameter(ParameterAdapter("alpha", lat, _latgetter("alpha"),
_latsetter("alpha")))
self.addParameter(ParameterAdapter("beta", lat, _latgetter("beta"),
_latsetter("beta")))
self.addParameter(ParameterAdapter("gamma", lat, _latgetter("gamma"),
_latsetter("gamma")))
return

def __repr__(self):
Expand Down Expand Up @@ -242,10 +243,10 @@ def __init__(self, name, stru):
for a in stru:
el = a.element.title()
# Try to sanitize the name.
el = el.replace("+","p")
el = el.replace("-","m")
el = el.replace("+", "p")
el = el.replace("-", "m")
i = cdict.get(el, 0)
aname = "%s%i"%(el,i)
aname = "%s%i" % (el, i)
cdict[el] = i+1
atom = DiffpyAtomParSet(aname, a)
self.addParameterSet(atom)
Expand Down
Loading