Example #1
0
def f1(element, energy):
    """
    
    The real part of anomalous X-ray scattering factor for 
    selected input energy (or energies) in eV.
    using xraylib backend

    Parameters:
        element : Name, Symbol or Atomic Number (Z) as input
        energy  : energy in keV  - scalar, list, tuple or numpy array
                    
    Returns:
        f1 : float or numpy array - same size as input energy 
             real part of anomalous X-ray scattering factor at input
             energy or energies 
    
    """
    z = elementDB[element]["Z"]
    if isinstance(energy, (list, tuple, np.ndarray)):
        f1 = np.zeroslike(energy)
        for i,enrg in enumerate(energy):
            f1[i] = xraylib.Fi(z,enrg)
    else:
        f1 = xraylib.Fi(z,enrg)

    return f1
Example #2
0
def Fi(ID,E=None):
  if E==None:
    E=pypsepics.get("SIOC:SYS0:ML00:AO627")/1000
  E=eV(E)/1000
  z=elementZ[ID]
  f=xraylib.Fi(z,E)  
  return f
Example #3
0
def reflectivity(descriptor,energy,theta,density=None,rough=0.0):



    if isinstance(descriptor,str):
        Z = xraylib.SymbolToAtomicNumber(descriptor)
        symbol = descriptor
    else:
        Z = descriptor
        symbol = xraylib.AtomicNumberToSymbol(descriptor)

    if density == None:
        density = xraylib.ElementDensity(Z)

    atwt = xraylib.AtomicWeight(Z)
    avogadro = codata.Avogadro
    toangstroms = codata.h * codata.c / codata.e * 1e10
    re = codata.e**2 / codata.m_e / codata.c**2 / (4*numpy.pi*codata.epsilon_0) * 1e2 # in cm

    molecules_per_cc = density * avogadro / atwt
    wavelength = toangstroms / energy  * 1e-8 # in cm
    k = molecules_per_cc * re * wavelength * wavelength / 2.0 / numpy.pi

    f1 = numpy.zeros_like(energy)
    f2 = numpy.zeros_like(energy)
    for i,ienergy in enumerate(energy):
        f1[i] = Z + xraylib.Fi(Z,1e-3*ienergy)
        f2[i] = - xraylib.Fii(Z,1e-3*ienergy)

    alpha = 2.0 * k * f1
    gamma = 2.0 * k * f2

    rs,rp,runp = interface_reflectivity(alpha,gamma,theta)

    if rough != 0:
        rough *= 1e-8 # to cm
        debyewaller = numpy.exp( -( 4.0 * numpy.pi * numpy.sin(theta) * rough / wavelength)**2)
    else:
        debyewaller = 1.0

    return rs*debyewaller,rp*debyewaller,runp*debyewaller
