Ejemplo n.º 1
0
    def setUp(self):
        self.Al2O3_xane_1 = XANES(Al2O3_spect['x'], Al2O3_spect['y'], Al2O3_stru, Element('Al'), 'K')

        self.Al2O3_xane_2 = deepcopy(self.Al2O3_xane_1)
        self.Al2O3_xane_2.x = self.Al2O3_xane_2.x - 40

        self.Al2O3_xane_left_shift5 = deepcopy(self.Al2O3_xane_1)
        self.Al2O3_xane_left_shift5.x = self.Al2O3_xane_left_shift5.x - 5

        self.Al2O3_xane_right_shift5 = deepcopy(self.Al2O3_xane_1)
        self.Al2O3_xane_right_shift5.x = self.Al2O3_xane_right_shift5.x + 5

        self.LiCoO2_xane = XANES(LiCoO2_spect['x'], LiCoO2_spect['y'], LiCoO2_stru, Element('Co'), 'K')
Ejemplo n.º 2
0
def msonify_xas(item):
    energy = py_.pluck(item['spectrum'], 0)  # (eV)
    intensity = py_.pluck(item['spectrum'], 3)  # (mu)
    structure = Structure.from_dict(item['structure'])
    absorption_specie = structure[item['absorbing_atom']].species_string
    mid_and_el = ",".join([item["mp_id"], absorption_specie])
    edge = "K"
    structure.add_site_property(
        'absorbing_atom',
        [i == item['absorbing_atom'] for i, _ in enumerate(structure.sites)])
    if len(energy) == 0:
        return {
            "spectrum": None,
            "mid_and_el": mid_and_el,
            "error": "Empty spectrum"
        }
    try:
        out = {
            "spectrum":
            XANES(
                x=energy,
                y=intensity,
                structure=structure,
                absorption_specie=absorption_specie,
                edge=edge,
            ).as_dict(),
            "mid_and_el":
            mid_and_el,
        }
    except ValueError as e:
        out = {"spectrum": None, "mid_and_el": mid_and_el, "error": str(e)}
    return out
Ejemplo n.º 3
0
def data_missing(xas_docs):
    """
    Do some sites have no spectra recorded?

    Checks symmetrically equivalent sites.
    """
    xas_docs = [d for d in xas_docs if "error" not in d]
    if len(xas_docs) == 0:
        return "No docs at all"
    spectra = [XANES.from_dict(d['spectrum']) for d in xas_docs]
    absorption_specie = spectra[0].absorption_specie
    ss = SymmSites(spectra[0].structure)
    absorbing_atoms = set([
        next(i for i, yes in enumerate(
            s.structure.site_properties['absorbing_atom']) if yes)
        for s in spectra
    ])
    site_indices_with_absorption_specie = [
        i for i, site in enumerate(spectra[0].structure.sites)
        if site.species_and_occu.reduced_formula == absorption_specie
    ]
    some_sites_absent = any(
        len(set(ss.get_equivalent_site_indices(i)) & absorbing_atoms) == 0
        for i in site_indices_with_absorption_specie)
    some_spectra_empty = any(len(s.energy) == 0 for s in spectra)
    return ((some_sites_absent and ("absent sites", absorbing_atoms))
            or (some_spectra_empty and "empty spectra"))
Ejemplo n.º 4
0
def site_weighted_spectrum(xas_docs, num_samples=200):
    """
    Equivalent-site-weighted spectrum for a specie in a structure.

    Args:
        xas_docs (list): MongoDB docs for all XAS XANES K-edge spectra
            for a specie for a structure.
        num_samples (int): Number of samples for interpolation.
            Original data has 100 data points.

    Returns:
        tuple: a plottable (x, y) pair for the spectrum
    """
    maxes, mins = [], []
    fs = []
    multiplicities = []
    absorbing_atoms = set()

    spectra = [XANES.from_dict(d['spectrum']) for d in xas_docs]

    for spectrum in spectra:
        # Checking the multiplicities of sites
        structure = spectrum.structure
        sa = SpacegroupAnalyzer(structure)
        ss = sa.get_symmetrized_structure()
        absorbing_atom = next(i for i, yes in enumerate(
            structure.site_properties['absorbing_atom']) if yes)
        multiplicity = len(ss.find_equivalent_sites(structure[absorbing_atom]))
        multiplicities.append(multiplicity)

        # Getting axis limits for each spectrum for the sites corresponding to
        # K-edge is a bit tricky, because the x-axis data points don't align
        # among different spectra for the same structure. So, prepare for
        # interpolation within the intersection of x-axis ranges.
        mins.append(spectrum.energy[0])
        maxes.append(spectrum.energy[-1])

        # 3rd-order spline interpolation
        f = interp1d(spectrum.energy,
                     spectrum.intensity,
                     kind='cubic',
                     bounds_error=False,
                     fill_value=0)
        fs.append(f)

        absorbing_atoms |= set(
            SymmSites(structure).get_equivalent_site_indices(absorbing_atom))

    energy = np.linspace(max(mins), min(maxes), num=num_samples)
    weighted_intensity = np.zeros(num_samples)
    sum_multiplicities = sum(multiplicities)
    for i in range(len(multiplicities)):
        weighted_intensity += (multiplicities[i] *
                               fs[i](energy)) / sum_multiplicities
    structure = spectra[0].structure
    structure.remove_site_property('absorbing_atom')
    structure.add_site_property(
        'absorbing_atom',
        [i in absorbing_atoms for i, _ in enumerate(structure.sites)])
    return XANES(
        x=energy,
        y=weighted_intensity,
        structure=structure,
        absorption_specie=spectra[0].absorption_specie,
        edge=spectra[0].edge,
    )
