Exemple #1
0
def struToParameterSet(name, stru):
    """Creates a ParameterSet from an structure.

    This returns a ParameterSet adapted for the structure depending on its
    type.

    stru    --  a structure object known by this module
    name    --  A name to give the structure.

    Raises TypeError if stru cannot be adapted

    """
    from diffpy.srfit.structure.diffpyparset import DiffpyStructureParSet
    if DiffpyStructureParSet.canAdapt(stru):
        return DiffpyStructureParSet(name, stru)

    from diffpy.srfit.structure.objcrystparset import ObjCrystCrystalParSet
    if ObjCrystCrystalParSet.canAdapt(stru):
        return ObjCrystCrystalParSet(name, stru)

    from diffpy.srfit.structure.objcrystparset import ObjCrystMoleculeParSet
    if ObjCrystMoleculeParSet.canAdapt(stru):
        return ObjCrystMoleculeParSet(name, stru)

    from diffpy.srfit.structure.cctbxparset import CCTBXCrystalParSet
    if CCTBXCrystalParSet.canAdapt(stru):
        return CCTBXCrystalParSet(name, stru)

    raise TypeError("Unadaptable structure format")
    def test_DiffPy_constrainAsSpaceGroup(self):
        """Test the constrainAsSpaceGroup function."""
        from diffpy.srfit.structure.diffpyparset import DiffpyStructureParSet
        from diffpy.srfit.structure.sgconstraints import constrainAsSpaceGroup

        stru = makeLaMnO3_P1()
        parset = DiffpyStructureParSet("LaMnO3", stru)

        sgpars = constrainAsSpaceGroup(parset, "P b n m",
                scatterers = parset.getScatterers()[::2],
                constrainadps = True)

        # Make sure that the new parameters were created
        for par in sgpars:
            self.assertNotEqual(None, par)
            self.assertNotEqual(None, par.getValue() )

        # Test the unconstrained atoms
        for scatterer in parset.getScatterers()[1::2]:
            self.assertFalse(scatterer.x.const)
            self.assertFalse(scatterer.y.const)
            self.assertFalse(scatterer.z.const)
            self.assertFalse(scatterer.U11.const)
            self.assertFalse(scatterer.U22.const)
            self.assertFalse(scatterer.U33.const)
            self.assertFalse(scatterer.U12.const)
            self.assertFalse(scatterer.U13.const)
            self.assertFalse(scatterer.U23.const)
            self.assertEqual(0, len(scatterer._constraints))

        proxied = [p.par for p in sgpars]

        def _consttest(par):
            return par.const
        def _constrainedtest(par):
            return par.constrained
        def _proxytest(par):
            return par in proxied
        def _alltests(par):
            return _consttest(par) or _constrainedtest(par) or _proxytest(par)

        for idx, scatterer in enumerate(parset.getScatterers()[::2]):
            # Under this scheme, atom 6 is free to vary
            test = False
            for par in [scatterer.x, scatterer.y, scatterer.z]:
                test |= _alltests(par)
            self.assertTrue(test)

            test = False
            for par in [scatterer.U11, scatterer.U22, scatterer.U33,
                    scatterer.U12, scatterer.U13, scatterer.U23]:
                test |= _alltests(par)

            self.assertTrue(test)

        return