Example #4
0
    def new_bragg(cls,
                  DESCRIPTOR="Graphite",
                  H_MILLER_INDEX=0,
                  K_MILLER_INDEX=0,
                  L_MILLER_INDEX=2,
                  TEMPERATURE_FACTOR=1.0,
                  E_MIN=5000.0,
                  E_MAX=15000.0,
                  E_STEP=100.0,
                  SHADOW_FILE="bragg.dat"):
        """
         SHADOW preprocessor for crystals - python+xraylib version

         -"""
        # retrieve physical constants needed
        codata = scipy.constants.codata.physical_constants
        codata_e2_mc2, tmp1, tmp2 = codata["classical electron radius"]
        # or, hard-code them
        # In [179]: print("codata_e2_mc2 = %20.11e \n" % codata_e2_mc2 )
        # codata_e2_mc2 =    2.81794032500e-15

        fileout = SHADOW_FILE
        descriptor = DESCRIPTOR

        hh = int(H_MILLER_INDEX)
        kk = int(K_MILLER_INDEX)
        ll = int(L_MILLER_INDEX)

        temper = float(TEMPERATURE_FACTOR)

        emin = float(E_MIN)
        emax = float(E_MAX)
        estep = float(E_STEP)

        #
        # end input section, start calculations
        #

        f = open(fileout, 'wt')

        cryst = xraylib.Crystal_GetCrystal(descriptor)
        volume = cryst['volume']

        #test crystal data - not needed
        itest = 1
        if itest:
            if (cryst == None):
                sys.exit(1)
            print("  Unit cell dimensions are %f %f %f" %
                  (cryst['a'], cryst['b'], cryst['c']))
            print("  Unit cell angles are %f %f %f" %
                  (cryst['alpha'], cryst['beta'], cryst['gamma']))
            print("  Unit cell volume is %f A^3" % volume)
            print("  Atoms at:")
            print("     Z  fraction    X        Y        Z")
            for i in range(cryst['n_atom']):
                atom = cryst['atom'][i]
                print("    %3i %f %f %f %f" %
                      (atom['Zatom'], atom['fraction'], atom['x'], atom['y'],
                       atom['z']))
            print("  ")

        volume = volume * 1e-8 * 1e-8 * 1e-8  # in cm^3
        #flag ZincBlende
        f.write("%i " % 0)
        #1/V*electronRadius
        f.write("%e " % ((1e0 / volume) * (codata_e2_mc2 * 1e2)))
        #dspacing
        dspacing = xraylib.Crystal_dSpacing(cryst, hh, kk, ll)
        f.write("%e " % (dspacing * 1e-8))
        f.write("\n")
        #Z's
        atom = cryst['atom']
        f.write("%i " % atom[0]["Zatom"])
        f.write("%i " % atom[-1]["Zatom"])
        f.write("%e " % temper)  # temperature parameter
        f.write("\n")

        ga = (1e0+0j) + cmath.exp(1j*cmath.pi*(hh+kk))  \
                                 + cmath.exp(1j*cmath.pi*(hh+ll))  \
                                 + cmath.exp(1j*cmath.pi*(kk+ll))
        gb = ga * cmath.exp(1j * cmath.pi * 0.5 * (hh + kk + ll))
        ga_bar = ga.conjugate()
        gb_bar = gb.conjugate()

        f.write("(%20.11e,%20.11e ) \n" % (ga.real, ga.imag))
        f.write("(%20.11e,%20.11e ) \n" % (ga_bar.real, ga_bar.imag))
        f.write("(%20.11e,%20.11e ) \n" % (gb.real, gb.imag))
        f.write("(%20.11e,%20.11e ) \n" % (gb_bar.real, gb_bar.imag))

        zetas = numpy.array([atom[0]["Zatom"], atom[-1]["Zatom"]])
        for zeta in zetas:
            xx01 = 1e0 / 2e0 / dspacing
            xx00 = xx01 - 0.1
            xx02 = xx01 + 0.1
            yy00 = xraylib.FF_Rayl(int(zeta), xx00)
            yy01 = xraylib.FF_Rayl(int(zeta), xx01)
            yy02 = xraylib.FF_Rayl(int(zeta), xx02)
            xx = numpy.array([xx00, xx01, xx02])
            yy = numpy.array([yy00, yy01, yy02])
            fit = numpy.polyfit(xx, yy, 2)
            #print "zeta: ",zeta
            #print "z,xx,YY: ",zeta,xx,yy
            #print "fit: ",fit[::-1] # reversed coeffs
            #print "fit-tuple: ",(tuple(fit[::-1].tolist())) # reversed coeffs
            #print("fit-tuple: %e %e %e  \n" % (tuple(fit[::-1].tolist())) ) # reversed coeffs
            f.write("%e %e %e  \n" %
                    (tuple(fit[::-1].tolist())))  # reversed coeffs

        npoint = int((emax - emin) / estep + 1)
        f.write(("%i \n") % npoint)
        for i in range(npoint):
            energy = (emin + estep * i)
            f1a = xraylib.Fi(int(zetas[0]), energy * 1e-3)
            f2a = xraylib.Fii(int(zetas[0]), energy * 1e-3)
            f1b = xraylib.Fi(int(zetas[1]), energy * 1e-3)
            f2b = xraylib.Fii(int(zetas[1]), energy * 1e-3)
            out = numpy.array([energy, f1a, abs(f2a), f1b, abs(f2b)])
            f.write(("%20.11e %20.11e %20.11e \n %20.11e %20.11e \n") %
                    (tuple(out.tolist())))

        f.close()
        print("File written to disk: %s" % fileout)
