Example #1
0
    def _getSrRealStructure(self):
        """Get the structure object for use with SrReal calculators.

        If this is periodic, then return the structure, otherwise, pass it
        inside of a nosymmetry wrapper.

        """
        if self._usesymmetry:
            return self.stru
        return nosymmetry(self.stru)
 def test_nosymmetry_pickling(self):
     '''check pickling of the NoSymmetryStructureAdapter wrapper.
     '''
     ni1ns = nosymmetry(self.nickel)
     r1, g1 = PDFCalculator()(ni1ns)
     ni2ns = cPickle.loads(cPickle.dumps(ni1ns))
     self.assertFalse(ni1ns is ni2ns)
     r2, g2 = PDFCalculator()(ni2ns)
     self.assertTrue(numpy.array_equal(r1, r2))
     self.assertTrue(numpy.array_equal(g1, g2))
     return
Example #3
0
    def _getSrRealStructure(self):
        """Get the structure object for use with SrReal calculators.

        If this is periodic, then return the structure, otherwise, pass it
        inside of a nosymmetry wrapper.

        """
        from diffpy.srreal.structureadapter import nosymmetry
        if self._usesymmetry:
            return self.stru
        return nosymmetry(self.stru)
Example #4
0
 def test_nosymmetry_pickling(self):
     '''check pickling of the NoSymmetryStructureAdapter wrapper.
     '''
     ni1ns = nosymmetry(self.nickel)
     r1, g1 = PDFCalculator()(ni1ns)
     ni2ns = pickle.loads(pickle.dumps(ni1ns))
     self.assertFalse(ni1ns is ni2ns)
     r2, g2 = PDFCalculator()(ni2ns)
     self.assertTrue(numpy.array_equal(r1, r2))
     self.assertTrue(numpy.array_equal(g1, g2))
     return
Example #5
0
 def test_nosymmetry_pickling(self):
     '''check pickling of the NoSymmetryStructureAdapter wrapper.
     '''
     ni1ns = nosymmetry(nickel)
     r1, g1 = PDFCalculator()(ni1ns)
     ni2ns = cPickle.loads(cPickle.dumps(ni1ns))
     self.failIf(ni1ns is ni2ns)
     r2, g2 = PDFCalculator()(ni2ns)
     self.failUnless(numpy.array_equal(r1, r2))
     self.failUnless(numpy.array_equal(g1, g2))
     return
 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 #7
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 test_nosymmetry_twice(self):
     '''check that second call of nosymmetry returns the same object.
     '''
     adpt1 = nosymmetry(self.nickel)
     adpt2 = nosymmetry(adpt1)
     self.assertTrue(adpt1 is adpt2)
Example #9
0
 def test_nosymmetry_twice(self):
     '''check that second call of nosymmetry returns the same object.
     '''
     adpt1 = nosymmetry(self.nickel)
     adpt2 = nosymmetry(adpt1)
     self.assertTrue(adpt1 is adpt2)
Example #10
0
 def test_nosymmetry_twice(self):
     '''check that second call of nosymmetry returns the same object.
     '''
     adpt1 = nosymmetry(nickel)
     adpt2 = nosymmetry(adpt1)
     self.failUnless(adpt1 is adpt2)
Example #11
0
 def _getSrRealStructure(self):
     if self.stru.periodic:
         rv = self.srrstru
     else:
         rv = nosymmetry(self.srrstru)
     return rv
