def test_createStructureAdapter(self):
     """check createStructureAdapter() routine.
     """
     adpt = createStructureAdapter(self.nickel)
     self.assertEqual(4, adpt.countSites())
     self.assertTrue(False is adpt.siteAnisotropy(0))
     self.assertTrue(isinstance(adpt, StructureAdapter))
     adpt1 = createStructureAdapter(adpt)
     self.assertTrue(adpt is adpt1)
     self.assertRaises(TypeError, createStructureAdapter, 77)
     self.assertRaises(TypeError, createStructureAdapter, range(8))
     self.assertRaises(TypeError, createStructureAdapter, None)
     self.assertRaises(TypeError, createStructureAdapter, {})
     return
Example #2
0
 def test_createStructureAdapter(self):
     """check createStructureAdapter() routine.
     """
     adpt = createStructureAdapter(self.nickel)
     self.assertEqual(4, adpt.countSites())
     self.assertTrue(False is adpt.siteAnisotropy(0))
     self.assertTrue(isinstance(adpt, StructureAdapter))
     adpt1 = createStructureAdapter(adpt)
     self.assertTrue(adpt is adpt1)
     self.assertRaises(TypeError, createStructureAdapter, 77)
     self.assertRaises(TypeError, createStructureAdapter, list(range(8)))
     self.assertRaises(TypeError, createStructureAdapter, None)
     self.assertRaises(TypeError, createStructureAdapter, {})
     return
Example #3
0
        def eval(self, stru=None):
            '''Perform parallel calculation and return internal value array.

            stru -- object that can be converted to StructureAdapter,
                    e.g., example diffpy Structure or pyobjcryst Crystal.
                    Use the last structure when None.

            Return numpy array.
            '''
            # use StructureAdapter for faster pickles
            from diffpy.srreal.structureadapter import createStructureAdapter
            if stru is None:
                struadpt = self.pqobj.getStructure()
            else:
                struadpt = createStructureAdapter(stru)
            self.pqobj.setStructure(struadpt)
            kwd = {
                'cpuindex': None,
                'ncpu': self.ncpu,
                'pqobj': copy.copy(self.pqobj),
            }
            # shallow copies of kwd dictionary each with a unique cpuindex
            arglist = [kwd.copy() for kwd['cpuindex'] in range(self.ncpu)]
            for pdata in self.pmap(_parallelData, arglist):
                self.pqobj._mergeParallelData(pdata, self.ncpu)
            return self.pqobj.value
Example #4
0
 def test_nometa(self):
     '''check NoMetaStructureAdapter.
     '''
     r0, g0 = PDFCalculator()(self.nickel)
     ni1 = self.nickel.copy()
     ni1.pdffit['scale'] = 2.0
     r1, g1 = PDFCalculator()(ni1)
     self.assertTrue(numpy.array_equal(r0, r1))
     self.assertTrue(numpy.allclose(2 * g0, g1))
     ni1nm = nometa(ni1)
     self.assertTrue(ni1nm is nometa(ni1nm))
     r1nm, g1nm = PDFCalculator()(ni1nm)
     self.assertTrue(numpy.array_equal(r0, r1nm))
     self.assertTrue(numpy.allclose(g0, g1nm))
     ni2 = self.nickel.copy()
     ni2.pdffit['delta2'] = 4
     r2, g2 = PDFCalculator()(ni2)
     r2, g2nm = PDFCalculator()(nometa(ni2))
     self.assertFalse(numpy.allclose(g0, g2))
     self.assertTrue(numpy.allclose(g0, g2nm))
     adpt2 = createStructureAdapter(ni2)
     ra2, ga2 = PDFCalculator()(adpt2)
     ra2, ga2nm = PDFCalculator()(nometa(adpt2))
     self.assertTrue(numpy.allclose(g2, ga2))
     self.assertTrue(numpy.allclose(g0, ga2nm))
     return
 def setUp(self):
     self.pwconst = PeakWidthModel.createByType('constant')
     self.pwconst.width = 2
     if self.tio2stru is None:
         self.tio2stru = loadDiffPyStructure('rutile.cif')
         self.tio2adpt = createStructureAdapter(self.tio2stru)
     return
 def test_nometa(self):
     '''check NoMetaStructureAdapter.
     '''
     r0, g0 = PDFCalculator()(self.nickel)
     ni1 = Structure(self.nickel)
     ni1.pdffit['scale'] = 2.0
     r1, g1 = PDFCalculator()(ni1)
     self.assertTrue(numpy.array_equal(r0, r1))
     self.assertTrue(numpy.allclose(2 * g0, g1))
     ni1nm = nometa(ni1)
     self.assertTrue(ni1nm is nometa(ni1nm))
     r1nm, g1nm = PDFCalculator()(ni1nm)
     self.assertTrue(numpy.array_equal(r0, r1nm))
     self.assertTrue(numpy.allclose(g0, g1nm))
     ni2 = Structure(self.nickel)
     ni2.pdffit['delta2'] = 4
     r2, g2 = PDFCalculator()(ni2)
     r2, g2nm = PDFCalculator()(nometa(ni2))
     self.assertFalse(numpy.allclose(g0, g2))
     self.assertTrue(numpy.allclose(g0, g2nm))
     adpt2 = createStructureAdapter(ni2)
     ra2, ga2 = PDFCalculator()(adpt2)
     ra2, ga2nm = PDFCalculator()(nometa(adpt2))
     self.assertTrue(numpy.allclose(g2, ga2))
     self.assertTrue(numpy.allclose(g0, ga2nm))
     return