def bragg():
    """
     SHADOW preprocessor for crystals - python+xraylib version

     -""" 
    # retrieve physical constants needed
    codata = scipy.constants.codata.physical_constants
    codata_e2_mc2, tmp1, tmp2 = codata["classical electron radius"]
    # or, hard-code them
    # In [179]: print("codata_e2_mc2 = %20.11e \n" % codata_e2_mc2 )
    # codata_e2_mc2 =    2.81794032500e-15

    print("bragg: SHADOW preprocessor for crystals - python+xraylib version")
    fileout = raw_input("Name of output file : ")
    f = open(fileout, 'wb')

    print(" bragg (python) only works now for ZincBlende Cubic structures. ")
    print(" Valid descriptor are: ")
    print("     Si (alternatiovely Si_NIST, Si2) ")
    print("     Ge")
    print("     Diamond")
    print("     GaAs, GaSb, GaP")
    print("     InAs, InP, InSb")
    print("     SiC")

    descriptor = raw_input("Name of crystal descriptor : ")
    cryst = xraylib.Crystal_GetCrystal(descriptor)

    #test crystal data - not needed
    itest = 1
    if itest: 
        if (cryst == None):
            sys.exit(1)
        print "  Unit cell dimensions are %f %f %f" % (cryst['a'],cryst['b'],cryst['c'])
        print "  Unit cell angles are %f %f %f" % (cryst['alpha'],cryst['beta'],cryst['gamma'])
        volume = cryst['volume']
        print "  Unit cell volume is %f" % volume
        volume = volume*1e-8*1e-8*1e-8 # in cm^3
        print "  Atoms at:"
        print "     Z  fraction    X        Y        Z"
        for i in range(cryst['n_atom']):
            atom =  cryst['atom'][i]
            print "    %3i %f %f %f %f" % (atom['Zatom'], atom['fraction'], atom['x'], atom['y'], atom['z'])
        print "  "

    print("Miller indices of crystal plane of reflection.")
    miller = raw_input("H K L: ")
    miller = miller.split()
    hh = int(miller[0])
    kk = int(miller[1])
    ll = int(miller[2])

    #flag ZincBlende
    f.write( "%i " % 0) 
    #1/V*electronRadius
    f.write( "%e " % ((1e0/volume)*(codata_e2_mc2*1e2)) ) 
    #dspacing
    dspacing = xraylib.Crystal_dSpacing(cryst, hh, kk, ll)
    f.write( "%e " % (dspacing*1e-8) ) 
    f.write( "\n")
    #Z's
    atom =  cryst['atom']
    f.write( "%i " % atom[0]["Zatom"] )
    f.write( "%i " % atom[7]["Zatom"] )
    temper = raw_input("Temperature (Debye-Waller) factor (set 1 for default): ")
    temper = float(temper)
    f.write( "%e " % temper ) # temperature parameter
    f.write( "\n")

    ga = (1e0+0j) + cmath.exp(1j*cmath.pi*(hh+kk))  \
                             + cmath.exp(1j*cmath.pi*(hh+ll))  \
                             + cmath.exp(1j*cmath.pi*(kk+ll))
    gb = ga * cmath.exp(1j*cmath.pi*0.5*(hh+kk+ll))
    ga_bar = ga.conjugate()
    gb_bar = gb.conjugate()


    f.write( "(%20.11e,%20.11e ) \n" % (ga.real, ga.imag) ) 
    f.write( "(%20.11e,%20.11e ) \n" % (ga_bar.real, ga_bar.imag) ) 
    f.write( "(%20.11e,%20.11e ) \n" % (gb.real, gb.imag) ) 
    f.write( "(%20.11e,%20.11e ) \n" % (gb_bar.real, gb_bar.imag) ) 

    zetas = numpy.array([atom[0]["Zatom"],atom[7]["Zatom"]])
    for zeta in zetas:
        xx01 = 1e0/2e0/dspacing
        xx00 = xx01-0.1
        xx02 = xx01+0.1
        yy00= xraylib.FF_Rayl(int(zeta),xx00)
        yy01= xraylib.FF_Rayl(int(zeta),xx01)
        yy02= xraylib.FF_Rayl(int(zeta),xx02)
        xx = numpy.array([xx00,xx01,xx02])
        yy = numpy.array([yy00,yy01,yy02])
        fit = numpy.polyfit(xx,yy,2)
        #print "zeta: ",zeta
        #print "z,xx,YY: ",zeta,xx,yy
        #print "fit: ",fit[::-1] # reversed coeffs
        #print "fit-tuple: ",(tuple(fit[::-1].tolist())) # reversed coeffs
        #print("fit-tuple: %e %e %e  \n" % (tuple(fit[::-1].tolist())) ) # reversed coeffs
        f.write("%e %e %e  \n" % (tuple(fit[::-1].tolist())) ) # reversed coeffs


    emin = raw_input("minimum photon energy (eV): ")
    emin = float(emin)
    emax = raw_input("maximum photon energy (eV): ")
    emax = float(emax)
    estep = raw_input("energy step (eV): ")
    estep = float(estep)

    npoint  = int( (emax - emin)/estep + 1 )
    f.write( ("%i \n") % npoint)
    for i in range(npoint): 
        energy = (emin+estep*i)
        f1a = xraylib.Fi(int(zetas[0]),energy*1e-3)
        f2a = xraylib.Fii(int(zetas[0]),energy*1e-3)
        f1b = xraylib.Fi(int(zetas[1]),energy*1e-3)
        f2b = xraylib.Fii(int(zetas[1]),energy*1e-3)
        out = numpy.array([energy,f1a,abs(f2a),f1b,abs(f2b)])
        f.write( ("%20.11e %20.11e %20.11e \n %20.11e %20.11e \n") % ( tuple(out.tolist()) ) )

    f.close()
    print("File written to disk: %s" % fileout)
    return None