Ejemplo n.º 5
0
 def setUp(self):
     self.xanes = XANES.from_dict(spect_data_dict)
Ejemplo n.º 6
0
 def test_to_from_dict(self):
     s = XANES.from_dict(self.xanes.as_dict())
     self.assertArrayAlmostEqual(s.y, self.xanes.y)
Ejemplo n.º 7
0
 def setUp(self):
     self.xanes = XANES.from_dict(spect_data_dict)
Ejemplo n.º 8
0
 def test_to_from_dict(self):
     s = XANES.from_dict(self.xanes.as_dict())
     self.assertArrayAlmostEqual(s.y, self.xanes.y)
Ejemplo n.º 9
0
def compare_my_unknown(fname,
                       mpid,
                       absorbing_atom,
                       data_dir=None,
                       dbroot=None,
                       xsrange=None,
                       ysrange=None):

    here = os.getcwd()

    if data_dir is None:
        data_dir = global_cache['data_dir']

    os.chdir(data_dir)

    if not os.path.isfile(fname):
        print(
            fname +
            ' is not found in unknowns folder.\n Please put it here as two-column text data.\n Exitting....'
        )
        os.chdir(here)
        return

    if dbroot is None:
        dbroot = t4iss_defaults['t4iss_data']

    if not os.path.isdir(dbroot):
        os.mkdir(dbroot)

    os.chdir(dbroot)

    if not os.path.isdir(mpid):
        print('XANES for ' + mpid +
              ' is NOT available in local database\n Exitting....')
        os.chdir(here)
        return
    else:
        os.chdir(mpid)

    ux, uy = np.loadtxt(join(data_dir, fname),
                        unpack=True,
                        comments='#',
                        usecols=(0, 1),
                        skiprows=0)
    uy = uy - uy[0]
    uy = uy / max(uy)

    structure = mg.Structure.from_file("CONTCAR")
    finder = SpacegroupAnalyzer(structure)
    symmetrized_structure = finder.get_symmetrized_structure()
    [
        sites, indices
    ] = symmetrized_structure.equivalent_sites, symmetrized_structure.equivalent_indices

    spectra = []
    for i, s in enumerate(sites):
        if s[0].species_string is absorbing_atom:
            f = 'feff_{:03d}_{}'.format(indices[i][0] + 1, absorbing_atom)
            if os.path.isdir(f):
                os.chdir(f)
                if os.path.isfile('xanes.pkl'):
                    xanes = pickle.load(open('xanes.pkl', 'rb'))
                elif os.path.isfile('xmu.dat'):
                    abs_specie = absorbing_atom
                    x, y = np.loadtxt('xmu.dat',
                                      unpack=True,
                                      comments='#',
                                      usecols=(0, 3),
                                      skiprows=0)
                    xanes = XANES(x, y, structure, Element(abs_specie), 'K')
                    pickle.dump(xanes, open('xanes.pkl', 'wb'))
                else:
                    xanes = []
                s_weight = len(indices[i])
                local_environment = symmetrized_structure.get_sites_in_sphere(
                    symmetrized_structure[indices[i][0]].coords, 10.1)
                spectra.append([s_weight, xanes, local_environment])
                os.chdir('..')
            else:
                xanes = []
                s_weight = len(indices[i])
                local_environment = symmetrized_structure.get_sites_in_sphere(
                    symmetrized_structure[indices[i][0]].coords, 10.1)
                spectra.append([s_weight, xanes, local_environment])

    unknown_spectra = [ux, uy]

    try:
        os.chdir(here)
        if xsrange is None:
            xsrange = [-6.0, 6.0]
        if ysrange is None:
            ysrange = [-2.0, 2.0]
        plot_XANES_with_unknown(spectra, mpid, unknown_spectra, xsrange,
                                ysrange)
    except Exception as exc:
        os.chdir(here)
        print('Error: \n Unable to plot. Something is wrong...')
        print(exc)
    #os.chdir(here)
    #plot_XANES_with_unknown(spectra,mpid,unknown_spectra,xsrange,ysrange)
    return