Example #7
0
 def setUp(self):
     self.pwconst = PeakWidthModel.createByType('constant')
     self.pwconst.width = 2
     if self.tio2stru is None:
         self.tio2stru = loadDiffPyStructure('rutile.cif')
         self.tio2adpt = createStructureAdapter(self.tio2stru)
     return
Example #8
0
        def eval(self, stru=None):
            '''Perform parallel calculation and return internal value array.

            stru -- object that can be converted to StructureAdapter,
                    e.g., example diffpy Structure or pyobjcryst Crystal.
                    Use the last structure when None.

            Return numpy array.
            '''
            # use StructureAdapter for faster pickles
            from diffpy.srreal.structureadapter import createStructureAdapter
            if stru is None:
                struadpt = self.pqobj.getStructure()
            else:
                struadpt = createStructureAdapter(stru)
            self.pqobj.setStructure(struadpt)
            kwd = { 'cpuindex' : None,
                    'ncpu' : self.ncpu,
                    'pqobj' : copy.copy(self.pqobj),
                    }
            # shallow copies of kwd dictionary each with a unique cpuindex
            arglist = [kwd.copy() for kwd['cpuindex'] in range(self.ncpu)]
            for pdata in self.pmap(_parallelData, arglist):
                self.pqobj._mergeParallelData(pdata, self.ncpu)
            return self.pqobj.value
Example #9
0
 def test_createStructureAdapterTypes(self):
     '''Check types returned by conversion from diffpy.structure.
     '''
     from diffpy.srreal.structureconverters import (
         DiffPyStructureAtomicAdapter, DiffPyStructurePeriodicAdapter)
     adpt = createStructureAdapter(self.nickel)
     self.assertTrue(type(adpt) is DiffPyStructurePeriodicAdapter)
     self.nickel.pdffit = None
     adpt1 = createStructureAdapter(self.nickel)
     self.assertTrue(type(adpt1) is PeriodicStructureAdapter)
     self.nickel.lattice.setLatPar(1, 1, 1, 90, 90, 90)
     adpt2 = createStructureAdapter(self.nickel)
     self.assertTrue(type(adpt2) is AtomicStructureAdapter)
     self.nickel.pdffit = dict(scale=1)
     adpt3 = createStructureAdapter(self.nickel)
     self.assertTrue(type(adpt3) is DiffPyStructureAtomicAdapter)
     return
 def test_createStructureAdapter_int64_occupancy(self):
     """Check Structure conversion when occupany is of numpy.int64 type.
     """
     self.nickel[0].occupancy = numpy.int64(0)
     self.nickel[1].occupancy = numpy.int64(1)
     adpt = createStructureAdapter(self.nickel)
     self.assertEqual(0.0, adpt.siteOccupancy(0))
     self.assertEqual(1.0, adpt.siteOccupancy(1))
     return