Example #6
0
print("CS2 Refractive Index at 10.0 keV : {} - {} i".format(xraylib.Refractive_Index_Re("CS2",10.0,1.261), xraylib.Refractive_Index_Im("CS2",10.0,1.261)))
print("C16H14O3 Refractive Index at 1 keV : {} - {} i".format(xraylib.Refractive_Index_Re("C16H14O3",1.0,1.2), xraylib.Refractive_Index_Im("C16H14O3",1.0,1.2)))
print("SiO2 Refractive Index at 5 keV : {} - {} i".format(xraylib.Refractive_Index_Re("SiO2",5.0,2.65), xraylib.Refractive_Index_Im("SiO2",5.0,2.65)))
print("Compton profile for Fe at pz = 1.1 : {}".format(xraylib.ComptonProfile(26,1.1)))
print("M5 Compton profile for Fe at pz = 1.1 : {}".format(xraylib.ComptonProfile_Partial(26, xraylib.M5_SHELL,1.1)))
print("M1->M5 Coster-Kronig transition probability for Au : {}".format(xraylib.CosKronTransProb(79, xraylib.FM15_TRANS)))
print("L1->L3 Coster-Kronig transition probability for Fe : {}".format(xraylib.CosKronTransProb(26, xraylib.FL13_TRANS)))
print("Au Ma1 XRF production cs at 10.0 keV (Kissel): {}".format(xraylib.CS_FluorLine_Kissel(79, xraylib.MA1_LINE,10.0)))
print("Au Mb XRF production cs at 10.0 keV (Kissel): {}".format(xraylib.CS_FluorLine_Kissel(79, xraylib.MB_LINE,10.0)))
print("Au Mg XRF production cs at 10.0 keV (Kissel): {}".format(xraylib.CS_FluorLine_Kissel(79, xraylib.MG_LINE,10.0)))
print("K atomic level width for Fe: {}".format(xraylib.AtomicLevelWidth(26, xraylib.K_SHELL)))
print("Bi L2-M5M5 Auger non-radiative rate: {}".format(xraylib.AugerRate(86, xraylib.L2_M5M5_AUGER)))
print("Bi L3 Auger yield: {}".format(xraylib.AugerYield(86, xraylib.L3_SHELL)))

print("Sr anomalous scattering factor Fi at 10.0 keV: {}".format(xraylib.Fi(38, 10.0)))
print("Sr anomalous scattering factor Fii at 10.0 keV: {}".format(xraylib.Fii(38, 10.0)))

symbol = xraylib.AtomicNumberToSymbol(26)
print("Symbol of element 26 is: {}".format(symbol))
print("Number of element Fe is: {}".format(xraylib.SymbolToAtomicNumber("Fe")))
Z = np.array([26])
symbol = xraylib.AtomicNumberToSymbol(Z[0])
print("Symbol of element 26 is: {}".format(symbol))
print("Pb Malpha XRF production cs at 20.0 keV with cascade effect: {}".format(xraylib.CS_FluorLine_Kissel(82, xraylib.MA1_LINE,20.0)))
print("Pb Malpha XRF production cs at 20.0 keV with radiative cascade effect: {}".format(xraylib.CS_FluorLine_Kissel_Radiative_Cascade(82, xraylib.MA1_LINE,20.0)))
print("Pb Malpha XRF production cs at 20.0 keV with non-radiative cascade effect: {}".format(xraylib.CS_FluorLine_Kissel_Nonradiative_Cascade(82, xraylib.MA1_LINE,20.0)))
print("Pb Malpha XRF production cs at 20.0 keV without cascade effect: {}".format(xraylib.CS_FluorLine_Kissel_no_Cascade(82, xraylib.MA1_LINE,20.0)))
print("Al mass energy-absorption cs at 20.0 keV: {}".format(xraylib.CS_Energy(13, 20.0)))
print("Pb mass energy-absorption cs at 40.0 keV: {}".format(xraylib.CS_Energy(82, 40.0)))
print("CdTe mass energy-absorption cs at 40.0 keV: {}".format(xraylib.CS_Energy_CP("CdTe", 40.0)))
Example #7
0
print("L1->L3 Coster-Kronig transition probability for Fe : {}".format(
    xraylib.CosKronTransProb(26, xraylib.FL13_TRANS)))