Example #12
0
    def learninglib_build(self, output_dir=None, pdfcal_cfg=None,
                          rdf=True, xrd=False, Bisoequiv=0.1,
                          rstep=None, DebyeCal=False, nosymmetry=False,
                          tth_range=None, wavelength=0.5):
        """ method to build learning lib with diffpy based on path
        of cif library. Paramters of G(r) calculation are set
        via glbl.<attribute>. "PDFCal_config.txt" file with PDFCalculator
        configuration will also be output

        Parameters
        ----------
        pdfcal_cfg : dict, optional
            configuration of PDF calculator, default is the one defined
            inside glbl class.
        rdf : bool, optional
            option to compute RDF or not. default to True, if not,
            compute pdf
        xrd : bool, optional
            option to compute XRD (which is slow). default to False.
        Bisoequiv : float, optional
            value of isotropic thermal parameter. default is 0.1.
            scientific equation: Biso = 8 (pi**2) Uiso
        rstep : float, optioanl
            space of PDF. default is pi/qmax.
        DebyeCal : bool, optional
            option to use Debye calculator. default is False.
        nosymmetry : bool, optional
            DEPRECATED for now. option to apply no symmetry.
            default is False.
        tth_range : ndarray, optional
            range of 2theta. default is [0:0.1:90]
        wavelength : float, optional
            wavelength in angstroms, default to 0.5 A which corresponds
            to Qmax ~= 17
        """
        # setup output dir
        timestr = _timestampstr(time.time())
        if output_dir is None:
            tail = "LearningLib_{}".format(timestr)
            output_dir = os.path.join(os.getcwd(), tail)
        print('=== output dir would be {} ==='.format(output_dir))
        self.output_dir = output_dir
        if tth_range is None:
            self.tth_range = np.arange(0, 90, 0.1)
        self.wavelength = wavelength
        self.std_q = theta2q(self.tth_range, self.wavelength)

        ####### configure pymatgen XRD calculator #####
        # instantiate calculators
        xrd_cal = XRDCalculator()
        xrd_cal.wavelength = self.wavelength
        xrd_cal.TWO_THETA_TOL = 10**-2
        self.calculate_params.update({'xrd_wavelength':
                                       xrd_cal.wavelength})

        xrd_list = []
        sg_list = []
        # (a,b,c, alpha, beta, gamma, volume)
        structure_list_1 = []  # primative cell
        structure_list_2 = []  # ordinary cell
        # chemical element
        composition_list_1 = []  # primative cell
        composition_list_2 = []  # ordinary cell
        fail_list = []

        ####### configure diffpy PDF calculator ######
        if DebyeCal:
            cal = DebyePDFCalculator()
            self.calculator_type = 'Debye'
        cal = PDFCalculator()
        self.calculator = cal
        self.calculator_type = 'PDF'
        self.calculate_params.update({'calculator_type':
                                      self.calculator_type})
        # setup calculator parameters
        if rstep is None:
            rstep = glbl.rstep
        self.rstep = rstep
        self.calculator.rstep = rstep  # annoying fact
        self.calculate_params.update({'rstep':rstep})

        if pdfcal_cfg is None:
            self.pdfcal_cfg = glbl.cfg
        self.calculate_params.update(self.pdfcal_cfg)

        # configure calculator
        for k,v in self.pdfcal_cfg.items():
            setattr(self.calculator, k, v)
        # empty list to store results
        gr_list = []
        rdf_list = []
        print("====== INFO: calculation parameters:====\n{}"
              .format(self.calculate_params))
        struc_df = pd.DataFrame()
        ############# loop through cifs #################
        for cif in sorted(self.cif_list):
            _cif = os.path.join(self.input_dir, cif)
            try:
                # diffpy structure
                struc = loadStructure(_cif)
                struc.Bisoequiv = Bisoequiv

                ## calculate PDF/RDF with diffpy ##
                if nosymmetry:
                    struc = nosymmetry(struc)
                cal.setStructure(struc)
                cal.eval()

                # pymatge structure
                struc_meta = CifParser(_cif)
                ## calculate XRD with pymatgen ##
                if xrd:
                    xrd = xrd_cal.get_xrd_data(struc_meta\
                            .get_structures(False).pop())
                    _xrd = np.asarray(xrd)[:,:2]
                    q, iq = _xrd.T
                    interp_q = assign_nearest(self.std_q, q, iq)
                    xrd_list.append(interp_q)
                else:
                    pass
                ## test space group info ##
                _sg = struc_meta.get_structures(False).pop()\
                        .get_space_group_info()
            except:
                print("{} fail".format(_cif))
                fail_list.append(cif)
            else:
                # no error for both pymatgen and diffpy
                gr_list.append(cal.pdf)
                rdf_list.append(cal.rdf)
                self.density_list.append(cal.slope)
                print('=== Finished evaluating PDF from structure {} ==='
                       .format(cif))
                ## update features ##
                flag = ['primitive', 'ordinary']
                option = [True, False]
                compo_list = [composition_list_1, composition_list_2]
                struc_fields = ['a','b','c','alpha','beta','gamma', 'volume']
                for f, op, compo in zip(flag, option, compo_list):
                    rv_dict = {}
                    struc = struc_meta.get_structures(op).pop()
                    a, b, c = struc.lattice.abc
                    aa, bb, cc = struc.lattice.angles
                    volume = struc.volume
                    for k, v in zip(struc_fields,
                                    [a, b, c, aa, bb, cc, volume]):
                        rv_dict.update({"{}_{}".format(f, k) : v})
                    compo.append(struc.composition.as_dict())
                    struc_df = struc_df.append(rv_dict,
                                               ignore_index=True)
                # sg info, use the ordinary setup
                sg_list.append(struc.get_space_group_info())
                print('=== Finished evaluating XRD from structure {} ==='
                      .format(cif))

        # finally, store crucial calculation results as attributes
        self.r_grid = cal.rgrid
        #4*pi * r^2 * rho(r) = R(r)  -> RDF to density 
        self.gr_array = np.asarray(gr_list)/4/np.pi/self.r_grid**2
        self.rdf_array = np.asarray(gr_list)
        self.density_list = np.asarray(self.density_list)
        self.xrd_info = np.asarray(xrd_list)
        self.sg_list = sg_list
        # 1 -> primitive , 2 -> ordinary
        self.composition_list_1 = composition_list_1
        self.composition_list_2 = composition_list_2
        self.struc_df = struc_df
        self.fail_list = fail_list