Example #11
0
 def test_createStructureAdapter_int64_occupancy(self):
     """Check Structure conversion when occupany is of numpy.int64 type.
     """
     self.nickel[0].occupancy = numpy.int64(0)
     self.nickel[1].occupancy = numpy.int64(1)
     adpt = createStructureAdapter(self.nickel)
     self.assertEqual(0.0, adpt.siteOccupancy(0))
     self.assertEqual(1.0, adpt.siteOccupancy(1))
     return
 def test_createStructureAdapterTypes(self):
     '''Check types returned by conversion from diffpy.Structure.
     '''
     from diffpy.srreal.structureconverters import (
         DiffPyStructureAtomicAdapter,
         DiffPyStructurePeriodicAdapter)
     adpt = createStructureAdapter(self.nickel)
     self.assertTrue(type(adpt) is DiffPyStructurePeriodicAdapter)
     self.nickel.pdffit = None
     adpt1 = createStructureAdapter(self.nickel)
     self.assertTrue(type(adpt1) is PeriodicStructureAdapter)
     self.nickel.lattice.setLatPar(1, 1, 1, 90, 90, 90)
     adpt2 = createStructureAdapter(self.nickel)
     self.assertTrue(type(adpt2) is AtomicStructureAdapter)
     self.nickel.pdffit = dict(scale=1)
     adpt3 = createStructureAdapter(self.nickel)
     self.assertTrue(type(adpt3) is DiffPyStructureAtomicAdapter)
     return
 def test_pickling(self):
     '''check pickling of StructureAdapter instances.
     '''
     adpt = createStructureAdapter(self.nickel)
     adpt1 = cPickle.loads(cPickle.dumps(adpt))
     self.assertFalse(adpt is adpt1)
     self.assertEqual(adpt.countSites(), adpt1.countSites())
     self.assertEqual(adpt.totalOccupancy(), adpt1.totalOccupancy())
     self.assertEqual(adpt.siteAtomType(1), adpt1.siteAtomType(1))
     self.assertTrue(numpy.array_equal(
         adpt.siteCartesianPosition(1), adpt1.siteCartesianPosition(1)))
     self.assertEqual(adpt.siteMultiplicity(1), adpt1.siteMultiplicity(1))
     self.assertEqual(adpt.siteOccupancy(1), adpt1.siteOccupancy(1))
     self.assertTrue(adpt.siteAnisotropy(1) is adpt1.siteAnisotropy(1))
     self.assertTrue(numpy.array_equal(
         adpt.siteCartesianUij(1), adpt1.siteCartesianUij(1)))
     return
Example #14
0
 def test_pickling(self):
     '''check pickling of StructureAdapter instances.
     '''
     adpt = createStructureAdapter(self.nickel)
     adpt1 = pickle.loads(pickle.dumps(adpt))
     self.assertFalse(adpt is adpt1)
     self.assertEqual(adpt.countSites(), adpt1.countSites())
     self.assertEqual(adpt.totalOccupancy(), adpt1.totalOccupancy())
     self.assertEqual(adpt.siteAtomType(1), adpt1.siteAtomType(1))
     self.assertTrue(
         numpy.array_equal(adpt.siteCartesianPosition(1),
                           adpt1.siteCartesianPosition(1)))
     self.assertEqual(adpt.siteMultiplicity(1), adpt1.siteMultiplicity(1))
     self.assertEqual(adpt.siteOccupancy(1), adpt1.siteOccupancy(1))
     self.assertTrue(adpt.siteAnisotropy(1) is adpt1.siteAnisotropy(1))
     self.assertTrue(
         numpy.array_equal(adpt.siteCartesianUij(1),
                           adpt1.siteCartesianUij(1)))
     return
Example #15
0
    def fromStructure(cls, stru, sftb, q=0):
        """\
        Calculate average scattering factors from a structure object.

        Parameters
        ----------
        stru : diffpy Structure or pyobjcryst Crystal or StructureAdapter
            The structure object that stores the atom species and their
            occupancies.  Can be any type with a registered conversion
            to the StructureAdapter class.
        sftb : ScatteringFactorTable or str
            The ScatteringFactorTable object for looking up the values.
            When string use `ScatteringFactorTable.createByType` to create
            a new lookup table of the specified type.
        q : float or NumPy array (optional)
            The Q value in inverse Angstroms for which to lookup
            the scattering factor values.

        See also
        --------
        RegisterStructureAdapter : to add support for more structure types.

        Returns
        -------
        SFAverage
            The calculated scattering factor averages.
        """
        # a bit of duck-typing for faster handling of diffpy.structure
        if hasattr(type(stru), 'composition'):
            composition = stru.composition
            if isinstance(composition, dict):
                return cls.fromComposition(composition, sftb, q)
        # otherwise let's convert to a known structure type
        from diffpy.srreal.structureadapter import createStructureAdapter
        adpt = createStructureAdapter(stru)
        composition = {}
        for i in range(adpt.countSites()):
            smbl = adpt.siteAtomType(i)
            cnt = adpt.siteOccupancy(i) * adpt.siteMultiplicity(i)
            composition[smbl] = composition.get(smbl, 0) + cnt
        return cls.fromComposition(composition, sftb, q)