print("Au Ma1 XRF production cs at 10.0 keV (Kissel): {}".format(
    xraylib.CS_FluorLine_Kissel(79, xraylib.MA1_LINE, 10.0)))
print("Au Mb XRF production cs at 10.0 keV (Kissel): {}".format(
    xraylib.CS_FluorLine_Kissel(79, xraylib.MB_LINE, 10.0)))
print("Au Mg XRF production cs at 10.0 keV (Kissel): {}".format(
    xraylib.CS_FluorLine_Kissel(79, xraylib.MG_LINE, 10.0)))
print("K atomic level width for Fe: {}".format(
    xraylib.AtomicLevelWidth(26, xraylib.K_SHELL)))
print("Bi L2-M5M5 Auger non-radiative rate: {}".format(
    xraylib.AugerRate(86, xraylib.L2_M5M5_AUGER)))
print("Bi L3 Auger yield: {}".format(xraylib.AugerYield(86, xraylib.L3_SHELL)))

print("Sr anomalous scattering factor Fi at 10.0 keV: {}".format(
    xraylib.Fi(38, 10.0)))
print("Sr anomalous scattering factor Fii at 10.0 keV: {}".format(
    xraylib.Fii(38, 10.0)))

symbol = xraylib.AtomicNumberToSymbol(26)
print("Symbol of element 26 is: {}".format(symbol))
print("Number of element Fe is: {}".format(xraylib.SymbolToAtomicNumber("Fe")))
Z = np.array([26])
symbol = xraylib.AtomicNumberToSymbol(Z[0])
print("Symbol of element 26 is: {}".format(symbol))
print("Pb Malpha XRF production cs at 20.0 keV with cascade effect: {}".format(
    xraylib.CS_FluorLine_Kissel(82, xraylib.MA1_LINE, 20.0)))
print(
    "Pb Malpha XRF production cs at 20.0 keV with radiative cascade effect: {}"
    .format(
        xraylib.CS_FluorLine_Kissel_Radiative_Cascade(82, xraylib.MA1_LINE,
Example #8
0
    def test_refractiveindex(self):
        density = float(2.328)
        c = compoundfromformula.CompoundFromFormula("Si", density, name="silicon")
        energy = np.asarray([5, 10], dtype=float)
        Z = 14

        # Xraylib (TODO: n_im sign is wrong!)
        n_re0 = np.asarray(
            [xraylib.Refractive_Index_Re("Si", e, density) for e in energy]
        )
        n_im0 = -np.asarray(
            [xraylib.Refractive_Index_Im("Si", e, density) for e in energy]
        )

        # Exactly like Xraylib (TODO: CS_Total -> CS_Photo_Total)
        delta = (
            density
            * 4.15179082788e-4
            * (Z + np.asarray([xraylib.Fi(Z, e) for e in energy]))
            / xraylib.AtomicWeight(Z)
            / energy ** 2
        )
        beta = (
            np.asarray([xraylib.CS_Total(Z, e) for e in energy])
            * density
            * 9.8663479e-9
            / energy
        )
        n_re1 = 1 - delta
        n_im1 = -beta

        np.testing.assert_allclose(n_re0, n_re1)
        np.testing.assert_allclose(n_im0, n_im1)

        # Im: Kissel
        n_im1b = (
            -np.asarray([xraylib.CS_Total_Kissel(Z, e) for e in energy])
            * density
            * 9.8663479e-9
            / energy
        )

        # Im: Kissel with pint
        n_im1d = (
            ureg.Quantity(c.mass_att_coeff(energy), "cm^2/g")
            * ureg.Quantity(c.density, "g/cm^3")
            * ureg.Quantity(energy, "keV").to("cm", "spectroscopy")
            / (4 * np.pi)
        )
        n_im1d = -n_im1d.to("dimensionless").magnitude

        r = n_im1b / n_im1d
        np.testing.assert_allclose(r, r[0])

        # Im: other formula
        n_im1c = (
            density
            * 4.15179082788e-4
            * (np.asarray([xraylib.Fii(Z, e) for e in energy]))
            / xraylib.AtomicWeight(Z)
            / energy ** 2
        )

        # Spectrocrunch
        n_re2 = c.refractive_index_real(energy)
        n_im2 = c.refractive_index_imag(energy)

        np.testing.assert_allclose(n_re2, n_re0)
        np.testing.assert_allclose(n_im2, n_im1c, rtol=1e-6)