Ejemplo n.º 10
0
def compare_two(mpid1, mpid2, absorbing_atom, dbroot=None):

    here = os.getcwd()

    if dbroot is None:
        dbroot = t4iss_defaults['t4iss_data']
    if not os.path.isdir(dbroot):
        os.mkdir(dbroot)

    os.chdir(dbroot)

    if not os.path.isdir(mpid1):
        print(
            'XANES for ' + mpid1 +
            ' is NOT available in local database\n Try get_XANES function first \n Exitting....'
        )
        os.chdir(here)
        return
    if not os.path.isdir(mpid2):
        print(
            'XANES for ' + mpid2 +
            ' is NOT available in local database\n Try get_XANES function first \n Exitting....'
        )
        os.chdir(here)
        return

    os.chdir(mpid1)
    structure = mg.Structure.from_file("CONTCAR")
    finder = SpacegroupAnalyzer(structure)
    symmetrized_structure = finder.get_symmetrized_structure()
    [
        sites, indices
    ] = symmetrized_structure.equivalent_sites, symmetrized_structure.equivalent_indices
    spectra = []
    for i, s in enumerate(sites):
        if s[0].species_string is absorbing_atom:
            f = 'feff_{:03d}_{}'.format(indices[i][0] + 1, absorbing_atom)
            if os.path.isdir(f):
                os.chdir(f)
                if os.path.isfile('xanes.pkl'):
                    xanes = pickle.load(open('xanes.pkl', 'rb'))
                elif os.path.isfile('xmu.dat'):
                    abs_specie = absorbing_atom
                    x, y = np.loadtxt('xmu.dat',
                                      unpack=True,
                                      comments='#',
                                      usecols=(0, 3),
                                      skiprows=0)
                    xanes = XANES(x, y, structure, Element(abs_specie), 'K')
                    pickle.dump(xanes, open('xanes.pkl', 'wb'))
                else:
                    xanes = []
                s_weight = len(indices[i])
                local_environment = symmetrized_structure.get_sites_in_sphere(
                    symmetrized_structure[indices[i][0]].coords, 10.1)
                spectra.append([s_weight, xanes, local_environment])
                os.chdir('..')
            else:
                xanes = []
                s_weight = len(indices[i])
                local_environment = symmetrized_structure.get_sites_in_sphere(
                    symmetrized_structure[indices[i][0]].coords, 10.1)
                spectra.append([s_weight, xanes, local_environment])
    os.chdir('..')
    spectra1 = spectra

    os.chdir(mpid2)
    structure = mg.Structure.from_file("CONTCAR")
    finder = SpacegroupAnalyzer(structure)
    symmetrized_structure = finder.get_symmetrized_structure()
    [
        sites, indices
    ] = symmetrized_structure.equivalent_sites, symmetrized_structure.equivalent_indices
    spectra = []
    for i, s in enumerate(sites):
        if s[0].species_string is absorbing_atom:
            f = 'feff_{:03d}_{}'.format(indices[i][0] + 1, absorbing_atom)
            if os.path.isdir(f):
                os.chdir(f)
                if os.path.isfile('xanes.pkl'):
                    xanes = pickle.load(open('xanes.pkl', 'rb'))
                elif os.path.isfile('xmu.dat'):
                    abs_specie = absorbing_atom
                    x, y = np.loadtxt('xmu.dat',
                                      unpack=True,
                                      comments='#',
                                      usecols=(0, 3),
                                      skiprows=0)
                    xanes = XANES(x, y, structure, Element(abs_specie), 'K')
                    pickle.dump(xanes, open('xanes.pkl', 'wb'))
                else:
                    xanes = []
                s_weight = len(indices[i])
                local_environment = symmetrized_structure.get_sites_in_sphere(
                    symmetrized_structure[indices[i][0]].coords, 10.1)
                spectra.append([s_weight, xanes, local_environment])
                os.chdir('..')
            else:
                xanes = []
                s_weight = len(indices[i])
                local_environment = symmetrized_structure.get_sites_in_sphere(
                    symmetrized_structure[indices[i][0]].coords, 10.1)
                spectra.append([s_weight, xanes, local_environment])
    os.chdir('..')
    spectra2 = spectra

    try:
        os.chdir(here)
        plot_XANES_two_together(spectra1, mpid1, spectra2, mpid2)
    except Exception as exc:
        os.chdir(here)
        print('Error: \n Unable to plot. Something is wrong...')
        print(exc)

    return