Example #16
0
    def fromStructure(cls, sftb, stru, q=0):
        """\
        Calculate average scattering factors from a structure object.

        Parameters
        ----------
        sftb : ScatteringFactorTable
            The ScatteringFactorTable object for looking up the values.
        stru : diffpy Structure or pyobjcryst Crystal or StructureAdapter
            The structure object that stores the atom species and their
            occupancies.  Can be any type with a registered conversion
            to the StructureAdapter class.
        q : float or NumPy array (optional)
            The Q value in inverse Angstroms for which to lookup
            the scattering factor values.

        See also
        --------
        RegisterStructureAdapter : to add support for more structure types.

        Returns
        -------
        SFAverage
            The calculated scattering factor averages.
        """
        # a bit of duck-typing for faster handling of diffpy.Structure
        if hasattr(type(stru), 'composition'):
            composition = stru.composition
            if isinstance(composition, dict):
                return cls.fromComposition(sftb, composition, q)
        # otherwise let's convert to a known structure type
        from diffpy.srreal.structureadapter import createStructureAdapter
        adpt = createStructureAdapter(stru)
        composition = {}
        for i in range(adpt.countSites()):
            smbl = adpt.siteAtomType(i)
            cnt = adpt.siteOccupancy(i) * adpt.siteMultiplicity(i)
            composition[smbl] = composition.get(smbl, 0) + cnt
        return cls.fromComposition(sftb, composition, q)
 def test_nosymmetry(self):
     '''check NoSymmetryStructureAdapter.
     '''
     pdfc0 = PDFCalculator()
     r0, g0 = pdfc0(self.nickel)
     rdf0 = pdfc0.rdf
     niuc = nosymmetry(self.nickel)
     self.assertTrue(niuc is nosymmetry(niuc))
     pdfc1 = PDFCalculator()
     r1, g1 = pdfc1(niuc)
     self.assertTrue(numpy.array_equal(r0, r1))
     self.assertFalse(numpy.allclose(g0, g1))
     tail = (r0 > 5.0)
     self.assertTrue(numpy.allclose(0.0 * g1[tail], g1[tail]))
     rdf0 = pdfc0.rdf
     rdf1 = pdfc1.rdf
     head = r0 < 3.0
     self.assertAlmostEqual(12.0, numpy.sum(rdf0[head] * pdfc0.rstep), 5)
     self.assertAlmostEqual(3.0, numpy.sum(rdf1[head] * pdfc1.rstep), 5)
     adpt0 = createStructureAdapter(self.nickel)
     ra2, ga2 = PDFCalculator()(nosymmetry(adpt0))
     self.assertTrue(numpy.array_equal(r0, ra2))
     self.assertTrue(numpy.allclose(g1, ga2))
     return
Example #18
0
 def test_nosymmetry(self):
     '''check NoSymmetryStructureAdapter.
     '''
     pdfc0 = PDFCalculator()
     r0, g0 = pdfc0(self.nickel)
     rdf0 = pdfc0.rdf
     niuc = nosymmetry(self.nickel)
     self.assertTrue(niuc is nosymmetry(niuc))
     pdfc1 = PDFCalculator()
     r1, g1 = pdfc1(niuc)
     self.assertTrue(numpy.array_equal(r0, r1))
     self.assertFalse(numpy.allclose(g0, g1))
     tail = (r0 > 5.0)
     self.assertTrue(numpy.allclose(0.0 * g1[tail], g1[tail]))
     rdf0 = pdfc0.rdf
     rdf1 = pdfc1.rdf
     head = r0 < 3.0
     self.assertAlmostEqual(12.0, numpy.sum(rdf0[head] * pdfc0.rstep), 5)
     self.assertAlmostEqual(3.0, numpy.sum(rdf1[head] * pdfc1.rstep), 5)
     adpt0 = createStructureAdapter(self.nickel)
     ra2, ga2 = PDFCalculator()(nosymmetry(adpt0))
     self.assertTrue(numpy.array_equal(r0, ra2))
     self.assertTrue(numpy.allclose(g1, ga2))
     return
 def setUp(self):
     rutile_crystal = loadObjCrystCrystal('TiO2_rutile-fit.cif')
     self.rutile = createStructureAdapter(rutile_crystal)
     return
Example #20
0
 def setUp(self):
     rutile_crystal = loadObjCrystCrystal(rutile_cif)
     self.rutile = createStructureAdapter(rutile_crystal)
     return
 def setUp(self):
     rutile_crystal = loadObjCrystCrystal(rutile_cif)
     self.rutile = createStructureAdapter(rutile_crystal)
     return
Example #22
0
 def setUp(self):
     rutile_crystal = loadObjCrystCrystal('TiO2_rutile-fit.cif')
     self.rutile = createStructureAdapter(rutile_crystal)
     return