Exemple #3
0
    def testConstrainAsSpaceGroup(self):
        """Test the constrainAsSpaceGroup function."""
        from diffpy.srfit.structure.sgconstraints import constrainAsSpaceGroup

        stru = makeLaMnO3_P1()
        parset = DiffpyStructureParSet("LaMnO3", stru)

        sgpars = constrainAsSpaceGroup(parset, "P b n m",
                scatterers = parset.getScatterers()[::2],
                constrainadps = True)

        # Make sure that the new parameters were created
        for par in sgpars:
            self.assertNotEquals(None, par)
            self.assertNotEquals(None, par.getValue() )

        # Test the unconstrained atoms
        for scatterer in parset.getScatterers()[1::2]:
            self.assertFalse(scatterer.x.const)
            self.assertFalse(scatterer.y.const)
            self.assertFalse(scatterer.z.const)
            self.assertFalse(scatterer.U11.const)
            self.assertFalse(scatterer.U22.const)
            self.assertFalse(scatterer.U33.const)
            self.assertFalse(scatterer.U12.const)
            self.assertFalse(scatterer.U13.const)
            self.assertFalse(scatterer.U23.const)
            self.assertEquals(0, len(scatterer._constraints))

        proxied = [p.par for p in sgpars]

        def _consttest(par):
            return par.const
        def _constrainedtest(par):
            return par.constrained
        def _proxytest(par):
            return par in proxied
        def _alltests(par):
            return _consttest(par) or _constrainedtest(par) or _proxytest(par)

        for idx, scatterer in enumerate(parset.getScatterers()[::2]):
            # Under this scheme, atom 6 is free to vary
            test = False
            for par in [scatterer.x, scatterer.y, scatterer.z]:
                test |= _alltests(par)
            self.assertTrue(test)

            test = False
            for par in [scatterer.U11, scatterer.U22, scatterer.U33,
                    scatterer.U12, scatterer.U13, scatterer.U23]:
                test |= _alltests(par)

            self.assertTrue(test)

        return
 def test_ConstrainAsSpaceGroup_args(self):
     """Test the arguments processing of constrainAsSpaceGroup function.
     """
     from diffpy.srfit.structure.sgconstraints import constrainAsSpaceGroup
     from diffpy.Structure.SpaceGroups import GetSpaceGroup
     stru = makeLaMnO3_P1()
     parset = DiffpyStructureParSet("LaMnO3", stru)
     sgpars = constrainAsSpaceGroup(parset, "P b n m")
     sg = GetSpaceGroup('P b n m')
     parset2 = DiffpyStructureParSet("LMO", makeLaMnO3_P1())
     sgpars2 = constrainAsSpaceGroup(parset2, sg)
     list(sgpars)
     list(sgpars2)
     self.assertEqual(sgpars.names, sgpars2.names)
     return
Exemple #5
0
def struToParameterSet(name, stru):
    """Creates a ParameterSet from an structure.

    This returns a ParameterSet adapted for the structure depending on its
    type.

    stru    --  a structure object known by this module
    name    --  A name to give the structure.

    Raises TypeError if stru cannot be adapted

    """
    from diffpy.srfit.structure.diffpyparset import DiffpyStructureParSet
    if DiffpyStructureParSet.canAdapt(stru):
        return DiffpyStructureParSet(name, stru)

    from diffpy.srfit.structure.objcrystparset import ObjCrystCrystalParSet
    if ObjCrystCrystalParSet.canAdapt(stru):
        return ObjCrystCrystalParSet(name, stru)

    from diffpy.srfit.structure.objcrystparset import ObjCrystMoleculeParSet
    if ObjCrystMoleculeParSet.canAdapt(stru):
        return ObjCrystMoleculeParSet(name, stru)

    from diffpy.srfit.structure.cctbxparset import CCTBXCrystalParSet
    if CCTBXCrystalParSet.canAdapt(stru):
        return CCTBXCrystalParSet(name, stru)

    raise TypeError("Unadaptable structure format")
Exemple #6
0
 def test_pickling(self):
     """Test pickling of DiffpyStructureParSet.
     """
     stru = Structure([Atom("C", [0, 0.2, 0.5])])
     dsps = DiffpyStructureParSet("dsps", stru)
     data = pickle.dumps(dsps)
     dsps2 = pickle.loads(data)
     self.assertEqual(1, len(dsps2.atoms))
     self.assertEqual(0.2, dsps2.atoms[0].y.value)
     return
Exemple #7
0
 def test___repr__(self):
     """Test representation of DiffpyStructureParSet objects.
     """
     lat = Lattice(3, 3, 2, 90, 90, 90)
     atom = Atom("C", [0, 0.2, 0.5])
     stru = Structure([atom], lattice=lat)
     dsps = DiffpyStructureParSet("dsps", stru)
     self.assertEqual(repr(stru), repr(dsps))
     self.assertEqual(repr(lat), repr(dsps.lattice))
     self.assertEqual(repr(atom), repr(dsps.atoms[0]))
     return
Exemple #8
0
    def setStructure(self, strufile):
        """Set the structure used in the calculation.

        strufile    --  The name of a structure file. A
                        diffpy.Structure.Structure object will be created from
                        the file, and that object will be passed to the 'iofq'
                        function whenever it is called.

        This will create the refinement Parameters using the
        DiffpyStructureParSet adapter from diffpy.srfit.structure.diffpyparset.
        DiffpyStructureParSet is a ParameterSet object that organizes and gives
        attribute access to Parameters and ParameterSets adapted from a diffpy
        Structure object.  The Parameters embedded in the DiffpyStructureParSet
        are proxies for attributes of the diffpy.Structure.Structure object
        that is needed by the 'iofq' function. The Parameters will be
        accessible by name under the 'phase' attribute of this generator, and
        are organized hierarchically:

        phase
          - lattice (retrieved with 'getLattice')
            - a
            - b
            - c
            - alpha
            - beta
            - gamma
          - scatterers (retrieved with 'getScatterers')
            - atom1 (the name depends on the element)
              - x
              - y
              - z
              - occ
              - U11
              - U22
              - U33
              - U12
              - U13
              - U23
              - Uiso
            - etc.

        The diffpy.Structure.Structure instance is held within the
        DiffpyStructureParSet as the 'stru' attribute.

        """
        # Load the structure from file
        from diffpy.Structure import Structure
        stru = Structure()
        stru.read(strufile)

        # Create a ParameterSet designed to interface with
        # diffpy.Structure.Structure objects that organizes the Parameter
        # hierarchy. Note that the DiffpyStructureParSet holds a handle to the
        # loaded structure that we use in the __call__ method below.
        #
        # We pass the diffpy.Structure.Structure instance, and give the
        # DiffpyStructureParSet the name "phase".
        parset = DiffpyStructureParSet("phase", stru)

        # Put this ParameterSet in the ProfileGenerator.
        self.addParameterSet(parset)

        return