Example #13
0
    def gr_lib_build(self, cif_lib_path):
        ''' method to calculate G(r) based on path of cif library located at.
    
        Paramters of G(r) calculation are set via glbl.<attribute>, one can tune it based on purpose of building library.
        After entire method, text file contains all G(r), space_group_symbol and material name will be saved respectively

        Parameters
        ----------
        
        cif_lib_path : str
            path to lib of cif files
        
        ''' 
        el_list = []   # data column 
        space_group_symbol_list = [] # reference for search have been done in the past        

        # set up calculation environment
        #dbc = DebyePDFCalculator()
        pdf = PDFCalculator()
        pdf.rstep = glbl.rstep
        cfg = {'qmin': glbl.q_min, 'qmax':glbl.q_max, 'rmin':glbl.r_min, 'rmax': glbl.r_max}
        Bisoequiv = glbl.Bisoequiv #FIXME: current value = 0.5, need to figure out the most suitable value
        print('====Parameter used in this PDF calculator is: {}===='.format(cfg))
        print('====Bisoequiv used in this PDF calculator is: {}===='.format(Bisoequiv))

        # step 1: list cif dir
        output_dir = os.path.join(self.data_dir, 'lib_data')
        self._makedirs(output_dir)
        self.output_dir = output_dir
        cif_f_list = [ f for f in os.listdir(self.cif_dir)]
        
        # hidden step as numpy can't take an empty array and stack
        struc = loadStructure(os.path.join(self.cif_dir, cif_f_list[0]))
        struc.Bisoequiv =  Bisoequiv
        (r,g) = pdf(nosymmetry(struc), **cfg)
        r_grid = np.array(r) # data x-axis
        gr_list = np.empty_like(np.array(g)) # data y-axis        

        for cif in cif_f_list:
            # part 2: calculate PDF with diffpy
            struc = loadStructure(os.path.join(self.cif_dir, cif))
            struc.Bisoequiv =  Bisoequiv
            #(r,g) = pdf(nosymmetry(struc), **cfg)
            (r,g) = pdf(struc, **cfg)
            print('Finished calculation of G(r) on {}'.format(cif))
            sep = cif.index('_')
            space_group_symbol = cif[:sep]
            m_name = cif[sep+1:]
            # part 3: save data
            #if not gr_list.any():
                #gr_list = np.append(gr_list, g)
            gr_list = np.vstack((gr_list,g))
            space_group_symbol_list.append(space_group_symbol)
            el_list.append(m_name)
            #print('size of gr_list = {}'.format(np.shape(gr_list)))
            #space_group_symbol_list = np.concatenate([space_group_symbol_list, space_group_symbol], axis=0)
            #el_list = np.concatenate([el_list, m_name], axis=0)

        time_info = time.strftime('%Y-%m-%d')
        gr_list_name = '{}_{}_Gr'.format(self.crystal_system, time_info)
        gr_list_w_name = os.path.join(output_dir, gr_list_name)
        print('Saving {}'.format(gr_list_w_name))
        np.save(gr_list_w_name, gr_list)

        r_grid_name = '{}_{}_rgrid'.format(self.crystal_system, time_info)
        r_grid_w_name = os.path.join(output_dir, r_grid_name)
        np.save(r_grid_w_name, r)
        
        space_group_symbol_list_name = '{}_{}_SpaceGroupSymbol'.format(self.crystal_system, time_info)
        space_group_symbol_list_w_name= os.path.join(output_dir, space_group_symbol_list_name)
        np.save(space_group_symbol_list_w_name, space_group_symbol_list) #fmt="%s")
        
        el_list_name = '{}_{}_Element'.format(self.crystal_system, time_info)
        el_list_w_name = os.path.join(output_dir, el_list_name)
        np.save(el_list_w_name, el_list) #fmt="%s")
        
        print('''====SUMMARY====:
for {} cystsal sytem,
Number of cif pulled out and G(r) calculated is {}'''.format(self.crystal_system, np.shape(gr_list)))
        return gr_list
 def test_nosymmetry_twice(self):
     '''check that second call of nosymmetry returns the same object.
     '''
     adpt1 = nosymmetry(nickel)
     adpt2 = nosymmetry(adpt1)
     self.failUnless(adpt1 is adpt2)