Ejemplo n.º 11
0
def get_XANES(mpr,
              mpid,
              absorbing_atom,
              run_feff=None,
              dbroot=None,
              plot=None,
              n_cpu=None,
              export_figure=None):

    here = os.getcwd()
    # this is the package current directory
    #here = os.path.dirname(os.path.realpath(__file__)) + "/.."

    if dbroot is None:
        dbroot = t4iss_defaults['t4iss_data']
    if not os.path.isdir(dbroot):
        os.mkdir(dbroot)

    if run_feff is None:
        run_feff = False
    elif run_feff:
        if os.name == 'nt':
            print(
                'Windows OS is not supported for run_feff=True \nSorry......')
            run_feff = False
        else:
            rFMS = 8.1
            rSCF = 6.1
            corehole = 'RPA'
            feff_cmd = global_cache['feff_cmd']

    if plot is None:
        plot = True

    if n_cpu is None:
        n_cpu = 2

    if export_figure is None:
        export_figure = False

    os.chdir(dbroot)

    if not os.path.isdir(mpid):
        data = mpr._make_request(
            '/xas/spectra_for', {
                "material_ids": json.dumps([mpid]),
                "elements": json.dumps([absorbing_atom])
            })
        if not data:
            mp_xas_avail = False
            if run_feff:
                os.makedirs(mpid, exist_ok=True)
                os.chdir(mpid)
            else:
                #print('XANES is not available in MP or local database.')
                try:
                    structure = mpr.get_structure_by_material_id(mpid,
                                                                 final=True)
                    os.makedirs(mpid, exist_ok=True)
                    os.chdir(mpid)
                except:
                    print('structure for ' + mpid +
                          ' is not available in MP.\nExitting....')
                    os.chdir(here)
                    return
        else:
            mp_xas_avail = True
            data = mpr.get_data(mpid, data_type="feff", prop="xas")
            os.makedirs(mpid, exist_ok=True)
            os.chdir(mpid)
    else:
        os.chdir(mpid)

    if not os.path.isfile('CONTCAR'):
        structure = mpr.get_structure_by_material_id(mpid, final=True)
        structure.to(fmt='poscar', filename='CONTCAR')
    structure = mg.Structure.from_file("CONTCAR")

    finder = SpacegroupAnalyzer(structure)
    symmetrized_structure = finder.get_symmetrized_structure()
    [
        sites, indices
    ] = symmetrized_structure.equivalent_sites, symmetrized_structure.equivalent_indices

    feff_todos = []
    for i, s in enumerate(sites):
        if s[0].species_string is absorbing_atom:
            f = 'feff_{:03d}_{}'.format(indices[i][0] + 1, absorbing_atom)
            if os.path.isdir(f):
                os.chdir(f)
                if not os.path.isfile('xanes.pkl'):
                    data = mpr._make_request(
                        '/xas/spectra_for', {
                            "material_ids": json.dumps([mpid]),
                            "elements": json.dumps([absorbing_atom])
                        })
                    if not data:
                        mp_xas_avail = False
                    else:
                        mp_xas_avail = True
                    if mp_xas_avail:
                        data = mpr.get_data(mpid, data_type="feff", prop="xas")
                        for xas_doc in data[0]['xas']:
                            abs_atom = xas_doc['absorbing_atom']
                            if abs_atom is indices[i][0]:
                                x, y = xas_doc['spectrum']
                                struct = xas_doc["structure"]
                                edge = xas_doc["edge"]
                                xanes = XANES(x,
                                              y,
                                              struct,
                                              Element(absorbing_atom),
                                              edge='K')
                                pickle.dump(xanes, open('xanes.pkl', 'wb'))
                                #out = np.column_stack( (x,y) )
                                #np.savetxt('xanes.dat', out, fmt="%10.3f %6.4e")
                        os.chdir('..')
                    elif os.path.isfile('xmu.dat'):
                        x, y = np.loadtxt('xmu.dat',
                                          unpack=True,
                                          comments='#',
                                          usecols=(0, 3),
                                          skiprows=0)
                        xanes = XANES(x, y, structure, Element(absorbing_atom),
                                      'K')
                        pickle.dump(xanes, open('xanes.pkl', 'wb'))
                        os.chdir('..')
                        #out = np.column_stack( (x,y) )
                        #np.savetxt('xanes.dat', out, fmt="%10.3f %6.4e")
                    elif run_feff:
                        write_feffinp('../CONTCAR',
                                      cai=indices[i][0],
                                      dmax=rFMS + 2,
                                      rFMS=rFMS,
                                      rSCF=rSCF,
                                      corehole=corehole)
                        feff_todos.append(os.getcwd())
                        os.chdir('..')
                else:
                    os.chdir('..')
            else:
                data = mpr._make_request(
                    '/xas/spectra_for', {
                        "material_ids": json.dumps([mpid]),
                        "elements": json.dumps([absorbing_atom])
                    })
                if data:
                    os.makedirs(f, exist_ok=True)
                    os.chdir(f)
                    data = mpr._make_request(
                        '/xas/spectra_for', {
                            "material_ids": json.dumps([mpid]),
                            "elements": json.dumps([absorbing_atom])
                        })
                    if not data:
                        mp_xas_avail = False
                    else:
                        mp_xas_avail = True
                    if mp_xas_avail:
                        data = mpr.get_data(mpid, data_type="feff", prop="xas")
                        for xas_doc in data[0]['xas']:
                            abs_atom = xas_doc['absorbing_atom']
                            if abs_atom is indices[i][0]:
                                x, y = xas_doc['spectrum']
                                struct = xas_doc["structure"]
                                edge = xas_doc["edge"]
                                xanes = XANES(x,
                                              y,
                                              struct,
                                              Element(absorbing_atom),
                                              edge='K')
                                pickle.dump(xanes, open('xanes.pkl', 'wb'))
                                #out = np.column_stack( (x,y) )
                                #np.savetxt('xanes.dat', out, fmt="%10.3f %6.4e")
                    os.chdir('..')
                elif run_feff:
                    os.makedirs(f, exist_ok=True)
                    os.chdir(f)
                    write_feffinp('../CONTCAR',
                                  cai=indices[i][0],
                                  dmax=rFMS + 2,
                                  rFMS=rFMS,
                                  rSCF=rSCF,
                                  corehole=corehole)
                    feff_todos.append(os.getcwd())
                    os.chdir('..')
                else:
                    break

    if run_feff:
        chunks = [
            feff_todos[i:i + n_cpu] for i in range(0, len(feff_todos), n_cpu)
        ]
        for c in chunks:
            f = open('feff.sh', "w+")
            for i in c:
                f.write('cd ' + i + '\n')
                f.write(feff_cmd + ' &\n')
                print('running feff at ' + i)
            f.write('wait')
            f.close()
            subprocess.call(' chmod +x feff.sh ', shell=True)
            subprocess.call(' ./feff.sh ', shell=True)
            if os.path.exists('feff.sh'): os.remove('feff.sh')

    spectra = []
    for i, s in enumerate(sites):
        if s[0].species_string is absorbing_atom:
            f = 'feff_{:03d}_{}'.format(indices[i][0] + 1, absorbing_atom)
            if os.path.isdir(f):
                os.chdir(f)
                if os.path.isfile('xanes.pkl'):
                    xanes = pickle.load(open('xanes.pkl', 'rb'))
                elif os.path.isfile('xmu.dat'):
                    abs_specie = absorbing_atom
                    x, y = np.loadtxt('xmu.dat',
                                      unpack=True,
                                      comments='#',
                                      usecols=(0, 3),
                                      skiprows=0)
                    xanes = XANES(x, y, structure, Element(abs_specie), 'K')
                    pickle.dump(xanes, open('xanes.pkl', 'wb'))
                    #out = np.column_stack( (x,y) )
                    #np.savetxt('xanes.dat', out, fmt="%10.3f %6.4f")
                else:
                    xanes = []
                s_weight = len(indices[i])
                local_environment = symmetrized_structure.get_sites_in_sphere(
                    symmetrized_structure[indices[i][0]].coords, 10.1)
                spectra.append([s_weight, xanes, local_environment])
                os.chdir('..')
            else:
                xanes = []
                s_weight = len(indices[i])
                local_environment = symmetrized_structure.get_sites_in_sphere(
                    symmetrized_structure[indices[i][0]].coords, 10.1)
                spectra.append([s_weight, xanes, local_environment])
    if plot:
        try:
            os.chdir(here)
            plot_XANES(spectra, mpid, export_figure)
        except Exception as exc:
            print('Error: \n Unable to plot. Something is wrong...')
            print(exc)
            os.chdir(here)
            return

    os.chdir(here)
    return