Exemple #9
0
    def testDiffpyStructureParSet(self):
        """Test the structure conversion."""

        a1 = Atom("Cu", xyz = numpy.array([.0, .1, .2]), Uisoequiv = 0.003)
        a2 = Atom("Ag", xyz = numpy.array([.3, .4, .5]), Uisoequiv = 0.002)
        l = Lattice(2.5, 2.5, 2.5, 90, 90, 90)

        dsstru = Structure([a1,a2], l)
        # Structure makes copies
        a1 = dsstru[0]
        a2 = dsstru[1]

        s = DiffpyStructureParSet("CuAg", dsstru)

        self.assertEquals(s.name, "CuAg")

        def _testAtoms():
            # Check the atoms thoroughly
            self.assertEquals(a1.element, s.Cu0.element)
            self.assertEquals(a2.element, s.Ag0.element)
            self.assertEquals(a1.Uisoequiv, s.Cu0.Uiso.getValue())
            self.assertEquals(a2.Uisoequiv, s.Ag0.Uiso.getValue())
            self.assertEquals(a1.Bisoequiv, s.Cu0.Biso.getValue())
            self.assertEquals(a2.Bisoequiv, s.Ag0.Biso.getValue())
            for i in xrange(1,4):
                for j in xrange(i,4):
                    uijstru = getattr(a1, "U%i%i"%(i,j))
                    uij = getattr(s.Cu0, "U%i%i"%(i,j)).getValue()
                    uji = getattr(s.Cu0, "U%i%i"%(j,i)).getValue()
                    self.assertEquals(uijstru, uij)
                    self.assertEquals(uijstru, uji)
                    bijstru = getattr(a1, "B%i%i"%(i,j))
                    bij = getattr(s.Cu0, "B%i%i"%(i,j)).getValue()
                    bji = getattr(s.Cu0, "B%i%i"%(j,i)).getValue()
                    self.assertEquals(bijstru, bij)
                    self.assertEquals(bijstru, bji)

            self.assertEquals(a1.xyz[0], s.Cu0.x.getValue())
            self.assertEquals(a1.xyz[1], s.Cu0.y.getValue())
            self.assertEquals(a1.xyz[2], s.Cu0.z.getValue())
            return

        def _testLattice():

            # Test the lattice
            self.assertEquals(dsstru.lattice.a, s.lattice.a.getValue())
            self.assertEquals(dsstru.lattice.b, s.lattice.b.getValue())
            self.assertEquals(dsstru.lattice.c, s.lattice.c.getValue())
            self.assertEquals(dsstru.lattice.alpha, s.lattice.alpha.getValue())
            self.assertEquals(dsstru.lattice.beta, s.lattice.beta.getValue())
            self.assertEquals(dsstru.lattice.gamma, s.lattice.gamma.getValue())

        _testAtoms()
        _testLattice()

        # Now change some values from the diffpy Structure
        a1.xyz[1] = 0.123
        a1.U11 = 0.321
        a1.B32 = 0.111
        dsstru.lattice.setLatPar(a=3.0, gamma=121)
        _testAtoms()
        _testLattice()

        # Now change values from the srfit DiffpyStructureParSet
        s.Cu0.x.setValue(0.456)
        s.Cu0.U22.setValue(0.441)
        s.Cu0.B13.setValue(0.550)
        d = dsstru.lattice.dist(a1.xyz, a2.xyz)
        s.lattice.b.setValue(4.6)
        s.lattice.alpha.setValue(91.3)
        _testAtoms()
        _testLattice()
        # Make sure the distance changed
        self.assertNotEquals(d, dsstru.lattice.dist(a1.xyz, a2.xyz))
        return